g1t/apps/sudo/app/lib/ledgers.test.ts
Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| sudo: Invoices, Audit log, and follow-ups due | 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; | |
| 3 | ||
| 4 | import { | |
| 5 | accountName, | |
| 6 | accountPath, | |
| 7 | actionLabel, | |
| 8 | auditHref, | |
| 9 | invoiceTotals, | |
| 10 | invoicesHref, | |
| 11 | olderBefore, | |
| 12 | parseAction, | |
| 13 | parseBefore, | |
| 14 | parseBy, | |
| 15 | parseInvoiceStatus, | |
| 16 | parseMonth, | |
| 17 | safeUrl, | |
| 18 | } from "./ledgers.ts"; | |
| 19 | ||
| 20 | const M = 1_000_000; | |
| 21 | ||
| 22 | test("invoice filters are checked", () => { | |
| 23 | assert.equal(parseInvoiceStatus("overdue"), "overdue"); | |
| 24 | assert.equal(parseInvoiceStatus("late"), null); | |
| 25 | assert.equal(parseInvoiceStatus(null), null); | |
| 26 | assert.equal(parseMonth("2026-10"), "2026-10"); | |
| 27 | assert.equal(parseMonth("2026-13"), null); | |
| 28 | assert.equal(parseMonth("2026-1"), null); | |
| 29 | assert.equal(parseMonth(""), null); | |
| 30 | }); | |
| 31 | ||
| 32 | test("totals: void counts for nothing; open, overdue and failed are outstanding", () => { | |
| 33 | const totals = invoiceTotals([ | |
| 34 | { amountMicros: 100 * M, status: "paid" }, | |
| 35 | { amountMicros: 50 * M, status: "open" }, | |
| 36 | { amountMicros: 20 * M, status: "failed" }, | |
| 37 | { amountMicros: 5 * M, status: "overdue" }, | |
| 38 | { amountMicros: 999 * M, status: "void" }, | |
| 39 | ]); | |
| 40 | assert.deepEqual(totals, { count: 5, amountMicros: 175 * M, paidMicros: 100 * M, outstandingMicros: 75 * M, outstanding: 3 }); | |
| 41 | assert.deepEqual(invoiceTotals([]), { count: 0, amountMicros: 0, paidMicros: 0, outstandingMicros: 0, outstanding: 0 }); | |
| 42 | }); | |
| 43 | ||
| 44 | test("links keep only the filters that are set", () => { | |
| 45 | assert.equal(invoicesHref({}), "/invoices"); | |
| 46 | assert.equal(invoicesHref({ status: "open", month: "2026-10" }), "/invoices?status=open&month=2026-10"); | |
| 47 | assert.equal(auditHref({}), "/audit"); | |
| 48 | assert.equal(auditHref({ by: "me@g1t.sh", before: "2026-10-01T00:00:00Z" }), "/audit?by=me%40g1t.sh&before=2026-10-01T00%3A00%3A00Z"); | |
| 49 | }); | |
| 50 | ||
| 51 | test("only https links are followed", () => { | |
| 52 | assert.equal(safeUrl("https://invoice.stripe.com/i/x"), "https://invoice.stripe.com/i/x"); | |
| 53 | assert.equal(safeUrl("javascript:alert(1)"), null); | |
| 54 | assert.equal(safeUrl("http://invoice.stripe.com"), null); | |
| 55 | assert.equal(safeUrl(null), null); | |
| 56 | }); | |
| 57 | ||
| 58 | test("account ids link to their pages; anything else does not", () => { | |
| 59 | assert.equal(accountPath("ws_acme"), "/workspaces/acme"); | |
| 60 | assert.equal(accountPath("ent_bigco"), "/enterprises/ent_bigco"); | |
| 61 | assert.equal(accountPath("stripe"), null); | |
| 62 | assert.equal(accountPath("acme"), null); | |
| 63 | assert.equal(accountPath("ws_-bad"), null); | |
| 64 | assert.equal(accountPath(""), null); | |
| 65 | assert.equal(accountPath(null), null); | |
| 66 | }); | |
| 67 | ||
| 68 | test("accounts are named by slug, or the enterprise's name when known", () => { | |
| 69 | assert.equal(accountName("ws_acme"), "acme"); | |
| 70 | assert.equal(accountName("ent_bigco", new Map([["ent_bigco", "BigCo Inc"]])), "BigCo Inc"); | |
| 71 | assert.equal(accountName("ent_bigco"), "an enterprise"); | |
| 72 | assert.equal(accountName("stripe"), "stripe"); | |
| 73 | }); | |
| 74 | ||
| 75 | test("actions read plainly, known or not", () => { | |
| 76 | assert.equal(actionLabel("credit"), "Credit issued"); | |
| 77 | assert.equal(actionLabel("stripe"), "From Stripe"); | |
| 78 | assert.equal(actionLabel("webhook"), "Stripe webhook registered"); | |
| 79 | assert.equal(actionLabel("price_change"), "Price change"); | |
| 80 | }); | |
| 81 | ||
| 82 | test("an older page only when this one was full", () => { | |
| 83 | const page = Array.from({ length: 100 }, (_, index) => ({ createdAt: `2026-10-01T00:00:${String(index % 60).padStart(2, "0")}Z` })); | |
| 84 | assert.equal(olderBefore(page), page[99].createdAt); | |
| 85 | assert.equal(olderBefore(page.slice(0, 99)), null); | |
| 86 | assert.equal(olderBefore([]), null); | |
| 87 | }); | |
| 88 | ||
| 89 | test("audit filters are checked", () => { | |
| 90 | assert.equal(parseBy(" Me@G1t.sh "), "me@g1t.sh"); | |
| 91 | assert.equal(parseBy("two words"), null); | |
| 92 | assert.equal(parseBy(""), null); | |
| 93 | assert.equal(parseAction("billing_link"), "billing_link"); | |
| 94 | assert.equal(parseAction("drop table"), null); | |
| 95 | assert.equal(parseBefore("2026-10-01T00:00:00Z"), "2026-10-01T00:00:00Z"); | |
| 96 | assert.equal(parseBefore("yesterday"), null); | |
| 97 | }); |