Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadActivity.test.ts

665 lines20.0 KB

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 buildPendingUserInputAnswers,
16 buildThreadFeed,
17 deriveThreadFeedPresentation,
18 isPendingUserInputOptionSelected,
19 setPendingUserInputCustomAnswer,
20 togglePendingUserInputOptionSelection,
21 type ThreadFeedActivity,
22 type ThreadFeedEntry,
23} from "./threadActivity";
24​
25const singleSelectQuestion = {
26 id: "runtime",
27 header: "Runtime",
28 question: "Which runtime should be used?",
29 options: [
30 { label: "Go", description: "One binary" },
31 { label: "Node.js", description: "Reuse TypeScript" },
32 ],
33 multiSelect: false,
34} as const;
35​
36const multiSelectQuestion = {
37 id: "scope",
38 header: "Scope",
39 question: "Which data should be collected?",
40 options: [
41 { label: "Orders", description: "Receipts" },
42 { label: "Listings", description: "Inventory" },
43 ],
44 multiSelect: true,
45} as const;
46​
47describe("pending user input answers", () => {
48 it("replaces single-select options and toggles multi-select options", () => {
49 expect(
50 togglePendingUserInputOptionSelection(
51 singleSelectQuestion,
52 { selectedOptionLabels: ["Go"] },
53 "Node.js",
54 ),
55 ).toEqual({ customAnswer: "", selectedOptionLabels: ["Node.js"] });
56​
57 const orders = togglePendingUserInputOptionSelection(multiSelectQuestion, undefined, "Orders");
58 const ordersAndListings = togglePendingUserInputOptionSelection(
59 multiSelectQuestion,
60 orders,
61 "Listings",
62 );
63 expect(ordersAndListings).toEqual({
64 customAnswer: "",
65 selectedOptionLabels: ["Orders", "Listings"],
66 });
67 expect(
68 togglePendingUserInputOptionSelection(multiSelectQuestion, ordersAndListings, "Orders"),
69 ).toEqual({ customAnswer: "", selectedOptionLabels: ["Listings"] });
70​
71 const paddedOrders = togglePendingUserInputOptionSelection(
72 multiSelectQuestion,
73 undefined,
74 " Orders ",
75 );
76 expect(paddedOrders).toEqual({ customAnswer: "", selectedOptionLabels: ["Orders"] });
77 expect(
78 togglePendingUserInputOptionSelection(multiSelectQuestion, paddedOrders, " Orders "),
79 ).toEqual({ customAnswer: "" });
80 });
81​
82 it("builds array answers for multi-select questions", () => {
83 expect(
84 buildPendingUserInputAnswers([singleSelectQuestion, multiSelectQuestion], {
85 runtime: { selectedOptionLabels: ["Go"] },
86 scope: { selectedOptionLabels: ["Orders", "Listings"] },
87 }),
88 ).toEqual({
89 runtime: "Go",
90 scope: ["Orders", "Listings"],
91 });
92 });
93​
94 it("clears selected options while a custom answer is active", () => {
95 expect(
96 setPendingUserInputCustomAnswer(
97 { selectedOptionLabels: ["Orders", "Listings"] },
98 "Orders first",
99 ),
100 ).toEqual({ customAnswer: "Orders first" });
101 });
102​
103 it("matches selected chips against normalized option labels", () => {
104 expect(
105 isPendingUserInputOptionSelected({ selectedOptionLabels: ["Orders"] }, " Orders "),
106 ).toBe(true);
107 expect(
108 isPendingUserInputOptionSelected(
109 { selectedOptionLabels: ["Orders"], customAnswer: "Orders first" },
110 " Orders ",
111 ),
112 ).toBe(false);
113 });
114});
115​
116function makeActivity(
117 input: Partial<OrchestrationThreadActivity> &
118 Pick<OrchestrationThreadActivity, "id" | "kind" | "summary" | "createdAt">,
119): OrchestrationThreadActivity {
120 return {
121 tone: "info",
122 payload: {},
123 turnId: null,
124 ...input,
125 };
127​
128function makeThread(
129 input: Partial<OrchestrationThread> & Pick<OrchestrationThread, "id" | "projectId" | "title">,
130): OrchestrationThread {
131 return {
132 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
133 runtimeMode: "full-access",
134 interactionMode: "default",
135 branch: null,
136 worktreePath: null,
137 latestTurn: null,
138 createdAt: "2026-04-01T00:00:00.000Z",
139 updatedAt: "2026-04-01T00:00:00.000Z",
140 archivedAt: null,
141 deletedAt: null,
142 messages: [],
143 proposedPlans: [],
144 activities: [],
145 checkpoints: [],
146 session: null,
147 ...input,
148 settledOverride: input.settledOverride ?? null,
149 settledAt: input.settledAt ?? null,
150 };
152​
153describe("buildThreadFeed", () => {
154 it("keeps historic work entries attributed to their turns", () => {
155 const thread = makeThread({
156 id: ThreadId.make("thread-1"),
157 projectId: ProjectId.make("project-1"),
158 title: "Runtime warning thread",
159 latestTurn: {
160 turnId: TurnId.make("turn-latest"),
161 state: "running",
162 requestedAt: "2026-04-01T00:00:00.000Z",
163 startedAt: "2026-04-01T00:00:01.000Z",
164 completedAt: null,
165 assistantMessageId: null,
166 },
167 activities: [
168 makeActivity({
169 id: EventId.make("activity-old"),
170 kind: "runtime.warning",
171 summary: "Runtime warning",
172 createdAt: "2026-04-01T00:00:02.000Z",
173 turnId: TurnId.make("turn-old"),
174 payload: {
175 message: "Old warning",
176 },
177 }),
178 makeActivity({
179 id: EventId.make("activity-latest"),
180 kind: "runtime.warning",
181 summary: "Runtime warning",
182 createdAt: "2026-04-01T00:00:03.000Z",
183 turnId: TurnId.make("turn-latest"),
184 payload: {
185 message: "Latest warning",
186 },
187 }),
188 ],
189 });
190​
191 const feed = buildThreadFeed(thread);
192 expect(feed).toMatchObject([
193 {
194 type: "activity-group",
195 turnId: "turn-old",
196 activities: [{ id: "activity-old", turnId: "turn-old" }],
197 },
198 {
199 type: "activity-group",
200 turnId: "turn-latest",
201 activities: [{ id: "activity-latest", turnId: "turn-latest" }],
202 },
203 ]);
204 });
205​
206 it("collapses matching tool lifecycle rows like desktop", () => {
207 const thread = makeThread({
208 id: ThreadId.make("thread-2"),
209 projectId: ProjectId.make("project-1"),
210 title: "Collapsed tools",
211 latestTurn: {
212 turnId: TurnId.make("turn-1"),
213 state: "completed",
214 requestedAt: "2026-04-01T00:00:00.000Z",
215 startedAt: "2026-04-01T00:00:01.000Z",
216 completedAt: "2026-04-01T00:00:03.000Z",
217 assistantMessageId: null,
218 },
219 activities: [
220 makeActivity({
221 id: EventId.make("tool-updated"),
222 kind: "tool.updated",
223 tone: "tool",
224 summary: "Run tests",
225 createdAt: "2026-04-01T00:00:01.000Z",
226 turnId: TurnId.make("turn-1"),
227 payload: {
228 title: "Run tests",
229 itemType: "command_execution",
230 detail: "/bin/zsh -lc 'bun run test'",
231 },
232 }),
233 makeActivity({
234 id: EventId.make("tool-completed"),
235 kind: "tool.completed",
236 tone: "tool",
237 summary: "Run tests completed",
238 createdAt: "2026-04-01T00:00:02.000Z",
239 turnId: TurnId.make("turn-1"),
240 payload: {
241 title: "Run tests",
242 itemType: "command_execution",
243 detail: "/bin/zsh -lc 'bun run test'",
244 },
245 }),
246 ],
247 });
248​
249 const feed = buildThreadFeed(thread);
250 const group = feed[0];
251​
252 expect(group).toMatchObject({
253 type: "activity-group",
254 });
255 if (!group || group.type !== "activity-group") {
256 return;
257 }
258​
259 expect(group.activities).toHaveLength(1);
260 expect(group.activities[0]).toMatchObject({
261 id: "tool-completed",
262 createdAt: "2026-04-01T00:00:02.000Z",
263 turnId: "turn-1",
264 summary: "Run tests",
265 detail: "bun run test",
266 canExpand: true,
267 icon: "command",
268 toolLike: true,
269 status: "success",
270 });
271 expect(group.activities[0]?.getFullDetail()).toBe("/bin/zsh -lc 'bun run test'");
272 expect(group.activities[0]?.getCopyText()).toBe(
273 "Run tests\nbun run test\n/bin/zsh -lc 'bun run test'",
274 );
275 });
276​
277 it("keeps MCP inputs available to expanded mobile work rows", () => {
278 const turnId = TurnId.make("turn-mcp");
279 const thread = makeThread({
280 id: ThreadId.make("thread-mcp"),
281 projectId: ProjectId.make("project-1"),
282 title: "Expandable MCP call",
283 latestTurn: {
284 turnId,
285 state: "completed",
286 requestedAt: "2026-04-01T00:00:00.000Z",
287 startedAt: "2026-04-01T00:00:01.000Z",
288 completedAt: "2026-04-01T00:00:03.000Z",
289 assistantMessageId: null,
290 },
291 activities: [
292 makeActivity({
293 id: EventId.make("mcp-completed"),
294 kind: "tool.completed",
295 tone: "tool",
296 summary: "Call repository tool",
297 createdAt: "2026-04-01T00:00:02.000Z",
298 turnId,
299 payload: {
300 title: "Call repository tool",
301 itemType: "mcp_tool_call",
302 detail: "repository.search",
303 status: "completed",
304 data: {
305 item: {
306 server: "repository",
307 tool: "search",
308 arguments: { query: "work log" },
309 },
310 },
311 },
312 }),
313 ],
314 });
315​
316 const group = buildThreadFeed(thread)[0];
317 expect(group).toMatchObject({ type: "activity-group" });
318 if (!group || group.type !== "activity-group") {
319 return;
320 }
321​
322 expect(group.activities[0]?.icon).toBe("wrench");
323 expect(group.activities[0]?.getFullDetail()).toContain('"query": "work log"');
324 expect(group.activities[0]?.getFullDetail()).toContain("repository.search");
325 });
326​
327 it("defers large tool output expansion until a work row is opened or copied", () => {
328 let serializedToolOutputs = 0;
329 const activities = Array.from({ length: 5_000 }, (_, index) =>
330 makeActivity({
331 id: EventId.make(`large-tool-${index}`),
332 kind: "tool.completed",
333 tone: "tool",
334 summary: `Tool ${index}`,
335 createdAt: new Date(Date.UTC(2026, 3, 1, 0, 0, index)).toISOString(),
336 payload: {
337 title: `Tool ${index}`,
338 itemType: "mcp_tool_call",
339 status: "completed",
340 data: {
341 item: {
342 toJSON: () => {
343 serializedToolOutputs += 1;
344 return { output: "x".repeat(32_768) };
345 },
346 },
347 },
348 },
349 }),
350 );
351 const thread = makeThread({
352 id: ThreadId.make("thread-large-tools"),
353 projectId: ProjectId.make("project-1"),
354 title: "Large tools",
355 activities,
356 });
357​
358 const feed = buildThreadFeed(thread);
359 expect(serializedToolOutputs).toBe(0);
360​
361 const group = feed[0];
362 expect(group).toMatchObject({ type: "activity-group" });
363 if (!group || group.type !== "activity-group") {
364 return;
365 }
366​
367 expect(group.activities).toHaveLength(5_000);
368 expect(group.activities[0]?.getFullDetail()).toContain('"output"');
369 expect(serializedToolOutputs).toBe(1);
370 expect(group.activities[0]?.getCopyText()).toContain('"output"');
371 expect(serializedToolOutputs).toBe(1);
372 });
373​
374 it("folds settled turn work while leaving the terminal answer visible", () => {
375 const turnId = TurnId.make("turn-1");
376 const thread = makeThread({
377 id: ThreadId.make("thread-3"),
378 projectId: ProjectId.make("project-1"),
379 title: "Folded work",
380 latestTurn: {
381 turnId,
382 state: "completed",
383 requestedAt: "2026-04-01T00:00:00.000Z",
384 startedAt: "2026-04-01T00:00:01.000Z",
385 completedAt: "2026-04-01T00:00:18.000Z",
386 assistantMessageId: MessageId.make("assistant-final"),
387 },
388 messages: [
389 {
390 id: MessageId.make("assistant-commentary"),
391 role: "assistant",
392 text: "I am checking.",
393 turnId,
394 streaming: false,
395 createdAt: "2026-04-01T00:00:02.000Z",
396 updatedAt: "2026-04-01T00:00:03.000Z",
397 },
398 {
399 id: MessageId.make("assistant-final"),
400 role: "assistant",
401 text: "Done.",
402 turnId,
403 streaming: false,
404 createdAt: "2026-04-01T00:00:17.000Z",
405 updatedAt: "2026-04-01T00:00:18.000Z",
406 },
407 ],
408 activities: [
409 makeActivity({
410 id: EventId.make("tool-completed"),
411 kind: "tool.completed",
412 tone: "tool",
413 summary: "Read files",
414 createdAt: "2026-04-01T00:00:05.000Z",
415 turnId,
416 payload: {
417 title: "Read files",
418 itemType: "file_read",
419 status: "completed",
420 },
421 }),
422 ],
423 });
424​
425 const feed = buildThreadFeed(thread);
426 const collapsed = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set());
427 expect(collapsed.map((entry) => entry.id)).toEqual(["turn-fold:turn-1", "assistant-final"]);
428 expect(collapsed[0]).toMatchObject({
429 type: "turn-fold",
430 label: "Worked for 17s",
431 expanded: false,
432 });
433​
434 const expanded = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set([turnId]));
435 expect(expanded.map((entry) => entry.id)).toEqual([
436 "turn-fold:turn-1",
437 "assistant-commentary",
438 "tool-completed",
439 "assistant-final",
440 ]);
441 });
442​
443 it("measures a steer-superseded turn from its user boundary through trailing work", () => {
444 const firstTurnId = TurnId.make("turn-1");
445 const secondTurnId = TurnId.make("turn-2");
446 const thread = makeThread({
447 id: ThreadId.make("thread-steered"),
448 projectId: ProjectId.make("project-1"),
449 title: "Steered work",
450 latestTurn: {
451 turnId: secondTurnId,
452 state: "running",
453 requestedAt: "2026-04-01T00:00:14.000Z",
454 startedAt: "2026-04-01T00:00:14.000Z",
455 completedAt: null,
456 assistantMessageId: MessageId.make("assistant-next"),
457 },
458 messages: [
459 {
460 id: MessageId.make("user-1"),
461 role: "user",
462 text: "Do it once more.",
463 turnId: null,
464 streaming: false,
465 createdAt: "2026-04-01T00:00:00.000Z",
466 updatedAt: "2026-04-01T00:00:00.000Z",
467 },
468 {
469 id: MessageId.make("assistant-commentary"),
470 role: "assistant",
471 text: "Kicking off call 1.",
472 turnId: firstTurnId,
473 streaming: false,
474 createdAt: "2026-04-01T00:00:09.000Z",
475 updatedAt: "2026-04-01T00:00:09.000Z",
476 },
477 {
478 id: MessageId.make("user-2"),
479 role: "user",
480 text: "Actually do 15.",
481 turnId: null,
482 streaming: false,
483 createdAt: "2026-04-01T00:00:14.000Z",
484 updatedAt: "2026-04-01T00:00:14.000Z",
485 },
486 {
487 id: MessageId.make("assistant-next"),
488 role: "assistant",
489 text: "One down - adjusting.",
490 turnId: secondTurnId,
491 streaming: true,
492 createdAt: "2026-04-01T00:00:17.000Z",
493 updatedAt: "2026-04-01T00:00:17.000Z",
494 },
495 ],
496 activities: [
497 makeActivity({
498 id: EventId.make("work-1"),
499 kind: "tool.completed",
500 tone: "tool",
501 summary: "Ran command",
502 createdAt: "2026-04-01T00:00:12.000Z",
503 turnId: firstTurnId,
504 payload: {
505 title: "Ran command",
506 itemType: "command_execution",
507 status: "completed",
508 },
509 }),
510 ],
511 });
512​
513 const feed = buildThreadFeed(thread);
514 const collapsed = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set());
515 expect(collapsed.find((entry) => entry.type === "turn-fold")).toMatchObject({
516 turnId: firstTurnId,
517 label: "Worked for 12s",
518 });
519 });
520​
521 it("keeps an active turn expanded and classifies error-shaped tool output", () => {
522 const turnId = TurnId.make("turn-running");
523 const thread = makeThread({
524 id: ThreadId.make("thread-4"),
525 projectId: ProjectId.make("project-1"),
526 title: "Running work",
527 latestTurn: {
528 turnId,
529 state: "running",
530 requestedAt: "2026-04-01T00:00:00.000Z",
531 startedAt: "2026-04-01T00:00:01.000Z",
532 completedAt: null,
533 assistantMessageId: null,
534 },
535 activities: [
536 makeActivity({
537 id: EventId.make("tool-failed"),
538 kind: "tool.completed",
539 tone: "tool",
540 summary: "Run command",
541 createdAt: "2026-04-01T00:00:05.000Z",
542 turnId,
543 payload: {
544 title: "Run command",
545 itemType: "command_execution",
546 detail: "zsh: command not found: nope",
547 status: "completed",
548 },
549 }),
550 ],
551 });
552​
553 const feed = buildThreadFeed(thread);
554 expect(deriveThreadFeedPresentation(feed, thread.latestTurn, new Set())).toEqual(feed);
555 expect(feed[0]).toMatchObject({
556 type: "activity-group",
557 activities: [{ status: "failure" }],
558 });
559 });
560​
561 it("appends active work as a normal timeline row", () => {
562 const startedAt = "2026-04-01T00:00:01.000Z";
563 const presented = deriveThreadFeedPresentation([], null, new Set(), new Set(), startedAt);
564​
565 expect(presented).toEqual([
566 {
567 type: "working",
568 id: "working-indicator-row",
569 createdAt: startedAt,
570 },
571 ]);
572 expect(deriveThreadFeedPresentation(presented, null, new Set())).toEqual([]);
573 });
574​
575 it("models work-log overflow as list rows", () => {
576 const activity = (
577 id: string,
578 createdAt: string,
579 status: ThreadFeedActivity["status"] = "success",
580 ): ThreadFeedActivity => ({
581 id,
582 createdAt,
583 turnId: null,
584 summary: `Tool ${id}`,
585 detail: null,
586 canExpand: false,
587 getFullDetail: () => null,
588 getCopyText: () => id,
589 icon: "command",
590 toolLike: true,
591 status,
592 });
593 const feed: ThreadFeedEntry[] = [
594 {
595 type: "activity-group",
596 id: "work-group-1",
597 createdAt: "2026-04-01T00:00:01.000Z",
598 turnId: null,
599 activities: [
600 activity("activity-1", "2026-04-01T00:00:01.000Z"),
601 activity("activity-neutral", "2026-04-01T00:00:02.000Z", "neutral"),
602 activity("activity-2", "2026-04-01T00:00:03.000Z"),
603 activity("activity-3", "2026-04-01T00:00:04.000Z"),
604 ],
605 },
606 ];
607​
608 const collapsed = deriveThreadFeedPresentation(feed, null, new Set());
609 expect(collapsed.map((entry) => entry.id)).toEqual(["activity-3", "work-toggle:work-group-1"]);
610 expect(collapsed[1]).toMatchObject({
611 type: "work-toggle",
612 groupId: "work-group-1",
613 hiddenCount: 2,
614 expanded: false,
615 });
616​
617 const expanded = deriveThreadFeedPresentation(feed, null, new Set(), new Set(["work-group-1"]));
618 expect(expanded.map((entry) => entry.id)).toEqual([
619 "activity-1",
620 "activity-2",
621 "activity-3",
622 "work-toggle:work-group-1",
623 ]);
624 expect(expanded.at(-1)).toMatchObject({
625 type: "work-toggle",
626 expanded: true,
627 });
628 });
629});
630​
631describe("quiet timeline: nested agents", () => {
632 it("keeps a nested agent's terminal row but hides its background work", () => {
633 const thread = makeThread({
634 id: ThreadId.make("thread-nested"),
635 projectId: ProjectId.make("project-1"),
636 title: "Nested agents",
637 activities: [
638 // A subagent's own shell: internal, covered by the owner's liveness.
639 makeActivity({
640 id: EventId.make("shell-done"),
641 kind: "task.completed",
642 summary: "Task completed",
643 createdAt: "2026-04-01T00:00:02.000Z",
644 payload: { taskId: "sh-1", agentId: "owner", agentKind: "background" },
645 }),
646 // A nested AGENT's completion: mobile has no Agents sheet, so this
647 // terminal row is the only signal it ever finished.
648 makeActivity({
649 id: EventId.make("nested-done"),
650 kind: "task.completed",
651 summary: "Task completed",
652 createdAt: "2026-04-01T00:00:03.000Z",
653 payload: { taskId: "n-1", agentId: "owner", agentKind: "agent" },
654 }),
655 ],
656 });
657​
658 const feed = buildThreadFeed(thread);
659 const ids = feed.flatMap((entry) =>
660 entry.type === "activity-group" ? entry.activities.map((row) => row.id) : [],
661 );
662 expect(ids).toContain("nested-done");
663 expect(ids).not.toContain("shell-done");
664 });
665});