{"draft":null,"from":2,"to":318,"files":[{"path":"client/test/box.test.ts","added":368,"removed":5,"diff":"--- a/client/test/box.test.ts\n+++ b/client/test/box.test.ts\n@@ -7,7 +7,23 @@\n import { tmpdir } from \"node:os\";\n import { join } from \"node:path\";\n import { test } from \"node:test\";\n-import { boxPublic, boxText, forgetBox, login, logout, mintKey, readBox, writeBox } from \"../src/box.ts\";\n+import { parseArgs } from \"../src/args.ts\";\n+import {\n+\tboxCreateProject,\n+\tboxPublic,\n+\tboxText,\n+\tforgetBox,\n+\tlogin,\n+\tloginRequest,\n+\tlogout,\n+\tmintFreeName,\n+\tmintKey,\n+\treadBox,\n+\tsignedInServers,\n+\twriteBox,\n+} from \"../src/box.ts\";\n+import { serverFrom } from \"../src/join-commands.ts\";\n+import { main } from \"../src/main.ts\";\n \n const SERVER = \"https://staging.abso.example\";\n const CREDENTIAL = \"abso_bx_the-secret-credential\";\n@@ -16,7 +32,6 @@\n \t\t{ project: \"abso\", role: \"writer\" },\n \t\t{ project: \"game\", role: \"reader\" },\n \t],\n-\tmax_agents: 5,\n };\n const EXPIRES = \"2026-10-15T00:00:00.000Z\";\n const record = { server: SERVER, id: \"b_0123abcd\", credential: CREDENTIAL, grant, expiresAt: Date.parse(EXPIRES) };\n@@ -55,6 +70,98 @@\n \t};\n }\n \n+test(\"login grant options validate project lists, roles and duration\", () => {\n+\tconst request = (...words: string[]) =>\n+\t\tloginRequest(\n+\t\t\tparseArgs(\"login\", words, {\n+\t\t\t\tprojects: \"string\",\n+\t\t\t\trole: \"string\",\n+\t\t\t\tdays: \"string\",\n+\t\t\t}),\n+\t\t);\n+\tassert.equal(request(), undefined);\n+\tassert.deepEqual(request(\"--projects\", \"alpha, beta,alpha\", \"--days\", \"30\"), {\n+\t\tprojects: [\n+\t\t\t{ project: \"alpha\", role: \"writer\" },\n+\t\t\t{ project: \"beta\", role: \"writer\" },\n+\t\t],\n+\t\tdays: 30,\n+\t});\n+\tassert.deepEqual(request(\"--projects\", \"alpha\", \"--role\", \"reader\"), {\n+\t\tprojects: [{ project: \"alpha\", role: \"reader\" }],\n+\t});\n+\tassert.deepEqual(request(\"--projects\", \"alpha\", \"--role\", \"owner\"), {\n+\t\tprojects: [{ project: \"alpha\", role: \"owner\" }],\n+\t});\n+\tassert.deepEqual(request(\"--days\", \"90\"), { days: 90 });\n+\tfor (const words of [\n+\t\t[\"--role\", \"writer\"],\n+\t\t[\"--projects\", \"\"],\n+\t\t[\"--projects\", \"alpha,\"],\n+\t\t[\"--projects\", \"../alpha\"],\n+\t\t[\"--projects\", \"alpha\", \"--role\", \"admin\"],\n+\t\t[\"--projects\", Array.from({ length: 101 }, (_, i) => `p${i}`).join(\",\")],\n+\t])\n+\t\tassert.throws(() => request(...words));\n+\tfor (const value of [\"0\", \"-1\", \"1.5\", \"1e1\", \"Infinity\", \"100\", \"abc\", \"\"])\n+\t\tassert.throws(() => request(\"--days\", value));\n+});\n+\n+test(\"login sends requested grants only at device start and retains the user's actual approval\", async () => {\n+\tconst h = home();\n+\tconst seen: Seen = [];\n+\tconst c = clock();\n+\tconst requested = { projects: [{ project: \"abso\", role: \"owner\" as const }], days: 90 };\n+\tconst approved = { projects: [{ project: \"abso\", role: \"reader\" }] };\n+\tconst got = await login(\n+\t\tSERVER,\n+\t\t{\n+\t\t\trequest: requested,\n+\t\t\tfetch: server(\n+\t\t\t\t[\n+\t\t\t\t\t[200, started],\n+\t\t\t\t\t[400, { error: \"authorization_pending\" }],\n+\t\t\t\t\t[200, { box_id: \"b_test\", box_token: CREDENTIAL, expires_at: EXPIRES, grant: approved }],\n+\t\t\t\t],\n+\t\t\t\tseen,\n+\t\t\t),\n+\t\t\tsleep: c.sleep,\n+\t\t\tnow: c.now,\n+\t\t\tsay: () => {},\n+\t\t},\n+\t\th,\n+\t);\n+\tassert.ok(seen[0]);\n+\tassert.deepEqual((seen[0].body as Record<string, unknown>).requested_grant, requested);\n+\tassert.deepEqual(\n+\t\tseen.slice(1).map((call) => call.body),\n+\t\t[{ device_code: started.device_code }, { device_code: started.device_code }],\n+\t);\n+\tassert.deepEqual(got.grant, approved);\n+\tassert.deepEqual(readBox(SERVER, h)?.grant, approved);\n+});\n+\n+test(\"invalid login grant flags fail before a network request through the CLI\", async () => {\n+\tconst original = globalThis.fetch;\n+\tlet calls = 0;\n+\tglobalThis.fetch = (async () => {\n+\t\tcalls++;\n+\t\tthrow new Error(\"unexpected request\");\n+\t}) as typeof fetch;\n+\ttry {\n+\t\tconst result = await main([\"login\", \"--projects\", \"alpha\", \"--days\", \"0\", \"--no-browser\"], {\n+\t\t\tcwd: home(),\n+\t\t\tenv: {},\n+\t\t\twrite: () => {},\n+\t\t});\n+\t\tassert.equal(result.exit, 1);\n+\t\tassert.match(result.stderr, /--days is from 1 to 90/);\n+\t\tassert.equal(calls, 0);\n+\t} finally {\n+\t\tglobalThis.fetch = original;\n+\t}\n+});\n+\n test(\"login shows the code, waits while it's pending, and keeps the credential in the user folder\", async () => {\n \tconst h = home();\n \tconst seen: Seen = [];\n@@ -91,16 +198,76 @@\n \t);\n \tassert.deepEqual(said, [\n \t\t\"To sign this box in, open https://abso.dev/login/box?code=WXYZ-BCDF and approve the code WXYZ-BCDF.\",\n+\t\t\"Waiting for approval. Ctrl-C stops waiting, and abso stops by itself when the code runs out.\",\n \t]);\n \tassert.equal(readBox(SERVER, h)?.credential, CREDENTIAL);\n-\tconst files = readdirSync(join(h, \".avcs\", \"boxes\"));\n+\tconst files = readdirSync(join(h, \".abso\", \"boxes\"));\n \tassert.equal(files.length, 1);\n-\tassert.ok(readFileSync(join(h, \".avcs\", \"boxes\", files[0]!), \"utf8\").includes(CREDENTIAL));\n+\tassert.ok(readFileSync(join(h, \".abso\", \"boxes\", files[0]!), \"utf8\").includes(CREDENTIAL));\n \tassert.equal(boxText(got, SERVER, 0).includes(CREDENTIAL), false);\n-\tassert.match(boxText(got, SERVER, 0), /abso \\(writer\\), game \\(reader\\)\\. Up to 5 agents\\./);\n+\tassert.match(boxText(got, SERVER, 0), /abso \\(writer\\), game \\(reader\\)\\.\\n/);\n+\tassert.doesNotMatch(boxText(got, SERVER, 0), /agents/);\n \tassert.equal(JSON.stringify(boxPublic(got)).includes(CREDENTIAL), false);\n });\n \n+test(\"login opens the approve page with the code in it, and prints the link and code all the same\", async () => {\n+\tconst said: string[] = [];\n+\tconst opened: string[] = [];\n+\tconst c = clock();\n+\tawait login(\n+\t\tSERVER,\n+\t\t{\n+\t\t\tfetch: server(\n+\t\t\t\t[\n+\t\t\t\t\t[200, started],\n+\t\t\t\t\t[200, { box_id: \"b_0123abcd\", box_token: CREDENTIAL, expires_at: EXPIRES, grant }],\n+\t\t\t\t],\n+\t\t\t\t[],\n+\t\t\t),\n+\t\t\tsleep: c.sleep,\n+\t\t\tnow: c.now,\n+\t\t\tsay: (line) => said.push(line),\n+\t\t\topen: (url) => (opened.push(url), true),\n+\t\t},\n+\t\thome(),\n+\t);\n+\tassert.deepEqual(opened, [\"https://abso.dev/login/box?code=WXYZ-BCDF\"]);\n+\tassert.deepEqual(said.slice(0, 2), [\n+\t\t\"Opening the approve page in your browser.\",\n+\t\t\"To sign this box in, open https://abso.dev/login/box?code=WXYZ-BCDF and approve the code WXYZ-BCDF.\",\n+\t]);\n+});\n+\n+test(\"an open that doesn't start says nothing about a browser, and the printed link stands\", async () => {\n+\tconst said: string[] = [];\n+\tconst c = clock();\n+\tawait login(\n+\t\tSERVER,\n+\t\t{\n+\t\t\tfetch: server(\n+\t\t\t\t[\n+\t\t\t\t\t[200, started],\n+\t\t\t\t\t[200, { box_id: \"b_0123abcd\", box_token: CREDENTIAL, expires_at: EXPIRES, grant }],\n+\t\t\t\t],\n+\t\t\t\t[],\n+\t\t\t),\n+\t\t\tsleep: c.sleep,\n+\t\t\tnow: c.now,\n+\t\t\tsay: (line) => said.push(line),\n+\t\t\topen: () => false,\n+\t\t},\n+\t\thome(),\n+\t);\n+\tassert.equal(\n+\t\tsaid[0],\n+\t\t\"To sign this box in, open https://abso.dev/login/box?code=WXYZ-BCDF and approve the code WXYZ-BCDF.\",\n+\t);\n+\tassert.equal(\n+\t\tsaid.some((l) => l.includes(\"browser\")),\n+\t\tfalse,\n+\t);\n+});\n+\n test(\"a code that's turned down or runs out says so and keeps nothing\", async () => {\n \tconst h = home();\n \tconst c = clock();\n@@ -160,6 +327,29 @@\n \tassert.equal(readBox(SERVER, h), undefined);\n });\n \n+test(\"login says it's still waiting about once a minute, and stops in the end whatever the server says\", async () => {\n+\tconst c = clock();\n+\tconst said: string[] = [];\n+\tconst pending = Array.from({ length: 2000 }, (): [number, unknown] => [400, { error: \"authorization_pending\" }]);\n+\tawait assert.rejects(\n+\t\tlogin(\n+\t\t\tSERVER,\n+\t\t\t{\n+\t\t\t\tfetch: server([[200, { ...started, expires_in: 10 ** 9, interval: 5 }], ...pending], []),\n+\t\t\t\tsleep: c.sleep,\n+\t\t\t\tnow: c.now,\n+\t\t\t\tsay: (line) => said.push(line),\n+\t\t\t},\n+\t\t\thome(),\n+\t\t),\n+\t\t/nobody approved the code WXYZ-BCDF in time\\. Run abso login again\\./,\n+\t);\n+\tassert.equal(c.slept(), 30 * 60 * 1000, \"a server that says never still gets an end\");\n+\tconst still = said.filter((line) => line === \"Still waiting for the code WXYZ-BCDF to be approved.\");\n+\tassert.equal(still.length, 30);\n+\tassert.equal(said.length, 2 + 30);\n+});\n+\n test(\"a key is minted with the box credential, sent only to the box's own server\", async () => {\n \tconst seen: Seen = [];\n \tconst minted = await mintKey(\n@@ -189,3 +379,176 @@\n \tassert.equal(readBox(SERVER, h), undefined);\n \tassert.equal(forgetBox(SERVER, h), false);\n });\n+\n+test(\"join and init pick the server in the one order: --server, the folder's, ABSO_SERVER, then prod\", () => {\n+\tconst h = home();\n+\tconst outside = home();\n+\tconst args = (server?: string) => ({ positionals: [], flags: new Map(server ? [[\"server\", server]] : []) });\n+\tconst none = { env: {}, cwd: outside };\n+\tassert.equal(serverFrom(args(), none), \"https://api.abso.dev\", \"nothing named: prod\");\n+\twriteBox({ ...record, expiresAt: Date.now() + 1e6 }, h);\n+\tassert.deepEqual(signedInServers(h), [SERVER]);\n+\tassert.equal(serverFrom(args(), none), \"https://api.abso.dev\", \"a sign-in elsewhere doesn't pick the server\");\n+\tassert.deepEqual(signedInServers(h, Date.now() + 2e6), [], \"an expired sign-in doesn't count\");\n+\tassert.equal(serverFrom(args(), { ...none, env: { ABSO_SERVER: \"https://env.example\" } }), \"https://env.example\");\n+\tassert.equal(\n+\t\tserverFrom(args(\"https://flag.example\"), { ...none, env: { ABSO_SERVER: \"https://env.example\" } }),\n+\t\t\"https://flag.example\",\n+\t);\n+\tassert.equal(serverFrom(args(SERVER), none), SERVER, \"--server is used as given\");\n+});\n+\n+test(\"join with no --agent takes the box's name, skips names this box holds, and moves past a taken one\", async () => {\n+\tconst seen: Seen = [];\n+\tconst fetcher = server(\n+\t\t[\n+\t\t\t[200, { box_id: record.id, agents: [{ project: \"abso\", agent: \"snow\", key_id: \"k_0\" }] }],\n+\t\t\t[409, { error: \"that agent name is already in use in this project; pick a new one\" }],\n+\t\t\t[200, { key: \"abso_sk_new\", id: \"k_3\" }],\n+\t\t],\n+\t\tseen,\n+\t);\n+\tconst got = await mintFreeName(record, \"abso\", \"snow\", fetcher);\n+\tassert.deepEqual(got, { key: \"abso_sk_new\", id: \"k_3\", agent: \"snow-3\" });\n+\tassert.deepEqual(\n+\t\tseen.map((s) => [new URL(s.url).pathname, (s.body as { agent?: string } | null)?.agent ?? null]),\n+\t\t[\n+\t\t\t[\"/box\", null],\n+\t\t\t[\"/box/agent-key\", \"snow-2\"],\n+\t\t\t[\"/box/agent-key\", \"snow-3\"],\n+\t\t],\n+\t\t\"snow is this box's already: never minted again, which would replace its key\",\n+\t);\n+});\n+\n+test(\"abso init from a box (abso 663): one key for every try, waiting out a pending create; abso box says all projects\", async () => {\n+\tconst record = { server: SERVER, id: \"b_1\", credential: CREDENTIAL, grant, expiresAt: Date.now() + 86_400_000 };\n+\tconst seen: { path: string; key: string | null; auth: string | null; body: string }[] = [];\n+\tlet calls = 0;\n+\tconst fetcher = (async (url: string | URL | Request, init?: RequestInit) => {\n+\t\tconst headers = new Headers(init?.headers);\n+\t\tseen.push({\n+\t\t\tpath: new URL(String(url)).pathname,\n+\t\t\tkey: headers.get(\"idempotency-key\"),\n+\t\t\tauth: headers.get(\"authorization\"),\n+\t\t\tbody: String(init?.body),\n+\t\t});\n+\t\tcalls++;\n+\t\tconst body =\n+\t\t\tcalls < 3\n+\t\t\t\t? { operation: { id: \"op\", state: \"pending\" } }\n+\t\t\t\t: { project: \"made\", id: \"proj_1\", visibility: \"private\" };\n+\t\treturn new Response(JSON.stringify(body), { status: calls < 3 ? 202 : 200 });\n+\t}) as typeof fetch;\n+\tconst made = await boxCreateProject(\n+\t\trecord,\n+\t\t{ name: \"made\", files: [{ path: \"a.txt\", text: \"hi\\n\" }] },\n+\t\t\"33333333-3333-4333-8333-333333333333\",\n+\t\tfetcher,\n+\t\tasync () => {},\n+\t);\n+\tassert.equal(made.id, \"proj_1\");\n+\tassert.equal(seen.length, 3);\n+\tassert.ok(seen.every((s) => s.path === \"/box/projects\" && s.key === \"33333333-3333-4333-8333-333333333333\"));\n+\tassert.ok(seen.every((s) => s.auth === `Box ${CREDENTIAL}`));\n+\tassert.deepEqual(JSON.parse(seen[0]!.body), { name: \"made\", files: [{ path: \"a.txt\", text: \"hi\\n\" }] });\n+\tconst all = { ...record, grant: { projects: [], scopes: [{ kind: \"member-all\", role: \"writer\" }] } };\n+\tassert.match(\n+\t\tboxText(all, SERVER),\n+\t\t/Projects: all of yours, now and later \\(writer at most\\)\\. abso init makes a new one/,\n+\t);\n+});\n+\n+// abso 684: in an agent's folder, login asks a person in the project instead of relaying the code.\n+function askedOf(withdrawn: number[]): NonNullable<Parameters<typeof login>[1][\"ask\"]> {\n+\treturn async ({ device, user }) => {\n+\t\tassert.equal(device, started.device_code, \"the ask is made with this client's own device code\");\n+\t\tassert.equal(user, started.user_code);\n+\t\treturn {\n+\t\t\tproject: \"abso\",\n+\t\t\twho: \"the person who approved this agent's box\",\n+\t\t\tasks: [41],\n+\t\t\texpiresAt: Date.parse(\"2026-10-15T19:30:00Z\"),\n+\t\t\trole: \"writer\",\n+\t\t\twithdraw: async () => void withdrawn.push(41),\n+\t\t};\n+\t};\n+}\n+\n+test(\"an asked login says who was asked and until when, and doesn't lead with the code (abso 684)\", async () => {\n+\tconst said: string[] = [];\n+\tconst c = clock();\n+\tawait login(\n+\t\tSERVER,\n+\t\t{\n+\t\t\task: askedOf([]),\n+\t\t\tfetch: server(\n+\t\t\t\t[\n+\t\t\t\t\t[200, started],\n+\t\t\t\t\t[200, { box_id: \"b_test\", box_token: CREDENTIAL, expires_at: EXPIRES, grant: { projects: [] } }],\n+\t\t\t\t],\n+\t\t\t\t[],\n+\t\t\t),\n+\t\t\tsleep: c.sleep,\n+\t\t\tnow: c.now,\n+\t\t\tsay: (line) => said.push(line),\n+\t\t},\n+\t\thome(),\n+\t);\n+\tassert.match(\n+\t\tsaid[0]!,\n+\t\t/^Asked the person who approved this agent's box \\(ask 41 in abso\\) to sign this box in, as at most writer\\.$/,\n+\t);\n+\tassert.match(said[1]!, /^Waiting for their answer, until .* CT\\. To approve it yourself instead: /);\n+\tassert.doesNotMatch(said.join(\"\\n\"), /approve the code/);\n+});\n+\n+test(\"an asked login that's turned down says who turned it down (abso 684)\", async () => {\n+\tconst c = clock();\n+\tawait assert.rejects(\n+\t\tlogin(\n+\t\t\tSERVER,\n+\t\t\t{\n+\t\t\t\task: askedOf([]),\n+\t\t\t\tfetch: server(\n+\t\t\t\t\t[\n+\t\t\t\t\t\t[200, started],\n+\t\t\t\t\t\t[400, { error: \"access_denied\" }],\n+\t\t\t\t\t],\n+\t\t\t\t\t[],\n+\t\t\t\t),\n+\t\t\t\tsleep: c.sleep,\n+\t\t\t\tnow: c.now,\n+\t\t\t\tsay: () => {},\n+\t\t\t},\n+\t\t\thome(),\n+\t\t),\n+\t\t/the person who approved this agent's box turned this sign-in down/,\n+\t);\n+});\n+\n+test(\"an asked login that nobody answers withdraws its ask and grants nothing (abso 684)\", async () => {\n+\tconst withdrawn: number[] = [];\n+\tconst c = clock();\n+\tawait assert.rejects(\n+\t\tlogin(\n+\t\t\tSERVER,\n+\t\t\t{\n+\t\t\t\task: askedOf(withdrawn),\n+\t\t\t\tfetch: server(\n+\t\t\t\t\t[\n+\t\t\t\t\t\t[200, started],\n+\t\t\t\t\t\t[400, { error: \"expired_token\" }],\n+\t\t\t\t\t],\n+\t\t\t\t\t[],\n+\t\t\t\t),\n+\t\t\t\tsleep: c.sleep,\n+\t\t\t\tnow: c.now,\n+\t\t\t\tsay: () => {},\n+\t\t\t},\n+\t\t\thome(),\n+\t\t),\n+\t\t/nobody answered in time, so nothing was granted/,\n+\t);\n+\tassert.deepEqual(withdrawn, [41]);\n+});"}],"unmatched":[]}