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