Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

JavaScript · 100 lines · 4.4 KiB · at 9472e3c

import { test } from "node:test";import assert from "node:assert/strict";import { h, load } from "../../lib/harness.mjs";const { cartTotal } = await load();// Build a coupon table in the solution's language (minSubtotalCents is optional).const fixed = (amountCents, minSubtotalCents) => ({ kind: "fixed", amountCents, minSubtotalCents: h.maybe(minSubtotalCents) });const percent = (p) => ({ kind: "percent", percent: p });const coupons = () => ({  save10: percent(10),  free: percent(100),  zero: percent(0),  five: fixed(500),  big: fixed(1000, 5000),  minzero: fixed(200, 0),});const total = (items, code, table = coupons()) => h.res(() => cartTotal(items, h.maybe(code), table));const ok = (subtotalCents, discountCents) => ({ ok: true, value: { subtotalCents, discountCents, totalCents: subtotalCents - discountCents } });const errOf = (r, keys) => {  if (r.ok !== false || !r.error) return r;  const out = { kind: r.error.kind };  for (const k of keys) out[k] = r.error[k];  return out;};const item = (unitPriceCents, quantity, sku = "x") => ({ sku, unitPriceCents, quantity });test("no coupon", () => {  assert.deepEqual(total([item(250, 2), item(100, 3)], undefined), ok(800, 0));});test("percent coupon rounds down", () => {  assert.deepEqual(total([item(1999, 1)], "save10"), ok(1999, 199));  assert.deepEqual(total([item(5, 1)], "save10"), ok(5, 0));  assert.deepEqual(total([item(1234, 2)], "free"), ok(2468, 2468));});test("a 0 percent coupon is a valid coupon", () => {  assert.deepEqual(total([item(1000, 1)], "zero"), ok(1000, 0));});test("fixed coupon is capped at the subtotal", () => {  assert.deepEqual(total([item(300, 1)], "five"), ok(300, 300));  assert.deepEqual(total([item(300, 3)], "five"), ok(900, 500));});test("fixed coupon minimum subtotal", () => {  assert.deepEqual(errOf(total([item(4999, 1)], "big"), ["code"]), { kind: "coupon_not_applicable", code: "big" });  assert.deepEqual(total([item(5000, 1)], "big"), ok(5000, 1000));  assert.deepEqual(total([item(0, 1)], "minzero"), ok(0, 0));  assert.deepEqual(errOf(total([], "big"), ["code"]), { kind: "coupon_not_applicable", code: "big" });});test("code lookup trims and ignores case; errors echo the code as passed", () => {  assert.deepEqual(total([item(1000, 1)], "  SAVE10 "), ok(1000, 100));  assert.deepEqual(total([item(1000, 1)], "Five"), ok(1000, 500));  assert.deepEqual(errOf(total([item(10, 1)], " Big "), ["code"]), { kind: "coupon_not_applicable", code: " Big " });  assert.deepEqual(errOf(total([item(10, 1)], " Nope"), ["code"]), { kind: "unknown_coupon", code: " Nope" });});test("empty or blank code means no coupon", () => {  assert.deepEqual(total([item(1000, 1)], ""), ok(1000, 0));  assert.deepEqual(total([item(1000, 1)], "   "), ok(1000, 0));  assert.deepEqual(total([item(1000, 1)], "\t"), ok(1000, 0));});test("only codes defined in the table are valid", () => {  for (const code of ["toString", "__proto__", "constructor", "hasOwnProperty", "valueOf"]) {    assert.deepEqual(errOf(total([item(1000, 1)], code), ["code"]), { kind: "unknown_coupon", code }, code);  }});test("invalid items report the first bad index", () => {  const bad = [-1, 1.5, "2", NaN, null, undefined, Infinity];  for (const q of bad) {    assert.deepEqual(errOf(total([item(100, 1), item(100, q)], undefined), ["index"]), { kind: "invalid_item", index: 1 }, String(q));    assert.deepEqual(errOf(total([item(q, 1), item(100, 1)], undefined), ["index"]), { kind: "invalid_item", index: 0 }, String(q));  }  assert.deepEqual(errOf(total([item(1, 1), item(-5, 1), item(1, -1)], undefined), ["index"]), { kind: "invalid_item", index: 1 });});test("zero prices and quantities are valid", () => {  assert.deepEqual(total([item(0, 5), item(250, 0)], undefined), ok(0, 0));  assert.deepEqual(total([item(0, 5), item(250, 0)], "save10"), ok(0, 0));  assert.deepEqual(total([], undefined), ok(0, 0));  assert.deepEqual(total([], "five"), ok(0, 0));});test("items are validated before the coupon", () => {  assert.deepEqual(errOf(total([item(100, -1)], "nope"), ["index"]), { kind: "invalid_item", index: 0 });});test("inputs are not modified", () => {  const items = [item(250, 2, "a"), item(100, 1, "b")];  const table = coupons();  const before = JSON.stringify([items, Object.keys(table)]);  total(items, " SAVE10 ", table);  total(items, "five", table);  assert.equal(JSON.stringify([items, Object.keys(table)]), before);});