Skip to content

Commit dd76f1a

Browse files
test(test-utils): type the Memento and secrets doubles and make freshness the helper's contract
The three fixture declarations in core/config/__tests__/ContextProxy.spec.ts were typed as any, so the file carried three of the five no-explicit-any suppressions it has. The shape they need already existed, privately, inside test-utils/vscode.ts: makeExtensionContext built its own local makeMemento(). That shape is now exported as makeFreshMemento and makeFreshSecrets, with typed double aliases (MementoDouble, SecretsDouble), and makeExtensionContext consumes the exported form so there is one definition rather than two local ones. This repo types ExtensionContext.globalState as Memento plus setKeysForSync and has no top-level vscode.Secrets, so the doubles are built from ExtensionContext["secrets"] and carry setKeysForSync: the double is typed to the interface instead of the call sites being loosened, and no call site needed a cast. Freshness is named, not inferred: the export is called makeFresh* and says in a comment that every call returns a new object with new spies, because a module-scope Memento in a shared test helper is a silent cross-test leak. The Memento double now keeps a real per-call store, and the pinned assertion writes a key through one double and asserts the next double cannot see it - its negative control (one shared store for every call) reddens exactly that assertion and nothing else, which is what shows the assertion is not vacuous. The secrets and extension doubles keep their payloads inline at the call site so the test still reads as the scenario rather than as the plumbing. Tests did not change meaning while their fixtures changed type: before, 67 passed of 67; after, 68 passed of 68, the single addition being the helper's own freshness assertion. tsc --noEmit over the tree is 0, which is the check that matters here because the change widens a module surface. eslint with the lowered entry is clean, so 2 is the real violation count rather than a permissive one. Suppression entry for core/config/__tests__/ContextProxy.spec.ts goes 5 -> 2, a decrease of 3, which is what the never-increase rule wants. The two survivors are the inline predicates at :1232 and :1248 ( (call: any[]) => call[0] === "customSupportPrompts"), which are outside this assignment and were left alone. upstream/main carries the same 5 for this file, so the baseline was not inherited looser than main's. The prune run reported no other entry changing, so this commit's suppressions diff is that one entry and the file is written by editing that entry in place so the diff shows one line rather than a re-emitted file. Test-only: no file outside src/test-utils and one spec changed, plus the suppression entry. Co-Authored-By: Claude <[email protected]>
1 parent ac3db54 commit dd76f1a

3 files changed

Lines changed: 110 additions & 17 deletions

File tree

‎src/core/config/__tests__/ContextProxy.spec.ts‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ import * as vscode from "vscode"
55
import { GLOBAL_STATE_KEYS, SECRET_STATE_KEYS, GLOBAL_SECRET_KEYS } from "@roo-code/types"
66

77
import { clearAllMocks } from "../../../test-utils/reset"
8-
import { makeExtensionContext, makeUri } from "../../../test-utils/vscode"
8+
import {
9+
makeExtensionContext,
10+
makeExtensionDouble,
11+
makeFreshMemento,
12+
makeFreshSecrets,
13+
makeUri,
14+
type MementoDouble,
15+
type SecretsDouble,
16+
} from "../../../test-utils/vscode"
917

