Skip to content

Commit 2fe58bd

Browse files
PierrunoYTclaude
andcommitted
test(task): snapshot posted partial updates and cover ask/dispose cancellation
- Capture ts, text, and partial at each messageUpdated post. Task mutates one ClineMessage, so reading the mock calls by reference showed the latest values for every post. - Add tests that a queued partial update is dropped when a partial ask is finalized and when the task is disposed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b50fb4f commit 2fe58bd

1 file changed

Lines changed: 60 additions & 20 deletions

File tree

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

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4339,23 +4339,26 @@ describe("Cline", () => {
43394339

43404340
describe("partial message update throttling", () => {
43414341
function createStreamingTask() {
4342+
// Task mutates one ClineMessage across deltas and finalization, so snapshot the
4343+
// fields of each messageUpdated post when it happens instead of keeping the reference.
4344+
const messageUpdates: Array<Pick<ClineMessage, "ts" | "text" | "partial">> = []
4345+
vi.mocked(mockProvider.postMessageToWebview).mockImplementation(async (message) => {
4346+
if (message.type === "messageUpdated" && message.clineMessage) {
4347+
const { ts, text, partial } = message.clineMessage
4348+
messageUpdates.push({ ts, text, partial })
4349+
}
4350+
})
4351+
43424352
const task = new Task({
43434353
provider: mockProvider,
43444354
apiConfiguration: mockApiConfig,
43454355
task: "test task",
43464356
startTask: false,
43474357
})
43484358
vi.spyOn(getTaskTestAccess(task), "saveClineMessages").mockResolvedValue(true)
4349-
return task
4359+
return { task, messageUpdates }
43504360
}
43514361

4352-
const messageUpdates = () =>
4353-
vi
4354-
.mocked(mockProvider.postMessageToWebview)
4355-
.mock.calls.flatMap(([message]) =>
4356-
message.type === "messageUpdated" && message.clineMessage ? [message.clineMessage] : [],
4357-
)
4358-
43594362
beforeEach(() => {
43604363
vi.useFakeTimers()
43614364
})
@@ -4365,7 +4368,7 @@ describe("Cline", () => {
43654368
})
43664369

43674370
it("coalesces a burst of partial updates and delivers the latest text last", async () => {
4368-
const task = createStreamingTask()
4371+
const { task, messageUpdates } = createStreamingTask()
43694372

43704373
await task.say("text", "chunk 0", undefined, true)
43714374
for (let i = 1; i <= 50; i++) {
@@ -4374,15 +4377,14 @@ describe("Cline", () => {
43744377
}
43754378
await vi.advanceTimersByTimeAsync(500)
43764379

4377-
const updates = messageUpdates()
43784380
// 50 deltas over ~250ms: far fewer than one post per delta.
4379-
expect(updates.length).toBeGreaterThan(0)
4380-
expect(updates.length).toBeLessThan(10)
4381-
expect(updates.at(-1)?.text).toBe("chunk 50")
4381+
expect(messageUpdates.length).toBeGreaterThan(0)
4382+
expect(messageUpdates.length).toBeLessThan(10)
4383+
expect(messageUpdates.at(-1)).toMatchObject({ text: "chunk 50", partial: true })
43824384
})
43834385

43844386
it("lets the finalized message through and drops the queued partial post", async () => {
4385-
const task = createStreamingTask()
4387+
const { task, messageUpdates } = createStreamingTask()
43864388

43874389
await task.say("text", "first", undefined, true)
43884390
await task.say("text", "second", undefined, true)
@@ -4393,21 +4395,59 @@ describe("Cline", () => {
43934395
expect(vi.getTimerCount()).toBe(0)
43944396
await vi.advanceTimersByTimeAsync(500)
43954397

4396-
expect(messageUpdates().at(-1)).toMatchObject({ text: "final text", partial: false })
4398+
expect(messageUpdates.map(({ text, partial }) => ({ text, partial }))).toEqual([
4399+
{ text: "second", partial: true },
4400+
{ text: "final text", partial: false },
4401+
])
4402+
})
4403+
4404+
it("drops a queued partial ask update when the ask is finalized", async () => {
4405+
const { task, messageUpdates } = createStreamingTask()
4406+
4407+
// Partial asks always reject with AskIgnoredError.
4408+
await task.ask("tool", "first", true).catch(() => {})
4409+
await task.ask("tool", "second", true).catch(() => {})
4410+
await task.ask("tool", "third", true).catch(() => {})
4411+
expect(task["pendingPartialMessageTimer"]).toBeDefined()
4412+
4413+
// A complete ask resolves only after a response, so leave it pending. Step 1ms per check
4414+
// so the final post lands well before the queued timer would fire on its own.
4415+
void task.ask("tool", "final", false).catch(() => {})
4416+
await vi.waitFor(() => expect(messageUpdates.at(-1)?.partial).toBe(false), { interval: 1 })
4417+
expect(task["pendingPartialMessageTimer"]).toBeUndefined()
4418+
await vi.advanceTimersByTimeAsync(500)
4419+
4420+
expect(messageUpdates.map(({ text, partial }) => ({ text, partial }))).toEqual([
4421+
{ text: "second", partial: true },
4422+
{ text: "final", partial: false },
4423+
])
4424+
})
4425+
4426+
it("drops a queued partial update when the task is disposed", async () => {
4427+
const { task, messageUpdates } = createStreamingTask()
4428+
4429+
await task.say("text", "first", undefined, true)
4430+
await task.say("text", "second", undefined, true)
4431+
await task.say("text", "third", undefined, true)
4432+
expect(task["pendingPartialMessageTimer"]).toBeDefined()
4433+
4434+
await task.dispose()
4435+
expect(task["pendingPartialMessageTimer"]).toBeUndefined()
4436+
await vi.advanceTimersByTimeAsync(500)
4437+
4438+
expect(messageUpdates.map(({ text }) => text)).toEqual(["second"])
43974439
})
43984440

43994441
it("posts updates that are far enough apart immediately", async () => {
4400-
const task = createStreamingTask()
4442+
const { task, messageUpdates } = createStreamingTask()
44014443

44024444
await task.say("text", "one", undefined, true)
44034445
await task.say("text", "two", undefined, true)
44044446
await vi.advanceTimersByTimeAsync(1000)
44054447
await task.say("text", "three", undefined, true)
44064448

4407-
// "two" is delivered by the trailing post; "three" goes out at once on the leading edge.
4408-
// (Posted messages are the same mutated object, so count posts rather than compare texts.)
4409-
expect(messageUpdates()).toHaveLength(2)
4410-
expect(messageUpdates().at(-1)?.text).toBe("three")
4449+
// "one" creates the message; "two" and "three" are each the first update in their window.
4450+
expect(messageUpdates.map(({ text }) => text)).toEqual(["two", "three"])
44114451
})
44124452
})
44134453

0 commit comments

Comments
 (0)