import { describe, expect, it } from "bun:test"; import { preflightNodeAppRequirements, renderAppRequirementAlerts, type NodeAppRequirementAlert, } from "./shared.js"; const BACKLOG_URL = "https://wx2.ekuaibao.com/api/openapi/v1/backlog/data/[@{choose('{\"key\":\"webhook_1\"}')}]"; const noCorpRuntime = { selectedCorp: undefined } as any; function alert(overrides: Partial = {}): NodeAppRequirementAlert { return { taskName: "action_pyrg", taskLabel: "提交审批", apiName: "单据审批(执行单据动作:同意 / 驳回 / 加签)", appName: "单据审批", powerCode: "TICKET_AUDIT_switch", productId: "o0hMBw000000g0", staticPath: "/api/openapi/v1/backlog/data/[", verified: true, unlicensed: true, message: "节点 [提交审批] (action_pyrg) 调用单据审批,但当前企业尚未开通该应用。", ...overrides, }; } describe("保存期应用权限预检", () => { it("非 HTTP 节点不触发任何检查", async () => { const result = await preflightNodeAppRequirements(noCorpRuntime, [ { name: "action_code", type: "code-js2", code: `return "${BACKLOG_URL}";` }, ]); expect(result.alerts).toEqual([]); expect(result.verifyError).toBeUndefined(); }); it("URL 无法静态判断(上游节点拼接)时不产生告警", async () => { const result = await preflightNodeAppRequirements(noCorpRuntime, [ { name: "action_dyn", type: "http", method: "POST", url: "@{choose('{\"key\":\"action_prev.url\"}')}" }, ]); expect(result.alerts).toEqual([]); }); it("命中接口但读不到企业清单 → verified=false,且不阻断", async () => { const result = await preflightNodeAppRequirements(noCorpRuntime, [ { name: "action_pyrg", type: "http", method: "POST", url: BACKLOG_URL }, ]); expect(result.alerts).toHaveLength(1); const item = result.alerts[0]!; expect(item.verified).toBe(false); expect(item.unlicensed).toBe(false); expect(item.powerCode).toBe("TICKET_AUDIT_switch"); expect(item.productId).toBe("o0hMBw000000g0"); expect(item.staticPath).toBe("/api/openapi/v1/backlog/data/["); expect(item.message).toContain("未能读取企业应用清单"); expect(result.verifyError).toContain("未选择企业"); }); it("非 http 与 http 混排时只检查 http 节点", async () => { const result = await preflightNodeAppRequirements(noCorpRuntime, [ { name: "trigger", type: "webhook", url: BACKLOG_URL }, { name: "action_pyrg", type: "http", method: "POST", url: BACKLOG_URL }, null, ]); expect(result.alerts.map((item) => item.taskName)).toEqual(["action_pyrg"]); }); }); describe("预检结果渲染", () => { it("无告警时输出空串,不污染正常输出", () => { expect(renderAppRequirementAlerts([])).toBe(""); }); it("未开通 → ⛔ 段并带判定依据路径", () => { const text = renderAppRequirementAlerts([alert()]); expect(text).toContain("⛔ 应用权限预检未通过"); expect(text).toContain("action_pyrg"); expect(text).toContain("判定依据: URL 静态路径 '/api/openapi/v1/backlog/data/['"); expect(text).not.toContain("⚠️"); }); it("未核对 → ⚠️ 段,不出现 ⛔", () => { const text = renderAppRequirementAlerts([ alert({ verified: false, unlicensed: false, message: "节点 [提交审批] 未能读取企业应用清单" }), ]); expect(text).toContain("⚠️ 应用权限未能核对"); expect(text).not.toContain("⛔"); }); });