Skip to content

Commit e0052db

Browse files
committed
fix(api): sync configuration with provider local state
1 parent 6655bb1 commit e0052db

2 files changed

Lines changed: 104 additions & 1 deletion

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { describe, it, expect, vi, beforeEach } from "vitest"
2+
import * as vscode from "vscode"
3+
4+
import { API } from "../api"
5+
import { type RooCodeSettings } from "@roo-code/types"
6+
import { ClineProvider } from "../../core/webview/ClineProvider"
7+
8+
vi.mock("vscode")
9+
vi.mock("../../core/webview/ClineProvider")
10+
11+
describe("API - setConfiguration", () => {
12+
let api: API
13+
let mockOutputChannel: vscode.OutputChannel
14+
let mockProvider: ClineProvider
15+
let contextValues: RooCodeSettings
16+
let viewLocalState: { apiConfiguration?: RooCodeSettings }
17+
let mockContextProxySetValues: ReturnType<typeof vi.fn<(values: RooCodeSettings) => Promise<void>>>
18+
let mockProviderSetValues: ReturnType<typeof vi.fn<(values: RooCodeSettings) => Promise<void>>>
19+
let mockSaveConfig: ReturnType<typeof vi.fn<(name: string, values: RooCodeSettings) => Promise<string>>>
20+
let mockPostStateToWebview: ReturnType<typeof vi.fn<() => Promise<void>>>
21+
22+
beforeEach(() => {
23+
mockOutputChannel = {
24+
appendLine: vi.fn(),
25+
} as unknown as vscode.OutputChannel
26+
27+
contextValues = {
28+
currentApiConfigName: "default",
29+
apiProvider: "deepseek",
30+
deepSeekBaseUrl: "http://localhost:3000/deepseek",
31+
apiModelId: "deepseek-v4-pro",
32+
}
33+
34+
viewLocalState = {
35+
apiConfiguration: {
36+
apiProvider: "openrouter",
37+
openRouterBaseUrl: "http://localhost:3000/openrouter",
38+
openRouterModelId: "openrouter/old-model",
39+
},
40+
}
41+
42+
mockContextProxySetValues = vi.fn().mockImplementation(async (values: RooCodeSettings) => {
43+
contextValues = {
44+
...contextValues,
45+
...values,
46+
}
47+
})
48+
49+
mockProviderSetValues = vi
50+
.fn<(values: RooCodeSettings) => Promise<void>>()
51+
.mockImplementation(async (values: RooCodeSettings) => {
52+
await mockContextProxySetValues(values)
53+
viewLocalState.apiConfiguration = values
54+
})
55+
56+
mockSaveConfig = vi
57+
.fn<(name: string, values: RooCodeSettings) => Promise<string>>()
58+
.mockResolvedValue("test-id")
59+
mockPostStateToWebview = vi.fn<() => Promise<void>>().mockResolvedValue(undefined)
60+
61+
mockProvider = {
62+
context: {} as vscode.ExtensionContext,
63+
contextProxy: {
64+
setValues: mockContextProxySetValues,
65+
},
66+
providerSettingsManager: {
67+
saveConfig: mockSaveConfig,
68+
},
69+
setValues: mockProviderSetValues,
70+
postStateToWebview: mockPostStateToWebview,
71+
getState: vi.fn().mockImplementation(async () => ({
72+
apiConfiguration: {
73+
...contextValues,
74+
...viewLocalState.apiConfiguration,
75+
},
76+
})),
77+
on: vi.fn(),
78+
getCurrentTaskStack: vi.fn().mockReturnValue([]),
79+
viewLaunched: true,
80+
} as unknown as ClineProvider
81+
82+
api = new API(mockOutputChannel, mockProvider)
83+
})
84+
85+
it("syncs sidebar provider view-local API configuration so getState reflects the new provider", async () => {
86+
const newConfiguration: RooCodeSettings = {
87+
currentApiConfigName: "deepseek-v4-pro",
88+
apiProvider: "deepseek",
89+
deepSeekBaseUrl: "http://localhost:3000/deepseek",
90+
apiModelId: "deepseek-v4-pro",
91+
}
92+
93+
await api.setConfiguration(newConfiguration)
94+
95+
const state = await mockProvider.getState()
96+
97+
expect(state.apiConfiguration.apiProvider).toBe("deepseek")
98+
expect(state.apiConfiguration.deepSeekBaseUrl).toBe("http://localhost:3000/deepseek")
99+
expect(mockProviderSetValues).toHaveBeenCalledWith(newConfiguration)
100+
expect(mockSaveConfig).toHaveBeenCalledWith("deepseek-v4-pro", newConfiguration)
101+
expect(mockPostStateToWebview).toHaveBeenCalled()
102+
})
103+
})

src/extension/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI {
499499
}
500500

501501
public async setConfiguration(values: RooCodeSettings) {
502-
await this.sidebarProvider.contextProxy.setValues(values)
502+
await this.sidebarProvider.setValues(values)
503503
await this.sidebarProvider.providerSettingsManager.saveConfig(values.currentApiConfigName || "default", values)
504504
await this.sidebarProvider.postStateToWebview()
505505
}

0 commit comments

Comments
 (0)