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

56 lines2,429 bytesCodeBlame
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { parseCredit, parseSlugList, parseTerms } from "./forms.ts";
5
6const NOW = new Date("2026-10-04T12:00:00Z");
7
8function 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
14test("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
30test("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
37test("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
46test("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
52test("credits are positive and capped", () => {
53 assert.deepEqual(parseCredit("25.50"), { ok: true, value: 25_500_000 });
54 assert.equal(parseCredit("0").ok, false);
55 assert.equal(parseCredit("10000.01").ok, false);
56});