|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + buildThreadTimeline, |
| 4 | + deriveThreadAgentStatus, |
| 5 | + shouldSuspendThreadSession, |
| 6 | +} from "./threadTimeline"; |
| 7 | + |
| 8 | +describe("buildThreadTimeline", () => { |
| 9 | + it("interleaves prompts, human replies, and agent turns chronologically", () => { |
| 10 | + const timeline = buildThreadTimeline({ |
| 11 | + prompts: [{ id: "prompt", text: "Start", timestamp: 100 }], |
| 12 | + humanMessages: [ |
| 13 | + { |
| 14 | + id: "human", |
| 15 | + content: "Reply", |
| 16 | + createdAt: "1970-01-01T00:00:00.150Z", |
| 17 | + }, |
| 18 | + ], |
| 19 | + agentMessages: [{ id: "agent", text: "Done", timestamp: 200 }], |
| 20 | + }); |
| 21 | + |
| 22 | + expect(timeline.map((row) => row.kind)).toEqual([ |
| 23 | + "prompt", |
| 24 | + "human", |
| 25 | + "agent", |
| 26 | + ]); |
| 27 | + }); |
| 28 | + |
| 29 | + it("keeps malformed timestamps at the end", () => { |
| 30 | + const timeline = buildThreadTimeline({ |
| 31 | + prompts: [{ id: "prompt", text: "Start", timestamp: 100 }], |
| 32 | + humanMessages: [{ id: "human", content: "Reply", createdAt: "invalid" }], |
| 33 | + agentMessages: [{ id: "agent", text: "Done", timestamp: 200 }], |
| 34 | + }); |
| 35 | + |
| 36 | + expect(timeline.map((row) => row.kind)).toEqual([ |
| 37 | + "prompt", |
| 38 | + "agent", |
| 39 | + "human", |
| 40 | + ]); |
| 41 | + }); |
| 42 | +}); |
| 43 | + |
| 44 | +describe("deriveThreadAgentStatus", () => { |
| 45 | + it.each([ |
| 46 | + { |
| 47 | + name: "returns no status before activity", |
| 48 | + input: {}, |
| 49 | + expected: null, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "prioritizes failures", |
| 53 | + input: { hasActivity: true, hasError: true, errorTitle: "Run failed" }, |
| 54 | + expected: { phase: "error", label: "Run failed" }, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "prioritizes pending permissions over active work", |
| 58 | + input: { |
| 59 | + hasActivity: true, |
| 60 | + pendingPermissionCount: 1, |
| 61 | + isPromptPending: true, |
| 62 | + }, |
| 63 | + expected: { phase: "needs_input", label: "Needs input" }, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "reports active work", |
| 67 | + input: { hasActivity: true, isPromptPending: true }, |
| 68 | + expected: { phase: "active", label: "Working…" }, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "reports shipped work", |
| 72 | + input: { hasActivity: true, hasPullRequest: true }, |
| 73 | + expected: { phase: "complete", label: "Shipped" }, |
| 74 | + }, |
| 75 | + ])("$name", ({ input, expected }) => { |
| 76 | + expect(deriveThreadAgentStatus(input)).toEqual(expected); |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | +describe("shouldSuspendThreadSession", () => { |
| 81 | + it("suspends a local runless task so reading cannot start work", () => { |
| 82 | + expect( |
| 83 | + shouldSuspendThreadSession({ |
| 84 | + isCloud: false, |
| 85 | + hasRun: false, |
| 86 | + hasSession: false, |
| 87 | + }), |
| 88 | + ).toBe(true); |
| 89 | + }); |
| 90 | + |
| 91 | + it.each([ |
| 92 | + { isCloud: true, hasRun: false, hasSession: false }, |
| 93 | + { isCloud: false, hasRun: true, hasSession: false }, |
| 94 | + { isCloud: false, hasRun: false, hasSession: true }, |
| 95 | + ])("keeps an existing or cloud session attached", (input) => { |
| 96 | + expect(shouldSuspendThreadSession(input)).toBe(false); |
| 97 | + }); |
| 98 | +}); |
0 commit comments