Skip to content

Commit 7537805

Browse files
committed
test(webview): dispose temp-id providers and await the load deterministically
The three temporary-id tests constructed ClineProviders that were never disposed: the constructor registers each instance in the static activeInstances set, so leaked instances linger for every later test in the worker and can change what the cross-instance reset broadcast and getInstanceForView see. The failed-write test restored the rejecting globalState.update stub only on the green path, so a failing assertion left the stub in place for later tests. The load test waited a fixed 10 ms for the constructor's fire-and-forget loadViewState; it now captures the in-flight promise and awaits it, so the absence assertion runs against a settled buffer. The CloudService double gains on/off so dispose can unsubscribe. Proofs: ClineProvider.sticky-mode.spec 30/30; eslint --max-warnings=0 clean; tsc clean except the two known local telemetry-dist noise lines; prettier ALL-MATCHED on the touched file. Test-only change: no production behavior touched, so no mutation control applies.
1 parent 109f821 commit 7537805

1 file changed

Lines changed: 48 additions & 21 deletions

File tree

‎src/core/webview/__tests__/ClineProvider.sticky-mode.spec.ts‎

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ vi.mock("@roo-code/cloud", () => ({
121121
get instance() {
122122
return {
123123
isAuthenticated: vi.fn().mockReturnValue(false),
124+
on: vi.fn(),
125+
off: vi.fn(),
124126
}
125127
},
126128
},
@@ -1662,19 +1664,35 @@ describe("ClineProvider - Sticky Mode", () => {
16621664
[tempId]: { mode: "architect", currentApiConfigName: "stale-profile" },
16631665
})
16641666

1667+
// The constructor fires loadViewState without awaiting it. Capture the in-flight
1668+
// promise so the assertions below run against a settled buffer instead of racing
1669+
// a fixed delay that only passes by luck on a fast machine.
1670+
// Private member: no typed API exposes the load promise, hence the double assertion.
1671+
const proto = ClineProvider.prototype as unknown as { loadViewState: () => Promise<void> }
1672+
const originalLoad = proto.loadViewState
1673+
let loadDone: Promise<void> = Promise.resolve()
1674+
proto.loadViewState = function (this: unknown) {
1675+
loadDone = originalLoad.call(this)
1676+
return loadDone
1677+
}
16651678
const fresh = new ClineProvider(mockContext, mockOutputChannel, "sidebar", proxy, new WebviewFocusTracker())
1666-
expect(fresh.viewId).toBe(tempId)
1667-
1668-
// Let the constructor's fire-and-forget loadViewState settle.
1669-
await new Promise((resolve) => setTimeout(resolve, 10))
1670-
1671-
// The unowned temporary-id entry must not be adopted...
1672-
expect(fresh["viewLocalState"]).toStrictEqual({})
1673-
1674-
// ...and the shared values still win, so the stale entry cannot reach getState().
1675-
const state = await fresh.getState()
1676-
expect(state.mode).toBe("code")
1677-
expect(state.currentApiConfigName).not.toBe("stale-profile")
1679+
try {
1680+
expect(fresh.viewId).toBe(tempId)
1681+
await loadDone
1682+
1683+
// The unowned temporary-id entry must not be adopted...
1684+
expect(fresh["viewLocalState"]).toStrictEqual({})
1685+
1686+
// ...and the shared values still win, so the stale entry cannot reach getState().
1687+
const state = await fresh.getState()
1688+
expect(state.mode).toBe("code")
1689+
expect(state.currentApiConfigName).not.toBe("stale-profile")
1690+
} finally {
1691+
proto.loadViewState = originalLoad
1692+
// The constructor registers the instance in ClineProvider.activeInstances;
1693+
// leaving it there would leak it into every later test in this worker.
1694+
await fresh.dispose()
1695+
}
16781696
})
16791697

16801698
it("does not merge a stale temporary-id entry into this instance's first durable write", async () => {
@@ -1686,13 +1704,17 @@ describe("ClineProvider - Sticky Mode", () => {
16861704
await proxy.setValue("viewStates", { [tempId]: { currentApiConfigName: "old-profile", updatedAt: 1 } })
16871705

16881706
const fresh = new ClineProvider(mockContext, mockOutputChannel, "sidebar", proxy, new WebviewFocusTracker())
1689-
expect(fresh.viewId).toBe(tempId)
1707+
try {
1708+
expect(fresh.viewId).toBe(tempId)
16901709

1691-
await fresh.saveViewState("mode", "code")
1710+
await fresh.saveViewState("mode", "code")
16921711

1693-
const states = fresh["getPersistedViewStates"]({ fresh: true })
1694-
expect(states[tempId]).toEqual({ mode: "code", updatedAt: expect.any(Number) })
1695-
expect(fresh["viewLocalState"].currentApiConfigName).toBeUndefined()
1712+
const states = fresh["getPersistedViewStates"]({ fresh: true })
1713+
expect(states[tempId]).toEqual({ mode: "code", updatedAt: expect.any(Number) })
1714+
expect(fresh["viewLocalState"].currentApiConfigName).toBeUndefined()
1715+
} finally {
1716+
await fresh.dispose()
1717+
}
16961718
})
16971719

16981720
it("does not mark the temporary id as authored when the durable write fails", async () => {
@@ -1712,10 +1734,15 @@ describe("ClineProvider - Sticky Mode", () => {
17121734
return Promise.resolve()
17131735
})
17141736

1715-
await expect(fresh.saveViewState("mode", "code")).rejects.toThrow("storage unavailable")
1716-
expect(fresh["wroteUnderTemporaryViewStateId"]).toBe(false)
1717-
1718-
mockContext.globalState.update = originalUpdate
1737+
try {
1738+
await expect(fresh.saveViewState("mode", "code")).rejects.toThrow("storage unavailable")
1739+
expect(fresh["wroteUnderTemporaryViewStateId"]).toBe(false)
1740+
} finally {
1741+
// Restore even when an assertion fails: a rejecting stub left in place makes
1742+
// every later test in this worker fail for an unrelated reason.
1743+
mockContext.globalState.update = originalUpdate
1744+
await fresh.dispose()
1745+
}
17191746
})
17201747

17211748
it("does not adopt an entry under the temporary name that this instance never wrote", async () => {

0 commit comments

Comments
 (0)