{"draft":null,"from":2,"to":318,"files":[{"path":"client/test/attach.test.ts","added":371,"removed":0,"diff":"--- /dev/null\n+++ b/client/test/attach.test.ts\n@@ -0,0 +1,371 @@\n+// --attach (abso issue 171): images go up to the Issues Worker before the\n+// write that carries them, and the issue's page lists them. The Issues server\n+// here is a stand-in that keeps what it was sent.\n+\n+import assert from \"node:assert/strict\";\n+import { mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from \"node:fs\";\n+import { createServer, type Server } from \"node:http\";\n+import type { AddressInfo } from \"node:net\";\n+import { tmpdir } from \"node:os\";\n+import { join } from \"node:path\";\n+import { after, before, describe, it } from \"node:test\";\n+import { issuePage } from \"../src/issues.ts\";\n+import type { IssuePage } from \"../src/issues-wire.ts\";\n+import { main } from \"../src/main.ts\";\n+import { type Fake, startFake } from \"./fake-server.ts\";\n+\n+const TOKEN = \"test-token-not-secret\";\n+let fake: Fake;\n+let issues: Server;\n+let issuesUrl: string;\n+const seen: { method: string; url: string; headers: Record<string, string | string[] | undefined>; body: Buffer }[] =\n+\t[];\n+const dirs: string[] = [];\n+\n+before(async () => {\n+\tfake = await startFake(0, TOKEN);\n+\tissues = createServer((req, res) => {\n+\t\tconst parts: Buffer[] = [];\n+\t\treq.on(\"data\", (c: Buffer) => parts.push(c));\n+\t\treq.on(\"end\", () => {\n+\t\t\tconst body = Buffer.concat(parts);\n+\t\t\tseen.push({ method: req.method!, url: req.url!, headers: req.headers, body });\n+\t\t\tconst json = (o: unknown) => res.writeHead(200, { \"content-type\": \"application/json\" }).end(JSON.stringify(o));\n+\t\t\t// A server from before attachments: it reads every POST body as JSON.\n+\t\t\tif (req.url!.includes(\"/p/old\") && req.url!.endsWith(\"/attachments\"))\n+\t\t\t\treturn res.writeHead(400, { \"content-type\": \"application/json\" }).end('{\"error\":\"the body is JSON\"}');\n+\t\t\tif (req.url!.endsWith(\"/attachments\"))\n+\t\t\t\treturn json({\n+\t\t\t\t\tid: `a${String(seen.length).padStart(12, \"0\")}`,\n+\t\t\t\t\tname: \"x\",\n+\t\t\t\t\ttype: \"image/png\",\n+\t\t\t\t\tsize: body.length,\n+\t\t\t\t});\n+\t\t\tif (req.url!.endsWith(\"/issue\")) return json({ id: 7 });\n+\t\t\tif (/\\/issue\\/\\d+\\/comment$/.test(req.url!)) return json({ id: 3 });\n+\t\t\t// An older Issues server keeps no files on asks: it answers without them (project \"keepless\").\n+\t\t\tconst older = req.url!.includes(\"/p/keepless/\");\n+\t\t\tif (req.url!.endsWith(\"/ask\")) {\n+\t\t\t\tconst sent = (JSON.parse(body.toString()) as { attachments?: string[] }).attachments;\n+\t\t\t\treturn json({ id: 9, ...(sent && !older ? { attachments: sent } : {}) });\n+\t\t\t}\n+\t\t\tif (/\\/answer\\/\\d+$/.test(req.url!)) {\n+\t\t\t\tconst sent = (JSON.parse(body.toString()) as { attachments?: string[] }).attachments ?? [];\n+\t\t\t\tconst files = older ? [] : sent.map((id) => ({ id, name: \"x\", type: \"image/png\", size: 1 }));\n+\t\t\t\treturn json({ ask: { by: \"bob\", answer: { by: \"ann\", words: \"w\", at: 0, attachments: files } } });\n+\t\t\t}\n+\t\t\t// Issue 9's ask carries a file on its question and one on its answer.\n+\t\t\tif (req.method === \"GET\" && /\\/issue\\/9$/.test(req.url!))\n+\t\t\t\treturn json({\n+\t\t\t\t\tissue: { id: 9 },\n+\t\t\t\t\tcomments: [],\n+\t\t\t\t\tedits: [],\n+\t\t\t\t\tasks: [\n+\t\t\t\t\t\t{\n+\t\t\t\t\t\t\tattachments: [{ id: \"a00000000000d\", name: \"question.png\", type: \"image/png\", size: 13 }],\n+\t\t\t\t\t\t\tanswer: { attachments: [{ id: \"a00000000000e\", name: \"answer.png\", type: \"image/png\", size: 13 }] },\n+\t\t\t\t\t\t},\n+\t\t\t\t\t],\n+\t\t\t\t\tclaims: [],\n+\t\t\t\t\tchildren: [],\n+\t\t\t\t});\n+\t\t\t// Issue 8 carries an svg-drawn image on itself and again on a comment, plus one more on the comment.\n+\t\t\tconst logo = { id: \"a00000000000b\", name: \"../shot one.png\", type: \"image/png\", size: 13 };\n+\t\t\tif (req.method === \"GET\" && /\\/issue\\/8$/.test(req.url!))\n+\t\t\t\treturn json({\n+\t\t\t\t\tissue: { id: 8, attachments: [{ ...logo, source: { name: \"logo.svg\", size: 20 } }] },\n+\t\t\t\t\tcomments: [{ attachments: [logo, { id: \"a00000000000c\", name: \"after.png\", type: \"image/png\", size: 13 }] }],\n+\t\t\t\t\tedits: [],\n+\t\t\t\t\tasks: [],\n+\t\t\t\t\tclaims: [],\n+\t\t\t\t\tchildren: [],\n+\t\t\t\t});\n+\t\t\tconst served = /\\/attachments\\/(a[0-9a-f]{12})(\\/source)?$/.exec(req.url!);\n+\t\t\tif (req.method === \"GET\" && served)\n+\t\t\t\treturn res.writeHead(200, { \"content-type\": \"image/png\" }).end(`${served[1]}${served[2] ?? \"\"}`);\n+\t\t\tres.writeHead(404).end(\"{}\");\n+\t\t});\n+\t});\n+\t// As fake-server.ts: never close an idle kept-alive connection, or under load the client can reuse one as it closes\n+\t// and fail with ECONNRESET (abso 410).\n+\tissues.keepAliveTimeout = 0;\n+\tawait new Promise<void>((ok) => issues.listen(0, \"127.0.0.1\", ok));\n+\tissuesUrl = `http://127.0.0.1:${(issues.address() as AddressInfo).port}`;\n+});\n+after(async () => {\n+\tawait fake.close();\n+\tawait new Promise((ok) => issues.close(ok));\n+\tfor (const d of dirs) rmSync(d, { recursive: true, force: true, maxRetries: 5 });\n+});\n+\n+const ENV = (): Record<string, string> => ({\n+\tAVCS_TOKEN: TOKEN,\n+\tAVCS_HOOK_FORCE: \"1\",\n+\tAVCS_ISSUES_SERVER: issuesUrl,\n+\tAVCS_SERVERS_FILE: \"no-servers.json\",\n+});\n+\n+async function avcs(cwd: string, argv: string[]): Promise<{ exit: number; out: string; json?: unknown }> {\n+\tconst r = await main([...argv, \"--json\"], { cwd, env: ENV(), write: () => {} });\n+\tlet json: unknown;\n+\ttry {\n+\t\tjson = JSON.parse(r.stdout);\n+\t} catch {}\n+\treturn { exit: r.exit, out: r.stdout + r.stderr, json };\n+}\n+\n+let n = 0;\n+async function folder(): Promise<string> {\n+\tconst dir = mkdtempSync(join(tmpdir(), \"abso-attach-\"));\n+\tdirs.push(dir);\n+\tconst r = await avcs(dir, [\"init\", \"--server\", fake.url, \"--project\", `attach${++n}`, \"--agent\", \"ann\"]);\n+\tassert.equal(r.exit, 0, r.out);\n+\twriteFileSync(join(dir, \"shot.png\"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 1, 2, 3]));\n+\twriteFileSync(join(dir, \"after.png\"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 4, 5]));\n+\treturn dir;\n+}\n+\n+describe(\"--attach (abso issue 171)\", () => {\n+\tit(\"uploads each file, then opens the issue carrying their ids\", async () => {\n+\t\tconst dir = await folder();\n+\t\tconst from = seen.length;\n+\t\tconst r = await avcs(dir, [\"issue\", \"new\", \"The page after the fix\", \"--attach\", \"shot.png,after.png\"]);\n+\t\tassert.equal(r.exit, 0, r.out);\n+\t\tconst calls = seen.slice(from);\n+\t\tassert.deepEqual(\n+\t\t\tcalls.map((c) => `${c.method} ${c.url.replace(/^\\/p\\/[^/]+/, \"\")}`),\n+\t\t\t[\"POST /a/ann/attachments\", \"POST /a/ann/attachments\", \"POST /a/ann/issue\"],\n+\t\t);\n+\t\tassert.equal(calls[0]!.headers[\"x-abso-name\"], \"shot.png\");\n+\t\tassert.equal(calls[0]!.headers[\"content-type\"], \"application/octet-stream\");\n+\t\tassert.deepEqual([...calls[0]!.body], [0x89, 0x50, 0x4e, 0x47, 1, 2, 3]);\n+\t\tconst opened = JSON.parse(calls[2]!.body.toString()) as { title: string; attachments: string[] };\n+\t\tassert.equal(opened.title, \"The page after the fix\");\n+\t\tassert.equal(opened.attachments.length, 2);\n+\t\tassert.deepEqual((r.json as { attachments: string[] }).attachments, opened.attachments);\n+\t});\n+\n+\tit(\"goes on a comment too, and nowhere else\", async () => {\n+\t\tconst dir = await folder();\n+\t\tconst from = seen.length;\n+\t\tconst r = await avcs(dir, [\"issue\", \"7\", \"--comment\", \"after\", \"--attach\", \"shot.png\"]);\n+\t\tassert.equal(r.exit, 0, r.out);\n+\t\tconst comment = JSON.parse(seen.at(-1)!.body.toString()) as { text: string; attachments: string[] };\n+\t\tassert.equal(comment.text, \"after\");\n+\t\tassert.equal(comment.attachments.length, 1);\n+\t\tassert.equal(seen.length - from, 2);\n+\t\tconst alone = await avcs(dir, [\"issue\", \"7\", \"--attach\", \"shot.png\"]);\n+\t\tassert.equal(alone.exit, 1);\n+\t\tassert.match(alone.out, /--attach goes with abso issue new or --comment/);\n+\t});\n+\n+\tit(\"says plainly when the server doesn't take attachments yet, and writes nothing\", async () => {\n+\t\tconst dir = mkdtempSync(join(tmpdir(), \"abso-attach-\"));\n+\t\tdirs.push(dir);\n+\t\tconst init = await avcs(dir, [\"init\", \"--server\", fake.url, \"--project\", \"old\", \"--agent\", \"ann\"]);\n+\t\tassert.equal(init.exit, 0, init.out);\n+\t\twriteFileSync(join(dir, \"shot.png\"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 1]));\n+\t\tconst from = seen.length;\n+\t\tconst r = await avcs(dir, [\"issue\", \"new\", \"T\", \"--attach\", \"shot.png\"]);\n+\t\tassert.equal(r.exit, 1);\n+\t\tassert.match(r.out, /doesn't take attachments yet, so shot.png wasn't sent and nothing was written/);\n+\t\tassert.deepEqual(\n+\t\t\tseen.slice(from).map((c) => c.url.replace(/^\\/p\\/[^/]+/, \"\")),\n+\t\t\t[\"/a/ann/attachments\"],\n+\t\t\t\"no issue was opened\",\n+\t\t);\n+\t});\n+\n+\tit(\"sends nothing when a file is missing\", async () => {\n+\t\tconst dir = await folder();\n+\t\tconst from = seen.length;\n+\t\tconst r = await avcs(dir, [\"issue\", \"new\", \"T\", \"--attach\", \"shot.png,nope.png\"]);\n+\t\tassert.equal(r.exit, 1);\n+\t\tassert.match(r.out, /--attach nope.png: no such file/);\n+\t\tassert.equal(seen.length, from);\n+\t});\n+\n+\tit(\"--save-attachments saves each attachment once, an svg's own file too, outside the folder\", async () => {\n+\t\tconst dir = await folder();\n+\t\tconst out = mkdtempSync(join(tmpdir(), \"abso-saved-\"));\n+\t\tdirs.push(out);\n+\t\tconst r = await avcs(dir, [\"issue\", \"8\", \"--save-attachments\", join(out, \"images\")]);\n+\t\tassert.equal(r.exit, 0, r.out);\n+\t\tconst names = readdirSync(join(out, \"images\")).sort();\n+\t\tassert.deepEqual(names, [\"a00000000000b-logo.svg\", \"a00000000000b-shot_one.png\", \"a00000000000c-after.png\"]);\n+\t\tassert.equal(readFileSync(join(out, \"images\", \"a00000000000b-logo.svg\"), \"utf8\"), \"a00000000000b/source\");\n+\t\tassert.equal(readFileSync(join(out, \"images\", \"a00000000000c-after.png\"), \"utf8\"), \"a00000000000c\");\n+\t\tassert.equal((r.json as { files: unknown[] }).files.length, 3);\n+\t\t// Into the folder only where abso leaves files out, or they'd be recorded as edits.\n+\t\tconst inside = await avcs(dir, [\"issue\", \"8\", \"--save-attachments\", \"images\"]);\n+\t\tassert.equal(inside.exit, 1);\n+\t\tassert.match(inside.out, /inside this abso folder/);\n+\t\tassert.equal((await avcs(dir, [\"issue\", \"8\", \"--save-attachments\", \".\"])).exit, 1);\n+\t\twriteFileSync(join(dir, \".absoignore\"), \"saved/\\n\");\n+\t\tassert.equal((await avcs(dir, [\"issue\", \"8\", \"--save-attachments\", \"saved\"])).exit, 0);\n+\t\tconst mixed = await avcs(dir, [\"issue\", \"8\", \"--save-attachments\", out, \"--comment\", \"x\"]);\n+\t\tassert.equal(mixed.exit, 1);\n+\t\tassert.match(mixed.out, /--save-attachments goes alone/);\n+\t});\n+\n+\tit(\"goes on an ask and on an answer, and not on a withdrawal\", async () => {\n+\t\tconst dir = await folder();\n+\t\tlet from = seen.length;\n+\t\tconst asked = await avcs(dir, [\n+\t\t\t\"ask\",\n+\t\t\t\"bob\",\n+\t\t\t\"Which looks right?\",\n+\t\t\t\"--issue\",\n+\t\t\t\"7\",\n+\t\t\t\"--attach\",\n+\t\t\t\"shot.png,after.png\",\n+\t\t]);\n+\t\tassert.equal(asked.exit, 0, asked.out);\n+\t\tassert.deepEqual(\n+\t\t\tseen.slice(from).map((c) => `${c.method} ${c.url.replace(/^\\/p\\/[^/]+/, \"\")}`),\n+\t\t\t[\"POST /a/ann/attachments\", \"POST /a/ann/attachments\", \"POST /a/ann/ask\"],\n+\t\t);\n+\t\tconst ask = JSON.parse(seen.at(-1)!.body.toString()) as { question: string; attachments: string[] };\n+\t\tassert.equal(ask.question, \"Which looks right?\");\n+\t\tassert.equal(ask.attachments.length, 2);\n+\t\tassert.deepEqual((asked.json as { attachments: string[] }).attachments, ask.attachments);\n+\t\tfrom = seen.length;\n+\t\tconst answered = await avcs(dir, [\"answer\", \"9\", \"This one\", \"--attach\", \"after.png\"]);\n+\t\tassert.equal(answered.exit, 0, answered.out);\n+\t\tconst answer = JSON.parse(seen.at(-1)!.body.toString()) as { words: string; attachments: string[] };\n+\t\tassert.equal(answer.words, \"This one\");\n+\t\tassert.equal(answer.attachments.length, 1);\n+\t\tassert.equal(seen.length - from, 2);\n+\t\tfrom = seen.length;\n+\t\tconst withdrawn = await avcs(dir, [\"ask\", \"--withdraw\", \"9\", \"--attach\", \"shot.png\"]);\n+\t\tassert.equal(withdrawn.exit, 1);\n+\t\tassert.equal(seen.length, from, \"nothing was sent\");\n+\t});\n+\n+\tit(\"says so when an older Issues server keeps an ask's words but not its files\", async () => {\n+\t\tconst dir = mkdtempSync(join(tmpdir(), \"abso-attach-\"));\n+\t\tdirs.push(dir);\n+\t\tconst init = await avcs(dir, [\"init\", \"--server\", fake.url, \"--project\", \"keepless\", \"--agent\", \"ann\"]);\n+\t\tassert.equal(init.exit, 0, init.out);\n+\t\twriteFileSync(join(dir, \"shot.png\"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 1]));\n+\t\tconst asked = await avcs(dir, [\"ask\", \"bob\", \"Which?\", \"--attach\", \"shot.png\"]);\n+\t\tassert.equal(asked.exit, 0, asked.out);\n+\t\tassert.match(asked.out, /didn't keep the file on ask 9: it is older than this client/);\n+\t\tconst answered = await avcs(dir, [\"answer\", \"9\", \"This\", \"--attach\", \"shot.png\"]);\n+\t\tassert.match(answered.out, /didn't keep the file on your answer to ask 9/);\n+\t\t// A server that keeps them says nothing of the kind.\n+\t\tconst kept = await avcs(await folder(), [\"ask\", \"bob\", \"Which?\", \"--attach\", \"shot.png\"]);\n+\t\tassert.doesNotMatch(kept.out, /didn't keep/);\n+\t});\n+\n+\tit(\"--save-attachments takes the files on an issue's asks and answers too\", async () => {\n+\t\tconst dir = await folder();\n+\t\tconst out = mkdtempSync(join(tmpdir(), \"abso-saved-\"));\n+\t\tdirs.push(out);\n+\t\tconst r = await avcs(dir, [\"issue\", \"9\", \"--save-attachments\", out]);\n+\t\tassert.equal(r.exit, 0, r.out);\n+\t\tassert.deepEqual(readdirSync(out).sort(), [\"a00000000000d-question.png\", \"a00000000000e-answer.png\"]);\n+\t});\n+\n+\tit(\"an issue's page lists its attachments and each comment's, with links\", () => {\n+\t\tconst page = {\n+\t\t\tissue: {\n+\t\t\t\tid: 7,\n+\t\t\t\ttitle: \"T\",\n+\t\t\t\tstatus: \"open\",\n+\t\t\t\topened: { by: \"ann\", at: 0 },\n+\t\t\t\tagent: null,\n+\t\t\t\tagents: [],\n+\t\t\t\tblockedBy: [],\n+\t\t\t\tlabels: [],\n+\t\t\t\tversions: [],\n+\t\t\t\twaitingOn: [],\n+\t\t\t\tparent: null,\n+\t\t\t\tchildren: 0,\n+\t\t\t\tupdated: 0,\n+\t\t\t\tclosed: null,\n+\t\t\t\tdescription: \"\",\n+\t\t\t\tlines: null,\n+\t\t\t\twatchers: [],\n+\t\t\t\tattachments: [{ id: \"a0123456789ab\", name: \"shot.png\", type: \"image/png\", size: 2048 }],\n+\t\t\t},\n+\t\t\tcomments: [\n+\t\t\t\t{\n+\t\t\t\t\tid: 1,\n+\t\t\t\t\tby: \"bob\",\n+\t\t\t\t\tkind: \"comment\",\n+\t\t\t\t\ttext: \"same here\",\n+\t\t\t\t\tat: 0,\n+\t\t\t\t\tattachments: [\n+\t\t\t\t\t\t{ id: \"a00000000000f\", name: \"mine.png\", type: \"image/png\", size: 3 * 1024 * 1024 },\n+\t\t\t\t\t\t{\n+\t\t\t\t\t\t\tid: \"a0000000000aa\",\n+\t\t\t\t\t\t\tname: \"logo.png\",\n+\t\t\t\t\t\t\ttype: \"image/png\",\n+\t\t\t\t\t\t\tsize: 4096,\n+\t\t\t\t\t\t\tsource: { name: \"logo.svg\", size: 512 },\n+\t\t\t\t\t\t},\n+\t\t\t\t\t],\n+\t\t\t\t},\n+\t\t\t],\n+\t\t\tedits: [],\n+\t\t\tasks: [],\n+\t\t\tclaims: [],\n+\t\t\tchildren: [],\n+\t\t} as unknown as IssuePage;\n+\t\tconst text = issuePage(page, {\n+\t\t\tattachmentUrl: (id: string) => `https://issues.example/p/x/attachments/${id}`,\n+\t\t}).join(\"\\n\");\n+\t\tassert.match(\n+\t\t\ttext,\n+\t\t\t/attached a0123456789ab: \"shot.png\", 2 KB, https:\\/\\/issues.example\\/p\\/x\\/attachments\\/a0123456789ab/,\n+\t\t);\n+\t\tassert.match(text, /\\n {4}attached a00000000000f: \"mine.png\", 3.0 MB/);\n+\t\t// The way in comes before the first link, since an agent's fetch of a link is refused.\n+\t\tassert.match(\n+\t\t\ttext,\n+\t\t\t/^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/,\n+\t\t);\n+\t\tassert.doesNotMatch(\n+\t\t\tissuePage({ ...page, issue: { ...page.issue, attachments: [] }, comments: [] } as unknown as IssuePage).join(\n+\t\t\t\t\"\\n\",\n+\t\t\t),\n+\t\t\t/--save-attachments/,\n+\t\t);\n+\t\t// An SVG is listed as its PNG, with its own file as a download beside it.\n+\t\tassert.match(\n+\t\t\ttext,\n+\t\t\t/\\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/,\n+\t\t);\n+\t\t// An ask lists its question's files under it, and its answer's under the answer.\n+\t\tconst file = (id: string, name: string) => ({ id, name, type: \"image/png\", size: 1024 });\n+\t\tconst asked = {\n+\t\t\t...page,\n+\t\t\tasks: [\n+\t\t\t\t{\n+\t\t\t\t\tid: 4,\n+\t\t\t\t\tissue: 7,\n+\t\t\t\t\tby: \"ann\",\n+\t\t\t\t\tof: \"bob\",\n+\t\t\t\t\tkind: \"question\",\n+\t\t\t\t\tquestion: \"Which?\",\n+\t\t\t\t\tattachments: [file(\"a00000000000d\", \"a.png\")],\n+\t\t\t\t\treview: false,\n+\t\t\t\t\tdue: null,\n+\t\t\t\t\trecommendation: null,\n+\t\t\t\t\toptions: [],\n+\t\t\t\t\tblocks: [],\n+\t\t\t\t\tat: 0,\n+\t\t\t\t\tanswer: { by: \"bob\", words: \"This\", at: 0, attachments: [file(\"a00000000000e\", \"b.png\")] },\n+\t\t\t\t\tseen: null,\n+\t\t\t\t},\n+\t\t\t],\n+\t\t} as unknown as IssuePage;\n+\t\tconst asks = issuePage(asked, { attachmentUrl: (id: string) => `https://i.example/${id}` }).join(\"\\n\");\n+\t\tassert.match(asks, /\\n5 files are attached below\\./, \"an ask's question and answer files count too\");\n+\t\tassert.match(\n+\t\t\tasks,\n+\t\t\t/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/,\n+\t\t);\n+\t});\n+});"}],"unmatched":[]}