Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { codexFeedbackMessage } from "@t3tools/client-runtime/state/threads";
3
4import {
5 EventId,
6 MessageId,
7 ProjectId,
8 ProviderInstanceId,
9 ThreadId,
10 TurnId,
11 type OrchestrationThread,
12 type OrchestrationThreadActivity,
13} from "@t3tools/contracts";
14
15import {
16 buildPendingUserInputAnswers,
17 buildThreadFeed,
18 derivePendingApprovals,
19 deriveThreadFeedPresentation,
20 isPendingUserInputOptionSelected,
21 setPendingUserInputCustomAnswer,
22 togglePendingUserInputOptionSelection,
23 type ThreadFeedActivity,
24 type ThreadFeedEntry,
25} from "./threadActivity";
26
27describe("Codex feedback pseudo-messages", () => {
28 it("keeps pending and completed feedback messages in the mobile thread body", () => {
29 const pending = {
30 id: MessageId.make("feedback-command"),
31 command: "/feedback The agent stopped early.",
32 createdAt: "2026-08-23T00:00:00.000Z",
33 status: "uploading" as const,
34 };
35 const entries = [codexFeedbackMessage(pending), codexFeedbackMessage(pending, "assistant")].map(
36 (message) => ({
37 type: "message" as const,
38 id: message.id,
39 createdAt: message.createdAt,
40 message,
41 }),
42 );
43
44 expect(deriveThreadFeedPresentation(entries, null, new Set())).toEqual(entries);
45 expect(entries[1]?.message.text).toBe("Sending feedback to OpenAI...");
46
47 const completed = codexFeedbackMessage(
48 { ...pending, status: "sent", feedbackId: "codex-thread-1" },
49 "assistant",
50 );
51 expect(completed.text).toContain("codex-thread-1");
52 });
53});
54
55const singleSelectQuestion = {
56 id: "runtime",
57 header: "Runtime",
58 question: "Which runtime should be used?",
59 options: [
60 { label: "Go", description: "One binary" },
61 { label: "Node.js", description: "Reuse TypeScript" },
62 ],
63 multiSelect: false,
64} as const;
65
66const multiSelectQuestion = {
67 id: "scope",
68 header: "Scope",
69 question: "Which data should be collected?",
70 options: [
71 { label: "Orders", description: "Receipts" },
72 { label: "Listings", description: "Inventory" },
73 ],
74 multiSelect: true,
75} as const;
76
77describe("pending user input answers", () => {
78 it("replaces single-select options and toggles multi-select options", () => {
79 expect(
80 togglePendingUserInputOptionSelection(
81 singleSelectQuestion,
82 { selectedOptionLabels: ["Go"] },
83 "Node.js",
84 ),
85 ).toEqual({ customAnswer: "", selectedOptionLabels: ["Node.js"] });
86
87 const orders = togglePendingUserInputOptionSelection(multiSelectQuestion, undefined, "Orders");
88 const ordersAndListings = togglePendingUserInputOptionSelection(
89 multiSelectQuestion,
90 orders,
91 "Listings",
92 );
93 expect(ordersAndListings).toEqual({
94 customAnswer: "",
95 selectedOptionLabels: ["Orders", "Listings"],
96 });
97 expect(
98 togglePendingUserInputOptionSelection(multiSelectQuestion, ordersAndListings, "Orders"),
99 ).toEqual({ customAnswer: "", selectedOptionLabels: ["Listings"] });
100
101 const paddedOrders = togglePendingUserInputOptionSelection(
102 multiSelectQuestion,
103 undefined,
104 " Orders ",
105 );
106 expect(paddedOrders).toEqual({ customAnswer: "", selectedOptionLabels: ["Orders"] });
107 expect(
108 togglePendingUserInputOptionSelection(multiSelectQuestion, paddedOrders, " Orders "),
109 ).toEqual({ customAnswer: "" });
110 });
111
112 it("builds array answers for multi-select questions", () => {
113 expect(
114 buildPendingUserInputAnswers([singleSelectQuestion, multiSelectQuestion], {
115 runtime: { selectedOptionLabels: ["Go"] },
116 scope: { selectedOptionLabels: ["Orders", "Listings"] },
117 }),
118 ).toEqual({
119 runtime: "Go",
120 scope: ["Orders", "Listings"],
121 });
122 });
123
124 it("clears selected options while a custom answer is active", () => {
125 expect(
126 setPendingUserInputCustomAnswer(
127 { selectedOptionLabels: ["Orders", "Listings"] },
128 "Orders first",
129 ),
130 ).toEqual({ customAnswer: "Orders first" });
131 });
132
133 it("matches selected chips against normalized option labels", () => {
134 expect(
135 isPendingUserInputOptionSelected({ selectedOptionLabels: ["Orders"] }, " Orders "),
136 ).toBe(true);
137 expect(
138 isPendingUserInputOptionSelected(
139 { selectedOptionLabels: ["Orders"], customAnswer: "Orders first" },
140 " Orders ",
141 ),
142 ).toBe(false);
143 });
144});
145
146describe("pending approvals", () => {
147 it("keeps app access approvals and persistence choices from remote environments", () => {
148 const options = [
149 { decision: "decline", label: "Decline" },
150 { decision: "acceptAlways", label: "Always allow Safari" },
151 { decision: "accept", label: "Approve" },
152 ];
153 const activity = makeActivity({
154 id: EventId.make("approval-safari"),
155 kind: "approval.requested",
156 summary: "App access approval requested",
157 createdAt: "2026-08-24T00:00:00.000Z",
158 payload: {
159 requestId: "req-safari",
160 requestType: "mcp_elicitation_approval",
161 detail: "Allow ChatGPT to use Safari?",
162 appName: "Safari",
163 options,
164 },
165 });
166
167 expect(derivePendingApprovals([activity])).toEqual([
168 {
169 requestId: "req-safari",
170 requestKind: "mcp-elicitation",
171 createdAt: "2026-08-24T00:00:00.000Z",
172 detail: "Allow ChatGPT to use Safari?",
173 appName: "Safari",
174 options,
175 },
176 ]);
177 });
178
179 it("removes an app access approval after a remote client rejects it", () => {
180 const requested = makeActivity({
181 id: EventId.make("approval-safari-open"),
182 kind: "approval.requested",
183 summary: "App access approval requested",
184 createdAt: "2026-08-24T00:00:00.000Z",
185 payload: { requestId: "req-safari", requestKind: "mcp-elicitation" },
186 });
187 const resolved = makeActivity({
188 id: EventId.make("approval-safari-resolved"),
189 kind: "approval.resolved",
190 summary: "Approval resolved",
191 createdAt: "2026-08-24T00:00:01.000Z",
192 payload: { requestId: "req-safari", decision: "decline" },
193 });
194
195 expect(derivePendingApprovals([requested, resolved])).toEqual([]);
196 });
197
198 it("derives unknown provider approvals as actionable generic approvals", () => {
199 const activity = makeActivity({
200 id: EventId.make("approval-unknown"),
201 kind: "approval.requested",
202 summary: "Approval requested",
203 createdAt: "2026-08-24T00:00:00.000Z",
204 payload: {
205 requestId: "req-unknown",
206 requestType: "unknown",
207 detail: "addCommentReaction|eyes|'confused'",
208 },
209 });
210
211 expect(derivePendingApprovals([activity])).toMatchObject([
212 {
213 requestId: "req-unknown",
214 requestKind: "command",
215 detail: "addCommentReaction|eyes|'confused'",
216 },
217 ]);
218 });
219});
220
221function makeActivity(
222 input: Partial<OrchestrationThreadActivity> &
223 Pick<OrchestrationThreadActivity, "id" | "kind" | "summary" | "createdAt">,
224): OrchestrationThreadActivity {
225 return {
226 tone: "info",
227 payload: {},
228 turnId: null,
229 ...input,
230 };
231}
232
233function makeThread(
234 input: Partial<OrchestrationThread> & Pick<OrchestrationThread, "id" | "projectId" | "title">,
235): OrchestrationThread {
236 return {
237 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
238 runtimeMode: "full-access",
239 interactionMode: "default",
240 branch: null,
241 worktreePath: null,
242 latestTurn: null,
243 createdAt: "2026-04-01T00:00:00.000Z",
244 updatedAt: "2026-04-01T00:00:00.000Z",
245 archivedAt: null,
246 deletedAt: null,
247 messages: [],
248 proposedPlans: [],
249 activities: [],
250 checkpoints: [],
251 session: null,
252 ...input,
253 settledOverride: input.settledOverride ?? null,
254 settledAt: input.settledAt ?? null,
255 };
256}
257
258describe("buildThreadFeed", () => {
259 it("keeps older local feedback before newer messages returned by the server", () => {
260 const submission = {
261 id: MessageId.make("feedback-command-ordering"),
262 command: "/feedback The agent stopped early.",
263 createdAt: "2026-08-23T00:00:01.000Z",
264 status: "sent" as const,
265 feedbackId: "codex-thread-1",
266 };
267 const laterMessage = {
268 id: MessageId.make("later-server-message"),
269 role: "assistant" as const,
270 text: "Newer server response",
271 turnId: null,
272 createdAt: "2026-08-23T00:00:02.000Z",
273 updatedAt: "2026-08-23T00:00:02.000Z",
274 streaming: false,
275 };
276 const thread = makeThread({
277 id: ThreadId.make("thread-feedback-ordering"),
278 projectId: ProjectId.make("project-1"),
279 title: "Feedback ordering",
280 messages: [laterMessage],
281 });
282
283 const feed = buildThreadFeed(thread, {
284 localMessages: [
285 codexFeedbackMessage(submission),
286 codexFeedbackMessage(submission, "assistant"),
287 ],
288 });
289
290 expect(feed.map((entry) => entry.id)).toEqual([
291 "feedback-command-ordering",
292 "feedback-command-ordering:feedback",
293 "later-server-message",
294 ]);
295 });
296
297 it("keeps historic work entries attributed to their turns", () => {
298 const thread = makeThread({
299 id: ThreadId.make("thread-1"),
300 projectId: ProjectId.make("project-1"),
301 title: "Runtime warning thread",
302 latestTurn: {
303 turnId: TurnId.make("turn-latest"),
304 state: "running",
305 requestedAt: "2026-04-01T00:00:00.000Z",
306 startedAt: "2026-04-01T00:00:01.000Z",
307 completedAt: null,
308 assistantMessageId: null,
309 },
310 activities: [
311 makeActivity({
312 id: EventId.make("activity-old"),
313 kind: "runtime.warning",
314 summary: "Runtime warning",
315 createdAt: "2026-04-01T00:00:02.000Z",
316 turnId: TurnId.make("turn-old"),
317 payload: {
318 message: "Old warning",
319 },
320 }),
321 makeActivity({
322 id: EventId.make("activity-latest"),
323 kind: "runtime.warning",
324 summary: "Runtime warning",
325 createdAt: "2026-04-01T00:00:03.000Z",
326 turnId: TurnId.make("turn-latest"),
327 payload: {
328 message: "Latest warning",
329 },
330 }),
331 ],
332 });
333
334 const feed = buildThreadFeed(thread);
335 expect(feed).toMatchObject([
336 {
337 type: "activity-group",
338 turnId: "turn-old",
339 activities: [{ id: "activity-old", turnId: "turn-old" }],
340 },
341 {
342 type: "activity-group",
343 turnId: "turn-latest",
344 activities: [{ id: "activity-latest", turnId: "turn-latest" }],
345 },
346 ]);
347 });
348
349 it("collapses matching tool lifecycle rows like desktop", () => {
350 const thread = makeThread({
351 id: ThreadId.make("thread-2"),
352 projectId: ProjectId.make("project-1"),
353 title: "Collapsed tools",
354 latestTurn: {
355 turnId: TurnId.make("turn-1"),
356 state: "completed",
357 requestedAt: "2026-04-01T00:00:00.000Z",
358 startedAt: "2026-04-01T00:00:01.000Z",
359 completedAt: "2026-04-01T00:00:03.000Z",
360 assistantMessageId: null,
361 },
362 activities: [
363 makeActivity({
364 id: EventId.make("tool-updated"),
365 kind: "tool.updated",
366 tone: "tool",
367 summary: "Run tests",
368 createdAt: "2026-04-01T00:00:01.000Z",
369 turnId: TurnId.make("turn-1"),
370 payload: {
371 title: "Run tests",
372 itemType: "command_execution",
373 detail: "/bin/zsh -lc 'bun run test'",
374 },
375 }),
376 makeActivity({
377 id: EventId.make("tool-completed"),
378 kind: "tool.completed",
379 tone: "tool",
380 summary: "Run tests completed",
381 createdAt: "2026-04-01T00:00:02.000Z",
382 turnId: TurnId.make("turn-1"),
383 payload: {
384 title: "Run tests",
385 itemType: "command_execution",
386 detail: "/bin/zsh -lc 'bun run test'",
387 },
388 }),
389 ],
390 });
391
392 const feed = buildThreadFeed(thread);
393 const group = feed[0];
394
395 expect(group).toMatchObject({
396 type: "activity-group",
397 });
398 if (!group || group.type !== "activity-group") {
399 return;
400 }
401
402 expect(group.activities).toHaveLength(1);
403 expect(group.activities[0]).toMatchObject({
404 id: "tool-completed",
405 createdAt: "2026-04-01T00:00:02.000Z",
406 turnId: "turn-1",
407 summary: "Run tests",
408 detail: "bun run test",
409 canExpand: true,
410 icon: "command",
411 toolLike: true,
412 status: "success",
413 });
414 expect(group.activities[0]?.getFullDetail()).toBe("/bin/zsh -lc 'bun run test'");
415 expect(group.activities[0]?.getCopyText()).toBe(
416 "Run tests\nbun run test\n/bin/zsh -lc 'bun run test'",
417 );
418 });
419
420 it("keeps MCP inputs available to expanded mobile work rows", () => {
421 const turnId = TurnId.make("turn-mcp");
422 const thread = makeThread({
423 id: ThreadId.make("thread-mcp"),
424 projectId: ProjectId.make("project-1"),
425 title: "Expandable MCP call",
426 latestTurn: {
427 turnId,
428 state: "completed",
429 requestedAt: "2026-04-01T00:00:00.000Z",
430 startedAt: "2026-04-01T00:00:01.000Z",
431 completedAt: "2026-04-01T00:00:03.000Z",
432 assistantMessageId: null,
433 },
434 activities: [
435 makeActivity({
436 id: EventId.make("mcp-completed"),
437 kind: "tool.completed",
438 tone: "tool",
439 summary: "Call repository tool",
440 createdAt: "2026-04-01T00:00:02.000Z",
441 turnId,
442 payload: {
443 title: "Call repository tool",
444 itemType: "mcp_tool_call",
445 detail: "repository.search",
446 status: "completed",
447 data: {
448 item: {
449 server: "repository",
450 tool: "search",
451 arguments: { query: "work log" },
452 },
453 },
454 },
455 }),
456 ],
457 });
458
459 const group = buildThreadFeed(thread)[0];
460 expect(group).toMatchObject({ type: "activity-group" });
461 if (!group || group.type !== "activity-group") {
462 return;
463 }
464
465 expect(group.activities[0]?.icon).toBe("wrench");
466 expect(group.activities[0]?.getFullDetail()).toContain('"query": "work log"');
467 expect(group.activities[0]?.getFullDetail()).toContain("repository.search");
468 });
469
470 it("defers large tool output expansion until a work row is opened or copied", () => {
471 let serializedToolOutputs = 0;
472 const activities = Array.from({ length: 5_000 }, (_, index) =>
473 makeActivity({
474 id: EventId.make(`large-tool-${index}`),
475 kind: "tool.completed",
476 tone: "tool",
477 summary: `Tool ${index}`,
478 createdAt: new Date(Date.UTC(2026, 3, 1, 0, 0, index)).toISOString(),
479 payload: {
480 title: `Tool ${index}`,
481 itemType: "mcp_tool_call",
482 status: "completed",
483 data: {
484 item: {
485 toJSON: () => {
486 serializedToolOutputs += 1;
487 return { output: "x".repeat(32_768) };
488 },
489 },
490 },
491 },
492 }),
493 );
494 const thread = makeThread({
495 id: ThreadId.make("thread-large-tools"),
496 projectId: ProjectId.make("project-1"),
497 title: "Large tools",
498 activities,
499 });
500
501 const feed = buildThreadFeed(thread);
502 expect(serializedToolOutputs).toBe(0);
503
504 const group = feed[0];
505 expect(group).toMatchObject({ type: "activity-group" });
506 if (!group || group.type !== "activity-group") {
507 return;
508 }
509
510 expect(group.activities).toHaveLength(5_000);
511 expect(group.activities[0]?.getFullDetail()).toContain('"output"');
512 expect(serializedToolOutputs).toBe(1);
513 expect(group.activities[0]?.getCopyText()).toContain('"output"');
514 expect(serializedToolOutputs).toBe(1);
515 });
516
517 it("keeps the first and terminal assistant messages visible around settled work", () => {
518 const turnId = TurnId.make("turn-1");
519 const thread = makeThread({
520 id: ThreadId.make("thread-3"),
521 projectId: ProjectId.make("project-1"),
522 title: "Folded work",
523 latestTurn: {
524 turnId,
525 state: "completed",
526 requestedAt: "2026-04-01T00:00:00.000Z",
527 startedAt: "2026-04-01T00:00:01.000Z",
528 completedAt: "2026-04-01T00:00:18.000Z",
529 assistantMessageId: MessageId.make("assistant-final"),
530 },
531 messages: [
532 {
533 id: MessageId.make("assistant-first"),
534 role: "assistant",
535 text: "Synthetic deployment checklist\n1. Confirm the deployment is ready.",
536 turnId,
537 streaming: false,
538 createdAt: "2026-04-01T00:00:02.000Z",
539 updatedAt: "2026-04-01T00:00:03.000Z",
540 },
541 {
542 id: MessageId.make("assistant-final"),
543 role: "assistant",
544 text: "Done.",
545 turnId,
546 streaming: false,
547 createdAt: "2026-04-01T00:00:17.000Z",
548 updatedAt: "2026-04-01T00:00:18.000Z",
549 },
550 ],
551 activities: [
552 makeActivity({
553 id: EventId.make("tool-completed"),
554 kind: "tool.completed",
555 tone: "tool",
556 summary: "Read files",
557 createdAt: "2026-04-01T00:00:05.000Z",
558 turnId,
559 payload: {
560 title: "Read files",
561 itemType: "file_read",
562 status: "completed",
563 },
564 }),
565 ],
566 });
567
568 const feed = buildThreadFeed(thread);
569 const collapsed = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set());
570 expect(collapsed.map((entry) => entry.id)).toEqual([
571 "assistant-first",
572 "turn-fold:turn-1",
573 "assistant-final",
574 ]);
575 expect(collapsed[1]).toMatchObject({
576 type: "turn-fold",
577 label: "Worked for 17s",
578 expanded: false,
579 });
580
581 const expanded = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set([turnId]));
582 expect(expanded.map((entry) => entry.id)).toEqual([
583 "assistant-first",
584 "turn-fold:turn-1",
585 "tool-completed",
586 "assistant-final",
587 ]);
588 });
589
590 it("folds assistant messages between the first and terminal messages", () => {
591 const turnId = TurnId.make("turn-1");
592 const thread = makeThread({
593 id: ThreadId.make("thread-middle-message"),
594 projectId: ProjectId.make("project-1"),
595 title: "Bounded narration",
596 latestTurn: {
597 turnId,
598 state: "completed",
599 requestedAt: "2026-04-01T00:00:00.000Z",
600 startedAt: "2026-04-01T00:00:01.000Z",
601 completedAt: "2026-04-01T00:00:06.000Z",
602 assistantMessageId: MessageId.make("assistant-final"),
603 },
604 messages: [
605 {
606 id: MessageId.make("assistant-first"),
607 role: "assistant",
608 text: "The main result is ready.",
609 turnId,
610 streaming: false,
611 createdAt: "2026-04-01T00:00:01.000Z",
612 updatedAt: "2026-04-01T00:00:02.000Z",
613 },
614 {
615 id: MessageId.make("assistant-middle"),
616 role: "assistant",
617 text: "I am checking one more detail.",
618 turnId,
619 streaming: false,
620 createdAt: "2026-04-01T00:00:03.000Z",
621 updatedAt: "2026-04-01T00:00:04.000Z",
622 },
623 {
624 id: MessageId.make("assistant-final"),
625 role: "assistant",
626 text: "Verification finished.",
627 turnId,
628 streaming: false,
629 createdAt: "2026-04-01T00:00:05.000Z",
630 updatedAt: "2026-04-01T00:00:06.000Z",
631 },
632 ],
633 });
634
635 const feed = buildThreadFeed(thread);
636 const rows = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set());
637
638 expect(rows.map((entry) => entry.id)).toEqual([
639 "assistant-first",
640 "turn-fold:turn-1",
641 "assistant-final",
642 ]);
643 });
644
645 it("measures a steer-superseded turn from its user boundary through trailing work", () => {
646 const firstTurnId = TurnId.make("turn-1");
647 const secondTurnId = TurnId.make("turn-2");
648 const thread = makeThread({
649 id: ThreadId.make("thread-steered"),
650 projectId: ProjectId.make("project-1"),
651 title: "Steered work",
652 latestTurn: {
653 turnId: secondTurnId,
654 state: "running",
655 requestedAt: "2026-04-01T00:00:14.000Z",
656 startedAt: "2026-04-01T00:00:14.000Z",
657 completedAt: null,
658 assistantMessageId: MessageId.make("assistant-next"),
659 },
660 messages: [
661 {
662 id: MessageId.make("user-1"),
663 role: "user",
664 text: "Do it once more.",
665 turnId: null,
666 streaming: false,
667 createdAt: "2026-04-01T00:00:00.000Z",
668 updatedAt: "2026-04-01T00:00:00.000Z",
669 },
670 {
671 id: MessageId.make("assistant-commentary"),
672 role: "assistant",
673 text: "Kicking off call 1.",
674 turnId: firstTurnId,
675 streaming: false,
676 createdAt: "2026-04-01T00:00:09.000Z",
677 updatedAt: "2026-04-01T00:00:09.000Z",
678 },
679 {
680 id: MessageId.make("user-2"),
681 role: "user",
682 text: "Actually do 15.",
683 turnId: null,
684 streaming: false,
685 createdAt: "2026-04-01T00:00:14.000Z",
686 updatedAt: "2026-04-01T00:00:14.000Z",
687 },
688 {
689 id: MessageId.make("assistant-next"),
690 role: "assistant",
691 text: "One down - adjusting.",
692 turnId: secondTurnId,
693 streaming: true,
694 createdAt: "2026-04-01T00:00:17.000Z",
695 updatedAt: "2026-04-01T00:00:17.000Z",
696 },
697 ],
698 activities: [
699 makeActivity({
700 id: EventId.make("work-1"),
701 kind: "tool.completed",
702 tone: "tool",
703 summary: "Ran command",
704 createdAt: "2026-04-01T00:00:12.000Z",
705 turnId: firstTurnId,
706 payload: {
707 title: "Ran command",
708 itemType: "command_execution",
709 status: "completed",
710 },
711 }),
712 ],
713 });
714
715 const feed = buildThreadFeed(thread);
716 const collapsed = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set());
717 expect(collapsed.find((entry) => entry.type === "turn-fold")).toMatchObject({
718 turnId: firstTurnId,
719 label: "Worked for 12s",
720 });
721 });
722
723 it("keeps an active turn expanded and classifies error-shaped tool output", () => {
724 const turnId = TurnId.make("turn-running");
725 const thread = makeThread({
726 id: ThreadId.make("thread-4"),
727 projectId: ProjectId.make("project-1"),
728 title: "Running work",
729 latestTurn: {
730 turnId,
731 state: "running",
732 requestedAt: "2026-04-01T00:00:00.000Z",
733 startedAt: "2026-04-01T00:00:01.000Z",
734 completedAt: null,
735 assistantMessageId: null,
736 },
737 activities: [
738 makeActivity({
739 id: EventId.make("tool-failed"),
740 kind: "tool.completed",
741 tone: "tool",
742 summary: "Run command",
743 createdAt: "2026-04-01T00:00:05.000Z",
744 turnId,
745 payload: {
746 title: "Run command",
747 itemType: "command_execution",
748 detail: "zsh: command not found: nope",
749 status: "completed",
750 },
751 }),
752 ],
753 });
754
755 const feed = buildThreadFeed(thread);
756 expect(deriveThreadFeedPresentation(feed, thread.latestTurn, new Set())).toEqual(feed);
757 expect(feed[0]).toMatchObject({
758 type: "activity-group",
759 activities: [{ status: "failure" }],
760 });
761 });
762
763 it("appends active work as a normal timeline row", () => {
764 const startedAt = "2026-04-01T00:00:01.000Z";
765 const presented = deriveThreadFeedPresentation([], null, new Set(), new Set(), startedAt);
766
767 expect(presented).toEqual([
768 {
769 type: "working",
770 id: "working-indicator-row",
771 createdAt: startedAt,
772 },
773 ]);
774 expect(deriveThreadFeedPresentation(presented, null, new Set())).toEqual([]);
775 });
776
777 it("models work-log overflow as list rows", () => {
778 const activity = (
779 id: string,
780 createdAt: string,
781 status: ThreadFeedActivity["status"] = "success",
782 ): ThreadFeedActivity => ({
783 id,
784 createdAt,
785 turnId: null,
786 summary: `Tool ${id}`,
787 detail: null,
788 canExpand: false,
789 getFullDetail: () => null,
790 getCopyText: () => id,
791 icon: "command",
792 toolLike: true,
793 status,
794 });
795 const feed: ThreadFeedEntry[] = [
796 {
797 type: "activity-group",
798 id: "work-group-1",
799 createdAt: "2026-04-01T00:00:01.000Z",
800 turnId: null,
801 activities: [
802 activity("activity-1", "2026-04-01T00:00:01.000Z"),
803 activity("activity-neutral", "2026-04-01T00:00:02.000Z", "neutral"),
804 activity("activity-2", "2026-04-01T00:00:03.000Z"),
805 activity("activity-3", "2026-04-01T00:00:04.000Z"),
806 ],
807 },
808 ];
809
810 const collapsed = deriveThreadFeedPresentation(feed, null, new Set());
811 expect(collapsed.map((entry) => entry.id)).toEqual(["activity-3", "work-toggle:work-group-1"]);
812 expect(collapsed[1]).toMatchObject({
813 type: "work-toggle",
814 groupId: "work-group-1",
815 hiddenCount: 2,
816 expanded: false,
817 });
818
819 const expanded = deriveThreadFeedPresentation(feed, null, new Set(), new Set(["work-group-1"]));
820 expect(expanded.map((entry) => entry.id)).toEqual([
821 "activity-1",
822 "activity-2",
823 "activity-3",
824 "work-toggle:work-group-1",
825 ]);
826 expect(expanded.at(-1)).toMatchObject({
827 type: "work-toggle",
828 expanded: true,
829 });
830 });
831});
832
833describe("quiet timeline: nested agents", () => {
834 it("keeps a nested agent's terminal row but hides its background work", () => {
835 const thread = makeThread({
836 id: ThreadId.make("thread-nested"),
837 projectId: ProjectId.make("project-1"),
838 title: "Nested agents",
839 activities: [
840 // A subagent's own shell: internal, covered by the owner's liveness.
841 makeActivity({
842 id: EventId.make("shell-done"),
843 kind: "task.completed",
844 summary: "Task completed",
845 createdAt: "2026-04-01T00:00:02.000Z",
846 payload: { taskId: "sh-1", agentId: "owner", agentKind: "background" },
847 }),
848 // A nested AGENT's completion: mobile has no Agents sheet, so this
849 // terminal row is the only signal it ever finished.
850 makeActivity({
851 id: EventId.make("nested-done"),
852 kind: "task.completed",
853 summary: "Task completed",
854 createdAt: "2026-04-01T00:00:03.000Z",
855 payload: { taskId: "n-1", agentId: "owner", agentKind: "agent" },
856 }),
857 ],
858 });
859
860 const feed = buildThreadFeed(thread);
861 const ids = feed.flatMap((entry) =>
862 entry.type === "activity-group" ? entry.activities.map((row) => row.id) : [],
863 );
864 expect(ids).toContain("nested-done");
865 expect(ids).not.toContain("shell-done");
866 });
867});