|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import type { |
| 3 | + ClaudeCliGateway, |
| 4 | + ClaudeCliRunCallbacks, |
| 5 | + ClaudeCliRunHandle, |
| 6 | +} from "../adapters/claude-cli"; |
| 7 | +import { ClaudeConversationStreamService } from "../services/claude-conversation-stream-service"; |
| 8 | +import { ProjectRuntime } from "../services/project-runtime"; |
| 9 | + |
| 10 | +class FakeClaudeCliGateway implements Pick<ClaudeCliGateway, "sendMessage"> { |
| 11 | + callbacks: ClaudeCliRunCallbacks | null = null; |
| 12 | + |
| 13 | + sendMessage( |
| 14 | + params: Parameters<ClaudeCliGateway["sendMessage"]>[0], |
| 15 | + callbacks: ClaudeCliRunCallbacks, |
| 16 | + ): ClaudeCliRunHandle { |
| 17 | + this.callbacks = callbacks; |
| 18 | + return { |
| 19 | + completion: Promise.resolve(), |
| 20 | + interrupt: () => {}, |
| 21 | + sessionId: Promise.resolve(params.sessionId ?? params.resumeSessionId ?? "session-1"), |
| 22 | + }; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// Mirrors the server's busy gate (isBusyAgentStatus): a worktree is "busy" for |
| 27 | +// web chat only while its agent lifecycle is starting/running. |
| 28 | +const isBusy = (lifecycle: string): boolean => lifecycle === "starting" || lifecycle === "running"; |
| 29 | + |
| 30 | +describe("Claude web-chat streaming lifecycle", () => { |
| 31 | + it("marks the worktree finished when a streamed claude -p turn ends so the next web message is allowed", () => { |
| 32 | + const runtime = new ProjectRuntime(); |
| 33 | + runtime.upsertWorktree({ worktreeId: "wt-1", branch: "feat", path: "/tmp/wt" }); |
| 34 | + const claude = new FakeClaudeCliGateway(); |
| 35 | + const stream = new ClaudeConversationStreamService({ claude }); |
| 36 | + |
| 37 | + // The same wiring the server uses in sendClaudeStreamingMessage: drive the |
| 38 | + // worktree lifecycle from the owned claude -p run instead of the lossy hook. |
| 39 | + const setLifecycle = (lifecycle: "running" | "stopped"): void => { |
| 40 | + runtime.applyEvent({ type: "agent_status_changed", worktreeId: "wt-1", branch: "feat", lifecycle }); |
| 41 | + }; |
| 42 | + const status = (): string => runtime.getWorktree("wt-1")!.agent.lifecycle; |
| 43 | + |
| 44 | + // Web message #1: server starts the owned claude -p run and marks it running. |
| 45 | + expect(stream.startRun({ |
| 46 | + conversationId: "session-1", |
| 47 | + turnId: "claude-turn:turn-1", |
| 48 | + cwd: "/tmp/wt", |
| 49 | + prompt: "first", |
| 50 | + sessionId: "session-1", |
| 51 | + onRunSettled: () => setLifecycle("stopped"), |
| 52 | + })).toEqual({ ok: true }); |
| 53 | + setLifecycle("running"); |
| 54 | + |
| 55 | + // While streaming, the worktree reads as busy — a 2nd web message is gated. |
| 56 | + expect(isBusy(status())).toBe(true); |
| 57 | + |
| 58 | + // The claude -p turn ends (stream result line) — the owned end-turn signal. |
| 59 | + claude.callbacks?.onComplete?.("session-1"); |
| 60 | + |
| 61 | + // Lands on the finished state (maps to "done" in the UI, same as a terminal |
| 62 | + // turn finishing) so the next web message passes the busy gate. |
| 63 | + expect(status()).toBe("stopped"); |
| 64 | + expect(isBusy(status())).toBe(false); |
| 65 | + }); |
| 66 | +}); |
0 commit comments