g1t/apps/sudo/app/lib/workspaces.test.ts
| 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 | |
| 6 | import { STANDARD_TERMS, joinWorkspaces, legacyAccountPath, matchesQuery } from "./workspaces.ts"; |
| 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 | |
| 80 | test("a slug only billing knows is still listed, marked unknown", () => { |
| 81 | const rows = joinWorkspaces([], [ACME]); |
| 82 | assert.deepEqual( |
| 83 | rows.map((row) => [row.slug, row.known]), |
| 84 | [ |
| 85 | ["acme", false], |
| 86 | ["acme-labs", false], |
| 87 | ], |
| 88 | ); |
| 89 | }); |
| 90 | |
| 91 | test("search covers slug, name, owners, their emails and the enterprise", () => { |
| 92 | const [row] = joinWorkspaces([workspace("acme-labs", "ada")], [ACME]); |
| 93 | assert.ok(matchesQuery(row, "LABS")); |
| 94 | assert.ok(matchesQuery(row, "ada@")); |
| 95 | assert.ok(matchesQuery(row, "acme corp")); |
| 96 | assert.ok(matchesQuery(row, "")); |
| 97 | assert.ok(!matchesQuery(row, "globex")); |
| 98 | }); |
| 99 | |
| 100 | test("old account links go to the workspace or the enterprise", () => { |
| 101 | assert.equal(legacyAccountPath("ws_syntaqx"), "/workspaces/syntaqx"); |
| 102 | assert.equal(legacyAccountPath("syntaqx"), "/workspaces/syntaqx"); |
| 103 | assert.equal(legacyAccountPath("ent_01abc"), "/enterprises/ent_01abc"); |
| 104 | assert.equal(legacyAccountPath("ws_"), null); |
| 105 | assert.equal(legacyAccountPath("../etc"), null); |
| 106 | }); |