Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import {
3 computeStableMessagesTimelineRows,
4 computeMessageDurationStart,
5 deriveMessagesTimelineRows,
6 normalizeCompactToolLabel,
7 resolveAssistantMessageCopyState,
8 shouldPreserveAssistantLineBreaks,
9} from "./MessagesTimeline.logic";
10
11describe("shouldPreserveAssistantLineBreaks", () => {
12 it("preserves Claude insight formatting without changing regular markdown", () => {
13 expect(
14 shouldPreserveAssistantLineBreaks(
15 "★ Insight ─────────────────\\nFirst observation\\nSecond observation\\n─────────────────",
16 ),
17 ).toBe(true);
18 expect(shouldPreserveAssistantLineBreaks("A normal\\nmarkdown paragraph")).toBe(false);
19 });
20});
21
22describe("computeMessageDurationStart", () => {
23 it("returns message createdAt when there is no preceding user message", () => {
24 const result = computeMessageDurationStart([
25 {
26 id: "a1",
27 role: "assistant",
28 createdAt: "2026-01-01T00:00:05Z",
29 updatedAt: "2026-01-01T00:00:10Z",
30 streaming: false,
31 },
32 ]);
33 expect(result).toEqual(new Map([["a1", "2026-01-01T00:00:05Z"]]));
34 });
35
36 it("uses the user message createdAt for the first assistant response", () => {
37 const result = computeMessageDurationStart([
38 {
39 id: "u1",
40 role: "user",
41 createdAt: "2026-01-01T00:00:00Z",
42 updatedAt: "2026-01-01T00:00:00Z",
43 streaming: false,
44 },
45 {
46 id: "a1",
47 role: "assistant",
48 createdAt: "2026-01-01T00:00:30Z",
49 updatedAt: "2026-01-01T00:00:30Z",
50 streaming: false,
51 },
52 ]);
53
54 expect(result).toEqual(
55 new Map([
56 ["u1", "2026-01-01T00:00:00Z"],
57 ["a1", "2026-01-01T00:00:00Z"],
58 ]),
59 );
60 });
61
62 it("uses the previous completed assistant updatedAt for subsequent assistant responses", () => {
63 const result = computeMessageDurationStart([
64 {
65 id: "u1",
66 role: "user",
67 createdAt: "2026-01-01T00:00:00Z",
68 updatedAt: "2026-01-01T00:00:00Z",
69 streaming: false,
70 },
71 {
72 id: "a1",
73 role: "assistant",
74 createdAt: "2026-01-01T00:00:30Z",
75 updatedAt: "2026-01-01T00:00:30Z",
76 streaming: false,
77 },
78 {
79 id: "a2",
80 role: "assistant",
81 createdAt: "2026-01-01T00:00:55Z",
82 updatedAt: "2026-01-01T00:00:55Z",
83 streaming: false,
84 },
85 ]);
86
87 expect(result).toEqual(
88 new Map([
89 ["u1", "2026-01-01T00:00:00Z"],
90 ["a1", "2026-01-01T00:00:00Z"],
91 ["a2", "2026-01-01T00:00:30Z"],
92 ]),
93 );
94 });
95
96 it("does not advance the boundary for a streaming message", () => {
97 const result = computeMessageDurationStart([
98 {
99 id: "u1",
100 role: "user",
101 createdAt: "2026-01-01T00:00:00Z",
102 updatedAt: "2026-01-01T00:00:00Z",
103 streaming: false,
104 },
105 {
106 id: "a1",
107 role: "assistant",
108 createdAt: "2026-01-01T00:00:30Z",
109 updatedAt: "2026-01-01T00:00:40Z",
110 streaming: true,
111 },
112 {
113 id: "a2",
114 role: "assistant",
115 createdAt: "2026-01-01T00:00:55Z",
116 updatedAt: "2026-01-01T00:00:55Z",
117 streaming: false,
118 },
119 ]);
120
121 expect(result).toEqual(
122 new Map([
123 ["u1", "2026-01-01T00:00:00Z"],
124 ["a1", "2026-01-01T00:00:00Z"],
125 ["a2", "2026-01-01T00:00:00Z"],
126 ]),
127 );
128 });
129
130 it("resets the boundary on a new user message", () => {
131 const result = computeMessageDurationStart([
132 {
133 id: "u1",
134 role: "user",
135 createdAt: "2026-01-01T00:00:00Z",
136 updatedAt: "2026-01-01T00:00:00Z",
137 streaming: false,
138 },
139 {
140 id: "a1",
141 role: "assistant",
142 createdAt: "2026-01-01T00:00:30Z",
143 updatedAt: "2026-01-01T00:00:30Z",
144 streaming: false,
145 },
146 {
147 id: "u2",
148 role: "user",
149 createdAt: "2026-01-01T00:01:00Z",
150 updatedAt: "2026-01-01T00:01:00Z",
151 streaming: false,
152 },
153 {
154 id: "a2",
155 role: "assistant",
156 createdAt: "2026-01-01T00:01:20Z",
157 updatedAt: "2026-01-01T00:01:20Z",
158 streaming: false,
159 },
160 ]);
161
162 expect(result).toEqual(
163 new Map([
164 ["u1", "2026-01-01T00:00:00Z"],
165 ["a1", "2026-01-01T00:00:00Z"],
166 ["u2", "2026-01-01T00:01:00Z"],
167 ["a2", "2026-01-01T00:01:00Z"],
168 ]),
169 );
170 });
171
172 it("handles system messages without affecting the boundary", () => {
173 const result = computeMessageDurationStart([
174 {
175 id: "u1",
176 role: "user",
177 createdAt: "2026-01-01T00:00:00Z",
178 updatedAt: "2026-01-01T00:00:00Z",
179 streaming: false,
180 },
181 {
182 id: "s1",
183 role: "system",
184 createdAt: "2026-01-01T00:00:01Z",
185 updatedAt: "2026-01-01T00:00:01Z",
186 streaming: false,
187 },
188 {
189 id: "a1",
190 role: "assistant",
191 createdAt: "2026-01-01T00:00:30Z",
192 updatedAt: "2026-01-01T00:00:30Z",
193 streaming: false,
194 },
195 ]);
196
197 expect(result).toEqual(
198 new Map([
199 ["u1", "2026-01-01T00:00:00Z"],
200 ["s1", "2026-01-01T00:00:00Z"],
201 ["a1", "2026-01-01T00:00:00Z"],
202 ]),
203 );
204 });
205
206 it("returns empty map for empty input", () => {
207 expect(computeMessageDurationStart([])).toEqual(new Map());
208 });
209});
210
211describe("normalizeCompactToolLabel", () => {
212 it("removes trailing completion wording from command labels", () => {
213 expect(normalizeCompactToolLabel("Ran command complete")).toBe("Ran command");
214 });
215
216 it("removes trailing completion wording from other labels", () => {
217 expect(normalizeCompactToolLabel("Read file completed")).toBe("Read file");
218 });
219});
220
221describe("resolveAssistantMessageCopyState", () => {
222 it("returns enabled copy state for completed assistant messages", () => {
223 expect(
224 resolveAssistantMessageCopyState({
225 showCopyButton: true,
226 text: "Ship it",
227 streaming: false,
228 }),
229 ).toEqual({
230 text: "Ship it",
231 visible: true,
232 });
233 });
234
235 it("hides copy while an assistant message is still streaming", () => {
236 expect(
237 resolveAssistantMessageCopyState({
238 showCopyButton: true,
239 text: "Still streaming",
240 streaming: true,
241 }),
242 ).toEqual({
243 text: "Still streaming",
244 visible: false,
245 });
246 });
247
248 it("hides copy for empty completed assistant messages", () => {
249 expect(
250 resolveAssistantMessageCopyState({
251 showCopyButton: true,
252 text: " ",
253 streaming: false,
254 }),
255 ).toEqual({
256 text: null,
257 visible: false,
258 });
259 });
260
261 it("hides copy for non-terminal assistant messages", () => {
262 expect(
263 resolveAssistantMessageCopyState({
264 showCopyButton: false,
265 text: "Interim thought",
266 streaming: false,
267 }),
268 ).toEqual({
269 text: "Interim thought",
270 visible: false,
271 });
272 });
273});
274
275describe("deriveMessagesTimelineRows", () => {
276 it("only enables assistant copy for the terminal assistant message in a turn", () => {
277 const rows = deriveMessagesTimelineRows({
278 timelineEntries: [
279 {
280 id: "user-1-entry",
281 kind: "message",
282 createdAt: "2026-01-01T00:00:00Z",
283 message: {
284 id: "user-1" as never,
285 role: "user",
286 text: "Write a poem",
287 turnId: null,
288 createdAt: "2026-01-01T00:00:00Z",
289 updatedAt: "2026-01-01T00:00:00Z",
290 streaming: false,
291 },
292 },
293 {
294 id: "assistant-thought-entry",
295 kind: "message",
296 createdAt: "2026-01-01T00:00:10Z",
297 message: {
298 id: "assistant-thought" as never,
299 role: "assistant",
300 text: "I should ground this first.",
301 turnId: "turn-1" as never,
302 createdAt: "2026-01-01T00:00:10Z",
303 updatedAt: "2026-01-01T00:00:11Z",
304 streaming: false,
305 },
306 },
307 {
308 id: "assistant-final-entry",
309 kind: "message",
310 createdAt: "2026-01-01T00:00:20Z",
311 message: {
312 id: "assistant-final" as never,
313 role: "assistant",
314 text: "Here is the poem.",
315 turnId: "turn-1" as never,
316 createdAt: "2026-01-01T00:00:20Z",
317 updatedAt: "2026-01-01T00:00:30Z",
318 streaming: false,
319 },
320 },
321 ],
322 expandedTurnIds: new Set(["turn-1" as never]),
323 isWorking: false,
324 activeTurnStartedAt: null,
325 turnDiffSummaryByAssistantMessageId: new Map(),
326 revertTurnCountByUserMessageId: new Map(),
327 });
328
329 const assistantRows = rows.filter(
330 (row): row is Extract<(typeof rows)[number], { kind: "message" }> =>
331 row.kind === "message" && row.message.role === "assistant",
332 );
333
334 expect(assistantRows).toHaveLength(2);
335 expect(assistantRows[0]?.showAssistantCopyButton).toBe(false);
336 expect(assistantRows[1]?.showAssistantCopyButton).toBe(true);
337 });
338
339 it("marks only the active assistant turn as streaming for copy controls", () => {
340 const rows = deriveMessagesTimelineRows({
341 timelineEntries: [
342 {
343 id: "assistant-one-entry",
344 kind: "message",
345 createdAt: "2026-01-01T00:00:10Z",
346 message: {
347 id: "assistant-one" as never,
348 role: "assistant",
349 text: "Earlier response.",
350 turnId: "turn-1" as never,
351 createdAt: "2026-01-01T00:00:10Z",
352 updatedAt: "2026-01-01T00:00:11Z",
353 streaming: false,
354 },
355 },
356 {
357 id: "assistant-two-entry",
358 kind: "message",
359 createdAt: "2026-01-01T00:00:20Z",
360 message: {
361 id: "assistant-two" as never,
362 role: "assistant",
363 text: "Active response.",
364 turnId: "turn-2" as never,
365 createdAt: "2026-01-01T00:00:20Z",
366 updatedAt: "2026-01-01T00:00:30Z",
367 streaming: false,
368 },
369 },
370 ],
371 latestTurn: {
372 turnId: "turn-2" as never,
373 state: "running",
374 startedAt: "2026-01-01T00:00:19Z",
375 completedAt: null,
376 },
377 isWorking: false,
378 activeTurnStartedAt: null,
379 turnDiffSummaryByAssistantMessageId: new Map(),
380 revertTurnCountByUserMessageId: new Map(),
381 });
382
383 const assistantRows = rows.filter(
384 (row): row is Extract<(typeof rows)[number], { kind: "message" }> =>
385 row.kind === "message" && row.message.role === "assistant",
386 );
387
388 expect(assistantRows[0]?.assistantCopyStreaming).toBe(false);
389 expect(assistantRows[1]?.assistantCopyStreaming).toBe(true);
390 });
391
392 it("projects assistant diff summaries and user revert counts onto the affected rows", () => {
393 const assistantTurnDiffSummary = {
394 turnId: "turn-1" as never,
395 completedAt: "2026-01-01T00:00:30Z",
396 assistantMessageId: "assistant-1" as never,
397 checkpointTurnCount: 2,
398 checkpointRef: "checkpoint-1" as never,
399 status: "ready" as const,
400 files: [{ path: "src/index.ts", kind: "modified", additions: 3, deletions: 1 }],
401 };
402
403 const rows = deriveMessagesTimelineRows({
404 timelineEntries: [
405 {
406 id: "user-entry",
407 kind: "message",
408 createdAt: "2026-01-01T00:00:00Z",
409 message: {
410 id: "user-1" as never,
411 role: "user",
412 text: "Do the thing",
413 turnId: null,
414 createdAt: "2026-01-01T00:00:00Z",
415 updatedAt: "2026-01-01T00:00:00Z",
416 streaming: false,
417 },
418 },
419 {
420 id: "assistant-entry",
421 kind: "message",
422 createdAt: "2026-01-01T00:00:20Z",
423 message: {
424 id: "assistant-1" as never,
425 role: "assistant",
426 text: "Done",
427 turnId: "turn-1" as never,
428 createdAt: "2026-01-01T00:00:20Z",
429 updatedAt: "2026-01-01T00:00:30Z",
430 streaming: false,
431 },
432 },
433 ],
434 isWorking: false,
435 activeTurnStartedAt: null,
436 turnDiffSummaryByAssistantMessageId: new Map([
437 ["assistant-1" as never, assistantTurnDiffSummary],
438 ]),
439 revertTurnCountByUserMessageId: new Map([["user-1" as never, 1]]),
440 });
441
442 const userRow = rows.find(
443 (row): row is Extract<(typeof rows)[number], { kind: "message" }> =>
444 row.kind === "message" && row.message.role === "user",
445 );
446 const assistantRow = rows.find(
447 (row): row is Extract<(typeof rows)[number], { kind: "message" }> =>
448 row.kind === "message" && row.message.role === "assistant",
449 );
450
451 expect(userRow?.revertTurnCount).toBe(1);
452 expect(assistantRow?.assistantTurnDiffSummary).toBe(assistantTurnDiffSummary);
453 });
454
455 it("folds settled-turn commentary and work behind a Worked-for row", () => {
456 const timelineEntries = [
457 {
458 id: "user-entry",
459 kind: "message" as const,
460 createdAt: "2026-01-01T00:00:00Z",
461 message: {
462 id: "user-1" as never,
463 role: "user" as const,
464 text: "Build it",
465 turnId: null,
466 createdAt: "2026-01-01T00:00:00Z",
467 updatedAt: "2026-01-01T00:00:00Z",
468 streaming: false,
469 },
470 },
471 {
472 id: "assistant-thought-entry",
473 kind: "message" as const,
474 createdAt: "2026-01-01T00:00:05Z",
475 message: {
476 id: "assistant-thought" as never,
477 role: "assistant" as const,
478 text: "Looking around first.",
479 turnId: "turn-1" as never,
480 createdAt: "2026-01-01T00:00:05Z",
481 updatedAt: "2026-01-01T00:00:06Z",
482 streaming: false,
483 },
484 },
485 {
486 id: "work-entry-1",
487 kind: "work" as const,
488 createdAt: "2026-01-01T00:00:08Z",
489 entry: {
490 id: "work-1",
491 createdAt: "2026-01-01T00:00:08Z",
492 turnId: "turn-1" as never,
493 label: "Ran command",
494 tone: "tool" as const,
495 },
496 },
497 {
498 id: "assistant-final-entry",
499 kind: "message" as const,
500 createdAt: "2026-01-01T00:00:20Z",
501 message: {
502 id: "assistant-final" as never,
503 role: "assistant" as const,
504 text: "Done",
505 turnId: "turn-1" as never,
506 createdAt: "2026-01-01T00:00:20Z",
507 updatedAt: "2026-01-01T00:00:22Z",
508 streaming: false,
509 },
510 },
511 ];
512
513 const collapsedRows = deriveMessagesTimelineRows({
514 timelineEntries,
515 isWorking: false,
516 activeTurnStartedAt: null,
517 turnDiffSummaryByAssistantMessageId: new Map(),
518 revertTurnCountByUserMessageId: new Map(),
519 });
520
521 const foldRow = collapsedRows.find(
522 (row): row is Extract<(typeof collapsedRows)[number], { kind: "turn-fold" }> =>
523 row.kind === "turn-fold",
524 );
525 expect(foldRow?.turnId).toBe("turn-1");
526 expect(foldRow?.expanded).toBe(false);
527 // User message boundary (00:00:00) → terminal message updatedAt (00:00:22).
528 expect(foldRow?.label).toBe("Worked for 22s");
529 expect(collapsedRows.map((row) => row.id)).toEqual([
530 "user-entry",
531 "turn-fold:turn-1",
532 "assistant-final-entry",
533 ]);
534
535 const expandedRows = deriveMessagesTimelineRows({
536 timelineEntries,
537 expandedTurnIds: new Set(["turn-1" as never]),
538 isWorking: false,
539 activeTurnStartedAt: null,
540 turnDiffSummaryByAssistantMessageId: new Map(),
541 revertTurnCountByUserMessageId: new Map(),
542 });
543
544 expect(expandedRows.map((row) => row.id)).toEqual([
545 "user-entry",
546 "turn-fold:turn-1",
547 "assistant-thought-entry",
548 "work-entry-1",
549 "assistant-final-entry",
550 ]);
551 expect(
552 expandedRows.find((row) => row.kind === "turn-fold" && row.expanded === true),
553 ).toBeDefined();
554 });
555
556 it("derives a sane duration for a steer-superseded turn with one instant commentary message", () => {
557 // A steer ends the previous turn early: its only message completes the
558 // instant it is created, and trailing work entries land after it. The
559 // fold duration must span from the user message that started the turn to
560 // the last entry, not message createdAt → message updatedAt (~0ms).
561 const rows = deriveMessagesTimelineRows({
562 timelineEntries: [
563 {
564 id: "user-entry",
565 kind: "message",
566 createdAt: "2026-01-01T00:00:00Z",
567 message: {
568 id: "user-1" as never,
569 role: "user" as const,
570 text: "do it once more",
571 turnId: null,
572 createdAt: "2026-01-01T00:00:00Z",
573 updatedAt: "2026-01-01T00:00:00Z",
574 streaming: false,
575 },
576 },
577 {
578 id: "assistant-commentary-entry",
579 kind: "message",
580 createdAt: "2026-01-01T00:00:09Z",
581 message: {
582 id: "assistant-commentary" as never,
583 role: "assistant" as const,
584 text: "Kicking off call 1.",
585 turnId: "turn-1" as never,
586 createdAt: "2026-01-01T00:00:09Z",
587 updatedAt: "2026-01-01T00:00:09Z",
588 streaming: false,
589 },
590 },
591 {
592 id: "work-entry-1",
593 kind: "work",
594 createdAt: "2026-01-01T00:00:12Z",
595 entry: {
596 id: "work-1",
597 createdAt: "2026-01-01T00:00:12Z",
598 turnId: "turn-1" as never,
599 label: "Ran command",
600 tone: "tool" as const,
601 },
602 },
603 {
604 id: "steer-user-entry",
605 kind: "message",
606 createdAt: "2026-01-01T00:00:14Z",
607 message: {
608 id: "user-2" as never,
609 role: "user" as const,
610 text: "actually do 15",
611 turnId: null,
612 createdAt: "2026-01-01T00:00:14Z",
613 updatedAt: "2026-01-01T00:00:14Z",
614 streaming: false,
615 },
616 },
617 {
618 id: "assistant-next-turn-entry",
619 kind: "message",
620 createdAt: "2026-01-01T00:00:17Z",
621 message: {
622 id: "assistant-next" as never,
623 role: "assistant" as const,
624 text: "One down — adjusting.",
625 turnId: "turn-2" as never,
626 createdAt: "2026-01-01T00:00:17Z",
627 updatedAt: "2026-01-01T00:00:17Z",
628 streaming: true,
629 },
630 },
631 ],
632 latestTurn: {
633 turnId: "turn-2" as never,
634 state: "running",
635 startedAt: "2026-01-01T00:00:14Z",
636 completedAt: null,
637 },
638 isWorking: true,
639 activeTurnStartedAt: "2026-01-01T00:00:14Z",
640 turnDiffSummaryByAssistantMessageId: new Map(),
641 revertTurnCountByUserMessageId: new Map(),
642 });
643
644 const foldRow = rows.find(
645 (row): row is Extract<(typeof rows)[number], { kind: "turn-fold" }> =>
646 row.kind === "turn-fold",
647 );
648 // User message (00:00:00) → trailing work entry (00:00:12).
649 expect(foldRow?.turnId).toBe("turn-1");
650 expect(foldRow?.label).toBe("Worked for 12s");
651 });
652
653 it("uses latest-turn timings and the stopped label for an interrupted latest turn", () => {
654 const rows = deriveMessagesTimelineRows({
655 timelineEntries: [
656 {
657 id: "work-entry-1",
658 kind: "work",
659 createdAt: "2026-01-01T00:00:05Z",
660 entry: {
661 id: "work-1",
662 createdAt: "2026-01-01T00:00:05Z",
663 turnId: "turn-1" as never,
664 label: "Ran command",
665 tone: "tool" as const,
666 },
667 },
668 ],
669 latestTurn: {
670 turnId: "turn-1" as never,
671 state: "interrupted",
672 startedAt: "2026-01-01T00:00:00Z",
673 completedAt: "2026-01-01T00:00:47Z",
674 },
675 isWorking: false,
676 activeTurnStartedAt: null,
677 turnDiffSummaryByAssistantMessageId: new Map(),
678 revertTurnCountByUserMessageId: new Map(),
679 });
680
681 expect(rows).toEqual([
682 expect.objectContaining({
683 kind: "turn-fold",
684 turnId: "turn-1",
685 label: "You stopped after 47s",
686 expanded: false,
687 }),
688 ]);
689 });
690
691 it("keeps the previous turn folded while a newly sent message awaits its turn", () => {
692 // Right after send, isWorking is true but latestTurn still points at the
693 // previous, settled turn — it must stay folded through that window.
694 const rows = deriveMessagesTimelineRows({
695 timelineEntries: [
696 {
697 id: "work-entry-1",
698 kind: "work",
699 createdAt: "2026-01-01T00:00:05Z",
700 entry: {
701 id: "work-1",
702 createdAt: "2026-01-01T00:00:05Z",
703 turnId: "turn-1" as never,
704 label: "Ran command",
705 tone: "tool" as const,
706 },
707 },
708 {
709 id: "assistant-final-entry",
710 kind: "message",
711 createdAt: "2026-01-01T00:00:20Z",
712 message: {
713 id: "assistant-final" as never,
714 role: "assistant",
715 text: "Done",
716 turnId: "turn-1" as never,
717 createdAt: "2026-01-01T00:00:20Z",
718 updatedAt: "2026-01-01T00:00:22Z",
719 streaming: false,
720 },
721 },
722 {
723 id: "user-followup-entry",
724 kind: "message",
725 createdAt: "2026-01-01T00:01:00Z",
726 message: {
727 id: "user-followup" as never,
728 role: "user",
729 text: "yooo",
730 turnId: null,
731 createdAt: "2026-01-01T00:01:00Z",
732 updatedAt: "2026-01-01T00:01:00Z",
733 streaming: false,
734 },
735 },
736 ],
737 latestTurn: {
738 turnId: "turn-1" as never,
739 state: "completed",
740 startedAt: "2026-01-01T00:00:00Z",
741 completedAt: "2026-01-01T00:00:22Z",
742 },
743 isWorking: true,
744 activeTurnStartedAt: "2026-01-01T00:01:00Z",
745 turnDiffSummaryByAssistantMessageId: new Map(),
746 revertTurnCountByUserMessageId: new Map(),
747 });
748
749 expect(rows.map((row) => row.id)).toEqual([
750 "turn-fold:turn-1",
751 "assistant-final-entry",
752 "user-followup-entry",
753 "working-indicator-row",
754 ]);
755 const finalRow = rows.find((row) => row.id === "assistant-final-entry");
756 expect(finalRow?.kind === "message" && finalRow.showAssistantMeta).toBe(true);
757 });
758
759 it("does not fold the active in-progress turn", () => {
760 const rows = deriveMessagesTimelineRows({
761 timelineEntries: [
762 {
763 id: "assistant-thought-entry",
764 kind: "message",
765 createdAt: "2026-01-01T00:00:05Z",
766 message: {
767 id: "assistant-thought" as never,
768 role: "assistant",
769 text: "Working on it.",
770 turnId: "turn-1" as never,
771 createdAt: "2026-01-01T00:00:05Z",
772 updatedAt: "2026-01-01T00:00:06Z",
773 streaming: false,
774 },
775 },
776 {
777 id: "work-entry-1",
778 kind: "work",
779 createdAt: "2026-01-01T00:00:08Z",
780 entry: {
781 id: "work-1",
782 createdAt: "2026-01-01T00:00:08Z",
783 turnId: "turn-1" as never,
784 label: "Ran command",
785 tone: "tool" as const,
786 },
787 },
788 ],
789 latestTurn: {
790 turnId: "turn-1" as never,
791 state: "running",
792 startedAt: "2026-01-01T00:00:00Z",
793 completedAt: null,
794 },
795 isWorking: true,
796 activeTurnStartedAt: "2026-01-01T00:00:00Z",
797 turnDiffSummaryByAssistantMessageId: new Map(),
798 revertTurnCountByUserMessageId: new Map(),
799 });
800
801 expect(rows.some((row) => row.kind === "turn-fold")).toBe(false);
802 expect(rows.map((row) => row.id)).toEqual([
803 "assistant-thought-entry",
804 "work-entry-1",
805 "working-indicator-row",
806 ]);
807 });
808
809 it("does not fold the session's running turn when latestTurn regresses", () => {
810 const rows = deriveMessagesTimelineRows({
811 timelineEntries: [
812 {
813 id: "previous-work-entry",
814 kind: "work",
815 createdAt: "2026-01-01T00:00:05Z",
816 entry: {
817 id: "previous-work",
818 createdAt: "2026-01-01T00:00:05Z",
819 turnId: "turn-1" as never,
820 label: "Read files",
821 tone: "tool" as const,
822 },
823 },
824 {
825 id: "user-followup-entry",
826 kind: "message",
827 createdAt: "2026-01-01T00:01:00Z",
828 message: {
829 id: "user-followup" as never,
830 role: "user",
831 text: "continue",
832 turnId: null,
833 createdAt: "2026-01-01T00:01:00Z",
834 updatedAt: "2026-01-01T00:01:00Z",
835 streaming: false,
836 },
837 },
838 {
839 id: "running-work-entry",
840 kind: "work",
841 createdAt: "2026-01-01T00:01:05Z",
842 entry: {
843 id: "running-work",
844 createdAt: "2026-01-01T00:01:05Z",
845 turnId: "turn-2" as never,
846 label: "Searched files",
847 tone: "tool" as const,
848 },
849 },
850 ],
851 latestTurn: {
852 turnId: "turn-1" as never,
853 state: "completed",
854 startedAt: "2026-01-01T00:00:00Z",
855 completedAt: "2026-01-01T00:00:25Z",
856 },
857 runningTurnId: "turn-2" as never,
858 isWorking: true,
859 activeTurnStartedAt: "2026-01-01T00:01:00Z",
860 turnDiffSummaryByAssistantMessageId: new Map(),
861 revertTurnCountByUserMessageId: new Map(),
862 });
863
864 expect(rows.filter((row) => row.kind === "turn-fold").map((row) => row.turnId)).toEqual([
865 "turn-1",
866 ]);
867 expect(rows.map((row) => row.id)).toContain("running-work-entry");
868 });
869
870 it("only shows assistant metadata on the terminal assistant message", () => {
871 const rows = deriveMessagesTimelineRows({
872 timelineEntries: [
873 {
874 id: "assistant-thought-entry",
875 kind: "message",
876 createdAt: "2026-01-01T00:00:10Z",
877 message: {
878 id: "assistant-thought" as never,
879 role: "assistant",
880 text: "Checking first.",
881 turnId: "turn-1" as never,
882 createdAt: "2026-01-01T00:00:10Z",
883 updatedAt: "2026-01-01T00:00:11Z",
884 streaming: false,
885 },
886 },
887 {
888 id: "assistant-final-entry",
889 kind: "message",
890 createdAt: "2026-01-01T00:00:20Z",
891 message: {
892 id: "assistant-final" as never,
893 role: "assistant",
894 text: "Done.",
895 turnId: "turn-1" as never,
896 createdAt: "2026-01-01T00:00:20Z",
897 updatedAt: "2026-01-01T00:00:30Z",
898 streaming: false,
899 },
900 },
901 ],
902 expandedTurnIds: new Set(["turn-1" as never]),
903 isWorking: false,
904 activeTurnStartedAt: null,
905 turnDiffSummaryByAssistantMessageId: new Map(),
906 revertTurnCountByUserMessageId: new Map(),
907 });
908
909 const assistantRows = rows.filter(
910 (row): row is Extract<(typeof rows)[number], { kind: "message" }> =>
911 row.kind === "message" && row.message.role === "assistant",
912 );
913
914 expect(assistantRows.map((row) => row.showAssistantMeta)).toEqual([false, true]);
915 });
916
917 it("withholds assistant metadata while the active turn is still in progress", () => {
918 const rows = deriveMessagesTimelineRows({
919 timelineEntries: [
920 {
921 id: "assistant-thought-entry",
922 kind: "message",
923 createdAt: "2026-01-01T00:00:10Z",
924 message: {
925 id: "assistant-thought" as never,
926 role: "assistant",
927 text: "Working on it.",
928 turnId: "turn-1" as never,
929 createdAt: "2026-01-01T00:00:10Z",
930 updatedAt: "2026-01-01T00:00:11Z",
931 streaming: false,
932 },
933 },
934 ],
935 latestTurn: {
936 turnId: "turn-1" as never,
937 state: "running",
938 startedAt: "2026-01-01T00:00:00Z",
939 completedAt: null,
940 },
941 isWorking: true,
942 activeTurnStartedAt: "2026-01-01T00:00:00Z",
943 turnDiffSummaryByAssistantMessageId: new Map(),
944 revertTurnCountByUserMessageId: new Map(),
945 });
946
947 const assistantRow = rows.find(
948 (row): row is Extract<(typeof rows)[number], { kind: "message" }> =>
949 row.kind === "message" && row.message.role === "assistant",
950 );
951
952 expect(assistantRow?.showAssistantMeta).toBe(false);
953 expect(assistantRow?.showAssistantCopyButton).toBe(false);
954 });
955
956 it("models work log overflow expansion as inserted list rows", () => {
957 const timelineEntries = [
958 {
959 id: "work-entry-1",
960 kind: "work" as const,
961 createdAt: "2026-01-01T00:00:01Z",
962 entry: {
963 id: "work-1",
964 createdAt: "2026-01-01T00:00:01Z",
965 label: "read",
966 detail: "Reading package.json",
967 tone: "tool" as const,
968 },
969 },
970 {
971 id: "work-entry-2",
972 kind: "work" as const,
973 createdAt: "2026-01-01T00:00:02Z",
974 entry: {
975 id: "work-2",
976 createdAt: "2026-01-01T00:00:02Z",
977 label: "edit",
978 detail: "Editing MessagesTimeline.tsx",
979 tone: "tool" as const,
980 },
981 },
982 {
983 id: "work-entry-3",
984 kind: "work" as const,
985 createdAt: "2026-01-01T00:00:03Z",
986 entry: {
987 id: "work-3",
988 createdAt: "2026-01-01T00:00:03Z",
989 label: "test",
990 detail: "Running tests",
991 tone: "tool" as const,
992 },
993 },
994 ];
995
996 const baseInput = {
997 timelineEntries,
998 isWorking: false,
999 activeTurnStartedAt: null,
1000 turnDiffSummaryByAssistantMessageId: new Map(),
1001 revertTurnCountByUserMessageId: new Map(),
1002 };
1003 const collapsedRows = deriveMessagesTimelineRows(baseInput);
1004 const expandedRows = deriveMessagesTimelineRows({
1005 ...baseInput,
1006 expandedWorkGroupIds: new Set(["work-group:work-entry-1"]),
1007 });
1008
1009 expect(collapsedRows.map((row) => row.id)).toEqual(["work-3", "work-toggle:work-entry-1"]);
1010 expect(collapsedRows.find((row) => row.kind === "work-toggle")).toMatchObject({
1011 groupId: "work-group:work-entry-1",
1012 hiddenCount: 2,
1013 expanded: false,
1014 onlyToolEntries: true,
1015 });
1016 expect(expandedRows.map((row) => row.id)).toEqual([
1017 "work-1",
1018 "work-2",
1019 "work-3",
1020 "work-toggle:work-entry-1",
1021 ]);
1022 expect(expandedRows.find((row) => row.kind === "work-toggle")).toMatchObject({
1023 expanded: true,
1024 });
1025 });
1026});
1027
1028describe("computeStableMessagesTimelineRows", () => {
1029 it("returns the previous result when row order and content are unchanged", () => {
1030 const firstUserMessage = {
1031 id: "user-1" as never,
1032 role: "user" as const,
1033 text: "First",
1034 turnId: null,
1035 createdAt: "2026-01-01T00:00:00Z",
1036 updatedAt: "2026-01-01T00:00:00Z",
1037 streaming: false,
1038 };
1039 const secondUserMessage = {
1040 id: "user-2" as never,
1041 role: "user" as const,
1042 text: "Second",
1043 turnId: null,
1044 createdAt: "2026-01-01T00:00:10Z",
1045 updatedAt: "2026-01-01T00:00:10Z",
1046 streaming: false,
1047 };
1048
1049 const rows = deriveMessagesTimelineRows({
1050 timelineEntries: [
1051 {
1052 id: "entry-user-1",
1053 kind: "message",
1054 createdAt: firstUserMessage.createdAt,
1055 message: firstUserMessage,
1056 },
1057 {
1058 id: "entry-user-2",
1059 kind: "message",
1060 createdAt: secondUserMessage.createdAt,
1061 message: secondUserMessage,
1062 },
1063 ],
1064 isWorking: false,
1065 activeTurnStartedAt: null,
1066 turnDiffSummaryByAssistantMessageId: new Map(),
1067 revertTurnCountByUserMessageId: new Map(),
1068 });
1069
1070 const initial = computeStableMessagesTimelineRows(rows, {
1071 byId: new Map(),
1072 result: [],
1073 });
1074
1075 const repeated = computeStableMessagesTimelineRows(rows, initial);
1076
1077 expect(repeated).toBe(initial);
1078 expect(repeated.result).toBe(initial.result);
1079 });
1080
1081 it("reuses work rows when equivalent timeline derivations create new grouped arrays", () => {
1082 const firstWorkEntry = {
1083 id: "work-1",
1084 createdAt: "2026-01-01T00:00:00Z",
1085 label: "thinking",
1086 detail: "Inspecting repository state",
1087 tone: "thinking" as const,
1088 };
1089 const secondWorkEntry = {
1090 id: "work-2",
1091 createdAt: "2026-01-01T00:00:01Z",
1092 label: "read",
1093 detail: "Reading package.json",
1094 tone: "tool" as const,
1095 };
1096
1097 const createRows = () =>
1098 deriveMessagesTimelineRows({
1099 timelineEntries: [
1100 {
1101 id: "entry-work-1",
1102 kind: "work",
1103 createdAt: firstWorkEntry.createdAt,
1104 entry: firstWorkEntry,
1105 },
1106 {
1107 id: "entry-work-2",
1108 kind: "work",
1109 createdAt: secondWorkEntry.createdAt,
1110 entry: secondWorkEntry,
1111 },
1112 ],
1113 isWorking: false,
1114 activeTurnStartedAt: null,
1115 turnDiffSummaryByAssistantMessageId: new Map(),
1116 revertTurnCountByUserMessageId: new Map(),
1117 });
1118
1119 const firstRows = createRows();
1120 const initial = computeStableMessagesTimelineRows(firstRows, {
1121 byId: new Map(),
1122 result: [],
1123 });
1124 const secondRows = createRows();
1125
1126 expect(secondRows[0]).not.toBe(firstRows[0]);
1127
1128 const repeated = computeStableMessagesTimelineRows(secondRows, initial);
1129
1130 expect(repeated).toBe(initial);
1131 expect(repeated.result[0]).toBe(initial.result[0]);
1132 });
1133
1134 it("returns a new result when row order changes without content changes", () => {
1135 const firstUserMessage = {
1136 id: "user-1" as never,
1137 role: "user" as const,
1138 text: "First",
1139 turnId: null,
1140 createdAt: "2026-01-01T00:00:00Z",
1141 updatedAt: "2026-01-01T00:00:00Z",
1142 streaming: false,
1143 };
1144 const secondUserMessage = {
1145 id: "user-2" as never,
1146 role: "user" as const,
1147 text: "Second",
1148 turnId: null,
1149 createdAt: "2026-01-01T00:00:10Z",
1150 updatedAt: "2026-01-01T00:00:10Z",
1151 streaming: false,
1152 };
1153
1154 const firstRows = deriveMessagesTimelineRows({
1155 timelineEntries: [
1156 {
1157 id: "entry-user-1",
1158 kind: "message",
1159 createdAt: firstUserMessage.createdAt,
1160 message: firstUserMessage,
1161 },
1162 {
1163 id: "entry-user-2",
1164 kind: "message",
1165 createdAt: secondUserMessage.createdAt,
1166 message: secondUserMessage,
1167 },
1168 ],
1169 isWorking: false,
1170 activeTurnStartedAt: null,
1171 turnDiffSummaryByAssistantMessageId: new Map(),
1172 revertTurnCountByUserMessageId: new Map(),
1173 });
1174
1175 const initial = computeStableMessagesTimelineRows(firstRows, {
1176 byId: new Map(),
1177 result: [],
1178 });
1179
1180 const reordered = computeStableMessagesTimelineRows([firstRows[1]!, firstRows[0]!], initial);
1181
1182 expect(reordered).not.toBe(initial);
1183 expect(reordered.result).toEqual([initial.result[1], initial.result[0]]);
1184 });
1185});