Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { MessageId } from "@t3tools/contracts";
3import type { MessagesTimelineRow } from "./MessagesTimeline.logic";
4import { deriveTimelineMinimapItems, resolveTimelineMinimapPreview } from "./timelineMinimapItems";
5import type { ChatMessage } from "../../types";
6
7function rows(
8 entries: ReadonlyArray<readonly ["user" | "assistant", string]>,
9): MessagesTimelineRow[] {
10 const messages: ChatMessage[] = entries.map(([role, text], index) => ({
11 id: MessageId.make(`message-${index}`),
12 role,
13 text,
14 streaming: false,
15 runId: null,
16 createdAt: new Date(index * 1000).toISOString(),
17 updatedAt: new Date(index * 1000).toISOString(),
18 }));
19 return messages.map((message) => ({
20 kind: "message",
21 id: message.id,
22 createdAt: message.createdAt,
23 message,
24 durationStart: message.createdAt,
25 showAssistantMeta: false,
26 showAssistantCopyButton: false,
27 assistantCopyStreaming: false,
28 }));
29}
30
31describe("timeline minimap previews", () => {
32 it("previews the last assistant response before the next prompt and retains jump targets", () => {
33 const source = rows([
34 ["user", " Inspect\n this "],
35 ["assistant", "Working"],
36 ["assistant", " Done\t now "],
37 ["user", "Next"],
38 ["assistant", "Second answer"],
39 ]);
40 const items = deriveTimelineMinimapItems(source);
41 expect(items).toHaveLength(2);
42 expect(resolveTimelineMinimapPreview(items[0]!)).toEqual({
43 ...items[0],
44 userText: "Inspect this",
45 assistantText: "Done now",
46 });
47 expect(source[items[0]!.rowIndex]!.id).toBe(items[0]!.id);
48 expect(resolveTimelineMinimapPreview(items[1]!)?.assistantText).toBe("Second answer");
49 expect(items[0]?.assistantText).toBe(" Done\t now ");
50 });
51
52 it("handles an unanswered prompt, empty responses, and a closed preview", () => {
53 const items = deriveTimelineMinimapItems(
54 rows([
55 ["user", "First"],
56 ["assistant", " \n\t"],
57 ["user", "Next"],
58 ]),
59 );
60 expect(items.map((item) => resolveTimelineMinimapPreview(item)?.assistantText)).toEqual([
61 null,
62 null,
63 ]);
64 expect(resolveTimelineMinimapPreview(null)).toBeNull();
65 });
66
67 it("renders assistant markdown only when resolving a preview and keeps user text literal", () => {
68 const userText = "Explain **operators** and [x] in [the docs](https://example.com/docs)";
69 const assistantText = [
70 "## Operators",
71 "Use **either** option from [the docs](https://example.com/docs).",
72 "",
73 "| Operator | Meaning |",
74 "| --- | :---: |",
75 "| `a \\| b` | either |",
76 "",
77 "- [x] Match `**/*.ts` files",
78 "",
79 "[x] deployment complete",
80 "",
81 "> Note: ~~old~~ new ",
82 "> Next line",
83 "",
84 "```sh",
85 "# install deps",
86 "- **not bold**",
87 "const snake_case = a * b;",
88 "```",
89 "",
90 "foo<br>bar",
91 "",
92 "<div>Critical <em>answer</em></div>",
93 ].join("\n");
94 const [item] = deriveTimelineMinimapItems(
95 rows([
96 ["user", userText],
97 ["assistant", assistantText],
98 ]),
99 );
100
101 expect(item).toMatchObject({ userText, assistantText });
102 expect(resolveTimelineMinimapPreview(item!)).toEqual({
103 ...item,
104 userText,
105 assistantText:
106 "Operators Use either option from the docs. Operator Meaning a | b either Match **/*.ts files [x] deployment complete Note: old new Next line # install deps - **not bold** const snake_case = a * b; foo bar Critical answer",
107 });
108 expect(item).toMatchObject({ userText, assistantText });
109 });
110
111 it("shows fresh streaming text without changing the jump target", () => {
112 const first = deriveTimelineMinimapItems(
113 rows([
114 ["user", "Explain"],
115 ["assistant", "First"],
116 ]),
117 )[0]!;
118 const next = { ...first, assistantText: "First\n second" };
119 expect(resolveTimelineMinimapPreview(next)).toEqual({
120 ...first,
121 assistantText: "First second",
122 });
123 expect(resolveTimelineMinimapPreview(first)?.assistantText).toBe("First");
124 });
125});