|
1 | | -// npx jest webview-ui/src/context/__tests__/ExtensionStateContext.test.tsx |
| 1 | +// cd webview-ui && npx jest src/context/__tests__/ExtensionStateContext.test.tsx |
2 | 2 |
|
3 | 3 | import { render, screen, act } from "@testing-library/react" |
4 | 4 |
|
@@ -26,6 +26,24 @@ const TestComponent = () => { |
26 | 26 | ) |
27 | 27 | } |
28 | 28 |
|
| 29 | +// Test component for API configuration |
| 30 | +const ApiConfigTestComponent = () => { |
| 31 | + const { apiConfiguration, setApiConfiguration } = useExtensionState() |
| 32 | + return ( |
| 33 | + <div> |
| 34 | + <div data-testid="api-configuration">{JSON.stringify(apiConfiguration)}</div> |
| 35 | + <button |
| 36 | + data-testid="update-api-config-button" |
| 37 | + onClick={() => setApiConfiguration({ apiModelId: "new-model", apiProvider: "anthropic" })}> |
| 38 | + Update API Config |
| 39 | + </button> |
| 40 | + <button data-testid="partial-update-button" onClick={() => setApiConfiguration({ modelTemperature: 0.7 })}> |
| 41 | + Partial Update |
| 42 | + </button> |
| 43 | + </div> |
| 44 | + ) |
| 45 | +} |
| 46 | + |
29 | 47 | describe("ExtensionStateContext", () => { |
30 | 48 | it("initializes with empty allowedCommands array", () => { |
31 | 49 | render( |
@@ -96,6 +114,70 @@ describe("ExtensionStateContext", () => { |
96 | 114 |
|
97 | 115 | consoleSpy.mockRestore() |
98 | 116 | }) |
| 117 | + |
| 118 | + it("updates apiConfiguration through setApiConfiguration", () => { |
| 119 | + render( |
| 120 | + <ExtensionStateContextProvider> |
| 121 | + <ApiConfigTestComponent /> |
| 122 | + </ExtensionStateContextProvider>, |
| 123 | + ) |
| 124 | + |
| 125 | + const initialContent = screen.getByTestId("api-configuration").textContent! |
| 126 | + expect(initialContent).toBeDefined() |
| 127 | + |
| 128 | + act(() => { |
| 129 | + screen.getByTestId("update-api-config-button").click() |
| 130 | + }) |
| 131 | + |
| 132 | + const updatedContent = screen.getByTestId("api-configuration").textContent! |
| 133 | + const updatedConfig = JSON.parse(updatedContent || "{}") |
| 134 | + |
| 135 | + expect(updatedConfig).toEqual( |
| 136 | + expect.objectContaining({ |
| 137 | + apiModelId: "new-model", |
| 138 | + apiProvider: "anthropic", |
| 139 | + }), |
| 140 | + ) |
| 141 | + }) |
| 142 | + |
| 143 | + it("correctly merges partial updates to apiConfiguration", () => { |
| 144 | + render( |
| 145 | + <ExtensionStateContextProvider> |
| 146 | + <ApiConfigTestComponent /> |
| 147 | + </ExtensionStateContextProvider>, |
| 148 | + ) |
| 149 | + |
| 150 | + // First set the initial configuration |
| 151 | + act(() => { |
| 152 | + screen.getByTestId("update-api-config-button").click() |
| 153 | + }) |
| 154 | + |
| 155 | + // Verify initial update |
| 156 | + const initialContent = screen.getByTestId("api-configuration").textContent! |
| 157 | + const initialConfig = JSON.parse(initialContent || "{}") |
| 158 | + expect(initialConfig).toEqual( |
| 159 | + expect.objectContaining({ |
| 160 | + apiModelId: "new-model", |
| 161 | + apiProvider: "anthropic", |
| 162 | + }), |
| 163 | + ) |
| 164 | + |
| 165 | + // Now perform a partial update |
| 166 | + act(() => { |
| 167 | + screen.getByTestId("partial-update-button").click() |
| 168 | + }) |
| 169 | + |
| 170 | + // Verify that the partial update was merged with the existing configuration |
| 171 | + const updatedContent = screen.getByTestId("api-configuration").textContent! |
| 172 | + const updatedConfig = JSON.parse(updatedContent || "{}") |
| 173 | + expect(updatedConfig).toEqual( |
| 174 | + expect.objectContaining({ |
| 175 | + apiModelId: "new-model", // Should retain this from previous update |
| 176 | + apiProvider: "anthropic", // Should retain this from previous update |
| 177 | + modelTemperature: 0.7, // Should add this from partial update |
| 178 | + }), |
| 179 | + ) |
| 180 | + }) |
99 | 181 | }) |
100 | 182 |
|
101 | 183 | describe("mergeExtensionState", () => { |
@@ -125,19 +207,35 @@ describe("mergeExtensionState", () => { |
125 | 207 | const prevState: ExtensionState = { |
126 | 208 | ...baseState, |
127 | 209 | apiConfiguration: { modelMaxTokens: 1234, modelMaxThinkingTokens: 123 }, |
| 210 | + experiments: { |
| 211 | + experimentalDiffStrategy: true, |
| 212 | + search_and_replace: true, |
| 213 | + insert_content: true, |
| 214 | + } as Record<ExperimentId, boolean>, |
128 | 215 | } |
129 | 216 |
|
130 | 217 | const newState: ExtensionState = { |
131 | 218 | ...baseState, |
132 | 219 | apiConfiguration: { modelMaxThinkingTokens: 456, modelTemperature: 0.3 }, |
| 220 | + experiments: { |
| 221 | + powerSteering: true, |
| 222 | + multi_search_and_replace: true, |
| 223 | + } as Record<ExperimentId, boolean>, |
133 | 224 | } |
134 | 225 |
|
135 | 226 | const result = mergeExtensionState(prevState, newState) |
136 | 227 |
|
137 | 228 | expect(result.apiConfiguration).toEqual({ |
138 | | - modelMaxTokens: 1234, |
139 | 229 | modelMaxThinkingTokens: 456, |
140 | 230 | modelTemperature: 0.3, |
141 | 231 | }) |
| 232 | + |
| 233 | + expect(result.experiments).toEqual({ |
| 234 | + experimentalDiffStrategy: true, |
| 235 | + search_and_replace: true, |
| 236 | + insert_content: true, |
| 237 | + powerSteering: true, |
| 238 | + multi_search_and_replace: true, |
| 239 | + }) |
142 | 240 | }) |
143 | 241 | }) |
0 commit comments