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.
This commit is contained in:
adilallo committed 2026-11-10 18:32:33 -07:00
1 parent 0efc907633
commit d9c475a967
74 files changed
+2401 -630

No files matched your search

+83 -167
View File
@@ -1,14 +1,16 @@
import { describe, it, expect } from "vitest";
import {
buildPrintableRuleHtmlDocument,
buildPublicRuleUrl,
buildStoredRulePdfBlob,
exportFilenameBase,
sectionsToCsv,
sectionsToMarkdown,
sectionsToPdfBlob,
exportStoredRuleAsMarkdown,
} from "../../../lib/create/ruleExport";
import type { CommunityRuleSection } from "../../../app/components/type/CommunityRule/CommunityRule.types";
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") {
@@ -22,16 +24,25 @@ async function readBlobAsArrayBuffer(blob: Blob): Promise<ArrayBuffer> {
});
}
describe("ruleExport", () => {
it("buildPublicRuleUrl encodes id and trims origin slash", () => {
expect(buildPublicRuleUrl("https://example.com/", "abc/xyz")).toBe(
"https://example.com/rules/abc%2Fxyz",
);
expect(buildPublicRuleUrl("https://example.com", "r1")).toBe(
"https://example.com/rules/r1",
);
});
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({
@@ -52,179 +63,84 @@ describe("ruleExport", () => {
).toBe("rule-full-uui");
});
it("sectionsToMarkdown renders title, summary, and sections", () => {
const sections: CommunityRuleSection[] = [
{
categoryName: "Values",
entries: [
{
title: "Solidarity",
body: "First paragraph.\n\nSecond paragraph.",
},
],
},
];
const md = sectionsToMarkdown(
"My Rule",
"Short summary.",
sections,
);
expect(md).toContain("# My Rule");
expect(md).toContain("Short summary.");
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("First paragraph.");
expect(md).toContain("Second paragraph.");
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("sectionsToCsv includes header row, title metadata, sections, and quotes commas", () => {
const sections: CommunityRuleSection[] = [
{
categoryName: "Values",
entries: [
{
title: "Solidarity",
body: "One, two",
},
],
},
];
const csv = sectionsToCsv("My Rule", "Sum, mary", sections);
expect(csv).toContain("Section,Entry,Block label,Content");
expect(csv).toContain('"Sum, mary"');
expect(csv).toContain('"One, two"');
expect(csv).toContain(",Title,,My Rule");
});
it("sectionsToMarkdown, sectionsToCsv, and printable HTML include imageUrl and fileUrl blocks", () => {
const sections: CommunityRuleSection[] = [
{
categoryName: "Values",
entries: [
{
title: "Entry",
body: "",
blocks: [
{
label: "Photo",
body: "Caption",
imageUrl: "https://cdn.example.com/pic.jpg",
},
{
label: "Handbook",
body: "Download",
fileUrl: "https://cdn.example.com/guidance.pdf",
},
],
},
],
},
];
const md = sectionsToMarkdown("Rule", null, sections);
expect(md).toContain("![Caption](https://cdn.example.com/pic.jpg)");
expect(md).toContain("[Download](https://cdn.example.com/guidance.pdf)");
const csv = sectionsToCsv("Rule", null, sections);
expect(csv).toContain("Caption\nhttps://cdn.example.com/pic.jpg");
expect(csv).toContain("Download\nhttps://cdn.example.com/guidance.pdf");
const html = buildPrintableRuleHtmlDocument("Rule", null, sections);
expect(html).toContain('src="https://cdn.example.com/pic.jpg"');
expect(html).toContain("Caption");
expect(html).toContain('href="https://cdn.example.com/guidance.pdf"');
expect(html).toContain("Download");
});
it("sectionsToPdfBlob embeds image and file URLs as text for labeled blocks", async () => {
const sections: CommunityRuleSection[] = [
{
categoryName: "Values",
entries: [
{
title: "Entry",
body: "",
blocks: [
{
label: "Photo",
body: "Caption",
imageUrl: "https://cdn.example.com/pic.jpg",
},
{
label: "Handbook",
body: "Download",
fileUrl: "https://cdn.example.com/guidance.pdf",
},
],
},
],
},
];
const blob = sectionsToPdfBlob("Rule", null, sections);
const buf = new Uint8Array(await readBlobAsArrayBuffer(blob));
const raw = new TextDecoder("latin1").decode(buf);
expect(raw).toContain("https://cdn.example.com/pic.jpg");
expect(raw).toContain("https://cdn.example.com/guidance.pdf");
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 sections: CommunityRuleSection[] = [
{
categoryName: 'Values <x>',
entries: [{ title: "Entry", body: "<script>bad()</script>" }],
},
];
const html = buildPrintableRuleHtmlDocument(
'Title <t>',
null,
sections,
{
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 PDF magic bytes", async () => {
const blob = buildStoredRulePdfBlob({
id: "id-1",
title: "Garden Norms",
summary: "Summary here.",
document: {
sections: [
{
categoryName: "Values",
entries: [{ title: "Solidarity", body: "Be kind.\n\nShare tools." }],
},
],
},
});
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("buildStoredRulePdfBlob throws exportEmptyDocument when sections empty", () => {
expect(() =>
buildStoredRulePdfBlob({
id: "id-1",
title: "T",
document: {},
}),
).toThrowError("exportEmptyDocument");
});
it("export pdf attachment filename matches csv/md convention", () => {
const rule = {
id: "id-1",
title: "Garden norms",
document: {
sections: [
{ categoryName: "X", entries: [{ title: "t", body: "b" }] },
],
},
};
expect(`${exportFilenameBase(rule)}-community-rule.pdf`).toBe(
"garden-norms-community-rule.pdf",
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");
});
});