Skip to content

Commit e7033c8

Browse files
author
Zoo (VP)
committed
test(dashboard): fix vitest isolation and add visual snapshot workflow
1 parent 4a0dc74 commit e7033c8

6 files changed

Lines changed: 310 additions & 202 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Update Visual Snapshots
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
branch:
7+
description: 'Branch to update snapshots on'
8+
required: true
9+
type: string
10+
11+
jobs:
12+
update-snapshots:
13+
runs-on: ubuntu-latest
14+
timeout-minutes: 10
15+
container:
16+
image: mcr.microsoft.com/playwright:v1.60.0-noble@sha256:9bd26ad900bb5e0f4dee75839e957a89ae89c2b7ab1e76050e559790e946b948
17+
options: --ipc=host
18+
permissions:
19+
contents: write
20+
21+
steps:
22+
- name: Checkout code
23+
uses: actions/checkout@v4
24+
with:
25+
ref: ${{ github.event.inputs.branch }}
26+
27+
- name: Setup Node.js and pnpm
28+
uses: ./.github/actions/setup-node-pnpm
29+
with:
30+
install-args: "--frozen-lockfile"
31+
32+
- name: Update Snapshots
33+
run: pnpm --filter @roo-code/vscode-webview test:visual -- --update-snapshots
34+
35+
- name: Commit and push changes
36+
uses: stefanzweifel/git-auto-commit-action@v5
37+
with:
38+
commit_message: "test(visual): update visual regression baselines [skip ci]"
39+
file_pattern: 'webview-ui/**/__screenshots__/*.png'

‎src/__tests__/extension.spec.ts‎

Lines changed: 91 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,76 @@
11
// npx vitest run __tests__/extension.spec.ts
22

3+
const { activateDashboardMock } = vi.hoisted(() => ({
4+
activateDashboardMock: vi.fn(),
5+
}))
6+
37
import type * as vscode from "vscode"
48
import 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

4575
vi.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.
5080
vi.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

5489
const mockCloudServiceInstance = {
@@ -110,6 +145,7 @@ vi.mock("../shared/language", () => ({
110145
vi.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,

‎src/eslint-suppressions.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"__tests__/extension.spec.ts": {
2828
"@typescript-eslint/no-explicit-any": {
29-
"count": 2
29+
"count": 1
3030
}
3131
},
3232
"__tests__/history-resume-delegation.spec.ts": {

‎webview-ui/playwright-ct.config.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ export default defineConfig({
8585
viewport: { width: 520, height: 360 },
8686
deviceScaleFactor: 1,
8787
colorScheme: "dark",
88+
video: "retain-on-failure",
89+
trace: "retain-on-failure",
8890
},
8991
expect: {
9092
toHaveScreenshot: {

0 commit comments

Comments
 (0)