Wire Publish rule from create flow

This commit is contained in:
adilallo
2026-04-07 22:26:25 -06:00
parent a4f0b449b6
commit 8f932e95cd
16 changed files with 839 additions and 252 deletions
+112
View File
@@ -0,0 +1,112 @@
import { describe, it, expect } from "vitest";
import {
buildPublishPayload,
parseDocumentSectionsForDisplay,
parseSectionsFromCreateFlowState,
} from "../../lib/create/buildPublishPayload";
import type { CreateFlowState } from "../../app/create/types";
describe("buildPublishPayload", () => {
it("returns error when title missing", () => {
expect(buildPublishPayload({})).toEqual({
ok: false,
error: "missingCommunityName",
});
});
it("returns error when title is whitespace only", () => {
expect(buildPublishPayload({ title: " \n\t " })).toEqual({
ok: false,
error: "missingCommunityName",
});
});
it("returns title and fallback Overview section when no sections", () => {
const r = buildPublishPayload({ title: "Oak Park Commons" });
expect(r.ok).toBe(true);
if (!r.ok) return;
expect(r.title).toBe("Oak Park Commons");
expect(r.summary).toBeUndefined();
expect(r.document).toEqual({
sections: [
{
categoryName: "Overview",
entries: [
{
title: "Community",
body: "This CommunityRule was created in the create flow. Add more detail in a future edit.",
},
],
},
],
});
});
it("includes trimmed summary in payload and uses it as fallback section body", () => {
const r = buildPublishPayload({
title: " My Group ",
summary: " We organize locally. ",
});
expect(r.ok).toBe(true);
if (!r.ok) return;
expect(r.title).toBe("My Group");
expect(r.summary).toBe("We organize locally.");
expect(r.document).toEqual({
sections: [
{
categoryName: "Overview",
entries: [{ title: "Community", body: "We organize locally." }],
},
],
});
});
it("uses valid state.sections when present", () => {
const sections: CreateFlowState["sections"] = [
{
categoryName: "Values",
entries: [{ title: "A", body: "B" }],
},
];
const r = buildPublishPayload({ title: "T", sections });
expect(r.ok).toBe(true);
if (!r.ok) return;
expect(r.document).toEqual({ sections });
});
it("filters invalid section entries from state.sections", () => {
const r = buildPublishPayload({
title: "T",
sections: [
{ categoryName: "Values", entries: [{ title: "A", body: "B" }] },
{ bad: true } as unknown as Record<string, unknown>,
],
});
expect(r.ok).toBe(true);
if (!r.ok) return;
expect(r.document).toEqual({
sections: [{ categoryName: "Values", entries: [{ title: "A", body: "B" }] }],
});
});
});
describe("parseDocumentSectionsForDisplay", () => {
it("returns empty for non-object", () => {
expect(parseDocumentSectionsForDisplay(null)).toEqual([]);
});
it("parses valid sections array", () => {
const doc = {
sections: [
{ categoryName: "X", entries: [{ title: "t", body: "b" }] },
],
};
expect(parseDocumentSectionsForDisplay(doc)).toEqual(doc.sections);
});
});
describe("parseSectionsFromCreateFlowState", () => {
it("returns empty when sections missing", () => {
expect(parseSectionsFromCreateFlowState({})).toEqual([]);
});
});
+71
View File
@@ -0,0 +1,71 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { publishRule } from "../../lib/create/api";
const input = {
title: "T",
document: { sections: [] as unknown[] },
};
describe("publishRule", () => {
const originalFetch = globalThis.fetch;
beforeEach(() => {
vi.resetAllMocks();
});
afterEach(() => {
globalThis.fetch = originalFetch;
});
it("returns ok on 200 with rule", async () => {
globalThis.fetch = vi.fn().mockResolvedValue(
new Response(JSON.stringify({ rule: { id: "r1", title: "T" } }), {
status: 200,
headers: { "Content-Type": "application/json" },
}),
);
const result = await publishRule(input);
expect(result).toEqual({ ok: true, id: "r1", title: "T" });
});
it("does not throw when body is empty (e.g. connection reset)", async () => {
globalThis.fetch = vi.fn().mockResolvedValue(
new Response("", {
status: 503,
statusText: "Service Unavailable",
}),
);
const result = await publishRule(input);
expect(result.ok).toBe(false);
if (result.ok === false) {
expect(result.status).toBe(503);
expect(result.error).toBe("Service Unavailable");
}
});
it("parses validation error when JSON present", async () => {
globalThis.fetch = vi.fn().mockResolvedValue(
new Response(
JSON.stringify({
error: { code: "validation_error", message: "title required" },
}),
{ status: 400, headers: { "Content-Type": "application/json" } },
),
);
const result = await publishRule(input);
expect(result).toEqual({
ok: false,
error: "title required",
status: 400,
});
});
it("returns network message when fetch rejects", async () => {
globalThis.fetch = vi.fn().mockRejectedValue(new Error("offline"));
const result = await publishRule(input);
expect(result).toEqual({
ok: false,
error: "Something went wrong. Check your connection and try again.",
});
});
});