76 lines
3.6 KiB
TypeScript
76 lines
3.6 KiB
TypeScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import {
|
|
cancelPartnerOrderInput,
|
|
createPartnerOrderInput,
|
|
partnerOrder,
|
|
partnerOrderEligible,
|
|
partnerOrderSummary,
|
|
} from "../src/lib/partner-orders.ts";
|
|
|
|
const id = "d66c798a-f764-4b88-8d13-ace328ff1010";
|
|
const createdAt = "2026-11-17T00:00:00.000Z";
|
|
const accountSnapshot = {
|
|
legalName: "Northlight Studio",
|
|
tradingName: null,
|
|
registrationNumber: "REG-100",
|
|
taxIdentifier: null,
|
|
countryCode: "IN",
|
|
contactEmail: "buyer@example.com",
|
|
contactPhone: null,
|
|
billingLine1: "30 Market Road",
|
|
billingCity: "Mumbai",
|
|
billingRegion: "Maharashtra",
|
|
billingPostalCode: "400001",
|
|
profileVersion: 2,
|
|
};
|
|
|
|
test("partner order inputs retain reference case and require the exact payload", () => {
|
|
const input = { quoteId: id, partnerReference: " Customer-PO/42 ", note: null, idempotencyKey: id };
|
|
assert.deepEqual(createPartnerOrderInput.parse(input), { ...input, partnerReference: "Customer-PO/42" });
|
|
assert.equal(createPartnerOrderInput.safeParse({ ...input, note: "x" }).success, false);
|
|
assert.equal(createPartnerOrderInput.safeParse({ ...input, partnerReference: "x".repeat(81) }).success, false);
|
|
assert.equal(createPartnerOrderInput.safeParse({ ...input, partnerReference: "<reference>" }).success, false);
|
|
assert.equal(createPartnerOrderInput.safeParse({ ...input, accountId: id }).success, false);
|
|
assert.deepEqual(cancelPartnerOrderInput.parse({ reason: " Partner withdrew ", idempotencyKey: id }), {
|
|
reason: "Partner withdrew", idempotencyKey: id,
|
|
});
|
|
assert.equal(cancelPartnerOrderInput.safeParse({ reason: " ", idempotencyKey: id }).success, false);
|
|
});
|
|
|
|
test("an accepted quotation is eligible only before its expiry instant", () => {
|
|
const expiresAt = "2026-11-17T10:00:00+05:30";
|
|
const expiry = Date.parse(expiresAt);
|
|
assert.equal(partnerOrderEligible({ status: "ACCEPTED", expiresAt }, expiry - 1), true);
|
|
assert.equal(partnerOrderEligible({ status: "ACCEPTED", expiresAt }, expiry), false);
|
|
assert.equal(partnerOrderEligible({ status: "ISSUED", expiresAt }, expiry - 1), false);
|
|
assert.equal(partnerOrderEligible({ status: "ACCEPTED", expiresAt: "invalid" }, expiry - 1), false);
|
|
});
|
|
|
|
test("order parsers preserve immutable snapshots and exact amounts while dropping backend metadata", () => {
|
|
const order = {
|
|
id, accountId: id, quoteId: id, createdById: id, number: "BO-123", status: "CANCELLED",
|
|
partnerReference: "Customer-PO/42", note: null, currency: "INR", createdAt,
|
|
merchandiseTotal: "9999999999999999.99",
|
|
accountSnapshot,
|
|
organizationId: id,
|
|
requestHash: "opaque",
|
|
lines: [{
|
|
id, orderId: id, organizationId: id, variantId: id, sku: "AMBER", name: "Amber candle",
|
|
quantity: 12, minQuantity: 6, unitPrice: "123.45", lineTotal: "1481.40",
|
|
}],
|
|
cancellation: { actorId: id, reason: "Partner withdrew", createdAt, orderId: id, requestHash: "opaque" },
|
|
};
|
|
const detail = partnerOrder.parse(order);
|
|
assert.equal(detail.merchandiseTotal, order.merchandiseTotal);
|
|
assert.equal(detail.lines[0].lineTotal, "1481.40");
|
|
assert.equal(detail.accountSnapshot.profileVersion, 2);
|
|
assert.deepEqual(detail.cancellation, { actorId: id, reason: "Partner withdrew", createdAt });
|
|
assert.equal("requestHash" in detail, false);
|
|
assert.equal("organizationId" in detail.lines[0], false);
|
|
const summary = partnerOrderSummary.parse(order);
|
|
assert.deepEqual(summary.accountSnapshot, { legalName: accountSnapshot.legalName });
|
|
assert.equal("lines" in summary, false);
|
|
assert.equal(partnerOrder.safeParse({ ...order, merchandiseTotal: 123.45 }).success, false);
|
|
});
|