{"draft":null,"from":33,"to":34,"files":[{"path":"client/test/orgs.test.ts","added":198,"removed":0,"diff":"--- /dev/null\n+++ b/client/test/orgs.test.ts\n@@ -0,0 +1,198 @@\n+// abso orgs and abso org: reads as the person who approved the box's sign-in, and the changes say where the\n+// website makes them. The server here is a stand-in that answers the org routes' documented shapes.\n+\n+import assert from \"node:assert/strict\";\n+import { mkdtempSync } from \"node:fs\";\n+import { tmpdir } from \"node:os\";\n+import { join } from \"node:path\";\n+import { describe, it } from \"node:test\";\n+import { parseArgs } from \"../src/args.ts\";\n+import { writeBox } from \"../src/box.ts\";\n+import { commandTable } from \"../src/main.ts\";\n+import { initInOrg, runOrg, runOrgs, siteOf } from \"../src/orgs.ts\";\n+\n+const SERVER = \"https://api.example.dev\";\n+const CREDENTIAL = \"abso_bx_test0000000000\";\n+\n+function signedIn(): string {\n+\tconst home = mkdtempSync(join(tmpdir(), \"abso-orgs-\"));\n+\twriteBox(\n+\t\t{\n+\t\t\tserver: SERVER,\n+\t\t\tid: \"box_1\",\n+\t\t\tcredential: CREDENTIAL,\n+\t\t\tgrant: { projects: [{ project: \"web\", role: \"writer\" }], max_agents: 3 },\n+\t\t\texpiresAt: Date.now() + 86_400_000,\n+\t\t},\n+\t\thome,\n+\t);\n+\treturn home;\n+}\n+\n+/** A stand-in server: answers from `routes` by path, and keeps what was asked. */\n+function server(routes: Record<string, { status: number; body: unknown }>) {\n+\tconst asked: { url: string; method: string; auth: string | null }[] = [];\n+\tconst fetcher = (async (url: string | URL | Request, init?: RequestInit) => {\n+\t\tconst u = new URL(String(url));\n+\t\tasked.push({\n+\t\t\turl: String(url),\n+\t\t\tmethod: init?.method ?? \"GET\",\n+\t\t\tauth: new Headers(init?.headers).get(\"authorization\"),\n+\t\t});\n+\t\tconst r = routes[u.pathname] ?? { status: 404, body: { error: \"no such route\" } };\n+\t\treturn new Response(JSON.stringify(r.body), { status: r.status });\n+\t}) as typeof fetch;\n+\treturn { fetcher, asked };\n+}\n+\n+const args = (line: string[]) => parseArgs(\"org\", line, commandTable().org!.flags);\n+\n+describe(\"abso orgs\", () => {\n+\tit(\"lists yours with your role, and invitations waiting, reading with the box's sign-in\", async () => {\n+\t\tconst home = signedIn();\n+\t\tconst s = server({\n+\t\t\t\"/orgs\": {\n+\t\t\t\tstatus: 200,\n+\t\t\t\tbody: {\n+\t\t\t\t\torgs: [\n+\t\t\t\t\t\t{ name: \"acme-labs\", displayName: \"Acme Labs\", role: \"owner\" },\n+\t\t\t\t\t\t{ name: \"beta\", displayName: \"beta\", role: \"member\" },\n+\t\t\t\t\t],\n+\t\t\t\t\tinvitations: [{ id: \"inv_1\", org: \"gamma\", role: \"member\", expiresAt: \"2026-10-16T10:00:00.000Z\" }],\n+\t\t\t\t},\n+\t\t\t},\n+\t\t});\n+\t\tconst out = await runOrgs(SERVER, { home, fetch: s.fetcher });\n+\t\tassert.equal(\n+\t\t\tout.text,\n+\t\t\t[\n+\t\t\t\t\"Your organizations (2):\",\n+\t\t\t\t\"  acme-labs (Acme Labs): owner\",\n+\t\t\t\t\"  beta: member\",\n+\t\t\t\t\"Invited to:\",\n+\t\t\t\t\"  gamma as member, until 2026-10-16 10:00 UTC\",\n+\t\t\t\t\"Accept them on the website, signed in: https://example.dev.\",\n+\t\t\t].join(\"\\n\"),\n+\t\t);\n+\t\tassert.deepEqual(s.asked, [{ url: `${SERVER}/orgs`, method: \"GET\", auth: `Box ${CREDENTIAL}` }]);\n+\t});\n+\n+\tit(\"says so when the box isn't signed in, the sign-in isn't taken, or orgs aren't on\", async () => {\n+\t\tconst empty = mkdtempSync(join(tmpdir(), \"abso-orgs-\"));\n+\t\tawait assert.rejects(runOrgs(SERVER, { home: empty, fetch: server({}).fetcher }), /abso login signs it in/);\n+\t\tconst home = signedIn();\n+\t\tconst expired = server({ \"/orgs\": { status: 401, body: { error: \"missing or wrong token\" } } });\n+\t\tawait assert.rejects(runOrgs(SERVER, { home, fetch: expired.fetcher }), /abso login renews it/);\n+\t\tconst off = server({ \"/orgs\": { status: 404, body: { error: \"organizations aren't on here yet\" } } });\n+\t\tawait assert.rejects(runOrgs(SERVER, { home, fetch: off.fetcher }), /api\\.example\\.dev: organizations aren't on/);\n+\t});\n+\n+\tit(\"never prints the credential, even when the server echoes it\", async () => {\n+\t\tconst home = signedIn();\n+\t\tconst s = server({ \"/orgs\": { status: 500, body: `oops ${CREDENTIAL}` } });\n+\t\tawait assert.rejects(runOrgs(SERVER, { home, fetch: s.fetcher }), (e: Error) => !e.message.includes(CREDENTIAL));\n+\t});\n+});\n+\n+describe(\"abso org <name>\", () => {\n+\tconst acme = {\n+\t\tname: \"acme-labs\",\n+\t\tdisplayName: \"Acme Labs\",\n+\t\tdescription: \"We make labs.\",\n+\t\trole: \"owner\",\n+\t\tmembersCan: \"write\",\n+\t\twhoMakesProjects: \"members\",\n+\t\tmembers: [\n+\t\t\t{ id: \"cf-1\", role: \"owner\", name: \"sam\", raised: [] },\n+\t\t\t{ id: \"cf-2\", role: \"member\", name: null, raised: [{ project: \"web\", role: \"owner\" }] },\n+\t\t],\n+\t\tprojects: [\"api\", \"web\"],\n+\t\tinvites: [{ id: \"inv_2\", role: \"member\", kind: \"link\", for: null, expiresAt: \"2026-10-16T10:00:00.000Z\" }],\n+\t};\n+\n+\tit(\"shows its projects, people, settings and open invites to a member, never a person's id\", async () => {\n+\t\tconst home = signedIn();\n+\t\tconst s = server({ \"/orgs/acme-labs\": { status: 200, body: acme } });\n+\t\tconst out = await runOrg(args([\"acme-labs\"]), SERVER, { home, fetch: s.fetcher });\n+\t\tassert.equal(\n+\t\t\tout.text,\n+\t\t\t[\n+\t\t\t\t\"acme-labs (Acme Labs)\",\n+\t\t\t\t\"  We make labs.\",\n+\t\t\t\t\"You're an owner. Members can write every project; members make projects.\",\n+\t\t\t\t\"Projects (2): api, web\",\n+\t\t\t\t\"People (2):\",\n+\t\t\t\t\"  sam: owner\",\n+\t\t\t\t\"  (no name yet): member, raised to owner in web\",\n+\t\t\t\t\"Open invites:\",\n+\t\t\t\t\"  a link, as member, until 2026-10-16 10:00 UTC\",\n+\t\t\t].join(\"\\n\"),\n+\t\t);\n+\t\tassert.equal(out.text.includes(\"cf-\"), false);\n+\t});\n+\n+\tit(\"shows a non-member only what the org shows everyone\", async () => {\n+\t\tconst home = signedIn();\n+\t\tconst s = server({\n+\t\t\t\"/orgs/acme-labs\": { status: 200, body: { name: \"acme-labs\", displayName: \"Acme Labs\", description: null } },\n+\t\t});\n+\t\tconst out = await runOrg(args([\"acme-labs\"]), SERVER, { home, fetch: s.fetcher });\n+\t\tassert.equal(out.text, \"acme-labs (Acme Labs)\\nYou aren't a member, so its people and settings aren't shown.\");\n+\t});\n+\n+\tit(\"says when there's no such org\", async () => {\n+\t\tconst home = signedIn();\n+\t\tawait assert.rejects(runOrg(args([\"nope\"]), SERVER, { home, fetch: server({}).fetcher }), /^.*nope: no such route/);\n+\t});\n+\n+\tit(\"sends no change: each says where on the website it is made, and nothing reaches the server\", async () => {\n+\t\tconst home = signedIn();\n+\t\tconst s = server({});\n+\t\tconst cases: [string[], RegExp][] = [\n+\t\t\t[[\"acme-labs\", \"--invite\", \"sam\"], /--invite is on acme-labs's People tab: https:\\/\\/example\\.dev\\/acme-labs\\./],\n+\t\t\t[[\"acme-labs\", \"--invite\", \"link\"], /People tab/],\n+\t\t\t[[\"acme-labs\", \"--role\", \"sam=owner\"], /--role is on acme-labs's People tab/],\n+\t\t\t[[\"acme-labs\", \"--remove\", \"jo\"], /--remove is on/],\n+\t\t\t[[\"acme-labs\", \"--leave\"], /--leave is on acme-labs's People tab/],\n+\t\t\t[[\"acme-labs\", \"--members\", \"read\"], /--members is on acme-labs's Settings tab/],\n+\t\t\t[[\"acme-labs\", \"--projects-by\", \"owners\"], /--projects-by is on acme-labs's Settings tab/],\n+\t\t\t[[\"new\", \"acme-labs\", \"--name\", \"Acme Labs\"], /Make acme-labs at https:\\/\\/example\\.dev\\/orgs\\/new\\./],\n+\t\t];\n+\t\tfor (const [line, said] of cases) {\n+\t\t\tawait assert.rejects(runOrg(args(line), SERVER, { home, fetch: s.fetcher }), said, line.join(\" \"));\n+\t\t\tawait assert.rejects(runOrg(args(line), SERVER, { home, fetch: s.fetcher }), /changed on the website for now/);\n+\t\t}\n+\t\tassert.deepEqual(s.asked, []);\n+\t});\n+\n+\tit(\"takes one change at a time, and --as only with --invite\", async () => {\n+\t\tconst home = signedIn();\n+\t\tconst s = server({});\n+\t\tawait assert.rejects(\n+\t\t\trunOrg(args([\"acme-labs\", \"--remove\", \"jo\", \"--leave\"]), SERVER, { home, fetch: s.fetcher }),\n+\t\t\t/one change at a time: --remove and --leave were given together/,\n+\t\t);\n+\t\tawait assert.rejects(\n+\t\t\trunOrg(args([\"acme-labs\", \"--as\", \"owner\"]), SERVER, { home, fetch: s.fetcher }),\n+\t\t\t/--as goes with --invite/,\n+\t\t);\n+\t\tawait assert.rejects(runOrg(args([]), SERVER, { home, fetch: s.fetcher }), /abso org <name>/);\n+\t});\n+});\n+\n+describe(\"the website for a server\", () => {\n+\tit(\"is the site api. serves, or the server itself\", () => {\n+\t\tassert.equal(siteOf(\"https://api.abso.dev\"), \"https://abso.dev\");\n+\t\tassert.equal(siteOf(\"https://staging.example.workers.dev/\"), \"https://staging.example.workers.dev\");\n+\t\tassert.equal(siteOf(\"http://localhost:8787\"), \"http://localhost:8787\");\n+\t});\n+});\n+\n+describe(\"abso init --org\", () => {\n+\tit(\"says the project is made on the website for now, with the org as its owner\", () => {\n+\t\tassert.throws(\n+\t\t\t() => initInOrg(\"acme-labs\", SERVER),\n+\t\t\t/made on the website for now: New project at https:\\/\\/example\\.dev, with acme-labs as its owner/,\n+\t\t);\n+\t});\n+});"}],"unmatched":[]}