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:
1 parent
0efc907633
commit
d9c475a967
74 files changed
+2401
-630
No files matched your search
@@ -0,0 +1,120 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { ruleExportToken } from "../../lib/server/ruleExportToken";
|
||||
|
||||
const isDatabaseConfiguredMock = vi.fn();
|
||||
const getSessionPepperMock = vi.fn();
|
||||
const findUniqueMock = vi.fn();
|
||||
const getSessionUserMock = vi.fn();
|
||||
const isRuleAdminEmailMock = vi.fn();
|
||||
|
||||
vi.mock("../../lib/server/env", () => ({
|
||||
isDatabaseConfigured: () => isDatabaseConfiguredMock(),
|
||||
getSessionPepper: () => getSessionPepperMock(),
|
||||
}));
|
||||
|
||||
vi.mock("../../lib/server/db", () => ({
|
||||
prisma: {
|
||||
publishedRule: {
|
||||
findUnique: (...args: unknown[]) => findUniqueMock(...args),
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("../../lib/server/session", () => ({
|
||||
getSessionUser: () => getSessionUserMock(),
|
||||
}));
|
||||
|
||||
vi.mock("../../lib/server/ruleAdmins", () => ({
|
||||
isRuleAdminEmail: (...args: unknown[]) => isRuleAdminEmailMock(...args),
|
||||
}));
|
||||
|
||||
import { GET } from "../../app/api/rules/[id]/export/[format]/route";
|
||||
|
||||
const PEPPER = "test-pepper-16chars";
|
||||
|
||||
function makeContext(id: string, format: string) {
|
||||
return { params: Promise.resolve({ id, format }) };
|
||||
}
|
||||
|
||||
function row() {
|
||||
return {
|
||||
id: "rule-1",
|
||||
title: "Garden Norms",
|
||||
summary: "We share tools.",
|
||||
document: {
|
||||
sections: [
|
||||
{
|
||||
categoryName: "Values",
|
||||
entries: [{ title: "Solidarity", body: "Be kind." }],
|
||||
},
|
||||
],
|
||||
},
|
||||
createdAt: new Date("2026-11-08T00:00:00.000Z"),
|
||||
userId: "user-1",
|
||||
};
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
isDatabaseConfiguredMock.mockReset();
|
||||
getSessionPepperMock.mockReset();
|
||||
findUniqueMock.mockReset();
|
||||
getSessionUserMock.mockReset();
|
||||
isRuleAdminEmailMock.mockReset();
|
||||
isDatabaseConfiguredMock.mockReturnValue(true);
|
||||
getSessionPepperMock.mockReturnValue(PEPPER);
|
||||
getSessionUserMock.mockResolvedValue(null);
|
||||
isRuleAdminEmailMock.mockReturnValue(false);
|
||||
findUniqueMock.mockResolvedValue(row());
|
||||
});
|
||||
|
||||
describe("GET /api/rules/[id]/export/[format]", () => {
|
||||
it("returns 404 for an unknown format", async () => {
|
||||
const res = await GET(
|
||||
new NextRequest("https://x.test/api/rules/rule-1/export/csv"),
|
||||
makeContext("rule-1", "csv"),
|
||||
);
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
|
||||
it("returns 401 without a token or session", async () => {
|
||||
const res = await GET(
|
||||
new NextRequest("https://x.test/api/rules/rule-1/export/pdf"),
|
||||
makeContext("rule-1", "pdf"),
|
||||
);
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
|
||||
it("returns a PDF attachment when the export token matches", async () => {
|
||||
const token = ruleExportToken("rule-1", PEPPER);
|
||||
const res = await GET(
|
||||
new NextRequest(
|
||||
`https://x.test/api/rules/rule-1/export/pdf?t=${token}`,
|
||||
),
|
||||
makeContext("rule-1", "pdf"),
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers.get("content-type")).toBe("application/pdf");
|
||||
expect(res.headers.get("content-disposition")).toContain(
|
||||
"Garden Norms Rule.pdf",
|
||||
);
|
||||
const buf = Buffer.from(await res.arrayBuffer());
|
||||
expect(buf.subarray(0, 5).toString()).toBe("%PDF-");
|
||||
});
|
||||
|
||||
it("returns RULE.md for the owner without a token", async () => {
|
||||
getSessionUserMock.mockResolvedValue({
|
||||
id: "user-1",
|
||||
email: "owner@example.com",
|
||||
});
|
||||
const res = await GET(
|
||||
new NextRequest("https://x.test/api/rules/rule-1/export/md"),
|
||||
makeContext("rule-1", "md"),
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers.get("content-type")).toContain("text/markdown");
|
||||
const text = await res.text();
|
||||
expect(text).toContain("# Garden Norms");
|
||||
expect(text).toContain("## Shared terms");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user