- MCP
- blame on this site's /mcp
- CLI
- abso blame client/test/attach.test.ts --version 9 --json
- JSON
- /christopher/abso-core/blame/client/test/attach.test.ts?version=9&format=json
171 linespinch 100%Last change pinch in version 5,
171 lines from 1 change in 1 version.
pinch5abso issue new and abso issue N --comment take --attach <file,file> (abso issue 171). Each file goes up to the Issues Worker first (POST /p/:project/a/:agent/attachments, the bytes as the body and the name URI-encoded in X-Avcs-Name), and the write carries the ids the uploads answered. Nothing is sent if a file is missing or over 10 MB. --attach anywhere else is refused. The issue's page lists each attachment under the issue or comment carrying it: id, name, size and its link. The wire types gain Attachment and attachments?. MCP's issue tool takes attach too, since its parameters come from the flags. The server side is abso 621 (off until Cloud binds the bucket). Tests: client/test/attach.test.ts, against a stand-in Issues server. Checked: biome ci, npm run check (698 pass).Compare1// --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 if (req.url!.endsWith("/attachments"))
35 return json({
36 id: `a${String(seen.length).padStart(12, "0")}`,
37 name: "x",
38 type: "image/png",
39 size: body.length,
40 });
41 if (req.url!.endsWith("/issue")) return json({ id: 7 });
42 if (/\/issue\/\d+\/comment$/.test(req.url!)) return json({ id: 3 });
43 res.writeHead(404).end("{}");
44 });
45 });
46 await new Promise<void>((ok) => issues.listen(0, "127.0.0.1", ok));
47 issuesUrl = `http://127.0.0.1:${(issues.address() as AddressInfo).port}`;
48});
49after(async () => {
50 await fake.close();
51 await new Promise((ok) => issues.close(ok));
52 for (const d of dirs) rmSync(d, { recursive: true, force: true, maxRetries: 5 });
53});
55const ENV = (): Record<string, string> => ({
56 AVCS_TOKEN: TOKEN,
57 AVCS_HOOK_FORCE: "1",
58 AVCS_ISSUES_SERVER: issuesUrl,
59 AVCS_SERVERS_FILE: "no-servers.json",
60});
62async function avcs(cwd: string, argv: string[]): Promise<{ exit: number; out: string; json?: unknown }> {
63 const r = await main([...argv, "--json"], { cwd, env: ENV(), write: () => {} });
64 let json: unknown;
65 try {
66 json = JSON.parse(r.stdout);
67 } catch {}
68 return { exit: r.exit, out: r.stdout + r.stderr, json };
69}
71let n = 0;
72async function folder(): Promise<string> {
73 const dir = mkdtempSync(join(tmpdir(), "abso-attach-"));
74 dirs.push(dir);
75 const r = await avcs(dir, ["init", "--server", fake.url, "--project", `attach${++n}`, "--agent", "ann"]);
76 assert.equal(r.exit, 0, r.out);
77 writeFileSync(join(dir, "shot.png"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 1, 2, 3]));
78 writeFileSync(join(dir, "after.png"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 4, 5]));
79 return dir;
80}
82describe("--attach (abso issue 171)", () => {
83 it("uploads each file, then opens the issue carrying their ids", async () => {
84 const dir = await folder();
85 const from = seen.length;
86 const r = await avcs(dir, ["issue", "new", "The page after the fix", "--attach", "shot.png,after.png"]);
87 assert.equal(r.exit, 0, r.out);
88 const calls = seen.slice(from);
89 assert.deepEqual(
90 calls.map((c) => `${c.method} ${c.url.replace(/^\/p\/[^/]+/, "")}`),
91 ["POST /a/ann/attachments", "POST /a/ann/attachments", "POST /a/ann/issue"],
92 );
93 assert.equal(calls[0]!.headers["x-avcs-name"], "shot.png");
94 assert.equal(calls[0]!.headers["content-type"], "application/octet-stream");
95 assert.deepEqual([...calls[0]!.body], [0x89, 0x50, 0x4e, 0x47, 1, 2, 3]);
96 const opened = JSON.parse(calls[2]!.body.toString()) as { title: string; attachments: string[] };
97 assert.equal(opened.title, "The page after the fix");
98 assert.equal(opened.attachments.length, 2);
99 assert.deepEqual((r.json as { attachments: string[] }).attachments, opened.attachments);
100 });
102 it("goes on a comment too, and nowhere else", async () => {
103 const dir = await folder();
104 const from = seen.length;
105 const r = await avcs(dir, ["issue", "7", "--comment", "after", "--attach", "shot.png"]);
106 assert.equal(r.exit, 0, r.out);
107 const comment = JSON.parse(seen.at(-1)!.body.toString()) as { text: string; attachments: string[] };
108 assert.equal(comment.text, "after");
109 assert.equal(comment.attachments.length, 1);
110 assert.equal(seen.length - from, 2);
111 const alone = await avcs(dir, ["issue", "7", "--attach", "shot.png"]);
112 assert.equal(alone.exit, 1);
113 assert.match(alone.out, /--attach goes with avcs issue new or --comment/);
114 });
116 it("sends nothing when a file is missing", async () => {
117 const dir = await folder();
118 const from = seen.length;
119 const r = await avcs(dir, ["issue", "new", "T", "--attach", "shot.png,nope.png"]);
120 assert.equal(r.exit, 1);
121 assert.match(r.out, /--attach nope.png: no such file/);
122 assert.equal(seen.length, from);
123 });
125 it("an issue's page lists its attachments and each comment's, with links", () => {
126 const page = {
127 issue: {
128 id: 7,
129 title: "T",
130 status: "open",
131 opened: { by: "ann", at: 0 },
132 agent: null,
133 agents: [],
134 blockedBy: [],
135 labels: [],
136 versions: [],
137 waitingOn: [],
138 parent: null,
139 children: 0,
140 updated: 0,
141 closed: null,
142 description: "",
143 lines: null,
144 watchers: [],
145 attachments: [{ id: "a0123456789ab", name: "shot.png", type: "image/png", size: 2048 }],
146 },
147 comments: [
148 {
149 id: 1,
150 by: "bob",
151 kind: "comment",
152 text: "same here",
153 at: 0,
154 attachments: [{ id: "a00000000000f", name: "mine.png", type: "image/png", size: 3 * 1024 * 1024 }],
155 },
156 ],
157 edits: [],
158 asks: [],
159 claims: [],
160 children: [],
161 } as unknown as IssuePage;
162 const text = issuePage(page, {
163 attachmentUrl: (id: string) => `https://issues.example/p/x/attachments/${id}`,
164 }).join("\n");
165 assert.match(
166 text,
167 /attached a0123456789ab: "shot.png", 2 KB, https:\/\/issues.example\/p\/x\/attachments\/a0123456789ab/,
168 );
169 assert.match(text, /\n {4}attached a00000000000f: "mine.png", 3.0 MB/);
170 });
171});