The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | applyTerminalAttachStreamEvent, |
| 3 | DEFAULT_MAX_TERMINAL_BUFFER_BYTES, |
| 4 | EMPTY_TERMINAL_BUFFER_STATE, |
| 5 | terminalOutputText, |
| 6 | } from "@t3tools/client-runtime/state/terminal"; |
| 7 | import { expect, it, vi } from "vite-plus/test"; |
| 8 | |
| 9 | import { createTerminalOutputWriter } from "./terminalOutputWriter"; |
| 10 | |
| 11 | function append( |
| 12 | state: typeof EMPTY_TERMINAL_BUFFER_STATE, |
| 13 | data: string, |
| 14 | limit = DEFAULT_MAX_TERMINAL_BUFFER_BYTES, |
| 15 | ) { |
| 16 | return applyTerminalAttachStreamEvent( |
| 17 | state, |
| 18 | { type: "output", data, threadId: "thread", terminalId: "term-1" }, |
| 19 | limit, |
| 20 | ); |
| 21 | } |
| 22 | |
| 23 | it("appends across rollover without replay and seeds fresh writers with only bounded history", async () => { |
| 24 | const write = vi.fn(async (_data: string, _reset: boolean) => {}); |
| 25 | const writer = createTerminalOutputWriter(write); |
| 26 | let state = EMPTY_TERMINAL_BUFFER_STATE; |
| 27 | const chunks: string[] = []; |
| 28 | await writer.write(state.output); |
| 29 | for (let index = 0; index < 128; index += 1) { |
| 30 | const chunk = `${index}\r\n`.padEnd(16 * 1024, "x"); |
| 31 | chunks.push(chunk); |
| 32 | state = append(state, chunk); |
| 33 | await writer.write(state.output); |
| 34 | expect(write).toHaveBeenLastCalledWith(chunk, false); |
| 35 | } |
| 36 | expect(state.output.retainedBytes).toBe(DEFAULT_MAX_TERMINAL_BUFFER_BYTES); |
| 37 | const retained = chunks.slice(-32).join(""); |
| 38 | expect(terminalOutputText(state.output)).toBe(retained); |
| 39 | expect(write.mock.calls.filter(([, reset]) => reset)).toEqual([["", true]]); |
| 40 | expect(write.mock.calls.reduce((total, [data]) => total + data.length, 0)).toBe(2 * 1024 * 1024); |
| 41 | |
| 42 | const remountedWrite = vi.fn(async () => {}); |
| 43 | await createTerminalOutputWriter(remountedWrite).write(state.output); |
| 44 | expect(remountedWrite.mock.calls).toEqual([[retained, true]]); |
| 45 | }); |
| 46 | |
| 47 | it("recovers the newer snapshot queued during a rejected append without a third write", async () => { |
| 48 | const started = Promise.withResolvers<void>(); |
| 49 | const release = Promise.withResolvers<void>(); |
| 50 | const write = vi.fn(async () => {}); |
| 51 | const writer = createTerminalOutputWriter(write); |
| 52 | const initial = append(EMPTY_TERMINAL_BUFFER_STATE, "initial"); |
| 53 | await writer.write(initial.output); |
| 54 | write.mockImplementationOnce(async () => { |
| 55 | started.resolve(); |
| 56 | await release.promise; |
| 57 | throw new Error("partially applied append"); |
| 58 | }); |
| 59 | const first = append(initial, "-first"); |
| 60 | const draining = writer.write(first.output); |
| 61 | await started.promise; |
| 62 | const latest = append(first, "-latest"); |
| 63 | void writer.write(latest.output); |
| 64 | release.resolve(); |
| 65 | await draining; |
| 66 | expect(write.mock.calls).toEqual([ |
| 67 | ["initial", true], |
| 68 | ["-first", false], |
| 69 | ["initial-first-latest", true], |
| 70 | ]); |
| 71 | await writer.write(append(latest, "-next").output); |
| 72 | expect(write).toHaveBeenLastCalledWith("-next", false); |
| 73 | }); |