Open History to see the last commit
1import { describe, expect, it } from "vitest";
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 completedAt: "2026-01-01T00:00:10Z",
18 },
19 ]);
20 expect(result).toEqual(new Map([["a1", "2026-01-01T00:00:05Z"]]));
21 });
22
23 it("uses the user message createdAt for the first assistant response", () => {
24 const result = computeMessageDurationStart([
25 { id: "u1", role: "user", createdAt: "2026-01-01T00:00:00Z" },
26 {
27 id: "a1",
28 role: "assistant",
29 createdAt: "2026-01-01T00:00:30Z",
30 completedAt: "2026-01-01T00:00:30Z",
31 },
32 ]);
33
34 expect(result).toEqual(
35 new Map([
36 ["u1", "2026-01-01T00:00:00Z"],
37 ["a1", "2026-01-01T00:00:00Z"],
38 ]),
39 );
40 });
41
42 it("uses the previous assistant completedAt for subsequent assistant responses", () => {
43 const result = computeMessageDurationStart([
44 { id: "u1", role: "user", createdAt: "2026-01-01T00:00:00Z" },
45 {
46 id: "a1",
47 role: "assistant",
48 createdAt: "2026-01-01T00:00:30Z",
49 completedAt: "2026-01-01T00:00:30Z",
50 },
51 {
52 id: "a2",
53 role: "assistant",
54 createdAt: "2026-01-01T00:00:55Z",
55 completedAt: "2026-01-01T00:00:55Z",
56 },
57 ]);
58
59 expect(result).toEqual(
60 new Map([
61 ["u1", "2026-01-01T00:00:00Z"],
62 ["a1", "2026-01-01T00:00:00Z"],
63 ["a2", "2026-01-01T00:00:30Z"],
64 ]),
65 );
66 });
67
68 it("does not advance the boundary for a streaming message without completedAt", () => {
69 const result = computeMessageDurationStart([
70 { id: "u1", role: "user", createdAt: "2026-01-01T00:00:00Z" },
71 { id: "a1", role: "assistant", createdAt: "2026-01-01T00:00:30Z" },
72 {
73 id: "a2",
74 role: "assistant",
75 createdAt: "2026-01-01T00:00:55Z",
76 completedAt: "2026-01-01T00:00:55Z",
77 },
78 ]);
79
80 expect(result).toEqual(
81 new Map([
82 ["u1", "2026-01-01T00:00:00Z"],
83 ["a1", "2026-01-01T00:00:00Z"],
84 ["a2", "2026-01-01T00:00:00Z"],
85 ]),
86 );
87 });
88
89 it("resets the boundary on a new user message", () => {
90 const result = computeMessageDurationStart([
91 { id: "u1", role: "user", createdAt: "2026-01-01T00:00:00Z" },
92 {
93 id: "a1",
94 role: "assistant",
95 createdAt: "2026-01-01T00:00:30Z",
96 completedAt: "2026-01-01T00:00:30Z",
97 },
98 { id: "u2", role: "user", createdAt: "2026-01-01T00:01:00Z" },
99 {
100 id: "a2",
101 role: "assistant",
102 createdAt: "2026-01-01T00:01:20Z",
103 completedAt: "2026-01-01T00:01:20Z",
104 },
105 ]);
106
107 expect(result).toEqual(
108 new Map([
109 ["u1", "2026-01-01T00:00:00Z"],
110 ["a1", "2026-01-01T00:00:00Z"],
111 ["u2", "2026-01-01T00:01:00Z"],
112 ["a2", "2026-01-01T00:01:00Z"],
113 ]),
114 );
115 });
116
117 it("handles system messages without affecting the boundary", () => {
118 const result = computeMessageDurationStart([
119 { id: "u1", role: "user", createdAt: "2026-01-01T00:00:00Z" },
120 { id: "s1", role: "system", createdAt: "2026-01-01T00:00:01Z" },
121 {
122 id: "a1",
123 role: "assistant",
124 createdAt: "2026-01-01T00:00:30Z",
125 completedAt: "2026-01-01T00:00:30Z",
126 },
127 ]);
128
129 expect(result).toEqual(
130 new Map([
131 ["u1", "2026-01-01T00:00:00Z"],
132 ["s1", "2026-01-01T00:00:00Z"],
133 ["a1", "2026-01-01T00:00:00Z"],
134 ]),
135 );
136 });
137
138 it("returns empty map for empty input", () => {
139 expect(computeMessageDurationStart([])).toEqual(new Map());
140 });
141});
142
143describe("normalizeCompactToolLabel", () => {
144 it("removes trailing completion wording from command labels", () => {
145 expect(normalizeCompactToolLabel("Ran command complete")).toBe("Ran command");
146 });
147
148 it("removes trailing completion wording from other labels", () => {
149 expect(normalizeCompactToolLabel("Read file completed")).toBe("Read file");
150 });
151});
152
153describe("resolveAssistantMessageCopyState", () => {
154 it("returns enabled copy state for completed assistant messages", () => {
155 expect(
156 resolveAssistantMessageCopyState({
157 showCopyButton: true,
158 text: "Ship it",
159 streaming: false,
160 }),
161 ).toEqual({
162 text: "Ship it",
163 visible: true,
164 });
165 });
166
167 it("hides copy while an assistant message is still streaming", () => {
168 expect(
169 resolveAssistantMessageCopyState({
170 showCopyButton: true,
171 text: "Still streaming",
172 streaming: true,
173 }),
174 ).toEqual({
175 text: "Still streaming",
176 visible: false,
177 });
178 });
179
180 it("hides copy for empty completed assistant messages", () => {
181 expect(
182 resolveAssistantMessageCopyState({
183 showCopyButton: true,
184 text: " ",
185 streaming: false,
186 }),
187 ).toEqual({
188 text: null,
189 visible: false,
190 });
191 });
192
193 it("hides copy for non-terminal assistant messages", () => {
194 expect(
195 resolveAssistantMessageCopyState({
196 showCopyButton: false,
197 text: "Interim thought",
198 streaming: false,
199 }),
200 ).toEqual({
201 text: "Interim thought",
202 visible: false,
203 });
204 });
205});
206
207describe("deriveMessagesTimelineRows", () => {
208 it("only enables assistant copy for the terminal assistant message in a turn", () => {
209 const rows = deriveMessagesTimelineRows({
210 timelineEntries: [
211 {
212 id: "user-1-entry",
213 kind: "message",
214 createdAt: "2026-01-01T00:00:00Z",
215 message: {
216 id: "user-1" as never,
217 role: "user",
218 text: "Write a poem",
219 turnId: null,
220 createdAt: "2026-01-01T00:00:00Z",
221 streaming: false,
222 },
223 },
224 {
225 id: "assistant-thought-entry",
226 kind: "message",
227 createdAt: "2026-01-01T00:00:10Z",
228 message: {
229 id: "assistant-thought" as never,
230 role: "assistant",
231 text: "I should ground this first.",
232 turnId: "turn-1" as never,
233 createdAt: "2026-01-01T00:00:10Z",
234 completedAt: "2026-01-01T00:00:11Z",
235 streaming: false,
236 },
237 },
238 {
239 id: "assistant-final-entry",
240 kind: "message",
241 createdAt: "2026-01-01T00:00:20Z",
242 message: {
243 id: "assistant-final" as never,
244 role: "assistant",
245 text: "Here is the poem.",
246 turnId: "turn-1" as never,
247 createdAt: "2026-01-01T00:00:20Z",
248 completedAt: "2026-01-01T00:00:30Z",
249 streaming: false,
250 },
251 },
252 ],
253 completionDividerBeforeEntryId: "assistant-final-entry",
254 isWorking: false,
255 activeTurnStartedAt: null,
256 turnDiffSummaryByAssistantMessageId: new Map(),
257 revertTurnCountByUserMessageId: new Map(),
258 });
259
260 const assistantRows = rows.filter(
261 (row): row is Extract<(typeof rows)[number], { kind: "message" }> =>
262 row.kind === "message" && row.message.role === "assistant",
263 );
264
265 expect(assistantRows).toHaveLength(2);
266 expect(assistantRows[0]?.showAssistantCopyButton).toBe(false);
267 expect(assistantRows[1]?.showAssistantCopyButton).toBe(true);
268 expect(assistantRows[1]?.showCompletionDivider).toBe(true);
269 });
270
271 it("projects assistant diff summaries and user revert counts onto the affected rows", () => {
272 const assistantTurnDiffSummary = {
273 turnId: "turn-1" as never,
274 completedAt: "2026-01-01T00:00:30Z",
275 assistantMessageId: "assistant-1" as never,
276 checkpointTurnCount: 2,
277 files: [{ path: "src/index.ts", additions: 3, deletions: 1 }],
278 };
279
280 const rows = deriveMessagesTimelineRows({
281 timelineEntries: [
282 {
283 id: "user-entry",
284 kind: "message",
285 createdAt: "2026-01-01T00:00:00Z",
286 message: {
287 id: "user-1" as never,
288 role: "user",
289 text: "Do the thing",
290 turnId: null,
291 createdAt: "2026-01-01T00:00:00Z",
292 streaming: false,
293 },
294 },
295 {
296 id: "assistant-entry",
297 kind: "message",
298 createdAt: "2026-01-01T00:00:20Z",
299 message: {
300 id: "assistant-1" as never,
301 role: "assistant",
302 text: "Done",
303 turnId: "turn-1" as never,
304 createdAt: "2026-01-01T00:00:20Z",
305 completedAt: "2026-01-01T00:00:30Z",
306 streaming: false,
307 },
308 },
309 ],
310 completionDividerBeforeEntryId: null,
311 isWorking: false,
312 activeTurnStartedAt: null,
313 turnDiffSummaryByAssistantMessageId: new Map([
314 ["assistant-1" as never, assistantTurnDiffSummary],
315 ]),
316 revertTurnCountByUserMessageId: new Map([["user-1" as never, 1]]),
317 });
318
319 const userRow = rows.find(
320 (row): row is Extract<(typeof rows)[number], { kind: "message" }> =>
321 row.kind === "message" && row.message.role === "user",
322 );
323 const assistantRow = rows.find(
324 (row): row is Extract<(typeof rows)[number], { kind: "message" }> =>
325 row.kind === "message" && row.message.role === "assistant",
326 );
327
328 expect(userRow?.revertTurnCount).toBe(1);
329 expect(assistantRow?.assistantTurnDiffSummary).toBe(assistantTurnDiffSummary);
330 });
331});
332
333describe("computeStableMessagesTimelineRows", () => {
334 it("returns the previous result when row order and content are unchanged", () => {
335 const firstUserMessage = {
336 id: "user-1" as never,
337 role: "user" as const,
338 text: "First",
339 turnId: null,
340 createdAt: "2026-01-01T00:00:00Z",
341 streaming: false,
342 };
343 const secondUserMessage = {
344 id: "user-2" as never,
345 role: "user" as const,
346 text: "Second",
347 turnId: null,
348 createdAt: "2026-01-01T00:00:10Z",
349 streaming: false,
350 };
351
352 const rows = deriveMessagesTimelineRows({
353 timelineEntries: [
354 {
355 id: "entry-user-1",
356 kind: "message",
357 createdAt: firstUserMessage.createdAt,
358 message: firstUserMessage,
359 },
360 {
361 id: "entry-user-2",
362 kind: "message",
363 createdAt: secondUserMessage.createdAt,
364 message: secondUserMessage,
365 },
366 ],
367 completionDividerBeforeEntryId: null,
368 isWorking: false,
369 activeTurnStartedAt: null,
370 turnDiffSummaryByAssistantMessageId: new Map(),
371 revertTurnCountByUserMessageId: new Map(),
372 });
373
374 const initial = computeStableMessagesTimelineRows(rows, {
375 byId: new Map(),
376 result: [],
377 });
378
379 const repeated = computeStableMessagesTimelineRows(rows, initial);
380
381 expect(repeated).toBe(initial);
382 expect(repeated.result).toBe(initial.result);
383 });
384
385 it("returns a new result when row order changes without content changes", () => {
386 const firstUserMessage = {
387 id: "user-1" as never,
388 role: "user" as const,
389 text: "First",
390 turnId: null,
391 createdAt: "2026-01-01T00:00:00Z",
392 streaming: false,
393 };
394 const secondUserMessage = {
395 id: "user-2" as never,
396 role: "user" as const,
397 text: "Second",
398 turnId: null,
399 createdAt: "2026-01-01T00:00:10Z",
400 streaming: false,
401 };
402
403 const firstRows = deriveMessagesTimelineRows({
404 timelineEntries: [
405 {
406 id: "entry-user-1",
407 kind: "message",
408 createdAt: firstUserMessage.createdAt,
409 message: firstUserMessage,
410 },
411 {
412 id: "entry-user-2",
413 kind: "message",
414 createdAt: secondUserMessage.createdAt,
415 message: secondUserMessage,
416 },
417 ],
418 completionDividerBeforeEntryId: null,
419 isWorking: false,
420 activeTurnStartedAt: null,
421 turnDiffSummaryByAssistantMessageId: new Map(),
422 revertTurnCountByUserMessageId: new Map(),
423 });
424
425 const initial = computeStableMessagesTimelineRows(firstRows, {
426 byId: new Map(),
427 result: [],
428 });
429
430 const reordered = computeStableMessagesTimelineRows([firstRows[1]!, firstRows[0]!], initial);
431
432 expect(reordered).not.toBe(initial);
433 expect(reordered.result).toEqual([initial.result[1], initial.result[0]]);
434 });
435});