pdf-import.test.ts

1.8 KB · raw

1/**2 * pdf-import.test.ts — coverage for the parser surface only.3 *4 * The actual pdf.js rendering + Aztec scan loop in importCodexPDF requires5 * a DOM (HTMLCanvasElement) which vitest's node environment does not provide.6 * End-to-end PDF round-trip lives in the manual Codex verification matrix7 * (see peach_codex_reliability.md §5.2). Here we only test the bits we can:8 * the defensive envelope parser.9 */1011import { describe, expect, it } from "vitest";12import { parseChunkEnvelope } from "./pdf-import";1314describe("parseChunkEnvelope", () => {15  it("parses a well-formed envelope", () => {16    const env = {17      v: 2,18      c: "PX-TEST-1234",19      i: 0,20      t: 15,21      p: false,22      d: "AAAA",23      h: "ABCDEF1234567890ABCDEF1234567890",24    };25    expect(parseChunkEnvelope(JSON.stringify(env))).toEqual(env);26  });2728  it("returns null for non-JSON input", () => {29    expect(parseChunkEnvelope("not json at all")).toBeNull();30  });3132  it("returns null for non-Peach JSON", () => {33    expect(parseChunkEnvelope(JSON.stringify({ hello: "world" }))).toBeNull();34  });3536  it("returns null when version is missing", () => {37    expect(38      parseChunkEnvelope(39        JSON.stringify({ c: "PX", i: 0, t: 5, p: false, d: "A", h: "A" }),40      ),41    ).toBeNull();42  });4344  it("returns null when fields have the wrong types", () => {45    expect(46      parseChunkEnvelope(47        JSON.stringify({48          v: 2,49          c: "PX",50          i: "0", // should be number51          t: 5,52          p: false,53          d: "A",54          h: "A",55        }),56      ),57    ).toBeNull();58  });5960  it("returns null for v=1 envelopes", () => {61    expect(62      parseChunkEnvelope(63        JSON.stringify({64          v: 1,65          c: "PX",66          i: 0,67          t: 5,68          p: false,69          d: "A",70          h: "A",71        }),72      ),73    ).toBeNull();74  });75});