11// npx vitest run __tests__/extension.spec.ts
22
3+ const { activateDashboardMock } = vi . hoisted ( ( ) => ( {
4+ activateDashboardMock : vi . fn ( ) ,
5+ } ) )
6+
37import type * as vscode from "vscode"
48import type { AuthState } from "@roo-code/types"
59
6- vi . mock ( "vscode" , ( ) => ( {
7- window : {
8- createOutputChannel : vi . fn ( ) . mockReturnValue ( {
9- appendLine : vi . fn ( ) ,
10- } ) ,
11- registerWebviewViewProvider : vi . fn ( ) ,
12- registerUriHandler : vi . fn ( ) ,
13- tabGroups : {
14- onDidChangeTabs : vi . fn ( ) ,
10+ // Spread the rich shared base mock (`src/__mocks__/vscode.js`, wired via the
11+ // `resolve.alias.vscode` in vitest.config.ts) so module-load-time consumers
12+ // (e.g. DecorationController via the real ClineProvider import chain) get the
13+ // full API surface (createTextEditorDecorationType, CodeActionKind,
14+ // onDidCloseTerminal, ...). The previous minimal inline object replaced the
15+ // base mock entirely, so any vscode API not enumerated here was `undefined`
16+ // and crashed at import time.
17+ vi . mock ( "vscode" , async ( importOriginal ) => {
18+ const actual = await importOriginal < typeof import ( "vscode" ) > ( )
19+ return {
20+ ...actual ,
21+ window : {
22+ ...actual . window ,
23+ createOutputChannel : vi . fn ( ) . mockReturnValue ( {
24+ appendLine : vi . fn ( ) ,
25+ } ) ,
26+ registerWebviewViewProvider : vi . fn ( ) ,
27+ registerUriHandler : vi . fn ( ) ,
28+ tabGroups : {
29+ onDidChangeTabs : vi . fn ( ) ,
30+ } ,
31+ onDidChangeActiveTextEditor : vi . fn ( ) ,
32+ createTextEditorDecorationType : vi . fn ( ) . mockReturnValue ( {
33+ dispose : vi . fn ( ) ,
34+ } ) ,
1535 } ,
16- onDidChangeActiveTextEditor : vi . fn ( ) ,
17- } ,
18- workspace : {
19- registerTextDocumentContentProvider : vi . fn ( ) ,
20- getConfiguration : vi . fn ( ) . mockReturnValue ( {
21- get : vi . fn ( ) . mockReturnValue ( [ ] ) ,
22- } ) ,
23- createFileSystemWatcher : vi . fn ( ) . mockReturnValue ( {
24- onDidCreate : vi . fn ( ) ,
25- onDidChange : vi . fn ( ) ,
26- onDidDelete : vi . fn ( ) ,
27- dispose : vi . fn ( ) ,
28- } ) ,
29- onDidChangeWorkspaceFolders : vi . fn ( ) ,
30- } ,
31- languages : {
32- registerCodeActionsProvider : vi . fn ( ) ,
33- } ,
34- commands : {
35- executeCommand : vi . fn ( ) ,
36- } ,
37- env : {
38- language : "en" ,
39- } ,
40- ExtensionMode : {
41- Production : 1 ,
42- } ,
43- } ) )
36+ workspace : {
37+ ...actual . workspace ,
38+ registerTextDocumentContentProvider : vi . fn ( ) ,
39+ getConfiguration : vi . fn ( ) . mockReturnValue ( {
40+ get : vi . fn ( ) . mockReturnValue ( [ ] ) ,
41+ } ) ,
42+ createFileSystemWatcher : vi . fn ( ) . mockReturnValue ( {
43+ onDidCreate : vi . fn ( ) ,
44+ onDidChange : vi . fn ( ) ,
45+ onDidDelete : vi . fn ( ) ,
46+ dispose : vi . fn ( ) ,
47+ } ) ,
48+ onDidChangeWorkspaceFolders : vi . fn ( ) ,
49+ } ,
50+ languages : {
51+ ...actual . languages ,
52+ registerCodeActionsProvider : vi . fn ( ) ,
53+ } ,
54+ commands : {
55+ ...actual . commands ,
56+ executeCommand : vi . fn ( ) ,
57+ } ,
58+ env : {
59+ ...actual . env ,
60+ language : "en" ,
61+ } ,
62+ ExtensionMode : {
63+ Production : 1 ,
64+ Development : 2 ,
65+ Test : 3 ,
66+ } ,
67+ // The base mock exports `Disposable` as a plain `{ dispose }` object, which
68+ // lacks the `Disposable.from(...)` static the real ClineProvider uses.
69+ Disposable : {
70+ from : vi . fn ( ) . mockReturnValue ( { dispose : vi . fn ( ) } ) ,
71+ } ,
72+ }
73+ } )
4474
4575vi . mock ( "dotenv" , ( ) => ( {
4676 config : vi . fn ( ) ,
@@ -49,6 +79,11 @@ vi.mock("dotenv", () => ({
4979// Mock fs so the extension module can safely check for optional .env.
5080vi . mock ( "fs" , ( ) => ( {
5181 existsSync : vi . fn ( ) . mockReturnValue ( false ) ,
82+ // The CompactTransport (reached through ContextProxy during activation)
83+ // lazily creates its log file. Stub these so activation never touches the
84+ // real filesystem.
85+ mkdirSync : vi . fn ( ) ,
86+ writeFileSync : vi . fn ( ) ,
5287} ) )
5388
5489const mockCloudServiceInstance = {
@@ -110,6 +145,7 @@ vi.mock("../shared/language", () => ({
110145vi . mock ( "../core/config/ContextProxy" , ( ) => ( {
111146 ContextProxy : {
112147 getInstance : vi . fn ( ) . mockResolvedValue ( {
148+ globalStorageUri : { fsPath : "/mock/global-storage-path" } ,
113149 getValue : vi . fn ( ) ,
114150 setValue : vi . fn ( ) ,
115151 getValues : vi . fn ( ) . mockReturnValue ( { } ) ,
@@ -168,6 +204,7 @@ vi.mock("../activate", () => ({
168204 registerCommands : vi . fn ( ) ,
169205 registerCodeActions : vi . fn ( ) ,
170206 registerTerminalActions : vi . fn ( ) ,
207+ activateDashboard : activateDashboardMock ,
171208 CodeActionProvider : vi . fn ( ) . mockImplementation ( function ( ) {
172209 return {
173210 providedCodeActionKinds : [ ] ,
@@ -227,6 +264,11 @@ describe("extension.ts", () => {
227264
228265 mockContext = {
229266 extensionPath : "/test/path" ,
267+ extensionUri : { fsPath : "/test/path" } ,
268+ // The real ContextProxy reads `context.globalStorageUri.fsPath`; the
269+ // real ClineProvider then reads `contextProxy.globalStorageUri.fsPath`.
270+ // Provide it so activation does not depend on mock interception.
271+ globalStorageUri : { fsPath : "/test/storage" } ,
230272 globalState : {
231273 get : vi . fn ( ) . mockReturnValue ( undefined ) ,
232274 update : vi . fn ( ) ,
@@ -291,13 +333,22 @@ describe("extension.ts", () => {
291333
292334 vi . mocked ( CloudService . hasInstance ) . mockReturnValue ( true )
293335
336+ // The relative `vi.mock("../core/webview/ClineProvider")` is not
337+ // intercepted under Vitest 4 in this environment (the real provider is
338+ // constructed during activation), and no webview is resolved, so the
339+ // real `getVisibleInstance()` returns undefined. Stub the static to
340+ // return a controllable provider so we can assert the auth handler
341+ // pushes webview state exactly once.
342+ const provider = { postStateToWebviewWithoutClineMessages : vi . fn ( ) }
343+ vi . spyOn (
344+ ClineProvider as unknown as { getVisibleInstance : ( ) => unknown } ,
345+ "getVisibleInstance" ,
346+ ) . mockReturnValue ( provider )
347+
294348 // Activate the extension
295349 const { activate } = await import ( "../extension" )
296350 await activate ( mockContext )
297351
298- const provider = ( ClineProvider as any ) . getVisibleInstance ( )
299- provider . postStateToWebviewWithoutClineMessages . mockClear ( )
300-
301352 await authStateChangedHandler ! ( {
302353 state : "active-session" as AuthState ,
303354 previousState : "logged-out" as AuthState ,
0 commit comments