import { describe, expect, it } from "vitest"; import { layerOf, splitConcernLayers } from "@govlab/docs/core/parsers/layer.parser.ts"; const DOC = [ "# @govlab/thing", "", "It does a thing.", "", "", "- `doThing()`", "- `undoThing()`", "", ].join("\n"); describe("splitConcernLayers", () => { it("returns each layer in order with its body", () => { const { layers, defects } = splitConcernLayers(DOC); expect(defects).toStrictEqual([]); expect(layers.map((layer) => layer.id)).toStrictEqual(["overview", "api"]); expect(layers[1]?.body).toBe("- `doThing()`\n- `undoThing()`"); }); it("reports an unclosed marker, a duplicate id and an orphan close", () => { expect(splitConcernLayers("\nbody, never closed").defects).toStrictEqual([ { code: "unclosed-marker", id: "x" }, ]); const duplicate = "\n1\n\n\n2\n"; expect(splitConcernLayers(duplicate).defects).toStrictEqual([{ code: "duplicate-marker", id: "a" }]); expect(splitConcernLayers("text\n").defects).toStrictEqual([ { code: "orphan-close", id: "ghost" }, ]); }); it("handles indented markers and content outside every layer", () => { const { layers, defects } = splitConcernLayers( "before\n \n inner\n \nafter", ); expect(defects).toStrictEqual([]); expect(layers.map((layer) => layer.body)).toStrictEqual([" inner"]); }); }); describe("layerOf", () => { it("extracts one layer body, and null when the layer is absent", () => { expect(layerOf(DOC, "api")).toBe("- `doThing()`\n- `undoThing()`"); expect(layerOf(DOC, "ai-context")).toBeNull(); }); });