-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathextension-host.test.ts
More file actions
767 lines (594 loc) · 23.1 KB
/
Copy pathextension-host.test.ts
File metadata and controls
767 lines (594 loc) · 23.1 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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
// pnpm --filter @roo-code/cli test src/agent/__tests__/extension-host.test.ts
import { EventEmitter } from "events"
import fs from "fs"
import type { ExtensionMessage, WebviewMessage } from "@roo-code/types"
import { setRuntimeConfigValues } from "@roo-code/vscode-shim"
import { DEFAULT_FLAGS } from "@/types/index.js"
import { type ExtensionHostOptions, ExtensionHost } from "../extension-host.js"
import { ExtensionClient } from "../extension-client.js"
import { AgentLoopState } from "../agent-state.js"
vi.mock("@roo-code/vscode-shim", () => ({
createVSCodeAPI: vi.fn(() => ({
context: { extensionPath: "/test/extension" },
})),
setRuntimeConfigValues: vi.fn(),
}))
vi.mock("@/lib/storage/index.js", () => ({
createEphemeralStorageDir: vi.fn(() => Promise.resolve("/tmp/roo-cli-test-ephemeral")),
}))
/**
* Create a test ExtensionHost with default options.
*/
function createTestHost({
mode = "code",
provider = "openrouter",
model = "test-model",
...options
}: Partial<ExtensionHostOptions> = {}): ExtensionHost {
return new ExtensionHost({
mode,
provider,
model,
workspacePath: "/test/workspace",
extensionPath: "/test/extension",
ephemeral: false,
debug: false,
exitOnComplete: false,
...options,
})
}
// Type for accessing private members
type PrivateHost = Record<string, unknown>
/**
* Helper to access private members for testing
*/
function getPrivate<T>(host: ExtensionHost, key: string): T {
return (host as unknown as PrivateHost)[key] as T
}
/**
* Helper to set private members for testing
*/
function setPrivate(host: ExtensionHost, key: string, value: unknown): void {
;(host as unknown as PrivateHost)[key] = value
}
/**
* Helper to call private methods for testing
* This uses a more permissive type to avoid TypeScript errors with private methods
*/
function callPrivate<T>(host: ExtensionHost, method: string, ...args: unknown[]): T {
const fn = (host as unknown as PrivateHost)[method] as ((...a: unknown[]) => T) | undefined
if (!fn) throw new Error(`Method ${method} not found`)
return fn.apply(host, args)
}
/**
* Helper to spy on private methods
* This uses a more permissive type to avoid TypeScript errors with vi.spyOn on private methods
*/
function spyOnPrivate(host: ExtensionHost, method: string) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return vi.spyOn(host as any, method)
}
describe("ExtensionHost", () => {
const initialRooCliRuntimeEnv = process.env.ROO_CLI_RUNTIME
beforeEach(() => {
vi.resetAllMocks()
if (initialRooCliRuntimeEnv === undefined) {
delete process.env.ROO_CLI_RUNTIME
} else {
process.env.ROO_CLI_RUNTIME = initialRooCliRuntimeEnv
}
// Clean up globals
delete (global as Record<string, unknown>).vscode
delete (global as Record<string, unknown>).__extensionHost
})
afterAll(() => {
if (initialRooCliRuntimeEnv === undefined) {
delete process.env.ROO_CLI_RUNTIME
} else {
process.env.ROO_CLI_RUNTIME = initialRooCliRuntimeEnv
}
})
describe("constructor", () => {
it("should store options correctly", () => {
const options: ExtensionHostOptions = {
mode: "code",
workspacePath: "/my/workspace",
extensionPath: "/my/extension",
apiKey: "test-key",
provider: "openrouter",
model: "test-model",
ephemeral: false,
debug: false,
exitOnComplete: false,
integrationTest: true, // Set explicitly for testing
}
const host = new ExtensionHost(options)
// Options are stored as-is
const storedOptions = getPrivate<ExtensionHostOptions>(host, "options")
expect(storedOptions.mode).toBe(options.mode)
expect(storedOptions.workspacePath).toBe(options.workspacePath)
expect(storedOptions.extensionPath).toBe(options.extensionPath)
expect(storedOptions.integrationTest).toBe(true)
})
it("should be an EventEmitter instance", () => {
const host = createTestHost()
expect(host).toBeInstanceOf(EventEmitter)
})
it("should initialize with default state values", () => {
const host = createTestHost()
expect(getPrivate(host, "isReady")).toBe(false)
expect(getPrivate(host, "vscode")).toBeNull()
expect(getPrivate(host, "extensionModule")).toBeNull()
})
it("should initialize managers", () => {
const host = createTestHost()
// Should have client, outputManager, promptManager, and askDispatcher
expect(getPrivate(host, "client")).toBeDefined()
expect(getPrivate(host, "outputManager")).toBeDefined()
expect(getPrivate(host, "promptManager")).toBeDefined()
expect(getPrivate(host, "askDispatcher")).toBeDefined()
})
it("should mark process as CLI runtime", () => {
delete process.env.ROO_CLI_RUNTIME
createTestHost()
expect(process.env.ROO_CLI_RUNTIME).toBe("1")
})
it("should set execaShellPath in initialSettings when terminalShell is provided", () => {
const host = createTestHost({ terminalShell: "/bin/bash" })
const emitSpy = vi.spyOn(host, "emit")
host.markWebviewReady()
const updateSettingsCall = emitSpy.mock.calls.find(
(call) =>
call[0] === "webviewMessage" &&
typeof call[1] === "object" &&
call[1] !== null &&
(call[1] as WebviewMessage).type === "updateSettings",
)
expect(updateSettingsCall).toBeDefined()
const payload = updateSettingsCall?.[1] as WebviewMessage
expect(payload.updatedSettings?.execaShellPath).toBe("/bin/bash")
})
})
describe("webview provider registration", () => {
it("should register webview provider without throwing", () => {
const host = createTestHost()
const mockProvider = { resolveWebviewView: vi.fn() }
// registerWebviewProvider is now a no-op, just ensure it doesn't throw
expect(() => {
host.registerWebviewProvider("test-view", mockProvider)
}).not.toThrow()
})
it("should unregister webview provider without throwing", () => {
const host = createTestHost()
const mockProvider = { resolveWebviewView: vi.fn() }
host.registerWebviewProvider("test-view", mockProvider)
// unregisterWebviewProvider is now a no-op, just ensure it doesn't throw
expect(() => {
host.unregisterWebviewProvider("test-view")
}).not.toThrow()
})
it("should handle unregistering non-existent provider gracefully", () => {
const host = createTestHost()
expect(() => {
host.unregisterWebviewProvider("non-existent")
}).not.toThrow()
})
})
describe("webview ready state", () => {
describe("isInInitialSetup", () => {
it("should return true before webview is ready", () => {
const host = createTestHost()
expect(host.isInInitialSetup()).toBe(true)
})
it("should return false after markWebviewReady is called", () => {
const host = createTestHost()
host.markWebviewReady()
expect(host.isInInitialSetup()).toBe(false)
})
})
describe("markWebviewReady", () => {
it("should set isReady to true", () => {
const host = createTestHost()
host.markWebviewReady()
expect(getPrivate(host, "isReady")).toBe(true)
})
it("should send webviewDidLaunch message", () => {
const host = createTestHost()
const emitSpy = vi.spyOn(host, "emit")
host.markWebviewReady()
expect(emitSpy).toHaveBeenCalledWith("webviewMessage", { type: "webviewDidLaunch" })
})
it("should send updateSettings message", () => {
const host = createTestHost()
const emitSpy = vi.spyOn(host, "emit")
host.markWebviewReady()
// Check that updateSettings was called
const updateSettingsCall = emitSpy.mock.calls.find(
(call) =>
call[0] === "webviewMessage" &&
typeof call[1] === "object" &&
call[1] !== null &&
(call[1] as WebviewMessage).type === "updateSettings",
)
expect(updateSettingsCall).toBeDefined()
})
it("should seed runtime config under the extension namespace", () => {
const host = createTestHost()
host.markWebviewReady()
expect(setRuntimeConfigValues).toHaveBeenCalledWith("zoo-code", expect.any(Object))
})
it("should force terminalShellIntegrationDisabled when terminalShell is provided", () => {
const host = createTestHost({ terminalShell: "/bin/bash" })
const emitSpy = vi.spyOn(host, "emit")
host.markWebviewReady()
const updateSettingsCall = emitSpy.mock.calls.find(
(call) =>
call[0] === "webviewMessage" &&
typeof call[1] === "object" &&
call[1] !== null &&
(call[1] as WebviewMessage).type === "updateSettings",
)
expect(updateSettingsCall).toBeDefined()
const payload = updateSettingsCall?.[1] as WebviewMessage
expect(payload.type).toBe("updateSettings")
expect(payload.updatedSettings?.terminalShellIntegrationDisabled).toBe(true)
})
})
})
describe("sendToExtension", () => {
it("should throw error when extension not ready", () => {
const host = createTestHost()
const message: WebviewMessage = { type: "requestModes" }
expect(() => {
host.sendToExtension(message)
}).toThrow("You cannot send messages to the extension before it is ready")
})
it("should emit webviewMessage event when webview is ready", () => {
const host = createTestHost()
const emitSpy = vi.spyOn(host, "emit")
const message: WebviewMessage = { type: "requestModes" }
host.markWebviewReady()
emitSpy.mockClear() // Clear the markWebviewReady calls
host.sendToExtension(message)
expect(emitSpy).toHaveBeenCalledWith("webviewMessage", message)
})
it("should not throw when webview is ready", () => {
const host = createTestHost()
host.markWebviewReady()
expect(() => {
host.sendToExtension({ type: "requestModes" })
}).not.toThrow()
})
})
describe("message handling via client", () => {
it("should forward extension messages to the client", () => {
const host = createTestHost()
const client = getPrivate(host, "client") as ExtensionClient
// Simulate extension message.
host.emit("extensionWebviewMessage", {
type: "state",
state: { clineMessages: [] },
} as unknown as ExtensionMessage)
// Message listener is set up in activate(), which we can't easily call in unit tests.
// But we can verify the client exists and has the handleMessage method.
expect(typeof client.handleMessage).toBe("function")
})
})
describe("public agent state API", () => {
it("should return agent state from getAgentState()", () => {
const host = createTestHost()
const state = host.getAgentState()
expect(state).toBeDefined()
expect(state.state).toBeDefined()
expect(state.isWaitingForInput).toBeDefined()
expect(state.isRunning).toBeDefined()
})
it("should return isWaitingForInput() status", () => {
const host = createTestHost()
expect(typeof host.isWaitingForInput()).toBe("boolean")
})
})
describe("quiet mode", () => {
describe("setupQuietMode", () => {
it("should not modify console when integrationTest is true", () => {
// By default, constructor sets integrationTest = true
const host = createTestHost()
const originalLog = console.log
callPrivate(host, "setupQuietMode")
// Console should not be modified since integrationTest is true
expect(console.log).toBe(originalLog)
})
it("should suppress console when integrationTest is false", () => {
// Capture the real console.log before any host is created
const originalLog = console.log
// Create host with integrationTest: true to prevent constructor from suppressing
const host = createTestHost({ integrationTest: true })
// Override integrationTest to false to test suppression
const options = getPrivate<ExtensionHostOptions>(host, "options")
options.integrationTest = false
callPrivate(host, "setupQuietMode")
// Console should be modified (suppressed)
expect(console.log).not.toBe(originalLog)
// Restore for other tests
callPrivate(host, "restoreConsole")
})
it("should preserve console.error even when suppressing", () => {
const host = createTestHost()
const originalError = console.error
// Override integrationTest to false
const options = getPrivate<ExtensionHostOptions>(host, "options")
options.integrationTest = false
callPrivate(host, "setupQuietMode")
expect(console.error).toBe(originalError)
callPrivate(host, "restoreConsole")
})
})
describe("restoreConsole", () => {
it("should restore original console methods when suppressed", () => {
// Capture the real console.log before any host is created
const originalLog = console.log
// Create host with integrationTest: true to prevent constructor from suppressing
const host = createTestHost({ integrationTest: true })
// Override integrationTest to false to actually suppress
const options = getPrivate<ExtensionHostOptions>(host, "options")
options.integrationTest = false
callPrivate(host, "setupQuietMode")
callPrivate(host, "restoreConsole")
expect(console.log).toBe(originalLog)
})
it("should handle case where console was not suppressed", () => {
const host = createTestHost()
expect(() => {
callPrivate(host, "restoreConsole")
}).not.toThrow()
})
})
})
describe("dispose", () => {
let host: ExtensionHost
beforeEach(() => {
host = createTestHost()
})
it("should remove message listener", async () => {
const listener = vi.fn()
setPrivate(host, "messageListener", listener)
host.on("extensionWebviewMessage", listener)
await host.dispose()
expect(getPrivate(host, "messageListener")).toBeNull()
})
it("should call extension deactivate if available", async () => {
const deactivateMock = vi.fn()
setPrivate(host, "extensionModule", {
deactivate: deactivateMock,
})
await host.dispose()
expect(deactivateMock).toHaveBeenCalled()
})
it("should clear vscode reference", async () => {
setPrivate(host, "vscode", { context: {} })
await host.dispose()
expect(getPrivate(host, "vscode")).toBeNull()
})
it("should clear extensionModule reference", async () => {
setPrivate(host, "extensionModule", {})
await host.dispose()
expect(getPrivate(host, "extensionModule")).toBeNull()
})
it("should delete global vscode", async () => {
;(global as Record<string, unknown>).vscode = {}
await host.dispose()
expect((global as Record<string, unknown>).vscode).toBeUndefined()
})
it("should delete global __extensionHost", async () => {
;(global as Record<string, unknown>).__extensionHost = {}
await host.dispose()
expect((global as Record<string, unknown>).__extensionHost).toBeUndefined()
})
it("should call restoreConsole", async () => {
const restoreConsoleSpy = spyOnPrivate(host, "restoreConsole")
await host.dispose()
expect(restoreConsoleSpy).toHaveBeenCalled()
})
it("should clear ROO_CLI_RUNTIME on dispose when it was previously unset", async () => {
delete process.env.ROO_CLI_RUNTIME
host = createTestHost()
expect(process.env.ROO_CLI_RUNTIME).toBe("1")
await host.dispose()
expect(process.env.ROO_CLI_RUNTIME).toBeUndefined()
})
it("should restore prior ROO_CLI_RUNTIME value on dispose", async () => {
process.env.ROO_CLI_RUNTIME = "preexisting-value"
host = createTestHost()
expect(process.env.ROO_CLI_RUNTIME).toBe("1")
await host.dispose()
expect(process.env.ROO_CLI_RUNTIME).toBe("preexisting-value")
})
})
describe("runTask", () => {
it("should send newTask message when called", async () => {
const host = createTestHost()
host.markWebviewReady()
const emitSpy = vi.spyOn(host, "emit")
const client = getPrivate(host, "client") as ExtensionClient
// Start the task (will hang waiting for completion)
const taskPromise = host.runTask("test prompt")
// Emit completion to resolve the promise via the client's emitter
const taskCompletedEvent = {
success: true,
stateInfo: {
state: AgentLoopState.IDLE,
isWaitingForInput: false,
isRunning: false,
isStreaming: false,
requiredAction: "start_task" as const,
description: "Task completed",
},
}
setTimeout(() => client.getEmitter().emit("taskCompleted", taskCompletedEvent), 10)
await taskPromise
expect(emitSpy).toHaveBeenCalledWith("webviewMessage", { type: "newTask", text: "test prompt" })
})
it("should include taskId when provided", async () => {
const host = createTestHost()
host.markWebviewReady()
const emitSpy = vi.spyOn(host, "emit")
const client = getPrivate(host, "client") as ExtensionClient
const taskPromise = host.runTask("test prompt", "task-123")
const taskCompletedEvent = {
success: true,
stateInfo: {
state: AgentLoopState.IDLE,
isWaitingForInput: false,
isRunning: false,
isStreaming: false,
requiredAction: "start_task" as const,
description: "Task completed",
},
}
setTimeout(() => client.getEmitter().emit("taskCompleted", taskCompletedEvent), 10)
await taskPromise
expect(emitSpy).toHaveBeenCalledWith("webviewMessage", {
type: "newTask",
text: "test prompt",
taskId: "task-123",
})
})
it("should resolve when taskCompleted is emitted on client", async () => {
const host = createTestHost()
host.markWebviewReady()
const client = getPrivate(host, "client") as ExtensionClient
const taskPromise = host.runTask("test prompt")
// Emit completion after a short delay via the client's emitter
const taskCompletedEvent = {
success: true,
stateInfo: {
state: AgentLoopState.IDLE,
isWaitingForInput: false,
isRunning: false,
isStreaming: false,
requiredAction: "start_task" as const,
description: "Task completed",
},
}
setTimeout(() => client.getEmitter().emit("taskCompleted", taskCompletedEvent), 10)
await expect(taskPromise).resolves.toBeUndefined()
})
it("should send showTaskWithId for resumeTask and resolve on completion", async () => {
const host = createTestHost()
host.markWebviewReady()
const emitSpy = vi.spyOn(host, "emit")
const client = getPrivate(host, "client") as ExtensionClient
const taskPromise = host.resumeTask("task-abc")
const taskCompletedEvent = {
success: true,
stateInfo: {
state: AgentLoopState.IDLE,
isWaitingForInput: false,
isRunning: false,
isStreaming: false,
requiredAction: "start_task" as const,
description: "Task completed",
},
}
setTimeout(() => client.getEmitter().emit("taskCompleted", taskCompletedEvent), 10)
await taskPromise
expect(emitSpy).toHaveBeenCalledWith("webviewMessage", { type: "showTaskWithId", text: "task-abc" })
})
})
describe("initial settings", () => {
it("should set mode from options", () => {
const host = createTestHost({ mode: "architect" })
const initialSettings = getPrivate<Record<string, unknown>>(host, "initialSettings")
expect(initialSettings.mode).toBe("architect")
})
it("should use default consecutiveMistakeLimit when not provided", () => {
const host = createTestHost()
const initialSettings = getPrivate<Record<string, unknown>>(host, "initialSettings")
expect(initialSettings.consecutiveMistakeLimit).toBe(DEFAULT_FLAGS.consecutiveMistakeLimit)
})
it("should set consecutiveMistakeLimit from options", () => {
const host = createTestHost({ consecutiveMistakeLimit: 8 })
const initialSettings = getPrivate<Record<string, unknown>>(host, "initialSettings")
expect(initialSettings.consecutiveMistakeLimit).toBe(8)
})
it("should enable auto-approval in non-interactive mode", () => {
const host = createTestHost({ nonInteractive: true })
const initialSettings = getPrivate<Record<string, unknown>>(host, "initialSettings")
expect(initialSettings.autoApprovalEnabled).toBe(true)
expect(initialSettings.alwaysAllowReadOnly).toBe(true)
expect(initialSettings.alwaysAllowWrite).toBe(true)
expect(initialSettings.alwaysAllowExecute).toBe(true)
})
it("should disable auto-approval in interactive mode", () => {
const host = createTestHost({ nonInteractive: false })
const initialSettings = getPrivate<Record<string, unknown>>(host, "initialSettings")
expect(initialSettings.autoApprovalEnabled).toBe(false)
})
it("should set reasoning effort when specified", () => {
const host = createTestHost({ reasoningEffort: "high" })
const initialSettings = getPrivate<Record<string, unknown>>(host, "initialSettings")
expect(initialSettings.enableReasoningEffort).toBe(true)
expect(initialSettings.reasoningEffort).toBe("high")
})
it("should disable reasoning effort when set to disabled", () => {
const host = createTestHost({ reasoningEffort: "disabled" })
const initialSettings = getPrivate<Record<string, unknown>>(host, "initialSettings")
expect(initialSettings.enableReasoningEffort).toBe(false)
})
it("should not set reasoning effort when unspecified", () => {
const host = createTestHost({ reasoningEffort: "unspecified" })
const initialSettings = getPrivate<Record<string, unknown>>(host, "initialSettings")
expect(initialSettings.enableReasoningEffort).toBeUndefined()
expect(initialSettings.reasoningEffort).toBeUndefined()
})
})
describe("ephemeral mode", () => {
it("should store ephemeral option correctly", () => {
const host = createTestHost({ ephemeral: true })
const options = getPrivate<ExtensionHostOptions>(host, "options")
expect(options.ephemeral).toBe(true)
})
it("should default ephemeralStorageDir to null", () => {
const host = createTestHost()
expect(getPrivate(host, "ephemeralStorageDir")).toBeNull()
})
it("should clean up ephemeral storage directory on dispose", async () => {
const host = createTestHost({ ephemeral: true })
// Set up a mock ephemeral storage directory
const mockEphemeralDir = "/tmp/roo-cli-test-ephemeral-cleanup"
setPrivate(host, "ephemeralStorageDir", mockEphemeralDir)
// Mock fs.promises.rm
const rmMock = vi.spyOn(fs.promises, "rm").mockResolvedValue(undefined)
await host.dispose()
expect(rmMock).toHaveBeenCalledWith(mockEphemeralDir, { recursive: true, force: true })
expect(getPrivate(host, "ephemeralStorageDir")).toBeNull()
rmMock.mockRestore()
})
it("should not clean up when ephemeralStorageDir is null", async () => {
const host = createTestHost()
// ephemeralStorageDir is null by default
expect(getPrivate(host, "ephemeralStorageDir")).toBeNull()
const rmMock = vi.spyOn(fs.promises, "rm").mockResolvedValue(undefined)
await host.dispose()
// rm should not be called when there's no ephemeral storage
expect(rmMock).not.toHaveBeenCalled()
rmMock.mockRestore()
})
it("should handle ephemeral storage cleanup errors gracefully", async () => {
const host = createTestHost({ ephemeral: true })
// Set up a mock ephemeral storage directory
setPrivate(host, "ephemeralStorageDir", "/tmp/roo-cli-test-ephemeral-error")
// Mock fs.promises.rm to throw an error
const rmMock = vi.spyOn(fs.promises, "rm").mockRejectedValue(new Error("Cleanup failed"))
// dispose should not throw even if cleanup fails
await expect(host.dispose()).resolves.toBeUndefined()
rmMock.mockRestore()
})
it("should not affect normal mode when ephemeral is false", () => {
const host = createTestHost({ ephemeral: false })
const options = getPrivate<ExtensionHostOptions>(host, "options")
expect(options.ephemeral).toBe(false)
expect(getPrivate(host, "ephemeralStorageDir")).toBeNull()
})
})
})