import { createElement } from "react"; import { renderToStaticMarkup } from "react-dom/server"; import { RichText } from "@payloadcms/richtext-lexical/react"; import { describe, expect, it } from "vitest"; import { briefingConverters } from "@/lib/intelligence/briefingConverters"; import { briefingEntryHasContent, briefingEntryToStructuredText, briefingToDiarySqf, briefingToStructuredTextSqf, type BriefingSqfEntry, } from "@/lib/intelligence/briefingSqf"; function text(value: string, format = 0, extra: Record = {}) { return { type: "text", version: 1, text: value, format, ...extra }; } function paragraph(children: unknown[], extra: Record = {}) { return { type: "paragraph", version: 1, children, ...extra }; } function entry(sectionName: string, children: unknown[]): BriefingSqfEntry { return { sectionName, details: { root: { type: "root", version: 1, children }, }, }; } describe("briefing Structured Text conversion", () => { it("maps bold, underline, code, and color formats", () => { const result = briefingEntryToStructuredText( entry("Situation", [ paragraph([ text("bold", 1), text(" "), text("under", 8), text(" "), text("code", 16), text(" "), text("red", 0, { color: "red" }), text(" "), text("both", 1, { color: "blue" }), ]), ]), ); expect(result).toContain("bold"); expect(result).toContain("under"); expect(result).toContain("code"); expect(result).toContain("red"); expect(result).toContain("both"); }); it("keeps text Arma cannot style as plain text", () => { const result = briefingEntryToStructuredText( entry("Situation", [paragraph([text("italic", 2), text(" "), text("crossed", 4)])]), ); expect(result).toBe("italic crossed"); }); it("resolves legacy CSS style colors and ignores unknowns", () => { const result = briefingEntryToStructuredText( entry("Situation", [ paragraph([ text("style", 0, { style: "color: #ff0000;" }), text(" "), text("browser", 0, { style: "color: rgb(255, 159, 67)" }), text(" "), text("state", 0, { $: { color: "red" } }), text(" "), text("unknown", 0, { color: "chartreuse" }), ]), ]), ); expect(result).toBe( "style browser state unknown", ); }); it("escapes Structured Text metacharacters", () => { const result = briefingEntryToStructuredText( entry("Situation", [paragraph([text(" & Bravo")])]), ); expect(result).toBe("<Alpha> & Bravo"); }); it("renders headings with size and alignment", () => { const heading = { type: "heading", version: 1, tag: "h2", children: [text("Orders")], }; const centered = paragraph([text("Centered")], { format: "center" }); const result = briefingEntryToStructuredText(entry("Mission", [heading, centered])); expect(result).toContain("Orders"); expect(result).toContain("Centered"); }); it("renders bullet, numbered, checklist, and nested lists", () => { const list = { type: "list", version: 1, listType: "number", tag: "ol", start: 3, children: [ { type: "listitem", version: 1, value: 1, children: [ text("First"), { type: "list", version: 1, listType: "bullet", tag: "ul", children: [{ type: "listitem", version: 1, value: 1, children: [text("Nested")] }], }, ], }, { type: "listitem", version: 1, value: 2, children: [text("Second")] }, ], }; const checklist = { type: "list", version: 1, listType: "check", tag: "ul", children: [ { type: "listitem", version: 1, checked: true, children: [text("Done")] }, { type: "listitem", version: 1, checked: false, children: [text("Todo")] }, ], }; const result = briefingEntryToStructuredText(entry("Execution", [list, checklist])); expect(result).toContain("3. First
\u00a0\u00a0\u00a0\u00a0\u2022 Nested"); expect(result).toContain("4. Second"); expect(result).toContain("[x] Done
[ ] Todo"); }); it("renders quotes, rules, links, and images as readable placeholders", () => { const quote = { type: "quote", version: 1, children: [text("Hold the line")], }; const rule = { type: "horizontalrule", version: 1 }; const link = { type: "link", version: 1, fields: { url: "https://example.test/opord" }, children: [text("OPORD")], }; const image = { type: "upload", version: 1, relationTo: "media", value: { filename: "map.jpg", url: "/media/map.jpg" }, }; const result = briefingEntryToStructuredText(entry("Signals", [quote, rule, link, image])); expect(result).toContain("| Hold the line"); expect(result).toContain("-----"); expect(result).toContain("OPORD (https://example.test/opord)"); expect(result).toContain("[Image: map.jpg]"); }); it("detects entries with no body text", () => { expect(briefingEntryHasContent(entry("Empty", [paragraph([])]))).toBe(false); expect(briefingEntryHasContent(entry("Full", [paragraph([text("Hi")])]))).toBe(true); expect(briefingEntryHasContent({ sectionName: "Bad", details: null })).toBe(false); }); it("renders colors on the mission page", () => { const data = { root: { type: "root", version: 1, direction: null, format: "", indent: 0, children: [ { type: "paragraph", version: 1, direction: null, format: "", indent: 0, textFormat: 0, children: [ text("Red alert", 1, { $: { color: "red" } }), text(" and "), text("styled", 0, { style: "color: rgb(96, 165, 250)" }), ], }, ], }, }; const html = renderToStaticMarkup( createElement(RichText, { data: data as never, converters: briefingConverters }), ); expect(html).toMatch(/Red alert<\/strong><\/span>/); expect(html).toMatch(/styled<\/span>/); }); }); describe("briefing SQF export", () => { const situation = entry("Situation", [paragraph([text("Enemy to the north.")])]); const mission = entry("Mission", [paragraph([text("Take the ridge.")])]); it("creates diary records bottom-up so they read in order", () => { const sqf = briefingToDiarySqf([situation, mission], { missionName: "The Pale" }); expect(sqf).toContain('player createDiaryRecord ["Diary", ["Situation",'); expect(sqf).toContain('player createDiaryRecord ["Diary", ["Mission",'); expect(sqf.indexOf('["Mission"')).toBeLessThan(sqf.indexOf('["Situation"')); expect(sqf).toContain('briefing for "The Pale"'); }); it("doubles quotes inside SQF strings", () => { const quoted = entry('Signals "Main"', [paragraph([text('Say "hello".')])]); const sqf = briefingToDiarySqf([quoted]); expect(sqf).toContain('["Signals ""Main"""'); expect(sqf).toContain('""hello""'); }); it("exports an array of Structured Text bodies with titles", () => { const sqf = briefingToStructuredTextSqf([situation, mission]); expect(sqf).toContain("private _briefing = ["); expect(sqf).toContain("Situation"); expect(sqf).toContain("Mission"); expect(sqf).toMatch(/take the ridge\."\n\]/i); expect(sqf).toContain("Take the ridge."); }); });