- 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?version=50&format=json
281 linesFirst import 47% · pinch 41% · devops 6% · infra-split 5% · review 1%Last change review in version 27, 6 versions changed it
You're reading version 50. The newest is 318, and 7 versions since then changed this file.
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).
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 { searchLines } from "../src/issues.ts";
18import { main } from "../src/main.ts";
19import { argvFor, mcpTools } from "../src/mcp.ts";
20import { type Fake, startFake } from "./fake-server.ts";
22const TOKEN = "test-token-not-secret";
24let fake: Fake;
25let issues: Server;
26let issuesUrl: string;
27/** Every call the stand-in Issues server took. */
28const seen: string[] = [];
29/** The bodies of the copy requests it took. */
30const copies: Record<string, unknown>[] = [];
31const dirs: string[] = [];
33before(async () => {
34 fake = await startFake(0, TOKEN);
35 issues = createServer((req, res) => {
36 seen.push(`${req.method} ${req.url}`);
37 const send = (status: number, body: unknown) =>
38 res.writeHead(status, { "content-type": "application/json" }).end(JSON.stringify(body));
39 if (req.method === "POST" && /\/copy$/.test(req.url ?? "")) {
40 let raw = "";
41 req.on("data", (c: Buffer) => (raw += c.toString("utf8")));
42 req.on("end", () => {
43 const body = JSON.parse(raw) as Record<string, unknown>;
44 copies.push(body);
45 // Issue 403 stands for a live server refusing an agent's copy.
46 if (body.issue === 403 && body.dryRun !== true)
47 return send(403, { error: "copying an issue out of its project takes an owner of that project" });
48 const skipped = { comments: [4], asks: [2] };
49 if (body.dryRun === true)
50 return send(200, {
51 dryRun: true,
52 plan: {
53 from: { project: body.from, issue: body.issue },
54 title: "Sync is slow",
55 edited: ["description"],
56 labels: ["client"],
57 closed: { how: "done" },
58 comments: [{ edited: false }, { edited: true }],
59 asks: [{ edited: false }],
60 skipped,
61 },
62 });
63 send(200, { ok: true, id: 31, from: { project: body.from, issue: body.issue }, skipped });
64 });
65 return;
66 }
67 send(500, { error: "a stand-in" });
68 });
69 await new Promise<void>((ok) => issues.listen(0, "127.0.0.1", ok));
70 issuesUrl = `http://127.0.0.1:${(issues.address() as AddressInfo).port}`;
71});
72after(async () => {
73 await fake.close();
74 await new Promise((ok) => issues.close(ok));
75 for (const d of dirs) rmSync(d, { recursive: true, force: true, maxRetries: 5 });
76});
78const ENV = (): Record<string, string> => ({
79 AVCS_TOKEN: TOKEN,
80 AVCS_HOOK_FORCE: "1",
81 AVCS_ISSUES_SERVER: issuesUrl,
82 AVCS_SERVERS_FILE: "no-servers.json",
83});
85async function avcs(
86 cwd: string,
87 argv: string[],
88 env: Record<string, string> = ENV(),
89): Promise<{ exit: number; out: string }> {
90 const r = await main([...argv, "--json"], { cwd, env, write: () => {} });
91 return { exit: r.exit, out: r.stdout + r.stderr };
92}
94let projects = 0;
95async function folder(agent: string): Promise<string> {
96 const dir = mkdtempSync(join(tmpdir(), "avcs-issues-"));
97 dirs.push(dir);
98 const r = await avcs(dir, ["init", "--server", fake.url, "--project", `issues${++projects}`, "--agent", agent]);
99 assert.equal(r.exit, 0, r.out);
100 return dir;
101}
103describe("issues commands, the client's part (#54)", () => {
104 it("sends the token only to an Issues Worker it trusts", async () => {
105 const dave = await folder("dave");
106 const before = seen.length;
107 const r = await avcs(dave, ["issues"], { ...ENV(), AVCS_ISSUES_SERVER: "https://issues.example.com" });
108 assert.equal(r.exit, 1, r.out);
109 assert.match(r.out, /sends your token only to its own servers and this machine/);
110 assert.equal(seen.length, before);
111 // A server with no Issues Worker beside it, and none named.
112 const { AVCS_ISSUES_SERVER: _, ...without } = ENV();
113 const none = await avcs(dave, ["waiting"], without);
114 assert.notEqual(none.exit, 0);
115 assert.match(
116 none.out,
117 /has no Issues Worker beside it that abso knows; ~\/\.abso\/servers\.json or ABSO_ISSUES_SERVER names one/,
118 );
119 });
121 it("finds a server's Issues Worker in this machine's servers.json, and may send it the token", async () => {
122 const dir = mkdtempSync(join(tmpdir(), "abso-servers-"));
123 dirs.push(dir);
124 const file = join(dir, "servers.json");
125 writeFileSync(file, JSON.stringify({ servers: { [fake.url]: { issues: issuesUrl } } }));
126 const erin = await folder("erin");
127 const before = seen.length;
128 const { AVCS_ISSUES_SERVER: _, ...without } = ENV();
129 await avcs(erin, ["waiting"], { ...without, AVCS_SERVERS_FILE: file });
130 assert.ok(seen.length > before, "the call went to the Issues Worker servers.json names");
131 const machine = readMachineServers({ AVCS_SERVERS_FILE: file });
132 assert.equal(issuesServerFor(fake.url, machine), issuesUrl);
133 assert.equal(issuesServerFor("https://abso.example", machine), undefined);
134 // abso's own server and its Issues Worker are built in.
135 assert.ok(tokenGoesTo("https://api.abso.dev"));
136 assert.ok(tokenGoesTo("https://issues.abso.dev"));
137 assert.equal(issuesServerFor("https://api.abso.dev/"), "https://issues.abso.dev");
138 });
140 it("are MCP tools", () => {
141 const names = mcpTools().map((t) => t.name);
142 for (const n of ["issues", "issue", "search", "ask", "waiting", "answer"]) assert.ok(names.includes(n), n);
143 assert.deepEqual(argvFor("ask", { args: ["alice", "Which login?"], issue: "3", review: true }), [
144 "ask",
145 "--issue=3",
146 "--review",
147 "--",
148 "alice",
149 "Which login?",
150 ]);
151 });
153 it("refuses usage mistakes before any call", async () => {
154 const erin = await folder("erin");
155 const before = seen.length;
156 for (const argv of [
157 ["issue"],
158 ["issue", "x"],
159 ["ask", "alice"],
160 ["ask", "a", "q", "--review", "--task"],
161 ["answer", "3"],
162 ["issue", "1", "--comment", "a", "--drop"],
163 ["issues", "extra"],
164 ["ask", "--withdraw", "x"],
165 ["ask", "--withdraw", "3", "alice", "q"],
166 ["ask", "--withdraw", "3", "--issue", "2"],
167 ["ask", "--withdraw", "3", "--task"],
168 ["ask", "alice", "q", "--note", "a note without --withdraw"],
169 ["search"],
170 ["search", "words", "--limit", "0"],
171 ["search", "words", "--limit", "51"],
172 ]) {
173 const r = await avcs(erin, argv);
174 assert.equal(r.exit, 1, `${argv.join(" ")}: ${r.out}`);
175 }
176 assert.equal(seen.length, before);
177 });
179 it("shows a search hit as the issue, then where it matched, without the match markers", () => {
180 const issue = { id: 12, title: "Sync is slow", status: "open" as const, updated: 0, labels: [] };
181 assert.deepEqual(
182 searchLines({ issue, where: "comment", comment: 40, snippet: "the \u0001network\u0002 drops mid-sync" }),
183 [' 12 open "Sync is slow"', ' in comment 40: "the network drops mid-sync"'],
184 );
185 assert.deepEqual(searchLines({ issue, where: "description", snippet: "Pages load late.", meaning: true }), [
186 ' 12 open "Sync is slow"',
187 ' by meaning: "Pages load late."',
188 ]);
189 assert.deepEqual(searchLines({ issue, where: "title", snippet: "Sync is slow" })[1], " in the title");
190 });
192 it("withdraw is one POST to the asker's own withdraw/<ask> route", async () => {
193 const erin = await folder("erin");
194 const before = seen.length;
195 await avcs(erin, ["ask", "--withdraw", "#3", "--note", "overtaken by later decisions"]);
196 const posts = seen.slice(before).filter((s) => s.startsWith("POST"));
197 assert.equal(posts.length, 1, posts.join("\n"));
198 assert.match(posts[0]!, /^POST \/p\/issues\d+\/a\/erin\/withdraw\/3$/);
199 });
200});
202describe("abso issue <N> --copy-to <project>", () => {
203 const text = async (cwd: string, argv: string[]) => {
204 const r = await main(argv, { cwd, env: ENV(), write: () => {} });
205 return { exit: r.exit, out: r.stdout + r.stderr };
206 };
208 it("a dry run asks the other project's copy route for the plan, and says nothing was written", async () => {
209 const gil = await folder("gil");
210 const before = seen.length;
211 const r = await text(gil, ["issue", "12", "--copy-to", "core", "--dry-run"]);
212 assert.equal(r.exit, 0, r.out);
213 assert.deepEqual(seen.slice(before), ["POST /p/core/a/gil/copy"]);
214 const from = (copies.at(-1) as { from: string }).from;
215 assert.match(from, /^issues\d+$/);
216 assert.deepEqual(copies.at(-1), { from, issue: 12, dryRun: true });
217 assert.equal(
218 r.out.trimEnd(),
219 [
220 `A dry run: nothing was written. Copying ${from} issue 12 into core would make:`,
221 ` "Sync is slow", done (description edited when copied)`,
222 " 2 comments (1 edited), 1 answered ask, labelled client",
223 " not copied: comments 4; asks 2 (open asks, and what --clean left out)",
224 "Run it again without --dry-run to copy it.",
225 ].join("\n"),
226 );
227 });
229 it("copies with the cleaned texts --clean gives, by source id", async () => {
230 const gil = await folder("gil");
231 writeFileSync(
232 join(gil, "clean.json"),
233 JSON.stringify({ description: "Cleaned.", comments: [{ id: 1 }, { id: 3, text: "Cleaned too." }] }),
234 );
235 const r = await text(gil, ["issue", "12", "--copy-to", "core", "--clean", "clean.json"]);
236 assert.equal(r.exit, 0, r.out);
237 const from = (copies.at(-1) as { from: string }).from;
238 assert.deepEqual(copies.at(-1), {
239 description: "Cleaned.",
240 comments: [{ id: 1 }, { id: 3, text: "Cleaned too." }],
241 from,
242 issue: 12,
243 });
244 assert.match(
245 r.out,
246 new RegExp(`^Copied ${from} issue 12 to core issue 31\\. Its comments and asks keep their authors`),
247 );
248 });
250 it("says a dry run still works when the server refuses the copy", async () => {
251 const gil = await folder("gil");
252 const r = await text(gil, ["issue", "403", "--copy-to", "core"]);
253 assert.equal(r.exit, 1, r.out);
254 assert.match(r.out, /takes an owner of that project\. --dry-run still shows what would go\./);
255 });
257 it("refuses mistakes before any call", async () => {
258 const gil = await folder("gil");
259 writeFileSync(join(gil, "bad.json"), JSON.stringify({ from: "elsewhere" }));
260 const before = seen.length;
261 for (const [argv, why] of [
262 [["issue", "12", "--copy-to", "core", "--comment", "x"], /--copy-to goes alone .* not with --comment/],
263 [["issue", "12", "--dry-run"], /--clean and --dry-run go with --copy-to <project>/],
264 [["issue", "12", "--copy-to", "core", "--clean", "nope.json"], /--clean nope\.json: no such file/],
265 [
266 ["issue", "12", "--copy-to", "core", "--clean", "bad.json"],
267 /from isn't one of title, description, comments, asks/,
268 ],
269 ] as const) {
270 const r = await text(gil, [...argv]);
271 assert.notEqual(r.exit, 0, argv.join(" "));
272 assert.match(r.out, why);
273 }
274 const { project } = readConfig(gil);
275 assert.match(
276 (await text(gil, ["issue", "12", "--copy-to", project])).out,
277 /issue 12 is already in issues\d+; --copy-to names another project/,
278 );
279 assert.equal(seen.length, before, "nothing reached the Issues server");
280 });
281});