import { afterEach, describe, expect, it, spyOn } from "bun:test"; import ky from "ky"; import { grantWorkflowNodesApi, SCRIPT_ENGINE_TEST_BASE_URL } from "./catalog/api.js"; describe("grantWorkflowNodesApi direct Piper request", () => { let spies: ReturnType[] = []; afterEach(() => { for (const spy of spies) spy.mockRestore(); spies = []; }); function mockJwt(): void { spies.push(spyOn(ky, "get").mockImplementation((() => ({ json: async () => ({ id: "business-jwt" }), })) as never)); } it("exchanges EASO JWT and posts the requested node types to Piper", async () => { mockJwt(); let requestedUrl = ""; let requestOptions: any; spies.push(spyOn(ky, "post").mockImplementation((async (url: string, options: any) => { requestedUrl = url; requestOptions = options; return { json: async () => ({ code: 200, msg: "新开通的节点:recalculate-the-approval-flow" }), }; }) as never)); const result = await grantWorkflowNodesApi({ token: "access-token", corpId: "corp-1", staffId: "corp-1:staff-1", nodes: ["recalculate-the-approval-flow"], baseUrl: "https://app.ekuaibao.com///", }); expect(result).toEqual({ success: true, saved: ["recalculate-the-approval-flow"], skipped: [], rawMessage: "新开通的节点:recalculate-the-approval-flow", isTest: false, }); expect(ky.get).toHaveBeenCalledWith( "https://app.ekuaibao.com/api/engine/realm/$EASO/actions/jwt", expect.objectContaining({ searchParams: { corpId: "corp-1" }, headers: expect.objectContaining({ accesstoken: "access-token", corpid: "corp-1", staffid: "corp-1:staff-1", }), }), ); expect(requestedUrl).toBe("https://piper.ekuaibao.com/api/easo/dsl/corporation"); expect(requestOptions.headers).toEqual({ Authorization: "Bearer business-jwt" }); expect(requestOptions.json).toEqual({ corporationId: "corp-1", dslTypes: ["recalculate-the-approval-flow"], "recalculate-the-approval-flow": true, "ui-notification": true, start: 0, }); }); it("reports already-open nodes as skipped", async () => { mockJwt(); spies.push(spyOn(ky, "post").mockImplementation((async () => ({ json: async () => ({ code: 200, msg: "以下节点已开通,不再重复开通:code" }), })) as never)); const result = await grantWorkflowNodesApi({ token: "access-token", corpId: "corp-1", staffId: "corp-1:staff-1", nodes: ["code"], }); expect(result.success).toBe(true); expect(result.saved).toEqual([]); expect(result.skipped).toEqual(["code"]); expect(result.isTest).toBe(false); }); it("uses the test Piper endpoint only when isTest is explicitly enabled", async () => { mockJwt(); let requestedUrl = ""; spies.push(spyOn(ky, "post").mockImplementation((async (url: string) => { requestedUrl = url; return { json: async () => ({ code: 200, msg: "新开通的节点:code" }) }; }) as never)); const result = await grantWorkflowNodesApi({ token: "access-token", corpId: "corp-1", staffId: "corp-1:staff-1", nodes: ["code"], isTest: true, }); expect(requestedUrl).toBe(`${SCRIPT_ENGINE_TEST_BASE_URL}/api/easo/dsl/corporation`); expect(result.isTest).toBe(true); }); it("does not treat an HTTP 200 with a failing business code as success", async () => { mockJwt(); spies.push(spyOn(ky, "post").mockImplementation((async () => ({ json: async () => ({ code: 500, msg: "JWT 签名无效" }), })) as never)); const result = await grantWorkflowNodesApi({ token: "access-token", corpId: "corp-1", staffId: "corp-1:staff-1", nodes: ["code"], }); expect(result).toEqual({ success: false, saved: [], skipped: [], rawMessage: "JWT 签名无效", isTest: false }); }); it("maps EASO exchange transport failures to a domain API error", async () => { spies.push(spyOn(ky, "get").mockImplementation((() => { throw new Error("Request failed with status code 404"); }) as never)); await expect(grantWorkflowNodesApi({ token: "access-token", corpId: "corp-1", staffId: "corp-1:staff-1", nodes: ["code"], })).rejects.toThrow("灰度节点开通 EASO 换票失败: Request failed with status code 404"); }); it("surfaces Piper transport failures without exposing credentials", async () => { mockJwt(); spies.push(spyOn(ky, "post").mockImplementation((async () => { throw new Error("connect ETIMEDOUT"); }) as never)); await expect(grantWorkflowNodesApi({ token: "secret-access-token", corpId: "corp-1", staffId: "corp-1:staff-1", nodes: ["code"], })).rejects.toThrow("灰度节点开通 Piper 请求失败: connect ETIMEDOUT"); }); });