File size: 1,491 Bytes
fc93158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { describe, expect, it } from "vitest";
import { parseViewerPayloadJson } from "./viewer-payload.js";

function buildValidPayload(): Record<string, unknown> {
  return {
    prerenderedHTML: "<div>ok</div>",
    langs: ["text"],
    oldFile: {
      name: "README.md",
      contents: "before",
    },
    newFile: {
      name: "README.md",
      contents: "after",
    },
    options: {
      theme: {
        light: "pierre-light",
        dark: "pierre-dark",
      },
      diffStyle: "unified",
      diffIndicators: "bars",
      disableLineNumbers: false,
      expandUnchanged: false,
      themeType: "dark",
      backgroundEnabled: true,
      overflow: "wrap",
      unsafeCSS: ":host{}",
    },
  };
}

describe("parseViewerPayloadJson", () => {
  it("accepts valid payload JSON", () => {
    const parsed = parseViewerPayloadJson(JSON.stringify(buildValidPayload()));
    expect(parsed.options.diffStyle).toBe("unified");
    expect(parsed.options.diffIndicators).toBe("bars");
  });

  it("rejects payloads with invalid shape", () => {
    const broken = buildValidPayload();
    broken.options = {
      ...(broken.options as Record<string, unknown>),
      diffIndicators: "invalid",
    };

    expect(() => parseViewerPayloadJson(JSON.stringify(broken))).toThrow(
      "Diff payload has invalid shape.",
    );
  });

  it("rejects invalid JSON", () => {
    expect(() => parseViewerPayloadJson("{not-json")).toThrow("Diff payload is not valid JSON.");
  });
});