Files
community-rule/tests/unit/lib/ruleExport.test.ts
T
adilallo d9c475a967 Give finished Rules a print path, folded launch counts, and a two-day follow-up.
Export is PDF, RULE.md, and Print on US Letter, with every shared term, no member names, and a takeaway that points at Ask an organizer. Finished Rules keep type and place so the team can read folded counts without a database, Umami can sit beside the app with no cookies, and the person who holds the pen gets working PDF and RULE.md links 48 hours later.
2026-11-10 18:32:33 -07:00

147 lines
4.8 KiB
TypeScript

import { describe, it, expect } from "vitest";
import {
buildPrintableRuleHtmlDocument,
buildStoredRulePdfBlob,
exportFilenameBase,
exportStoredRuleAsMarkdown,
} from "../../../lib/create/ruleExport";
import {
RULE_MD_FILENAME,
exportPdfFilename,
} from "../../../lib/create/ruleExportModel";
import exportCopy from "../../../messages/en/create/export.json";
import sharedTerms from "../../../messages/en/create/sharedTerms.json";
async function readBlobAsArrayBuffer(blob: Blob): Promise<ArrayBuffer> {
if (typeof blob.arrayBuffer === "function") {
return blob.arrayBuffer();
}
return new Promise<ArrayBuffer>((resolve, reject) => {
const r = new FileReader();
r.onload = (): void => resolve(r.result as ArrayBuffer);
r.onerror = (): void => reject(new Error("FileReader failed"));
r.readAsArrayBuffer(blob);
});
}
const origin = "https://communityrule.info";
const source = {
id: "id-1",
title: "Garden Norms",
summary: "We share tools.",
document: {
sections: [
{
categoryName: "Values",
entries: [{ title: "Solidarity", body: "Be kind.\n\nShare tools." }],
},
],
stakeholders: [{ name: "Maya Chen", email: "maya@example.com" }],
},
finishedAt: "2026-11-08T12:00:00.000Z",
};
describe("ruleExport", () => {
it("exportFilenameBase slugifies title", () => {
expect(
exportFilenameBase({
id: "id-1",
title: "Mutual Aid Mondays!",
document: {},
}),
).toBe("mutual-aid-mondays");
});
it("exportFilenameBase falls back to id fragment", () => {
expect(
exportFilenameBase({
id: "full-uuid-here",
title: " ",
document: {},
}),
).toBe("rule-full-uui");
});
it("PDF filename is {Group} Rule.pdf", () => {
expect(exportPdfFilename("Garden Norms")).toBe("Garden Norms Rule.pdf");
expect(RULE_MD_FILENAME).toBe("RULE.md");
});
it("RULE.md has front matter, shared terms, takeaway, and no member names", () => {
const md = exportStoredRuleAsMarkdown(source, origin);
expect(md).toContain("title: Garden Norms");
expect(md).toContain("summary: We share tools.");
expect(md).toContain("finished: 2026-11-08");
expect(md).toContain("# Garden Norms");
expect(md).toContain("## Values");
expect(md).toContain("### Solidarity");
expect(md).toContain("## Shared terms");
expect(md).toContain("**active members**");
expect(md).toContain(sharedTerms.terms[0]?.meaning ?? "good standing");
expect(md).toContain(exportCopy.takeaway);
expect(md).toContain("https://communityrule.info/#ask-organizer");
expect(md).not.toContain("Maya Chen");
expect(md).not.toContain("maya@example.com");
expect(md).not.toContain("whenNoCardCoversIt");
expect(md).not.toContain("setBy");
});
it("print HTML is US Letter, credits CommunityRule, and omits names", () => {
const html = buildPrintableRuleHtmlDocument(source, origin);
expect(html).toContain("@page { size: letter;");
expect(html).toContain("Garden Norms");
expect(html).toContain(exportCopy.credit);
expect(html).toContain(exportCopy.takeaway);
expect(html).toContain("https://communityrule.info/#ask-organizer");
expect(html).toContain("Shared terms");
expect(html).not.toContain("Maya Chen");
expect(html).not.toContain("maya@example.com");
expect(html).not.toContain("<script>");
});
it("buildPrintableRuleHtmlDocument escapes HTML in user content", () => {
const html = buildPrintableRuleHtmlDocument(
{
id: "id-1",
title: "Title <t>",
summary: null,
document: {
sections: [
{
categoryName: "Values <x>",
entries: [{ title: "Entry", body: "<script>bad()</script>" }],
},
],
},
},
origin,
);
expect(html).toContain("&lt;script&gt;");
expect(html).not.toContain("<script>bad()");
expect(html).toContain("Values &lt;x&gt;");
});
it("buildStoredRulePdfBlob produces application/pdf with takeaway and no names", async () => {
const blob = buildStoredRulePdfBlob(source, origin);
expect(blob.type).toBe("application/pdf");
expect(blob.size).toBeGreaterThan(500);
const buf = new Uint8Array(await readBlobAsArrayBuffer(blob));
expect(String.fromCharCode(...buf.subarray(0, 5))).toBe("%PDF-");
const raw = new TextDecoder("latin1").decode(buf);
expect(raw).toContain("Garden Norms");
expect(raw).toContain("Tell us what this Rule changed");
expect(raw).not.toContain("Maya Chen");
expect(raw).not.toContain("maya@example.com");
});
it("exports an empty document without throwing", () => {
const md = exportStoredRuleAsMarkdown(
{ id: "id-1", title: "T", document: {} },
origin,
);
expect(md).toContain("# T");
expect(md).toContain("## Shared terms");
});
});