{"draft":null,"from":4,"to":5,"files":[{"path":"client/test/attach.test.ts","added":171,"removed":0,"diff":"--- /dev/null\n+++ b/client/test/attach.test.ts\n@@ -0,0 +1,171 @@\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, 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\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\tres.writeHead(404).end(\"{}\");\n+\t\t});\n+\t});\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-avcs-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 avcs issue new or --comment/);\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(\"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: [{ id: \"a00000000000f\", name: \"mine.png\", type: \"image/png\", size: 3 * 1024 * 1024 }],\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});\n+});"}],"unmatched":[]}