Skip to content

Commit 8156406

Browse files
test(task): guard immediate clean state on start
Verify stale webview messages are cleared and posted through the immediate state path before the first task message. This keeps task startup outside the throttled update path. Signed-off-by: JunyongParkDev <[email protected]>
1 parent 46eaa17 commit 8156406

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import type { ApiMessage } from "../../task-persistence"
2727

2828
type TaskTestAccess = {
2929
getSystemPrompt: () => Promise<string>
30+
getEnabledMcpToolsCount: () => Promise<{ enabledToolCount: number; enabledServerCount: number }>
31+
initiateTaskLoop: (userContent: Anthropic.Messages.ContentBlockParam[]) => Promise<void>
3032
startTask: (task?: string, images?: string[]) => Promise<void>
3133
resumeTaskFromHistory: () => Promise<void>
3234
presentAssistantMessageSafe: () => void
@@ -2615,6 +2617,50 @@ describe("Cline", () => {
26152617
})
26162618
})
26172619

2620+
describe("startTask", () => {
2621+
it("posts a clean state immediately before adding the first task message", async () => {
2622+
const task = new Task({
2623+
provider: mockProvider,
2624+
apiConfiguration: mockApiConfig,
2625+
task: "new task",
2626+
startTask: false,
2627+
})
2628+
const taskAccess = getTaskTestAccess(task)
2629+
2630+
task.clineMessages = [{ ts: 1, type: "say", say: "text", text: "stale message" }]
2631+
2632+
let resolvePostState: (() => void) | undefined
2633+
const pendingPostState = new Promise<void>((resolve) => {
2634+
resolvePostState = resolve
2635+
})
2636+
const postStateSpy = vi
2637+
.mocked(mockProvider.postStateToWebviewWithoutTaskHistory)
2638+
.mockImplementationOnce(async () => {
2639+
expect(task.clineMessages).toEqual([])
2640+
await pendingPostState
2641+
})
2642+
const saySpy = vi.spyOn(task, "say").mockResolvedValue(undefined)
2643+
vi.spyOn(taskAccess, "getEnabledMcpToolsCount").mockResolvedValue({
2644+
enabledToolCount: 0,
2645+
enabledServerCount: 0,
2646+
})
2647+
const initiateTaskLoopSpy = vi.spyOn(taskAccess, "initiateTaskLoop").mockResolvedValue(undefined)
2648+
2649+
const startPromise = taskAccess.startTask("new task")
2650+
2651+
expect(postStateSpy).toHaveBeenCalledTimes(1)
2652+
expect(mockProvider.postStateToWebviewThrottled).not.toHaveBeenCalled()
2653+
expect(saySpy).not.toHaveBeenCalled()
2654+
2655+
resolvePostState?.()
2656+
await startPromise
2657+
2658+
expect(saySpy).toHaveBeenCalledOnce()
2659+
expect(saySpy).toHaveBeenCalledWith("text", "new task", undefined)
2660+
expect(initiateTaskLoopSpy).toHaveBeenCalledOnce()
2661+
})
2662+
})
2663+
26182664
describe("start()", () => {
26192665
it("should be a no-op if the task was already started in the constructor", () => {
26202666
const task = new Task({

0 commit comments

Comments
 (0)