{"draft":null,"from":2,"to":318,"files":[{"path":"client/test/server-flag.test.ts","added":267,"removed":0,"diff":"--- /dev/null\n+++ b/client/test/server-flag.test.ts\n@@ -0,0 +1,267 @@\n+// --server on every command that talks to a server (abso-core issue 10). The rules, each a test:\n+// - every command is sorted: one that works on this machine only takes no --server and says so; every other one\n+//   takes it, and help says the same words about it for each kind;\n+// - a missing, empty or malformed value is refused before anything is sent, for every family of command;\n+// - in a joined folder, --server naming the folder's own server reaches it; another server is refused, never\n+//   swapped in, and nothing is sent; outside a joined folder a folder command with --server is refused too;\n+// - an explicit --server goes to that server's own Issues Worker, which ABSO_ISSUES_SERVER doesn't redirect;\n+// - a command that picks its server (whoami, box, logout) uses the one named, even inside a folder on another;\n+// - with nothing named (abso-core issue 9) the server is the joined folder's, else ABSO_SERVER, else prod, whatever\n+//   this box is signed in to (doctor.test.ts has whoami with staging alone and with staging and prod both).\n+\n+import assert from \"node:assert/strict\";\n+import { mkdtempSync, rmSync, writeFileSync } from \"node:fs\";\n+import { createServer, type Server } from \"node:http\";\n+import type { AddressInfo } from \"node:net\";\n+import { tmpdir } from \"node:os\";\n+import { join } from \"node:path\";\n+import { after, before, describe, it } from \"node:test\";\n+import { writeConfig } from \"../src/config.ts\";\n+import { commandTable, main } from \"../src/main.ts\";\n+import { chooseServer, PROD_SERVER, SERVER_ORDER, sameServer, serverHelp, serverUse } from \"../src/server-choice.ts\";\n+\n+/** A made-up token: the tests never read the real ~/.abso/token. */\n+const TOKEN = \"abso_test-token-for-server-flag\";\n+\n+interface Listening {\n+\treadonly server: Server;\n+\treadonly url: string;\n+\treadonly paths: string[];\n+}\n+\n+/** A server that notes each path it is asked for and answers 500, so a command stops after its first call. */\n+async function listen(): Promise<Listening> {\n+\tconst paths: string[] = [];\n+\tconst server = createServer((req, res) => {\n+\t\tpaths.push(req.url ?? \"\");\n+\t\tres.writeHead(500, { \"content-type\": \"application/json\" });\n+\t\tres.end(JSON.stringify({ error: \"a test server\" }));\n+\t});\n+\tawait new Promise<void>((done) => server.listen(0, \"127.0.0.1\", done));\n+\treturn { server, url: `http://127.0.0.1:${(server.address() as AddressInfo).port}`, paths };\n+}\n+\n+const temps: string[] = [];\n+function temp(name: string): string {\n+\tconst dir = mkdtempSync(join(tmpdir(), `abso-server-flag-${name}-`));\n+\ttemps.push(dir);\n+\treturn dir;\n+}\n+\n+let home: Listening;\n+let other: Listening;\n+let issuesElsewhere: Listening;\n+let env: Record<string, string>;\n+\n+before(async () => {\n+\thome = await listen();\n+\tother = await listen();\n+\tissuesElsewhere = await listen();\n+\tconst servers = join(temp(\"servers\"), \"servers.json\");\n+\twriteFileSync(servers, \"{}\");\n+\tenv = { ABSO_TOKEN: TOKEN, ABSO_SERVERS_FILE: servers };\n+});\n+\n+after(() => {\n+\tfor (const l of [home, other, issuesElsewhere]) l.server.close();\n+\tfor (const dir of temps) rmSync(dir, { recursive: true, force: true });\n+});\n+\n+function folder(server = home.url): string {\n+\tconst root = temp(\"folder\");\n+\twriteConfig(root, { server, project: \"demo\", agent: \"alice\" });\n+\treturn root;\n+}\n+\n+const run = (cwd: string, argv: string[], extra: Record<string, string> = {}) =>\n+\tmain(argv, { cwd, env: { ...env, ...extra }, write: () => {} });\n+\n+function calls(): number {\n+\treturn home.paths.length + other.paths.length + issuesElsewhere.paths.length;\n+}\n+\n+function clear(): void {\n+\tfor (const l of [home, other, issuesElsewhere]) l.paths.length = 0;\n+}\n+\n+describe(\"--server on every command\", () => {\n+\tit(\"sorts every command: local ones take no --server, every other one does, and help says so alike\", async () => {\n+\t\tconst table = commandTable();\n+\t\tconst local = Object.keys(table).filter((name) => serverUse(name) === \"local\");\n+\t\tassert.deepEqual(local.sort(), [\"check-ignore\", \"fix-eol\", \"pause\", \"resume\", \"skill\"]);\n+\t\tfor (const [name, def] of Object.entries(table)) {\n+\t\t\tif (serverUse(name) === \"local\") {\n+\t\t\t\tassert.equal(def.flags.server, undefined, name);\n+\t\t\t\tassert.equal(serverHelp(name), undefined, name);\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tassert.equal(def.flags.server, \"string\", name);\n+\t\t\tconst help = await run(process.cwd(), [name, \"--help\"]);\n+\t\t\tassert.ok(help.stdout.includes(`  ${serverHelp(name)}\\n`), `abso ${name} --help names --server`);\n+\t\t}\n+\t\tconst data = JSON.parse((await run(process.cwd(), [\"help\", \"--json\"])).stdout) as {\n+\t\t\tcommands: { name: string; server?: string; flags: Record<string, string> }[];\n+\t\t};\n+\t\tfor (const c of data.commands.filter((c) => c.name in table)) assert.equal(c.server, serverHelp(c.name), c.name);\n+\t\tconst text = (await run(process.cwd(), [\"help\"])).stdout;\n+\t\tassert.match(text, /Every command that talks to a server takes --server <url>/);\n+\t});\n+\n+\tit(\"refuses --server on a command that works on this machine only\", async () => {\n+\t\tfor (const argv of [\n+\t\t\t[\"skill\", \"show\", \"--server\", home.url],\n+\t\t\t[\"check-ignore\", \"a.txt\", `--server=${home.url}`],\n+\t\t\t[\"pause\", \"--server\", home.url],\n+\t\t]) {\n+\t\t\tconst r = await run(folder(), argv);\n+\t\t\tassert.equal(r.exit, 1, argv.join(\" \"));\n+\t\t\tassert.match(r.stderr, /works on this machine only and talks to no server, so it takes no --server/);\n+\t\t}\n+\t\tconst words = await run(folder(), [\"check-ignore\", \"--\", \"--server\"]);\n+\t\tassert.doesNotMatch(words.stderr, /takes no --server/, \"a path after -- is a path\");\n+\t});\n+\n+\tit(\"refuses a missing, empty or malformed value before anything is sent, in every family\", async () => {\n+\t\tconst root = folder();\n+\t\tconst outside = temp(\"outside\");\n+\t\tclear();\n+\t\tconst families: [string, string[]][] = [\n+\t\t\t[root, [\"status\"]],\n+\t\t\t[root, [\"issue\", \"new\", \"A title\"]],\n+\t\t\t[root, [\"ask\", \"bob\", \"Why?\"]],\n+\t\t\t[root, [\"news\"]],\n+\t\t\t[root, [\"pipeline\", \"runs\"]],\n+\t\t\t[outside, [\"whoami\"]],\n+\t\t\t[outside, [\"login\"]],\n+\t\t\t[outside, [\"join\", \"demo\"]],\n+\t\t\t[outside, [\"org\", \"acme\"]],\n+\t\t\t[outside, [\"key\", \"list\"]],\n+\t\t];\n+\t\tfor (const [cwd, argv] of families) {\n+\t\t\tconst missing = await run(cwd, [...argv, \"--server\"]);\n+\t\t\tassert.match(missing.stderr, /--server needs a value/, argv.join(\" \"));\n+\t\t\tconst empty = await run(cwd, [...argv, \"--server=\"]);\n+\t\t\tassert.match(empty.stderr, /--server needs a server's URL, like https:\\/\\/api\\.abso\\.dev/, argv.join(\" \"));\n+\t\t\tfor (const bad of [\"api.abso.dev\", \"ftp://api.abso.dev\", \"https://me@api.abso.dev\", \"https://x.example/?a=1\"]) {\n+\t\t\t\tconst r = await run(cwd, [...argv, \"--server\", bad]);\n+\t\t\t\tassert.equal(r.exit, 1, `${argv.join(\" \")} --server ${bad}`);\n+\t\t\t\tassert.match(r.stderr, /--server takes an http or https URL with no user name, query or fragment/);\n+\t\t\t}\n+\t\t}\n+\t\tassert.equal(calls(), 0, \"nothing was sent\");\n+\t});\n+\n+\tit(\"in a joined folder, reaches the folder's own server and refuses another without sending anything\", async () => {\n+\t\tconst root = folder();\n+\t\tfor (const argv of [[\"status\"], [\"log\"], [\"issues\"], [\"issue\", \"1\"], [\"news\"], [\"waiting\"], [\"ci\"]]) {\n+\t\t\tclear();\n+\t\t\tconst same = await run(root, [...argv, \"--server\", `${home.url.toUpperCase().replace(\"HTTP\", \"http\")}/`]);\n+\t\t\tassert.doesNotMatch(same.stderr, /joined on/, argv.join(\" \"));\n+\t\t\tassert.ok(home.paths.length > 0, `abso ${argv.join(\" \")} reached the folder's server`);\n+\t\t\tassert.equal(other.paths.length, 0);\n+\t\t\tclear();\n+\t\t\tconst refused = await run(root, [...argv, \"--server\", other.url]);\n+\t\t\tassert.equal(refused.exit, 1);\n+\t\t\tassert.match(\n+\t\t\t\trefused.stderr,\n+\t\t\t\tnew RegExp(\n+\t\t\t\t\t`this folder is joined on ${home.url}, not ${other.url}, so abso ${argv[0]} won't send its work or key there`,\n+\t\t\t\t),\n+\t\t\t);\n+\t\t\tassert.equal(calls(), 0, `abso ${argv.join(\" \")} sent nothing`);\n+\t\t}\n+\t});\n+\n+\tit(\"outside a joined folder, a folder command with --server says how to join one there\", async () => {\n+\t\tconst outside = temp(\"outside\");\n+\t\tfor (const argv of [[\"status\"], [\"issue\", \"new\", \"A title\"], [\"ask\", \"bob\", \"Why?\"], [\"news\"]]) {\n+\t\t\tconst r = await run(outside, [...argv, \"--server\", home.url]);\n+\t\t\tassert.equal(r.exit, 1);\n+\t\t\tassert.match(r.stderr, new RegExp(`abso join <project> --server ${home.url}`));\n+\t\t}\n+\t\tassert.equal(calls(), 0);\n+\t});\n+\n+\tit(\"an explicit --server takes issues to that server's own Issues Worker, past ABSO_ISSUES_SERVER\", async () => {\n+\t\tconst root = folder();\n+\t\tconst elsewhere = { ABSO_ISSUES_SERVER: issuesElsewhere.url };\n+\t\tclear();\n+\t\tawait run(root, [\"issue\", \"1\"], elsewhere);\n+\t\tassert.ok(issuesElsewhere.paths.length > 0, \"without --server, ABSO_ISSUES_SERVER names the Issues Worker\");\n+\t\tassert.ok(home.paths.every((p) => !p.startsWith(\"/issues/\")));\n+\t\tclear();\n+\t\tawait run(root, [\"issue\", \"1\", \"--server\", home.url], elsewhere);\n+\t\tassert.equal(issuesElsewhere.paths.length, 0, \"--server isn't redirected\");\n+\t\tassert.ok(home.paths.length > 0);\n+\t\tassert.ok(\n+\t\t\thome.paths.some((p) => p.startsWith(\"/issues/p/demo/\")),\n+\t\t\t`the open server keeps issues at /issues/p/<project>: ${home.paths}`,\n+\t\t);\n+\t});\n+\n+\tit(\"a command that picks its server uses the one named, even in a folder on another\", async () => {\n+\t\tconst root = folder(other.url);\n+\t\tfor (const argv of [[\"whoami\"], [\"box\"], [\"logout\"]]) {\n+\t\t\tconst r = await run(root, [...argv, \"--server\", home.url]);\n+\t\t\tassert.match(r.stdout, new RegExp(home.url.replace(/\\./g, \"\\\\.\")), argv.join(\" \"));\n+\t\t}\n+\t\tclear();\n+\t\tconst doctor = await run(root, [\"doctor\", \"--server\", home.url]);\n+\t\tassert.match(doctor.stdout, new RegExp(`${home.url.replace(/\\./g, \"\\\\.\")} answered 500`));\n+\t\tassert.equal(other.paths.filter((p) => p === \"/version\").length, 0, \"doctor didn't swap in the folder's server\");\n+\t});\n+\n+\tit(\"counts two spellings of one server as the same, and different ports or paths as different\", () => {\n+\t\tassert.ok(sameServer(\"https://API.abso.dev/\", \"https://api.abso.dev\"));\n+\t\tassert.ok(!sameServer(\"http://127.0.0.1:8787\", \"http://127.0.0.1:8788\"));\n+\t\tassert.ok(!sameServer(\"http://127.0.0.1:8787\", \"http://localhost:8787\"));\n+\t\tassert.ok(!sameServer(\"https://x.example/a\", \"https://x.example/b\"));\n+\t});\n+});\n+\n+describe(\"the server when nothing names one\", () => {\n+\tconst STAGING = \"https://staging.abso.example\";\n+\n+\tit(\"is --server, else the joined folder's, else ABSO_SERVER, else prod\", () => {\n+\t\tconst outside = temp(\"outside\");\n+\t\tconst staging = folder(STAGING);\n+\t\tassert.equal(PROD_SERVER, \"https://api.abso.dev\");\n+\t\tassert.equal(chooseServer(undefined, outside, {}), PROD_SERVER, \"nothing named: prod\");\n+\t\tassert.equal(chooseServer(undefined, outside, { ABSO_SERVER: STAGING }), STAGING, \"the environment\");\n+\t\tassert.equal(chooseServer(undefined, outside, { AVCS_SERVER: STAGING }), STAGING, \"its older name\");\n+\t\tassert.equal(chooseServer(undefined, staging, {}), STAGING, \"a joined staging folder\");\n+\t\tassert.equal(chooseServer(undefined, staging, { ABSO_SERVER: \"https://env.example\" }), STAGING, \"folder first\");\n+\t\tassert.equal(chooseServer(PROD_SERVER, staging, { ABSO_SERVER: STAGING }), PROD_SERVER, \"explicit prod\");\n+\t\tassert.equal(chooseServer(STAGING, outside, {}), STAGING, \"explicit staging\");\n+\t\tassert.throws(\n+\t\t\t() => chooseServer(undefined, outside, { ABSO_SERVER: \"staging\" }),\n+\t\t\t/ABSO_SERVER takes an http or https URL/,\n+\t\t);\n+\t});\n+\n+\tit(\"help names the order once for every command that picks its server\", async () => {\n+\t\tassert.equal(\n+\t\t\tSERVER_ORDER,\n+\t\t\t\"--server <url>, else the server of the abso folder you're in, else ABSO_SERVER, else prod (https://api.abso.dev)\",\n+\t\t);\n+\t\tfor (const name of [\"join\", \"init\", \"login\", \"whoami\", \"doctor\", \"box\", \"logout\", \"orgs\", \"org\"])\n+\t\t\tassert.ok(serverHelp(name)?.includes(SERVER_ORDER), name);\n+\t\tassert.ok((await run(process.cwd(), [\"help\"])).stdout.includes(SERVER_ORDER));\n+\t});\n+\n+\tit(\"a joined folder's commands go to its server with nothing named, and ABSO_SERVER doesn't move them\", async () => {\n+\t\tconst root = folder();\n+\t\tclear();\n+\t\tawait run(root, [\"status\"], { ABSO_SERVER: other.url });\n+\t\tassert.ok(home.paths.length > 0);\n+\t\tassert.equal(other.paths.length, 0);\n+\t});\n+\n+\tit(\"whoami in a joined folder looks at the folder's server, and another named there is used as given\", async () => {\n+\t\tconst root = folder();\n+\t\tconst here = await run(root, [\"whoami\"], { ABSO_SERVER: other.url });\n+\t\tassert.ok(here.stdout.includes(`isn't signed in to ${home.url}`), here.stdout);\n+\t\tconst named = await run(root, [\"box\", \"--server\", other.url]);\n+\t\tassert.ok(named.stdout.includes(`isn't signed in to ${other.url}`), named.stdout);\n+\t});\n+});"}],"unmatched":[]}