// MUST be first: sets DATABASE_URL before db_client is evaluated. import "./setup_env"; // 同 meego_workitem_missing.test.ts:删掉插件凭据,测试进程拿不到真 token、敲不到生产 Meego。 delete process.env.MEEGO_PLUGIN_ID; delete process.env.MEEGO_PLUGIN_SECRET; process.env.MEEGO_TOKEN = "test-static-token"; delete process.env.MEEGO_AI_STAMP; import { test, expect, describe, afterEach } from "bun:test"; const { aiFixFieldValues, aiFixStampForTask, stampMeegoAiFix, setMeegoIssueResolved, AI_FIELD_KEYS: K } = await import("../src/meego"); /** * Streamline 修过的单要在 Meego 上筛得出来:「是否尝试用AI修复」「是否AI修复」等字段。 * 改之前,已解决的单上「是否尝试用AI修复」读出来是「否」。 */ const URL_TT = "https://meego.larkoffice.com/tiktok/issue/detail/7328764565"; const realFetch = globalThis.fetch; afterEach(() => { globalThis.fetch = realFetch; }); const detailWith = (fields: Array<{ field_key: string; field_value: unknown }>, state = "OPEN") => ({ id: 7328764565, work_item_status: { state_key: state }, fields: [{ field_key: "issue_operator", field_value: ["owner-key"] }, ...fields], }); describe("aiFixFieldValues", () => { const stamp = aiFixStampForTask( { id: "t-1", attempts: 3, durationMs: 100, totalDurationMs: 47 * 60_000 + 20_000, mrUrl: "https://bits/mr/1" }, true, ); test("空白单:全部写上", () => { const v = aiFixFieldValues(stamp, detailWith([]) as any); expect(v[K.tried]).toEqual({ value: "u8gmqaqq5" }); expect(v[K.fixed]).toBe(true); expect(v[K.tools]).toEqual([{ value: "8hkn2a40_" }]); // 空着 → 未使用 expect(v[K.mr]).toBe("https://bits/mr/1"); expect(v[K.rounds]).toBe(3); expect(v[K.minutes]).toBe(47); // 用累计耗时,不是最后一批 expect(String(v[K.rcaReport])).toContain("?task_id=t-1"); }); test("「否」改成「是」;已经对的字段不重写", () => { const v = aiFixFieldValues(stamp, detailWith([ { field_key: K.tried, field_value: { label: "否", value: "nrxnziv78" } }, { field_key: K.fixed, field_value: true }, { field_key: K.rounds, field_value: 3 }, ]) as any); expect(v[K.tried]).toEqual({ value: "u8gmqaqq5" }); expect(K.fixed in v).toBe(false); expect(K.rounds in v).toBe(false); }); test("使用AI工具:人选过的不动", () => { const v = aiFixFieldValues(stamp, detailWith([ { field_key: K.tools, field_value: [{ value: "c2_kgz661", label: "Trae" }] }, ]) as any); expect(K.tools in v).toBe(false); }); test("AI-RCA Report 已是别的系统的链接就不覆盖", () => { const v = aiFixFieldValues(stamp, detailWith([{ field_key: K.rcaReport, field_value: "https://rca.example/1" }]) as any); expect(K.rcaReport in v).toBe(false); }); test("没结论时不写「是否AI修复」;判为无法复现写 false", () => { expect(K.fixed in aiFixFieldValues(aiFixStampForTask({ id: "x" }), null)).toBe(false); expect(aiFixFieldValues(aiFixStampForTask({ id: "x" }, false), null)[K.fixed]).toBe(false); }); }); /** 记下所有请求;按 URL 回 query / PUT / 其它。 */ function stubMeego(detail: unknown, onPut: (body: any) => { ok: boolean; msg?: string }) { const calls: Array<{ method: string; url: string; body: any }> = []; globalThis.fetch = (async (input: any, init?: any) => { const url = String(typeof input === "string" ? input : input?.url ?? input); const method = init?.method ?? "GET"; const body = init?.body ? JSON.parse(init.body) : undefined; calls.push({ method, url, body }); const json = (o: unknown) => new Response(JSON.stringify(o), { status: 200, headers: { "content-type": "application/json" } }); if (url.endsWith("/query")) return json({ err_code: 0, data: [detail] }); if (method === "PUT") { const r = onPut(body); return json(r.ok ? { err_code: 0, data: {} } : { err_code: 20006, err: { msg: r.msg } }); } throw new Error(`unexpected ${method} ${url}`); }) as typeof fetch; return calls; } describe("stampMeegoAiFix", () => { test("以经办人身份 PUT;某字段被报 illegal 就去掉它重试", async () => { let n = 0; const calls = stubMeego(detailWith([]), () => (++n === 2 ? { ok: false, msg: `field [${K.mr}] is illegal` } : { ok: true })); const r = await stampMeegoAiFix("7328764565", URL_TT, aiFixStampForTask({ id: "t-1", attempts: 1, durationMs: 60_000, mrUrl: "https://bits/mr/1" })); expect(r.ok).toBe(true); expect(r.written).not.toContain(K.mr); expect(r.written).toContain(K.tried); const puts = calls.filter((c) => c.method === "PUT"); expect(puts[0].url).toEndWith("/open_api/tiktok/work_item/issue/7328764565"); // 第一步只写「是否尝试用AI修复」:其余字段联动于它,同一个 PUT 里会被 Meego 静默丢掉 expect(puts[0].body.update_fields.map((f: any) => f.field_key)).toEqual([K.tried]); // 第二步被报 illegal(MR)→ 去掉重试 expect(puts).toHaveLength(3); expect(puts[1].body.update_fields.some((f: any) => f.field_key === K.mr)).toBe(true); expect(puts[2].body.update_fields.some((f: any) => f.field_key === K.mr)).toBe(false); expect(puts[2].body.update_fields.find((f: any) => f.field_key === K.tools)?.field_value).toEqual([{ value: "8hkn2a40_" }]); }); test("别的项目不写(field_key 只属于 tiktok/issue)", async () => { const calls = stubMeego(detailWith([]), () => ({ ok: true })); const r = await stampMeegoAiFix("1", "https://meego.larkoffice.com/lemon8/issue/detail/1", aiFixStampForTask({ id: "t" })); expect(r.skipped).toBe(true); expect(calls).toHaveLength(0); }); test("已经是终态的单:跳过流转,字段照写", async () => { const calls = stubMeego(detailWith([], "CLOSED"), () => ({ ok: true })); const r = await setMeegoIssueResolved("7328764565", URL_TT, { ai: aiFixStampForTask({ id: "t-1" }, true) }); expect(r.ok).toBe(true); expect(r.skipped).toBe(true); expect(r.aiStamp?.ok).toBe(true); expect(r.aiStamp?.written).toContain(K.fixed); expect(calls.some((c) => c.url.includes("state_change"))).toBe(false); }); });