|
| 1 | +import { test } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { runMatch } from "./matching"; |
| 4 | +import { routeApproval } from "./policy"; |
| 5 | +import { CLIENT_PROFILES, profileById } from "@/db/client-profiles"; |
| 6 | +import type { Invoice, PurchaseOrder } from "./schema"; |
| 7 | + |
| 8 | +/** |
| 9 | + * Profile-driven behaviour — the config-driven claim. The SAME invoice routes |
| 10 | + * differently depending on the client profile (tolerances + approval tiers). This |
| 11 | + * is what "onboarding a client is config, not code" means in practice. |
| 12 | + */ |
| 13 | + |
| 14 | +// An invoice 3% over the PO on one line. Small money, small variance. |
| 15 | +const PO: PurchaseOrder = { |
| 16 | + poNumber: "PO-1", |
| 17 | + vendor: "Acme", |
| 18 | + currency: "USD", |
| 19 | + lineItems: [ |
| 20 | + { sku: "A", description: "Widget", qty: 10, unitPrice: 100, amount: 1000 }, |
| 21 | + ], |
| 22 | + total: 1000, |
| 23 | +}; |
| 24 | +const INV: Invoice = { |
| 25 | + invoiceNumber: "INV-1", |
| 26 | + poNumber: "PO-1", |
| 27 | + vendor: "Acme", |
| 28 | + issueDate: "2026-05-01", |
| 29 | + currency: "USD", |
| 30 | + // 103 vs 100 = 3% over. |
| 31 | + lineItems: [ |
| 32 | + { sku: "A", description: "Widget", qty: 10, unitPrice: 103, amount: 1030 }, |
| 33 | + ], |
| 34 | + subtotal: 1030, |
| 35 | + tax: null, |
| 36 | + total: 1030, |
| 37 | +}; |
| 38 | + |
| 39 | +const strict = profileById("severn-manufacturing"); |
| 40 | +const relaxed = profileById("meridian-distribution"); |
| 41 | + |
| 42 | +test("a 3% overage is an exception under the strict profile, clean under relaxed", () => { |
| 43 | + const strictMatch = runMatch( |
| 44 | + { invoice: INV, purchaseOrder: PO, goodsReceipt: null }, |
| 45 | + strict.tolerances, |
| 46 | + ); |
| 47 | + const relaxedMatch = runMatch( |
| 48 | + { invoice: INV, purchaseOrder: PO, goodsReceipt: null }, |
| 49 | + relaxed.tolerances, |
| 50 | + ); |
| 51 | + // strict: 0.5% tolerance → 3% is a price variance → exception. |
| 52 | + assert.equal(strictMatch.verdict, "exception"); |
| 53 | + // relaxed: 5% tolerance → 3% is within noise → clean. |
| 54 | + assert.equal(relaxedMatch.verdict, "clean"); |
| 55 | +}); |
| 56 | + |
| 57 | +test("the same exception routes to a different tier per profile", () => { |
| 58 | + // Force an exception both ways by using the strict match (an exception exists). |
| 59 | + const match = runMatch( |
| 60 | + { invoice: INV, purchaseOrder: PO, goodsReceipt: null }, |
| 61 | + strict.tolerances, |
| 62 | + ); |
| 63 | + assert.equal(match.verdict, "exception"); |
| 64 | + |
| 65 | + // ~30 USD at stake, 3% variance. |
| 66 | + const strictDecision = routeApproval(match, strict.approvalPolicy); |
| 67 | + const relaxedDecision = routeApproval(match, relaxed.approvalPolicy); |
| 68 | + |
| 69 | + // strict: 2% manager threshold → 3% trips manager. |
| 70 | + assert.equal(strictDecision.tier, "manager"); |
| 71 | + // relaxed: 10% manager threshold, $5k → 3% / $30 stays under → still manager |
| 72 | + // (an exception never auto-approves), but proves the policy is applied, not fixed. |
| 73 | + assert.equal(relaxedDecision.tier, "manager"); |
| 74 | +}); |
| 75 | + |
| 76 | +test("profileById falls back to standard for unknown / missing ids", () => { |
| 77 | + assert.equal(profileById("nope").id, "standard"); |
| 78 | + assert.equal(profileById(null).id, "standard"); |
| 79 | + assert.equal(profileById(undefined).id, "standard"); |
| 80 | +}); |
| 81 | + |
| 82 | +test("every seeded profile is internally valid", () => { |
| 83 | + for (const p of CLIENT_PROFILES) { |
| 84 | + assert.ok(p.tolerances.pricePct >= 0); |
| 85 | + assert.ok( |
| 86 | + p.approvalPolicy.director.amount >= p.approvalPolicy.manager.amount, |
| 87 | + ); |
| 88 | + } |
| 89 | +}); |
0 commit comments