235 lines
7.2 KiB
TypeScript
235 lines
7.2 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { buildAppliedTemplateCreateFlowState } from "../../lib/create/applyTemplatePrefill";
|
|
import { buildFinalReviewCategoriesFromState } from "../../lib/create/buildFinalReviewCategories";
|
|
import {
|
|
templateBodyToCategories,
|
|
templateBodyToReviewData,
|
|
templateCategoryToGroupKey,
|
|
templateReviewDataForSlug,
|
|
templateSummaryFromBody,
|
|
} from "../../lib/create/templateReviewMapping";
|
|
import templateChipsFile from "../../data/templates/templateChips.json";
|
|
import templateReviewMessages from "../../messages/en/create/templateReview.json";
|
|
|
|
describe("templateReviewMapping", () => {
|
|
it("maps body sections to Rule categories", () => {
|
|
const body = {
|
|
sections: [
|
|
{
|
|
categoryName: "Values",
|
|
entries: [
|
|
{ title: "Solidarity", body: "Long body" },
|
|
{ title: "Ecology", body: "More" },
|
|
],
|
|
},
|
|
],
|
|
};
|
|
const cats = templateBodyToCategories(body);
|
|
expect(cats).toHaveLength(1);
|
|
expect(cats[0].name).toBe("Values");
|
|
expect(cats[0].chipOptions.map((c) => c.label)).toEqual([
|
|
"Solidarity",
|
|
"Ecology",
|
|
]);
|
|
expect(cats[0].chipOptions.every((c) => c.state === "unselected")).toBe(
|
|
true,
|
|
);
|
|
});
|
|
|
|
it("uses description for summary when present", () => {
|
|
expect(
|
|
templateSummaryFromBody("Short API description", { sections: [] }),
|
|
).toBe("Short API description");
|
|
});
|
|
|
|
it("falls back to first entry body when description empty", () => {
|
|
const body = {
|
|
sections: [
|
|
{
|
|
categoryName: "X",
|
|
entries: [{ title: "T", body: " First paragraph. " }],
|
|
},
|
|
],
|
|
};
|
|
expect(templateSummaryFromBody("", body)).toBe("First paragraph.");
|
|
});
|
|
|
|
describe("templateCategoryToGroupKey", () => {
|
|
it.each([
|
|
["Values", "coreValues"],
|
|
["Core values", "coreValues"],
|
|
["Communication", "communication"],
|
|
["Membership", "membership"],
|
|
["Decision-making", "decisionApproaches"],
|
|
["Decision making", "decisionApproaches"],
|
|
["Conflict management", "conflictManagement"],
|
|
["Conflict Resolution", "conflictManagement"],
|
|
] as const)("maps %s -> %s", (input, expected) => {
|
|
expect(templateCategoryToGroupKey(input)).toBe(expected);
|
|
});
|
|
|
|
it("returns null for unknown categories", () => {
|
|
expect(templateCategoryToGroupKey("Mystery")).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("templateBodyToReviewData", () => {
|
|
it("returns group-keyed chip detail lookup aligned with categories", () => {
|
|
const body = {
|
|
sections: [
|
|
{
|
|
categoryName: "Decision-making",
|
|
entries: [
|
|
{ title: "Consensus Decision-Making", body: "body-1" },
|
|
{ title: "Modified Consensus", body: "body-2" },
|
|
],
|
|
},
|
|
{
|
|
categoryName: "Values",
|
|
entries: [{ title: "Solidarity", body: "body-3" }],
|
|
},
|
|
{
|
|
categoryName: "Mystery",
|
|
entries: [{ title: "Unknown", body: "body-4" }],
|
|
},
|
|
],
|
|
};
|
|
const { categories, chipDetailsByChipId } = templateBodyToReviewData(body);
|
|
expect(categories).toHaveLength(3);
|
|
|
|
const firstChipId = categories[0].chipOptions[0].id;
|
|
expect(chipDetailsByChipId[firstChipId]).toEqual({
|
|
chipId: firstChipId,
|
|
chipLabel: "Consensus Decision-Making",
|
|
categoryName: "Decision-making",
|
|
groupKey: "decisionApproaches",
|
|
body: "body-1",
|
|
});
|
|
expect(
|
|
chipDetailsByChipId[categories[1].chipOptions[0].id].groupKey,
|
|
).toBe("coreValues");
|
|
expect(
|
|
chipDetailsByChipId[categories[2].chipOptions[0].id].groupKey,
|
|
).toBeNull();
|
|
});
|
|
|
|
it("is resilient to bad body input", () => {
|
|
expect(templateBodyToReviewData(null).categories).toEqual([]);
|
|
expect(templateBodyToReviewData({}).chipDetailsByChipId).toEqual({});
|
|
expect(templateBodyToReviewData({ sections: "nope" }).categories).toEqual(
|
|
[],
|
|
);
|
|
});
|
|
});
|
|
});
|
|
|
|
const FINAL_REVIEW_NAMES = {
|
|
values: "Values",
|
|
communication: "Communication",
|
|
membership: "Membership",
|
|
decisions: "Decision-making",
|
|
conflict: "Conflict management",
|
|
};
|
|
|
|
const STALE_CONSENSUS_BODY = {
|
|
sections: [
|
|
{
|
|
categoryName: "Values",
|
|
entries: [{ title: "Community Care", body: "" }],
|
|
},
|
|
{
|
|
categoryName: "Communication",
|
|
entries: [
|
|
{ title: "In-Person Meetings", body: "" },
|
|
{ title: "Loomio", body: "" },
|
|
],
|
|
},
|
|
{
|
|
categoryName: "Membership",
|
|
entries: [{ title: "Peer Sponsorship", body: "" }],
|
|
},
|
|
{
|
|
categoryName: "Decision-making",
|
|
entries: [
|
|
{ title: "Consensus or Vote-Based Approval", body: "" },
|
|
{ title: "Consensus Decision-Making", body: "" },
|
|
],
|
|
},
|
|
{
|
|
categoryName: "Conflict management",
|
|
entries: [{ title: "Facilitated Negotiation", body: "" }],
|
|
},
|
|
],
|
|
};
|
|
|
|
describe("templateReviewDataForSlug", () => {
|
|
it("lists remade catalog names for consensus, not API body titles", () => {
|
|
const { categories } = templateReviewDataForSlug(
|
|
"consensus",
|
|
STALE_CONSENSUS_BODY,
|
|
);
|
|
const labels = categories.flatMap((row) =>
|
|
row.chipOptions.map((chip) => chip.label),
|
|
);
|
|
expect(labels).toContain("How we run meetings");
|
|
expect(labels).toContain("In-person meetings");
|
|
expect(labels).toContain("Conflict ladder");
|
|
expect(labels).toContain("Community care");
|
|
expect(labels).toContain("Decision speeds");
|
|
expect(labels).not.toContain("In-Person Meetings");
|
|
expect(labels).not.toContain("Community Care");
|
|
expect(labels).not.toContain("Peer Sponsorship");
|
|
expect(labels).not.toContain("Facilitated Negotiation");
|
|
});
|
|
|
|
it("matches Use without changes final-review labels", () => {
|
|
const slugs = Object.keys(
|
|
(templateChipsFile as { templates: Record<string, unknown> }).templates,
|
|
);
|
|
for (const slug of slugs) {
|
|
const detail = templateReviewDataForSlug(slug, STALE_CONSENSUS_BODY);
|
|
const seeded = buildAppliedTemplateCreateFlowState({
|
|
slug,
|
|
body: STALE_CONSENSUS_BODY,
|
|
mode: "useWithoutChanges",
|
|
});
|
|
const review = buildFinalReviewCategoriesFromState(
|
|
seeded,
|
|
FINAL_REVIEW_NAMES,
|
|
);
|
|
const detailByName = new Map(
|
|
detail.categories.map((row) => [
|
|
row.name,
|
|
row.chipOptions.map((chip) => chip.label),
|
|
]),
|
|
);
|
|
for (const row of review) {
|
|
if (row.chips.length === 0) continue;
|
|
expect(detailByName.get(row.name), slug).toEqual(row.chips);
|
|
}
|
|
}
|
|
});
|
|
|
|
it("falls back to body titles when the slug is not in the catalog", () => {
|
|
const cats = templateReviewDataForSlug("unknown-template", {
|
|
sections: [
|
|
{
|
|
categoryName: "Values",
|
|
entries: [{ title: "Solidarity", body: "Long body" }],
|
|
},
|
|
],
|
|
}).categories;
|
|
expect(cats[0].chipOptions.map((chip) => chip.label)).toEqual([
|
|
"Solidarity",
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe("template review intro", () => {
|
|
it("reads one size fits all", () => {
|
|
expect(templateReviewMessages.intro.description).toContain(
|
|
"one size fits all",
|
|
);
|
|
});
|
|
});
|