- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/test/flag-command.test.ts?format=json
158 linesreadspeed 100%Last change readspeed in version 261, 1 version changed it
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.
4import assert from "node:assert/strict";
5import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
6import { tmpdir } from "node:os";
7import { join } from "node:path";
8import { test } from "node:test";
9import { parseArgs } from "../src/args.ts";
10import { adminFor, changeText, type FlagChange, flagListText, type ListedFlag, runFlag } from "../src/flag-commands.ts";
12const SERVER = "http://localhost:8787";
13const KEY = "abso_sk_owner-key-value";
14const ADMIN = "admin-token-value";
15const FLAGS = { why: "string", project: "string", person: "string", limit: "string" } as const;
17function 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" }));
21 return root;
22}
24interface Seen {
25 url: string;
26 method: string;
27 headers: Headers;
28 body?: unknown;
29}
31async function run(
32 argv: string[],
33 env: Record<string, string>,
34 answer: { status: number; body: unknown },
35 seen: Seen[] = [],
36) {
37 const previous = globalThis.fetch;
38 globalThis.fetch = (async (url, init) => {
39 seen.push({
40 url: String(url),
41 method: init?.method ?? "GET",
42 headers: new Headers(init?.headers),
43 ...(typeof init?.body === "string" ? { body: JSON.parse(init.body) } : {}),
44 });
45 return new Response(JSON.stringify(answer.body), { status: answer.status });
46 }) as typeof fetch;
47 try {
48 const out = await runFlag(parseArgs("flag", argv, FLAGS), {
49 cwd: folder(),
50 env: { ABSO_TOKEN: KEY, ...env },
51 write: () => {},
52 });
53 return { out, seen };
54 } finally {
55 globalThis.fetch = previous;
56 }
57}
59const 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" },
64 old: null,
65 new: "settle",
66 why: "trying Settle on game",
67};
69test("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);
73});
75test("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"],
80 safe: "off",
81 what: "Auto-settle's mode for an unpicked project.",
82 env: "advise",
83 projects: { game: "settle" },
84 people: { "person:p1": "off" },
85 },
86 };
87 assert.equal(
88 flagListText(flags),
89 [
90 "autosettle-default: advise. Takes off, advise, settle; safe off.",
91 " Auto-settle's mode for an unpicked project.",
92 " project game: settle",
93 " person:p1: off",
94 "byoc-demo: on (unset). Takes on, off; safe on; staging only.",
95 " The BYOC demo page.",
96 ].join("\n"),
97 );
98 assert.equal(flagListText({}), "No flags.");
99});
101test("a change in a line: when, who, which flag where, old to new, and why", () => {
102 assert.equal(
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"',
105 );
106 assert.match(changeText({ ...change, scope: { env: true }, old: "settle", new: null }), / here: settle to unset\./);
107});
109test("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 } },
114 );
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 } } },
126 );
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" });
129});
131test("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"],
139 ]) {
140 const seen: Seen[] = [];
141 await assert.rejects(run(argv, {}, { status: 200, body: {} }, seen), argv.join(" "));
142 assert.equal(seen.length, 0, argv.join(" "));
143 }
144});
146test("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/,
150 );
151 await assert.rejects(
152 run(["log"], {}, { status: 400, body: { error: `bad ${KEY}` } }),
153 (e: Error) => !e.message.includes(KEY) && /\[redacted\]/.test(e.message),
154 );
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/);
158});