Lines 3–9 at 318 (were 3–9 at 14)
33// here is a stand-in that keeps what it was sent. 55import assert from "node:assert/strict"; 6−import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
6+import { mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; 77import { createServer, type Server } from "node:http"; 88import type { AddressInfo } from "node:net"; 99import { tmpdir } from "node:os"; Lines 31–39 at 318 (were 31–36 at 14)
3131 const body = Buffer.concat(parts); 3232 seen.push({ method: req.method!, url: req.url!, headers: req.headers, body }); 3333 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"}'); 3437 if (req.url!.endsWith("/attachments")) 3639 id: `a${String(seen.length).padStart(12, "0")}`, Lines 43–94 at 318 (were 40–48 at 14)
4144 if (req.url!.endsWith("/issue")) return json({ id: 7 }); 4245 if (/\/issue\/\d+\/comment$/.test(req.url!)) return json({ id: 3 }); 46+ // An older Issues server keeps no files on asks: it answers without them (project "keepless"). 47+ const older = req.url!.includes("/p/keepless/"); 48+ if (req.url!.endsWith("/ask")) { 49+ const sent = (JSON.parse(body.toString()) as { attachments?: string[] }).attachments; 50+ return json({ id: 9, ...(sent && !older ? { attachments: sent } : {}) }); 52+ if (/\/answer\/\d+$/.test(req.url!)) { 53+ const sent = (JSON.parse(body.toString()) as { attachments?: string[] }).attachments ?? []; 54+ const files = older ? [] : sent.map((id) => ({ id, name: "x", type: "image/png", size: 1 })); 55+ return json({ ask: { by: "bob", answer: { by: "ann", words: "w", at: 0, attachments: files } } }); 57+ // Issue 9's ask carries a file on its question and one on its answer. 58+ if (req.method === "GET" && /\/issue\/9$/.test(req.url!)) 65+ attachments: [{ id: "a00000000000d", name: "question.png", type: "image/png", size: 13 }], 66+ answer: { attachments: [{ id: "a00000000000e", name: "answer.png", type: "image/png", size: 13 }] }, 72+ // Issue 8 carries an svg-drawn image on itself and again on a comment, plus one more on the comment. 73+ const logo = { id: "a00000000000b", name: "../shot one.png", type: "image/png", size: 13 }; 74+ if (req.method === "GET" && /\/issue\/8$/.test(req.url!)) 76+ issue: { id: 8, attachments: [{ ...logo, source: { name: "logo.svg", size: 20 } }] }, 77+ comments: [{ attachments: [logo, { id: "a00000000000c", name: "after.png", type: "image/png", size: 13 }] }], 83+ const served = /\/attachments\/(a[0-9a-f]{12})(\/source)?$/.exec(req.url!); 84+ if (req.method === "GET" && served) 85+ return res.writeHead(200, { "content-type": "image/png" }).end(`${served[1]}${served[2] ?? ""}`); 4386 res.writeHead(404).end("{}"); 89+ // As fake-server.ts: never close an idle kept-alive connection, or under load the client can reuse one as it closes 90+ // and fail with ECONNRESET (abso 410). 91+ issues.keepAliveTimeout = 0; 4692 await new Promise<void>((ok) => issues.listen(0, "127.0.0.1", ok)); 4793 issuesUrl = `http://127.0.0.1:${(issues.address() as AddressInfo).port}`; Lines 136–142 at 318 (were 90–96 at 14)
90136 calls.map((c) => `${c.method} ${c.url.replace(/^\/p\/[^/]+/, "")}`), 91137 ["POST /a/ann/attachments", "POST /a/ann/attachments", "POST /a/ann/issue"], 93− assert.equal(calls[0]!.headers["x-avcs-name"], "shot.png");
139+ assert.equal(calls[0]!.headers["x-abso-name"], "shot.png"); 94140 assert.equal(calls[0]!.headers["content-type"], "application/octet-stream"); 95141 assert.deepEqual([...calls[0]!.body], [0x89, 0x50, 0x4e, 0x47, 1, 2, 3]); 96142 const opened = JSON.parse(calls[2]!.body.toString()) as { title: string; attachments: string[] }; Lines 156–181 at 318 (were 110–118 at 14)
110156 assert.equal(seen.length - from, 2); 111157 const alone = await avcs(dir, ["issue", "7", "--attach", "shot.png"]); 112158 assert.equal(alone.exit, 1); 113− assert.match(alone.out, /--attach goes with avcs issue new or --comment/);
159+ assert.match(alone.out, /--attach goes with abso issue new or --comment/); 162+ it("says plainly when the server doesn't take attachments yet, and writes nothing", async () => { 163+ const dir = mkdtempSync(join(tmpdir(), "abso-attach-")); 165+ const init = await avcs(dir, ["init", "--server", fake.url, "--project", "old", "--agent", "ann"]); 166+ assert.equal(init.exit, 0, init.out); 167+ writeFileSync(join(dir, "shot.png"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 1])); 168+ const from = seen.length; 169+ const r = await avcs(dir, ["issue", "new", "T", "--attach", "shot.png"]); 170+ assert.equal(r.exit, 1); 171+ assert.match(r.out, /doesn't take attachments yet, so shot.png wasn't sent and nothing was written/); 173+ seen.slice(from).map((c) => c.url.replace(/^\/p\/[^/]+/, "")), 174+ ["/a/ann/attachments"], 175+ "no issue was opened", 116179 it("sends nothing when a file is missing", async () => { 117180 const dir = await folder(); 118181 const from = seen.length; Lines 185–272 at 318 (were 122–127 at 14)
122185 assert.equal(seen.length, from); 188+ it("--save-attachments saves each attachment once, an svg's own file too, outside the folder", async () => { 189+ const dir = await folder(); 190+ const out = mkdtempSync(join(tmpdir(), "abso-saved-")); 192+ const r = await avcs(dir, ["issue", "8", "--save-attachments", join(out, "images")]); 193+ assert.equal(r.exit, 0, r.out); 194+ const names = readdirSync(join(out, "images")).sort(); 195+ assert.deepEqual(names, ["a00000000000b-logo.svg", "a00000000000b-shot_one.png", "a00000000000c-after.png"]); 196+ assert.equal(readFileSync(join(out, "images", "a00000000000b-logo.svg"), "utf8"), "a00000000000b/source"); 197+ assert.equal(readFileSync(join(out, "images", "a00000000000c-after.png"), "utf8"), "a00000000000c"); 198+ assert.equal((r.json as { files: unknown[] }).files.length, 3); 199+ // Into the folder only where abso leaves files out, or they'd be recorded as edits. 200+ const inside = await avcs(dir, ["issue", "8", "--save-attachments", "images"]); 201+ assert.equal(inside.exit, 1); 202+ assert.match(inside.out, /inside this abso folder/); 203+ assert.equal((await avcs(dir, ["issue", "8", "--save-attachments", "."])).exit, 1); 204+ writeFileSync(join(dir, ".absoignore"), "saved/\n"); 205+ assert.equal((await avcs(dir, ["issue", "8", "--save-attachments", "saved"])).exit, 0); 206+ const mixed = await avcs(dir, ["issue", "8", "--save-attachments", out, "--comment", "x"]); 207+ assert.equal(mixed.exit, 1); 208+ assert.match(mixed.out, /--save-attachments goes alone/); 211+ it("goes on an ask and on an answer, and not on a withdrawal", async () => { 212+ const dir = await folder(); 213+ let from = seen.length; 214+ const asked = await avcs(dir, [ 217+ "Which looks right?", 221+ "shot.png,after.png", 223+ assert.equal(asked.exit, 0, asked.out); 225+ seen.slice(from).map((c) => `${c.method} ${c.url.replace(/^\/p\/[^/]+/, "")}`), 226+ ["POST /a/ann/attachments", "POST /a/ann/attachments", "POST /a/ann/ask"], 228+ const ask = JSON.parse(seen.at(-1)!.body.toString()) as { question: string; attachments: string[] }; 229+ assert.equal(ask.question, "Which looks right?"); 230+ assert.equal(ask.attachments.length, 2); 231+ assert.deepEqual((asked.json as { attachments: string[] }).attachments, ask.attachments); 233+ const answered = await avcs(dir, ["answer", "9", "This one", "--attach", "after.png"]); 234+ assert.equal(answered.exit, 0, answered.out); 235+ const answer = JSON.parse(seen.at(-1)!.body.toString()) as { words: string; attachments: string[] }; 236+ assert.equal(answer.words, "This one"); 237+ assert.equal(answer.attachments.length, 1); 238+ assert.equal(seen.length - from, 2); 240+ const withdrawn = await avcs(dir, ["ask", "--withdraw", "9", "--attach", "shot.png"]); 241+ assert.equal(withdrawn.exit, 1); 242+ assert.equal(seen.length, from, "nothing was sent"); 245+ it("says so when an older Issues server keeps an ask's words but not its files", async () => { 246+ const dir = mkdtempSync(join(tmpdir(), "abso-attach-")); 248+ const init = await avcs(dir, ["init", "--server", fake.url, "--project", "keepless", "--agent", "ann"]); 249+ assert.equal(init.exit, 0, init.out); 250+ writeFileSync(join(dir, "shot.png"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 1])); 251+ const asked = await avcs(dir, ["ask", "bob", "Which?", "--attach", "shot.png"]); 252+ assert.equal(asked.exit, 0, asked.out); 253+ assert.match(asked.out, /didn't keep the file on ask 9: it is older than this client/); 254+ const answered = await avcs(dir, ["answer", "9", "This", "--attach", "shot.png"]); 255+ assert.match(answered.out, /didn't keep the file on your answer to ask 9/); 256+ // A server that keeps them says nothing of the kind. 257+ const kept = await avcs(await folder(), ["ask", "bob", "Which?", "--attach", "shot.png"]); 258+ assert.doesNotMatch(kept.out, /didn't keep/); 261+ it("--save-attachments takes the files on an issue's asks and answers too", async () => { 262+ const dir = await folder(); 263+ const out = mkdtempSync(join(tmpdir(), "abso-saved-")); 265+ const r = await avcs(dir, ["issue", "9", "--save-attachments", out]); 266+ assert.equal(r.exit, 0, r.out); 267+ assert.deepEqual(readdirSync(out).sort(), ["a00000000000d-question.png", "a00000000000e-answer.png"]); 125270 it("an issue's page lists its attachments and each comment's, with links", () => { Lines 296–311 at 318 (were 151–157 at 14)
152297 text: "same here", 154− attachments: [{ id: "a00000000000f", name: "mine.png", type: "image/png", size: 3 * 1024 * 1024 }],
300+ { id: "a00000000000f", name: "mine.png", type: "image/png", size: 3 * 1024 * 1024 }, 302+ id: "a0000000000aa", 306+ source: { name: "logo.svg", size: 512 }, Lines 321–371 at 318 (were 167–171 at 14)
167321 /attached a0123456789ab: "shot.png", 2 KB, https:\/\/issues.example\/p\/x\/attachments\/a0123456789ab/, 169323 assert.match(text, /\n {4}attached a00000000000f: "mine.png", 3.0 MB/); 324+ // The way in comes before the first link, since an agent's fetch of a link is refused. 327+ /^Issue [^\n]*\n[^\n]*\n3 files are attached below\. To read them: abso issue \d+ --save-attachments <folder> saves every one there, then open the files\. The links are for people signed in on the website\.\n/, 329+ assert.doesNotMatch( 330+ issuePage({ ...page, issue: { ...page.issue, attachments: [] }, comments: [] } as unknown as IssuePage).join( 333+ /--save-attachments/, 335+ // An SVG is listed as its PNG, with its own file as a download beside it. 338+ /\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/, 340+ // An ask lists its question's files under it, and its answer's under the answer. 341+ const file = (id: string, name: string) => ({ id, name, type: "image/png", size: 1024 }); 352+ attachments: [file("a00000000000d", "a.png")], 355+ recommendation: null, 359+ answer: { by: "bob", words: "This", at: 0, attachments: [file("a00000000000e", "b.png")] }, 363+ } as unknown as IssuePage; 364+ const asks = issuePage(asked, { attachmentUrl: (id: string) => `https://i.example/${id}` }).join("\n"); 365+ assert.match(asks, /\n5 files are attached below\./, "an ask's question and answer files count too"); 368+ /ask 4: .*"Which\?"\n {4}attached a00000000000d: "a.png", 1 KB, https:\/\/i.example\/a00000000000d\n {4}answer: "This"\n {6}attached a00000000000e: "b.png", 1 KB/,