g1t/apps/sudo/app/lib/workspaces.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.
| Billing on Stripe's pages, month-end charges, warnings; sudo by workspace | 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; | |
| 3 | ||
| 4 | import type { AccountSummary, AdminWorkspace, Limit, Terms } from "@g1t/contracts"; | |
| 5 | ||
| Stripe webhooks, enterprise invoices, and sudo for both | 6 | import { STANDARD_TERMS, joinWorkspaces, legacyAccountPath, paginate } from "./workspaces.ts"; |
| Billing on Stripe's pages, month-end charges, warnings; sudo by workspace | 7 | |
| 8 | function limit(workspace: string, account: string, exposureMicros = 0): Limit { | |
| 9 | return { | |
| 10 | workspace, | |
| 11 | account, | |
| 12 | accountName: account, | |
| 13 | trust: "new", | |
| 14 | exposureMicros, | |
| 15 | ceilingMicros: 10_000_000, | |
| 16 | trustCeilingMicros: 10_000_000, | |
| 17 | spendLimitMicros: null, | |
| 18 | state: "ok", | |
| 19 | message: null, | |
| 20 | }; | |
| 21 | } | |
| 22 | ||
| 23 | function workspace(slug: string, owner = `${slug}-owner`): AdminWorkspace { | |
| 24 | return { | |
| 25 | slug, | |
| 26 | name: slug.toUpperCase(), | |
| 27 | createdAt: "2026-10-01T00:00:00Z", | |
| 28 | owners: [{ username: owner, email: `${owner}@example.com` }], | |
| 29 | memberCount: 1, | |
| 30 | }; | |
| 31 | } | |
| 32 | ||
| 33 | const COMPED: Terms = { ...STANDARD_TERMS, kind: "comped", note: "g1t's own" }; | |
| 34 | ||
| 35 | const OWN: AccountSummary = { | |
| 36 | account: { id: "ws_syntaqx", kind: "workspace", name: "syntaqx", terms: COMPED, workspaces: ["syntaqx"], createdAt: "2026-10-05T00:00:00Z" }, | |
| 37 | limit: limit("syntaqx", "ws_syntaqx", 3_000_000), | |
| 38 | chargedMicros: 0, | |
| 39 | costMicros: 3_000_000, | |
| 40 | paidMicros: 0, | |
| 41 | byWorkspace: [{ workspace: "syntaqx", chargedMicros: 0, costMicros: 3_000_000, paidMicros: 0 }], | |
| 42 | }; | |
| 43 | ||
| 44 | const ACME: AccountSummary = { | |
| 45 | account: { id: "ent_acme", kind: "enterprise", name: "Acme Corp", terms: STANDARD_TERMS, workspaces: ["acme", "acme-labs"], createdAt: "2026-10-02T00:00:00Z" }, | |
| 46 | limit: limit("acme", "ent_acme", 9_000_000), | |
| 47 | chargedMicros: 12_000_000, | |
| 48 | costMicros: 6_000_000, | |
| 49 | paidMicros: 0, | |
| 50 | byWorkspace: [ | |
| 51 | { workspace: "acme", chargedMicros: 12_000_000, costMicros: 6_000_000, paidMicros: 0 }, | |
| 52 | { workspace: "acme-labs", chargedMicros: 0, costMicros: 0, paidMicros: 0 }, | |
| 53 | ], | |
| 54 | }; | |
| 55 | ||
| 56 | test("every workspace is listed, with or without billing", () => { | |
| 57 | const rows = joinWorkspaces([workspace("syntaqx"), workspace("quiet")], [OWN]); | |
| 58 | assert.deepEqual( | |
| 59 | rows.map((row) => row.slug), | |
| 60 | ["syntaqx", "quiet"], | |
| 61 | ); | |
| 62 | const [syntaqx, quiet] = rows; | |
| 63 | assert.equal(syntaqx.billing.terms.kind, "comped"); | |
| 64 | assert.equal(syntaqx.billing.costMicros, 3_000_000); | |
| 65 | assert.equal(syntaqx.billing.billedTo, null); | |
| 66 | assert.equal(quiet.billing.terms.kind, "standard"); | |
| 67 | assert.equal(quiet.billing.limit, null); | |
| 68 | assert.equal(quiet.billing.chargedMicros, 0); | |
| 69 | }); | |
| 70 | ||
| 71 | test("a workspace on an enterprise shows the enterprise and only its own share", () => { | |
| 72 | const rows = joinWorkspaces([workspace("acme"), workspace("acme-labs")], [ACME]); | |
| 73 | const labs = rows.find((row) => row.slug === "acme-labs"); | |
| 74 | assert.deepEqual(labs?.billing.billedTo, { id: "ent_acme", name: "Acme Corp" }); | |
| 75 | assert.equal(labs?.billing.chargedMicros, 0); | |
| 76 | assert.equal(labs?.billing.limit?.exposureMicros, 9_000_000); | |
| 77 | assert.equal(rows.find((row) => row.slug === "acme")?.billing.chargedMicros, 12_000_000); | |
| 78 | }); | |
| 79 | ||
| Stripe webhooks, enterprise invoices, and sudo for both | 80 | test("a page lists only its own workspaces, not the rest of an enterprise", () => { |
| 81 | const rows = joinWorkspaces([workspace("acme")], [ACME]); | |
| Billing on Stripe's pages, month-end charges, warnings; sudo by workspace | 82 | assert.deepEqual( |
| Stripe webhooks, enterprise invoices, and sudo for both | 83 | rows.map((row) => row.slug), |
| 84 | ["acme"], | |
| Billing on Stripe's pages, month-end charges, warnings; sudo by workspace | 85 | ); |
| 86 | }); | |
| 87 | ||
| Stripe webhooks, enterprise invoices, and sudo for both | 88 | test("pages stay within the list", () => { |
| 89 | const items = Array.from({ length: 120 }, (_, i) => i); | |
| 90 | assert.deepEqual(paginate(items, null).items.slice(0, 2), [0, 1]); | |
| 91 | const third = paginate(items, "3"); | |
| 92 | assert.equal(third.page, 3); | |
| 93 | assert.equal(third.pages, 3); | |
| 94 | assert.equal(third.items.length, 20); | |
| 95 | assert.equal(paginate(items, "99").page, 3); | |
| 96 | assert.equal(paginate(items, "-4").page, 1); | |
| 97 | assert.equal(paginate(items, "abc").page, 1); | |
| 98 | assert.deepEqual(paginate([], "2"), { page: 1, pages: 1, items: [] }); | |
| Billing on Stripe's pages, month-end charges, warnings; sudo by workspace | 99 | }); |
| 100 | ||
| 101 | test("old account links go to the workspace or the enterprise", () => { | |
| 102 | assert.equal(legacyAccountPath("ws_syntaqx"), "/workspaces/syntaqx"); | |
| 103 | assert.equal(legacyAccountPath("syntaqx"), "/workspaces/syntaqx"); | |
| 104 | assert.equal(legacyAccountPath("ent_01abc"), "/enterprises/ent_01abc"); | |
| 105 | assert.equal(legacyAccountPath("ws_"), null); | |
| 106 | assert.equal(legacyAccountPath("../etc"), null); | |
| 107 | }); |