Skip to content

Commit bc72f33

Browse files
zoomote2[bot]roomote
authored andcommitted
[Refactor] Add test-utils guardrails and convert remaining stragglers (#2279)
* refactor: add test-utils guardrails and convert remaining stragglers Co-authored-by: Roomote <roomote@roomote.dev> * refactor: convert new test-utils stragglers to shared helpers --------- Co-authored-by: Roomote <roomote@roomote.dev>
1 parent 7db02d9 commit bc72f33

22 files changed

Lines changed: 217 additions & 148 deletions

‎.github/pull_request_template.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Write the steps that test your changes. This helps reviewers check your work.
4040
- [ ] **Self-Review**: I reviewed my own code with care.
4141
- [ ] **Writing Style**: The PR title and description use Simplified Technical English (ASD-STE100): short sentences, plain words, active voice, one idea per sentence, no contractions.
4242
- [ ] **Testing**: New and/or updated tests have been added to cover my changes (if applicable).
43+
- [ ] **Test Helpers** (test changes only): Shared test helpers (`src/test-utils/`, `@/utils/test-utils`) are used only for mechanical duplication; test intent and scenario-specific mocks remain visible.
4344
- [ ] **Visual Snapshot** (UI changes only): If a user would notice this change at a glance (layout, theme tokens, brand elements, empty/error states), I added or updated a `*.visual.tsx` snapshot in `webview-ui/`. See `webview-ui/AGENTS.md` → "When a UI change needs a snapshot".
4445
- [ ] **Documentation Impact**: I checked if my changes need documentation updates (see "Documentation Updates" section below).
4546
- [ ] **Contribution Guidelines**: I read and agree to the [Contributor Guidelines](/CONTRIBUTING.md).

‎src/api/providers/__tests__/github-copilot.spec.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const mockLanguageModelChat = {
119119
}
120120

121121
describe("connectGitHubCopilot", () => {
122-
beforeEach(() => vi.clearAllMocks())
122+
beforeEach(() => clearAllMocks())
123123

124124
it("authenticates through VS Code and selects only Copilot models", async () => {
125125
vi.mocked(vscode.authentication.getSession).mockResolvedValue({
@@ -413,7 +413,7 @@ describe("Copilot model capabilities", () => {
413413
}
414414

415415
beforeEach(() => {
416-
vi.clearAllMocks()
416+
clearAllMocks()
417417
mockLanguageModelChat.countTokens.mockResolvedValue(10)
418418
mockLanguageModelChat.sendRequest.mockImplementation(async () => ({
419419
stream: (async function* () {
@@ -1322,7 +1322,7 @@ describe("getGitHubCopilotModels", () => {
13221322
...overrides,
13231323
})
13241324

1325-
beforeEach(() => vi.clearAllMocks())
1325+
beforeEach(() => clearAllMocks())
13261326

13271327
it("describes each model by identity and by the capabilities the host reports", async () => {
13281328
vi.mocked(vscode.lm.selectChatModels).mockResolvedValue([

‎src/api/providers/__tests__/io-intelligence.spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ioIntelligenceDefaultModelId, providerIdentifiers } from "@roo-code/typ
99

1010
import { buildApiHandler } from "../../index"
1111
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
12+
import { clearAllMocks } from "../../../test-utils/reset"
1213
import { IOIntelligenceHandler } from "../io-intelligence"
1314
import { getModels } from "../fetchers/modelCache"
1415

@@ -42,7 +43,7 @@ const loggedText = (spy: { mock: { calls: unknown[][] } }) =>
4243

4344
describe("IOIntelligenceHandler", () => {
4445
beforeEach(() => {
45-
vi.clearAllMocks()
46+
clearAllMocks()
4647
vi.mocked(getModels).mockResolvedValue({
4748
"meta-llama/Llama-3.3-70B-Instruct": {
4849
maxTokens: 8192,

‎src/api/providers/__tests__/nanogpt.spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { nanoGptDefaultModelId, providerIdentifiers } from "@roo-code/types"
99

1010
import { buildApiHandler } from "../../index"
1111
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
12+
import { clearAllMocks } from "../../../test-utils/reset"
1213
import { createReadFileTool } from "../../../core/prompts/tools/native-tools/read_file"
1314
import { NanoGptHandler } from "../nanogpt"
1415
import { getModels } from "../fetchers/modelCache"
@@ -45,7 +46,7 @@ const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "H
4546

4647
describe("NanoGptHandler", () => {
4748
beforeEach(() => {
48-
vi.clearAllMocks()
49+
clearAllMocks()
4950
vi.mocked(getModels).mockResolvedValue({
5051
"model:thinking": {
5152
maxTokens: 128000,

‎src/api/providers/__tests__/vscode-lm.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6676,7 +6676,7 @@ describe("getVsCodeLmModels", () => {
66766676
...overrides,
66776677
})
66786678

6679-
beforeEach(() => vi.clearAllMocks())
6679+
beforeEach(() => clearAllMocks())
66806680

66816681
it("returns serializable model metadata with host-reported image support", async () => {
66826682
const model = liveModel({ capabilities: { supportsImageToText: true } })

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ describe("CustomModesManager", () => {
198198
const firstResult = await manager.getCustomModes()
199199

200200
// Reset mock to verify it's not called again
201-
vi.clearAllMocks()
201+
clearAllMocks()
202202

203203
// Setup mocks again for second call
204204
;(fileExistsAtPath as Mock).mockImplementation(async (path: string) => {
@@ -236,7 +236,7 @@ describe("CustomModesManager", () => {
236236
await manager.getCustomModes()
237237

238238
// Reset mocks to track new calls
239-
vi.clearAllMocks()
239+
clearAllMocks()
240240

241241
// Update a mode
242242
const updatedMode: ModeConfig = {
@@ -260,7 +260,7 @@ describe("CustomModesManager", () => {
260260
await manager.updateCustomMode("mode1", updatedMode)
261261

262262
// Reset mocks again
263-
vi.clearAllMocks()
263+
clearAllMocks()
264264

265265
// Next call should read from file again (cache invalidated)
266266
await manager.getCustomModes()
@@ -282,7 +282,7 @@ describe("CustomModesManager", () => {
282282
await manager.getCustomModes()
283283

284284
// Reset mocks to track new calls
285-
vi.clearAllMocks()
285+
clearAllMocks()
286286

287287
// Delete a mode
288288
await manager.deleteCustomMode("mode1")
@@ -296,7 +296,7 @@ describe("CustomModesManager", () => {
296296
})
297297

298298
// Reset mocks again
299-
vi.clearAllMocks()
299+
clearAllMocks()
300300

301301
// Next call should read from file again (cache invalidated)
302302
await manager.getCustomModes()
@@ -321,7 +321,7 @@ describe("CustomModesManager", () => {
321321
await manager.getCustomModes()
322322

323323
// Reset mocks to track new calls
324-
vi.clearAllMocks()
324+
clearAllMocks()
325325

326326
// Setup for update
327327
const updatedMode: ModeConfig = {
@@ -346,7 +346,7 @@ describe("CustomModesManager", () => {
346346
await manager.updateCustomMode("mode1", updatedMode)
347347

348348
// Reset mocks again
349-
vi.clearAllMocks()
349+
clearAllMocks()
350350

351351
// Setup mocks again
352352
;(fileExistsAtPath as Mock).mockImplementation(async (path: string) => {
@@ -387,7 +387,7 @@ describe("CustomModesManager", () => {
387387
await manager.getCustomModes()
388388

389389
// Reset mock to verify it's not called again
390-
vi.clearAllMocks()
390+
clearAllMocks()
391391

392392
// Setup mocks again for second call
393393
;(fileExistsAtPath as Mock).mockImplementation(async (path: string) => {
@@ -408,7 +408,7 @@ describe("CustomModesManager", () => {
408408
currentTime += 11000
409409

410410
// Reset mocks again
411-
vi.clearAllMocks()
411+
clearAllMocks()
412412

413413
// Setup mocks again for third call
414414
;(fileExistsAtPath as Mock).mockImplementation(async (path: string) => {

‎src/eslint.config.mjs‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ const testPathTraversalSelectors = [
2727
},
2828
]
2929

30+
// Test-utils rollout guardrail: lanes converted to the shared reset helper
31+
// stay converted. Use clearAllMocks from test-utils/reset.
32+
const testResetHelperSelector = {
33+
selector:
34+
"CallExpression[callee.object.name='vi'][callee.property.name='clearAllMocks'], CallExpression[callee.object.name='vitest'][callee.property.name='clearAllMocks']",
35+
message:
36+
"Use the shared clearAllMocks() helper from src/test-utils/reset instead of calling vi.clearAllMocks() directly.",
37+
}
38+
3039
/** @type {import("eslint").Linter.Config} */
3140
export default [
3241
...config,
@@ -85,9 +94,6 @@ export default [
8594
"@typescript-eslint/no-floating-promises": "error",
8695
},
8796
},
88-
{
89-
ignores: [".stryker-tmp", "webview-ui", "out"],
90-
},
9197
{
9298
// Tests inside __tests__/ directories must not traverse 3+ levels above
9399
// the test file. That escapes the src/ package root and makes Turbo
@@ -112,4 +118,34 @@ export default [
112118
"no-restricted-syntax": ["error", ...testPathTraversalSelectors, ...apiProviderTestSelectors],
113119
},
114120
},
121+
{
122+
// Test-utils rollout guardrail: lanes converted to the shared reset
123+
// helpers stay converted. Use clearAllMocks from test-utils/reset.
124+
// no-restricted-syntax replaces earlier blocks for the same files, so
125+
// repeat the traversal selectors here.
126+
files: [
127+
"core/config/__tests__/**/*.ts",
128+
"services/code-index/**/__tests__/**/*.ts",
129+
"integrations/terminal/**/__tests__/**/*.ts",
130+
],
131+
rules: {
132+
"no-restricted-syntax": ["error", ...testPathTraversalSelectors, testResetHelperSelector],
133+
},
134+
},
135+
{
136+
// Same guardrail for the API provider lane; this block also replaces the
137+
// API provider hygiene selectors, so repeat those too.
138+
files: ["api/providers/__tests__/**/*.ts"],
139+
rules: {
140+
"no-restricted-syntax": [
141+
"error",
142+
...testPathTraversalSelectors,
143+
...apiProviderTestSelectors,
144+
testResetHelperSelector,
145+
],
146+
},
147+
},
148+
{
149+
ignores: [".stryker-tmp", "webview-ui", "out"],
150+
},
115151
]

‎src/services/code-index/__tests__/code-index-manager-registry.spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from "vscode"
22
import { makeExtensionContext, makeTextDocument, makeTextEditor, makeUri } from "../../../test-utils/vscode"
3+
import { clearAllMocks } from "../../../test-utils/reset"
34
import { CodeIndexManager } from "../manager"
45
import { CodeIndexManagerRegistry } from "../code-index-manager-registry"
56
import { CodeIndexWorkspaceScope } from "../code-index-workspace-scope"
@@ -25,7 +26,7 @@ describe("CodeIndexManagerRegistry", () => {
2526
let second: vscode.WorkspaceFolder
2627

2728
beforeEach(() => {
28-
vi.clearAllMocks()
29+
clearAllMocks()
2930
context = makeExtensionContext()
3031
first = { uri: makeUri("/first"), name: "first", index: 0 }
3132
second = { uri: makeUri("/second"), name: "second", index: 1 }

‎src/services/code-index/__tests__/code-index-scope.spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { makeExtensionContext } from "../../../test-utils/vscode"
2+
import { clearAllMocks } from "../../../test-utils/reset"
23
import { CodeIndexScope } from "../code-index-scope"
34
import { CodeIndexStatusManager } from "../code-index-status-manager"
45
import { CodeIndexManagerRegistry } from "../code-index-manager-registry"
@@ -9,7 +10,7 @@ vi.mock("../code-index-manager-registry", () => ({
910
}))
1011

1112
describe("CodeIndexScope", () => {
12-
beforeEach(() => vi.clearAllMocks())
13+
beforeEach(() => clearAllMocks())
1314

1415
it("creates, initializes and disposes its status manager", () => {
1516
const context = makeExtensionContext()

‎src/services/code-index/__tests__/code-index-status-manager.spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
makeExtensionContext,
77
makeWorkspaceConfiguration,
88
} from "../../../test-utils/vscode"
9+
import { clearAllMocks } from "../../../test-utils/reset"
910
import { CodeIndexManager } from "../manager"
1011
import { CodeIndexStateManager } from "../state-manager"
1112
import { CodeIndexStatusManager, type CodeIndexStatus } from "../code-index-status-manager"
@@ -68,7 +69,7 @@ describe("CodeIndexStatusManager", () => {
6869
let disposeWorkspaceFolders: ReturnType<typeof vi.fn<() => void>>
6970

7071
beforeEach(() => {
71-
vi.clearAllMocks()
72+
clearAllMocks()
7273
vi.mocked(vscode.workspace.getConfiguration).mockReturnValue(makeWorkspaceConfiguration())
7374
Object.defineProperty(vscode.window, "activeTextEditor", { configurable: true, value: undefined })
7475
Object.defineProperty(vscode.workspace, "workspaceFolders", {

0 commit comments

Comments
 (0)