| 1 | import assert from "node:assert/strict"; |
| 2 | import { attachOrdinaryFile, attachmentUploadFailure } from "../lib/attachmentSubmit"; |
| 3 | import type { AppBindings } from "../lib/bridge"; |
| 4 | |
| 5 | const limit = { code: "attachment.too_large", message: "attachment exceeds 4 byte stream limit (user)", limit_bytes: 4, limit_source: "user" }; |
| 6 | await assert.rejects( |
| 7 | attachOrdinaryFile({} as AppBindings, "captured", new File(["12345"], "app.apk"), undefined, |
| 8 | (async () => new Response(JSON.stringify(limit), { status: 413, headers: { "content-type": "application/json" } })) as typeof fetch), |
| 9 | (error: unknown) => { |
| 10 | assert.ok(error instanceof Error); |
| 11 | assert.equal(error.message, limit.message); |
| 12 | assert.equal((error as Error & typeof limit).code, limit.code); |
| 13 | assert.equal((error as Error & typeof limit).limit_bytes, 4); |
| 14 | assert.equal((error as Error & typeof limit).limit_source, "user"); |
| 15 | assert.equal(attachmentUploadFailure(error, "generic failure"), limit.message); |
| 16 | assert.equal(attachmentUploadFailure(error, "generic failure", bytes => `limit ${bytes}`), "limit 4"); |
| 17 | return true; |
| 18 | }, |
| 19 | ); |
| 20 | assert.equal(attachmentUploadFailure(new Error("unclassified server error"), "generic failure"), "generic failure"); |
| 21 | await assert.rejects( |
| 22 | attachOrdinaryFile({} as AppBindings, "captured", new File(["12345"], "app.apk"), undefined, |
| 23 | (async () => new Response("upstream unavailable", { status: 502 })) as typeof fetch), |
| 24 | /File upload failed \(502\)/, |
| 25 | ); |
| 26 | console.log("attachment stream limit: typed server errors retain ceiling and source"); |
| 27 |