|
| 1 | +import { afterAll, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { feishuPlugin } from "../channel-plugin-api.js"; |
| 3 | + |
| 4 | +const renderPresentation = vi.hoisted(() => vi.fn()); |
| 5 | +const sendPayload = vi.hoisted(() => vi.fn()); |
| 6 | + |
| 7 | +vi.mock("./channel.runtime.js", () => ({ |
| 8 | + feishuChannelRuntime: { feishuOutbound: { renderPresentation, sendPayload } }, |
| 9 | +})); |
| 10 | + |
| 11 | +afterAll(() => { |
| 12 | + vi.doUnmock("./channel.runtime.js"); |
| 13 | + vi.resetModules(); |
| 14 | +}); |
| 15 | + |
| 16 | +describe("Feishu public outbound presentation hooks", () => { |
| 17 | + beforeEach(() => { |
| 18 | + vi.resetAllMocks(); |
| 19 | + }); |
| 20 | + |
| 21 | + const presentation = { title: "Status", blocks: [{ type: "text" as const, text: "Ready" }] }; |
| 22 | + const payload = { presentation }; |
| 23 | + const ctx = { |
| 24 | + cfg: {}, |
| 25 | + to: "chat:oc_group", |
| 26 | + accountId: "work", |
| 27 | + threadId: "om_parent", |
| 28 | + text: "", |
| 29 | + payload, |
| 30 | + }; |
| 31 | + |
| 32 | + it("renders and sends native payloads through the advertised plugin capability", async () => { |
| 33 | + const rendered = { channelData: { feishu: { card: { schema: "2.0" } } } }; |
| 34 | + const receipt = { channel: "feishu", messageId: "om_card" }; |
| 35 | + renderPresentation.mockResolvedValueOnce(rendered); |
| 36 | + sendPayload.mockResolvedValueOnce(receipt); |
| 37 | + |
| 38 | + expect(feishuPlugin.outbound?.presentationCapabilities?.supported).toBe(true); |
| 39 | + const renderContext = { ctx, payload, presentation }; |
| 40 | + const result = await feishuPlugin.outbound?.renderPresentation?.(renderContext); |
| 41 | + expect(renderPresentation).toHaveBeenCalledExactlyOnceWith(renderContext); |
| 42 | + expect(result).toBe(rendered); |
| 43 | + |
| 44 | + const sendContext = { ...ctx, payload: rendered }; |
| 45 | + await expect(feishuPlugin.outbound?.sendPayload?.(sendContext)).resolves.toBe(receipt); |
| 46 | + expect(sendPayload).toHaveBeenCalledExactlyOnceWith(sendContext); |
| 47 | + }); |
| 48 | + |
| 49 | + it("preserves native renderer and sender failures", async () => { |
| 50 | + const renderError = new Error("card rendering failed"); |
| 51 | + renderPresentation.mockRejectedValueOnce(renderError); |
| 52 | + await expect( |
| 53 | + feishuPlugin.outbound?.renderPresentation?.({ ctx, payload, presentation }), |
| 54 | + ).rejects.toBe(renderError); |
| 55 | + |
| 56 | + const sendError = new Error("card delivery failed"); |
| 57 | + sendPayload.mockRejectedValueOnce(sendError); |
| 58 | + await expect(feishuPlugin.outbound?.sendPayload?.(ctx)).rejects.toBe(sendError); |
| 59 | + }); |
| 60 | +}); |
0 commit comments