- 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=28&format=json
205 linespinch 100% · review 1%Last change pinch in version 24, 4 versions changed it
You're reading version 28. The newest is 318, and 5 versions since then changed this file.
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.
5import assert from "node:assert/strict";
6import { mkdtempSync, 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 res.writeHead(404).end("{}");
47 });
48 });
49 await new Promise<void>((ok) => issues.listen(0, "127.0.0.1", ok));
50 issuesUrl = `http://127.0.0.1:${(issues.address() as AddressInfo).port}`;
51});
52after(async () => {
53 await fake.close();
54 await new Promise((ok) => issues.close(ok));
55 for (const d of dirs) rmSync(d, { recursive: true, force: true, maxRetries: 5 });
56});
58const ENV = (): Record<string, string> => ({
59 AVCS_TOKEN: TOKEN,
60 AVCS_HOOK_FORCE: "1",
61 AVCS_ISSUES_SERVER: issuesUrl,
62 AVCS_SERVERS_FILE: "no-servers.json",
63});
65async function avcs(cwd: string, argv: string[]): Promise<{ exit: number; out: string; json?: unknown }> {
66 const r = await main([...argv, "--json"], { cwd, env: ENV(), write: () => {} });
67 let json: unknown;
68 try {
69 json = JSON.parse(r.stdout);
70 } catch {}
71 return { exit: r.exit, out: r.stdout + r.stderr, json };
72}
74let n = 0;
75async function folder(): Promise<string> {
76 const dir = mkdtempSync(join(tmpdir(), "abso-attach-"));
77 dirs.push(dir);
78 const r = await avcs(dir, ["init", "--server", fake.url, "--project", `attach${++n}`, "--agent", "ann"]);
79 assert.equal(r.exit, 0, r.out);
80 writeFileSync(join(dir, "shot.png"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 1, 2, 3]));
81 writeFileSync(join(dir, "after.png"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 4, 5]));
82 return dir;
83}
85describe("--attach (abso issue 171)", () => {
86 it("uploads each file, then opens the issue carrying their ids", async () => {
87 const dir = await folder();
88 const from = seen.length;
89 const r = await avcs(dir, ["issue", "new", "The page after the fix", "--attach", "shot.png,after.png"]);
90 assert.equal(r.exit, 0, r.out);
91 const calls = seen.slice(from);
92 assert.deepEqual(
93 calls.map((c) => `${c.method} ${c.url.replace(/^\/p\/[^/]+/, "")}`),
94 ["POST /a/ann/attachments", "POST /a/ann/attachments", "POST /a/ann/issue"],
95 );
96 assert.equal(calls[0]!.headers["x-avcs-name"], "shot.png");
97 assert.equal(calls[0]!.headers["content-type"], "application/octet-stream");
98 assert.deepEqual([...calls[0]!.body], [0x89, 0x50, 0x4e, 0x47, 1, 2, 3]);
99 const opened = JSON.parse(calls[2]!.body.toString()) as { title: string; attachments: string[] };
100 assert.equal(opened.title, "The page after the fix");
101 assert.equal(opened.attachments.length, 2);
102 assert.deepEqual((r.json as { attachments: string[] }).attachments, opened.attachments);
103 });
105 it("goes on a comment too, and nowhere else", async () => {
106 const dir = await folder();
107 const from = seen.length;
108 const r = await avcs(dir, ["issue", "7", "--comment", "after", "--attach", "shot.png"]);
109 assert.equal(r.exit, 0, r.out);
110 const comment = JSON.parse(seen.at(-1)!.body.toString()) as { text: string; attachments: string[] };
111 assert.equal(comment.text, "after");
112 assert.equal(comment.attachments.length, 1);
113 assert.equal(seen.length - from, 2);
114 const alone = await avcs(dir, ["issue", "7", "--attach", "shot.png"]);
115 assert.equal(alone.exit, 1);
116 assert.match(alone.out, /--attach goes with abso issue new or --comment/);
117 });
119 it("says plainly when the server doesn't take attachments yet, and writes nothing", async () => {
120 const dir = mkdtempSync(join(tmpdir(), "abso-attach-"));
121 dirs.push(dir);
122 const init = await avcs(dir, ["init", "--server", fake.url, "--project", "old", "--agent", "ann"]);
123 assert.equal(init.exit, 0, init.out);
124 writeFileSync(join(dir, "shot.png"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 1]));
125 const from = seen.length;
126 const r = await avcs(dir, ["issue", "new", "T", "--attach", "shot.png"]);
127 assert.equal(r.exit, 1);
128 assert.match(r.out, /doesn't take attachments yet, so shot.png wasn't sent and nothing was written/);
129 assert.deepEqual(
130 seen.slice(from).map((c) => c.url.replace(/^\/p\/[^/]+/, "")),
131 ["/a/ann/attachments"],
132 "no issue was opened",
133 );
134 });
136 it("sends nothing when a file is missing", async () => {
137 const dir = await folder();
138 const from = seen.length;
139 const r = await avcs(dir, ["issue", "new", "T", "--attach", "shot.png,nope.png"]);
140 assert.equal(r.exit, 1);
141 assert.match(r.out, /--attach nope.png: no such file/);
142 assert.equal(seen.length, from);
143 });
145 it("an issue's page lists its attachments and each comment's, with links", () => {
146 const page = {
147 issue: {
148 id: 7,
149 title: "T",
150 status: "open",
151 opened: { by: "ann", at: 0 },
152 agent: null,
153 agents: [],
154 blockedBy: [],
155 labels: [],
156 versions: [],
157 waitingOn: [],
158 parent: null,
159 children: 0,
160 updated: 0,
161 closed: null,
162 description: "",
163 lines: null,
164 watchers: [],
165 attachments: [{ id: "a0123456789ab", name: "shot.png", type: "image/png", size: 2048 }],
166 },
167 comments: [
168 {
169 id: 1,
170 by: "bob",
171 kind: "comment",
172 text: "same here",
173 at: 0,
174 attachments: [
175 { id: "a00000000000f", name: "mine.png", type: "image/png", size: 3 * 1024 * 1024 },
176 {
177 id: "a0000000000aa",
178 name: "logo.png",
179 type: "image/png",
180 size: 4096,
181 source: { name: "logo.svg", size: 512 },
182 },
183 ],
184 },
185 ],
186 edits: [],
187 asks: [],
188 claims: [],
189 children: [],
190 } as unknown as IssuePage;
191 const text = issuePage(page, {
192 attachmentUrl: (id: string) => `https://issues.example/p/x/attachments/${id}`,
193 }).join("\n");
194 assert.match(
195 text,
196 /attached a0123456789ab: "shot.png", 2 KB, https:\/\/issues.example\/p\/x\/attachments\/a0123456789ab/,
197 );
198 assert.match(text, /\n {4}attached a00000000000f: "mine.png", 3.0 MB/);
199 // An SVG is listed as its PNG, with its own file as a download beside it.
200 assert.match(
201 text,
202 /\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/,
203 );
204 });
205});