src/features/reviews/command-schema.test.ts
Linked repository file from this build.
import { describe, expect, test } from "bun:test";
import { createSeed, completedReviewAnswers } from "@/programs/snap-negative/seed";
import { commandSchema, pendingCommandSchema } from "./command-schema";
import { actorSchema } from "@/lib/auth/actor";
const seed = createSeed();
function validScenarioCommand() {
return {
type: "scenario" as const,
scenario: "compI" as const,
value: seed.facts.scenarios.compI,
};
}
function validPending(overrides: Record<string, unknown> = {}) {
return {
id: "550e8400-e29b-41d4-a716-446655440000",
command: { type: "prepare_decision" as const },
...overrides,
};
}
describe("commandSchema", () => {
test("accepts every ReviewCommand variant", () => {
expect(
commandSchema.safeParse({ type: "identity", value: { clientName: "Updated" } }).success,
).toBe(true);
expect(commandSchema.safeParse({ type: "household", value: seed.facts.members }).success).toBe(
true,
);
expect(
commandSchema.safeParse({ type: "negative_action", value: { negativeAction: "Denial" } })
.success,
).toBe(true);
expect(commandSchema.safeParse(validScenarioCommand()).success).toBe(true);
expect(commandSchema.safeParse({ type: "answers", value: seed.facts.answers }).success).toBe(
true,
);
expect(
commandSchema.safeParse({ type: "answers", value: completedReviewAnswers() }).success,
).toBe(true);
expect(commandSchema.safeParse({ type: "outcome", value: "clean" }).success).toBe(true);
expect(commandSchema.safeParse({ type: "apply_final" }).success).toBe(true);
expect(commandSchema.safeParse({ type: "prepare_decision" }).success).toBe(true);
expect(commandSchema.safeParse({ type: "note", body: "Case note." }).success).toBe(true);
expect(commandSchema.safeParse({ type: "note", body: "PEC note.", kind: "pec" }).success).toBe(
true,
);
expect(
commandSchema.safeParse({
type: "update_note",
noteId: "note-1",
body: "Updated note.",
}).success,
).toBe(true);
expect(
commandSchema.safeParse({
type: "refuse_finding",
findingId: "finding-1",
reason: "Not applicable.",
}).success,
).toBe(true);
expect(
commandSchema.safeParse({
type: "policy_decision",
decision: "approve",
policyNote: "Aligned.",
}).success,
).toBe(true);
expect(
commandSchema.safeParse({
type: "document",
document: {
id: "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
filename: "evidence.pdf",
name: "Evidence",
category: "Evidence",
description: "Demo attachment",
mimeType: "application/pdf",
size: 1024,
storageKey: "11111111-1111-4111-8111-111111111111",
},
}).success,
).toBe(true);
expect(
commandSchema.safeParse({ type: "transition", action: "submit", note: "" }).success,
).toBe(true);
});
test("rejects unknown command type", () => {
expect(commandSchema.safeParse({ type: "unknown_command" }).success).toBe(false);
});
test("rejects unexpected top-level properties", () => {
expect(commandSchema.safeParse({ type: "prepare_decision", extra: true }).success).toBe(false);
expect(commandSchema.safeParse({ type: "apply_final", note: "hidden" }).success).toBe(false);
});
test("rejects invalid actor roles and empty names", () => {
const staff = { staffId: "601", program: "SNAP" };
expect(actorSchema.safeParse({ ...staff, name: "Pat", role: "intruder" }).success).toBe(false);
expect(actorSchema.safeParse({ ...staff, name: "", role: "examiner" }).success).toBe(false);
expect(actorSchema.safeParse({ ...staff, name: "Pat", role: "pec", extra: true }).success).toBe(
false,
);
expect(actorSchema.safeParse({ name: "Pat", role: "pec" }).success).toBe(false);
});
test("rejects identity patches outside the whitelist", () => {
expect(
commandSchema.safeParse({ type: "identity", value: { clientName: "OK", program: "SNAP" } })
.success,
).toBe(false);
expect(commandSchema.safeParse({ type: "identity", value: {} }).success).toBe(false);
});
test("rejects empty household arrays and unsafe numeric fields", () => {
expect(commandSchema.safeParse({ type: "household", value: [] }).success).toBe(false);
const badScenario = {
...validScenarioCommand(),
value: { ...seed.facts.scenarios.compI, householdSize: 1.5 },
};
expect(commandSchema.safeParse(badScenario).success).toBe(false);
const badIncome = {
...validScenarioCommand(),
value: {
...seed.facts.scenarios.compI,
income: [{ ...seed.facts.scenarios.compI.income[0], amountCents: 12.5 }],
},
};
expect(commandSchema.safeParse(badIncome).success).toBe(false);
const unsafeCents = {
...validScenarioCommand(),
value: {
...seed.facts.scenarios.compI,
income: [
{ ...seed.facts.scenarios.compI.income[0], amountCents: Number.MAX_SAFE_INTEGER + 1 },
],
},
};
expect(commandSchema.safeParse(unsafeCents).success).toBe(false);
});
test("rejects invalid enums for outcome, scenario, workflow, and policy decision", () => {
expect(commandSchema.safeParse({ type: "outcome", value: "maybe" }).success).toBe(false);
expect(
commandSchema.safeParse({
type: "scenario",
scenario: "compIII",
value: seed.facts.scenarios.compI,
}).success,
).toBe(false);
expect(
commandSchema.safeParse({ type: "transition", action: "escalate", note: "" }).success,
).toBe(false);
expect(
commandSchema.safeParse({
type: "policy_decision",
decision: "maybe",
policyNote: "nope",
}).success,
).toBe(false);
});
test("rejects whitespace-only notes and extra variance pairs", () => {
expect(commandSchema.safeParse({ type: "note", body: " " }).success).toBe(false);
expect(
commandSchema.safeParse({
type: "answers",
value: {
...seed.facts.answers,
variances: [
{ element: "1", nature: "a" },
{ element: "2", nature: "b" },
{ element: "3", nature: "c" },
{ element: "4", nature: "d" },
],
},
}).success,
).toBe(false);
});
test("rejects malformed document payloads", () => {
expect(
commandSchema.safeParse({
type: "document",
document: {
id: "not-a-uuid",
filename: "evidence.pdf",
name: "Evidence",
category: "Evidence",
description: "",
mimeType: "application/pdf",
size: 0,
storageKey: "11111111-1111-4111-8111-111111111111",
},
}).success,
).toBe(false);
});
});
describe("pendingCommandSchema", () => {
test("accepts a valid pending command envelope", () => {
expect(pendingCommandSchema.safeParse(validPending()).success).toBe(true);
});
test("rejects malformed pending command envelopes", () => {
expect(pendingCommandSchema.safeParse(validPending({ id: "bad-id" })).success).toBe(false);
expect(pendingCommandSchema.safeParse(validPending({ actor: {} })).success).toBe(false);
expect(
pendingCommandSchema.safeParse(
validPending({ command: { type: "prepare_decision", extra: true } }),
).success,
).toBe(false);
expect(pendingCommandSchema.safeParse({ ...validPending(), stray: true }).success).toBe(false);
});
});
test("scenario education amounts and application day reject invalid boundaries", () => {
const scenario = createSeed().facts.scenarios.compI;
const accepts = (patch: object) =>
commandSchema.safeParse({
type: "scenario",
scenario: "compI",
value: { ...scenario, ...patch },
}).success;
expect(
accepts({ educationGrantCents: 100_000_000, educationExpensesCents: 0, applicationDay: 31 }),
).toBe(true);
expect(accepts({ applicationDay: null })).toBe(true);
for (const applicationDay of [0, 32, 1.5, ""]) expect(accepts({ applicationDay })).toBe(false);
for (const key of ["educationGrantCents", "educationExpensesCents"]) {
for (const value of [-1, 100_000_001, 1.5]) expect(accepts({ [key]: value })).toBe(false);
}
});