// MUST be first: sets DATABASE_URL before db_client is evaluated. import "./setup_env"; // 同 meego_workitem_missing.test.ts:删掉 PLUGIN_ID/SECRET,测试进程拿不到真凭据, // 也就不可能敲到生产 Meego;静态 token 让 downloadMeegoFile 走到发请求那一步。 delete process.env.MEEGO_PLUGIN_ID; delete process.env.MEEGO_PLUGIN_SECRET; process.env.MEEGO_TOKEN = "test-static-token"; import { test, expect, describe, afterEach } from "bun:test"; const { downloadMeegoFile, meegoErrorEnvelope } = await import("../src/meego"); /** * 附件下载:`application/json` 不等于错误。 * * 生产上 EasyFeedback 工单挂的 dom-tree.json / aweme-model.json 回来就是 * 200 + application/json,旧代码按 content-type 一律当错误信封,于是这两个附件 * 每次都被静默丢掉(代理回 502 `{"error":"HTTP 200"}`)。 */ const MEEGO_URL = "https://meego.larkoffice.com/tiktok/issue/detail/7386666710"; const realFetch = globalThis.fetch; afterEach(() => { globalThis.fetch = realFetch; }); function stubDownload(body: string, status: number, contentType: string) { globalThis.fetch = (async (input: any) => { const url = String(typeof input === "string" ? input : input?.url ?? input); if (url.endsWith("/file/download")) { return new Response(body, { status, headers: { "content-type": contentType } }); } throw new Error(`意外的出网请求: ${url}`); }) as typeof fetch; } describe("downloadMeegoFile · JSON 附件 vs 错误信封", () => { test("附件本身是 JSON → 原样返回字节", async () => { const file = JSON.stringify({ root: { class: "UIWindow", children: [] } }); stubDownload(file, 200, "application/json"); const r = await downloadMeegoFile("7386666710", "uuid-dom-tree", MEEGO_URL); expect(r.ok).toBe(true); expect(new TextDecoder().decode(r.bytes!)).toBe(file); }); test("200 + 错误信封 → 失败,带上 err_msg", async () => { stubDownload(JSON.stringify({ err_code: 30019, err_msg: "uuid not found", err: {} }), 200, "application/json"); const r = await downloadMeegoFile("7386666710", "uuid-x", MEEGO_URL); expect(r.ok).toBe(false); expect(r.error).toBe("uuid not found"); }); test("非 2xx → 失败", async () => { stubDownload(JSON.stringify({ err_code: 10301, err_msg: "Check Token Perm Failed" }), 403, "application/json"); const r = await downloadMeegoFile("7386666710", "uuid-x", MEEGO_URL); expect(r.ok).toBe(false); expect(r.error).toBe("Check Token Perm Failed"); }); test("二进制附件不受影响", async () => { stubDownload("PK\u0003\u0004zip-bytes", 200, "application/zip"); const r = await downloadMeegoFile("7386666710", "uuid-zip", MEEGO_URL); expect(r.ok).toBe(true); expect(r.contentType).toBe("application/zip"); }); }); describe("meegoErrorEnvelope", () => { const enc = (v: unknown) => new TextEncoder().encode(typeof v === "string" ? v : JSON.stringify(v)); test.each([ [{ err_code: 30019, err_msg: "uuid not found" }, "uuid not found"], [{ err_code: 1, err: { msg: "boom" } }, "boom"], [{ err_code: 0, err_msg: "" }, "err_code 0"], ])("信封 %j → %s", (body, msg) => { expect(meegoErrorEnvelope(enc(body))).toBe(msg); }); test.each([ [{ err_code: 5 }], // 只有 err_code,没有 err_msg / err:像业务数据 [[{ err_code: 1, err_msg: "x" }]], [{ root: {} }], ["not json"], ])("不是信封 %j", (body) => { expect(meegoErrorEnvelope(enc(body))).toBeNull(); }); });