import { describe, expect, test } from "bun:test"; import { eddyShareDownloadName } from "@bbbug/shared/eddy_share"; import { safeFileName } from "@bbbug/shared/ideas"; import { TOS_DOWNLOAD_BASE } from "@bbbug/shared/protocol"; import { attachmentDisposition, ideaBundleResponse } from "../src/idea_bundle"; const stored = (name: string) => `${TOS_DOWNLOAD_BASE}/streamline/ideas/00000000-0000-4000-8000-000000000000/eddy-${safeFileName(name)}`; describe("Eddy 包存到 TOS 再下回来:扩展名和文件名", () => { test("存下的对象名保留扩展名,但中文标题只剩下划线(所以下载时要另给名字)", () => { expect(safeFileName("首页换个标题.eddy")).toBe("_.eddy"); expect(safeFileName("首页换个标题.eddy.zip")).toBe("_.eddy.zip"); expect(safeFileName("Repost & Send.eddy")).toBe("Repost_Send.eddy"); }); test("下载名 = 想法标题 + 上传时的扩展名", () => { expect(eddyShareDownloadName("首页换个标题", stored("首页换个标题.eddy"))).toBe("首页换个标题.eddy"); expect(eddyShareDownloadName("首页换个标题", stored("首页换个标题.eddy.zip"))).toBe("首页换个标题.eddy.zip"); expect(eddyShareDownloadName("首页换个标题", stored("old.zip"))).toBe("首页换个标题.zip"); expect(eddyShareDownloadName("首页换个标题", `${stored("a.EDDY")}?x=1#frag`)).toBe("首页换个标题.eddy"); }); test("标题里不能当文件名的字符换成 -,首尾的点和空格去掉;没有标题或认不出扩展名时有缺省", () => { expect(eddyShareDownloadName(' a/b\\c:d*e?"fh|i. ', stored("x.eddy"))).toBe("a-b-c-d-e--f-g-h-i.eddy"); expect(eddyShareDownloadName("两个按钮 · A&B", stored("x.eddy"))).toBe("两个按钮 · A&B.eddy"); expect(eddyShareDownloadName(" .. ", stored("x.eddy"))).toBe("Eddy 任务.eddy"); expect(eddyShareDownloadName("长".repeat(300), stored("x.eddy"))).toBe(`${"长".repeat(100)}.eddy`); expect(eddyShareDownloadName("标题", "https://example.invalid/bundle")).toBe("标题.eddy.zip"); }); test("Content-Disposition:filename* 带 UTF-8 真名,filename 是 ASCII 后备名", () => { expect(attachmentDisposition("首页换个标题.eddy")).toBe(`attachment; filename="_.eddy"; filename*=UTF-8''%E9%A6%96%E9%A1%B5%E6%8D%A2%E4%B8%AA%E6%A0%87%E9%A2%98.eddy`); expect(attachmentDisposition(`it's "a" (b)*.eddy`)).toBe(`attachment; filename="it's _a_ (b)*.eddy"; filename*=UTF-8''it%27s%20%22a%22%20%28b%29%2A.eddy`); }); }); describe("ideaBundleResponse", () => { const body = new Uint8Array([0x50, 0x4b, 3, 4, 1, 2, 3]); const idea = { title: "首页换个标题", bundleUrl: stored("首页换个标题.eddy") }; test("从 TOS 取回来原样转出:名字是标题 + .eddy,类型是 octet-stream", async () => { const asked: string[] = []; const res = await ideaBundleResponse(idea, async (url) => { asked.push(url); return new Response(body, { headers: { "content-type": "application/zip", "content-length": String(body.length) } }); }); expect(asked).toEqual([idea.bundleUrl]); expect(res.status).toBe(200); expect(res.headers.get("content-type")).toBe("application/octet-stream"); expect(res.headers.get("content-disposition")).toBe(attachmentDisposition("首页换个标题.eddy")); expect(res.headers.get("content-length")).toBe(String(body.length)); expect([...new Uint8Array(await res.arrayBuffer())]).toEqual([...body]); }); test("原来传的 .eddy.zip 下回来还是 .eddy.zip", async () => { const res = await ideaBundleResponse({ title: "老包", bundleUrl: stored("老包.eddy.zip") }, async () => new Response(body)); expect(res.headers.get("content-disposition")).toBe(attachmentDisposition("老包.eddy.zip")); }); test("上游压缩过时不照抄长度", async () => { const res = await ideaBundleResponse(idea, async () => new Response(body, { headers: { "content-length": "3", "content-encoding": "gzip" } })); expect(res.headers.get("content-length")).not.toBe("3"); }); test("TOS 取不到(非 2xx 或请求失败)就 302 回直链", async () => { for (const fetcher of [async () => new Response("no", { status: 404 }), async () => { throw new Error("reset"); }]) { const res = await ideaBundleResponse(idea, fetcher); expect(res.status).toBe(302); expect(res.headers.get("location")).toBe(idea.bundleUrl); } }); test("不是 TOS 下载地址的不代取,直接 302;没有包是 404", async () => { let called = 0; const res = await ideaBundleResponse({ title: "x", bundleUrl: "https://example.invalid/eddy-bundle.zip" }, async () => { called++; return new Response(body); }); expect(res.status).toBe(302); expect(res.headers.get("location")).toBe("https://example.invalid/eddy-bundle.zip"); expect(called).toBe(0); expect((await ideaBundleResponse({ title: "x", bundleUrl: null })).status).toBe(404); }); });