{"draft":null,"from":101,"to":102,"files":[{"path":"client/test/box.test.ts","added":99,"removed":0,"diff":"--- a/client/test/box.test.ts\n+++ b/client/test/box.test.ts\n@@ -7,11 +7,13 @@\n import { tmpdir } from \"node:os\";\n import { join } from \"node:path\";\n import { test } from \"node:test\";\n+import { parseArgs } from \"../src/args.ts\";\n import {\n \tboxPublic,\n \tboxText,\n \tforgetBox,\n \tlogin,\n+\tloginRequest,\n \tlogout,\n \tmintFreeName,\n \tmintKey,\n@@ -20,6 +22,7 @@\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@@ -67,6 +70,102 @@\n \t};\n }\n \n+test(\"login grant options validate project lists, roles, limits 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\t\"max-agents\": \"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\", \"--max-agents\", \"1\", \"--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\tmax_agents: 1,\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(\"--max-agents\", \"50\", \"--days\", \"90\"), { max_agents: 50, 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 flag of [\"--max-agents\", \"--days\"]) {\n+\t\tfor (const value of [\"0\", \"-1\", \"1.5\", \"1e1\", \"Infinity\", \"100\", \"abc\", \"\"])\n+\t\t\tassert.throws(() => request(flag, value));\n+\t}\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 }], max_agents: 10, days: 90 };\n+\tconst approved = { projects: [{ project: \"abso\", role: \"reader\" }], max_agents: 1 };\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 = [];"}],"unmatched":[]}