The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { MessageId, RunAttemptId, RunId, type ScreenReaderAnnouncements } from "@t3tools/contracts"; |
| 2 | import * as DateTime from "effect/DateTime"; |
| 3 | import { describe, expect, it } from "vite-plus/test"; |
| 4 | |
| 5 | import type { TimelineAttempt, TimelineEntry, WorkLogEntry } from "../../session-logic"; |
| 6 | import { |
| 7 | nextTurnDetailAnnouncement, |
| 8 | turnDetail, |
| 9 | type TurnDetailMemory, |
| 10 | type TurnDetailSnapshot, |
| 11 | type TurnMessage, |
| 12 | } from "./turnDetailAnnouncement"; |
| 13 | |
| 14 | const run = RunId.make("run-1"); |
| 15 | const at = "2026-01-01T00:00:00.000Z"; |
| 16 | |
| 17 | function 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 | |
| 39 | function 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 | |
| 48 | const prompt = message("prompt", "user"); |
| 49 | const command = (id: string, fields: Partial<WorkLogEntry> = {}) => |
| 50 | work(id, { |
| 51 | itemType: "command_execution", |
| 52 | command: "git status", |
| 53 | toolLifecycleStatus: "inProgress", |
| 54 | ...fields, |
| 55 | }); |
| 56 | const notice = work("notice", { itemType: "system_notice", tone: "info", label: "Heads up" }); |
| 57 | const steer = message("steer", "user", { inputIntent: "steer" }); |
| 58 | const time = (clock: string) => DateTime.makeUnsafe(`2026-01-01T${clock}.000Z`); |
| 59 | const 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 | }); |
| 75 | const 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 | |
| 83 | describe("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: "the context being compacted", |
| 120 | entries: [ |
| 121 | prompt, |
| 122 | command("git"), |
| 123 | work("compact", { |
| 124 | tone: "info", |
| 125 | sourceActivityKind: "context-compaction", |
| 126 | label: "Compacting context", |
| 127 | toolLifecycleStatus: "inProgress", |
| 128 | }), |
| 129 | ], |
| 130 | activity: "Compacting context", |
| 131 | }, |
| 132 | { |
| 133 | name: "nothing once the context is compacted", |
| 134 | entries: [ |
| 135 | prompt, |
| 136 | command("git"), |
| 137 | work("compact", { |
| 138 | tone: "info", |
| 139 | sourceActivityKind: "context-compaction", |
| 140 | label: "Context compacted", |
| 141 | toolLifecycleStatus: "completed", |
| 142 | }), |
| 143 | ], |
| 144 | activity: null, |
| 145 | }, |
| 146 | { |
| 147 | name: "nothing from the turn before", |
| 148 | entries: [message("earlier", "user"), command("git"), prompt], |
| 149 | activity: null, |
| 150 | }, |
| 151 | ])("names $name", ({ entries, activity }) => { |
| 152 | expect(turnDetail(entries, undefined, null).activity).toBe(activity); |
| 153 | }); |
| 154 | |
| 155 | it("cuts a long step without splitting an emoji", () => { |
| 156 | const edit = work("edit", { itemType: "file_change", changedFiles: [`${"a".repeat(111)}😀z`] }); |
| 157 | expect(turnDetail([prompt, edit], undefined, null).activity).not.toMatch(/[\uD800-\uDFFF]/u); |
| 158 | }); |
| 159 | |
| 160 | it.each<{ |
| 161 | name: string; |
| 162 | entries: ReadonlyArray<TimelineEntry>; |
| 163 | finished: ReadonlyArray<string>; |
| 164 | }>([ |
| 165 | { |
| 166 | name: "a message the agent kept working after, but not its last", |
| 167 | entries: [ |
| 168 | prompt, |
| 169 | message("first", "assistant"), |
| 170 | command("git"), |
| 171 | message("last", "assistant"), |
| 172 | ], |
| 173 | finished: ["first"], |
| 174 | }, |
| 175 | { |
| 176 | name: "a message once the next one starts", |
| 177 | entries: [ |
| 178 | prompt, |
| 179 | message("first", "assistant"), |
| 180 | message("second", "assistant", { streaming: true }), |
| 181 | ], |
| 182 | finished: ["first"], |
| 183 | }, |
| 184 | { |
| 185 | name: "no reply a steer cut off", |
| 186 | entries: [ |
| 187 | prompt, |
| 188 | inAttempt(message("cut off", "assistant"), "attempt-1", "superseded"), |
| 189 | steer, |
| 190 | inAttempt(command("git"), "attempt-2", "running"), |
| 191 | ], |
| 192 | finished: [], |
| 193 | }, |
| 194 | { |
| 195 | name: "a reply that completed before a steer replaced its attempt", |
| 196 | entries: [ |
| 197 | prompt, |
| 198 | completedAt( |
| 199 | inAttempt(message("complete", "assistant"), "attempt-1", "superseded", "00:00:10"), |
| 200 | "00:00:04", |
| 201 | ), |
| 202 | steer, |
| 203 | inAttempt(command("git"), "attempt-2", "running"), |
| 204 | ], |
| 205 | finished: ["complete"], |
| 206 | }, |
| 207 | { |
| 208 | name: "no last message that only a notice follows", |
| 209 | entries: [prompt, command("git"), message("last", "assistant"), notice], |
| 210 | finished: [], |
| 211 | }, |
| 212 | { |
| 213 | name: "no last message when another run picks the thread up", |
| 214 | entries: [ |
| 215 | prompt, |
| 216 | message("last", "assistant"), |
| 217 | command("git", { runId: RunId.make("run-2") }), |
| 218 | ], |
| 219 | finished: [], |
| 220 | }, |
| 221 | ])("counts $name as finished", ({ entries, finished }) => { |
| 222 | const { messages } = turnDetail(entries, undefined, null); |
| 223 | expect(messages.filter((spoken) => spoken.finished).map((spoken) => spoken.text)).toEqual( |
| 224 | finished, |
| 225 | ); |
| 226 | }); |
| 227 | |
| 228 | it.each<{ name: string; knownTailId: string | null; fresh: ReadonlyArray<boolean> }>([ |
| 229 | { name: "after the entry last seen", knownTailId: "seen", fresh: [false, true] }, |
| 230 | { name: "after a prompt that was last seen", knownTailId: "prompt", fresh: [true, true] }, |
| 231 | { name: "not when the entry last seen is gone", knownTailId: "gone", fresh: [false, false] }, |
| 232 | ])("marks messages as fresh $name", ({ knownTailId, fresh }) => { |
| 233 | const entries = [ |
| 234 | prompt, |
| 235 | message("loaded", "assistant"), |
| 236 | command("seen"), |
| 237 | message("new", "assistant"), |
| 238 | command("git"), |
| 239 | ]; |
| 240 | expect( |
| 241 | turnDetail(entries, undefined, knownTailId).messages.map((spoken) => spoken.fresh), |
| 242 | ).toEqual(fresh); |
| 243 | }); |
| 244 | }); |
| 245 | |
| 246 | type Frame = Pick<TurnDetailSnapshot, "status" | "activity" | "messages">; |
| 247 | const said = (id: string, finished = true, fresh = false): TurnMessage => ({ |
| 248 | id, |
| 249 | text: id, |
| 250 | finished, |
| 251 | fresh, |
| 252 | }); |
| 253 | const frame = ( |
| 254 | activity: string | null, |
| 255 | messages: ReadonlyArray<TurnMessage> = [], |
| 256 | status: TurnDetailSnapshot["status"] = "working", |
| 257 | ): Frame => ({ status, activity, messages }); |
| 258 | |
| 259 | function speak(level: ScreenReaderAnnouncements, frames: ReadonlyArray<Frame>) { |
| 260 | let memory: TurnDetailMemory | null = null; |
| 261 | const spoken: string[] = []; |
| 262 | for (const next of frames) { |
| 263 | const result = nextTurnDetailAnnouncement(memory, { ...next, level, tailId: null }); |
| 264 | memory = result.memory; |
| 265 | spoken.push(...result.messages); |
| 266 | if (result.activity !== null) spoken.push(result.activity); |
| 267 | } |
| 268 | return spoken; |
| 269 | } |
| 270 | |
| 271 | describe("nextTurnDetailAnnouncement", () => { |
| 272 | const turn = [ |
| 273 | frame(null), |
| 274 | frame("Running git", [said("first", false)]), |
| 275 | frame("Running git", [said("first", false)]), |
| 276 | frame("Reading a.ts", [said("first"), said("second", false)]), |
| 277 | frame(null, [said("first"), said("second")]), |
| 278 | ]; |
| 279 | |
| 280 | it.each<{ level: ScreenReaderAnnouncements; spoken: ReadonlyArray<string> }>([ |
| 281 | { level: "status", spoken: [] }, |
| 282 | { level: "messages", spoken: ["T3 Code: first", "T3 Code: second"] }, |
| 283 | { |
| 284 | level: "everything", |
| 285 | spoken: ["Running git", "T3 Code: first", "Reading a.ts", "T3 Code: second"], |
| 286 | }, |
| 287 | ])("says each message and step once at the $level level", ({ level, spoken }) => { |
| 288 | expect(speak(level, turn)).toEqual(spoken); |
| 289 | }); |
| 290 | |
| 291 | it("stays silent about what was there when the thread opened", () => { |
| 292 | expect( |
| 293 | speak("everything", [ |
| 294 | frame("Running git", [said("first")]), |
| 295 | frame("Running git", [said("first")]), |
| 296 | frame("Reading a.ts", [said("first"), said("second", true, true)]), |
| 297 | ]), |
| 298 | ).toEqual(["T3 Code: second", "Reading a.ts"]); |
| 299 | }); |
| 300 | |
| 301 | it("stays silent about history loaded above the messages it knows", () => { |
| 302 | expect( |
| 303 | speak("messages", [ |
| 304 | frame(null, [said("recent")]), |
| 305 | frame(null, [said("older"), said("recent")]), |
| 306 | frame(null, [said("older"), said("recent")]), |
| 307 | ]), |
| 308 | ).toEqual([]); |
| 309 | }); |
| 310 | |
| 311 | it("reads Markdown as the text it renders and skips a message with none", () => { |
| 312 | const first = { |
| 313 | ...said("first", true, true), |
| 314 | text: "**Tests** pass in `web`.\n\n- See [the docs](https://x.test/a(b)c?token=secret)<br>", |
| 315 | }; |
| 316 | expect( |
| 317 | speak("messages", [ |
| 318 | frame(null), |
| 319 | frame(null, [first, { ...said("empty", true, true), text: "<!-- note -->" }]), |
| 320 | ]), |
| 321 | ).toEqual(["T3 Code: Tests pass in web. See the docs"]); |
| 322 | }); |
| 323 | |
| 324 | it("cuts a long message without splitting an emoji", () => { |
| 325 | const long = { ...said("long", true, true), text: `${"a".repeat(1999)}😀z` }; |
| 326 | const [spoken] = speak("messages", [frame(null), frame(null, [long])]); |
| 327 | expect(spoken).not.toMatch(/[\uD800-\uDFFF]/u); |
| 328 | }); |
| 329 | |
| 330 | it("says a step again after waiting on the user", () => { |
| 331 | expect( |
| 332 | speak("everything", [ |
| 333 | frame(null), |
| 334 | frame("Running git"), |
| 335 | frame("Running git", [], "approval"), |
| 336 | frame("Running git"), |
| 337 | ]), |
| 338 | ).toEqual(["Running git", "Running git"]); |
| 339 | }); |
| 340 | |
| 341 | it("stays silent about a turn that already ended", () => { |
| 342 | expect( |
| 343 | speak("everything", [ |
| 344 | frame(null, [], "idle"), |
| 345 | frame("Running git", [said("first", true, true)], "idle"), |
| 346 | frame("Running git", [said("first")]), |
| 347 | ]), |
| 348 | ).toEqual(["Running git"]); |
| 349 | }); |
| 350 | }); |