66 lines
3.6 KiB
TypeScript
66 lines
3.6 KiB
TypeScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { goodsReceipt, parseInspection, receiptInput } from "../src/lib/procurement.ts";
|
|
import { materialInput, purchaseOrderInput, supplierInput, supplyTerms, supplyTermsInput } from "../src/lib/procurement.ts";
|
|
|
|
const id = "d66c798a-f764-4b88-8d13-ace328ff1010";
|
|
test("partial receipts accept positive quantities and reject duplicate order lines", () => {
|
|
const line = { purchaseOrderLineId: id, quantity: "0.125" };
|
|
assert.equal(receiptInput.safeParse({ lines: [line] }).success, true);
|
|
assert.equal(receiptInput.safeParse({ lines: [line, line] }).success, false);
|
|
assert.equal(receiptInput.safeParse({ lines: [{ ...line, quantity: "0.000" }] }).success, false);
|
|
});
|
|
|
|
test("inspection reconciles exact decimal quantities and requires a reason for rejects", () => {
|
|
const receipt = goodsReceipt.parse({
|
|
id, deliveryNote: null, createdAt: "2026-11-12T00:00:00.000Z",
|
|
lines: [{ id, purchaseOrderLineId: id, quantity: "0.3", inspection: null }],
|
|
});
|
|
const decision = { receiptLineId: id, acceptedQuantity: "0.100", rejectedQuantity: "0.200", rejectionReason: "Damaged" };
|
|
assert.deepEqual(parseInspection({ lines: [decision] }, receipt), { lines: [decision] });
|
|
assert.throws(() => parseInspection({ lines: [{ ...decision, acceptedQuantity: "0.101" }] }, receipt), /add up/);
|
|
assert.throws(() => parseInspection({ lines: [{ ...decision, rejectionReason: undefined }] }, receipt), /rejection reason/);
|
|
assert.throws(() => parseInspection({ lines: [decision, decision] }, receipt), /one decision/);
|
|
});
|
|
const terms = {
|
|
supplierSku: "",
|
|
leadTimeDays: 10,
|
|
minOrderQuantity: "0.125",
|
|
unitPrice: "900000000.01",
|
|
currency: "INR",
|
|
active: true,
|
|
};
|
|
|
|
test("supplier directory normalizes business codes and email addresses", () => {
|
|
const value = supplierInput.parse({
|
|
name: " Candle supply ", code: " wax_1 ", contactName: "Supplier contact",
|
|
email: "CONTACT@example.com", phone: "+91 1234567890", active: true,
|
|
});
|
|
assert.equal(value.name, "Candle supply");
|
|
assert.equal(value.code, "WAX_1");
|
|
assert.equal(value.email, "contact@example.com");
|
|
});
|
|
|
|
test("material directory enforces backend material kinds and units", () => {
|
|
const value = { name: "Wax", code: "WAX", kind: "WAX", unit: "GRAM", active: true };
|
|
assert.equal(materialInput.safeParse(value).success, true);
|
|
assert.equal(materialInput.safeParse({ ...value, unit: "BOX" }).success, false);
|
|
});
|
|
|
|
test("supply terms preserve precise string quantities and enforce request scale", () => {
|
|
assert.deepEqual(supplyTermsInput.parse(terms), terms);
|
|
assert.equal(supplyTermsInput.safeParse({ ...terms, minOrderQuantity: "0.000" }).success, false);
|
|
assert.equal(supplyTermsInput.safeParse({ ...terms, unitPrice: "2.1" }).success, false);
|
|
assert.equal(supplyTermsInput.safeParse({ ...terms, leadTimeDays: 366 }).success, false);
|
|
assert.equal(supplyTerms.parse({ ...terms, id, materialId: id, unitPrice: "2", minOrderQuantity: "1.5" }).unitPrice, "2");
|
|
});
|
|
|
|
test("draft orders require distinct materials, exact quantities and a calendar date", () => {
|
|
const line = { materialId: id, quantity: "1.001" };
|
|
const input = { supplierId: id, expectedDeliveryDate: "2026-12-01", lines: [line] };
|
|
assert.deepEqual(purchaseOrderInput.parse(input), input);
|
|
assert.equal(purchaseOrderInput.safeParse({ ...input, lines: [line, { ...line, materialId: id.toUpperCase() }] }).success, false);
|
|
assert.equal(purchaseOrderInput.safeParse({ ...input, expectedDeliveryDate: "2026-02-31" }).success, false);
|
|
assert.equal(purchaseOrderInput.safeParse({ ...input, lines: [] }).success, false);
|
|
});
|