Follow-up to the merged #11 PR (which shipped the enum-based catalogue): make categories user-defined data and the PO T&C a dynamic editor. - categories are a TermsCategory TABLE (not an enum) — admins add new ones; - every PO T&C line is catalogued, incl. the previously-fixed boilerplate (seeded under a "General" category) and an "Others" bucket; - the PO form is a dynamic editor: "+ Add term", pick a category, type/pick a clause (components/po/po-terms-editor.tsx), used by new/edit/manager-edit. Migration: the already-released 20260624140000 migration is untouched; a new 20260624150000 FORWARD migration renames the enum, creates the table, migrates existing enum clauses onto category rows, adds isDefault/sortOrder + the two fixed lines under General, and adds PurchaseOrder.terms (JSON snapshot that supersedes the legacy tc* columns for export/detail; old POs fall back to tc*). Tests rewritten for category creation + catalogue/default helpers. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
109 lines
5.2 KiB
TypeScript
109 lines
5.2 KiB
TypeScript
/**
|
|
* Integration tests for the Terms & Conditions admin CRUD (issue #11).
|
|
* Categories are user-defined data: adding a clause under a new category name
|
|
* creates the category. Covers CRUD + the manage_terms guard + the catalogue /
|
|
* default-terms helpers that feed the PO editor.
|
|
*/
|
|
import { vi, describe, it, expect, afterAll } from "vitest";
|
|
|
|
vi.mock("@/auth", () => ({ auth: vi.fn() }));
|
|
vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
|
|
|
|
import { auth } from "@/auth";
|
|
import { db } from "@/lib/db";
|
|
import {
|
|
createTerm,
|
|
updateTerm,
|
|
toggleTermActive,
|
|
deleteTerm,
|
|
} from "@/app/(portal)/admin/terms/actions";
|
|
import { getTermsCatalogue, getDefaultPoTerms } from "@/lib/terms-data";
|
|
import { makeSession, fd } from "./helpers";
|
|
|
|
const mockedAuth = vi.mocked(auth);
|
|
const PREFIX = "INTTEST_TERMS_";
|
|
const asManager = () => mockedAuth.mockResolvedValue(makeSession("u-mgr", "MANAGER") as never);
|
|
|
|
afterAll(async () => {
|
|
await db.termsCondition.deleteMany({ where: { text: { startsWith: PREFIX } } });
|
|
await db.termsCategory.deleteMany({ where: { name: { startsWith: PREFIX } } });
|
|
});
|
|
|
|
describe("createTerm", () => {
|
|
it("creates a new category on the fly and the clause under it", async () => {
|
|
asManager();
|
|
const result = await createTerm(fd({ categoryName: `${PREFIX}Warranty`, text: `${PREFIX}12 months` }));
|
|
expect(result).toEqual({ ok: true });
|
|
|
|
const cat = await db.termsCategory.findFirstOrThrow({ where: { name: `${PREFIX}Warranty` }, include: { clauses: true } });
|
|
expect(cat.clauses).toHaveLength(1);
|
|
expect(cat.clauses[0].text).toBe(`${PREFIX}12 months`);
|
|
});
|
|
|
|
it("reuses an existing category (case-insensitive) for a second clause", async () => {
|
|
asManager();
|
|
await createTerm(fd({ categoryName: `${PREFIX}warranty`, text: `${PREFIX}24 months` }));
|
|
const cats = await db.termsCategory.findMany({ where: { name: { startsWith: PREFIX, mode: "insensitive" }, AND: { name: { equals: `${PREFIX}Warranty`, mode: "insensitive" } } } });
|
|
expect(cats).toHaveLength(1); // no duplicate category
|
|
});
|
|
|
|
it("requires a category and clause text", async () => {
|
|
asManager();
|
|
expect("error" in (await createTerm(fd({ categoryName: " ", text: "x" })))).toBe(true);
|
|
expect("error" in (await createTerm(fd({ categoryName: `${PREFIX}X`, text: " " })))).toBe(true);
|
|
});
|
|
|
|
it("refuses callers without manage_terms", async () => {
|
|
mockedAuth.mockResolvedValue(makeSession("u-tech", "TECHNICAL") as never);
|
|
expect(await createTerm(fd({ categoryName: `${PREFIX}X`, text: `${PREFIX}nope` }))).toEqual({ error: "Forbidden" });
|
|
mockedAuth.mockResolvedValue(makeSession("u-acc", "ACCOUNTS") as never);
|
|
expect(await createTerm(fd({ categoryName: `${PREFIX}X`, text: `${PREFIX}nope` }))).toEqual({ error: "Forbidden" });
|
|
});
|
|
});
|
|
|
|
describe("updateTerm / toggle / delete", () => {
|
|
it("edits, toggles active, then deletes a clause", async () => {
|
|
asManager();
|
|
await createTerm(fd({ categoryName: `${PREFIX}Edit`, text: `${PREFIX}old` }));
|
|
const t = await db.termsCondition.findFirstOrThrow({ where: { text: `${PREFIX}old` } });
|
|
|
|
expect(await updateTerm(t.id, fd({ categoryName: `${PREFIX}Edit2`, text: `${PREFIX}new` }))).toEqual({ ok: true });
|
|
const after = await db.termsCondition.findUniqueOrThrow({ where: { id: t.id }, include: { category: true } });
|
|
expect(after.text).toBe(`${PREFIX}new`);
|
|
expect(after.category.name).toBe(`${PREFIX}Edit2`);
|
|
|
|
expect(await toggleTermActive(t.id)).toEqual({ ok: true });
|
|
expect((await db.termsCondition.findUniqueOrThrow({ where: { id: t.id } })).isActive).toBe(false);
|
|
|
|
expect(await deleteTerm(t.id)).toEqual({ ok: true });
|
|
expect(await db.termsCondition.findUnique({ where: { id: t.id } })).toBeNull();
|
|
});
|
|
|
|
it("guards update/toggle/delete behind the permission", async () => {
|
|
mockedAuth.mockResolvedValue(makeSession("u-tech", "TECHNICAL") as never);
|
|
expect(await updateTerm("x", fd({ categoryName: `${PREFIX}X`, text: "y" }))).toEqual({ error: "Forbidden" });
|
|
expect(await toggleTermActive("x")).toEqual({ error: "Forbidden" });
|
|
expect(await deleteTerm("x")).toEqual({ error: "Forbidden" });
|
|
});
|
|
});
|
|
|
|
describe("catalogue + default terms helpers", () => {
|
|
it("getTermsCatalogue exposes active categories with their active clauses", async () => {
|
|
asManager();
|
|
await createTerm(fd({ categoryName: `${PREFIX}Cat`, text: `${PREFIX}active clause` }));
|
|
await createTerm(fd({ categoryName: `${PREFIX}Cat`, text: `${PREFIX}inactive clause` }));
|
|
const inactive = await db.termsCondition.findFirstOrThrow({ where: { text: `${PREFIX}inactive clause` } });
|
|
await toggleTermActive(inactive.id);
|
|
|
|
const cat = (await getTermsCatalogue()).find((c) => c.name === `${PREFIX}Cat`);
|
|
expect(cat?.clauses).toContain(`${PREFIX}active clause`);
|
|
expect(cat?.clauses).not.toContain(`${PREFIX}inactive clause`);
|
|
});
|
|
|
|
it("getDefaultPoTerms returns isDefault clauses", async () => {
|
|
asManager();
|
|
await createTerm(fd({ categoryName: `${PREFIX}Def`, text: `${PREFIX}default clause`, isDefault: "true" }));
|
|
const defaults = await getDefaultPoTerms();
|
|
expect(defaults.some((d) => d.text === `${PREFIX}default clause` && d.category === `${PREFIX}Def`)).toBe(true);
|
|
});
|
|
});
|