Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/client/test/visitor.test.ts?version=253&format=json
342 linespinch 99% · rename 1% · autosettle 1% · open 1%Last change autosettle in version 220, 6 versions changed it

You're reading version 253. The newest is 318, and 2 versions since then changed this file. Go to the newestCompare with 318

1// A visitor folder: abso join <project> --reader with no sign-in, for a public project. It reads
2// through the routes anyone can read and sends no credential. The rules, each a test:
3// - no Authorization header and no cookie go out, with a token in the environment and a box sign-in
4// present, and no redirect is followed;
5// - join answers a private project and a missing one alike, by the status answer's code alone;
6// - the commands it runs are an allow-list, and a new command fails this test until it's sorted;
7// - its config comes from the command line only, never from an answer;
8// - what comes back goes through the terminal filter, a 429's text too;
9// - show prints a file and writes nothing, even for a path outside the folder.
11import assert from "node:assert/strict";
12import { execFile } from "node:child_process";
13import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs";
14import { createServer, type IncomingHttpHeaders, type Server } from "node:http";
15import type { AddressInfo } from "node:net";
16import { tmpdir } from "node:os";
17import { join, resolve } from "node:path";
18import { after, before, describe, it } from "node:test";
19import { Api } from "../src/api.ts";
20import { writeBox } from "../src/box.ts";
21import { writeConfig } from "../src/config.ts";
22import { isPublic } from "../src/join-commands.ts";
23import { commandTable, main } from "../src/main.ts";
24import { VISITOR_ELSEWHERE, VISITOR_READS } from "../src/visitor.ts";
26const CLIENT = resolve(import.meta.dirname, "..", "abso.ts");
27const ESC = "\x1b";
28const TOKEN = "abso_test-token-never-sent";
29const CREDENTIAL = "abso_bx_test-credential-never-sent";
31/** Every command a visitor folder refuses. A new command fails the allow-list test until it's here or a read. */
32const REFUSED = [
33 "init",
34 "key",
35 "box",
36 "whoami",
37 "doctor",
38 "orgs",
39 "org",
40 "skill",
41 "join",
42 "record",
43 "push",
44 "bisect",
45 "reader",
46 "settle",
47 "pull",
48 "run",
49 "serve",
50 "mv",
51 "review",
52 "note",
53 "notes",
54 "ask",
55 "waiting",
56 "answer",
57 "ack",
58 "reply",
59 "unqueue",
60 "visibility",
61 "autosettle",
62 "ci",
63 "shot",
64 "prep",
65 "fetch",
66 "pause",
67 "resume",
68 "logs",
69 "switch",
70 "land",
71 "draft-pull",
72 "diff",
73 "restore",
74 "export",
75 "announce",
76 "start",
77 "news",
78 "fix-eol",
79 "who",
80 "drop",
81 "pluck",
82 "amend",
83 "label",
84 "labels",
85 "follow",
86 "check-ignore",
87 "pipeline",
88];
90let site: Server;
91let url: string;
92// Where the site's redirect points: a visitor never gets here.
93let elsewhere: Server;
94let elsewhereUrl: string;
95const heard: { url: string; headers: IncomingHttpHeaders }[] = [];
96let elsewhereHeard = 0;
97const dirs: string[] = [];
98const temp = (name: string) => {
99 const dir = mkdtempSync(join(tmpdir(), `abso-test-visitor-${name}-`));
100 dirs.push(dir);
101 return dir;
102};
104const json = (res: import("node:http").ServerResponse, status: number, body: unknown, headers = {}) =>
105 res.writeHead(status, { "content-type": "application/json", ...headers }).end(JSON.stringify(body));
107before(async () => {
108 elsewhere = createServer((_req, res) => {
109 elsewhereHeard++;
110 json(res, 200, { version: 9, agents: [] });
111 });
112 await new Promise<void>((done) => elsewhere.listen(0, "127.0.0.1", done));
113 elsewhereUrl = `http://127.0.0.1:${(elsewhere.address() as AddressInfo).port}`;
114 site = createServer((req, res) => {
115 heard.push({ url: req.url ?? "", headers: req.headers });
116 const path = new URL(req.url ?? "/", "http://x").pathname;
117 if (path === "/p/abso-core/status")
118 // An answer naming another server: the folder's config never takes it.
119 return json(res, 200, { version: 3, agents: [], server: "http://answer.example" });
120 if (path === "/p/abso-core/file")
121 return json(res, 200, { path: "a.txt", version: 1, text: `hi ${ESC}]52;c;aGk=\x07 there\n` });
122 if (path === "/p/abso-core/log")
123 return json(
124 res,
125 429,
126 { error: `too many signed-out reads from this address: 120 a minute; sign in, or try again ${ESC}[2Jlater` },
127 { "Retry-After": "60" },
128 );
129 if (path === "/p/abso-core/where")
130 return res.writeHead(302, { location: `${elsewhereUrl}/p/abso-core/where` }).end();
131 // A private project answers 401 with a body that looks like a status: only the code counts.
132 if (path === "/p/private/status") return json(res, 401, { version: 1, agents: [] });
133 if (path === "/p/forbidden/status") return json(res, 403, { error: "members only" });
134 if (path === "/p/missing/status") return json(res, 404, { error: "no such project" });
135 json(res, 401, { error: "sign in" });
136 });
137 await new Promise<void>((done) => site.listen(0, "127.0.0.1", done));
138 url = `http://127.0.0.1:${(site.address() as AddressInfo).port}`;
139});
140after(() => {
141 site.close();
142 elsewhere.close();
143 for (const dir of dirs) rmSync(dir, { recursive: true, force: true });
144});
146/** Runs the client as its own process, in `cwd`, with `home` as its home and only the environment given. */
147function client(
148 cwd: string,
149 home: string,
150 args: string[],
151 extra: Record<string, string> = {},
152): Promise<{ code: number; stdout: string; stderr: string }> {
153 const env: Record<string, string> = {
154 PATH: process.env.PATH ?? "",
155 HOME: home,
156 USERPROFILE: home,
157 AVCS_SERVERS_FILE: "no-servers.json",
158 ABSO_ISSUES_SERVER: url,
159 ...extra,
160 };
161 if (process.env.SystemRoot) env.SystemRoot = process.env.SystemRoot;
162 return new Promise((done) => {
163 execFile(process.execPath, [CLIENT, ...args], { cwd, env }, (error, stdout, stderr) =>
164 done({ code: typeof error?.code === "number" ? error.code : 0, stdout, stderr }),
165 );
166 });
169/** A visitor folder made by abso join, signed out, and the home it was made in. */
170async function joined(): Promise<{ root: string; home: string; out: string }> {
171 const home = temp("home");
172 // In a parent of its own, so a test can tell nothing was written beside it.
173 const root = join(temp("parent"), "folder");
174 mkdirSync(root);
175 const run = await client(root, home, ["join", "abso-core", "--reader", "--server", url]);
176 assert.equal(run.code, 0, run.stderr);
177 return { root, home, out: run.stdout };
180describe("a visitor folder", () => {
181 it("join --reader, signed out, to a public project writes only its config, from the command line", async () => {
182 const { root, out } = await joined();
183 assert.match(out, /Joined abso-core to read it signed out, at version 3/);
184 assert.deepEqual(readdirSync(root), [".abso"]);
185 assert.deepEqual(readdirSync(join(root, ".abso")), ["config.json"]);
186 // The status answer named another server; the config keeps the one typed.
187 assert.deepEqual(JSON.parse(readFileSync(join(root, ".abso", "config.json"), "utf8")), {
188 server: url,
189 project: "abso-core",
190 visitor: true,
191 });
192 });
194 it("sends no Authorization and no cookie, with a token set and a box sign-in present, and follows no redirect", async () => {
195 const { root, home } = await joined();
196 writeBox(
197 {
198 server: url,
199 id: "b_0123abcd",
200 credential: CREDENTIAL,
201 grant: { projects: [{ project: "abso-core", role: "writer" }] },
202 expiresAt: Date.now() + 86_400_000,
203 },
204 home,
205 );
206 const tokens = { ABSO_TOKEN: TOKEN, AVCS_TOKEN: TOKEN };
207 heard.length = 0;
208 elsewhereHeard = 0;
209 assert.equal((await client(root, home, ["status"], tokens)).code, 0);
210 assert.equal((await client(root, home, ["show", "1:a.txt"], tokens)).code, 0);
211 const where = await client(root, home, ["where"], tokens);
212 assert.notEqual(where.code, 0);
213 assert.ok(heard.length >= 3);
214 for (const { headers } of heard) {
215 assert.equal(headers.authorization, undefined);
216 assert.equal(headers.cookie, undefined);
217 assert.doesNotMatch(JSON.stringify(headers), /never-sent/);
218 }
219 assert.equal(elsewhereHeard, 0);
220 });
222 it("asks fetch to follow no redirect and sends no credential, join's public check too (a fetch spy)", async () => {
223 const root = temp("spy");
224 writeConfig(root, { server: url, project: "abso-core", agent: "", visitor: true });
225 const real = globalThis.fetch;
226 const calls: RequestInit[] = [];
227 globalThis.fetch = (async (_input: unknown, init?: RequestInit) => {
228 calls.push(init ?? {});
229 return new Response(JSON.stringify({ version: 3, agents: [] }), { status: 200 });
230 }) as typeof fetch;
231 try {
232 const out = await main(["status"], { cwd: root, env: { ABSO_TOKEN: TOKEN }, write: () => {} });
233 assert.equal(out.exit, 0, out.stderr);
234 assert.equal(await isPublic(url, "abso-core"), true);
235 } finally {
236 globalThis.fetch = real;
237 }
238 assert.equal(calls.length, 2);
239 for (const init of calls) {
240 assert.equal(init.redirect, "error");
241 const headers = new Headers(init.headers);
242 assert.equal(headers.get("authorization"), null);
243 assert.equal(headers.get("cookie"), null);
244 }
245 });
247 it("join answers a private project, a members-only one and a missing one alike, by the code alone", async () => {
248 const runs = [];
249 for (const project of ["private", "forbidden", "missing"]) {
250 const root = temp("closed");
251 runs.push({ root, ...(await client(root, temp("home"), ["join", project, "--reader", "--server", url])) });
252 }
253 for (const run of runs) {
254 assert.equal(run.code, runs[0]!.code);
255 assert.notEqual(run.code, 0);
256 assert.equal(run.stderr, runs[0]!.stderr);
257 assert.equal(existsSync(join(run.root, ".abso")), false);
258 }
259 assert.match(runs[0]!.stderr, /isn't signed in to .*abso login signs it in/);
260 });
262 it("every request its Api makes goes through one send: reads only, no redirect, no sign-in", async () => {
263 const real = globalThis.fetch;
264 const calls: RequestInit[] = [];
265 globalThis.fetch = (async (_input: unknown, init?: RequestInit) => {
266 calls.push(init ?? {});
267 return new Response("{}", { status: 200 });
268 }) as typeof fetch;
269 try {
270 const visitor = new Api({ server: url, project: "abso-core", agent: "", visitor: true, token: TOKEN });
271 await visitor.send(`${url}/p/abso-core/serve`, { headers: visitor.headers() });
272 await assert.rejects(visitor.send(`${url}/p/abso-core/a/x/run`, { method: "POST" }), /sends only reads/);
273 const member = new Api({ server: url, project: "abso-core", agent: "x", token: TOKEN });
274 await member.send(`${url}/p/abso-core/serve`, { headers: member.headers() });
275 } finally {
276 globalThis.fetch = real;
277 }
278 assert.equal(calls.length, 2);
279 assert.equal(calls[0]!.redirect, "error");
280 assert.equal(new Headers(calls[0]!.headers).get("authorization"), null);
281 assert.equal(calls[1]!.redirect, undefined);
282 assert.equal(new Headers(calls[1]!.headers).get("authorization"), `Bearer ${TOKEN}`);
283 });
285 it("runs only its reads: every other command is refused before any call, and a new command fails here", async () => {
286 const sorted = new Set([...Object.keys(VISITOR_READS), ...VISITOR_ELSEWHERE, ...REFUSED]);
287 for (const name of Object.keys(commandTable()))
288 assert.ok(
289 sorted.has(name),
290 `abso ${name} is new: add it to VISITOR_READS if it only reads, else to REFUSED here`,
291 );
292 const root = temp("refuse");
293 writeConfig(root, { server: url, project: "abso-core", agent: "", visitor: true });
294 const real = globalThis.fetch;
295 let calls = 0;
296 globalThis.fetch = (async () => {
297 calls++;
298 return new Response("{}", { status: 200 });
299 }) as typeof fetch;
300 try {
301 const tries = [
302 ...REFUSED.map((c) => [c]),
303 ["issue", "new", "a title"],
304 ["issue", "3", "--comment", "hi"],
305 ["issues", "--drafts"],
306 ["blame", "a.txt", "--mine"],
307 ];
308 for (const argv of tries) {
309 const out = await main(argv, { cwd: root, env: { ABSO_TOKEN: TOKEN }, write: () => {} });
310 assert.notEqual(out.exit, 0, argv.join(" "));
311 assert.match(out.stderr, /This folder reads abso-core signed out\. To work on it: abso login/, argv.join(" "));
312 }
313 } finally {
314 globalThis.fetch = real;
315 }
316 assert.equal(calls, 0);
317 });
319 it("prints what comes back through the terminal filter, a 429's text too", async () => {
320 const { root, home } = await joined();
321 const show = await client(root, home, ["show", "1:a.txt"]);
322 assert.equal(show.code, 0, show.stderr);
323 assert.doesNotMatch(show.stdout, /\x1b/);
324 assert.match(show.stdout, /hi \\x1b\]52;c;aGk=\\x07 there/);
325 const log = await client(root, home, ["log"]);
326 assert.notEqual(log.code, 0);
327 assert.doesNotMatch(log.stderr + log.stdout, /\x1b/);
328 assert.match(log.stderr, /too many signed-out reads from this address/);
329 });
331 it("show prints a file and writes nothing, even for a path outside the folder", async () => {
332 const { root, home } = await joined();
333 const parent = resolve(root, "..");
334 const before = { here: readdirSync(root).sort(), above: readdirSync(parent).sort() };
335 await client(root, home, ["show", "1:a.txt"]);
336 const outside = await client(root, home, ["show", "1:../x"]);
337 assert.notEqual(outside.code, 0);
338 assert.deepEqual(readdirSync(root).sort(), before.here);
339 assert.deepEqual(readdirSync(parent).sort(), before.above);
340 assert.equal(existsSync(join(parent, "x")), false);
341 });
342});