|
| 1 | +// npx vitest run core/webview/__tests__/webviewMessageHandler.workspaceModeApiConfig.spec.ts |
| 2 | + |
| 3 | +import { webviewMessageHandler } from "../webviewMessageHandler" |
| 4 | +import type { ClineProvider } from "../ClineProvider" |
| 5 | + |
| 6 | +describe("webviewMessageHandler - setWorkspaceModeApiConfig", () => { |
| 7 | + let mockProvider: { |
| 8 | + context: { |
| 9 | + workspaceState: { |
| 10 | + get: ReturnType<typeof vi.fn> |
| 11 | + update: ReturnType<typeof vi.fn> |
| 12 | + } |
| 13 | + } |
| 14 | + getState: ReturnType<typeof vi.fn> |
| 15 | + postStateToWebview: ReturnType<typeof vi.fn> |
| 16 | + providerSettingsManager: { |
| 17 | + setModeConfig: ReturnType<typeof vi.fn> |
| 18 | + } |
| 19 | + postMessageToWebview: ReturnType<typeof vi.fn> |
| 20 | + getCurrentTask: ReturnType<typeof vi.fn> |
| 21 | + } |
| 22 | + |
| 23 | + let workspaceStateStore: Record<string, unknown> |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + vi.clearAllMocks() |
| 27 | + |
| 28 | + workspaceStateStore = {} |
| 29 | + |
| 30 | + mockProvider = { |
| 31 | + context: { |
| 32 | + workspaceState: { |
| 33 | + get: vi.fn().mockImplementation((key: string, defaultValue?: unknown) => { |
| 34 | + return key in workspaceStateStore ? workspaceStateStore[key] : defaultValue |
| 35 | + }), |
| 36 | + update: vi.fn().mockImplementation((key: string, value: unknown) => { |
| 37 | + workspaceStateStore[key] = value |
| 38 | + return Promise.resolve() |
| 39 | + }), |
| 40 | + }, |
| 41 | + }, |
| 42 | + getState: vi.fn().mockResolvedValue({ |
| 43 | + currentApiConfigName: "test-config", |
| 44 | + listApiConfigMeta: [{ name: "test-config", id: "config-123" }], |
| 45 | + customModes: [], |
| 46 | + }), |
| 47 | + postStateToWebview: vi.fn(), |
| 48 | + providerSettingsManager: { |
| 49 | + setModeConfig: vi.fn(), |
| 50 | + }, |
| 51 | + postMessageToWebview: vi.fn(), |
| 52 | + getCurrentTask: vi.fn(), |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + it("sets a workspace mode API config for a specific mode", async () => { |
| 57 | + await webviewMessageHandler(mockProvider as unknown as ClineProvider, { |
| 58 | + type: "setWorkspaceModeApiConfig", |
| 59 | + mode: "code", |
| 60 | + text: "config-123", |
| 61 | + }) |
| 62 | + |
| 63 | + expect(mockProvider.context.workspaceState.update).toHaveBeenCalledWith("workspaceModeApiConfigs", { |
| 64 | + code: "config-123", |
| 65 | + }) |
| 66 | + expect(mockProvider.postStateToWebview).toHaveBeenCalled() |
| 67 | + }) |
| 68 | + |
| 69 | + it("clears a workspace mode API config when text is undefined", async () => { |
| 70 | + // Pre-populate with an existing mapping |
| 71 | + workspaceStateStore["workspaceModeApiConfigs"] = { code: "config-123", architect: "config-456" } |
| 72 | + |
| 73 | + await webviewMessageHandler(mockProvider as unknown as ClineProvider, { |
| 74 | + type: "setWorkspaceModeApiConfig", |
| 75 | + mode: "code", |
| 76 | + // text is undefined - clears the override |
| 77 | + }) |
| 78 | + |
| 79 | + expect(mockProvider.context.workspaceState.update).toHaveBeenCalledWith("workspaceModeApiConfigs", { |
| 80 | + architect: "config-456", |
| 81 | + }) |
| 82 | + expect(mockProvider.postStateToWebview).toHaveBeenCalled() |
| 83 | + }) |
| 84 | + |
| 85 | + it("preserves existing workspace configs when adding a new one", async () => { |
| 86 | + workspaceStateStore["workspaceModeApiConfigs"] = { architect: "config-456" } |
| 87 | + |
| 88 | + await webviewMessageHandler(mockProvider as unknown as ClineProvider, { |
| 89 | + type: "setWorkspaceModeApiConfig", |
| 90 | + mode: "code", |
| 91 | + text: "config-789", |
| 92 | + }) |
| 93 | + |
| 94 | + expect(mockProvider.context.workspaceState.update).toHaveBeenCalledWith("workspaceModeApiConfigs", { |
| 95 | + architect: "config-456", |
| 96 | + code: "config-789", |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + it("does nothing if mode is not provided", async () => { |
| 101 | + await webviewMessageHandler(mockProvider as unknown as ClineProvider, { |
| 102 | + type: "setWorkspaceModeApiConfig", |
| 103 | + // mode is undefined |
| 104 | + text: "config-123", |
| 105 | + }) |
| 106 | + |
| 107 | + expect(mockProvider.context.workspaceState.update).not.toHaveBeenCalled() |
| 108 | + }) |
| 109 | +}) |
| 110 | + |
| 111 | +describe("webviewMessageHandler - clearWorkspaceModeApiConfig", () => { |
| 112 | + let mockProvider: { |
| 113 | + context: { |
| 114 | + workspaceState: { |
| 115 | + get: ReturnType<typeof vi.fn> |
| 116 | + update: ReturnType<typeof vi.fn> |
| 117 | + } |
| 118 | + } |
| 119 | + getState: ReturnType<typeof vi.fn> |
| 120 | + postStateToWebview: ReturnType<typeof vi.fn> |
| 121 | + providerSettingsManager: { |
| 122 | + setModeConfig: ReturnType<typeof vi.fn> |
| 123 | + } |
| 124 | + postMessageToWebview: ReturnType<typeof vi.fn> |
| 125 | + getCurrentTask: ReturnType<typeof vi.fn> |
| 126 | + } |
| 127 | + |
| 128 | + beforeEach(() => { |
| 129 | + vi.clearAllMocks() |
| 130 | + |
| 131 | + mockProvider = { |
| 132 | + context: { |
| 133 | + workspaceState: { |
| 134 | + get: vi.fn(), |
| 135 | + update: vi.fn().mockResolvedValue(undefined), |
| 136 | + }, |
| 137 | + }, |
| 138 | + getState: vi.fn().mockResolvedValue({ |
| 139 | + currentApiConfigName: "test-config", |
| 140 | + listApiConfigMeta: [{ name: "test-config", id: "config-123" }], |
| 141 | + customModes: [], |
| 142 | + }), |
| 143 | + postStateToWebview: vi.fn(), |
| 144 | + providerSettingsManager: { |
| 145 | + setModeConfig: vi.fn(), |
| 146 | + }, |
| 147 | + postMessageToWebview: vi.fn(), |
| 148 | + getCurrentTask: vi.fn(), |
| 149 | + } |
| 150 | + }) |
| 151 | + |
| 152 | + it("clears all workspace mode API configs", async () => { |
| 153 | + await webviewMessageHandler(mockProvider as unknown as ClineProvider, { |
| 154 | + type: "clearWorkspaceModeApiConfig", |
| 155 | + }) |
| 156 | + |
| 157 | + expect(mockProvider.context.workspaceState.update).toHaveBeenCalledWith("workspaceModeApiConfigs", {}) |
| 158 | + expect(mockProvider.postStateToWebview).toHaveBeenCalled() |
| 159 | + }) |
| 160 | +}) |
0 commit comments