From 92a34c75683a848b788b73bb7402e51dcbc7fb90 Mon Sep 17 00:00:00 2001 From: Bertan Ari Date: Tue, 1 Dec 2026 11:36:42 -0800 Subject: [PATCH] test: isolate focus tracker tests from leaked activations --- src/__tests__/extension.spec.ts | 47 ++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/__tests__/extension.spec.ts b/src/__tests__/extension.spec.ts index 49ce91d585..446b08fff6 100644 --- a/src/__tests__/extension.spec.ts +++ b/src/__tests__/extension.spec.ts @@ -236,17 +236,21 @@ describe("extension.ts", () => { let mockContext: vscode.ExtensionContext let settingsUpdatedHandler: ((data: Record) => void | Promise) | undefined - beforeEach(() => { - vi.clearAllMocks() - - mockContext = { + // Partial double: activate() reads only extensionPath, globalState and subscriptions. + const createMockContext = () => + ({ extensionPath: "/test/path", globalState: { get: vi.fn().mockReturnValue(undefined), update: vi.fn(), }, subscriptions: [], - } as unknown as vscode.ExtensionContext + }) as unknown as vscode.ExtensionContext + + beforeEach(() => { + vi.clearAllMocks() + + mockContext = createMockContext() settingsUpdatedHandler = undefined }) @@ -255,6 +259,8 @@ describe("extension.ts", () => { "shares the extension-owned focus tracker with the sidebar and code actions and disposes its listeners", { timeout: activationTestTimeout }, async () => { + // Local context: if this test times out, its late activate() must not reach a later test's context. + const context = createMockContext() vi.resetModules() const { WebviewFocusTracker } = await import("../core/webview/WebviewFocusTracker") const { ClineProvider } = await import("../core/webview/ClineProvider") @@ -263,21 +269,21 @@ describe("extension.ts", () => { const vscode = await import("vscode") const { activate } = await import("../extension") - await activate(mockContext) + await activate(context) - const trackers = mockContext.subscriptions.filter((entry) => entry instanceof WebviewFocusTracker) + const trackers = context.subscriptions.filter((entry) => entry instanceof WebviewFocusTracker) expect(trackers).toHaveLength(1) const tracker = trackers[0] const outputChannel = vi.mocked(vscode.window.createOutputChannel).mock.results[0].value expect(ClineProvider).toHaveBeenCalledExactlyOnceWith( - mockContext, + context, outputChannel, "sidebar", - await ContextProxy.getInstance(mockContext), + await ContextProxy.getInstance(context), tracker, null, ) - expect(registerCodeActions).toHaveBeenCalledExactlyOnceWith(mockContext, tracker) + expect(registerCodeActions).toHaveBeenCalledExactlyOnceWith(context, tracker) const messages = makeEventEmitter() const disposed = makeEventEmitter() @@ -297,6 +303,7 @@ describe("extension.ts", () => { "keeps focus trackers independent across extension activations", { timeout: activationTestTimeout }, async () => { + const firstContext = createMockContext() vi.resetModules() const { WebviewFocusTracker } = await import("../core/webview/WebviewFocusTracker") const { registerCodeActions } = await import("../activate") @@ -311,23 +318,21 @@ describe("extension.ts", () => { }, }) - await activate(mockContext) + await activate(firstContext) await activate(secondContext) - const firstTracker = mockContext.subscriptions.find((entry) => entry instanceof WebviewFocusTracker) + const firstTracker = firstContext.subscriptions.find((entry) => entry instanceof WebviewFocusTracker) const secondTracker = secondContext.subscriptions.find((entry) => entry instanceof WebviewFocusTracker) expect(firstTracker).toBeInstanceOf(WebviewFocusTracker) expect(secondTracker).toBeInstanceOf(WebviewFocusTracker) expect(secondTracker).not.toBe(firstTracker) - // A timed-out test leaves its activation running in this worker. Scope - // the assertion to the contexts of this test so a leaked call from that - // orphan cannot fail it. The order and identity contract is unchanged. - const ownCalls = vi - .mocked(registerCodeActions) - .mock.calls.filter(([context]) => context === mockContext || context === secondContext) - expect(ownCalls).toHaveLength(2) - expect(ownCalls[0]).toEqual([mockContext, firstTracker]) - expect(ownCalls[1]).toEqual([secondContext, secondTracker]) + // Match calls by context, not position: a leaked activation from a timed-out test may add calls. + const calls = vi.mocked(registerCodeActions).mock.calls + const firstCalls = calls.filter(([context]) => context === firstContext) + const secondCalls = calls.filter(([context]) => context === secondContext) + expect(firstCalls).toEqual([[firstContext, firstTracker]]) + expect(secondCalls).toEqual([[secondContext, secondTracker]]) + expect(calls.indexOf(firstCalls[0])).toBeLessThan(calls.indexOf(secondCalls[0])) firstTracker?.dispose() secondTracker?.dispose() secretChanges.dispose()