This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathExtensionStateContext.spec.tsx
More file actions
260 lines (223 loc) · 7.53 KB
/
Copy pathExtensionStateContext.spec.tsx
File metadata and controls
260 lines (223 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import { render, screen, act } from "@/utils/test-utils"
import {
type ProviderSettings,
type ExperimentId,
type ExtensionState,
DEFAULT_CHECKPOINT_TIMEOUT_SECONDS,
} from "@roo-code/types"
import { ExtensionStateContextProvider, useExtensionState, mergeExtensionState } from "../ExtensionStateContext"
const TestComponent = () => {
const { allowedCommands, setAllowedCommands, soundEnabled, showRooIgnoredFiles, setShowRooIgnoredFiles } =
useExtensionState()
return (
<div>
<div data-testid="allowed-commands">{JSON.stringify(allowedCommands)}</div>
<div data-testid="sound-enabled">{JSON.stringify(soundEnabled)}</div>
<div data-testid="show-rooignored-files">{JSON.stringify(showRooIgnoredFiles)}</div>
<button data-testid="update-button" onClick={() => setAllowedCommands(["npm install", "git status"])}>
Update Commands
</button>
<button data-testid="toggle-rooignore-button" onClick={() => setShowRooIgnoredFiles(!showRooIgnoredFiles)}>
Update Commands
</button>
</div>
)
}
const ApiConfigTestComponent = () => {
const { apiConfiguration, setApiConfiguration } = useExtensionState()
return (
<div>
<div data-testid="api-configuration">{JSON.stringify(apiConfiguration)}</div>
<button
data-testid="update-api-config-button"
onClick={() => setApiConfiguration({ apiModelId: "new-model", apiProvider: "anthropic" })}>
Update API Config
</button>
<button data-testid="partial-update-button" onClick={() => setApiConfiguration({ modelTemperature: 0.7 })}>
Partial Update
</button>
</div>
)
}
describe("ExtensionStateContext", () => {
it("initializes with empty allowedCommands array", () => {
render(
<ExtensionStateContextProvider>
<TestComponent />
</ExtensionStateContextProvider>,
)
expect(JSON.parse(screen.getByTestId("allowed-commands").textContent!)).toEqual([])
})
it("initializes with soundEnabled set to false", () => {
render(
<ExtensionStateContextProvider>
<TestComponent />
</ExtensionStateContextProvider>,
)
expect(JSON.parse(screen.getByTestId("sound-enabled").textContent!)).toBe(false)
})
it("initializes with showRooIgnoredFiles set to true", () => {
render(
<ExtensionStateContextProvider>
<TestComponent />
</ExtensionStateContextProvider>,
)
expect(JSON.parse(screen.getByTestId("show-rooignored-files").textContent!)).toBe(true)
})
it("updates showRooIgnoredFiles through setShowRooIgnoredFiles", () => {
render(
<ExtensionStateContextProvider>
<TestComponent />
</ExtensionStateContextProvider>,
)
act(() => {
screen.getByTestId("toggle-rooignore-button").click()
})
expect(JSON.parse(screen.getByTestId("show-rooignored-files").textContent!)).toBe(false)
})
it("updates allowedCommands through setAllowedCommands", () => {
render(
<ExtensionStateContextProvider>
<TestComponent />
</ExtensionStateContextProvider>,
)
act(() => {
screen.getByTestId("update-button").click()
})
expect(JSON.parse(screen.getByTestId("allowed-commands").textContent!)).toEqual(["npm install", "git status"])
})
it("throws error when used outside provider", () => {
// Suppress console.error for this test since we expect an error
const consoleSpy = vi.spyOn(console, "error")
consoleSpy.mockImplementation(() => {})
expect(() => {
render(<TestComponent />)
}).toThrow("useExtensionState must be used within an ExtensionStateContextProvider")
consoleSpy.mockRestore()
})
it("updates apiConfiguration through setApiConfiguration", () => {
render(
<ExtensionStateContextProvider>
<ApiConfigTestComponent />
</ExtensionStateContextProvider>,
)
const initialContent = screen.getByTestId("api-configuration").textContent!
expect(initialContent).toBeDefined()
act(() => {
screen.getByTestId("update-api-config-button").click()
})
const updatedContent = screen.getByTestId("api-configuration").textContent!
const updatedConfig = JSON.parse(updatedContent || "{}")
expect(updatedConfig).toEqual(
expect.objectContaining({
apiModelId: "new-model",
apiProvider: "anthropic",
}),
)
})
it("correctly merges partial updates to apiConfiguration", () => {
render(
<ExtensionStateContextProvider>
<ApiConfigTestComponent />
</ExtensionStateContextProvider>,
)
// First set the initial configuration
act(() => {
screen.getByTestId("update-api-config-button").click()
})
// Verify initial update
const initialContent = screen.getByTestId("api-configuration").textContent!
const initialConfig = JSON.parse(initialContent || "{}")
expect(initialConfig).toEqual(
expect.objectContaining({
apiModelId: "new-model",
apiProvider: "anthropic",
}),
)
// Now perform a partial update
act(() => {
screen.getByTestId("partial-update-button").click()
})
// Verify that the partial update was merged with the existing configuration
const updatedContent = screen.getByTestId("api-configuration").textContent!
const updatedConfig = JSON.parse(updatedContent || "{}")
expect(updatedConfig).toEqual(
expect.objectContaining({
apiModelId: "new-model", // Should retain this from previous update
apiProvider: "anthropic", // Should retain this from previous update
modelTemperature: 0.7, // Should add this from partial update
}),
)
})
})
describe("mergeExtensionState", () => {
it("should correctly merge extension states", () => {
const baseState: ExtensionState = {
version: "",
mcpEnabled: false,
enableMcpServerCreation: false,
clineMessages: [],
taskHistory: [],
shouldShowAnnouncement: false,
enableCheckpoints: true,
writeDelayMs: 1000,
mode: "default",
experiments: {} as Record<ExperimentId, boolean>,
customModes: [],
maxOpenTabsContext: 20,
maxWorkspaceFiles: 100,
apiConfiguration: { providerId: "openrouter" } as ProviderSettings,
telemetrySetting: "unset",
showRooIgnoredFiles: true,
enableSubfolderRules: false,
renderContext: "sidebar",
maxReadFileLine: 500,
cloudUserInfo: null,
organizationAllowList: { allowAll: true, providers: {} },
autoCondenseContext: true,
autoCondenseContextPercent: 100,
cloudIsAuthenticated: false,
sharingEnabled: false,
publicSharingEnabled: false,
profileThresholds: {},
hasOpenedModeSelector: false, // Add the new required property
maxImageFileSize: 5,
maxTotalImageSize: 20,
remoteControlEnabled: false,
taskSyncEnabled: false,
featureRoomoteControlEnabled: false,
isBrowserSessionActive: false,
checkpointTimeout: DEFAULT_CHECKPOINT_TIMEOUT_SECONDS, // Add the checkpoint timeout property
}
const prevState: ExtensionState = {
...baseState,
apiConfiguration: { modelMaxTokens: 1234, modelMaxThinkingTokens: 123 },
experiments: {} as Record<ExperimentId, boolean>,
checkpointTimeout: DEFAULT_CHECKPOINT_TIMEOUT_SECONDS - 5,
}
const newState: ExtensionState = {
...baseState,
apiConfiguration: { modelMaxThinkingTokens: 456, modelTemperature: 0.3 },
experiments: {
preventFocusDisruption: false,
imageGeneration: false,
runSlashCommand: false,
multipleNativeToolCalls: false,
customTools: false,
} as Record<ExperimentId, boolean>,
checkpointTimeout: DEFAULT_CHECKPOINT_TIMEOUT_SECONDS + 5,
}
const result = mergeExtensionState(prevState, newState)
expect(result.apiConfiguration).toEqual({
modelMaxThinkingTokens: 456,
modelTemperature: 0.3,
})
expect(result.experiments).toEqual({
preventFocusDisruption: false,
imageGeneration: false,
runSlashCommand: false,
multipleNativeToolCalls: false,
customTools: false,
})
})
})