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/attach.test.ts?version=33&format=json
242 linespinch 100% · review 1%Last change pinch in version 29, 5 versions changed it

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

1// --attach (abso issue 171): images go up to the Issues Worker before the
2// write that carries them, and the issue's page lists them. The Issues server
3// here is a stand-in that keeps what it was sent.
4
5import assert from "node:assert/strict";
6import { mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
7import { createServer, type Server } from "node:http";
8import type { AddressInfo } from "node:net";
9import { tmpdir } from "node:os";
10import { join } from "node:path";
11import { after, before, describe, it } from "node:test";
12import { issuePage } from "../src/issues.ts";
13import type { IssuePage } from "../src/issues-wire.ts";
14import { main } from "../src/main.ts";
15import { type Fake, startFake } from "./fake-server.ts";
17const TOKEN = "test-token-not-secret";
18let fake: Fake;
19let issues: Server;
20let issuesUrl: string;
21const seen: { method: string; url: string; headers: Record<string, string | string[] | undefined>; body: Buffer }[] =
22 [];
23const dirs: string[] = [];
25before(async () => {
26 fake = await startFake(0, TOKEN);
27 issues = createServer((req, res) => {
28 const parts: Buffer[] = [];
29 req.on("data", (c: Buffer) => parts.push(c));
30 req.on("end", () => {
31 const body = Buffer.concat(parts);
32 seen.push({ method: req.method!, url: req.url!, headers: req.headers, body });
33 const json = (o: unknown) => res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify(o));
34 // A server from before attachments: it reads every POST body as JSON.
35 if (req.url!.includes("/p/old") && req.url!.endsWith("/attachments"))
36 return res.writeHead(400, { "content-type": "application/json" }).end('{"error":"the body is JSON"}');
37 if (req.url!.endsWith("/attachments"))
38 return json({
39 id: `a${String(seen.length).padStart(12, "0")}`,
40 name: "x",
41 type: "image/png",
42 size: body.length,
43 });
44 if (req.url!.endsWith("/issue")) return json({ id: 7 });
45 if (/\/issue\/\d+\/comment$/.test(req.url!)) return json({ id: 3 });
46 // Issue 8 carries an svg-drawn image on itself and again on a comment, plus one more on the comment.
47 const logo = { id: "a00000000000b", name: "../shot one.png", type: "image/png", size: 13 };
48 if (req.method === "GET" && /\/issue\/8$/.test(req.url!))
49 return json({
50 issue: { id: 8, attachments: [{ ...logo, source: { name: "logo.svg", size: 20 } }] },
51 comments: [{ attachments: [logo, { id: "a00000000000c", name: "after.png", type: "image/png", size: 13 }] }],
52 edits: [],
53 asks: [],
54 claims: [],
55 children: [],
56 });
57 const served = /\/attachments\/(a[0-9a-f]{12})(\/source)?$/.exec(req.url!);
58 if (req.method === "GET" && served)
59 return res.writeHead(200, { "content-type": "image/png" }).end(`${served[1]}${served[2] ?? ""}`);
60 res.writeHead(404).end("{}");
61 });
62 });
63 await new Promise<void>((ok) => issues.listen(0, "127.0.0.1", ok));
64 issuesUrl = `http://127.0.0.1:${(issues.address() as AddressInfo).port}`;
65});
66after(async () => {
67 await fake.close();
68 await new Promise((ok) => issues.close(ok));
69 for (const d of dirs) rmSync(d, { recursive: true, force: true, maxRetries: 5 });
70});
72const ENV = (): Record<string, string> => ({
73 AVCS_TOKEN: TOKEN,
74 AVCS_HOOK_FORCE: "1",
75 AVCS_ISSUES_SERVER: issuesUrl,
76 AVCS_SERVERS_FILE: "no-servers.json",
77});
79async function avcs(cwd: string, argv: string[]): Promise<{ exit: number; out: string; json?: unknown }> {
80 const r = await main([...argv, "--json"], { cwd, env: ENV(), write: () => {} });
81 let json: unknown;
82 try {
83 json = JSON.parse(r.stdout);
84 } catch {}
85 return { exit: r.exit, out: r.stdout + r.stderr, json };
88let n = 0;
89async function folder(): Promise<string> {
90 const dir = mkdtempSync(join(tmpdir(), "abso-attach-"));
91 dirs.push(dir);
92 const r = await avcs(dir, ["init", "--server", fake.url, "--project", `attach${++n}`, "--agent", "ann"]);
93 assert.equal(r.exit, 0, r.out);
94 writeFileSync(join(dir, "shot.png"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 1, 2, 3]));
95 writeFileSync(join(dir, "after.png"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 4, 5]));
96 return dir;
99describe("--attach (abso issue 171)", () => {
100 it("uploads each file, then opens the issue carrying their ids", async () => {
101 const dir = await folder();
102 const from = seen.length;
103 const r = await avcs(dir, ["issue", "new", "The page after the fix", "--attach", "shot.png,after.png"]);
104 assert.equal(r.exit, 0, r.out);
105 const calls = seen.slice(from);
106 assert.deepEqual(
107 calls.map((c) => `${c.method} ${c.url.replace(/^\/p\/[^/]+/, "")}`),
108 ["POST /a/ann/attachments", "POST /a/ann/attachments", "POST /a/ann/issue"],
109 );
110 assert.equal(calls[0]!.headers["x-avcs-name"], "shot.png");
111 assert.equal(calls[0]!.headers["content-type"], "application/octet-stream");
112 assert.deepEqual([...calls[0]!.body], [0x89, 0x50, 0x4e, 0x47, 1, 2, 3]);
113 const opened = JSON.parse(calls[2]!.body.toString()) as { title: string; attachments: string[] };
114 assert.equal(opened.title, "The page after the fix");
115 assert.equal(opened.attachments.length, 2);
116 assert.deepEqual((r.json as { attachments: string[] }).attachments, opened.attachments);
117 });
119 it("goes on a comment too, and nowhere else", async () => {
120 const dir = await folder();
121 const from = seen.length;
122 const r = await avcs(dir, ["issue", "7", "--comment", "after", "--attach", "shot.png"]);
123 assert.equal(r.exit, 0, r.out);
124 const comment = JSON.parse(seen.at(-1)!.body.toString()) as { text: string; attachments: string[] };
125 assert.equal(comment.text, "after");
126 assert.equal(comment.attachments.length, 1);
127 assert.equal(seen.length - from, 2);
128 const alone = await avcs(dir, ["issue", "7", "--attach", "shot.png"]);
129 assert.equal(alone.exit, 1);
130 assert.match(alone.out, /--attach goes with abso issue new or --comment/);
131 });
133 it("says plainly when the server doesn't take attachments yet, and writes nothing", async () => {
134 const dir = mkdtempSync(join(tmpdir(), "abso-attach-"));
135 dirs.push(dir);
136 const init = await avcs(dir, ["init", "--server", fake.url, "--project", "old", "--agent", "ann"]);
137 assert.equal(init.exit, 0, init.out);
138 writeFileSync(join(dir, "shot.png"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 1]));
139 const from = seen.length;
140 const r = await avcs(dir, ["issue", "new", "T", "--attach", "shot.png"]);
141 assert.equal(r.exit, 1);
142 assert.match(r.out, /doesn't take attachments yet, so shot.png wasn't sent and nothing was written/);
143 assert.deepEqual(
144 seen.slice(from).map((c) => c.url.replace(/^\/p\/[^/]+/, "")),
145 ["/a/ann/attachments"],
146 "no issue was opened",
147 );
148 });
150 it("sends nothing when a file is missing", async () => {
151 const dir = await folder();
152 const from = seen.length;
153 const r = await avcs(dir, ["issue", "new", "T", "--attach", "shot.png,nope.png"]);
154 assert.equal(r.exit, 1);
155 assert.match(r.out, /--attach nope.png: no such file/);
156 assert.equal(seen.length, from);
157 });
159 it("--save-attachments saves each attachment once, an svg's own file too, outside the folder", async () => {
160 const dir = await folder();
161 const out = mkdtempSync(join(tmpdir(), "abso-saved-"));
162 dirs.push(out);
163 const r = await avcs(dir, ["issue", "8", "--save-attachments", join(out, "images")]);
164 assert.equal(r.exit, 0, r.out);
165 const names = readdirSync(join(out, "images")).sort();
166 assert.deepEqual(names, ["a00000000000b-logo.svg", "a00000000000b-shot_one.png", "a00000000000c-after.png"]);
167 assert.equal(readFileSync(join(out, "images", "a00000000000b-logo.svg"), "utf8"), "a00000000000b/source");
168 assert.equal(readFileSync(join(out, "images", "a00000000000c-after.png"), "utf8"), "a00000000000c");
169 assert.equal((r.json as { files: unknown[] }).files.length, 3);
170 // Into the folder only where abso leaves files out, or they'd be recorded as edits.
171 const inside = await avcs(dir, ["issue", "8", "--save-attachments", "images"]);
172 assert.equal(inside.exit, 1);
173 assert.match(inside.out, /inside this abso folder/);
174 assert.equal((await avcs(dir, ["issue", "8", "--save-attachments", "."])).exit, 1);
175 writeFileSync(join(dir, ".absoignore"), "saved/\n");
176 assert.equal((await avcs(dir, ["issue", "8", "--save-attachments", "saved"])).exit, 0);
177 const mixed = await avcs(dir, ["issue", "8", "--save-attachments", out, "--comment", "x"]);
178 assert.equal(mixed.exit, 1);
179 assert.match(mixed.out, /--save-attachments goes alone/);
180 });
182 it("an issue's page lists its attachments and each comment's, with links", () => {
183 const page = {
184 issue: {
185 id: 7,
186 title: "T",
187 status: "open",
188 opened: { by: "ann", at: 0 },
189 agent: null,
190 agents: [],
191 blockedBy: [],
192 labels: [],
193 versions: [],
194 waitingOn: [],
195 parent: null,
196 children: 0,
197 updated: 0,
198 closed: null,
199 description: "",
200 lines: null,
201 watchers: [],
202 attachments: [{ id: "a0123456789ab", name: "shot.png", type: "image/png", size: 2048 }],
203 },
204 comments: [
205 {
206 id: 1,
207 by: "bob",
208 kind: "comment",
209 text: "same here",
210 at: 0,
211 attachments: [
212 { id: "a00000000000f", name: "mine.png", type: "image/png", size: 3 * 1024 * 1024 },
213 {
214 id: "a0000000000aa",
215 name: "logo.png",
216 type: "image/png",
217 size: 4096,
218 source: { name: "logo.svg", size: 512 },
219 },
220 ],
221 },
222 ],
223 edits: [],
224 asks: [],
225 claims: [],
226 children: [],
227 } as unknown as IssuePage;
228 const text = issuePage(page, {
229 attachmentUrl: (id: string) => `https://issues.example/p/x/attachments/${id}`,
230 }).join("\n");
231 assert.match(
232 text,
233 /attached a0123456789ab: "shot.png", 2 KB, https:\/\/issues.example\/p\/x\/attachments\/a0123456789ab/,
234 );
235 assert.match(text, /\n {4}attached a00000000000f: "mine.png", 3.0 MB/);
236 // An SVG is listed as its PNG, with its own file as a download beside it.
237 assert.match(
238 text,
239 /\n {4}attached a0000000000aa: "logo.png", 4 KB, \S+\/a0000000000aa\n {6}drawn from "logo.svg", 512 B, https:\/\/issues.example\/p\/x\/attachments\/a0000000000aa\/source/,
240 );
241 });
242});