Files
openclaw/extensions/zalo/src/monitor.image.polling.test.ts
2026-04-28 03:52:19 +01:00

111 lines
3.2 KiB
TypeScript

import { createRuntimeEnv } from "openclaw/plugin-sdk/plugin-test-runtime";
import { afterAll, beforeEach, describe, expect, it } from "vitest";
import {
createImageLifecycleCore,
createImageUpdate,
createLifecycleMonitorSetup,
expectImageLifecycleDelivery,
settleAsyncWork,
} from "./test-support/lifecycle-test-support.js";
import {
getUpdatesMock,
getZaloRuntimeMock,
loadCachedLifecycleMonitorModule,
resetLifecycleTestState,
sendMessageMock,
} from "./test-support/monitor-mocks-test-support.js";
describe("Zalo polling image handling", () => {
const {
core,
finalizeInboundContextMock,
recordInboundSessionMock,
fetchRemoteMediaMock,
saveMediaBufferMock,
} = createImageLifecycleCore();
beforeEach(async () => {
await resetLifecycleTestState();
getZaloRuntimeMock.mockReturnValue(core);
});
afterAll(async () => {
await resetLifecycleTestState();
});
it("downloads inbound image media from photo_url and preserves display_name", async () => {
getUpdatesMock
.mockResolvedValueOnce({
ok: true,
result: createImageUpdate({ date: 1774084566880 }),
})
.mockImplementation(() => new Promise(() => {}));
const { monitorZaloProvider } = await loadCachedLifecycleMonitorModule("zalo-image-polling");
const abort = new AbortController();
const runtime = createRuntimeEnv();
const { account, config } = createLifecycleMonitorSetup({
accountId: "default",
dmPolicy: "open",
});
const run = monitorZaloProvider({
token: "zalo-token", // pragma: allowlist secret
account,
config,
runtime,
abortSignal: abort.signal,
});
await settleAsyncWork();
expect(fetchRemoteMediaMock).toHaveBeenCalledTimes(1);
expectImageLifecycleDelivery({
fetchRemoteMediaMock,
saveMediaBufferMock,
finalizeInboundContextMock,
recordInboundSessionMock,
});
abort.abort();
await run;
});
it("rejects unauthorized DM images before downloading media", async () => {
getUpdatesMock
.mockResolvedValueOnce({
ok: true,
result: createImageUpdate({
messageId: "msg-unauthorized-1",
userId: "user-unauthorized-1",
chatId: "chat-unauthorized-1",
}),
})
.mockImplementation(() => new Promise(() => {}));
const { monitorZaloProvider } = await loadCachedLifecycleMonitorModule("zalo-image-polling");
const abort = new AbortController();
const runtime = createRuntimeEnv();
const { account, config } = createLifecycleMonitorSetup({
accountId: "default",
dmPolicy: "pairing",
allowFrom: ["allowed-user"],
});
const run = monitorZaloProvider({
token: "zalo-token", // pragma: allowlist secret
account,
config,
runtime,
abortSignal: abort.signal,
});
await settleAsyncWork();
expect(sendMessageMock).toHaveBeenCalledTimes(1);
expect(fetchRemoteMediaMock).not.toHaveBeenCalled();
expect(saveMediaBufferMock).not.toHaveBeenCalled();
expect(finalizeInboundContextMock).not.toHaveBeenCalled();
expect(recordInboundSessionMock).not.toHaveBeenCalled();
abort.abort();
await run;
});
});