Skip to content

Commit cf9206a

Browse files
author
easonLiangWorldedtech
committed
test(task): stop leaking the fixed-task-id record between save-stage tests
The task uuid is mocked to a fixed value, so the ui_messages.json this save writes outlives the test that created it. The sibling test below removes the whole directory and recreates it, which is the only reason the leak has not surfaced: with merge = true, any later save that reads its merged output would fold in a stale record and fail by run order rather than by behavior. The body now runs in try/finally, removing ui_messages.json and restoring both spies whatever the assertions do. The spies moved out of the try so the finally can see them. Local run: 162 passed in Task.spec.ts (the one remaining failure, blocks a truncated write_to_file call instead of executing it, fails identically at cf5abe6 without this change - local artifact), eslint clean, no suppression change.
1 parent cf5abe6 commit cf9206a

1 file changed

Lines changed: 36 additions & 28 deletions

File tree

‎src/core/task/__tests__/Task.spec.ts‎

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6158,42 +6158,50 @@ describe("Cline", () => {
61586158
// task directory to exist (uuid v7 is mocked to the fixed id below).
61596159
const taskDir = path.join(os.tmpdir(), "test-storage", "tasks", "00000000-0000-7000-8000-000000000000")
61606160
fsReal.mkdirSync(taskDir, { recursive: true })
6161+
// Declared outside the try so the finally block can restore them.
61616162
const updateSpy = vi
61626163
.spyOn(getTaskTestAccess(Task.prototype), "updateClineMessage")
61636164
.mockResolvedValue(undefined)
6164-
const metadataFailure = new Error("task history stage failed")
6165-
const historySpy = vi.spyOn(mockProvider, "updateTaskHistory").mockRejectedValueOnce(metadataFailure)
6166-
6167-
const task = new Task({
6168-
provider: mockProvider,
6169-
apiConfiguration: mockApiConfig,
6170-
task: "test task",
6171-
startTask: false,
6172-
})
6165+
const historySpy = vi.spyOn(mockProvider, "updateTaskHistory")
6166+
try {
6167+
historySpy.mockRejectedValueOnce(new Error("task history stage failed"))
61736168

6174-
const partialToolAsk = {
6175-
ts: Date.now() - 1,
6176-
type: "ask" as const,
6177-
ask: "tool" as const,
6178-
text: "partial tool message",
6179-
partial: true,
6180-
}
6169+
const task = new Task({
6170+
provider: mockProvider,
6171+
apiConfiguration: mockApiConfig,
6172+
task: "test task",
6173+
startTask: false,
6174+
})
61816175

6182-
task.clineMessages.push(partialToolAsk)
6176+
const partialToolAsk = {
6177+
ts: Date.now() - 1,
6178+
type: "ask" as const,
6179+
ask: "tool" as const,
6180+
text: "partial tool message",
6181+
partial: true,
6182+
}
61836183

6184-
await expect(task.finalizePartialToolAsk("partial tool message")).resolves.toBeUndefined()
6185-
await flushMicrotasks()
6184+
task.clineMessages.push(partialToolAsk)
61866185

6187-
expect(partialToolAsk.partial).toBe(false)
6188-
expect(task.clineMessages[0].isAnswered).toBe(true)
6189-
// The message array persisted, so the webview update must run even though a
6190-
// later save stage failed...
6191-
expect(updateSpy).toHaveBeenCalledWith(partialToolAsk)
6192-
// ...and the later-stage failure is observed instead of silently swallowed.
6193-
expect(consoleErrorSpy).toHaveBeenCalledWith("Failed to save task metadata:", expect.any(Error))
6186+
await expect(task.finalizePartialToolAsk("partial tool message")).resolves.toBeUndefined()
6187+
await flushMicrotasks()
61946188

6195-
updateSpy.mockRestore()
6196-
historySpy.mockRestore()
6189+
expect(partialToolAsk.partial).toBe(false)
6190+
expect(task.clineMessages[0].isAnswered).toBe(true)
6191+
// The message array persisted, so the webview update must run even though a
6192+
// later save stage failed...
6193+
expect(updateSpy).toHaveBeenCalledWith(partialToolAsk)
6194+
// ...and the later-stage failure is observed instead of silently swallowed.
6195+
expect(consoleErrorSpy).toHaveBeenCalledWith("Failed to save task metadata:", expect.any(Error))
6196+
} finally {
6197+
// The task uuid is mocked to a fixed value, so the record this save writes outlives
6198+
// the test. Later saves run with merge = true and would fold a stale
6199+
// ui_messages.json into their own message arrays, so drop it here instead of
6200+
// leaving it for whichever test cleans up next.
6201+
fsReal.rmSync(path.join(taskDir, "ui_messages.json"), { force: true })
6202+
updateSpy.mockRestore()
6203+
historySpy.mockRestore()
6204+
}
61976205
})
61986206

61996207
it("finalizePartialToolAsk skips the webview update when the message write itself fails", async () => {

0 commit comments

Comments
 (0)