import { afterAll, beforeEach, describe, expect, it } from "vitest" ;
import { createRuntimeEnv } from "../../../test/helpers/plugins/runtime-env.js" ;
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;
});
});
Messung V0.5 in Prozent C=93 H=90 G=91
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-05)
¤
*© Formatika GbR, Deutschland