Skip to content

Commit 509f92c

Browse files
test(webview): spy the profile and modes methods instead of suppressing the manager swaps
The view-state tests replaced whole managers on a readonly field, and every one of those assignments needed a @ts-ignore: ten of them, at the profile-lookup helper, the failing-lookup and superseded-view cases, the empty-custom-modes and resetCustomModes cases, the profile-store save paths and the write-ownership case. The methods they stub are public on both managers, so the stubs now go on the methods with vi.spyOn and the directives go away: 17 suppression directives in the file, 7 left, all seven pre-existing and outside this PR's diff. The profile-lookup helper's stalled profile is now typed from the manager's own resolved type - Awaited<ReturnType<ProviderSettingsManager["getProfile"]>> - so the stubs are checked against the real signature with no assertion and no directive; keeping id optional there is deliberate, because the doubles omit it today and the load path strips name and keeps the rest as the buffered apiConfiguration, so an id that the doubles do not supply would show up in what those tests assert. Diff: one file, 34 insertions, 39 deletions. 39 deleted lines are 10 directives, the ten replacement-object blocks and the hand-written profile fields; not one deleted line is an it(...) or an expect(...), so this is deleted-duplicate 0 and deleted-coverage 0. Test-only: it contributes no changed executable lines to the mutation-diff buckets. Two controls, run rather than described. Remove the getProfile spy that the profile-lookup helper installs and core/webview goes red: Test Files 1 failed | 33 passed (34), Tests 5 failed | 962 passed (967) - the helper stub is load-bearing. Remove the three profile-store stubs on the saver manager in the two sibling-overlay cases and the suite stays green: 34 files, 967 tests. That is a real finding rather than a clean bill: those three stubs are not what makes those two cases pass today - the real manager against the mocked memento produces the same viewLocalState - so they are kept as isolation of intent, not sold as coverage, and the question of whether those cases should assert on the store calls at all is a question for the reviewer rather than something to hide behind a green run. With the stubs restored: 34 files, 967 tests, 0 failures; tsc -p . --noEmit exit 0; eslint --max-warnings=0 --no-warn-ignored on the file exit 0; prettier --check exit 0. Two tool artefacts measured on the way. eslint --prune-suppressions rewrote src/eslint-suppressions.json with 1441 added and 1441 removed lines, dropping entries for files outside this diff (anthropic-vertex.spec.ts and others); the spec's own count stayed 195, so the pruning was unrelated cleanup and the file was reverted to HEAD rather than shipped as a side effect of checking one file. And my inserted listConfig stub exceeded the print width, so the edited file genuinely failed prettier --check while the HEAD copy of the same file passed it: formatting was applied and the gates re-run, which is the difference between a file that needed work and the junction worktree's whole-tree prettier noise. Review row: database id 4477029124 (Trivial, Maintainability & Code Quality).
1 parent ec54bce commit 509f92c

1 file changed

Lines changed: 34 additions & 39 deletions

File tree

‎src/core/webview/__tests__/ClineProvider.spec.ts‎

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { defaultModeSlug } from "../../../shared/modes"
3030
import { experimentDefault } from "../../../shared/experiments"
3131
import { setTtsEnabled } from "../../../utils/tts"
3232
import { ContextProxy } from "../../config/ContextProxy"
33+
import type { ProviderSettingsManager } from "../../config/ProviderSettingsManager"
3334
import { WorkspaceIndexingEnablementManager } from "../../../services/code-index/workspace-indexing-enablement-manager"
3435
import { Task, TaskOptions } from "../../task/Task"
3536
import { safeWriteJson } from "../../../utils/safeWriteJson"
@@ -426,11 +427,10 @@ afterAll(() => {
426427
vi.restoreAllMocks()
427428
})
428429

429-
type StalledProfile = {
430-
name: string
431-
apiProvider?: string
432-
openRouterModelId?: string
433-
}
430+
// The stalled profile is typed from the manager's own return type so the profile spies below need no
431+
// assertion, and the shape stays what a real getProfile resolves to (id optional, so the doubles that omit
432+
// it keep matching what the load path receives).
433+
type StalledProfile = Awaited<ReturnType<ProviderSettingsManager["getProfile"]>>
434434

