Versions 8 to 318
new file, 158 lines
1+// abso flag: a server's feature flags listed, flipped with a reason, and audited. The admin token goes only to a server 2+// ABSO_ADMIN_SERVERS names; the folder's own key goes with every call, for a server that takes an owner's key. Every 3+// test passes its own ABSO_ADMIN_TOKEN, so this machine's ~/.abso is never read. 4+import assert from "node:assert/strict"; 5+import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; 6+import { tmpdir } from "node:os"; 7+import { join } from "node:path"; 8+import { test } from "node:test"; 9+import { parseArgs } from "../src/args.ts"; 10+import { adminFor, changeText, type FlagChange, flagListText, type ListedFlag, runFlag } from "../src/flag-commands.ts"; 12+const SERVER = "http://localhost:8787"; 13+const KEY = "abso_sk_owner-key-value"; 14+const ADMIN = "admin-token-value"; 15+const FLAGS = { why: "string", project: "string", person: "string", limit: "string" } as const; 17+function folder(): string { 18+ const root = mkdtempSync(join(tmpdir(), "abso-flag-")); 19+ mkdirSync(join(root, ".abso")); 20+ writeFileSync(join(root, ".abso", "config.json"), JSON.stringify({ server: SERVER, project: "abso", agent: "me" })); 33+ env: Record<string, string>, 34+ answer: { status: number; body: unknown }, 37+ const previous = globalThis.fetch; 38+ globalThis.fetch = (async (url, init) => { 41+ method: init?.method ?? "GET", 42+ headers: new Headers(init?.headers), 43+ ...(typeof init?.body === "string" ? { body: JSON.parse(init.body) } : {}), 45+ return new Response(JSON.stringify(answer.body), { status: answer.status }); 48+ const out = await runFlag(parseArgs("flag", argv, FLAGS), { 50+ env: { ABSO_TOKEN: KEY, ...env }, 53+ return { out, seen }; 55+ globalThis.fetch = previous; 59+const change: FlagChange = { 60+ at: "2026-10-15T19:04:00.000Z", 61+ by: "key k_1, box b", 62+ flag: "autosettle-default", 63+ scope: { project: "game" }, 66+ why: "trying Settle on game", 69+test("the admin token goes only to a server ABSO_ADMIN_SERVERS names, and is never needed to try", () => { 70+ assert.equal(adminFor(SERVER, { ABSO_ADMIN_TOKEN: ADMIN }), undefined, "no server named"); 71+ assert.equal(adminFor(SERVER, { ABSO_ADMIN_TOKEN: ADMIN, ABSO_ADMIN_SERVERS: "https://api.abso.dev" }), undefined); 72+ assert.equal(adminFor(SERVER, { ABSO_ADMIN_TOKEN: ADMIN, ABSO_ADMIN_SERVERS: SERVER }), ADMIN); 75+test("list says each flag's value, what it takes, its safe value, what it gates, and every place it's set", () => { 76+ const flags: Record<string, ListedFlag> = { 77+ "byoc-demo": { values: ["on", "off"], safe: "on", what: "The BYOC demo page.", stagingOnly: true }, 78+ "autosettle-default": { 79+ values: ["off", "advise", "settle"], 81+ what: "Auto-settle's mode for an unpicked project.", 83+ projects: { game: "settle" }, 84+ people: { "person:p1": "off" }, 90+ "autosettle-default: advise. Takes off, advise, settle; safe off.", 91+ " Auto-settle's mode for an unpicked project.", 92+ " project game: settle", 94+ "byoc-demo: on (unset). Takes on, off; safe on; staging only.", 95+ " The BYOC demo page.", 98+ assert.equal(flagListText({}), "No flags."); 101+test("a change in a line: when, who, which flag where, old to new, and why", () => { 103+ changeText(change, Date.parse("2026-10-15T20:00:00Z")), 104+ '19:04 UTC key k_1, box b autosettle-default project game: unset to settle. "trying Settle on game"', 106+ assert.match(changeText({ ...change, scope: { env: true }, old: "settle", new: null }), / here: settle to unset\./); 109+test("set posts the value, scope and reason with the folder's key; the admin token only where it may go", async () => { 110+ const { out, seen } = await run( 111+ ["set", "autosettle-default", "settle", "--project", "game", "--why", "trying Settle on game"], 112+ { ABSO_ADMIN_TOKEN: ADMIN }, 113+ { status: 200, body: { revision: 3, change } }, 115+ assert.equal(seen.length, 1); 116+ assert.equal(seen[0]!.url, `${SERVER}/admin/flags/autosettle-default`); 117+ assert.equal(seen[0]!.method, "POST"); 118+ assert.deepEqual(seen[0]!.body, { value: "settle", why: "trying Settle on game", project: "game" }); 119+ assert.equal(seen[0]!.headers.get("authorization"), `Bearer ${KEY}`); 120+ assert.equal(seen[0]!.headers.get("x-abso-admin"), null, "ABSO_ADMIN_SERVERS doesn't name it"); 121+ assert.match(out.text, /unset to settle/); 122+ const named = await run( 123+ ["unset", "autosettle-default", "--why", "back to the default"], 124+ { ABSO_ADMIN_TOKEN: ADMIN, ABSO_ADMIN_SERVERS: SERVER }, 125+ { status: 200, body: { revision: 4, change: { ...change, scope: { env: true }, old: "advise", new: null } } }, 127+ assert.equal(named.seen[0]!.headers.get("x-abso-admin"), ADMIN); 128+ assert.deepEqual(named.seen[0]!.body, { value: null, why: "back to the default" }); 131+test("a change without a reason, or for a project and a person both, is refused before anything is sent", async () => { 132+ for (const argv of [ 133+ ["set", "autosettle-default", "advise"], 134+ ["set", "autosettle-default", "advise", "--why", "x"], 135+ ["set", "autosettle-default", "advise", "--why", "both", "--project", "a", "--person", "person:b"], 136+ ["set", "autosettle-default", "advise", "--why", "bad person", "--person", "p1"], 137+ ["unset", "autosettle-default", "advise", "--why", "too many words"], 138+ ["flip", "autosettle-default"], 140+ const seen: Seen[] = []; 141+ await assert.rejects(run(argv, {}, { status: 200, body: {} }, seen), argv.join(" ")); 142+ assert.equal(seen.length, 0, argv.join(" ")); 146+test("the server's refusal is said in its words, with no credential in it", async () => { 147+ await assert.rejects( 148+ run(["list"], {}, { status: 403, body: { error: "flags take the admin token here" } }), 149+ /flags take the admin token here/, 151+ await assert.rejects( 152+ run(["log"], {}, { status: 400, body: { error: `bad ${KEY}` } }), 153+ (e: Error) => !e.message.includes(KEY) && /\[redacted\]/.test(e.message), 155+ const { out, seen } = await run(["log", "--limit", "5"], {}, { status: 200, body: { changes: [change] } }); 156+ assert.equal(seen[0]!.url, `${SERVER}/admin/flags/audit?limit=5`); 157+ assert.match(out.text, /trying Settle on game/);