flagon-io/g1t

public

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t/apps/sudo/app/lib/forms.test.ts

181 lines9,057 bytesCodeBlame
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import {
5 parseAllowances,
6 parseCredit,
7 parseDecision,
8 parseEmail,
9 parseGoodwill,
10 parsePayment,
11 parseSales,
12 parseSalesNote,
13 parseSlugList,
14 parseTerms,
15} from "./forms.ts";
16
17const NOW = new Date("2026-10-04T12:00:00Z");
18
19function form(entries: Record<string, string>): FormData {
20 const data = new FormData();
21 for (const [name, value] of Object.entries(entries)) data.set(name, value);
22 return data;
23}
24
25test("comped terms keep a ceiling and an end date, and name who set them", () => {
26 const result = parseTerms(form({ kind: "comped", note: "g1t's own", ceiling: "500", until: "2026-12-31", discount: "40" }), "owner@g1t.sh", NOW);
27 assert.deepEqual(result, {
28 ok: true,
29 value: {
30 kind: "comped",
31 discountPercent: 0,
32 ceilingMicros: 500_000_000,
33 note: "g1t's own",
34 until: "2026-12-31T23:59:59Z",
35 setBy: "owner@g1t.sh",
36 setAt: NOW.toISOString(),
37 },
38 });
39});
40
41test("standard terms clear everything but the note", () => {
42 const result = parseTerms(form({ kind: "standard", note: "back to normal", ceiling: "5", until: "2027-01-01" }), "a@g1t.sh", NOW);
43 assert.ok(result.ok);
44 assert.equal(result.value.ceilingMicros, null);
45 assert.equal(result.value.until, null);
46});
47
48test("terms are refused without a note, with a bad discount, or ending in the past", () => {
49 assert.equal(parseTerms(form({ kind: "comped", note: "" }), "a", NOW).ok, false);
50 assert.equal(parseTerms(form({ kind: "custom", note: "x", discount: "101" }), "a", NOW).ok, false);
51 assert.equal(parseTerms(form({ kind: "custom", note: "x" }), "a", NOW).ok, false);
52 assert.equal(parseTerms(form({ kind: "custom", note: "x", discount: "20", until: "2026-10-01" }), "a", NOW).ok, false);
53 assert.equal(parseTerms(form({ kind: "custom", note: "x", discount: "20", until: "2026-02-30" }), "a", NOW).ok, false);
54 assert.equal(parseTerms(form({ kind: "free", note: "x" }), "a", NOW).ok, false);
55});
56
57test("workspace lists take commas, spaces and lines, once each", () => {
58 assert.deepEqual(parseSlugList("acme, Acme-labs\nbeta beta"), { ok: true, value: ["acme", "acme-labs", "beta"] });
59 assert.equal(parseSlugList("acme, bad--slug").ok, false);
60 assert.equal(parseSlugList("../etc").ok, false);
61});
62
63test("invoice emails are one address", () => {
64 assert.deepEqual(parseEmail(" Billing@Acme.com "), { ok: true, value: "billing@acme.com" });
65 assert.equal(parseEmail("a@b").ok, false);
66 assert.equal(parseEmail("a@b.com, c@d.com").ok, false);
67 assert.equal(parseEmail("a@@b.com").ok, false);
68 assert.equal(parseEmail("").ok, false);
69});
70
71test("credits are positive and capped", () => {
72 assert.deepEqual(parseCredit("25.50"), { ok: true, value: 25_500_000 });
73 assert.equal(parseCredit("0").ok, false);
74 assert.equal(parseCredit("10000.01").ok, false);
75});
76
77test("a sales record: a stage, an owner's email or nobody, a next step and its day", () => {
78 assert.deepEqual(parseSales(form({ stage: "contacted", owner: " Me@G1t.sh ", nextStep: "Call about\n terms", nextAt: "2026-10-09" })), {
79 ok: true,
80 value: { stage: "contacted", owner: "me@g1t.sh", nextStep: "Call about terms", nextAt: "2026-10-09" },
81 });
82 assert.deepEqual(parseSales(form({ stage: "none", owner: "", nextStep: "", nextAt: "" })), {
83 ok: true,
84 value: { stage: "none", owner: null, nextStep: null, nextAt: null },
85 });
86 assert.equal(parseSales(form({ stage: "closed" })).ok, false);
87 assert.equal(parseSales(form({ stage: "lead", owner: "not an email" })).ok, false);
88 assert.equal(parseSales(form({ stage: "lead", nextStep: "x".repeat(201) })).ok, false);
89 assert.equal(parseSales(form({ stage: "lead", nextStep: "Call", nextAt: "2026-02-30" })).ok, false);
90 assert.equal(parseSales(form({ stage: "lead", nextStep: "Call", nextAt: "next week" })).ok, false);
91 // A date needs a step to go with it.
92 assert.equal(parseSales(form({ stage: "lead", nextAt: "2026-10-09" })).ok, false);
93});
94
95test("a sales note is required, and not endless", () => {
96 assert.deepEqual(parseSalesNote("Spoke to Ana; wants an enterprise quote."), { ok: true, value: "Spoke to Ana; wants an enterprise quote." });
97 assert.equal(parseSalesNote("").ok, false);
98 assert.equal(parseSalesNote("x".repeat(2001)).ok, false);
99});
100
101
102test("allowances: the plan without its price, pool shares, and staff's overrides", () => {
103 assert.deepEqual(parseAllowances(form({})), {
104 ok: true,
105 value: { plan: false, ossRepoMicros: null, trialMicros: null, maxConcurrentAgents: null, runCapMicros: null, issueCapMicros: null, hold: null },
106 });
107 assert.deepEqual(
108 parseAllowances(form({ plan: "on", oss: "5", trial: "2.50", agents: "20", runCap: "25", issueCap: "500", hold: " Card disputed;\nwaiting on the bank " })),
109 {
110 ok: true,
111 value: {
112 plan: true,
113 ossRepoMicros: 5_000_000,
114 trialMicros: 2_500_000,
115 maxConcurrentAgents: 20,
116 runCapMicros: 25_000_000,
117 issueCapMicros: 500_000_000,
118 hold: "Card disputed; waiting on the bank",
119 },
120 },
121 );
122 assert.equal(parseAllowances(form({ oss: "lots" })).ok, false);
123 assert.equal(parseAllowances(form({ trial: "5000" })).ok, false);
124 assert.equal(parseAllowances(form({ agents: "0" })).ok, false);
125 assert.equal(parseAllowances(form({ agents: "101" })).ok, false);
126 assert.equal(parseAllowances(form({ agents: "2.5" })).ok, false);
127 assert.equal(parseAllowances(form({ runCap: "0.05" })).ok, false);
128 assert.equal(parseAllowances(form({ runCap: "1000.01" })).ok, false);
129 assert.equal(parseAllowances(form({ issueCap: "10000.01" })).ok, false);
130 assert.equal(parseAllowances(form({ hold: "x".repeat(201) })).ok, false);
131});
132
133test("a bank transfer: an amount, its reference, a note, and the slug typed out", () => {
134 const ok = { amount: "1,500", reference: " TRX 0042 ", note: "Invoice INV-7, by wire", confirmation: "acme" };
135 assert.deepEqual(parsePayment(form(ok), "acme"), { ok: true, value: { amountMicros: 1_500_000_000, reference: "TRX 0042", note: "Invoice INV-7, by wire" } });
136 assert.equal(parsePayment(form({ ...ok, amount: "0" }), "acme").ok, false);
137 assert.equal(parsePayment(form({ ...ok, amount: "100000.01" }), "acme").ok, false);
138 assert.equal(parsePayment(form({ ...ok, reference: "" }), "acme").ok, false);
139 assert.equal(parsePayment(form({ ...ok, note: "" }), "acme").ok, false);
140 assert.equal(parsePayment(form({ ...ok, confirmation: "Acme" }), "acme").ok, false);
141});
142
143test("a decision: approve as asked or at another amount; decline needs a note", () => {
144 assert.deepEqual(parseDecision(form({ id: "lr_1", decision: "approve" })), {
145 ok: true,
146 value: { id: "lr_1", decision: "approve", amountMicros: null, note: "" },
147 });
148 assert.deepEqual(parseDecision(form({ id: "lr_1", decision: "approve", amount: "750", note: "Half now." })), {
149 ok: true,
150 value: { id: "lr_1", decision: "approve", amountMicros: 750_000_000, note: "Half now." },
151 });
152 assert.equal(parseDecision(form({ id: "lr_1", decision: "approve", amount: "0.50" })).ok, false);
153 assert.equal(parseDecision(form({ id: "lr_1", decision: "approve", amount: "1000000.01" })).ok, false);
154 assert.equal(parseDecision(form({ id: "lr_1", decision: "decline" })).ok, false);
155 assert.deepEqual(parseDecision(form({ id: "lr_1", decision: "decline", note: "After a month of payments.", amount: "5" })), {
156 ok: true,
157 value: { id: "lr_1", decision: "decline", amountMicros: null, note: "After a month of payments." },
158 });
159 assert.equal(parseDecision(form({ id: "../x", decision: "approve" })).ok, false);
160 assert.equal(parseDecision(form({ id: "lr_1", decision: "maybe" })).ok, false);
161});
162
163test("goodwill: no amount is the one-click credit; a reason when one is needed; a day not in the future", () => {
164 const never = () => false;
165 const always = () => true;
166 assert.deepEqual(parseGoodwill(form({}), never, NOW), { ok: true, value: { amountMicros: null, reason: "", day: null } });
167 assert.deepEqual(parseGoodwill(form({ amount: "60", reason: "Looping agent on #12, owner caught it late.", day: "2026-10-03" }), always, NOW), {
168 ok: true,
169 value: { amountMicros: 60_000_000, reason: "Looping agent on #12, owner caught it late.", day: "2026-10-03" },
170 });
171 assert.equal(parseGoodwill(form({ amount: "60", reason: "oops" }), always, NOW).ok, false);
172 assert.equal(parseGoodwill(form({ amount: "0" }), never, NOW).ok, false);
173 assert.equal(parseGoodwill(form({ amount: "10000.01" }), never, NOW).ok, false);
174 assert.equal(parseGoodwill(form({ day: "2026-10-05" }), never, NOW).ok, false);
175 assert.equal(parseGoodwill(form({ day: "2026-02-30" }), never, NOW).ok, false);
176 // The check is given the amount typed, or null for the one-click credit.
177 const seen: (number | null)[] = [];
178 parseGoodwill(form({ amount: "12.50" }), (amount) => (seen.push(amount), false), NOW);
179 parseGoodwill(form({}), (amount) => (seen.push(amount), false), NOW);
180 assert.deepEqual(seen, [12_500_000, null]);
181});