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=190&format=json
341 linespinch 99% · rename 1% · open 1%Last change open in version 171, 5 versions changed it

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