1
0
Fork 0
polaris-task-force/tests/int/briefing-sqf.int.spec.ts
Z8MB1E 34c0d5681f fix(rich-text): render full lexical formatting incl. colours across all fields
RichText authored in the admin panel was rendered as plain text on the
public site: headings/lists/quotes were flattened by Tailwind preflight
(no @tailwindcss/typography installed), and Payload's default JSX
converter ignored text colours.

- src/lib/lexical: shared editor factory (ptfLexicalEditor) + shared
  colour palette (textState) + colour converter (richTextConverters)
- RichTextView: canonical .lexical-content renderer (colour-aware),
  swapped into every frontend render site
- All rich-text editors route through the factory, so every field
  inherits the coloured editor (incl. the previously-unedited ones)
- .lexical-content CSS extended with checklist/table/heading rules,
  wrapped in @layer components so per-page utilities still override;
  dropped the now-dead per-page prose hacks
- Remove the now-dead briefingConverters in favour of the shared
  converter; add tests/int/rich-text-view.int.spec.ts
2026-11-18 13:10:26 -05:00

250 lines
8.1 KiB
TypeScript

import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { RichText } from "@payloadcms/richtext-lexical/react";
import { describe, expect, it } from "vitest";
import { richTextConverters } from "@/lib/lexical/richTextConverters";
import {
briefingEntryHasContent,
briefingEntryToStructuredText,
briefingToDiarySqf,
briefingToStructuredTextSqf,
type BriefingSqfEntry,
} from "@/lib/intelligence/briefingSqf";
function text(value: string, format = 0, extra: Record<string, unknown> = {}) {
return { type: "text", version: 1, text: value, format, ...extra };
}
function paragraph(children: unknown[], extra: Record<string, unknown> = {}) {
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("<t font='PuristaBold'>bold</t>");
expect(result).toContain("<t underline='1'>under</t>");
expect(result).toContain("<t font='EtelkaMonospacePro'>code</t>");
expect(result).toContain("<t color='#ff5c5c'>red</t>");
expect(result).toContain("<t font='PuristaBold' color='#60a5fa'>both</t>");
});
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(
"<t color='#ff0000'>style</t> <t color='#ff9f43'>browser</t> <t color='#ff5c5c'>state</t> unknown",
);
});
it("escapes Structured Text metacharacters", () => {
const result = briefingEntryToStructuredText(
entry("Situation", [paragraph([text("<Alpha> & Bravo")])]),
);
expect(result).toBe("&lt;Alpha&gt; &amp; 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("<t font='PuristaBold' size='1.35'>Orders</t>");
expect(result).toContain("<t align='center'>Centered</t>");
});
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<br/>\u00a0\u00a0\u00a0\u00a0\u2022 Nested");
expect(result).toContain("4. Second");
expect(result).toContain("[x] Done<br/>[ ] 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("<t color='#9ca3af'>| Hold the line</t>");
expect(result).toContain("<t color='#6b7280'>-----</t>");
expect(result).toContain("OPORD <t color='#9ca3af'>(https://example.test/opord)</t>");
expect(result).toContain("<t color='#9ca3af'>[Image: map.jpg]</t>");
});
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: richTextConverters }),
);
expect(html).toMatch(/<span style="color:\s*#ff5c5c"><strong>Red alert<\/strong><\/span>/);
expect(html).toMatch(/<span style="color:\s*#60a5fa">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("<t font='PuristaBold' size='1.2'>Situation</t>");
expect(sqf).toContain("<t font='PuristaBold' size='1.2'>Mission</t>");
expect(sqf).toMatch(/take the ridge\."\n\]/i);
expect(sqf).toContain("Take the ridge.");
});
});