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