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/issues.test.ts?draft=login-timeout&format=json
353 linespinch 53% · First import 38% · devops 5% · infra-split 4% · review 1%0 versions changed it
1// The Issues commands (#54) as far as the client alone decides them: which
2// Issues Worker a folder talks to, whether the token may go there, the MCP
3// tools, and the usage mistakes refused before any call. The Issues server
4// here is a stand-in that only counts calls; the commands against the real
5// store are issues/test/client-commands.test.ts, on the platform side (abso
6// issue 141).
7
8import assert from "node:assert/strict";
9import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
10import { createServer, type Server } from "node:http";
11import type { AddressInfo } from "node:net";
12import { tmpdir } from "node:os";
13import { join } from "node:path";
14import { after, before, describe, it } from "node:test";
15import { readConfig, readMachineServers } from "../src/config.ts";
16import { issuesServerFor, tokenGoesTo } from "../src/core.ts";
17import { issuePage, searchLines } from "../src/issues.ts";
18import type { IssuePage } from "../src/issues-wire.ts";
19import { main } from "../src/main.ts";
20import { argvFor, mcpTools } from "../src/mcp.ts";
21import { type Fake, startFake } from "./fake-server.ts";
23const TOKEN = "test-token-not-secret";
25let fake: Fake;
26let issues: Server;
27let issuesUrl: string;
28/** Every call the stand-in Issues server took. */
29const seen: string[] = [];
30/** The bodies of the copy requests it took. */
31const copies: Record<string, unknown>[] = [];
32const dirs: string[] = [];
34before(async () => {
35 fake = await startFake(0, TOKEN);
36 issues = createServer((req, res) => {
37 seen.push(`${req.method} ${req.url}`);
38 const send = (status: number, body: unknown) =>
39 res.writeHead(status, { "content-type": "application/json" }).end(JSON.stringify(body));
40 if (req.method === "POST" && /\/copy$/.test(req.url ?? "")) {
41 let raw = "";
42 req.on("data", (c: Buffer) => (raw += c.toString("utf8")));
43 req.on("end", () => {
44 const body = JSON.parse(raw) as Record<string, unknown>;
45 copies.push(body);
46 // Issue 403 stands for a live server refusing an agent's copy.
47 if (body.issue === 403 && body.dryRun !== true)
48 return send(403, { error: "copying an issue out of its project takes an owner of that project" });
49 const skipped = { comments: [4], asks: [2] };
50 if (body.dryRun === true)
51 return send(200, {
52 dryRun: true,
53 plan: {
54 from: { project: body.from, issue: body.issue },
55 title: "Sync is slow",
56 edited: ["description"],
57 labels: ["client"],
58 closed: { how: "done" },
59 comments: [{ edited: false }, { edited: true }],
60 asks: [{ edited: false }],
61 skipped,
62 },
63 });
64 send(200, { ok: true, id: 31, from: { project: body.from, issue: body.issue }, skipped });
65 });
66 return;
67 }
68 // An ack answers with the ask, acknowledged and still open.
69 const ack = /\/a\/([^/]+)\/ack\/(\d+)$/.exec(req.url ?? "");
70 if (req.method === "POST" && ack)
71 return send(200, {
72 ask: { id: Number(ack[2]), by: "desk", of: ack[1], answer: null, ack: { at: Date.now(), note: "" } },
73 });
74 send(500, { error: "a stand-in" });
75 });
76 await new Promise<void>((ok) => issues.listen(0, "127.0.0.1", ok));
77 issuesUrl = `http://127.0.0.1:${(issues.address() as AddressInfo).port}`;
78});
79after(async () => {
80 await fake.close();
81 await new Promise((ok) => issues.close(ok));
82 for (const d of dirs) rmSync(d, { recursive: true, force: true, maxRetries: 5 });
83});
85const ENV = (): Record<string, string> => ({
86 AVCS_TOKEN: TOKEN,
87 AVCS_HOOK_FORCE: "1",
88 AVCS_ISSUES_SERVER: issuesUrl,
89 AVCS_SERVERS_FILE: "no-servers.json",
90});
92async function avcs(
93 cwd: string,
94 argv: string[],
95 env: Record<string, string> = ENV(),
96): Promise<{ exit: number; out: string }> {
97 const r = await main([...argv, "--json"], { cwd, env, write: () => {} });
98 return { exit: r.exit, out: r.stdout + r.stderr };
101let projects = 0;
102async function folder(agent: string): Promise<string> {
103 const dir = mkdtempSync(join(tmpdir(), "avcs-issues-"));
104 dirs.push(dir);
105 const r = await avcs(dir, ["init", "--server", fake.url, "--project", `issues${++projects}`, "--agent", agent]);
106 assert.equal(r.exit, 0, r.out);
107 return dir;
110describe("issues commands, the client's part (#54)", () => {
111 it("sends the token only to an Issues Worker it trusts", async () => {
112 const dave = await folder("dave");
113 const before = seen.length;
114 const r = await avcs(dave, ["issues"], { ...ENV(), AVCS_ISSUES_SERVER: "https://issues.example.com" });
115 assert.equal(r.exit, 1, r.out);
116 assert.match(r.out, /sends your token only to its own servers and this machine/);
117 assert.equal(seen.length, before);
118 // A server with no Issues Worker beside it, and none named.
119 const { AVCS_ISSUES_SERVER: _, ...without } = ENV();
120 const none = await avcs(dave, ["waiting"], without);
121 assert.notEqual(none.exit, 0);
122 assert.match(
123 none.out,
124 /has no Issues Worker beside it that abso knows; ~\/\.abso\/servers\.json or ABSO_ISSUES_SERVER names one/,
125 );
126 });
128 it("finds a server's Issues Worker in this machine's servers.json, and may send it the token", async () => {
129 const dir = mkdtempSync(join(tmpdir(), "abso-servers-"));
130 dirs.push(dir);
131 const file = join(dir, "servers.json");
132 writeFileSync(file, JSON.stringify({ servers: { [fake.url]: { issues: issuesUrl } } }));
133 const erin = await folder("erin");
134 const before = seen.length;
135 const { AVCS_ISSUES_SERVER: _, ...without } = ENV();
136 await avcs(erin, ["waiting"], { ...without, AVCS_SERVERS_FILE: file });
137 assert.ok(seen.length > before, "the call went to the Issues Worker servers.json names");
138 const machine = readMachineServers({ AVCS_SERVERS_FILE: file });
139 assert.equal(issuesServerFor(fake.url, machine), issuesUrl);
140 assert.equal(issuesServerFor("https://abso.example", machine), undefined);
141 // abso's own server and its Issues Worker are built in.
142 assert.ok(tokenGoesTo("https://api.abso.dev"));
143 assert.ok(tokenGoesTo("https://issues.abso.dev"));
144 assert.equal(issuesServerFor("https://api.abso.dev/"), "https://issues.abso.dev");
145 });
147 it("are MCP tools", () => {
148 const names = mcpTools().map((t) => t.name);
149 for (const n of ["issues", "issue", "search", "ask", "waiting", "answer"]) assert.ok(names.includes(n), n);
150 assert.deepEqual(argvFor("ask", { args: ["alice", "Which login?"], issue: "3", review: true }), [
151 "ask",
152 "--issue=3",
153 "--review",
154 "--",
155 "alice",
156 "Which login?",
157 ]);
158 });
160 it("refuses usage mistakes before any call", async () => {
161 const erin = await folder("erin");
162 const before = seen.length;
163 for (const argv of [
164 ["issue"],
165 ["issue", "x"],
166 ["ask", "alice"],
167 ["ask", "a", "q", "--review", "--task"],
168 ["answer", "3"],
169 ["issue", "1", "--comment", "a", "--drop"],
170 ["issues", "extra"],
171 ["ask", "--withdraw", "x"],
172 ["ask", "--withdraw", "3", "alice", "q"],
173 ["ask", "--withdraw", "3", "--issue", "2"],
174 ["ask", "--withdraw", "3", "--task"],
175 ["ask", "alice", "q", "--note", "a note without --withdraw"],
176 ["search"],
177 ["search", "words", "--limit", "0"],
178 ["search", "words", "--limit", "51"],
179 ]) {
180 const r = await avcs(erin, argv);
181 assert.equal(r.exit, 1, `${argv.join(" ")}: ${r.out}`);
182 }
183 assert.equal(seen.length, before);
184 });
186 it("shows a search hit as the issue, then where it matched, without the match markers", () => {
187 const issue = { id: 12, title: "Sync is slow", status: "open" as const, updated: 0, labels: [] };
188 assert.deepEqual(
189 searchLines({ issue, where: "comment", comment: 40, snippet: "the \u0001network\u0002 drops mid-sync" }),
190 [' 12 open "Sync is slow"', ' in comment 40: "the network drops mid-sync"'],
191 );
192 assert.deepEqual(searchLines({ issue, where: "description", snippet: "Pages load late.", meaning: true }), [
193 ' 12 open "Sync is slow"',
194 ' by meaning: "Pages load late."',
195 ]);
196 assert.deepEqual(searchLines({ issue, where: "title", snippet: "Sync is slow" })[1], " in the title");
197 });
199 it("withdraw is one POST to the asker's own withdraw/<ask> route", async () => {
200 const erin = await folder("erin");
201 const before = seen.length;
202 await avcs(erin, ["ask", "--withdraw", "#3", "--note", "overtaken by later decisions"]);
203 const posts = seen.slice(before).filter((s) => s.startsWith("POST"));
204 assert.equal(posts.length, 1, posts.join("\n"));
205 assert.match(posts[0]!, /^POST \/p\/issues\d+\/a\/erin\/withdraw\/3$/);
206 });
208 it("ack is one POST to the one asked's ack/<ask> route, and says the ask stays open", async () => {
209 const fay = await folder("fay");
210 const before = seen.length;
211 const r = await main(["ack", "7", "on it after the deploy"], { cwd: fay, env: ENV(), write: () => {} });
212 assert.equal(r.exit, 0, r.stdout + r.stderr);
213 assert.equal(r.stdout, 'Acknowledged ask 7. desk hears it; the ask stays open until abso answer 7 "...".\n');
214 const posts = seen.slice(before).filter((s) => s.startsWith("POST"));
215 assert.deepEqual(posts.length, 1, posts.join("\n"));
216 assert.match(posts[0]!, /^POST \/p\/issues\d+\/a\/fay\/ack\/7$/);
217 assert.equal((await main(["ack"], { cwd: fay, env: ENV(), write: () => {} })).exit, 1);
218 });
220 it("an acknowledged ask reads as acknowledged, not answered, with the note under it", () => {
221 const ask = {
222 id: 7,
223 issue: 3,
224 by: "desk",
225 of: "fay",
226 kind: "question",
227 question: "Which way?",
228 review: false,
229 due: null,
230 recommendation: null,
231 options: [],
232 blocks: [],
233 at: 0,
234 answer: null,
235 seen: 0,
236 ack: { at: 0, note: "on it after the deploy" },
237 };
238 const page = {
239 issue: {
240 id: 3,
241 title: "t",
242 status: "blocked",
243 parent: null,
244 opened: { by: "desk", at: 0 },
245 agent: null,
246 agents: [],
247 blockedBy: [],
248 labels: [],
249 versions: [],
250 waitingOn: ["fay"],
251 closed: null,
252 description: "",
253 lines: null,
254 watchers: [],
255 },
256 comments: [],
257 edits: [],
258 asks: [ask],
259 claims: [],
260 children: [],
261 } as unknown as IssuePage;
262 const text = issuePage(page).join("\n");
263 assert.match(
264 text,
265 /\n {2}ask 7: desk, [^\n]*asks fay a question on issue 3 \(acknowledged [^;)]+, not answered yet\): "Which way\?"\n {4}fay acknowledged: "on it after the deploy"/,
266 );
267 assert.doesNotMatch(text, /answer:/);
268 const answered = { ...ask, answer: { by: "fay", words: "This way", at: 0 } };
269 const later = issuePage({ ...page, asks: [answered] } as unknown as IssuePage).join("\n");
270 assert.doesNotMatch(later, /acknowledged/, "an answer replaces the ack");
271 });
272});
274describe("abso issue <N> --copy-to <project>", () => {
275 const text = async (cwd: string, argv: string[]) => {
276 const r = await main(argv, { cwd, env: ENV(), write: () => {} });
277 return { exit: r.exit, out: r.stdout + r.stderr };
278 };
280 it("a dry run asks the other project's copy route for the plan, and says nothing was written", async () => {
281 const gil = await folder("gil");
282 const before = seen.length;
283 const r = await text(gil, ["issue", "12", "--copy-to", "core", "--dry-run"]);
284 assert.equal(r.exit, 0, r.out);
285 assert.deepEqual(seen.slice(before), ["POST /p/core/a/gil/copy"]);
286 const from = (copies.at(-1) as { from: string }).from;
287 assert.match(from, /^issues\d+$/);
288 assert.deepEqual(copies.at(-1), { from, issue: 12, dryRun: true });
289 assert.equal(
290 r.out.trimEnd(),
291 [
292 `A dry run: nothing was written. Copying ${from} issue 12 into core would make:`,
293 ` "Sync is slow", done (description edited when copied)`,
294 " 2 comments (1 edited), 1 answered ask, labelled client",
295 " not copied: comments 4; asks 2 (open asks, and what --clean left out)",
296 "Run it again without --dry-run to copy it.",
297 ].join("\n"),
298 );
299 });
301 it("copies with the cleaned texts --clean gives, by source id", async () => {
302 const gil = await folder("gil");
303 writeFileSync(
304 join(gil, "clean.json"),
305 JSON.stringify({ description: "Cleaned.", comments: [{ id: 1 }, { id: 3, text: "Cleaned too." }] }),
306 );
307 const r = await text(gil, ["issue", "12", "--copy-to", "core", "--clean", "clean.json"]);
308 assert.equal(r.exit, 0, r.out);
309 const from = (copies.at(-1) as { from: string }).from;
310 assert.deepEqual(copies.at(-1), {
311 description: "Cleaned.",
312 comments: [{ id: 1 }, { id: 3, text: "Cleaned too." }],
313 from,
314 issue: 12,
315 });
316 assert.match(
317 r.out,
318 new RegExp(`^Copied ${from} issue 12 to core issue 31\\. Its comments and asks keep their authors`),
319 );
320 });
322 it("says a dry run still works when the server refuses the copy", async () => {
323 const gil = await folder("gil");
324 const r = await text(gil, ["issue", "403", "--copy-to", "core"]);
325 assert.equal(r.exit, 1, r.out);
326 assert.match(r.out, /takes an owner of that project\. --dry-run still shows what would go\./);
327 });
329 it("refuses mistakes before any call", async () => {
330 const gil = await folder("gil");
331 writeFileSync(join(gil, "bad.json"), JSON.stringify({ from: "elsewhere" }));
332 const before = seen.length;
333 for (const [argv, why] of [
334 [["issue", "12", "--copy-to", "core", "--comment", "x"], /--copy-to goes alone .* not with --comment/],
335 [["issue", "12", "--dry-run"], /--clean and --dry-run go with --copy-to <project>/],
336 [["issue", "12", "--copy-to", "core", "--clean", "nope.json"], /--clean nope\.json: no such file/],
337 [
338 ["issue", "12", "--copy-to", "core", "--clean", "bad.json"],
339 /from isn't one of title, description, comments, asks/,
340 ],
341 ] as const) {
342 const r = await text(gil, [...argv]);
343 assert.notEqual(r.exit, 0, argv.join(" "));
344 assert.match(r.out, why);
345 }
346 const { project } = readConfig(gil);
347 assert.match(
348 (await text(gil, ["issue", "12", "--copy-to", project])).out,
349 /issue 12 is already in issues\d+; --copy-to names another project/,
350 );
351 assert.equal(seen.length, before, "nothing reached the Issues server");
352 });
353});