|
| 1 | +import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import type { SignalEventHandlerDeps } from "./monitor/event-handler.types.js"; |
| 4 | + |
| 5 | +const signalRpcRequestMock = vi.hoisted(() => vi.fn()); |
| 6 | +const saveMediaBufferMock = vi.hoisted(() => vi.fn()); |
| 7 | + |
| 8 | +let capturedFetchAttachment: SignalEventHandlerDeps["fetchAttachment"] | undefined; |
| 9 | + |
| 10 | +vi.mock("openclaw/plugin-sdk/media-runtime", async () => { |
| 11 | + const actual = await vi.importActual<typeof import("openclaw/plugin-sdk/media-runtime")>( |
| 12 | + "openclaw/plugin-sdk/media-runtime", |
| 13 | + ); |
| 14 | + return { |
| 15 | + ...actual, |
| 16 | + saveMediaBuffer: saveMediaBufferMock, |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +vi.mock("./client-adapter.js", async () => { |
| 21 | + const actual = await vi.importActual<typeof import("./client-adapter.js")>("./client-adapter.js"); |
| 22 | + return { |
| 23 | + ...actual, |
| 24 | + signalRpcRequest: signalRpcRequestMock, |
| 25 | + }; |
| 26 | +}); |
| 27 | + |
| 28 | +vi.mock("./monitor/event-handler.js", () => ({ |
| 29 | + createSignalEventHandler: (deps: SignalEventHandlerDeps) => { |
| 30 | + capturedFetchAttachment = deps.fetchAttachment; |
| 31 | + return async () => {}; |
| 32 | + }, |
| 33 | +})); |
| 34 | + |
| 35 | +vi.mock("./signal-ingress.js", () => ({ |
| 36 | + startSignalIngressMonitor: async () => ({ |
| 37 | + receive: async () => {}, |
| 38 | + stop: async () => {}, |
| 39 | + }), |
| 40 | +})); |
| 41 | + |
| 42 | +vi.mock("./sse-reconnect.js", () => ({ |
| 43 | + runSignalSseLoop: async () => {}, |
| 44 | +})); |
| 45 | + |
| 46 | +const { monitorSignalProvider } = await import("./monitor.js"); |
| 47 | + |
| 48 | +const config = { |
| 49 | + channels: { |
| 50 | + signal: { |
| 51 | + transport: { kind: "external-native", url: "http://127.0.0.1:8080" }, |
| 52 | + dmPolicy: "open", |
| 53 | + allowFrom: ["*"], |
| 54 | + }, |
| 55 | + }, |
| 56 | +} satisfies OpenClawConfig; |
| 57 | + |
| 58 | +function requireCapturedFetchAttachment(): SignalEventHandlerDeps["fetchAttachment"] { |
| 59 | + if (!capturedFetchAttachment) { |
| 60 | + throw new Error("expected monitor to configure fetchAttachment"); |
| 61 | + } |
| 62 | + return capturedFetchAttachment; |
| 63 | +} |
| 64 | + |
| 65 | +describe("Signal attachment fetch", () => { |
| 66 | + beforeEach(() => { |
| 67 | + capturedFetchAttachment = undefined; |
| 68 | + signalRpcRequestMock.mockReset(); |
| 69 | + saveMediaBufferMock.mockReset().mockResolvedValue({ |
| 70 | + path: "/tmp/signal-attachment.png", |
| 71 | + contentType: "image/png", |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it("rejects malformed base64 attachment data", async () => { |
| 76 | + signalRpcRequestMock.mockResolvedValue({ |
| 77 | + data: "iVBORw0KGgoAAAANSUhE%%%UgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQIW2Nk+M/wHwAF/gL+M6Q10QAAAABJRU5ErkJggg==", |
| 78 | + }); |
| 79 | + |
| 80 | + await monitorSignalProvider({ config, autoStart: false }); |
| 81 | + const fetchAttachment = requireCapturedFetchAttachment(); |
| 82 | + |
| 83 | + await expect( |
| 84 | + fetchAttachment({ |
| 85 | + baseUrl: "http://127.0.0.1:8080", |
| 86 | + attachment: { id: "attachment-123", contentType: "image/png" }, |
| 87 | + sender: "+15550001111", |
| 88 | + maxBytes: 8 * 1024 * 1024, |
| 89 | + }), |
| 90 | + ).rejects.toThrow("Signal attachment attachment-123 returned malformed base64 data"); |
| 91 | + expect(saveMediaBufferMock).not.toHaveBeenCalled(); |
| 92 | + }); |
| 93 | +}); |
0 commit comments