|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { parseCaml } from "@os-legal/caml"; |
| 3 | +import { normalizeCamlSource, parseCamlArticle } from "../normalizeCamlSource"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Helper: list block types per chapter, so assertions read as a structural |
| 7 | + * snapshot rather than poking at deep object shapes. |
| 8 | + */ |
| 9 | +function blockTypesByChapter(source: string): string[][] { |
| 10 | + return parseCaml(source).chapters.map((c) => c.blocks.map((b) => b.type)); |
| 11 | +} |
| 12 | + |
| 13 | +const FRONTMATTER = [ |
| 14 | + "---", |
| 15 | + 'version: "1.0"', |
| 16 | + "hero:", |
| 17 | + ' title: ["Fort Worth"]', |
| 18 | + "---", |
| 19 | + "", |
| 20 | +].join("\n"); |
| 21 | + |
| 22 | +describe("normalizeCamlSource", () => { |
| 23 | + it("returns source unchanged when there is no depth-4 fence (fast path)", () => { |
| 24 | + const src = |
| 25 | + FRONTMATTER + |
| 26 | + [ |
| 27 | + "::: chapter {#intro}", |
| 28 | + "## Intro", |
| 29 | + "Just prose, no blocks.", |
| 30 | + ":::", |
| 31 | + "", |
| 32 | + ].join("\n"); |
| 33 | + expect(normalizeCamlSource(src)).toBe(src); |
| 34 | + }); |
| 35 | + |
| 36 | + it("leaves correctly-nested blocks untouched", () => { |
| 37 | + const src = |
| 38 | + FRONTMATTER + |
| 39 | + [ |
| 40 | + "::: chapter {theme: dark}", |
| 41 | + "## Major Projects", |
| 42 | + "", |
| 43 | + ":::: cards {columns: 2}", |
| 44 | + "- **Infrastructure** | Major | #0f766e", |
| 45 | + " Body text.", |
| 46 | + "::::", |
| 47 | + "", |
| 48 | + ":::", |
| 49 | + "", |
| 50 | + ].join("\n"); |
| 51 | + expect(normalizeCamlSource(src)).toBe(src); |
| 52 | + }); |
| 53 | + |
| 54 | + it("does not touch YAML frontmatter even when the body needs wrapping", () => { |
| 55 | + const src = |
| 56 | + FRONTMATTER + |
| 57 | + [":::: corpus-stats", "- documents | Documents", "::::", ""].join("\n"); |
| 58 | + const normalized = normalizeCamlSource(src); |
| 59 | + expect(normalized.startsWith(FRONTMATTER.trimEnd())).toBe(true); |
| 60 | + expect(parseCaml(normalized).frontmatter.hero).toBeTruthy(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("is idempotent", () => { |
| 64 | + const src = |
| 65 | + FRONTMATTER + |
| 66 | + [ |
| 67 | + ":::: corpus-stats", |
| 68 | + "- documents | Documents", |
| 69 | + "- annotations | Annotations", |
| 70 | + "::::", |
| 71 | + "", |
| 72 | + ].join("\n"); |
| 73 | + const once = normalizeCamlSource(src); |
| 74 | + const twice = normalizeCamlSource(once); |
| 75 | + expect(twice).toBe(once); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +describe("parseCamlArticle — repairs mis-nested blocks the raw parser leaks", () => { |
| 80 | + it("recovers a top-level corpus-stats block (the canonical leak)", () => { |
| 81 | + const src = |
| 82 | + FRONTMATTER + |
| 83 | + [ |
| 84 | + ":::: corpus-stats", |
| 85 | + "- documents | Documents", |
| 86 | + "- annotations | Annotations", |
| 87 | + "::::", |
| 88 | + "", |
| 89 | + ].join("\n"); |
| 90 | + |
| 91 | + // Raw parser leaks the block body (incl. literal ::::) as prose. |
| 92 | + const rawBlocks = blockTypesByChapter(src); |
| 93 | + expect(rawBlocks).toEqual([["prose"]]); |
| 94 | + const leakedProse = parseCaml(src).chapters[0].blocks[0]; |
| 95 | + expect(leakedProse.type).toBe("prose"); |
| 96 | + expect((leakedProse as { content: string }).content).toContain("::::"); |
| 97 | + |
| 98 | + // parseCamlArticle wraps it so the block is recognised. |
| 99 | + const doc = parseCamlArticle(src); |
| 100 | + const block = doc.chapters[0].blocks[0]; |
| 101 | + expect(block.type).toBe("corpus-stats"); |
| 102 | + expect((block as { items: unknown[] }).items).toHaveLength(2); |
| 103 | + }); |
| 104 | + |
| 105 | + it("recovers the screenshot scenario: dark cards chapter then a stray corpus-stats", () => { |
| 106 | + const src = |
| 107 | + FRONTMATTER + |
| 108 | + [ |
| 109 | + "::: chapter {theme: dark}", |
| 110 | + "## Major Projects", |
| 111 | + "", |
| 112 | + ":::: cards {columns: 2}", |
| 113 | + "- **Infrastructure** | Major | #0f766e", |
| 114 | + " Body.", |
| 115 | + "::::", |
| 116 | + "", |
| 117 | + ":::", |
| 118 | + "", |
| 119 | + "::: chapter {#documentation}", |
| 120 | + "## Documentation", |
| 121 | + "Closing prose.", |
| 122 | + ":::", |
| 123 | + "", |
| 124 | + ":::: corpus-stats", |
| 125 | + "- documents | Documents", |
| 126 | + "- annotations | Annotations", |
| 127 | + "::::", |
| 128 | + "", |
| 129 | + ].join("\n"); |
| 130 | + |
| 131 | + const doc = parseCamlArticle(src); |
| 132 | + const types = doc.chapters.map((c) => c.blocks.map((b) => b.type)); |
| 133 | + expect(types).toEqual([["cards"], ["prose"], ["corpus-stats"]]); |
| 134 | + }); |
| 135 | + |
| 136 | + it("adopts a run of stray blocks with prose between them into one chapter", () => { |
| 137 | + const src = |
| 138 | + FRONTMATTER + |
| 139 | + [ |
| 140 | + ":::: pills", |
| 141 | + "- 247 | **Docs** | Q4", |
| 142 | + "::::", |
| 143 | + "", |
| 144 | + "Mid prose between blocks.", |
| 145 | + "", |
| 146 | + ":::: cta", |
| 147 | + "- [View](#x) {primary}", |
| 148 | + "::::", |
| 149 | + "", |
| 150 | + ].join("\n"); |
| 151 | + |
| 152 | + const doc = parseCamlArticle(src); |
| 153 | + expect(doc.chapters).toHaveLength(1); |
| 154 | + expect(doc.chapters[0].blocks.map((b) => b.type)).toEqual([ |
| 155 | + "pills", |
| 156 | + "prose", |
| 157 | + "cta", |
| 158 | + ]); |
| 159 | + }); |
| 160 | + |
| 161 | + it("recovers a stray tabs block that itself nests depth-5 fences", () => { |
| 162 | + const src = |
| 163 | + FRONTMATTER + |
| 164 | + [ |
| 165 | + ":::: tabs", |
| 166 | + '::::: tab {label: "US", color: #0f766e}', |
| 167 | + "#### United States", |
| 168 | + "Federal regulations analyzed.", |
| 169 | + ":::::", |
| 170 | + "::::", |
| 171 | + "", |
| 172 | + ].join("\n"); |
| 173 | + |
| 174 | + const doc = parseCamlArticle(src); |
| 175 | + expect(doc.chapters[0].blocks.map((b) => b.type)).toEqual(["tabs"]); |
| 176 | + }); |
| 177 | +}); |
0 commit comments