Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 EventId,
5 MessageId,
6 ProjectId,
7 ProviderInstanceId,
8 ThreadId,
9 TurnId,
10 type OrchestrationThread,
11 type OrchestrationThreadActivity,
12} from "@t3tools/contracts";
13
14import {
15 buildThreadFeed,
16 deriveThreadFeedPresentation,
17 type ThreadFeedActivity,
18 type ThreadFeedEntry,
19} from "./threadActivity";
20
21function makeActivity(
22 input: Partial<OrchestrationThreadActivity> &
23 Pick<OrchestrationThreadActivity, "id" | "kind" | "summary" | "createdAt">,
24): OrchestrationThreadActivity {
25 return {
26 tone: "info",
27 payload: {},
28 turnId: null,
29 ...input,
30 };
31}
32
33function makeThread(
34 input: Partial<OrchestrationThread> & Pick<OrchestrationThread, "id" | "projectId" | "title">,
35): OrchestrationThread {
36 return {
37 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
38 runtimeMode: "full-access",
39 interactionMode: "default",
40 branch: null,
41 worktreePath: null,
42 latestTurn: null,
43 createdAt: "2026-04-01T00:00:00.000Z",
44 updatedAt: "2026-04-01T00:00:00.000Z",
45 archivedAt: null,
46 deletedAt: null,
47 messages: [],
48 proposedPlans: [],
49 activities: [],
50 checkpoints: [],
51 session: null,
52 ...input,
53 settledOverride: input.settledOverride ?? null,
54 settledAt: input.settledAt ?? null,
55 };
56}
57
58describe("buildThreadFeed", () => {
59 it("keeps historic work entries attributed to their turns", () => {
60 const thread = makeThread({
61 id: ThreadId.make("thread-1"),
62 projectId: ProjectId.make("project-1"),
63 title: "Runtime warning thread",
64 latestTurn: {
65 turnId: TurnId.make("turn-latest"),
66 state: "running",
67 requestedAt: "2026-04-01T00:00:00.000Z",
68 startedAt: "2026-04-01T00:00:01.000Z",
69 completedAt: null,
70 assistantMessageId: null,
71 },
72 activities: [
73 makeActivity({
74 id: EventId.make("activity-old"),
75 kind: "runtime.warning",
76 summary: "Runtime warning",
77 createdAt: "2026-04-01T00:00:02.000Z",
78 turnId: TurnId.make("turn-old"),
79 payload: {
80 message: "Old warning",
81 },
82 }),
83 makeActivity({
84 id: EventId.make("activity-latest"),
85 kind: "runtime.warning",
86 summary: "Runtime warning",
87 createdAt: "2026-04-01T00:00:03.000Z",
88 turnId: TurnId.make("turn-latest"),
89 payload: {
90 message: "Latest warning",
91 },
92 }),
93 ],
94 });
95
96 const feed = buildThreadFeed(thread);
97 expect(feed).toMatchObject([
98 {
99 type: "activity-group",
100 turnId: "turn-old",
101 activities: [{ id: "activity-old", turnId: "turn-old" }],
102 },
103 {
104 type: "activity-group",
105 turnId: "turn-latest",
106 activities: [{ id: "activity-latest", turnId: "turn-latest" }],
107 },
108 ]);
109 });
110
111 it("collapses matching tool lifecycle rows like desktop", () => {
112 const thread = makeThread({
113 id: ThreadId.make("thread-2"),
114 projectId: ProjectId.make("project-1"),
115 title: "Collapsed tools",
116 latestTurn: {
117 turnId: TurnId.make("turn-1"),
118 state: "completed",
119 requestedAt: "2026-04-01T00:00:00.000Z",
120 startedAt: "2026-04-01T00:00:01.000Z",
121 completedAt: "2026-04-01T00:00:03.000Z",
122 assistantMessageId: null,
123 },
124 activities: [
125 makeActivity({
126 id: EventId.make("tool-updated"),
127 kind: "tool.updated",
128 tone: "tool",
129 summary: "Run tests",
130 createdAt: "2026-04-01T00:00:01.000Z",
131 turnId: TurnId.make("turn-1"),
132 payload: {
133 title: "Run tests",
134 itemType: "command_execution",
135 detail: "/bin/zsh -lc 'bun run test'",
136 },
137 }),
138 makeActivity({
139 id: EventId.make("tool-completed"),
140 kind: "tool.completed",
141 tone: "tool",
142 summary: "Run tests completed",
143 createdAt: "2026-04-01T00:00:02.000Z",
144 turnId: TurnId.make("turn-1"),
145 payload: {
146 title: "Run tests",
147 itemType: "command_execution",
148 detail: "/bin/zsh -lc 'bun run test'",
149 },
150 }),
151 ],
152 });
153
154 const feed = buildThreadFeed(thread);
155 const group = feed[0];
156
157 expect(group).toMatchObject({
158 type: "activity-group",
159 });
160 if (!group || group.type !== "activity-group") {
161 return;
162 }
163
164 expect(group.activities).toHaveLength(1);
165 expect(group.activities[0]).toMatchObject({
166 id: "tool-completed",
167 createdAt: "2026-04-01T00:00:02.000Z",
168 turnId: "turn-1",
169 summary: "Run tests",
170 detail: "bun run test",
171 canExpand: true,
172 icon: "command",
173 toolLike: true,
174 status: "success",
175 });
176 expect(group.activities[0]?.getFullDetail()).toBe("/bin/zsh -lc 'bun run test'");
177 expect(group.activities[0]?.getCopyText()).toBe(
178 "Run tests\nbun run test\n/bin/zsh -lc 'bun run test'",
179 );
180 });
181
182 it("keeps MCP inputs available to expanded mobile work rows", () => {
183 const turnId = TurnId.make("turn-mcp");
184 const thread = makeThread({
185 id: ThreadId.make("thread-mcp"),
186 projectId: ProjectId.make("project-1"),
187 title: "Expandable MCP call",
188 latestTurn: {
189 turnId,
190 state: "completed",
191 requestedAt: "2026-04-01T00:00:00.000Z",
192 startedAt: "2026-04-01T00:00:01.000Z",
193 completedAt: "2026-04-01T00:00:03.000Z",
194 assistantMessageId: null,
195 },
196 activities: [
197 makeActivity({
198 id: EventId.make("mcp-completed"),
199 kind: "tool.completed",
200 tone: "tool",
201 summary: "Call repository tool",
202 createdAt: "2026-04-01T00:00:02.000Z",
203 turnId,
204 payload: {
205 title: "Call repository tool",
206 itemType: "mcp_tool_call",
207 detail: "repository.search",
208 status: "completed",
209 data: {
210 item: {
211 server: "repository",
212 tool: "search",
213 arguments: { query: "work log" },
214 },
215 },
216 },
217 }),
218 ],
219 });
220
221 const group = buildThreadFeed(thread)[0];
222 expect(group).toMatchObject({ type: "activity-group" });
223 if (!group || group.type !== "activity-group") {
224 return;
225 }
226
227 expect(group.activities[0]?.icon).toBe("wrench");
228 expect(group.activities[0]?.getFullDetail()).toContain('"query": "work log"');
229 expect(group.activities[0]?.getFullDetail()).toContain("repository.search");
230 });
231
232 it("defers large tool output expansion until a work row is opened or copied", () => {
233 let serializedToolOutputs = 0;
234 const activities = Array.from({ length: 5_000 }, (_, index) =>
235 makeActivity({
236 id: EventId.make(`large-tool-${index}`),
237 kind: "tool.completed",
238 tone: "tool",
239 summary: `Tool ${index}`,
240 createdAt: new Date(Date.UTC(2026, 3, 1, 0, 0, index)).toISOString(),
241 payload: {
242 title: `Tool ${index}`,
243 itemType: "mcp_tool_call",
244 status: "completed",
245 data: {
246 item: {
247 toJSON: () => {
248 serializedToolOutputs += 1;
249 return { output: "x".repeat(32_768) };
250 },
251 },
252 },
253 },
254 }),
255 );
256 const thread = makeThread({
257 id: ThreadId.make("thread-large-tools"),
258 projectId: ProjectId.make("project-1"),
259 title: "Large tools",
260 activities,
261 });
262
263 const feed = buildThreadFeed(thread);
264 expect(serializedToolOutputs).toBe(0);
265
266 const group = feed[0];
267 expect(group).toMatchObject({ type: "activity-group" });
268 if (!group || group.type !== "activity-group") {
269 return;
270 }
271
272 expect(group.activities).toHaveLength(5_000);
273 expect(group.activities[0]?.getFullDetail()).toContain('"output"');
274 expect(serializedToolOutputs).toBe(1);
275 expect(group.activities[0]?.getCopyText()).toContain('"output"');
276 expect(serializedToolOutputs).toBe(1);
277 });
278
279 it("folds settled turn work while leaving the terminal answer visible", () => {
280 const turnId = TurnId.make("turn-1");
281 const thread = makeThread({
282 id: ThreadId.make("thread-3"),
283 projectId: ProjectId.make("project-1"),
284 title: "Folded work",
285 latestTurn: {
286 turnId,
287 state: "completed",
288 requestedAt: "2026-04-01T00:00:00.000Z",
289 startedAt: "2026-04-01T00:00:01.000Z",
290 completedAt: "2026-04-01T00:00:18.000Z",
291 assistantMessageId: MessageId.make("assistant-final"),
292 },
293 messages: [
294 {
295 id: MessageId.make("assistant-commentary"),
296 role: "assistant",
297 text: "I am checking.",
298 turnId,
299 streaming: false,
300 createdAt: "2026-04-01T00:00:02.000Z",
301 updatedAt: "2026-04-01T00:00:03.000Z",
302 },
303 {
304 id: MessageId.make("assistant-final"),
305 role: "assistant",
306 text: "Done.",
307 turnId,
308 streaming: false,
309 createdAt: "2026-04-01T00:00:17.000Z",
310 updatedAt: "2026-04-01T00:00:18.000Z",
311 },
312 ],
313 activities: [
314 makeActivity({
315 id: EventId.make("tool-completed"),
316 kind: "tool.completed",
317 tone: "tool",
318 summary: "Read files",
319 createdAt: "2026-04-01T00:00:05.000Z",
320 turnId,
321 payload: {
322 title: "Read files",
323 itemType: "file_read",
324 status: "completed",
325 },
326 }),
327 ],
328 });
329
330 const feed = buildThreadFeed(thread);
331 const collapsed = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set());
332 expect(collapsed.map((entry) => entry.id)).toEqual(["turn-fold:turn-1", "assistant-final"]);
333 expect(collapsed[0]).toMatchObject({
334 type: "turn-fold",
335 label: "Worked for 17s",
336 expanded: false,
337 });
338
339 const expanded = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set([turnId]));
340 expect(expanded.map((entry) => entry.id)).toEqual([
341 "turn-fold:turn-1",
342 "assistant-commentary",
343 "tool-completed",
344 "assistant-final",
345 ]);
346 });
347
348 it("measures a steer-superseded turn from its user boundary through trailing work", () => {
349 const firstTurnId = TurnId.make("turn-1");
350 const secondTurnId = TurnId.make("turn-2");
351 const thread = makeThread({
352 id: ThreadId.make("thread-steered"),
353 projectId: ProjectId.make("project-1"),
354 title: "Steered work",
355 latestTurn: {
356 turnId: secondTurnId,
357 state: "running",
358 requestedAt: "2026-04-01T00:00:14.000Z",
359 startedAt: "2026-04-01T00:00:14.000Z",
360 completedAt: null,
361 assistantMessageId: MessageId.make("assistant-next"),
362 },
363 messages: [
364 {
365 id: MessageId.make("user-1"),
366 role: "user",
367 text: "Do it once more.",
368 turnId: null,
369 streaming: false,
370 createdAt: "2026-04-01T00:00:00.000Z",
371 updatedAt: "2026-04-01T00:00:00.000Z",
372 },
373 {
374 id: MessageId.make("assistant-commentary"),
375 role: "assistant",
376 text: "Kicking off call 1.",
377 turnId: firstTurnId,
378 streaming: false,
379 createdAt: "2026-04-01T00:00:09.000Z",
380 updatedAt: "2026-04-01T00:00:09.000Z",
381 },
382 {
383 id: MessageId.make("user-2"),
384 role: "user",
385 text: "Actually do 15.",
386 turnId: null,
387 streaming: false,
388 createdAt: "2026-04-01T00:00:14.000Z",
389 updatedAt: "2026-04-01T00:00:14.000Z",
390 },
391 {
392 id: MessageId.make("assistant-next"),
393 role: "assistant",
394 text: "One down - adjusting.",
395 turnId: secondTurnId,
396 streaming: true,
397 createdAt: "2026-04-01T00:00:17.000Z",
398 updatedAt: "2026-04-01T00:00:17.000Z",
399 },
400 ],
401 activities: [
402 makeActivity({
403 id: EventId.make("work-1"),
404 kind: "tool.completed",
405 tone: "tool",
406 summary: "Ran command",
407 createdAt: "2026-04-01T00:00:12.000Z",
408 turnId: firstTurnId,
409 payload: {
410 title: "Ran command",
411 itemType: "command_execution",
412 status: "completed",
413 },
414 }),
415 ],
416 });
417
418 const feed = buildThreadFeed(thread);
419 const collapsed = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set());
420 expect(collapsed.find((entry) => entry.type === "turn-fold")).toMatchObject({
421 turnId: firstTurnId,
422 label: "Worked for 12s",
423 });
424 });
425
426 it("keeps an active turn expanded and classifies error-shaped tool output", () => {
427 const turnId = TurnId.make("turn-running");
428 const thread = makeThread({
429 id: ThreadId.make("thread-4"),
430 projectId: ProjectId.make("project-1"),
431 title: "Running work",
432 latestTurn: {
433 turnId,
434 state: "running",
435 requestedAt: "2026-04-01T00:00:00.000Z",
436 startedAt: "2026-04-01T00:00:01.000Z",
437 completedAt: null,
438 assistantMessageId: null,
439 },
440 activities: [
441 makeActivity({
442 id: EventId.make("tool-failed"),
443 kind: "tool.completed",
444 tone: "tool",
445 summary: "Run command",
446 createdAt: "2026-04-01T00:00:05.000Z",
447 turnId,
448 payload: {
449 title: "Run command",
450 itemType: "command_execution",
451 detail: "zsh: command not found: nope",
452 status: "completed",
453 },
454 }),
455 ],
456 });
457
458 const feed = buildThreadFeed(thread);
459 expect(deriveThreadFeedPresentation(feed, thread.latestTurn, new Set())).toEqual(feed);
460 expect(feed[0]).toMatchObject({
461 type: "activity-group",
462 activities: [{ status: "failure" }],
463 });
464 });
465
466 it("appends active work as a normal timeline row", () => {
467 const startedAt = "2026-04-01T00:00:01.000Z";
468 const presented = deriveThreadFeedPresentation([], null, new Set(), new Set(), startedAt);
469
470 expect(presented).toEqual([
471 {
472 type: "working",
473 id: "working-indicator-row",
474 createdAt: startedAt,
475 },
476 ]);
477 expect(deriveThreadFeedPresentation(presented, null, new Set())).toEqual([]);
478 });
479
480 it("models work-log overflow as list rows", () => {
481 const activity = (
482 id: string,
483 createdAt: string,
484 status: ThreadFeedActivity["status"] = "success",
485 ): ThreadFeedActivity => ({
486 id,
487 createdAt,
488 turnId: null,
489 summary: `Tool ${id}`,
490 detail: null,
491 canExpand: false,
492 getFullDetail: () => null,
493 getCopyText: () => id,
494 icon: "command",
495 toolLike: true,
496 status,
497 });
498 const feed: ThreadFeedEntry[] = [
499 {
500 type: "activity-group",
501 id: "work-group-1",
502 createdAt: "2026-04-01T00:00:01.000Z",
503 turnId: null,
504 activities: [
505 activity("activity-1", "2026-04-01T00:00:01.000Z"),
506 activity("activity-neutral", "2026-04-01T00:00:02.000Z", "neutral"),
507 activity("activity-2", "2026-04-01T00:00:03.000Z"),
508 activity("activity-3", "2026-04-01T00:00:04.000Z"),
509 ],
510 },
511 ];
512
513 const collapsed = deriveThreadFeedPresentation(feed, null, new Set());
514 expect(collapsed.map((entry) => entry.id)).toEqual(["activity-3", "work-toggle:work-group-1"]);
515 expect(collapsed[1]).toMatchObject({
516 type: "work-toggle",
517 groupId: "work-group-1",
518 hiddenCount: 2,
519 expanded: false,
520 });
521
522 const expanded = deriveThreadFeedPresentation(feed, null, new Set(), new Set(["work-group-1"]));
523 expect(expanded.map((entry) => entry.id)).toEqual([
524 "activity-1",
525 "activity-2",
526 "activity-3",
527 "work-toggle:work-group-1",
528 ]);
529 expect(expanded.at(-1)).toMatchObject({
530 type: "work-toggle",
531 expanded: true,
532 });
533 });
534});