Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { mergeClaudeWorkflowProgress } from "./claudeWorkflowProgress.ts";
4
5const snapshotFrame = (
6 entries: ReadonlyArray<Record<string, unknown>>,
7 usage?: Record<string, number>,
8) => ({
9 type: "system",
10 subtype: "task_progress",
11 task_id: "w1",
12 description: "Alpha: alpha:one",
13 workflow_progress: entries,
14 ...(usage === undefined ? {} : { usage }),
15});
16
17const phase = (index: number, title: string) => ({ type: "workflow_phase", index, title });
18
19const agent = (overrides: Record<string, unknown>) => ({
20 type: "workflow_agent",
21 index: 1,
22 label: "alpha:one",
23 state: "start",
24 phaseIndex: 1,
25 phaseTitle: "Alpha",
26 ...overrides,
27});
28
29describe("mergeClaudeWorkflowProgress", () => {
30 it("maps provider agent states and per-member metrics", () => {
31 const workflow = mergeClaudeWorkflowProgress({
32 previous: undefined,
33 message: snapshotFrame([
34 phase(1, "Alpha"),
35 agent({
36 state: "done",
37 agentId: "a1",
38 model: "claude-opus-5[1m]",
39 attempt: 2,
40 tokens: 71_141,
41 toolCalls: 3,
42 durationMs: 1277,
43 startedAt: 1_700_000_000_000,
44 lastProgressAt: 1_700_000_004_432,
45 queuedAt: 1_699_999_999_000,
46 promptPreview: "Reply with exactly: A1",
47 resultPreview: "A1",
48 }),
49 agent({ index: 2, label: "alpha:two", state: "queued", lastProgressAt: 1000 }),
50 agent({ index: 3, state: "progress", lastProgressAt: 2000 }),
51 ]),
52 });
53 expect(workflow?.phases).toEqual([{ index: 1, title: "Alpha" }]);
54 expect(workflow?.agents[0]).toEqual({
55 index: 1,
56 label: "alpha:one",
57 agentId: "a1",
58 state: "completed",
59 phaseIndex: 1,
60 phaseTitle: "Alpha",
61 model: "claude-opus-5[1m]",
62 attempt: 2,
63 totalTokens: 71_141,
64 toolCalls: 3,
65 durationMs: 1277,
66 queuedAt: 1_699_999_999_000,
67 startedAt: 1_700_000_000_000,
68 completedAt: 1_700_000_004_432,
69 prompt: "Reply with exactly: A1",
70 result: "A1",
71 });
72 expect(workflow?.agents[1]?.state).toBe("queued");
73 expect(workflow?.agents[1]).not.toHaveProperty("completedAt");
74 expect(workflow?.agents[2]).not.toHaveProperty("completedAt");
75 });
76
77 it.each(["something-new", "toString", "constructor", "__proto__"])(
78 "treats unrecognised state %s as still running rather than settled",
79 (state) => {
80 const workflow = mergeClaudeWorkflowProgress({
81 previous: undefined,
82 message: snapshotFrame([agent({ state })]),
83 });
84 expect(workflow?.agents[0]?.state).toBe("running");
85 },
86 );
87
88 it("keeps the roster through a usage-only frame", () => {
89 const first = mergeClaudeWorkflowProgress({
90 previous: undefined,
91 name: "probe-wf",
92 message: snapshotFrame([phase(1, "Alpha"), agent({})], {
93 total_tokens: 100,
94 tool_uses: 1,
95 duration_ms: 40,
96 }),
97 });
98 const second = mergeClaudeWorkflowProgress({
99 previous: first,
100 message: snapshotFrame([], { total_tokens: 250, tool_uses: 2, duration_ms: 900 }),
101 });
102 expect(second?.agents).toHaveLength(1);
103 expect(second?.phases).toHaveLength(1);
104 expect(second?.name).toBe("probe-wf");
105 expect(second?.totalTokens).toBe(250);
106 });
107
108 it("keeps other members and phase links when a sparse frame starts a new attempt", () => {
109 const first = mergeClaudeWorkflowProgress({
110 previous: undefined,
111 message: snapshotFrame([
112 phase(1, "Alpha"),
113 agent({
114 state: "error",
115 attempt: 1,
116 promptPreview: "First prompt",
117 resultPreview: "First result",
118 tokens: 100,
119 toolCalls: 2,
120 durationMs: 50,
121 queuedAt: 1000,
122 startedAt: 1010,
123 lastProgressAt: 2010,
124 }),
125 agent({ index: 2, state: "done" }),
126 ]),
127 });
128 expect(first?.agents[0]?.completedAt).toBe(2010);
129 expect(first?.agents[1]).not.toHaveProperty("completedAt");
130 const second = mergeClaudeWorkflowProgress({
131 previous: first,
132 message: snapshotFrame([
133 { type: "workflow_agent", index: 1, label: "alpha:one", state: "start", attempt: 2 },
134 ]),
135 });
136 expect(second?.agents).toEqual([
137 {
138 index: 1,
139 label: "alpha:one",
140 state: "running",
141 attempt: 2,
142 phaseIndex: 1,
143 phaseTitle: "Alpha",
144 },
145 first?.agents[1],
146 ]);
147 expect(second?.phases).toEqual(first?.phases);
148 });
149
150 it("ignores an older attempt after a retry completes", () => {
151 const first = mergeClaudeWorkflowProgress({
152 previous: undefined,
153 message: snapshotFrame([agent({ state: "done", attempt: 2, resultPreview: "New result" })]),
154 });
155 const second = mergeClaudeWorkflowProgress({
156 previous: first,
157 message: snapshotFrame([agent({ state: "error", attempt: 1, resultPreview: "Old result" })]),
158 });
159 expect(second?.agents).toEqual(first?.agents);
160 });
161
162 it.each([
163 ["done", "start"],
164 ["done", "queued"],
165 ["error", "start"],
166 ["error", "queued"],
167 ["start", "queued"],
168 ])("keeps member state when the same attempt regresses from %s to %s", (state, staleState) => {
169 const first = mergeClaudeWorkflowProgress({
170 previous: undefined,
171 message: snapshotFrame([agent({ state, attempt: 2, resultPreview: "Current result" })]),
172 });
173 const second = mergeClaudeWorkflowProgress({
174 previous: first,
175 message: snapshotFrame([
176 agent({ state: staleState, attempt: 2, resultPreview: "Old result" }),
177 ]),
178 });
179 expect(second?.agents).toEqual(first?.agents);
180 });
181
182 it("drops malformed entries instead of failing the frame", () => {
183 const workflow = mergeClaudeWorkflowProgress({
184 previous: undefined,
185 message: snapshotFrame([
186 { type: "workflow_phase", title: "No index" },
187 { type: "workflow_agent", index: 4 },
188 { type: "something_else", index: 1, title: "Ignored" },
189 phase(1, "Alpha"),
190 ]),
191 });
192 expect(workflow).toEqual({ phases: [{ index: 1, title: "Alpha" }], agents: [] });
193 });
194});