{"draft":null,"from":116,"to":318,"files":[{"path":"client/test/flag-command.test.ts","added":158,"removed":0,"diff":"--- /dev/null\n+++ b/client/test/flag-command.test.ts\n@@ -0,0 +1,158 @@\n+// abso flag: a server's feature flags listed, flipped with a reason, and audited. The admin token goes only to a server\n+// ABSO_ADMIN_SERVERS names; the folder's own key goes with every call, for a server that takes an owner's key. Every\n+// test passes its own ABSO_ADMIN_TOKEN, so this machine's ~/.abso is never read.\n+import assert from \"node:assert/strict\";\n+import { mkdirSync, mkdtempSync, writeFileSync } from \"node:fs\";\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 { adminFor, changeText, type FlagChange, flagListText, type ListedFlag, runFlag } from \"../src/flag-commands.ts\";\n+\n+const SERVER = \"http://localhost:8787\";\n+const KEY = \"abso_sk_owner-key-value\";\n+const ADMIN = \"admin-token-value\";\n+const FLAGS = { why: \"string\", project: \"string\", person: \"string\", limit: \"string\" } as const;\n+\n+function folder(): string {\n+\tconst root = mkdtempSync(join(tmpdir(), \"abso-flag-\"));\n+\tmkdirSync(join(root, \".abso\"));\n+\twriteFileSync(join(root, \".abso\", \"config.json\"), JSON.stringify({ server: SERVER, project: \"abso\", agent: \"me\" }));\n+\treturn root;\n+}\n+\n+interface Seen {\n+\turl: string;\n+\tmethod: string;\n+\theaders: Headers;\n+\tbody?: unknown;\n+}\n+\n+async function run(\n+\targv: string[],\n+\tenv: Record<string, string>,\n+\tanswer: { status: number; body: unknown },\n+\tseen: Seen[] = [],\n+) {\n+\tconst previous = globalThis.fetch;\n+\tglobalThis.fetch = (async (url, init) => {\n+\t\tseen.push({\n+\t\t\turl: String(url),\n+\t\t\tmethod: init?.method ?? \"GET\",\n+\t\t\theaders: new Headers(init?.headers),\n+\t\t\t...(typeof init?.body === \"string\" ? { body: JSON.parse(init.body) } : {}),\n+\t\t});\n+\t\treturn new Response(JSON.stringify(answer.body), { status: answer.status });\n+\t}) as typeof fetch;\n+\ttry {\n+\t\tconst out = await runFlag(parseArgs(\"flag\", argv, FLAGS), {\n+\t\t\tcwd: folder(),\n+\t\t\tenv: { ABSO_TOKEN: KEY, ...env },\n+\t\t\twrite: () => {},\n+\t\t});\n+\t\treturn { out, seen };\n+\t} finally {\n+\t\tglobalThis.fetch = previous;\n+\t}\n+}\n+\n+const change: FlagChange = {\n+\tat: \"2026-10-15T19:04:00.000Z\",\n+\tby: \"key k_1, box b\",\n+\tflag: \"autosettle-default\",\n+\tscope: { project: \"game\" },\n+\told: null,\n+\tnew: \"settle\",\n+\twhy: \"trying Settle on game\",\n+};\n+\n+test(\"the admin token goes only to a server ABSO_ADMIN_SERVERS names, and is never needed to try\", () => {\n+\tassert.equal(adminFor(SERVER, { ABSO_ADMIN_TOKEN: ADMIN }), undefined, \"no server named\");\n+\tassert.equal(adminFor(SERVER, { ABSO_ADMIN_TOKEN: ADMIN, ABSO_ADMIN_SERVERS: \"https://api.abso.dev\" }), undefined);\n+\tassert.equal(adminFor(SERVER, { ABSO_ADMIN_TOKEN: ADMIN, ABSO_ADMIN_SERVERS: SERVER }), ADMIN);\n+});\n+\n+test(\"list says each flag's value, what it takes, its safe value, what it gates, and every place it's set\", () => {\n+\tconst flags: Record<string, ListedFlag> = {\n+\t\t\"byoc-demo\": { values: [\"on\", \"off\"], safe: \"on\", what: \"The BYOC demo page.\", stagingOnly: true },\n+\t\t\"autosettle-default\": {\n+\t\t\tvalues: [\"off\", \"advise\", \"settle\"],\n+\t\t\tsafe: \"off\",\n+\t\t\twhat: \"Auto-settle's mode for an unpicked project.\",\n+\t\t\tenv: \"advise\",\n+\t\t\tprojects: { game: \"settle\" },\n+\t\t\tpeople: { \"person:p1\": \"off\" },\n+\t\t},\n+\t};\n+\tassert.equal(\n+\t\tflagListText(flags),\n+\t\t[\n+\t\t\t\"autosettle-default: advise. Takes off, advise, settle; safe off.\",\n+\t\t\t\"  Auto-settle's mode for an unpicked project.\",\n+\t\t\t\"  project game: settle\",\n+\t\t\t\"  person:p1: off\",\n+\t\t\t\"byoc-demo: on (unset). Takes on, off; safe on; staging only.\",\n+\t\t\t\"  The BYOC demo page.\",\n+\t\t].join(\"\\n\"),\n+\t);\n+\tassert.equal(flagListText({}), \"No flags.\");\n+});\n+\n+test(\"a change in a line: when, who, which flag where, old to new, and why\", () => {\n+\tassert.equal(\n+\t\tchangeText(change, Date.parse(\"2026-10-15T20:00:00Z\")),\n+\t\t'19:04 UTC  key k_1, box b  autosettle-default project game: unset to settle. \"trying Settle on game\"',\n+\t);\n+\tassert.match(changeText({ ...change, scope: { env: true }, old: \"settle\", new: null }), / here: settle to unset\\./);\n+});\n+\n+test(\"set posts the value, scope and reason with the folder's key; the admin token only where it may go\", async () => {\n+\tconst { out, seen } = await run(\n+\t\t[\"set\", \"autosettle-default\", \"settle\", \"--project\", \"game\", \"--why\", \"trying Settle on game\"],\n+\t\t{ ABSO_ADMIN_TOKEN: ADMIN },\n+\t\t{ status: 200, body: { revision: 3, change } },\n+\t);\n+\tassert.equal(seen.length, 1);\n+\tassert.equal(seen[0]!.url, `${SERVER}/admin/flags/autosettle-default`);\n+\tassert.equal(seen[0]!.method, \"POST\");\n+\tassert.deepEqual(seen[0]!.body, { value: \"settle\", why: \"trying Settle on game\", project: \"game\" });\n+\tassert.equal(seen[0]!.headers.get(\"authorization\"), `Bearer ${KEY}`);\n+\tassert.equal(seen[0]!.headers.get(\"x-abso-admin\"), null, \"ABSO_ADMIN_SERVERS doesn't name it\");\n+\tassert.match(out.text, /unset to settle/);\n+\tconst named = await run(\n+\t\t[\"unset\", \"autosettle-default\", \"--why\", \"back to the default\"],\n+\t\t{ ABSO_ADMIN_TOKEN: ADMIN, ABSO_ADMIN_SERVERS: SERVER },\n+\t\t{ status: 200, body: { revision: 4, change: { ...change, scope: { env: true }, old: \"advise\", new: null } } },\n+\t);\n+\tassert.equal(named.seen[0]!.headers.get(\"x-abso-admin\"), ADMIN);\n+\tassert.deepEqual(named.seen[0]!.body, { value: null, why: \"back to the default\" });\n+});\n+\n+test(\"a change without a reason, or for a project and a person both, is refused before anything is sent\", async () => {\n+\tfor (const argv of [\n+\t\t[\"set\", \"autosettle-default\", \"advise\"],\n+\t\t[\"set\", \"autosettle-default\", \"advise\", \"--why\", \"x\"],\n+\t\t[\"set\", \"autosettle-default\", \"advise\", \"--why\", \"both\", \"--project\", \"a\", \"--person\", \"person:b\"],\n+\t\t[\"set\", \"autosettle-default\", \"advise\", \"--why\", \"bad person\", \"--person\", \"p1\"],\n+\t\t[\"unset\", \"autosettle-default\", \"advise\", \"--why\", \"too many words\"],\n+\t\t[\"flip\", \"autosettle-default\"],\n+\t]) {\n+\t\tconst seen: Seen[] = [];\n+\t\tawait assert.rejects(run(argv, {}, { status: 200, body: {} }, seen), argv.join(\" \"));\n+\t\tassert.equal(seen.length, 0, argv.join(\" \"));\n+\t}\n+});\n+\n+test(\"the server's refusal is said in its words, with no credential in it\", async () => {\n+\tawait assert.rejects(\n+\t\trun([\"list\"], {}, { status: 403, body: { error: \"flags take the admin token here\" } }),\n+\t\t/flags take the admin token here/,\n+\t);\n+\tawait assert.rejects(\n+\t\trun([\"log\"], {}, { status: 400, body: { error: `bad ${KEY}` } }),\n+\t\t(e: Error) => !e.message.includes(KEY) && /\\[redacted\\]/.test(e.message),\n+\t);\n+\tconst { out, seen } = await run([\"log\", \"--limit\", \"5\"], {}, { status: 200, body: { changes: [change] } });\n+\tassert.equal(seen[0]!.url, `${SERVER}/admin/flags/audit?limit=5`);\n+\tassert.match(out.text, /trying Settle on game/);\n+});"}],"unmatched":[]}