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 pathSettingsView.unsaved-changes.spec.tsx
More file actions
607 lines (536 loc) · 19.2 KB
/
Copy pathSettingsView.unsaved-changes.spec.tsx
File metadata and controls
607 lines (536 loc) · 19.2 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
import { render, screen, fireEvent, waitFor } from "@testing-library/react"
import { vi, describe, it, expect, beforeEach } from "vitest"
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
import React from "react"
import SettingsView from "../SettingsView"
// Mock vscode API
const mockPostMessage = vi.fn()
const mockVscode = {
postMessage: mockPostMessage,
}
;(global as any).acquireVsCodeApi = () => mockVscode
// Mock the extension state context
vi.mock("@src/context/ExtensionStateContext", () => ({
useExtensionState: vi.fn(),
}))
// Mock the translation context
vi.mock("@src/i18n/TranslationContext", () => ({
useAppTranslation: () => ({
t: (key: string) => key,
}),
useTranslation: () => ({
t: (key: string) => key,
}),
}))
// Mock UI components
vi.mock("@src/components/ui", () => ({
ToggleSwitch: ({ checked, onChange, "aria-label": ariaLabel, "data-testid": dataTestId }: any) => (
<button role="switch" aria-checked={checked} aria-label={ariaLabel} data-testid={dataTestId} onClick={onChange}>
Toggle
</button>
),
Input: ({ value, onChange, placeholder, id, type, className, ...props }: any) => (
<input
type={type || "text"}
value={value}
onChange={onChange}
placeholder={placeholder}
id={id}
className={className}
{...props}
/>
),
Textarea: ({ value, onChange, placeholder, id, className, ...props }: any) => (
<textarea
value={value}
onChange={onChange}
placeholder={placeholder}
id={id}
className={className}
{...props}
/>
),
Checkbox: ({ checked, onCheckedChange, id, className, ...props }: any) => (
<input
type="checkbox"
checked={checked}
onChange={(e) => onCheckedChange?.(e.target.checked)}
id={id}
className={className}
{...props}
/>
),
AlertDialog: ({ children }: any) => <div>{children}</div>,
AlertDialogContent: ({ children }: any) => <div>{children}</div>,
AlertDialogTitle: ({ children }: any) => <div>{children}</div>,
AlertDialogDescription: ({ children }: any) => <div>{children}</div>,
AlertDialogCancel: ({ children, onClick }: any) => <button onClick={onClick}>{children}</button>,
AlertDialogAction: ({ children, onClick }: any) => <button onClick={onClick}>{children}</button>,
AlertDialogHeader: ({ children }: any) => <div>{children}</div>,
AlertDialogFooter: ({ children }: any) => <div>{children}</div>,
Button: ({ children, onClick, disabled, ...props }: any) => (
<button onClick={onClick} disabled={disabled} {...props}>
{children}
</button>
),
Tooltip: ({ children }: any) => <>{children}</>,
TooltipContent: ({ children }: any) => <div>{children}</div>,
TooltipProvider: ({ children }: any) => <>{children}</>,
TooltipTrigger: ({ children }: any) => <>{children}</>,
StandardTooltip: ({ children, content }: any) => <div title={content}>{children}</div>,
Popover: ({ children }: any) => <>{children}</>,
PopoverTrigger: ({ children }: any) => <>{children}</>,
PopoverContent: ({ children }: any) => <div>{children}</div>,
Select: ({ children, value, onValueChange }: any) => (
<div data-testid="select" data-value={value}>
<button onClick={() => onValueChange && onValueChange("test-change")}>{value}</button>
{children}
</div>
),
SelectContent: ({ children }: any) => <div data-testid="select-content">{children}</div>,
SelectGroup: ({ children }: any) => <div data-testid="select-group">{children}</div>,
SelectItem: ({ children, value }: any) => (
<div data-testid={`select-item-${value}`} data-value={value}>
{children}
</div>
),
SelectTrigger: ({ children }: any) => <div data-testid="select-trigger">{children}</div>,
SelectValue: ({ placeholder }: any) => <div data-testid="select-value">{placeholder}</div>,
Slider: ({ value, onValueChange, "data-testid": dataTestId }: any) => (
<input
type="range"
value={value?.[0] ?? 0}
onChange={(e) => onValueChange?.([parseFloat(e.target.value)])}
data-testid={dataTestId}
/>
),
SearchableSelect: ({ value, onValueChange, options, placeholder }: any) => (
<select value={value} onChange={(e) => onValueChange(e.target.value)} data-testid="searchable-select">
{placeholder && <option value="">{placeholder}</option>}
{options?.map((opt: any) => (
<option key={opt.value} value={opt.value}>
{opt.label}
</option>
))}
</select>
),
Collapsible: ({ children, open }: any) => (
<div className="collapsible-mock" data-open={open}>
{children}
</div>
),
CollapsibleTrigger: ({ children, className, onClick }: any) => (
<div className={`collapsible-trigger-mock ${className || ""}`} onClick={onClick}>
{children}
</div>
),
CollapsibleContent: ({ children, className }: any) => (
<div className={`collapsible-content-mock ${className || ""}`}>{children}</div>
),
Dialog: ({ children, ...props }: any) => (
<div data-testid="dialog" {...props}>
{children}
</div>
),
DialogContent: ({ children, ...props }: any) => (
<div data-testid="dialog-content" {...props}>
{children}
</div>
),
DialogHeader: ({ children, ...props }: any) => (
<div data-testid="dialog-header" {...props}>
{children}
</div>
),
DialogTitle: ({ children, ...props }: any) => (
<div data-testid="dialog-title" {...props}>
{children}
</div>
),
DialogDescription: ({ children, ...props }: any) => (
<div data-testid="dialog-description" {...props}>
{children}
</div>
),
DialogFooter: ({ children, ...props }: any) => (
<div data-testid="dialog-footer" {...props}>
{children}
</div>
),
}))
// Mock ModesView and McpView since they're rendered during indexing
vi.mock("@src/components/modes/ModesView", () => ({
default: () => null,
}))
vi.mock("@src/components/mcp/McpView", () => ({
default: () => null,
}))
// Mock Tab components
vi.mock("../common/Tab", () => ({
Tab: ({ children }: any) => <div>{children}</div>,
TabContent: React.forwardRef<HTMLDivElement, any>(({ children }, ref) => <div ref={ref}>{children}</div>),
TabHeader: ({ children }: any) => <div>{children}</div>,
TabList: ({ children }: any) => <div data-testid="settings-tab-list">{children}</div>,
TabTrigger: React.forwardRef<HTMLButtonElement, any>(({ children, onClick }, ref) => (
<button ref={ref} onClick={onClick}>
{children}
</button>
)),
}))
// Mock child components that are complex
// Mock ApiConfigManager to not interact with props
vi.mock("../ApiConfigManager", () => ({
default: vi.fn(() => <div data-testid="api-config-manager">ApiConfigManager</div>),
}))
vi.mock("../ApiOptions", () => ({
default: vi.fn(() => <div data-testid="api-options">ApiOptions</div>),
}))
// Mock other settings components - ensure they don't interact with props
vi.mock("../AutoApproveSettings", () => ({
AutoApproveSettings: vi.fn(() => <div>AutoApproveSettings</div>),
}))
vi.mock("../CheckpointSettings", () => ({
CheckpointSettings: vi.fn(() => <div>CheckpointSettings</div>),
}))
vi.mock("../NotificationSettings", () => ({
NotificationSettings: vi.fn(() => <div>NotificationSettings</div>),
}))
vi.mock("../ContextManagementSettings", () => ({
ContextManagementSettings: vi.fn(() => <div>ContextManagementSettings</div>),
}))
vi.mock("../TerminalSettings", () => ({
TerminalSettings: vi.fn(() => <div>TerminalSettings</div>),
}))
vi.mock("../ExperimentalSettings", () => ({
ExperimentalSettings: vi.fn(() => <div>ExperimentalSettings</div>),
}))
vi.mock("../LanguageSettings", () => ({
LanguageSettings: vi.fn(() => <div>LanguageSettings</div>),
}))
vi.mock("../About", () => ({
About: vi.fn(() => <div>About</div>),
}))
vi.mock("../PromptsSettings", () => ({
default: vi.fn(() => <div>PromptsSettings</div>),
}))
vi.mock("../SlashCommandsSettings", () => ({
SlashCommandsSettings: vi.fn(() => <div>SlashCommandsSettings</div>),
}))
vi.mock("../UISettings", () => ({
UISettings: vi.fn(() => <div>UISettings</div>),
}))
vi.mock("../SectionHeader", () => ({
SectionHeader: ({ children }: any) => <div>{children}</div>,
}))
vi.mock("../Section", () => ({
Section: ({ children }: any) => <div>{children}</div>,
}))
vi.mock("../SettingsSearch", () => ({
SettingsSearch: () => null,
}))
import { useExtensionState } from "@src/context/ExtensionStateContext"
import ApiOptions from "../ApiOptions"
describe("SettingsView - Unsaved Changes Detection", () => {
let queryClient: QueryClient
const defaultExtensionState = {
currentApiConfigName: "default",
listApiConfigMeta: [],
uriScheme: "vscode",
settingsImportedAt: undefined,
apiConfiguration: {
apiProvider: "openai",
apiModelId: "", // Empty string initially
},
alwaysAllowReadOnly: false,
alwaysAllowReadOnlyOutsideWorkspace: false,
allowedCommands: [],
deniedCommands: [],
allowedMaxRequests: undefined,
allowedMaxCost: undefined,
language: "en",
alwaysAllowExecute: false,
alwaysAllowMcp: false,
alwaysAllowModeSwitch: false,
alwaysAllowSubtasks: false,
alwaysAllowWrite: false,
alwaysAllowWriteOutsideWorkspace: false,
alwaysAllowWriteProtected: false,
autoCondenseContext: false,
autoCondenseContextPercent: 50,
enableCheckpoints: false,
experiments: {},
maxOpenTabsContext: 10,
maxWorkspaceFiles: 200,
mcpEnabled: false,
soundEnabled: false,
ttsEnabled: false,
ttsSpeed: 1.0,
soundVolume: 0.5,
telemetrySetting: "unset",
terminalOutputLineLimit: 500,
terminalOutputCharacterLimit: 50000,
terminalShellIntegrationTimeout: 3000,
terminalShellIntegrationDisabled: false,
terminalCommandDelay: 0,
terminalPowershellCounter: false,
terminalZshClearEolMark: false,
terminalZshOhMy: false,
terminalZshP10k: false,
terminalZdotdir: false,
writeDelayMs: 0,
showRooIgnoredFiles: false,
maxReadFileLine: -1,
maxImageFileSize: 5,
maxTotalImageSize: 20,
customCondensingPrompt: "",
customSupportPrompts: {},
profileThresholds: {},
alwaysAllowFollowupQuestions: false,
followupAutoApproveTimeoutMs: undefined,
includeDiagnosticMessages: false,
maxDiagnosticMessages: 50,
includeTaskHistoryInEnhance: true,
openRouterImageApiKey: undefined,
openRouterImageGenerationSelectedModel: undefined,
reasoningBlockCollapsed: true,
autoExpandDiffs: false,
}
beforeEach(() => {
vi.clearAllMocks()
// Reset the ApiOptions mock to its default implementation
vi.mocked(ApiOptions).mockImplementation(() => {
// Don't do anything with props, just render a div
return <div data-testid="api-options">ApiOptions</div>
})
queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
mutations: { retry: false },
},
})
;(useExtensionState as any).mockReturnValue(defaultExtensionState)
})
// TODO: Fix underlying issue - dialog appears even when no user changes have been made
// This happens because some component is triggering setCachedStateField during initialization
// without properly marking it as a non-user action
it.skip("should not show unsaved changes when settings are automatically initialized", async () => {
const onDone = vi.fn()
render(
<QueryClientProvider client={queryClient}>
<SettingsView onDone={onDone} />
</QueryClientProvider>,
)
// Wait for the component to render
await waitFor(() => {
expect(screen.getByTestId("api-options")).toBeInTheDocument()
})
// Wait for any async state updates to complete
await waitFor(() => {
const saveButton = screen.getByTestId("save-button") as HTMLButtonElement
expect(saveButton.disabled).toBe(true)
})
// Click the Done button
const doneButton = screen.getByText("settings:common.done")
fireEvent.click(doneButton)
// Should not show unsaved changes dialog - onDone should be called immediately
await waitFor(() => {
expect(onDone).toHaveBeenCalled()
})
// Verify no dialog appeared
expect(screen.queryByText("settings:unsavedChangesDialog.title")).not.toBeInTheDocument()
})
// TODO: Fix underlying issue - see above
it.skip("should not trigger unsaved changes for automatic model initialization", async () => {
const onDone = vi.fn()
// Mock ApiOptions to simulate ModelPicker initialization
vi.mocked(ApiOptions).mockImplementation(({ setApiConfigurationField, apiConfiguration }) => {
const [hasInitialized, setHasInitialized] = React.useState(false)
React.useEffect(() => {
// Only run once and only if not already initialized
if (!hasInitialized && apiConfiguration?.apiModelId === "") {
// Simulate automatic initialization from empty string to a value
setApiConfigurationField("apiModelId", "default-model", false)
setHasInitialized(true)
}
}, [hasInitialized, apiConfiguration?.apiModelId, setApiConfigurationField])
return <div data-testid="api-options">ApiOptions with Init</div>
})
render(
<QueryClientProvider client={queryClient}>
<SettingsView onDone={onDone} />
</QueryClientProvider>,
)
// Wait for the component to render and effects to run
await waitFor(() => {
expect(screen.getByTestId("api-options")).toBeInTheDocument()
})
// Give time for effects to complete
await new Promise((resolve) => setTimeout(resolve, 100))
// Check that save button is disabled (no changes detected)
const saveButton = screen.getByTestId("save-button") as HTMLButtonElement
expect(saveButton.disabled).toBe(true)
// Click the Done button
const doneButton = screen.getByText("settings:common.done")
fireEvent.click(doneButton)
// Should not show unsaved changes dialog
expect(screen.queryByText("settings:unsavedChangesDialog.title")).not.toBeInTheDocument()
// onDone should be called
expect(onDone).toHaveBeenCalled()
})
it("should show unsaved changes when user makes actual changes", async () => {
const onDone = vi.fn()
// Create a custom mock for this test that simulates user interaction
const ApiOptionsWithButton = vi.fn(({ setApiConfigurationField }) => {
const handleUserChange = () => {
// Simulate user action (isUserAction = true by default)
setApiConfigurationField("apiModelId", "user-selected-model")
}
return (
<div data-testid="api-options">
<button onClick={handleUserChange} data-testid="change-model">
Change Model
</button>
</div>
)
})
// Override the mock for this specific test
vi.mocked(ApiOptions).mockImplementation(ApiOptionsWithButton)
render(
<QueryClientProvider client={queryClient}>
<SettingsView onDone={onDone} />
</QueryClientProvider>,
)
// Wait for the component to render
await waitFor(() => {
expect(screen.getByTestId("api-options")).toBeInTheDocument()
})
// Simulate user changing a setting
const changeButton = screen.getByTestId("change-model")
fireEvent.click(changeButton)
// Click the Done button
const doneButton = screen.getByText("settings:common.done")
fireEvent.click(doneButton)
// Should show unsaved changes dialog
await waitFor(() => {
expect(screen.getByText("settings:unsavedChangesDialog.title")).toBeInTheDocument()
})
// onDone should not be called yet
expect(onDone).not.toHaveBeenCalled()
})
// TODO: Fix underlying issue - see above
it.skip("should handle initialization from undefined to value without triggering unsaved changes", async () => {
const onDone = vi.fn()
// Start with undefined apiModelId
const stateWithUndefined = {
...defaultExtensionState,
apiConfiguration: {
apiProvider: "openai",
apiModelId: undefined,
},
}
;(useExtensionState as any).mockReturnValue(stateWithUndefined)
render(
<QueryClientProvider client={queryClient}>
<SettingsView onDone={onDone} />
</QueryClientProvider>,
)
// Wait for initialization
await waitFor(() => {
expect(screen.getByTestId("api-options")).toBeInTheDocument()
})
// Wait for save button to be disabled (no changes)
await waitFor(() => {
const saveButton = screen.getByTestId("save-button") as HTMLButtonElement
expect(saveButton.disabled).toBe(true)
})
// Click Done button
const doneButton = screen.getByText("settings:common.done")
fireEvent.click(doneButton)
// Should call onDone immediately without showing dialog
await waitFor(() => {
expect(onDone).toHaveBeenCalled()
})
// Verify no dialog appeared
expect(screen.queryByText("settings:unsavedChangesDialog.title")).not.toBeInTheDocument()
})
// TODO: Fix underlying issue - see above
it.skip("should handle initialization from null to value without triggering unsaved changes", async () => {
const onDone = vi.fn()
// Start with null apiModelId
const stateWithNull = {
...defaultExtensionState,
apiConfiguration: {
apiProvider: "openai",
apiModelId: null,
},
}
;(useExtensionState as any).mockReturnValue(stateWithNull)
render(
<QueryClientProvider client={queryClient}>
<SettingsView onDone={onDone} />
</QueryClientProvider>,
)
// Wait for initialization
await waitFor(() => {
expect(screen.getByTestId("api-options")).toBeInTheDocument()
})
// Wait for save button to be disabled (no changes)
await waitFor(() => {
const saveButton = screen.getByTestId("save-button") as HTMLButtonElement
expect(saveButton.disabled).toBe(true)
})
// Click Done button
const doneButton = screen.getByText("settings:common.done")
fireEvent.click(doneButton)
// Should call onDone immediately without showing dialog
await waitFor(() => {
expect(onDone).toHaveBeenCalled()
})
// Verify no dialog appeared
expect(screen.queryByText("settings:unsavedChangesDialog.title")).not.toBeInTheDocument()
})
// TODO: Fix underlying issue - see above
it.skip("should not trigger changes when ApiOptions syncs model IDs during mount", async () => {
const onDone = vi.fn()
// This specifically tests the bug we fixed where ApiOptions' useEffect
// was syncing selectedModelId with apiModelId and incorrectly triggering
// change detection because it wasn't passing isUserAction=false
// Mock ApiOptions to simulate the actual sync behavior
vi.mocked(ApiOptions).mockImplementation(({ setApiConfigurationField, apiConfiguration }) => {
const [hasSynced, setHasSynced] = React.useState(false)
React.useEffect(() => {
// Simulate the automatic sync that happens in the real component
// This should NOT trigger unsaved changes because isUserAction=false
// Only sync once to avoid multiple calls
if (!hasSynced && apiConfiguration?.apiModelId === "") {
setApiConfigurationField("apiModelId", "synced-model", false)
setHasSynced(true)
}
}, [hasSynced, apiConfiguration?.apiModelId, setApiConfigurationField])
return <div data-testid="api-options">ApiOptions</div>
})
render(
<QueryClientProvider client={queryClient}>
<SettingsView onDone={onDone} />
</QueryClientProvider>,
)
// Wait for component to fully mount and ApiOptions effect to run
await waitFor(() => {
expect(screen.getByTestId("api-options")).toBeInTheDocument()
})
// Wait for any async effects to complete
await new Promise((resolve) => setTimeout(resolve, 100))
// Save button should still be disabled (no user changes)
const saveButton = screen.getByTestId("save-button") as HTMLButtonElement
expect(saveButton.disabled).toBe(true)
// Clicking done should not show dialog
const doneButton = screen.getByText("settings:common.done")
fireEvent.click(doneButton)
// Should call onDone directly without showing unsaved changes dialog
await waitFor(() => {
expect(onDone).toHaveBeenCalled()
})
// No dialog should appear
expect(screen.queryByText("settings:unsavedChangesDialog.title")).not.toBeInTheDocument()
})
})