| 1 | import { readFileSync } from "node:fs"; |
| 2 | import { describe, expect, it } from "vitest"; |
| 3 | import { captureCaption } from "./capture-caption"; |
| 4 | import { RELEASED_CAPTURE_FRAMES, RELEASED_CAPTURE_META, type ReleasedCaptureFrame } from "./released-capture.generated"; |
| 5 | |
| 6 | const released = RELEASED_CAPTURE_FRAMES as Record<string, ReleasedCaptureFrame>; |
| 7 | const dir = new URL("./terminal-captures/released/", import.meta.url); |
| 8 | const provenance = JSON.parse(readFileSync(new URL("provenance.json", dir), "utf8")) as { |
| 9 | binary: { version_line: string }; |
| 10 | model: { model: string }; |
| 11 | frames: { id: string; file: string; columns: number; rows: number }[]; |
| 12 | }; |
| 13 | const visibleText = (frame: ReleasedCaptureFrame) => frame.lines.map((runs) => runs.map(([text]) => text).join("")); |
| 14 | const stripped = (file: string) => |
| 15 | readFileSync(new URL(file, dir), "utf8") |
| 16 | .replace(/\n$/, "") |
| 17 | .replace(/\x1b\[[0-9;]*m/g, "") |
| 18 | .split("\n"); |
| 19 | |
| 20 | describe("capture caption", () => { |
| 21 | it("gives a released task sequence that shows the test run the released caption", () => { |
| 22 | const sequence = ["02-task-prompt", "03-task-diff", "04-task-approval", "05-task-result"].map((n) => released[`cols100/${n}`]); |
| 23 | expect(captureCaption(sequence)).toMatchObject({ caption: "captionReleased", reason: null }); |
| 24 | }); |
| 25 | |
| 26 | it("gives no caption under a frame that is not about the task or shows no test run", () => { |
| 27 | expect(captureCaption([released["cols100/01-model-picker"]])).toMatchObject({ caption: null }); |
| 28 | expect(captureCaption([released["cols100/00-launch"]]).caption).toBeNull(); |
| 29 | expect(captureCaption([released["cols56/05-task-result"], released["cols56/01-model-picker"]]).caption).toBeNull(); |
| 30 | const reason = captureCaption([released["cols100/03-task-diff"], released["cols100/04-task-approval"]]); |
| 31 | expect(reason.caption).toBeNull(); |
| 32 | expect(reason.reason).toContain("tests ran"); |
| 33 | expect(captureCaption([released["cols100/06-receipt.crop"]]).caption).toBeNull(); |
| 34 | }); |
| 35 | |
| 36 | it("gives no caption to no frames", () => { |
| 37 | expect(captureCaption([]).caption).toBeNull(); |
| 38 | }); |
| 39 | |
| 40 | it("flags frames that lack the model name or the Ask marker", () => { |
| 41 | const notes = captureCaption([released["cols100/01-model-picker"], released["cols100/05-task-result"], released["cols100/00-launch"]]).frames; |
| 42 | expect(notes.map((n) => n.needsPostureNote)).toEqual([true, false, false]); |
| 43 | expect(notes[0]).toMatchObject({ lacksAskPosture: true, lacksModelName: false }); |
| 44 | }); |
| 45 | }); |
| 46 | |
| 47 | describe("released capture", () => { |
| 48 | it("reads the version line from the provenance file", () => { |
| 49 | expect(RELEASED_CAPTURE_META.versionLine).toBe(provenance.binary.version_line); |
| 50 | for (const frame of Object.values(released)) { |
| 51 | expect(frame.versionLine).toBe(provenance.binary.version_line); |
| 52 | expect(frame.provenance).toBe("released"); |
| 53 | } |
| 54 | }); |
| 55 | |
| 56 | it("keeps every frame's text identical to its capture with the escape codes removed", () => { |
| 57 | expect(Object.keys(released)).toEqual(provenance.frames.map((f) => f.id)); |
| 58 | for (const entry of provenance.frames) { |
| 59 | const frame = released[entry.id]; |
| 60 | const text = visibleText(frame); |
| 61 | expect(text).toHaveLength(entry.rows); |
| 62 | text.forEach((row) => expect([...row]).toHaveLength(entry.columns)); |
| 63 | expect(text.map((row) => row.trimEnd()), entry.id).toEqual(stripped(entry.file).map((row) => row.trimEnd())); |
| 64 | } |
| 65 | }); |
| 66 | |
| 67 | it("records what each frame shows to match its own text", () => { |
| 68 | for (const frame of Object.values(released)) { |
| 69 | const text = visibleText(frame).join("\n"); |
| 70 | expect(frame.shows.askPosture, frame.id).toBe(text.includes("● accept edits")); |
| 71 | expect(frame.shows.modelName, frame.id).toBe(text.includes(provenance.model.model)); |
| 72 | expect(frame.shows.testRun, frame.id).toBe(/Ran \d+ tests? in [0-9.]+s/.test(text) && /\bOK\b/.test(text)); |
| 73 | } |
| 74 | }); |
| 75 | }); |
| 76 |