g1t/apps/sudo/app/lib/forms.test.ts
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { parseCredit, parseEmail, parseSales, parseSalesNote, parseSlugList, parseTerms } from "./forms.ts"; |
| 5 | |
| 6 | const NOW = new Date("2026-10-04T12:00:00Z"); |
| 7 | |
| 8 | function form(entries: Record<string, string>): FormData { |
| 9 | const data = new FormData(); |
| 10 | for (const [name, value] of Object.entries(entries)) data.set(name, value); |
| 11 | return data; |
| 12 | } |
| 13 | |
| 14 | test("comped terms keep a ceiling and an end date, and name who set them", () => { |
| 15 | const result = parseTerms(form({ kind: "comped", note: "g1t's own", ceiling: "500", until: "2026-12-31", discount: "40" }), "owner@g1t.sh", NOW); |
| 16 | assert.deepEqual(result, { |
| 17 | ok: true, |
| 18 | value: { |
| 19 | kind: "comped", |
| 20 | discountPercent: 0, |
| 21 | ceilingMicros: 500_000_000, |
| 22 | note: "g1t's own", |
| 23 | until: "2026-12-31T23:59:59Z", |
| 24 | setBy: "owner@g1t.sh", |
| 25 | setAt: NOW.toISOString(), |
| 26 | }, |
| 27 | }); |
| 28 | }); |
| 29 | |
| 30 | test("standard terms clear everything but the note", () => { |
| 31 | const result = parseTerms(form({ kind: "standard", note: "back to normal", ceiling: "5", until: "2027-01-01" }), "a@g1t.sh", NOW); |
| 32 | assert.ok(result.ok); |
| 33 | assert.equal(result.value.ceilingMicros, null); |
| 34 | assert.equal(result.value.until, null); |
| 35 | }); |
| 36 | |
| 37 | test("terms are refused without a note, with a bad discount, or ending in the past", () => { |
| 38 | assert.equal(parseTerms(form({ kind: "comped", note: "" }), "a", NOW).ok, false); |
| 39 | assert.equal(parseTerms(form({ kind: "custom", note: "x", discount: "101" }), "a", NOW).ok, false); |
| 40 | assert.equal(parseTerms(form({ kind: "custom", note: "x" }), "a", NOW).ok, false); |
| 41 | assert.equal(parseTerms(form({ kind: "custom", note: "x", discount: "20", until: "2026-10-01" }), "a", NOW).ok, false); |
| 42 | assert.equal(parseTerms(form({ kind: "custom", note: "x", discount: "20", until: "2026-02-30" }), "a", NOW).ok, false); |
| 43 | assert.equal(parseTerms(form({ kind: "free", note: "x" }), "a", NOW).ok, false); |
| 44 | }); |
| 45 | |
| 46 | test("workspace lists take commas, spaces and lines, once each", () => { |
| 47 | assert.deepEqual(parseSlugList("acme, Acme-labs\nbeta beta"), { ok: true, value: ["acme", "acme-labs", "beta"] }); |
| 48 | assert.equal(parseSlugList("acme, bad--slug").ok, false); |
| 49 | assert.equal(parseSlugList("../etc").ok, false); |
| 50 | }); |
| 51 | |
| 52 | test("invoice emails are one address", () => { |
| 53 | assert.deepEqual(parseEmail(" Billing@Acme.com "), { ok: true, value: "billing@acme.com" }); |
| 54 | assert.equal(parseEmail("a@b").ok, false); |
| 55 | assert.equal(parseEmail("a@b.com, c@d.com").ok, false); |
| 56 | assert.equal(parseEmail("a@@b.com").ok, false); |
| 57 | assert.equal(parseEmail("").ok, false); |
| 58 | }); |
| 59 | |
| 60 | test("credits are positive and capped", () => { |
| 61 | assert.deepEqual(parseCredit("25.50"), { ok: true, value: 25_500_000 }); |
| 62 | assert.equal(parseCredit("0").ok, false); |
| 63 | assert.equal(parseCredit("10000.01").ok, false); |
| 64 | }); |
| 65 | |
| 66 | test("a sales record: a stage, an owner's email or nobody, a next step and its day", () => { |
| 67 | assert.deepEqual(parseSales(form({ stage: "contacted", owner: " Me@G1t.sh ", nextStep: "Call about\n terms", nextAt: "2026-10-09" })), { |
| 68 | ok: true, |
| 69 | value: { stage: "contacted", owner: "me@g1t.sh", nextStep: "Call about terms", nextAt: "2026-10-09" }, |
| 70 | }); |
| 71 | assert.deepEqual(parseSales(form({ stage: "none", owner: "", nextStep: "", nextAt: "" })), { |
| 72 | ok: true, |
| 73 | value: { stage: "none", owner: null, nextStep: null, nextAt: null }, |
| 74 | }); |
| 75 | assert.equal(parseSales(form({ stage: "closed" })).ok, false); |
| 76 | assert.equal(parseSales(form({ stage: "lead", owner: "not an email" })).ok, false); |
| 77 | assert.equal(parseSales(form({ stage: "lead", nextStep: "x".repeat(201) })).ok, false); |
| 78 | assert.equal(parseSales(form({ stage: "lead", nextStep: "Call", nextAt: "2026-02-30" })).ok, false); |
| 79 | assert.equal(parseSales(form({ stage: "lead", nextStep: "Call", nextAt: "next week" })).ok, false); |
| 80 | // A date needs a step to go with it. |
| 81 | assert.equal(parseSales(form({ stage: "lead", nextAt: "2026-10-09" })).ok, false); |
| 82 | }); |
| 83 | |
| 84 | test("a sales note is required, and not endless", () => { |
| 85 | assert.deepEqual(parseSalesNote("Spoke to Ana; wants an enterprise quote."), { ok: true, value: "Spoke to Ana; wants an enterprise quote." }); |
| 86 | assert.equal(parseSalesNote("").ok, false); |
| 87 | assert.equal(parseSalesNote("x".repeat(2001)).ok, false); |
| 88 | }); |