Skip to content

Commit 2835d7e

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 <[email protected]>
1 parent a2fcb85 commit 2835d7e

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
@@ -4368,23 +4368,26 @@ describe("Cline", () => {
43684368

43694369
describe("partial message update throttling", () => {
43704370
function createStreamingTask() {
4371+
// Task mutates one ClineMessage across deltas and finalization, so snapshot the
4372+
// fields of each messageUpdated post when it happens instead of keeping the reference.
4373+
const messageUpdates: Array<Pick<ClineMessage, "ts" | "text" | "partial">> = []
4374+
vi.mocked(mockProvider.postMessageToWebview).mockImplementation(async (message) => {
4375+
if (message.type === "messageUpdated" && message.clineMessage) {
4376+
const { ts, text, partial } = message.clineMessage
4377+
messageUpdates.push({ ts, text, partial })
4378+
}
4379+
})
4380+
43714381
const task = new Task({
43724382
provider: mockProvider,
43734383
apiConfiguration: mockApiConfig,
43744384
task: "test task",
43754385
startTask: false,
43764386
})
43774387
vi.spyOn(getTaskTestAccess(task), "saveClineMessages").mockResolvedValue(true)
4378-
return task
4388+
return { task, messageUpdates }
43794389
}
43804390

4381-
const messageUpdates = () =>
4382-
vi
4383-
.mocked(mockProvider.postMessageToWebview)
4384-
.mock.calls.flatMap(([message]) =>
4385-
message.type === "messageUpdated" && message.clineMessage ? [message.clineMessage] : [],
4386-
)
4387-
43884391
beforeEach(() => {
43894392
vi.useFakeTimers()
43904393
})
@@ -4394,7 +4397,7 @@ describe("Cline", () => {
43944397
})
43954398

43964399
it("coalesces a burst of partial updates and delivers the latest text last", async () => {
4397-
const task = createStreamingTask()
4400+
const { task, messageUpdates } = createStreamingTask()
43984401

43994402
await task.say("text", "chunk 0", undefined, true)
44004403
for (let i = 1; i <= 50; i++) {
@@ -4403,15 +4406,14 @@ describe("Cline", () => {
44034406
}
44044407
await vi.advanceTimersByTimeAsync(500)
44054408

4406-
const updates = messageUpdates()
44074409
// 50 deltas over ~250ms: far fewer than one post per delta.
4408-
expect(updates.length).toBeGreaterThan(0)
4409-
expect(updates.length).toBeLessThan(10)
4410-
expect(updates.at(-1)?.text).toBe("chunk 50")
4410+
expect(messageUpdates.length).toBeGreaterThan(0)
4411+
expect(messageUpdates.length).toBeLessThan(10)
4412+
expect(messageUpdates.at(-1)).toMatchObject({ text: "chunk 50", partial: true })
44114413
})
44124414

44134415
it("lets the finalized message through and drops the queued partial post", async () => {
4414-
const task = createStreamingTask()
4416+
const { task, messageUpdates } = createStreamingTask()
44154417

44164418
await task.say("text", "first", undefined, true)
44174419
await task.say("text", "second", undefined, true)
@@ -4422,21 +4424,59 @@ describe("Cline", () => {
44224424
expect(vi.getTimerCount()).toBe(0)
44234425
await vi.advanceTimersByTimeAsync(500)
44244426

4425-
expect(messageUpdates().at(-1)).toMatchObject({ text: "final text", partial: false })
4427+
expect(messageUpdates.map(({ text, partial }) => ({ text, partial }))).toEqual([
4428+
{ text: "second", partial: true },
4429+
{ text: "final text", partial: false },
4430+
])
4431+
})
4432+
4433+
it("drops a queued partial ask update when the ask is finalized", async () => {
4434+
const { task, messageUpdates } = createStreamingTask()
4435+
4436+
// Partial asks always reject with AskIgnoredError.
4437+
await task.ask("tool", "first", true).catch(() => {})
4438+
await task.ask("tool", "second", true).catch(() => {})
4439+
await task.ask("tool", "third", true).catch(() => {})
4440+
expect(task["pendingPartialMessageTimer"]).toBeDefined()
4441+
4442+
// A complete ask resolves only after a response, so leave it pending. Step 1ms per check
4443+
// so the final post lands well before the queued timer would fire on its own.
4444+
void task.ask("tool", "final", false).catch(() => {})
4445+
await vi.waitFor(() => expect(messageUpdates.at(-1)?.partial).toBe(false), { interval: 1 })
4446+
expect(task["pendingPartialMessageTimer"]).toBeUndefined()
4447+
await vi.advanceTimersByTimeAsync(500)
4448+
4449+
expect(messageUpdates.map(({ text, partial }) => ({ text, partial }))).toEqual([
4450+
{ text: "second", partial: true },
4451+
{ text: "final", partial: false },
4452+
])
4453+
})
4454+
4455+
it("drops a queued partial update when the task is disposed", async () => {
4456+
const { task, messageUpdates } = createStreamingTask()
4457+
4458+
await task.say("text", "first", undefined, true)
4459+
await task.say("text", "second", undefined, true)
4460+
await task.say("text", "third", undefined, true)
4461+
expect(task["pendingPartialMessageTimer"]).toBeDefined()
4462+
4463+
await task.dispose()
4464+
expect(task["pendingPartialMessageTimer"]).toBeUndefined()
4465+
await vi.advanceTimersByTimeAsync(500)
4466+
4467+
expect(messageUpdates.map(({ text }) => text)).toEqual(["second"])
44264468
})
44274469

44284470
it("posts updates that are far enough apart immediately", async () => {
4429-
const task = createStreamingTask()
4471+
const { task, messageUpdates } = createStreamingTask()
44304472

44314473
await task.say("text", "one", undefined, true)
44324474
await task.say("text", "two", undefined, true)
44334475
await vi.advanceTimersByTimeAsync(1000)
44344476
await task.say("text", "three", undefined, true)
44354477

4436-
// "two" is delivered by the trailing post; "three" goes out at once on the leading edge.
4437-
// (Posted messages are the same mutated object, so count posts rather than compare texts.)
4438-
expect(messageUpdates()).toHaveLength(2)
4439-
expect(messageUpdates().at(-1)?.text).toBe("three")
4478+
// "one" creates the message; "two" and "three" are each the first update in their window.
4479+
expect(messageUpdates.map(({ text }) => text)).toEqual(["two", "three"])
44404480
})
44414481
})
44424482

0 commit comments

Comments
 (0)