// MUST be first: sets DATABASE_URL before db_client is evaluated (image_uploads → // tos/plan_images don't touch the DAL, but the module graph of routes does). import "./setup_env"; import { describe, expect, it } from "bun:test"; import { extractTosImageUrls } from "@bbbug/shared/protocol"; import { checkUploadImage, imageMarkdown, storeUploadImage, uploadImageForOpenApi, MAX_UPLOAD_IMAGE_BYTES, } from "../src/image_uploads"; const png = (size = 8, name = "shot.png") => new File([new Uint8Array(size)], name, { type: "image/png" }); describe("checkUploadImage", () => { it("accepts a PNG and reports the TOS object extension", () => { const r = checkUploadImage(png()); expect(r.ok).toBe(true); if (r.ok) { expect(r.ext).toBe("png"); expect(r.contentType).toBe("image/png"); expect(r.file.size).toBe(8); } }); it("keeps accepting the other whitelisted types the console has always taken", () => { for (const [type, ext] of [ ["image/jpeg", "jpg"], ["image/gif", "gif"], ["image/webp", "webp"], ["image/heic", "heic"], ["image/svg+xml", "svg"], ] as const) { const r = checkUploadImage(new File([new Uint8Array(3)], "x", { type })); expect(r.ok, type).toBe(true); if (r.ok) expect(r.ext).toBe(ext); } }); it("rejects a missing multipart field with 400 — the shape of the request is the problem", () => { for (const raw of [undefined, null, "", "not-a-file", 42]) { expect(checkUploadImage(raw)).toEqual({ ok: false, status: 400, error: expect.stringContaining("file is required") }); } }); it("rejects non-image uploads with 415", () => { const r = checkUploadImage(new File([new Uint8Array(3)], "log.txt", { type: "text/plain" })); expect(r.ok).toBe(false); if (!r.ok) { expect(r.status).toBe(415); expect(r.error).toContain("text/plain"); } // A file with no content-type at all is not silently treated as an image. expect(checkUploadImage(new File([new Uint8Array(3)], "mystery")).ok).toBe(false); }); it("rejects an empty file with 400 instead of storing a broken image", () => { expect(checkUploadImage(png(0))).toEqual({ ok: false, status: 400, error: "file is empty" }); }); it("rejects images over the 12MB cap with 413", () => { const r = checkUploadImage(png(MAX_UPLOAD_IMAGE_BYTES + 1)); expect(r.ok).toBe(false); if (!r.ok) { expect(r.status).toBe(413); expect(r.error).toContain("12MB"); } }); it("accepts an image exactly at the cap", () => { expect(checkUploadImage(png(MAX_UPLOAD_IMAGE_BYTES)).ok).toBe(true); }); }); describe("storeUploadImage", () => { it("stores under . and returns the public voffline URL", async () => { const calls: { key: string; bytes: Uint8Array; contentType: string }[] = []; const bytes = new Uint8Array([1, 2, 3, 4]); const stored = await storeUploadImage( new File([bytes], "shot.png", { type: "image/png" }), "png", "image/png", async (key, data, contentType) => { calls.push({ key, bytes: data, contentType }); return `https://voffline.example/img_upload/${key}`; }, ); expect(stored.uuid).toMatch(/^[0-9a-f-]{36}$/); expect(stored.ext).toBe("png"); expect(calls).toHaveLength(1); expect(calls[0].key).toBe(`${stored.uuid}.png`); expect(calls[0].contentType).toBe("image/png"); expect([...calls[0].bytes]).toEqual([1, 2, 3, 4]); expect(stored.url).toBe(`https://voffline.example/img_upload/${stored.uuid}.png`); }); it("produces a URL the satellite's description-image materialiser actually recognises", async () => { // 跨模块契约:上传返回的 URL 必须命中 shared/src/protocol.ts 的 TOS_IMAGE_BASE 前缀, // 否则卫星只会在正文里看到一个打不开的链接。这里用真实的前缀,不 mock 它。 const stored = await storeUploadImage(png(), "png", "image/png", async (key) => `https://voffline.byted.org/download/tos/schedule/streamline/img_upload/${key}`); const refs = extractTosImageUrls(`反例:\n\n![shot.png](${stored.url})`); expect(refs).toEqual([{ url: stored.url, file: `${stored.uuid}.png` }]); }); }); describe("imageMarkdown", () => { it("builds a markdown image reference the satellite can materialise", () => { expect(imageMarkdown("shot.png", "https://voffline.example/img_upload/a.png")) .toBe("![shot.png](https://voffline.example/img_upload/a.png)"); }); it("keeps the syntax intact when the filename would break it", () => { expect(imageMarkdown("a [b]\r\nc.png", "https://x/y.png")).toBe("![a b c.png](https://x/y.png)"); // 反斜杠会转义后面的 `]`,同样提前闭合图片语法。 expect(imageMarkdown("a\\", "https://x/y.png")).toBe("![a](https://x/y.png)"); expect(imageMarkdown("shot\\.png", "https://x/y.png")).toBe("![shot .png](https://x/y.png)"); }); it("falls back to a placeholder rather than an empty alt", () => { expect(imageMarkdown(undefined, "https://x/y.png")).toBe("![image](https://x/y.png)"); expect(imageMarkdown(" ", "https://x/y.png")).toBe("![image](https://x/y.png)"); }); it("caps the alt so a giant filename cannot bloat the description", () => { const url = "https://x/y.png"; const md = imageMarkdown("n".repeat(500), url); const alt = md.slice(2, md.indexOf("](", 2)); expect(alt.length).toBe(120); expect(md).toBe(`![${alt}](${url})`); }); }); describe("uploadImageForOpenApi", () => { const upload = async (key: string) => `https://voffline.example/img_upload/${key}`; it("returns the public URL plus a ready-to-paste markdown line", async () => { const out = await uploadImageForOpenApi(png(), { tosConfigured: true, upload }); expect(out.ok).toBe(true); if (out.ok) { expect(out.body).toMatchObject({ ext: "png", name: "shot.png" }); expect(out.body.url).toBe(`https://voffline.example/img_upload/${out.body.uuid}.png`); expect(out.body.markdown).toBe(`![shot.png](${out.body.url})`); } }); it("reports storage-not-configured as 503 instead of signing an unusable URL", async () => { let uploaded = false; const out = await uploadImageForOpenApi(png(), { tosConfigured: false, upload: async (key) => { uploaded = true; return key; } }); expect(out.ok).toBe(false); if (!out.ok) { expect(out.status).toBe(503); expect(out.error).toContain("not configured"); } expect(uploaded).toBe(false); }); it("hides TOS internals behind a generic, retryable 502", async () => { const out = await uploadImageForOpenApi(png(), { tosConfigured: true, upload: async () => { throw new Error("429 ExceedQPS at tos-s3-cn-north.byted.org/streamline"); } }); expect(out).toEqual({ ok: false, status: 502, error: "image upload failed; please retry later" }); }); it("checks the request before the storage configuration — a malformed call stays 4xx", async () => { const out = await uploadImageForOpenApi(new File([new Uint8Array([1])], "x.txt", { type: "text/plain" }), { tosConfigured: false }); expect(out.ok).toBe(false); if (!out.ok) expect(out.status).toBe(415); }); });