1018
import { logger } from "../../../utils/logging"
1119
import { ContextProxy, SettingsWriteError } from "../ContextProxy"
@@ -24,26 +32,26 @@ vi.mock("vscode", () => ({
2432

2533
describe("ContextProxy", () => {
2634
let proxy: ContextProxy
27-
let mockContext: any
28-
let mockGlobalState: any
29-
let mockSecrets: any
35+
let mockContext: vscode.ExtensionContext
36+
let mockGlobalState: MementoDouble
37+
let mockSecrets: SecretsDouble
3038

3139
beforeEach(async () => {
3240
// Reset mocks
3341
clearAllMocks()
3442

3543
// Mock globalState
36-
mockGlobalState = {
44+
mockGlobalState = makeFreshMemento({
3745
get: vi.fn(),
3846
update: vi.fn().mockResolvedValue(undefined),
39-
}
47+
})
4048

4149
// Mock secrets
42-
mockSecrets = {
50+
mockSecrets = makeFreshSecrets({
4351
get: vi.fn().mockResolvedValue("test-secret"),
4452
store: vi.fn().mockResolvedValue(undefined),
4553
delete: vi.fn().mockResolvedValue(undefined),
46-
}
54+
})
4755

4856
// Mock the extension context
4957
mockContext = {
@@ -56,14 +64,26 @@ describe("ContextProxy", () => {
5664
}),
5765
globalStorageUri: makeUri("/test/storage"),
5866
logUri: makeUri("/test/logs"),
59-
extension: { packageJSON: { version: "1.0.0" } },
67+
extension: makeExtensionDouble({ packageJSON: { version: "1.0.0" } }),
6068
}
6169

6270
// Create proxy instance
6371
proxy = new ContextProxy(mockContext)
6472
await proxy.initialize()
6573
})
6674

75+
describe("shared test-utils doubles", () => {
76+
it("gives each Memento double its own stored values", () => {
77+
// Freshness is the helper's contract: a module-scope Memento in a shared test helper leaks
78+
// stored values between cases, and nothing else in this run would notice.
79+
const first = makeFreshMemento()
80+
void first.update("leakProbe", "stored-in-first")
81+
const second = makeFreshMemento()
82+
expect(second.get("leakProbe")).toBeUndefined()
83+
expect(second.keys()).toEqual([])
84+
})
85+
})
86+
6787
describe("read-only pass-through properties", () => {
6888
it("should return extension properties from the original context", () => {
6989
expect(proxy.extensionUri).toBe(mockContext.extensionUri)

‎src/eslint-suppressions.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@
536536
},
537537
"core/config/__tests__/ContextProxy.spec.ts": {
538538
"@typescript-eslint/no-explicit-any": {
539-
"count": 5
539+
"count": 2
540540
}
541541
},
542542
"core/config/__tests__/CustomModesManager.exportImportSlugChange.spec.ts": {

‎src/test-utils/vscode.ts‎

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { vi } from "vitest"
2+
import type { Mock } from "vitest"
23
import type * as vscode from "vscode"
34

45
export function makePosition(line = 0, character = 0): vscode.Position {
@@ -130,17 +131,89 @@ export function makeWorkspaceConfiguration(values: Record<string, unknown> = {})
130131
} as unknown as vscode.WorkspaceConfiguration
131132
}
132133

133-
export function makeExtensionContext(overrides: Partial<vscode.ExtensionContext> = {}): vscode.ExtensionContext {
134-
const makeMemento = () => ({
135-
get: vi.fn(),
136-
update: vi.fn().mockResolvedValue(undefined),
137-
keys: vi.fn(() => []),
134+
export type MementoDouble = vscode.Memento & {
135+
get: Mock
136+
update: Mock
137+
keys: Mock
138+
// This repo types ExtensionContext.globalState as Memento plus setKeysForSync, so the double
139+
// carries it: typing the double to the interface is what keeps the call sites unmodified.
140+
setKeysForSync: Mock
141+
}
142+
143+
// This repo's vscode typings have no top-level vscode.Secrets; the secrets shape is the one
144+
// ExtensionContext already declares, so the double is built from that rather than a name that
145+
// does not exist in this declaration file.
146+
export type SecretsDouble = vscode.ExtensionContext["secrets"] & {
147+
get: Mock
148+
store: Mock
149+
delete: Mock
150+
onDidChange: Mock
151+
}
152+
153+
/**
154+
* A VS Code Memento double. Every call returns a NEW object with NEW spies, so a value stored
155+
* through one double is invisible to the next: a module-scope Memento in a shared test helper is a
156+
* silent cross-test leak, and freshness is the helper's contract rather than an inference.
157+
*/
158+
export function makeFreshMemento(overrides: Partial<MementoDouble> = {}): MementoDouble {
159+
// Each call gets its own store, so a value written through one double cannot be read back through
160+
// the next. A shared Memento would leak, and only an assertion that writes and re-reads can show it.
161+
const store = new Map<string, unknown>()
162+
const get = vi.fn((key: string, defaultValue?: unknown) => (store.has(key) ? store.get(key) : defaultValue))
163+
const update = vi.fn((key: string, value: unknown) => {
164+
if (value === undefined) {
165+
store.delete(key)
166+
} else {
167+
store.set(key, value)
168+
}
169+
return Promise.resolve(undefined)
138170
})
171+
const keys = vi.fn(() => [...store.keys()])
172+
return {
173+
get,
174+
update,
175+
keys,
176+
setKeysForSync: vi.fn(),
177+
...overrides,
178+
}
179+
}
139180

181+
/** Same freshness rule as makeFreshMemento: a new object and new spies on every call. */
182+
export function makeFreshSecrets(overrides: Partial<SecretsDouble> = {}): SecretsDouble {
183+
return {
184+
get: vi.fn(),
185+
store: vi.fn().mockResolvedValue(undefined),
186+
delete: vi.fn().mockResolvedValue(undefined),
187+
onDidChange: vi.fn(() => makeDisposable()),
188+
...overrides,
189+
}
190+
}
191+
192+
/**
193+
* An Extension double for the fields a test actually reads. Extension is class-backed in VS Code,
194+
* so the structural cast stays here with this reason instead of spreading to each call site.
195+
*/
196+
export function makeExtensionDouble(overrides: Partial<vscode.Extension<unknown>> = {}): vscode.Extension<unknown> {
197+
const extensionUri = overrides.extensionUri ?? makeUri("/mock/extension")
198+
return {
199+
extensionKind: 1 as vscode.ExtensionKind,
200+
id: "mock.extension",
201+
extensionUri,
202+
extensionPath: overrides.extensionPath ?? "/mock/extension",
203+
isActive: false,
204+
manifest: {},
205+
packageJSON: { name: "mock-extension", version: "0.0.0" },
206+
activate: vi.fn().mockResolvedValue(undefined),
207+
exports: undefined,
208+
...overrides,
209+
} as unknown as vscode.Extension<unknown>
210+
}
211+
212+
export function makeExtensionContext(overrides: Partial<vscode.ExtensionContext> = {}): vscode.ExtensionContext {
140213
return {
141214
subscriptions: [],
142-
workspaceState: makeMemento(),
143-
globalState: makeMemento(),
215+
workspaceState: makeFreshMemento(),
216+
globalState: makeFreshMemento(),
144217
secrets: {
145218
get: vi.fn(),
146219
store: vi.fn().mockResolvedValue(undefined),

0 commit comments

Comments
 (0)