Skip to content

Commit 1ffdc2e

Browse files
test(webview): cover the profile-list re-derive failing at its write
ClineProvider.ts:2844-2847 re-derives the profile list cache on a dirty flag: it re-reads the list from disk, writes it back, and clears the flag only after both halves land. The rows beside this one cover the two ends - a re-derive whose disk read rejects, and a re-derive that succeeds all the way through - and neither reaches the middle case, the read succeeding while the write that follows it rejects. A re-ordering that clears the flag between the read and the write passes the whole existing file, so the contract that the flag survives its own rejected write was untested. The new row arms the two rejections separately - the switch tail's write, which is what sets the flag, and the recovery's own write, which is what the row is about - and asserts that the state post still lands, that the flag stays set, that the cache still holds the pre-switch list, and that a later assembly retries and heals it. expect(recoveryWriteRejected).toBe(true) sits in the row so a reader can see the failure was produced on the write rather than on the read. Control. With the flag cleared one line earlier, between the re-read and the write (the re-ordering this row exists to catch), the file is 1 failed | 35 passed (36), the single red being this row at spec.ts:891 on expect(provider["profileListCacheDirty"]).toBe(true) - expected false to be true. The same re-ordering against the file as it stood before this row is 35 passed (35), which is the gap stated as a number. Against the shipped source the file is 36 passed (36) and core/webview is 35 files, 1156 passed (1156). +88/-0 in this one spec file - 70 executable lines, 14 comment lines and 4 blank lines, no production file touched. Measured with .tmp-node22 node v22.23.4 and vitest.core.config.ts, run from src. eslint --prune-suppressions --max-warnings=0 exits 0 with src/eslint-suppressions.json restored byte-for-byte, repo-root prettier --check exits 0, and tsc --noEmit from src reports the same single error before and after this change - activate/registerCommands.ts(225,2) TS2561 - in a file this commit does not touch.
1 parent dd76f1a commit 1ffdc2e

1 file changed

Lines changed: 88 additions & 0 deletions

File tree

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,94 @@ describe("ClineProvider - Sticky Mode", () => {
812812
expect(provider.getValue("listApiConfigMeta")).not.toEqual(fresh)
813813
})
814814

815+
// The two rows above cover a re-derive that fails at the disk read and a re-derive that
816+
// succeeds all the way through. Neither reaches the middle case the guard in
817+
// ClineProvider.ts:2844-2847 is written for: the re-read from disk succeeds and the write
818+
// that follows it rejects. The flag is cleared only after both halves land, so this is the
819+
// case that fails when the clear moves above the write - a flag cleared before its write
820+
// lands is the dropped write again with a fix written around it. The rejection used for the
821+
// switch tail is not the rejection used for the recovery: the row arms them separately so
822+
// the failure can only have arrived on the recovery's own write.
823+
it("keeps the dirty flag set when the re-derive reads disk but its write rejects", async () => {
824+
const mockTask = Object.assign(
825+
{} as Task,
826+
{
827+
taskId: "recovery-write-rejected-task",
828+
taskMode: "code",
829+
_taskMode: "code",
830+
emit: vi.fn(),
831+
saveClineMessages: vi.fn(),
832+
clineMessages: [],
833+
apiConversationHistory: [],
834+
updateApiConfiguration: vi.fn(),
835+
} as Partial<Task>,
836+
)
837+
const historyItem: HistoryItem = {
838+
id: "recovery-write-rejected-task",
839+
ts: Date.now(),
840+
task: "recovery write rejected",
841+
mode: "code",
842+
number: 1,
843+
tokensIn: 0,
844+
tokensOut: 0,
845+
cacheWrites: 0,
846+
cacheReads: 0,
847+
totalCost: 0,
848+
}
849+
vi.spyOn(provider.taskHistoryStore, "get").mockReturnValue(historyItem)
850+
vi.spyOn(provider, "updateTaskHistory").mockImplementation((item: HistoryItem) => {
851+
historyItem.mode = item.mode
852+
return Promise.resolve([])
853+
})
854+
await provider.addClineToStack(mockTask)
855+
await provider.contextProxy.setValue("mode", "code")
856+
const fresh = [{ id: "fresh-id", name: "fresh-profile" }]
857+
const listConfig = vi.spyOn(provider.providerSettingsManager, "listConfig").mockResolvedValue(fresh)
858+
vi.spyOn(provider.providerSettingsManager, "getModeConfigId").mockResolvedValue(undefined)
859+
const proxy = provider.contextProxy
860+
const originalSetValue = proxy.setValue.bind(proxy)
861+
let phase: "switch" | "recovery" | "restored" = "switch"
862+
let switchWriteRejected = false
863+
let recoveryWriteRejected = false
864+
const write = vi.spyOn(proxy, "setValue").mockImplementation((key, value) => {
865+
if (key === "listApiConfigMeta") {
866+
if (phase === "switch" && !switchWriteRejected) {
867+
switchWriteRejected = true
868+
return Promise.reject(new Error("switch tail cache write rejected"))
869+
}
870+
if (phase === "recovery" && !recoveryWriteRejected) {
871+
recoveryWriteRejected = true
872+
return Promise.reject(new Error("recovery cache write rejected"))
873+
}
874+
}
875+
return originalSetValue(key, value)
876+
})
877+
878+
// The rejected switch tail is what sets the flag; ClineProvider.ts:2968-2973 keeps
879+
// propagating that rejection, so the switch is awaited through a catch.
880+
await provider["handleModeSwitchUnlocked"]("ask", mockTask).catch(() => {})
881+
expect(provider["profileListCacheDirty"]).toBe(true)
882+
883+
phase = "recovery"
884+
const assembliesBefore = listConfig.mock.calls.length
885+
// The re-read succeeds - listConfig is still resolving - and the state post survives the
886+
// rejected write, because the recovery swallows its own failure rather than turning every
887+
// state assembly into a failing call.
888+
await provider.getStateToPostToWebview()
889+
expect(recoveryWriteRejected).toBe(true)
890+
expect(listConfig).toHaveBeenCalledTimes(assembliesBefore + 1)
891+
expect(provider["profileListCacheDirty"]).toBe(true)
892+
expect(provider.getValue("listApiConfigMeta")).not.toEqual(fresh)
893+
894+
write.mockRestore()
895+
phase = "restored"
896+
// A flag that stays set is only worth having if a later assembly retries and heals it.
897+
await provider.getStateToPostToWebview()
898+
expect(provider["profileListCacheDirty"]).toBe(false)
899+
expect(provider.getValue("listApiConfigMeta")).toEqual(fresh)
900+
expect(listConfig).toHaveBeenCalledTimes(assembliesBefore + 2)
901+
})
902+
815903
it("does not re-derive when nothing is dirty", async () => {
816904
// The flag is the only trigger: without it the recovery would turn every state post into a disk
817905
// read, a worse contract than the stale cache it repairs.

0 commit comments

Comments
 (0)