435435
function stallProviderSettingsProfile(provider: ClineProvider) {
436436
let resolveProfile: (value: StalledProfile) => void = () => {}
@@ -445,8 +445,10 @@ function stallProviderSettingsProfile(provider: ClineProvider) {
445445
}
446446
return new Promise<StalledProfile>((resolve) => (resolveProfile = resolve))
447447
})
448-
// @ts-ignore - Reassign the readonly providerSettingsManager for the test; the double only backs getProfile.
449-
provider.providerSettingsManager = { getProfile: getProfileSpy }
448+
// The manager field is readonly and getProfile is the only member this scenario reaches, so the spy goes on the
449+
// method rather than onto a replacement manager: the double stays checked against the real signature, the
450+
// one-call guard below still fails loudly on a second lookup, and no suppression directive is needed.
451+
vi.spyOn(provider.providerSettingsManager, "getProfile").mockImplementation(() => getProfileSpy())
450452
// Return a stable wrapper around the closure binding: resolveProfile is
451453
// reassigned to the pending promise's resolver once getProfile is called,
452454
// so returning the variable directly would hand the test the initial no-op.
@@ -6181,8 +6183,7 @@ describe("ClineProvider", () => {
61816183
new WebviewFocusTracker(),
61826184
)
61836185
const logSpy = vi.spyOn(provider, "log")
6184-
// @ts-ignore - Replace providerSettingsManager with a failing test double.
6185-
provider.providerSettingsManager = { getProfile: vi.fn().mockRejectedValue(new Error("profile missing")) }
6186+
vi.spyOn(provider.providerSettingsManager, "getProfile").mockRejectedValue(new Error("profile missing"))
61866187
await provider.saveViewState("currentApiConfigName", "my-profile")
61876188
await provider["setViewStateId"]("stable-sidebar-view")
61886189
expect(provider["viewLocalState"].currentApiConfigName).toBe("my-profile")
@@ -6200,13 +6201,10 @@ describe("ClineProvider", () => {
62006201
new WebviewFocusTracker(),
62016202
)
62026203
const logSpy = vi.spyOn(provider, "log")
6203-
// @ts-ignore - Replace providerSettingsManager with a test double that registers a newer id.
6204-
provider.providerSettingsManager = {
6205-
getProfile: vi.fn().mockImplementation(() => {
6206-
provider["viewStateId"] = "superseded-view"
6207-
return Promise.resolve({ name: "my-profile", apiProvider: providerIdentifiers.openrouter })
6208-
}),
6209-
}
6204+
vi.spyOn(provider.providerSettingsManager, "getProfile").mockImplementation(() => {
6205+
provider["viewStateId"] = "superseded-view"
6206+
return Promise.resolve({ name: "my-profile", apiProvider: providerIdentifiers.openrouter })
6207+
})
62106208
await provider.saveViewState("currentApiConfigName", "my-profile")
62116209
await provider["setViewStateId"]("stable-sidebar-view")
62126210
expect(provider["viewLocalState"]).not.toHaveProperty("apiConfiguration")
@@ -6224,8 +6222,7 @@ describe("ClineProvider", () => {
62246222
new WebviewFocusTracker(),
62256223
)
62266224
const logSpy = vi.spyOn(provider, "log")
6227-
// @ts-ignore - Replace customModesManager with a test double (no custom modes).
6228-
provider.customModesManager = { getCustomModes: vi.fn().mockResolvedValue([]), dispose: vi.fn() }
6225+
vi.spyOn(provider.customModesManager, "getCustomModes").mockResolvedValue([])
62296226
// The file-level modes mock resolves every slug to a mode; drop the pinned slug.
62306227
const modesModule = vi.mocked(await import("../../../shared/modes"))
62316228
const originalMode = modesModule.getModeBySlug("code")
@@ -6256,8 +6253,7 @@ describe("ClineProvider", () => {
62566253
new WebviewFocusTracker(),
62576254
)
62586255
const { getProfile: getProfileSpy, resolveProfile } = stallProviderSettingsProfile(provider)
6259-
// @ts-ignore - Replace customModesManager with a test double (no custom modes).
6260-
provider.customModesManager = { getCustomModes: vi.fn().mockResolvedValue([]), dispose: vi.fn() }
6256+
vi.spyOn(provider.customModesManager, "getCustomModes").mockResolvedValue([])
62616257
await provider.saveViewState("mode", "code")
62626258
await provider.saveViewState("currentApiConfigName", "cfg-a")
62636259
const load = provider["setViewStateId"]("stable-sidebar-view")
@@ -6548,13 +6544,11 @@ describe("ClineProvider", () => {
65486544
new WebviewFocusTracker(),
65496545
)
65506546
const logSpy = vi.spyOn(provider, "log")
6551-
const getProfileSpy = vi.fn().mockResolvedValue({
6547+
const getProfileSpy = vi.spyOn(provider.providerSettingsManager, "getProfile").mockResolvedValue({
65526548
name: "my-profile",
65536549
apiProvider: providerIdentifiers.openrouter,
65546550
openRouterModelId: "model-x",
65556551
})
6556-
// @ts-ignore - Replace providerSettingsManager with a test double for the profile lookup.
6557-
provider.providerSettingsManager = { getProfile: getProfileSpy }
65586552
// The ownership check requires the writer to release the id before the
65596553
// next view adopts it; the durable entry it wrote stays in storage.
65606554
await writer.dispose()
@@ -6654,8 +6648,7 @@ describe("ClineProvider", () => {
66546648
new WebviewFocusTracker(),
66556649
)
66566650
const logSpy = vi.spyOn(provider, "log")
6657-
// @ts-ignore - Replace customModesManager with a test double (no custom modes).
6658-
provider.customModesManager = { getCustomModes: vi.fn().mockResolvedValue([]), dispose: vi.fn() }
6651+
vi.spyOn(provider.customModesManager, "getCustomModes").mockResolvedValue([])
66596652
// The file-level modes mock resolves every slug to a mode; narrow it to the slugs under test.
66606653
const modesModule = vi.mocked(await import("../../../shared/modes"))
66616654
const originalMode = modesModule.getModeBySlug("code")
@@ -6712,8 +6705,8 @@ describe("ClineProvider", () => {
67126705
new WebviewFocusTracker(),
67136706
)
67146707
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
6715-
// @ts-ignore - Replace customModesManager with a test double (the real reset writes to disk).
6716-
provider.customModesManager = { resetCustomModes: vi.fn().mockResolvedValue(undefined), dispose: vi.fn() }
6708+
// The real resetCustomModes writes the modes file, so the method is stubbed rather than the manager replaced.
6709+
vi.spyOn(provider.customModesManager, "resetCustomModes").mockResolvedValue(undefined)
67176710
// The modal answer is a string label; the last-typed vscode overload expects a
67186711
// MessageItem. A double assertion is the last-resort cast here (AGENTS.md): the
67196712
// production path compares the answer against the string label directly, so the
@@ -6776,12 +6769,13 @@ describe("ClineProvider", () => {
67766769
new WebviewFocusTracker(),
67776770
)
67786771
await pinned.setValue("currentApiConfigName", "shared-profile")
6779-
// @ts-ignore - The double only backs the profile-store calls of the save path.
6780-
saver.providerSettingsManager = {
6781-
saveConfig: vi.fn().mockResolvedValue("shared-id"),
6782-
listConfig: vi.fn().mockResolvedValue([{ name: "shared-profile", id: "shared-id" }]),
6783-
setModeConfig: vi.fn().mockResolvedValue(undefined),
6784-
}
6772+
// Only these three profile-store calls are exercised by the save path, so each is stubbed on the real manager
6773+
// instead of replacing it, which keeps the stubs checked against the declared signatures.
6774+
vi.spyOn(saver.providerSettingsManager, "saveConfig").mockResolvedValue("shared-id")
6775+
vi.spyOn(saver.providerSettingsManager, "listConfig").mockResolvedValue([
6776+
{ name: "shared-profile", id: "shared-id" },
6777+
])
6778+
vi.spyOn(saver.providerSettingsManager, "setModeConfig").mockResolvedValue(undefined)
67856779
const providerSettings = { apiProvider: providerIdentifiers.anthropic, apiKey: "fresh-key" }
67866780

67876781
await saver.upsertProviderProfile("shared-profile", providerSettings, false)
@@ -6809,12 +6803,13 @@ describe("ClineProvider", () => {
68096803
new WebviewFocusTracker(),
68106804
)
68116805
await pinned.setValue("currentApiConfigName", "shared-profile")
6812-
// @ts-ignore - The double only backs the profile-store calls of the save path.
6813-
saver.providerSettingsManager = {
6814-
saveConfig: vi.fn().mockResolvedValue("shared-id"),
6815-
listConfig: vi.fn().mockResolvedValue([{ name: "shared-profile", id: "shared-id" }]),
6816-
setModeConfig: vi.fn().mockResolvedValue(undefined),
6817-
}
6806+
// Only these three profile-store calls are exercised by the save path, so each is stubbed on the real manager
6807+
// instead of replacing it, which keeps the stubs checked against the declared signatures.
6808+
vi.spyOn(saver.providerSettingsManager, "saveConfig").mockResolvedValue("shared-id")
6809+
vi.spyOn(saver.providerSettingsManager, "listConfig").mockResolvedValue([
6810+
{ name: "shared-profile", id: "shared-id" },
6811+
])
6812+
vi.spyOn(saver.providerSettingsManager, "setModeConfig").mockResolvedValue(undefined)
68186813
const providerSettings = { apiProvider: providerIdentifiers.anthropic, apiKey: "fresh-key" }
68196814

68206815
await saver.upsertProviderProfile("shared-profile", providerSettings, true)

0 commit comments

Comments
 (0)