Open History to see the last commit
1import { MessageId, RunAttemptId, RunId, type ScreenReaderAnnouncements } from "@t3tools/contracts";
2import * as DateTime from "effect/DateTime";
3import { describe, expect, it } from "vite-plus/test";
4
5import type { TimelineAttempt, TimelineEntry, WorkLogEntry } from "../../session-logic";
6import {
7 nextTurnDetailAnnouncement,
8 turnDetail,
9 type TurnDetailMemory,
10 type TurnDetailSnapshot,
11 type TurnMessage,
12} from "./turnDetailAnnouncement";
13
14const run = RunId.make("run-1");
15const at = "2026-01-01T00:00:00.000Z";
16
17function message(
18 id: string,
19 role: "user" | "assistant",
20 fields: Partial<Extract<TimelineEntry, { kind: "message" }>["message"]> = {},
21): TimelineEntry {
22 return {
23 kind: "message",
24 id,
25 createdAt: at,
26 message: {
27 id: MessageId.make(id),
28 role,
29 text: id,
30 runId: role === "assistant" ? run : null,
31 streaming: false,
32 createdAt: at,
33 updatedAt: at,
34 ...fields,
35 },
36 };
37}
38
39function work(id: string, fields: Partial<WorkLogEntry> = {}): TimelineEntry {
40 return {
41 kind: "work",
42 id,
43 createdAt: at,
44 entry: { id, createdAt: at, runId: run, label: id, tone: "tool", ...fields },
45 };
46}
47
48const prompt = message("prompt", "user");
49const command = (id: string, fields: Partial<WorkLogEntry> = {}) =>
50 work(id, {
51 itemType: "command_execution",
52 command: "git status",
53 toolLifecycleStatus: "inProgress",
54 ...fields,
55 });
56const notice = work("notice", { itemType: "system_notice", tone: "info", label: "Heads up" });
57const steer = message("steer", "user", { inputIntent: "steer" });
58const time = (clock: string) => DateTime.makeUnsafe(`2026-01-01T${clock}.000Z`);
59const inAttempt = (
60 entry: TimelineEntry,
61 id: string,
62 status: TimelineAttempt["status"],
63 completed?: string,
64): TimelineEntry => ({
65 ...entry,
66 attempt: {
67 id: RunAttemptId.make(id),
68 runId: run,
69 attemptOrdinal: 1,
70 rootNodeId: `${id}-root` as TimelineAttempt["rootNodeId"],
71 status,
72 completedAt: completed === undefined ? null : time(completed),
73 },
74});
75const completedAt = (entry: TimelineEntry, clock: string) =>
76 ({
77 ...entry,
78 projectedItem: {
79 item: { type: "assistant_message", status: "completed", completedAt: time(clock) },
80 },
81 }) as TimelineEntry;
82
83describe("turnDetail", () => {
84 it.each<{
85 name: string;
86 entries: ReadonlyArray<TimelineEntry>;
87 activity: string | null;
88 }>([
89 { name: "a running command", entries: [prompt, command("git")], activity: "Running git" },
90 {
91 name: "reasoning, without its text",
92 entries: [
93 prompt,
94 work("thought", { itemType: "reasoning", tone: "thinking", detail: "Private idea." }),
95 ],
96 activity: "Thinking",
97 },
98 {
99 name: "a file change, with the verb its icon stands for",
100 entries: [prompt, work("edit", { itemType: "file_change", changedFiles: ["search.ts"] })],
101 activity: "Editing search.ts",
102 },
103 {
104 name: "the step under a request and a notice",
105 entries: [prompt, command("git"), work("ask", { itemType: "approval_request" }), notice],
106 activity: "Running git",
107 },
108 {
109 name: "nothing for a failed step",
110 entries: [prompt, command("git", { toolLifecycleStatus: "failed" })],
111 activity: null,
112 },
113 {
114 name: "nothing once the agent is writing again",
115 entries: [prompt, command("git"), message("reply", "assistant", { streaming: true })],
116 activity: null,
117 },
118 {
119 name: "nothing while the context is compacted",
120 entries: [
121 prompt,
122 command("git"),
123 work("compact", { tone: "info", sourceActivityKind: "context-compaction" }),
124 ],
125 activity: null,
126 },
127 {
128 name: "nothing from the turn before",
129 entries: [message("earlier", "user"), command("git"), prompt],
130 activity: null,
131 },
132 ])("names $name", ({ entries, activity }) => {
133 expect(turnDetail(entries, undefined, null).activity).toBe(activity);
134 });
135
136 it.each<{
137 name: string;
138 entries: ReadonlyArray<TimelineEntry>;
139 finished: ReadonlyArray<string>;
140 }>([
141 {
142 name: "a message the agent kept working after, but not its last",
143 entries: [
144 prompt,
145 message("first", "assistant"),
146 command("git"),
147 message("last", "assistant"),
148 ],
149 finished: ["first"],
150 },
151 {
152 name: "a message once the next one starts",
153 entries: [
154 prompt,
155 message("first", "assistant"),
156 message("second", "assistant", { streaming: true }),
157 ],
158 finished: ["first"],
159 },
160 {
161 name: "no reply a steer cut off",
162 entries: [
163 prompt,
164 inAttempt(message("cut off", "assistant"), "attempt-1", "superseded"),
165 steer,
166 inAttempt(command("git"), "attempt-2", "running"),
167 ],
168 finished: [],
169 },
170 {
171 name: "a reply that completed before a steer replaced its attempt",
172 entries: [
173 prompt,
174 completedAt(
175 inAttempt(message("complete", "assistant"), "attempt-1", "superseded", "00:00:10"),
176 "00:00:04",
177 ),
178 steer,
179 inAttempt(command("git"), "attempt-2", "running"),
180 ],
181 finished: ["complete"],
182 },
183 {
184 name: "no last message that only a notice follows",
185 entries: [prompt, command("git"), message("last", "assistant"), notice],
186 finished: [],
187 },
188 {
189 name: "no last message when another run picks the thread up",
190 entries: [
191 prompt,
192 message("last", "assistant"),
193 command("git", { runId: RunId.make("run-2") }),
194 ],
195 finished: [],
196 },
197 ])("counts $name as finished", ({ entries, finished }) => {
198 const { messages } = turnDetail(entries, undefined, null);
199 expect(messages.filter((spoken) => spoken.finished).map((spoken) => spoken.text)).toEqual(
200 finished,
201 );
202 });
203
204 it.each<{ name: string; knownTailId: string | null; fresh: ReadonlyArray<boolean> }>([
205 { name: "after the entry last seen", knownTailId: "seen", fresh: [false, true] },
206 { name: "after a prompt that was last seen", knownTailId: "prompt", fresh: [true, true] },
207 { name: "not when the entry last seen is gone", knownTailId: "gone", fresh: [false, false] },
208 ])("marks messages as fresh $name", ({ knownTailId, fresh }) => {
209 const entries = [
210 prompt,
211 message("loaded", "assistant"),
212 command("seen"),
213 message("new", "assistant"),
214 command("git"),
215 ];
216 expect(
217 turnDetail(entries, undefined, knownTailId).messages.map((spoken) => spoken.fresh),
218 ).toEqual(fresh);
219 });
220});
221
222type Frame = Pick<TurnDetailSnapshot, "status" | "activity" | "messages">;
223const said = (id: string, finished = true, fresh = false): TurnMessage => ({
224 id,
225 text: id,
226 finished,
227 fresh,
228});
229const frame = (
230 activity: string | null,
231 messages: ReadonlyArray<TurnMessage> = [],
232 status: TurnDetailSnapshot["status"] = "working",
233): Frame => ({ status, activity, messages });
234
235function speak(level: ScreenReaderAnnouncements, frames: ReadonlyArray<Frame>) {
236 let memory: TurnDetailMemory | null = null;
237 const spoken: string[] = [];
238 for (const next of frames) {
239 const result = nextTurnDetailAnnouncement(memory, { ...next, level, tailId: null });
240 memory = result.memory;
241 spoken.push(...result.messages);
242 if (result.activity !== null) spoken.push(result.activity);
243 }
244 return spoken;
245}
246
247describe("nextTurnDetailAnnouncement", () => {
248 const turn = [
249 frame(null),
250 frame("Running git", [said("first", false)]),
251 frame("Running git", [said("first", false)]),
252 frame("Reading a.ts", [said("first"), said("second", false)]),
253 frame(null, [said("first"), said("second")]),
254 ];
255
256 it.each<{ level: ScreenReaderAnnouncements; spoken: ReadonlyArray<string> }>([
257 { level: "status", spoken: [] },
258 { level: "messages", spoken: ["T3 Code: first", "T3 Code: second"] },
259 {
260 level: "everything",
261 spoken: ["Running git", "T3 Code: first", "Reading a.ts", "T3 Code: second"],
262 },
263 ])("says each message and step once at the $level level", ({ level, spoken }) => {
264 expect(speak(level, turn)).toEqual(spoken);
265 });
266
267 it("stays silent about what was there when the thread opened", () => {
268 expect(
269 speak("everything", [
270 frame("Running git", [said("first")]),
271 frame("Running git", [said("first")]),
272 frame("Reading a.ts", [said("first"), said("second", true, true)]),
273 ]),
274 ).toEqual(["T3 Code: second", "Reading a.ts"]);
275 });
276
277 it("stays silent about history loaded above the messages it knows", () => {
278 expect(
279 speak("messages", [
280 frame(null, [said("recent")]),
281 frame(null, [said("older"), said("recent")]),
282 frame(null, [said("older"), said("recent")]),
283 ]),
284 ).toEqual([]);
285 });
286
287 it("reads Markdown as the text it renders and skips a message with none", () => {
288 const first = {
289 ...said("first", true, true),
290 text: "**Tests** pass in `web`.\n\n- See [the docs](https://x.test/a(b)c?token=secret)<br>",
291 };
292 expect(
293 speak("messages", [
294 frame(null),
295 frame(null, [first, { ...said("empty", true, true), text: "<!-- note -->" }]),
296 ]),
297 ).toEqual(["T3 Code: Tests pass in web. See the docs"]);
298 });
299
300 it("says a step again after waiting on the user", () => {
301 expect(
302 speak("everything", [
303 frame(null),
304 frame("Running git"),
305 frame("Running git", [], "approval"),
306 frame("Running git"),
307 ]),
308 ).toEqual(["Running git", "Running git"]);
309 });
310
311 it("stays silent about a turn that already ended", () => {
312 expect(
313 speak("everything", [
314 frame(null, [], "idle"),
315 frame("Running git", [said("first", true, true)], "idle"),
316 frame("Running git", [said("first")]),
317 ]),
318 ).toEqual(["Running git"]);
319 });
320});