242 lines
5.9 KiB
TypeScript
242 lines
5.9 KiB
TypeScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { productSchema, organizationSchema } from "../src/lib/api.ts";
|
|
import {
|
|
captureRecoveryToken,
|
|
getRecoveryToken,
|
|
clearRecoveryToken,
|
|
} from "../src/lib/recovery-token.ts";
|
|
import {
|
|
addressInput,
|
|
address,
|
|
role,
|
|
group,
|
|
variant,
|
|
warehouse,
|
|
stock,
|
|
stockInput,
|
|
adminProductSummary,
|
|
pricing,
|
|
checkoutInput,
|
|
couponInput,
|
|
variantInput,
|
|
money,
|
|
moneyTotal,
|
|
checkoutIntent,
|
|
} from "../src/lib/commerce.ts";
|
|
const id = "d66c798a-f764-4b88-8d13-ace328ff1010";
|
|
test("organization UUIDs normalize case before session organization checks", () => {
|
|
assert.equal(organizationSchema.parse(id.toUpperCase()), id);
|
|
});
|
|
test("stock identifiers normalize case before request and retry scopes", () => {
|
|
assert.equal(
|
|
stockInput.parse({
|
|
variantId: id.toUpperCase(),
|
|
warehouseId: id.toUpperCase(),
|
|
}).variantId,
|
|
id,
|
|
);
|
|
});
|
|
test("response schemas accept persisted metadata while write schemas stay strict", () => {
|
|
const extra = {
|
|
id,
|
|
organizationId: id,
|
|
createdAt: "2026-10-12T00:00:00.000Z",
|
|
};
|
|
const samples = [
|
|
[
|
|
address,
|
|
{
|
|
...extra,
|
|
recipient: "A",
|
|
line1: "Street",
|
|
line2: "",
|
|
city: "City",
|
|
region: "State",
|
|
postalCode: "100000",
|
|
countryCode: "IN",
|
|
phone: "+919999999999",
|
|
isDefault: true,
|
|
},
|
|
],
|
|
[role, { ...extra, name: "Staff", permissions: [], isSystem: false }],
|
|
[group, { ...extra, name: "Floral", slug: "floral", kind: "CATEGORY" }],
|
|
[
|
|
variant,
|
|
{
|
|
...extra,
|
|
productId: id,
|
|
sku: "A",
|
|
name: "Small",
|
|
price: "10",
|
|
currency: "INR",
|
|
active: true,
|
|
attributes: {},
|
|
},
|
|
],
|
|
[warehouse, { ...extra, name: "Main" }],
|
|
[stock, { ...extra, variantId: id, warehouseId: id, onHand: 10 }],
|
|
[
|
|
adminProductSummary,
|
|
{
|
|
...extra,
|
|
name: "Candle",
|
|
slug: "candle",
|
|
description: "",
|
|
status: "DRAFT",
|
|
},
|
|
],
|
|
[
|
|
pricing,
|
|
{
|
|
...extra,
|
|
name: "India",
|
|
currency: "INR",
|
|
countryCode: "IN",
|
|
region: "",
|
|
taxMode: "EXCLUSIVE",
|
|
merchandiseTaxBps: 0,
|
|
shippingFee: "0",
|
|
shippingTaxBps: 0,
|
|
freeShippingMinimum: null,
|
|
active: false,
|
|
},
|
|
],
|
|
] as const;
|
|
for (const [schema, value] of samples) {
|
|
const parsed = schema.safeParse(value);
|
|
assert.equal(parsed.success, true, JSON.stringify(parsed));
|
|
}
|
|
assert.equal(addressInput.safeParse(samples[0][1]).success, false);
|
|
});
|
|
test("recovery token is captured in memory and removed from URL history", () => {
|
|
const token = "A".repeat(43);
|
|
let replaced = "";
|
|
captureRecoveryToken(
|
|
{ hash: `#token=${token}`, pathname: "/reset-password", search: "" },
|
|
{
|
|
replaceState: (_data, _unused, url) => {
|
|
replaced = url;
|
|
},
|
|
},
|
|
);
|
|
assert.equal(replaced, "/reset-password");
|
|
assert.equal(getRecoveryToken(), token);
|
|
clearRecoveryToken();
|
|
assert.equal(getRecoveryToken(), null);
|
|
captureRecoveryToken(
|
|
{ hash: "#token=invalid", pathname: "/reset-password", search: "" },
|
|
{
|
|
replaceState: (_data, _unused, url) => {
|
|
replaced = url;
|
|
},
|
|
},
|
|
);
|
|
assert.equal(getRecoveryToken(), null);
|
|
});
|
|
test("backend decimal output accepts integer and one-decimal strings", () => {
|
|
for (const price of ["10", "10.5", "10.50"])
|
|
assert.equal(
|
|
productSchema.safeParse({
|
|
id,
|
|
name: "Candle",
|
|
slug: "candle",
|
|
description: "",
|
|
variants: [
|
|
{
|
|
id,
|
|
name: "Small",
|
|
sku: "CANDLE",
|
|
price,
|
|
currency: "INR",
|
|
attributes: {},
|
|
},
|
|
],
|
|
groups: [],
|
|
}).success,
|
|
true,
|
|
);
|
|
});
|
|
test("money display and estimated totals remain exact", () => {
|
|
assert.equal(money("10.5"), "10.50");
|
|
assert.equal(
|
|
moneyTotal([
|
|
{ price: "0.1", quantity: 3 },
|
|
{ price: "9999999999.99", quantity: 100 },
|
|
]),
|
|
"999999999999.30",
|
|
);
|
|
for (const invalid of ["NaN", "1e3", "-1", "1.234"])
|
|
assert.throws(() => money(invalid));
|
|
});
|
|
test("write schemas reject unsafe input before requests", () => {
|
|
assert.equal(
|
|
variantInput.safeParse({
|
|
sku: "A",
|
|
name: "Candle",
|
|
price: "10.5",
|
|
currency: "INR",
|
|
active: true,
|
|
attributes: {},
|
|
}).success,
|
|
false,
|
|
);
|
|
assert.equal(
|
|
addressInput.safeParse({
|
|
recipient: "A",
|
|
line1: "Street",
|
|
line2: "",
|
|
city: "City",
|
|
region: "State",
|
|
postalCode: "100000",
|
|
countryCode: "IN",
|
|
phone: "not-phone",
|
|
isDefault: false,
|
|
}).success,
|
|
false,
|
|
);
|
|
assert.equal(
|
|
checkoutInput.safeParse({
|
|
cartVersion: 0,
|
|
addressId: id,
|
|
idempotencyKey: id,
|
|
organizationId: id,
|
|
}).success,
|
|
false,
|
|
);
|
|
});
|
|
test("checkout intent normalizes coupon and freezes identical retry payload", () => {
|
|
const intent = checkoutIntent(
|
|
{ cartVersion: 3, addressId: id, couponCode: " save10 " },
|
|
id,
|
|
);
|
|
assert.deepEqual(intent, {
|
|
cartVersion: 3,
|
|
addressId: id,
|
|
couponCode: "SAVE10",
|
|
idempotencyKey: id,
|
|
});
|
|
assert.equal(Object.isFrozen(intent), true);
|
|
});
|
|
test("coupon date, currency and usage rules are enforced", () => {
|
|
const valid = {
|
|
code: "SAVE",
|
|
currency: "INR",
|
|
minimumSubtotal: "0.00",
|
|
kind: "PERCENT",
|
|
percentBps: 1000,
|
|
maxUses: 5,
|
|
perUserLimit: 1,
|
|
startsAt: "2026-10-12T00:00:00+05:30",
|
|
endsAt: "2026-10-13T00:00:00+05:30",
|
|
};
|
|
assert.equal(couponInput.safeParse(valid).success, true);
|
|
for (const change of [
|
|
{ percentBps: 10001 },
|
|
{ perUserLimit: 6 },
|
|
{ endsAt: valid.startsAt },
|
|
{ currency: "XXX" },
|
|
])
|
|
assert.equal(couponInput.safeParse({ ...valid, ...change }).success, false);
|
|
});
|