-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathContextManagementSettings.spec.tsx
More file actions
500 lines (410 loc) · 17.8 KB
/
Copy pathContextManagementSettings.spec.tsx
File metadata and controls
500 lines (410 loc) · 17.8 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
// npx vitest src/components/settings/__tests__/ContextManagementSettings.spec.tsx
import { render, screen, fireEvent, waitFor } from "@/utils/test-utils"
import { ContextManagementSettings } from "../ContextManagementSettings"
// Mock the translation hook
vi.mock("@/hooks/useAppTranslation", () => ({
useAppTranslation: () => ({
t: (key: string) => {
// Return specific translations for our test cases
if (key === "settings:contextManagement.diagnostics.maxMessages.unlimitedLabel") {
return "Unlimited"
}
return key
},
}),
}))
// Mock the UI components
vi.mock("@/components/ui", () => ({
...vi.importActual("@/components/ui"),
Slider: ({ value, onValueChange, "data-testid": dataTestId, disabled, min, max }: any) => (
<input
type="range"
value={value?.[0] ?? 0}
min={min}
max={max}
onChange={(e) => onValueChange([parseFloat(e.target.value)])}
onKeyDown={(e) => {
const currentValue = value?.[0] ?? 0
if (e.key === "ArrowRight") {
onValueChange([currentValue + 1])
} else if (e.key === "ArrowLeft") {
onValueChange([currentValue - 1])
}
}}
data-testid={dataTestId}
disabled={disabled}
role="slider"
/>
),
Input: ({ value, onChange, "data-testid": dataTestId, ...props }: any) => (
<input value={value} onChange={onChange} data-testid={dataTestId} {...props} />
),
Button: ({ children, onClick, ...props }: any) => (
<button onClick={onClick} {...props}>
{children}
</button>
),
Select: ({ children, ...props }: any) => (
<div role="combobox" {...props}>
{children}
</div>
),
SelectTrigger: ({ children, ...props }: any) => <div {...props}>{children}</div>,
SelectValue: ({ children, ...props }: any) => <div {...props}>{children}</div>,
SelectContent: ({ children, ...props }: any) => <div {...props}>{children}</div>,
SelectItem: ({ children, ...props }: any) => <div {...props}>{children}</div>,
StandardTooltip: ({ children, content }: any) => <div title={content}>{children}</div>,
}))
// Mock vscode utilities - this is necessary since we're not in a VSCode environment
vi.mock("@/utils/vscode", () => ({
vscode: {
postMessage: vi.fn(),
},
}))
// Mock VSCode components to behave like standard HTML elements
vi.mock("@vscode/webview-ui-toolkit/react", () => ({
VSCodeCheckbox: ({ checked, onChange, children, "data-testid": dataTestId, ...props }: any) => (
<label data-testid={dataTestId} {...props}>
<input
type="checkbox"
role="checkbox"
checked={checked || false}
aria-checked={checked || false}
onChange={(e: any) => onChange?.({ target: { checked: e.target.checked } })}
/>
{children}
</label>
),
VSCodeTextArea: ({ value, onChange, ...props }: any) => <textarea value={value} onChange={onChange} {...props} />,
}))
describe("ContextManagementSettings", () => {
const defaultProps = {
autoCondenseContext: false,
autoCondenseContextPercent: 80,
listApiConfigMeta: [],
maxOpenTabsContext: 20,
maxWorkspaceFiles: 200,
showRooIgnoredFiles: false,
compactToolUI: false,
profileThresholds: {},
includeDiagnosticMessages: true,
maxDiagnosticMessages: 50,
writeDelayMs: 1000,
customSupportPrompts: {},
setCustomSupportPrompts: vi.fn(),
setCachedStateField: vi.fn(),
}
beforeEach(() => {
vi.clearAllMocks()
})
it("renders diagnostic settings", () => {
render(<ContextManagementSettings {...defaultProps} />)
// Check for diagnostic checkbox
expect(screen.getByTestId("include-diagnostic-messages-checkbox")).toBeInTheDocument()
// Check for slider
expect(screen.getByTestId("max-diagnostic-messages-slider")).toBeInTheDocument()
expect(screen.getByText("50")).toBeInTheDocument()
})
it("renders with diagnostic messages enabled", () => {
render(<ContextManagementSettings {...defaultProps} includeDiagnosticMessages={true} />)
const checkbox = screen.getByTestId("include-diagnostic-messages-checkbox")
expect(checkbox.querySelector("input")).toBeChecked()
const slider = screen.getByTestId("max-diagnostic-messages-slider")
expect(slider).toBeInTheDocument()
expect(slider).toHaveValue("50")
})
it("renders with diagnostic messages disabled", () => {
render(<ContextManagementSettings {...defaultProps} includeDiagnosticMessages={false} />)
const checkbox = screen.getByTestId("include-diagnostic-messages-checkbox")
expect(checkbox.querySelector("input")).not.toBeChecked()
// Slider should still be rendered when diagnostics are disabled
expect(screen.getByTestId("max-diagnostic-messages-slider")).toBeInTheDocument()
expect(screen.getByText("50")).toBeInTheDocument()
})
it("calls setCachedStateField when include diagnostic messages checkbox is toggled", async () => {
const setCachedStateField = vi.fn()
render(<ContextManagementSettings {...defaultProps} setCachedStateField={setCachedStateField} />)
const checkbox = screen.getByTestId("include-diagnostic-messages-checkbox").querySelector("input")!
fireEvent.click(checkbox)
await waitFor(() => {
expect(setCachedStateField).toHaveBeenCalledWith("includeDiagnosticMessages", false)
})
})
it("renders the compact tool UI toggle", () => {
render(<ContextManagementSettings {...defaultProps} />)
expect(screen.getByTestId("compact-tool-ui-checkbox")).toBeInTheDocument()
})
it("calls setCachedStateField when the compact tool UI checkbox is toggled", async () => {
const setCachedStateField = vi.fn()
render(<ContextManagementSettings {...defaultProps} setCachedStateField={setCachedStateField} />)
const checkbox = screen.getByTestId("compact-tool-ui-checkbox").querySelector("input")!
fireEvent.click(checkbox)
await waitFor(() => {
expect(setCachedStateField).toHaveBeenCalledWith("compactToolUI", true)
})
})
it("calls setCachedStateField when max diagnostic messages slider is changed", async () => {
const setCachedStateField = vi.fn()
render(<ContextManagementSettings {...defaultProps} setCachedStateField={setCachedStateField} />)
const slider = screen.getByTestId("max-diagnostic-messages-slider")
fireEvent.change(slider, { target: { value: "100" } })
await waitFor(() => {
expect(setCachedStateField).toHaveBeenCalledWith("maxDiagnosticMessages", -1)
})
})
it("keeps slider visible when include diagnostic messages is unchecked", () => {
const { rerender } = render(<ContextManagementSettings {...defaultProps} includeDiagnosticMessages={true} />)
const slider = screen.getByTestId("max-diagnostic-messages-slider")
expect(slider).toBeInTheDocument()
// Update to disabled - slider should still be visible
rerender(<ContextManagementSettings {...defaultProps} includeDiagnosticMessages={false} />)
expect(screen.getByTestId("max-diagnostic-messages-slider")).toBeInTheDocument()
})
it("displays correct max diagnostic messages value", () => {
const { rerender } = render(<ContextManagementSettings {...defaultProps} maxDiagnosticMessages={25} />)
expect(screen.getByText("25")).toBeInTheDocument()
// Update value - 100 should display as "Unlimited"
rerender(<ContextManagementSettings {...defaultProps} maxDiagnosticMessages={100} />)
expect(
screen.getByText("settings:contextManagement.diagnostics.maxMessages.unlimitedLabel"),
).toBeInTheDocument()
// Test unlimited value (-1) displays as "Unlimited"
rerender(<ContextManagementSettings {...defaultProps} maxDiagnosticMessages={-1} />)
expect(
screen.getByText("settings:contextManagement.diagnostics.maxMessages.unlimitedLabel"),
).toBeInTheDocument()
})
it("renders other context management settings", () => {
render(<ContextManagementSettings {...defaultProps} />)
// Check for other sliders
expect(screen.getByTestId("open-tabs-limit-slider")).toBeInTheDocument()
expect(screen.getByTestId("workspace-files-limit-slider")).toBeInTheDocument()
// Check for checkboxes
expect(screen.getByTestId("show-rooignored-files-checkbox")).toBeInTheDocument()
expect(screen.getByTestId("auto-condense-context-checkbox")).toBeInTheDocument()
})
describe("Edge cases for maxDiagnosticMessages", () => {
it("handles zero value as unlimited", async () => {
const setCachedStateField = vi.fn()
render(
<ContextManagementSettings
{...defaultProps}
maxDiagnosticMessages={0}
setCachedStateField={setCachedStateField}
/>,
)
// Zero is now treated as unlimited
expect(
screen.getByText("settings:contextManagement.diagnostics.maxMessages.unlimitedLabel"),
).toBeInTheDocument()
const slider = screen.getByTestId("max-diagnostic-messages-slider")
// Zero should map to slider position 100 (unlimited)
expect(slider).toHaveValue("100")
})
it("handles negative values as unlimited", async () => {
const setCachedStateField = vi.fn()
render(
<ContextManagementSettings
{...defaultProps}
maxDiagnosticMessages={-10}
setCachedStateField={setCachedStateField}
/>,
)
// Component displays "Unlimited" for any negative value
expect(
screen.getByText("settings:contextManagement.diagnostics.maxMessages.unlimitedLabel"),
).toBeInTheDocument()
// Slider should be at max position (100) for negative values
const slider = screen.getByTestId("max-diagnostic-messages-slider")
expect(slider).toHaveValue("100")
})
it("handles very large numbers by capping at maximum", async () => {
const setCachedStateField = vi.fn()
const largeNumber = 1000
render(
<ContextManagementSettings
{...defaultProps}
maxDiagnosticMessages={largeNumber}
setCachedStateField={setCachedStateField}
/>,
)
// Should display the actual value even if it exceeds slider max
expect(screen.getByText(largeNumber.toString())).toBeInTheDocument()
// Slider value would be capped at max (100)
const slider = screen.getByTestId("max-diagnostic-messages-slider")
expect(slider).toHaveValue("100")
})
it("enforces maximum value constraint", async () => {
const setCachedStateField = vi.fn()
render(<ContextManagementSettings {...defaultProps} setCachedStateField={setCachedStateField} />)
const slider = screen.getByTestId("max-diagnostic-messages-slider")
// Test that setting value above 100 gets capped
fireEvent.change(slider, { target: { value: "150" } })
await waitFor(() => {
// Should be capped at 100, which maps to -1 (unlimited)
expect(setCachedStateField).toHaveBeenCalledWith("maxDiagnosticMessages", -1)
})
})
it("handles boundary value at minimum (1)", async () => {
const setCachedStateField = vi.fn()
render(<ContextManagementSettings {...defaultProps} setCachedStateField={setCachedStateField} />)
const slider = screen.getByTestId("max-diagnostic-messages-slider")
fireEvent.change(slider, { target: { value: "1" } })
await waitFor(() => {
expect(setCachedStateField).toHaveBeenCalledWith("maxDiagnosticMessages", 1)
})
})
it("handles boundary value at maximum (100) as unlimited (-1)", async () => {
const setCachedStateField = vi.fn()
render(<ContextManagementSettings {...defaultProps} setCachedStateField={setCachedStateField} />)
const slider = screen.getByTestId("max-diagnostic-messages-slider")
fireEvent.change(slider, { target: { value: "100" } })
await waitFor(() => {
// When slider is at 100, it should set the value to -1 (unlimited)
expect(setCachedStateField).toHaveBeenCalledWith("maxDiagnosticMessages", -1)
})
})
it("handles decimal values by parsing as float", async () => {
const setCachedStateField = vi.fn()
render(<ContextManagementSettings {...defaultProps} setCachedStateField={setCachedStateField} />)
const slider = screen.getByTestId("max-diagnostic-messages-slider")
fireEvent.change(slider, { target: { value: "50.7" } })
await waitFor(() => {
// The mock slider component parses as float
expect(setCachedStateField).toHaveBeenCalledWith("maxDiagnosticMessages", 50.7)
})
})
})
it("renders with autoCondenseContext enabled", () => {
const propsWithAutoCondense = {
...defaultProps,
autoCondenseContext: true,
autoCondenseContextPercent: 75,
}
render(<ContextManagementSettings {...propsWithAutoCondense} />)
// Should render the auto condense section
const autoCondenseCheckbox = screen.getByTestId("auto-condense-context-checkbox")
expect(autoCondenseCheckbox).toBeInTheDocument()
// Should render the threshold slider with correct value
const slider = screen.getByTestId("condense-threshold-slider")
expect(slider).toBeInTheDocument()
// Should render the profile select dropdown
const selects = screen.getAllByRole("combobox")
expect(selects).toHaveLength(1)
})
describe("Auto Condense Context functionality", () => {
const autoCondenseProps = {
...defaultProps,
autoCondenseContext: true,
autoCondenseContextPercent: 75,
listApiConfigMeta: [
{ id: "config-1", name: "Config 1" },
{ id: "config-2", name: "Config 2" },
],
}
it("toggles auto condense context setting", () => {
const mockSetCachedStateField = vitest.fn()
const props = { ...autoCondenseProps, setCachedStateField: mockSetCachedStateField }
render(<ContextManagementSettings {...props} />)
const checkbox = screen.getByTestId("auto-condense-context-checkbox")
const input = checkbox.querySelector('input[type="checkbox"]')
expect(input).toBeChecked()
// Toggle off
fireEvent.click(checkbox)
expect(mockSetCachedStateField).toHaveBeenCalledWith("autoCondenseContext", false)
})
it("shows threshold settings when auto condense is enabled", () => {
render(<ContextManagementSettings {...autoCondenseProps} />)
// Threshold settings should be visible
expect(screen.getByTestId("condense-threshold-slider")).toBeInTheDocument()
// One combobox for profile selection
expect(screen.getAllByRole("combobox")).toHaveLength(1)
})
it("updates auto condense context percent", () => {
const mockSetCachedStateField = vitest.fn()
const props = { ...autoCondenseProps, setCachedStateField: mockSetCachedStateField }
render(<ContextManagementSettings {...props} />)
// Find the condense threshold slider
const slider = screen.getByTestId("condense-threshold-slider")
// Test slider interaction
slider.focus()
fireEvent.keyDown(slider, { key: "ArrowRight" })
expect(mockSetCachedStateField).toHaveBeenCalledWith("autoCondenseContextPercent", 76)
})
it("displays correct auto condense context percent value", () => {
render(<ContextManagementSettings {...autoCondenseProps} />)
expect(screen.getByText("75%")).toBeInTheDocument()
})
})
it("handles boundary values for sliders", () => {
const mockSetCachedStateField = vitest.fn()
const props = {
...defaultProps,
maxOpenTabsContext: 0,
maxWorkspaceFiles: 500,
maxGitStatusFiles: 0,
setCachedStateField: mockSetCachedStateField,
}
render(<ContextManagementSettings {...props} />)
// Check boundary values are displayed by checking the slider values directly
const openTabsSlider = screen.getByTestId("open-tabs-limit-slider")
expect(openTabsSlider).toHaveValue("0")
const workspaceFilesSlider = screen.getByTestId("workspace-files-limit-slider")
expect(workspaceFilesSlider).toHaveValue("500")
const gitStatusSlider = screen.getByTestId("max-git-status-files-slider")
expect(gitStatusSlider).toHaveValue("0")
})
it("handles undefined optional props gracefully", () => {
const propsWithUndefined = {
...defaultProps,
showRooIgnoredFiles: undefined,
}
expect(() => {
render(<ContextManagementSettings {...propsWithUndefined} />)
}).not.toThrow()
// Should use default values
expect(screen.getByText("20")).toBeInTheDocument() // default maxOpenTabsContext
expect(screen.getByText("200")).toBeInTheDocument() // default maxWorkspaceFiles
})
describe("Conditional rendering", () => {
it("does not render threshold settings when autoCondenseContext is false", () => {
const propsWithoutAutoCondense = {
...defaultProps,
autoCondenseContext: false,
}
render(<ContextManagementSettings {...propsWithoutAutoCondense} />)
// When auto condense is false, threshold slider should not be visible
expect(screen.queryByTestId("condense-threshold-slider")).not.toBeInTheDocument()
})
})
describe("Accessibility", () => {
it("has proper labels and descriptions", () => {
render(<ContextManagementSettings {...defaultProps} />)
// Check that labels are present
expect(screen.getByText("settings:contextManagement.openTabs.label")).toBeInTheDocument()
expect(screen.getByText("settings:contextManagement.workspaceFiles.label")).toBeInTheDocument()
expect(screen.getByText("settings:contextManagement.rooignore.label")).toBeInTheDocument()
// Check that descriptions are present
expect(screen.getByText("settings:contextManagement.openTabs.description")).toBeInTheDocument()
expect(screen.getByText("settings:contextManagement.workspaceFiles.description")).toBeInTheDocument()
expect(screen.getByText("settings:contextManagement.rooignore.description")).toBeInTheDocument()
})
it("has proper test ids for all interactive elements", () => {
render(<ContextManagementSettings {...defaultProps} />)
expect(screen.getByTestId("open-tabs-limit-slider")).toBeInTheDocument()
expect(screen.getByTestId("workspace-files-limit-slider")).toBeInTheDocument()
expect(screen.getByTestId("show-rooignored-files-checkbox")).toBeInTheDocument()
})
})
describe("Integration with translation system", () => {
it("uses translation keys for all text content", () => {
render(<ContextManagementSettings {...defaultProps} />)
// Verify that translation keys are being used (mocked to return the key)
expect(screen.getByText("settings:sections.contextManagement")).toBeInTheDocument()
expect(screen.getByText("settings:contextManagement.description")).toBeInTheDocument()
expect(screen.getByText("settings:contextManagement.openTabs.label")).toBeInTheDocument()
expect(screen.getByText("settings:contextManagement.workspaceFiles.label")).toBeInTheDocument()
expect(screen.getByText("settings:contextManagement.rooignore.label")).toBeInTheDocument()
})
})
})