Lines 7–29 at 318 (were 7–13 at 41)
77import { tmpdir } from "node:os"; 88import { join } from "node:path"; 99import { test } from "node:test"; 10−import { boxPublic, boxText, forgetBox, login, logout, mintKey, readBox, writeBox } from "../src/box.ts";
10+import { parseArgs } from "../src/args.ts"; 24+} from "../src/box.ts"; 25+import { serverFrom } from "../src/join-commands.ts"; 26+import { main } from "../src/main.ts"; 1228const SERVER = "https://staging.abso.example"; 1329const CREDENTIAL = "abso_bx_the-secret-credential"; Lines 32–37 at 318 (were 16–22 at 41)
1632 { project: "abso", role: "writer" }, 1733 { project: "game", role: "reader" }, 19− max_agents: 5,
2136const EXPIRES = "2026-10-15T00:00:00.000Z"; 2237const record = { server: SERVER, id: "b_0123abcd", credential: CREDENTIAL, grant, expiresAt: Date.parse(EXPIRES) }; Lines 70–167 at 318 (were 55–60 at 41)
73+test("login grant options validate project lists, roles and duration", () => { 74+ const request = (...words: string[]) => 76+ parseArgs("login", words, { 82+ assert.equal(request(), undefined); 83+ assert.deepEqual(request("--projects", "alpha, beta,alpha", "--days", "30"), { 85+ { project: "alpha", role: "writer" }, 86+ { project: "beta", role: "writer" }, 90+ assert.deepEqual(request("--projects", "alpha", "--role", "reader"), { 91+ projects: [{ project: "alpha", role: "reader" }], 93+ assert.deepEqual(request("--projects", "alpha", "--role", "owner"), { 94+ projects: [{ project: "alpha", role: "owner" }], 96+ assert.deepEqual(request("--days", "90"), { days: 90 }); 97+ for (const words of [ 98+ ["--role", "writer"], 100+ ["--projects", "alpha,"], 101+ ["--projects", "../alpha"], 102+ ["--projects", "alpha", "--role", "admin"], 103+ ["--projects", Array.from({ length: 101 }, (_, i) => `p${i}`).join(",")], 105+ assert.throws(() => request(...words)); 106+ for (const value of ["0", "-1", "1.5", "1e1", "Infinity", "100", "abc", ""]) 107+ assert.throws(() => request("--days", value)); 110+test("login sends requested grants only at device start and retains the user's actual approval", async () => { 112+ const seen: Seen = []; 114+ const requested = { projects: [{ project: "abso", role: "owner" as const }], days: 90 }; 115+ const approved = { projects: [{ project: "abso", role: "reader" }] }; 116+ const got = await login( 123+ [400, { error: "authorization_pending" }], 124+ [200, { box_id: "b_test", box_token: CREDENTIAL, expires_at: EXPIRES, grant: approved }], 135+ assert.deepEqual((seen[0].body as Record<string, unknown>).requested_grant, requested); 137+ seen.slice(1).map((call) => call.body), 138+ [{ device_code: started.device_code }, { device_code: started.device_code }], 140+ assert.deepEqual(got.grant, approved); 141+ assert.deepEqual(readBox(SERVER, h)?.grant, approved); 144+test("invalid login grant flags fail before a network request through the CLI", async () => { 145+ const original = globalThis.fetch; 147+ globalThis.fetch = (async () => { 149+ throw new Error("unexpected request"); 152+ const result = await main(["login", "--projects", "alpha", "--days", "0", "--no-browser"], { 157+ assert.equal(result.exit, 1); 158+ assert.match(result.stderr, /--days is from 1 to 90/); 159+ assert.equal(calls, 0); 161+ globalThis.fetch = original; 58165test("login shows the code, waits while it's pending, and keeps the credential in the user folder", async () => { 60167 const seen: Seen = []; Lines 198–273 at 318 (were 91–106 at 41)
92199 assert.deepEqual(said, [ 93200 "To sign this box in, open https://abso.dev/login/box?code=WXYZ-BCDF and approve the code WXYZ-BCDF.", 201+ "Waiting for approval. Ctrl-C stops waiting, and abso stops by itself when the code runs out.", 95203 assert.equal(readBox(SERVER, h)?.credential, CREDENTIAL); 96204 const files = readdirSync(join(h, ".abso", "boxes")); 97205 assert.equal(files.length, 1); 98206 assert.ok(readFileSync(join(h, ".abso", "boxes", files[0]!), "utf8").includes(CREDENTIAL)); 99207 assert.equal(boxText(got, SERVER, 0).includes(CREDENTIAL), false); 100− assert.match(boxText(got, SERVER, 0), /abso \(writer\), game \(reader\)\. Up to 5 agents\./);
208+ assert.match(boxText(got, SERVER, 0), /abso \(writer\), game \(reader\)\.\n/); 209+ assert.doesNotMatch(boxText(got, SERVER, 0), /agents/); 101210 assert.equal(JSON.stringify(boxPublic(got)).includes(CREDENTIAL), false); 213+test("login opens the approve page with the code in it, and prints the link and code all the same", async () => { 214+ const said: string[] = []; 215+ const opened: string[] = []; 223+ [200, { box_id: "b_0123abcd", box_token: CREDENTIAL, expires_at: EXPIRES, grant }], 229+ say: (line) => said.push(line), 230+ open: (url) => (opened.push(url), true), 234+ assert.deepEqual(opened, ["https://abso.dev/login/box?code=WXYZ-BCDF"]); 235+ assert.deepEqual(said.slice(0, 2), [ 236+ "Opening the approve page in your browser.", 237+ "To sign this box in, open https://abso.dev/login/box?code=WXYZ-BCDF and approve the code WXYZ-BCDF.", 241+test("an open that doesn't start says nothing about a browser, and the printed link stands", async () => { 242+ const said: string[] = []; 250+ [200, { box_id: "b_0123abcd", box_token: CREDENTIAL, expires_at: EXPIRES, grant }], 256+ say: (line) => said.push(line), 263+ "To sign this box in, open https://abso.dev/login/box?code=WXYZ-BCDF and approve the code WXYZ-BCDF.", 266+ said.some((l) => l.includes("browser")), 104271test("a code that's turned down or runs out says so and keeps nothing", async () => { 106273 const c = clock(); Lines 327–355 at 318 (were 160–165 at 41)
160327 assert.equal(readBox(SERVER, h), undefined); 330+test("login says it's still waiting about once a minute, and stops in the end whatever the server says", async () => { 332+ const said: string[] = []; 333+ const pending = Array.from({ length: 2000 }, (): [number, unknown] => [400, { error: "authorization_pending" }]); 334+ await assert.rejects( 338+ fetch: server([[200, { ...started, expires_in: 10 ** 9, interval: 5 }], ...pending], []), 341+ say: (line) => said.push(line), 345+ /nobody approved the code WXYZ-BCDF in time\. Run abso login again\./, 347+ assert.equal(c.slept(), 30 * 60 * 1000, "a server that says never still gets an end"); 348+ const still = said.filter((line) => line === "Still waiting for the code WXYZ-BCDF to be approved."); 349+ assert.equal(still.length, 30); 350+ assert.equal(said.length, 2 + 30); 163353test("a key is minted with the box credential, sent only to the box's own server", async () => { 164354 const seen: Seen = []; 165355 const minted = await mintKey( Lines 379–554 at 318 (were 189–191 at 41)
189379 assert.equal(readBox(SERVER, h), undefined); 190380 assert.equal(forgetBox(SERVER, h), false); 383+test("join and init pick the server in the one order: --server, the folder's, ABSO_SERVER, then prod", () => { 385+ const outside = home(); 386+ const args = (server?: string) => ({ positionals: [], flags: new Map(server ? [["server", server]] : []) }); 387+ const none = { env: {}, cwd: outside }; 388+ assert.equal(serverFrom(args(), none), "https://api.abso.dev", "nothing named: prod"); 389+ writeBox({ ...record, expiresAt: Date.now() + 1e6 }, h); 390+ assert.deepEqual(signedInServers(h), [SERVER]); 391+ assert.equal(serverFrom(args(), none), "https://api.abso.dev", "a sign-in elsewhere doesn't pick the server"); 392+ assert.deepEqual(signedInServers(h, Date.now() + 2e6), [], "an expired sign-in doesn't count"); 393+ assert.equal(serverFrom(args(), { ...none, env: { ABSO_SERVER: "https://env.example" } }), "https://env.example"); 395+ serverFrom(args("https://flag.example"), { ...none, env: { ABSO_SERVER: "https://env.example" } }), 396+ "https://flag.example", 398+ assert.equal(serverFrom(args(SERVER), none), SERVER, "--server is used as given"); 401+test("join with no --agent takes the box's name, skips names this box holds, and moves past a taken one", async () => { 402+ const seen: Seen = []; 403+ const fetcher = server( 405+ [200, { box_id: record.id, agents: [{ project: "abso", agent: "snow", key_id: "k_0" }] }], 406+ [409, { error: "that agent name is already in use in this project; pick a new one" }], 407+ [200, { key: "abso_sk_new", id: "k_3" }], 411+ const got = await mintFreeName(record, "abso", "snow", fetcher); 412+ assert.deepEqual(got, { key: "abso_sk_new", id: "k_3", agent: "snow-3" }); 414+ seen.map((s) => [new URL(s.url).pathname, (s.body as { agent?: string } | null)?.agent ?? null]), 417+ ["/box/agent-key", "snow-2"], 418+ ["/box/agent-key", "snow-3"], 420+ "snow is this box's already: never minted again, which would replace its key", 424+test("abso init from a box (abso 663): one key for every try, waiting out a pending create; abso box says all projects", async () => { 425+ const record = { server: SERVER, id: "b_1", credential: CREDENTIAL, grant, expiresAt: Date.now() + 86_400_000 }; 426+ const seen: { path: string; key: string | null; auth: string | null; body: string }[] = []; 428+ const fetcher = (async (url: string | URL | Request, init?: RequestInit) => { 429+ const headers = new Headers(init?.headers); 431+ path: new URL(String(url)).pathname, 432+ key: headers.get("idempotency-key"), 433+ auth: headers.get("authorization"), 434+ body: String(init?.body), 439+ ? { operation: { id: "op", state: "pending" } } 440+ : { project: "made", id: "proj_1", visibility: "private" }; 441+ return new Response(JSON.stringify(body), { status: calls < 3 ? 202 : 200 }); 443+ const made = await boxCreateProject( 445+ { name: "made", files: [{ path: "a.txt", text: "hi\n" }] }, 446+ "33333333-3333-4333-8333-333333333333", 450+ assert.equal(made.id, "proj_1"); 451+ assert.equal(seen.length, 3); 452+ assert.ok(seen.every((s) => s.path === "/box/projects" && s.key === "33333333-3333-4333-8333-333333333333")); 453+ assert.ok(seen.every((s) => s.auth === `Box ${CREDENTIAL}`)); 454+ assert.deepEqual(JSON.parse(seen[0]!.body), { name: "made", files: [{ path: "a.txt", text: "hi\n" }] }); 455+ const all = { ...record, grant: { projects: [], scopes: [{ kind: "member-all", role: "writer" }] } }; 457+ boxText(all, SERVER), 458+ /Projects: all of yours, now and later \(writer at most\)\. abso init makes a new one/, 462+// abso 684: in an agent's folder, login asks a person in the project instead of relaying the code. 463+function askedOf(withdrawn: number[]): NonNullable<Parameters<typeof login>[1]["ask"]> { 464+ return async ({ device, user }) => { 465+ assert.equal(device, started.device_code, "the ask is made with this client's own device code"); 466+ assert.equal(user, started.user_code); 469+ who: "the person who approved this agent's box", 471+ expiresAt: Date.parse("2026-10-15T19:30:00Z"), 473+ withdraw: async () => void withdrawn.push(41), 478+test("an asked login says who was asked and until when, and doesn't lead with the code (abso 684)", async () => { 479+ const said: string[] = []; 488+ [200, { box_id: "b_test", box_token: CREDENTIAL, expires_at: EXPIRES, grant: { projects: [] } }], 494+ say: (line) => said.push(line), 500+ /^Asked the person who approved this agent's box \(ask 41 in abso\) to sign this box in, as at most writer\.$/, 502+ assert.match(said[1]!, /^Waiting for their answer, until .* CT\. To approve it yourself instead: /); 503+ assert.doesNotMatch(said.join("\n"), /approve the code/); 506+test("an asked login that's turned down says who turned it down (abso 684)", async () => { 508+ await assert.rejects( 516+ [400, { error: "access_denied" }], 526+ /the person who approved this agent's box turned this sign-in down/, 530+test("an asked login that nobody answers withdraws its ask and grants nothing (abso 684)", async () => { 531+ const withdrawn: number[] = []; 533+ await assert.rejects( 537+ ask: askedOf(withdrawn), 541+ [400, { error: "expired_token" }], 551+ /nobody answered in time, so nothing was granted/, 553+ assert.deepEqual(withdrawn, [41]);