{"draft":null,"from":90,"to":318,"files":[{"path":"client/test/init-credential.test.ts","added":171,"removed":0,"diff":"--- /dev/null\n+++ b/client/test/init-credential.test.ts\n@@ -0,0 +1,171 @@\n+// abso init and join sign in with the box sign-in, and use this machine's ~/.abso/token only with --token-file. A\n+// project is never made or joined with a shared credential that happened to be in a file, and both say what they sign\n+// in with. The client runs as its own process with its own home, so nothing on this machine is read.\n+\n+import assert from \"node:assert/strict\";\n+import { execFile } from \"node:child_process\";\n+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from \"node:fs\";\n+import { createServer, type IncomingHttpHeaders, type Server } from \"node:http\";\n+import type { AddressInfo } from \"node:net\";\n+import { tmpdir } from \"node:os\";\n+import { join, resolve } from \"node:path\";\n+import { after, before, describe, it } from \"node:test\";\n+import { parseArgs } from \"../src/args.ts\";\n+import { writeBox } from \"../src/box.ts\";\n+import { tokenCredential } from \"../src/join-commands.ts\";\n+import { type Fake, startFake } from \"./fake-server.ts\";\n+\n+const CLIENT = resolve(import.meta.dirname, \"..\", \"abso.ts\");\n+const TOKEN = \"file-token-not-secret\";\n+const CREDENTIAL = \"abso_bx_box-credential-not-secret\";\n+let fake: Fake;\n+// Stands in for a server that takes box sign-ins: it notes each request, then answers 500 so the run stops there.\n+let boxes: Server;\n+let boxesUrl: string;\n+const heard: { url: string; headers: IncomingHttpHeaders }[] = [];\n+const dirs: string[] = [];\n+const temp = () => {\n+\tconst dir = mkdtempSync(join(tmpdir(), \"abso-test-init-credential-\"));\n+\tdirs.push(dir);\n+\treturn dir;\n+};\n+\n+before(async () => {\n+\tfake = await startFake(0, TOKEN);\n+\tboxes = createServer((req, res) => {\n+\t\theard.push({ url: req.url ?? \"\", headers: req.headers });\n+\t\tres.writeHead(500, { \"content-type\": \"application/json\" }).end('{\"error\":\"stop here\"}');\n+\t});\n+\tawait new Promise<void>((done) => boxes.listen(0, \"127.0.0.1\", done));\n+\tboxesUrl = `http://127.0.0.1:${(boxes.address() as AddressInfo).port}`;\n+});\n+after(async () => {\n+\tawait fake.close();\n+\tboxes.close();\n+\tfor (const dir of dirs) rmSync(dir, { recursive: true, force: true });\n+});\n+\n+/** A home holding a token file, and a box sign-in for `boxFor` when given. */\n+function home(boxFor?: string): string {\n+\tconst dir = temp();\n+\tmkdirSync(join(dir, \".abso\"), { recursive: true });\n+\twriteFileSync(join(dir, \".abso\", \"token\"), `${TOKEN}\\n`);\n+\tif (boxFor)\n+\t\twriteBox(\n+\t\t\t{\n+\t\t\t\tserver: boxFor,\n+\t\t\t\tid: \"b_0123abcd\",\n+\t\t\t\tcredential: CREDENTIAL,\n+\t\t\t\tgrant: { projects: [], scopes: [{ kind: \"member-all\", role: \"owner\" }] },\n+\t\t\t\texpiresAt: Date.now() + 86_400_000,\n+\t\t\t},\n+\t\t\tdir,\n+\t\t);\n+\treturn dir;\n+}\n+\n+function client(\n+\thomeDir: string,\n+\tcwd: string,\n+\t...args: string[]\n+): Promise<{ code: number; stdout: string; stderr: string }> {\n+\tconst env: Record<string, string> = {\n+\t\tPATH: process.env.PATH ?? \"\",\n+\t\tHOME: homeDir,\n+\t\tUSERPROFILE: homeDir,\n+\t\tAVCS_SERVERS_FILE: \"no-servers.json\",\n+\t\tABSO_NO_BROWSER: \"1\",\n+\t};\n+\tif (process.env.CLAUDE_CONFIG_DIR) env.CLAUDE_CONFIG_DIR = process.env.CLAUDE_CONFIG_DIR;\n+\tif (process.env.SystemRoot) env.SystemRoot = process.env.SystemRoot;\n+\treturn new Promise((done) => {\n+\t\texecFile(process.execPath, [CLIENT, ...args], { cwd, env }, (error, stdout, stderr) =>\n+\t\t\tdone({ code: typeof error?.code === \"number\" ? error.code : 0, stdout, stderr }),\n+\t\t);\n+\t});\n+}\n+\n+function folder(): string {\n+\tconst dir = temp();\n+\twriteFileSync(join(dir, \"a.txt\"), \"hi\\n\");\n+\treturn dir;\n+}\n+\n+const FLAGS = { \"token-file\": \"boolean\" } as const;\n+\n+describe(\"what init and join sign in with\", () => {\n+\tit(\"the token file only with --token-file, never into a folder with a key; ABSO_TOKEN as given\", () => {\n+\t\tconst server = \"http://127.0.0.1:1\";\n+\t\tconst file = () => TOKEN;\n+\t\tconst none = parseArgs(\"init\", [], FLAGS);\n+\t\tconst asked = parseArgs(\"init\", [\"--token-file\"], FLAGS);\n+\t\tassert.equal(tokenCredential(none, {}, server, temp(), file), undefined, \"a token file alone is not used\");\n+\t\tassert.deepEqual(tokenCredential(asked, {}, server, temp(), file), {\n+\t\t\ttoken: TOKEN,\n+\t\t\twords: \"the token file, ~/.abso/token, as --token-file asks\",\n+\t\t});\n+\t\tassert.throws(() => tokenCredential(asked, {}, server, temp(), () => undefined), /this machine has none/);\n+\t\tconst keyed = temp();\n+\t\tmkdirSync(join(keyed, \".abso\"));\n+\t\twriteFileSync(join(keyed, \".abso\", \"key\"), \"{}\");\n+\t\tassert.throws(() => tokenCredential(asked, {}, server, keyed, file), /already has a key/);\n+\t\tassert.equal(tokenCredential(none, { ABSO_TOKEN: \"env-token\" }, server, temp(), file)?.words, \"ABSO_TOKEN\");\n+\t});\n+\n+\tit(\"init with a token file and a box sign-in, no flag: signs in with the box, and says so\", async () => {\n+\t\tconst run = await client(home(boxesUrl), folder(), \"init\", \"--project\", \"made\", \"--server\", boxesUrl);\n+\t\tassert.match(run.stderr, /Signing in with this box's sign-in, b_0123abcd\\./);\n+\t\tassert.ok(heard.length > 0);\n+\t\tassert.ok(\n+\t\t\theard.every((h) => h.headers.authorization === `Box ${CREDENTIAL}`),\n+\t\t\t\"every request carried the box's sign-in\",\n+\t\t);\n+\t\tassert.ok(\n+\t\t\theard.every((h) => !String(h.headers.authorization).includes(TOKEN)),\n+\t\t\t\"the token file was never sent\",\n+\t\t);\n+\t});\n+\n+\tit(\"init with only a token file, no flag: refused before anything is made, naming --token-file\", async () => {\n+\t\tconst run = await client(home(), folder(), \"init\", \"--project\", \"unasked\", \"--agent\", \"me\", \"--server\", fake.url);\n+\t\tassert.notEqual(run.code, 0);\n+\t\tassert.match(run.stderr, /abso login signs it in\\. ~\\/\\.abso\\/token is used only with --token-file\\./);\n+\t\tconst status = await fetch(`${fake.url}/p/unasked/status`, { headers: { authorization: `Bearer ${TOKEN}` } });\n+\t\tassert.equal(status.status, 404, \"no project was made\");\n+\t});\n+\n+\tit(\"init and join with --token-file: sign in with the file, and say so\", async () => {\n+\t\tconst homeDir = home();\n+\t\tconst made = await client(\n+\t\t\thomeDir,\n+\t\t\tfolder(),\n+\t\t\t\"init\",\n+\t\t\t\"--project\",\n+\t\t\t\"asked\",\n+\t\t\t\"--agent\",\n+\t\t\t\"me\",\n+\t\t\t\"--token-file\",\n+\t\t\t\"--server\",\n+\t\t\tfake.url,\n+\t\t);\n+\t\tassert.equal(made.code, 0, made.stderr);\n+\t\tassert.match(made.stderr, /Signing in with the token file, ~\\/\\.abso\\/token, as --token-file asks\\./);\n+\t\tassert.match(made.stdout, /Created project asked/);\n+\t\tconst unasked = await client(homeDir, temp(), \"join\", \"asked\", \"--agent\", \"other\", \"--server\", fake.url);\n+\t\tassert.notEqual(unasked.code, 0, \"join without the flag sends no token\");\n+\t\tassert.match(unasked.stderr, /~\\/\\.abso\\/token is used only with --token-file\\./);\n+\t\tconst joined = await client(\n+\t\t\thomeDir,\n+\t\t\ttemp(),\n+\t\t\t\"join\",\n+\t\t\t\"asked\",\n+\t\t\t\"--agent\",\n+\t\t\t\"other\",\n+\t\t\t\"--token-file\",\n+\t\t\t\"--server\",\n+\t\t\tfake.url,\n+\t\t);\n+\t\tassert.equal(joined.code, 0, joined.stderr);\n+\t\tassert.match(joined.stderr, /Signing in with the token file/);\n+\t});\n+});"}],"unmatched":[]}