@openclaw/whatsapp 2026.5.2 → 2026.5.3-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/access-control-BQ3tCkH8.js +288 -0
- package/dist/account-config-CNQ33oav.js +47 -0
- package/dist/account-ids-DG18xddX.js +5 -0
- package/dist/accounts-BDqgpFEB.js +116 -0
- package/dist/action-runtime-DTGNgG-i.js +74 -0
- package/dist/action-runtime-api.js +2 -0
- package/dist/action-runtime.runtime.js +2 -0
- package/dist/active-listener-N7GFKsuN.js +11 -0
- package/dist/agent-tools-login-DpRxLNRB.js +89 -0
- package/dist/api.js +148 -0
- package/dist/audio-preflight.runtime-Bcdsl_ri.js +7 -0
- package/dist/auth-presence.js +32 -0
- package/dist/auth-store-BjCb50Ry.js +381 -0
- package/dist/channel-DQ-5Xoj9.js +542 -0
- package/dist/channel-config-api.js +2 -0
- package/dist/channel-plugin-api.js +2 -0
- package/dist/channel-react-action-Df20qAy9.js +42 -0
- package/dist/channel.runtime-DKXRnA8x.js +55 -0
- package/dist/channel.setup-DOHR2BGp.js +20 -0
- package/dist/command-policy-O8sRnqUV.js +8 -0
- package/dist/config-schema-Dmo66Ubb.js +27 -0
- package/dist/connection-controller-BiHAsTM8.js +596 -0
- package/dist/connection-controller-registry-D_iIGz5m.js +22 -0
- package/dist/constants-DLGCIbfl.js +4 -0
- package/dist/constants.js +2 -0
- package/dist/contract-api.js +19 -0
- package/dist/creds-files-_2V8jzQa.js +19 -0
- package/dist/directory-config-DVs4ffXu.js +37 -0
- package/dist/directory-contract-api.js +2 -0
- package/dist/doctor-A9tXm9Om.js +48 -0
- package/dist/doctor-contract-api.js +10 -0
- package/dist/group-session-contract-B8QBn0I2.js +15 -0
- package/dist/identity-QHAlV7Fu.js +75 -0
- package/dist/index.js +18 -0
- package/dist/legacy-session-surface-api.js +8 -0
- package/dist/legacy-state-migrations-api.js +2 -0
- package/dist/light-runtime-api.js +5 -0
- package/dist/login-DymdrKca.js +52 -0
- package/dist/login-qr-DIS0Iv-7.js +378 -0
- package/dist/login-qr-api.js +2 -0
- package/dist/login-qr-runtime.js +16 -0
- package/dist/monitor-C4Fhg_lD.js +3349 -0
- package/dist/normalize-CWYiGkXw.js +2 -0
- package/dist/normalize-target-BPVHNsM5.js +74 -0
- package/dist/outbound-adapter-BssVnZdz.js +29 -0
- package/dist/outbound-base-Bq1UYEK5.js +119 -0
- package/dist/outbound-media-contract-CGss_QHy.js +163 -0
- package/dist/outbound-payload-test-api.js +2 -0
- package/dist/quoted-message-TENQzyxV.js +108 -0
- package/dist/reaction-level-CRVEVAfi.js +16 -0
- package/dist/reply-resolver.runtime-BRCgoO4C.js +2 -0
- package/dist/resolve-outbound-target-CN-Wo6IY.js +39 -0
- package/dist/rolldown-runtime-RkAeH_Qm.js +27 -0
- package/dist/runtime-6fZ67abR.js +8 -0
- package/dist/runtime-api.js +15 -0
- package/dist/runtime-group-policy-Gp1lQ0at.js +11 -0
- package/dist/secret-contract-api.js +5 -0
- package/dist/security-contract-DqH8JDX6.js +26 -0
- package/dist/security-contract-api.js +2 -0
- package/dist/send-CIyLFxdZ.js +234 -0
- package/dist/session-contract-B0efQf9P.js +27 -0
- package/dist/session-errors-CSuKZPub.js +63 -0
- package/dist/session.runtime-C1eSE_KK.js +13 -0
- package/dist/setup-core-DZKvpzbH.js +46 -0
- package/dist/setup-entry.js +23 -0
- package/dist/setup-plugin-api.js +2 -0
- package/dist/setup-surface-ieFnIW4g.js +344 -0
- package/dist/shared-D_wzSVar.js +307 -0
- package/dist/state-migrations-DnCdvoYD.js +39 -0
- package/dist/targets.js +2 -0
- package/dist/test-api.js +3 -0
- package/dist/text-runtime-C1zNiZeO.js +116 -0
- package/package.json +14 -6
- package/action-runtime-api.ts +0 -1
- package/action-runtime.runtime.ts +0 -1
- package/api.ts +0 -213
- package/auth-presence.ts +0 -80
- package/channel-config-api.ts +0 -1
- package/channel-plugin-api.ts +0 -3
- package/config-api.ts +0 -4
- package/constants.ts +0 -1
- package/contract-api.ts +0 -29
- package/directory-contract-api.ts +0 -4
- package/doctor-contract-api.ts +0 -8
- package/index.test.ts +0 -13
- package/index.ts +0 -16
- package/legacy-session-surface-api.ts +0 -6
- package/legacy-state-migrations-api.ts +0 -1
- package/light-runtime-api.ts +0 -12
- package/login-qr-api.ts +0 -1
- package/login-qr-runtime.ts +0 -23
- package/outbound-payload-test-api.ts +0 -1
- package/runtime-api.ts +0 -84
- package/secret-contract-api.ts +0 -4
- package/security-contract-api.ts +0 -4
- package/setup-entry.test.ts +0 -21
- package/setup-entry.ts +0 -21
- package/setup-plugin-api.ts +0 -3
- package/src/account-config.ts +0 -77
- package/src/account-ids.ts +0 -13
- package/src/account-types.ts +0 -5
- package/src/accounts.test.ts +0 -182
- package/src/accounts.ts +0 -176
- package/src/accounts.whatsapp-auth.test.ts +0 -59
- package/src/action-runtime-target-auth.ts +0 -27
- package/src/action-runtime.test.ts +0 -330
- package/src/action-runtime.ts +0 -76
- package/src/active-listener.test.ts +0 -65
- package/src/active-listener.ts +0 -17
- package/src/agent-tools-login.test.ts +0 -81
- package/src/agent-tools-login.ts +0 -113
- package/src/approval-auth.test.ts +0 -24
- package/src/approval-auth.ts +0 -27
- package/src/auth-store.runtime.ts +0 -1
- package/src/auth-store.test.ts +0 -311
- package/src/auth-store.ts +0 -502
- package/src/auto-reply/config.runtime.ts +0 -16
- package/src/auto-reply/constants.ts +0 -1
- package/src/auto-reply/deliver-reply.test.ts +0 -843
- package/src/auto-reply/deliver-reply.ts +0 -279
- package/src/auto-reply/loggers.ts +0 -6
- package/src/auto-reply/mentions.ts +0 -131
- package/src/auto-reply/monitor/ack-reaction.test.ts +0 -170
- package/src/auto-reply/monitor/ack-reaction.ts +0 -99
- package/src/auto-reply/monitor/audio-preflight.runtime.ts +0 -9
- package/src/auto-reply/monitor/broadcast.ts +0 -153
- package/src/auto-reply/monitor/commands.ts +0 -19
- package/src/auto-reply/monitor/echo.ts +0 -64
- package/src/auto-reply/monitor/group-activation.runtime.ts +0 -1
- package/src/auto-reply/monitor/group-activation.test.ts +0 -189
- package/src/auto-reply/monitor/group-activation.ts +0 -73
- package/src/auto-reply/monitor/group-gating.audio-preflight.test.ts +0 -103
- package/src/auto-reply/monitor/group-gating.runtime.ts +0 -8
- package/src/auto-reply/monitor/group-gating.ts +0 -217
- package/src/auto-reply/monitor/group-members.test.ts +0 -56
- package/src/auto-reply/monitor/group-members.ts +0 -65
- package/src/auto-reply/monitor/inbound-context.test.ts +0 -97
- package/src/auto-reply/monitor/inbound-context.ts +0 -92
- package/src/auto-reply/monitor/inbound-dispatch.runtime.ts +0 -21
- package/src/auto-reply/monitor/inbound-dispatch.test.ts +0 -895
- package/src/auto-reply/monitor/inbound-dispatch.ts +0 -422
- package/src/auto-reply/monitor/last-route.test.ts +0 -37
- package/src/auto-reply/monitor/last-route.ts +0 -61
- package/src/auto-reply/monitor/message-line.runtime.ts +0 -38
- package/src/auto-reply/monitor/message-line.ts +0 -54
- package/src/auto-reply/monitor/on-message.audio-preflight.test.ts +0 -354
- package/src/auto-reply/monitor/on-message.ts +0 -291
- package/src/auto-reply/monitor/peer.ts +0 -17
- package/src/auto-reply/monitor/process-message.audio-preflight.test.ts +0 -420
- package/src/auto-reply/monitor/process-message.test.ts +0 -355
- package/src/auto-reply/monitor/process-message.ts +0 -531
- package/src/auto-reply/monitor/runtime-api.ts +0 -30
- package/src/auto-reply/monitor-state.test.ts +0 -64
- package/src/auto-reply/monitor-state.ts +0 -106
- package/src/auto-reply/monitor.ts +0 -600
- package/src/auto-reply/reply-resolver.runtime.ts +0 -1
- package/src/auto-reply/types.ts +0 -48
- package/src/auto-reply/util.ts +0 -63
- package/src/auto-reply/web-auto-reply-monitor.test.ts +0 -687
- package/src/auto-reply/web-auto-reply-utils.test.ts +0 -392
- package/src/auto-reply.broadcast-groups.combined.test.ts +0 -246
- package/src/auto-reply.broadcast-groups.test-harness.ts +0 -47
- package/src/auto-reply.impl.ts +0 -6
- package/src/auto-reply.test-harness.ts +0 -421
- package/src/auto-reply.ts +0 -1
- package/src/auto-reply.web-auto-reply.compresses-common-formats-jpeg-cap.test.ts +0 -427
- package/src/auto-reply.web-auto-reply.connection-and-logging.e2e.test.ts +0 -908
- package/src/auto-reply.web-auto-reply.last-route.test.ts +0 -184
- package/src/channel-actions.runtime.ts +0 -7
- package/src/channel-actions.test.ts +0 -216
- package/src/channel-actions.ts +0 -84
- package/src/channel-outbound.test.ts +0 -131
- package/src/channel-outbound.ts +0 -36
- package/src/channel-react-action.runtime.ts +0 -7
- package/src/channel-react-action.test.ts +0 -294
- package/src/channel-react-action.ts +0 -84
- package/src/channel.runtime.ts +0 -117
- package/src/channel.setup.test.ts +0 -461
- package/src/channel.setup.ts +0 -28
- package/src/channel.ts +0 -339
- package/src/command-policy.ts +0 -7
- package/src/config-accessors.test.ts +0 -34
- package/src/config-accessors.ts +0 -22
- package/src/config-schema.test.ts +0 -121
- package/src/config-schema.ts +0 -6
- package/src/config-ui-hints.ts +0 -24
- package/src/connection-controller-registry.test.ts +0 -27
- package/src/connection-controller-registry.ts +0 -49
- package/src/connection-controller.test.ts +0 -282
- package/src/connection-controller.ts +0 -679
- package/src/creds-files.ts +0 -19
- package/src/creds-persistence.ts +0 -102
- package/src/directory-config.test.ts +0 -60
- package/src/directory-config.ts +0 -40
- package/src/directory-contract.test.ts +0 -56
- package/src/doctor-contract.ts +0 -11
- package/src/doctor.test.ts +0 -74
- package/src/doctor.ts +0 -56
- package/src/group-intro.ts +0 -15
- package/src/group-policy.test.ts +0 -36
- package/src/group-policy.ts +0 -40
- package/src/group-session-contract.ts +0 -20
- package/src/group-session-key.test.ts +0 -53
- package/src/group-session-key.ts +0 -41
- package/src/heartbeat.ts +0 -34
- package/src/identity.ts +0 -164
- package/src/inbound/access-control.test-harness.ts +0 -37
- package/src/inbound/access-control.test.ts +0 -375
- package/src/inbound/access-control.ts +0 -237
- package/src/inbound/dedupe.ts +0 -132
- package/src/inbound/extract.test.ts +0 -282
- package/src/inbound/extract.ts +0 -488
- package/src/inbound/lifecycle.ts +0 -39
- package/src/inbound/media.node.test.ts +0 -83
- package/src/inbound/media.ts +0 -102
- package/src/inbound/monitor.ts +0 -890
- package/src/inbound/runtime-api.ts +0 -7
- package/src/inbound/save-media.runtime.ts +0 -1
- package/src/inbound/send-api.test.ts +0 -319
- package/src/inbound/send-api.ts +0 -145
- package/src/inbound/send-result.ts +0 -61
- package/src/inbound/types.ts +0 -106
- package/src/inbound-context.contract.test.ts +0 -32
- package/src/inbound-policy.ts +0 -231
- package/src/inbound.media.test.ts +0 -320
- package/src/inbound.test.ts +0 -290
- package/src/inbound.ts +0 -9
- package/src/login-qr.test.ts +0 -483
- package/src/login-qr.ts +0 -542
- package/src/login.coverage.test.ts +0 -133
- package/src/login.test.ts +0 -89
- package/src/login.ts +0 -70
- package/src/logout.test.ts +0 -162
- package/src/media.test.ts +0 -480
- package/src/media.ts +0 -10
- package/src/monitor-inbox.allows-messages-from-senders-allowfrom-list.test-support.ts +0 -400
- package/src/monitor-inbox.append-upsert.test-support.ts +0 -133
- package/src/monitor-inbox.behavior.test.ts +0 -5
- package/src/monitor-inbox.blocks-messages-from-unauthorized-senders-not-allowfrom.test-support.ts +0 -362
- package/src/monitor-inbox.captures-media-path-image-messages.test-support.ts +0 -306
- package/src/monitor-inbox.streams-inbound-messages.test-support.ts +0 -729
- package/src/monitor-inbox.test-harness.ts +0 -304
- package/src/normalize-target.ts +0 -123
- package/src/normalize.ts +0 -7
- package/src/outbound-adapter.poll.test.ts +0 -62
- package/src/outbound-adapter.sendpayload.test.ts +0 -195
- package/src/outbound-adapter.ts +0 -36
- package/src/outbound-base.test.ts +0 -560
- package/src/outbound-base.ts +0 -244
- package/src/outbound-media-contract.ts +0 -278
- package/src/outbound-media.runtime.ts +0 -36
- package/src/outbound-payload.contract.test.ts +0 -61
- package/src/outbound-send-deps.ts +0 -1
- package/src/outbound-test-support.ts +0 -16
- package/src/pairing-security.test-harness.ts +0 -59
- package/src/qr-image.ts +0 -1
- package/src/qr-terminal.ts +0 -1
- package/src/quoted-message.test.ts +0 -83
- package/src/quoted-message.ts +0 -184
- package/src/reaction-level.test.ts +0 -94
- package/src/reaction-level.ts +0 -21
- package/src/reconnect.test.ts +0 -51
- package/src/reconnect.ts +0 -55
- package/src/resolve-outbound-target.test.ts +0 -288
- package/src/resolve-outbound-target.ts +0 -58
- package/src/resolve-target.test.ts +0 -120
- package/src/runtime-api.ts +0 -59
- package/src/runtime-group-policy.ts +0 -16
- package/src/runtime.ts +0 -9
- package/src/security-contract.ts +0 -47
- package/src/security-fix.ts +0 -71
- package/src/send.test.ts +0 -534
- package/src/send.ts +0 -292
- package/src/session-contract.test.ts +0 -31
- package/src/session-contract.ts +0 -43
- package/src/session-errors.ts +0 -125
- package/src/session-route.test.ts +0 -41
- package/src/session-route.ts +0 -32
- package/src/session.runtime.ts +0 -8
- package/src/session.test.ts +0 -559
- package/src/session.ts +0 -330
- package/src/setup-core.ts +0 -52
- package/src/setup-finalize.ts +0 -446
- package/src/setup-surface.test.ts +0 -367
- package/src/setup-surface.ts +0 -69
- package/src/setup-test-helpers.ts +0 -216
- package/src/shared.ts +0 -291
- package/src/socket-timing.test.ts +0 -49
- package/src/socket-timing.ts +0 -38
- package/src/state-migrations.ts +0 -54
- package/src/status-issues.test.ts +0 -137
- package/src/status-issues.ts +0 -185
- package/src/system-prompt.test.ts +0 -199
- package/src/system-prompt.ts +0 -31
- package/src/targets-runtime.ts +0 -180
- package/src/test-helpers.ts +0 -690
- package/src/text-runtime.test.ts +0 -162
- package/src/text-runtime.ts +0 -11
- package/src/vcard.ts +0 -84
- package/targets.ts +0 -5
- package/test-api.ts +0 -2
- package/tsconfig.json +0 -16
|
@@ -1,427 +0,0 @@
|
|
|
1
|
-
import crypto from "node:crypto";
|
|
2
|
-
import sharp from "sharp";
|
|
3
|
-
import { beforeAll, describe, expect, it, vi } from "vitest";
|
|
4
|
-
import {
|
|
5
|
-
createMockWebListener,
|
|
6
|
-
createAcceptedWhatsAppSendResult,
|
|
7
|
-
installWebAutoReplyTestHomeHooks,
|
|
8
|
-
installWebAutoReplyUnitTestHooks,
|
|
9
|
-
resetLoadConfigMock,
|
|
10
|
-
setLoadConfigMock,
|
|
11
|
-
} from "./auto-reply.test-harness.js";
|
|
12
|
-
import type { WebInboundMessage } from "./inbound.js";
|
|
13
|
-
|
|
14
|
-
installWebAutoReplyTestHomeHooks();
|
|
15
|
-
|
|
16
|
-
let monitorWebChannel: typeof import("./auto-reply/monitor.js").monitorWebChannel;
|
|
17
|
-
|
|
18
|
-
describe("web auto-reply", () => {
|
|
19
|
-
installWebAutoReplyUnitTestHooks({ pinDns: true });
|
|
20
|
-
type ListenerFactory = NonNullable<Parameters<typeof monitorWebChannel>[1]>;
|
|
21
|
-
const SMALL_MEDIA_CAP_MB = 0.1;
|
|
22
|
-
const SMALL_MEDIA_CAP_BYTES = Math.floor(SMALL_MEDIA_CAP_MB * 1024 * 1024);
|
|
23
|
-
|
|
24
|
-
beforeAll(async () => {
|
|
25
|
-
({ monitorWebChannel } = await import("./auto-reply/monitor.js"));
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
async function setupSingleInboundMessage(params: {
|
|
29
|
-
resolverValue: { text: string; mediaUrl: string };
|
|
30
|
-
sendMedia: ReturnType<typeof vi.fn>;
|
|
31
|
-
reply?: ReturnType<typeof vi.fn>;
|
|
32
|
-
}) {
|
|
33
|
-
const reply =
|
|
34
|
-
params.reply ?? vi.fn().mockResolvedValue(createAcceptedWhatsAppSendResult("text", "r1"));
|
|
35
|
-
const sendComposing = vi.fn(async () => undefined);
|
|
36
|
-
const resolver = vi.fn().mockResolvedValue(params.resolverValue);
|
|
37
|
-
|
|
38
|
-
let capturedOnMessage: ((msg: WebInboundMessage) => Promise<void>) | undefined;
|
|
39
|
-
const listenerFactory: ListenerFactory = async ({ onMessage }) => {
|
|
40
|
-
capturedOnMessage = onMessage;
|
|
41
|
-
return createMockWebListener();
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
await monitorWebChannel(false, listenerFactory, false, resolver);
|
|
45
|
-
expect(capturedOnMessage).toBeDefined();
|
|
46
|
-
|
|
47
|
-
return {
|
|
48
|
-
reply,
|
|
49
|
-
dispatch: async (
|
|
50
|
-
id = "msg1",
|
|
51
|
-
overrides?: Partial<
|
|
52
|
-
Pick<WebInboundMessage, "from" | "conversationId" | "to" | "accountId" | "chatId">
|
|
53
|
-
>,
|
|
54
|
-
) => {
|
|
55
|
-
await capturedOnMessage?.({
|
|
56
|
-
body: "hello",
|
|
57
|
-
from: "+1",
|
|
58
|
-
conversationId: "+1",
|
|
59
|
-
to: "+2",
|
|
60
|
-
accountId: "default",
|
|
61
|
-
chatType: "direct",
|
|
62
|
-
chatId: "+1",
|
|
63
|
-
...overrides,
|
|
64
|
-
id,
|
|
65
|
-
sendComposing,
|
|
66
|
-
reply,
|
|
67
|
-
sendMedia: params.sendMedia,
|
|
68
|
-
} as WebInboundMessage);
|
|
69
|
-
},
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function getSingleImagePayload(sendMedia: ReturnType<typeof vi.fn>) {
|
|
74
|
-
expect(sendMedia).toHaveBeenCalledTimes(1);
|
|
75
|
-
return sendMedia.mock.calls[0][0] as {
|
|
76
|
-
image: Buffer;
|
|
77
|
-
caption?: string;
|
|
78
|
-
mimetype?: string;
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
async function withMediaCap<T>(mediaMaxMb: number, run: () => Promise<T>): Promise<T> {
|
|
83
|
-
setLoadConfigMock(() => ({
|
|
84
|
-
channels: {
|
|
85
|
-
whatsapp: {
|
|
86
|
-
allowFrom: ["*"],
|
|
87
|
-
mediaMaxMb,
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
}));
|
|
91
|
-
try {
|
|
92
|
-
return await run();
|
|
93
|
-
} finally {
|
|
94
|
-
resetLoadConfigMock();
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function mockFetchMediaBuffer(buffer: Buffer, mime: string) {
|
|
99
|
-
return vi.spyOn(globalThis, "fetch").mockResolvedValue({
|
|
100
|
-
ok: true,
|
|
101
|
-
body: true,
|
|
102
|
-
arrayBuffer: async () =>
|
|
103
|
-
buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength),
|
|
104
|
-
headers: { get: () => mime },
|
|
105
|
-
status: 200,
|
|
106
|
-
} as unknown as Response);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
async function expectCompressedImageWithinCap(params: {
|
|
110
|
-
mediaUrl: string;
|
|
111
|
-
mime: string;
|
|
112
|
-
image: Buffer;
|
|
113
|
-
messageId: string;
|
|
114
|
-
mediaMaxMb?: number;
|
|
115
|
-
}) {
|
|
116
|
-
await withMediaCap(params.mediaMaxMb ?? 1, async () => {
|
|
117
|
-
const sendMedia = vi.fn();
|
|
118
|
-
const { reply, dispatch } = await setupSingleInboundMessage({
|
|
119
|
-
resolverValue: { text: "hi", mediaUrl: params.mediaUrl },
|
|
120
|
-
sendMedia,
|
|
121
|
-
});
|
|
122
|
-
const fetchMock = mockFetchMediaBuffer(params.image, params.mime);
|
|
123
|
-
|
|
124
|
-
await dispatch(params.messageId);
|
|
125
|
-
|
|
126
|
-
const payload = getSingleImagePayload(sendMedia);
|
|
127
|
-
expect(payload.image.length).toBeLessThanOrEqual((params.mediaMaxMb ?? 1) * 1024 * 1024);
|
|
128
|
-
expect(payload.mimetype).toBe("image/jpeg");
|
|
129
|
-
expect(reply).not.toHaveBeenCalled();
|
|
130
|
-
fetchMock.mockRestore();
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
it("compresses common formats to jpeg under the cap", async () => {
|
|
135
|
-
const formats = [
|
|
136
|
-
{
|
|
137
|
-
name: "png",
|
|
138
|
-
mime: "image/png",
|
|
139
|
-
make: (buf: Buffer, opts: { width: number; height: number }) =>
|
|
140
|
-
sharp(buf, {
|
|
141
|
-
raw: { width: opts.width, height: opts.height, channels: 3 },
|
|
142
|
-
})
|
|
143
|
-
.png({ compressionLevel: 0 })
|
|
144
|
-
.toBuffer(),
|
|
145
|
-
},
|
|
146
|
-
{
|
|
147
|
-
name: "jpeg",
|
|
148
|
-
mime: "image/jpeg",
|
|
149
|
-
make: (buf: Buffer, opts: { width: number; height: number }) =>
|
|
150
|
-
sharp(buf, {
|
|
151
|
-
raw: { width: opts.width, height: opts.height, channels: 3 },
|
|
152
|
-
})
|
|
153
|
-
// Keep source > cap with fewer pixels so the test runs faster.
|
|
154
|
-
.jpeg({ quality: 100, chromaSubsampling: "4:4:4" })
|
|
155
|
-
.toBuffer(),
|
|
156
|
-
},
|
|
157
|
-
{
|
|
158
|
-
name: "webp",
|
|
159
|
-
mime: "image/webp",
|
|
160
|
-
make: (buf: Buffer, opts: { width: number; height: number }) =>
|
|
161
|
-
sharp(buf, {
|
|
162
|
-
raw: { width: opts.width, height: opts.height, channels: 3 },
|
|
163
|
-
})
|
|
164
|
-
.webp({ quality: 100 })
|
|
165
|
-
.toBuffer(),
|
|
166
|
-
},
|
|
167
|
-
] as const;
|
|
168
|
-
|
|
169
|
-
const width = 320;
|
|
170
|
-
const height = 320;
|
|
171
|
-
const sharedRaw = crypto.randomBytes(width * height * 3);
|
|
172
|
-
|
|
173
|
-
const renderedFormats = await Promise.all(
|
|
174
|
-
formats.map(async (fmt) =>
|
|
175
|
-
Object.assign({}, fmt, { image: await fmt.make(sharedRaw, { width, height }) }),
|
|
176
|
-
),
|
|
177
|
-
);
|
|
178
|
-
|
|
179
|
-
await withMediaCap(SMALL_MEDIA_CAP_MB, async () => {
|
|
180
|
-
const sendMedia = vi.fn();
|
|
181
|
-
const { reply, dispatch } = await setupSingleInboundMessage({
|
|
182
|
-
resolverValue: {
|
|
183
|
-
text: "hi",
|
|
184
|
-
mediaUrl: "https://example.com/big.image",
|
|
185
|
-
},
|
|
186
|
-
sendMedia,
|
|
187
|
-
});
|
|
188
|
-
let fetchIndex = 0;
|
|
189
|
-
|
|
190
|
-
const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async () => {
|
|
191
|
-
const matched =
|
|
192
|
-
renderedFormats[Math.min(fetchIndex, renderedFormats.length - 1)] ?? renderedFormats[0];
|
|
193
|
-
fetchIndex += 1;
|
|
194
|
-
const { image, mime } = matched;
|
|
195
|
-
return {
|
|
196
|
-
ok: true,
|
|
197
|
-
body: true,
|
|
198
|
-
arrayBuffer: async () =>
|
|
199
|
-
image.buffer.slice(image.byteOffset, image.byteOffset + image.byteLength),
|
|
200
|
-
headers: { get: () => mime },
|
|
201
|
-
status: 200,
|
|
202
|
-
} as unknown as Response;
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
try {
|
|
206
|
-
for (const [index, fmt] of renderedFormats.entries()) {
|
|
207
|
-
expect(fmt.image.length).toBeGreaterThan(SMALL_MEDIA_CAP_BYTES);
|
|
208
|
-
const beforeCalls = sendMedia.mock.calls.length;
|
|
209
|
-
await dispatch(`msg-${fmt.name}-${index}`, {
|
|
210
|
-
from: `+1${index}`,
|
|
211
|
-
conversationId: `conv-${index}`,
|
|
212
|
-
chatId: `conv-${index}`,
|
|
213
|
-
});
|
|
214
|
-
expect(sendMedia).toHaveBeenCalledTimes(beforeCalls + 1);
|
|
215
|
-
const payload = sendMedia.mock.calls[beforeCalls]?.[0] as {
|
|
216
|
-
image: Buffer;
|
|
217
|
-
caption?: string;
|
|
218
|
-
mimetype?: string;
|
|
219
|
-
};
|
|
220
|
-
expect(payload.image.length).toBeLessThanOrEqual(SMALL_MEDIA_CAP_BYTES);
|
|
221
|
-
expect(payload.mimetype).toBe("image/jpeg");
|
|
222
|
-
}
|
|
223
|
-
expect(sendMedia).toHaveBeenCalledTimes(renderedFormats.length);
|
|
224
|
-
expect(reply).not.toHaveBeenCalled();
|
|
225
|
-
} finally {
|
|
226
|
-
fetchMock.mockRestore();
|
|
227
|
-
}
|
|
228
|
-
});
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
it("honors channels.whatsapp.mediaMaxMb for outbound auto-replies", async () => {
|
|
232
|
-
const bigPng = await sharp({
|
|
233
|
-
create: {
|
|
234
|
-
width: 256,
|
|
235
|
-
height: 256,
|
|
236
|
-
channels: 3,
|
|
237
|
-
background: { r: 0, g: 0, b: 255 },
|
|
238
|
-
},
|
|
239
|
-
})
|
|
240
|
-
.png({ compressionLevel: 0 })
|
|
241
|
-
.toBuffer();
|
|
242
|
-
expect(bigPng.length).toBeGreaterThan(SMALL_MEDIA_CAP_BYTES);
|
|
243
|
-
await expectCompressedImageWithinCap({
|
|
244
|
-
mediaUrl: "https://example.com/big.png",
|
|
245
|
-
mime: "image/png",
|
|
246
|
-
image: bigPng,
|
|
247
|
-
messageId: "msg1",
|
|
248
|
-
mediaMaxMb: SMALL_MEDIA_CAP_MB,
|
|
249
|
-
});
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
it("prefers per-account WhatsApp media caps for outbound auto-replies", async () => {
|
|
253
|
-
const bigPng = await sharp({
|
|
254
|
-
create: {
|
|
255
|
-
width: 256,
|
|
256
|
-
height: 256,
|
|
257
|
-
channels: 3,
|
|
258
|
-
background: { r: 255, g: 0, b: 0 },
|
|
259
|
-
},
|
|
260
|
-
})
|
|
261
|
-
.png({ compressionLevel: 0 })
|
|
262
|
-
.toBuffer();
|
|
263
|
-
expect(bigPng.length).toBeGreaterThan(SMALL_MEDIA_CAP_BYTES);
|
|
264
|
-
|
|
265
|
-
setLoadConfigMock(() => ({
|
|
266
|
-
channels: {
|
|
267
|
-
whatsapp: {
|
|
268
|
-
allowFrom: ["*"],
|
|
269
|
-
mediaMaxMb: 1,
|
|
270
|
-
accounts: {
|
|
271
|
-
work: {
|
|
272
|
-
mediaMaxMb: SMALL_MEDIA_CAP_MB,
|
|
273
|
-
},
|
|
274
|
-
},
|
|
275
|
-
},
|
|
276
|
-
},
|
|
277
|
-
}));
|
|
278
|
-
|
|
279
|
-
try {
|
|
280
|
-
const sendMedia = vi.fn();
|
|
281
|
-
const { reply, dispatch } = await setupSingleInboundMessage({
|
|
282
|
-
resolverValue: { text: "hi", mediaUrl: "https://example.com/account-big.png" },
|
|
283
|
-
sendMedia,
|
|
284
|
-
});
|
|
285
|
-
const fetchMock = mockFetchMediaBuffer(bigPng, "image/png");
|
|
286
|
-
|
|
287
|
-
await dispatch("msg-account-cap", { accountId: "work" });
|
|
288
|
-
|
|
289
|
-
const payload = getSingleImagePayload(sendMedia);
|
|
290
|
-
expect(payload.image.length).toBeLessThanOrEqual(SMALL_MEDIA_CAP_BYTES);
|
|
291
|
-
expect(payload.mimetype).toBe("image/jpeg");
|
|
292
|
-
expect(reply).not.toHaveBeenCalled();
|
|
293
|
-
fetchMock.mockRestore();
|
|
294
|
-
} finally {
|
|
295
|
-
resetLoadConfigMock();
|
|
296
|
-
}
|
|
297
|
-
});
|
|
298
|
-
it("sends PDF media as a document", async () => {
|
|
299
|
-
const sendMedia = vi.fn();
|
|
300
|
-
const { reply, dispatch } = await setupSingleInboundMessage({
|
|
301
|
-
resolverValue: { text: "hi", mediaUrl: "https://example.com/file.pdf" },
|
|
302
|
-
sendMedia,
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
const fetchMock = mockFetchMediaBuffer(Buffer.from("%PDF-1.4"), "application/pdf");
|
|
306
|
-
|
|
307
|
-
await dispatch("msg-pdf");
|
|
308
|
-
|
|
309
|
-
expect(sendMedia).toHaveBeenCalledTimes(1);
|
|
310
|
-
const payload = sendMedia.mock.calls[0][0] as {
|
|
311
|
-
document?: Buffer;
|
|
312
|
-
caption?: string;
|
|
313
|
-
fileName?: string;
|
|
314
|
-
};
|
|
315
|
-
expect(payload.document).toBeInstanceOf(Buffer);
|
|
316
|
-
expect(payload.fileName).toBe("file.pdf");
|
|
317
|
-
expect(payload.caption).toBe("hi");
|
|
318
|
-
expect(reply).not.toHaveBeenCalled();
|
|
319
|
-
|
|
320
|
-
fetchMock.mockRestore();
|
|
321
|
-
});
|
|
322
|
-
|
|
323
|
-
it("falls back to text when media send fails", async () => {
|
|
324
|
-
const sendMedia = vi.fn().mockRejectedValue(new Error("boom"));
|
|
325
|
-
const { reply, dispatch } = await setupSingleInboundMessage({
|
|
326
|
-
resolverValue: {
|
|
327
|
-
text: "hi",
|
|
328
|
-
mediaUrl: "https://example.com/img.png",
|
|
329
|
-
},
|
|
330
|
-
sendMedia,
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
const smallPng = await sharp({
|
|
334
|
-
create: {
|
|
335
|
-
width: 64,
|
|
336
|
-
height: 64,
|
|
337
|
-
channels: 3,
|
|
338
|
-
background: { r: 0, g: 255, b: 0 },
|
|
339
|
-
},
|
|
340
|
-
})
|
|
341
|
-
.png()
|
|
342
|
-
.toBuffer();
|
|
343
|
-
const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValue({
|
|
344
|
-
ok: true,
|
|
345
|
-
body: true,
|
|
346
|
-
arrayBuffer: async () =>
|
|
347
|
-
smallPng.buffer.slice(smallPng.byteOffset, smallPng.byteOffset + smallPng.byteLength),
|
|
348
|
-
headers: { get: () => "image/png" },
|
|
349
|
-
status: 200,
|
|
350
|
-
} as unknown as Response);
|
|
351
|
-
|
|
352
|
-
await dispatch("msg1");
|
|
353
|
-
|
|
354
|
-
expect(sendMedia).toHaveBeenCalledTimes(1);
|
|
355
|
-
const fallback = reply.mock.calls[0]?.[0] as string;
|
|
356
|
-
expect(fallback).toContain("hi");
|
|
357
|
-
expect(fallback).toContain("Media failed");
|
|
358
|
-
fetchMock.mockRestore();
|
|
359
|
-
});
|
|
360
|
-
it("returns a warning when remote media fetch 404s", async () => {
|
|
361
|
-
const sendMedia = vi.fn();
|
|
362
|
-
const { reply, dispatch } = await setupSingleInboundMessage({
|
|
363
|
-
resolverValue: {
|
|
364
|
-
text: "caption",
|
|
365
|
-
mediaUrl: "https://example.com/missing.jpg",
|
|
366
|
-
},
|
|
367
|
-
sendMedia,
|
|
368
|
-
});
|
|
369
|
-
|
|
370
|
-
const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValue({
|
|
371
|
-
ok: false,
|
|
372
|
-
status: 404,
|
|
373
|
-
body: null,
|
|
374
|
-
arrayBuffer: async () => new ArrayBuffer(0),
|
|
375
|
-
headers: { get: () => "text/plain" },
|
|
376
|
-
} as unknown as Response);
|
|
377
|
-
|
|
378
|
-
await dispatch("msg1");
|
|
379
|
-
|
|
380
|
-
expect(sendMedia).not.toHaveBeenCalled();
|
|
381
|
-
const fallback = reply.mock.calls[0]?.[0] as string;
|
|
382
|
-
expect(fallback).toContain("caption");
|
|
383
|
-
expect(fallback).toContain("Media failed");
|
|
384
|
-
expect(fallback).not.toContain("404");
|
|
385
|
-
|
|
386
|
-
fetchMock.mockRestore();
|
|
387
|
-
});
|
|
388
|
-
it("sends media with a caption when delivery succeeds", async () => {
|
|
389
|
-
const sendMedia = vi.fn().mockResolvedValue(createAcceptedWhatsAppSendResult("media", "m1"));
|
|
390
|
-
const { reply, dispatch } = await setupSingleInboundMessage({
|
|
391
|
-
resolverValue: {
|
|
392
|
-
text: "hi",
|
|
393
|
-
mediaUrl: "https://example.com/img.png",
|
|
394
|
-
},
|
|
395
|
-
sendMedia,
|
|
396
|
-
});
|
|
397
|
-
|
|
398
|
-
const png = await sharp({
|
|
399
|
-
create: {
|
|
400
|
-
width: 64,
|
|
401
|
-
height: 64,
|
|
402
|
-
channels: 3,
|
|
403
|
-
background: { r: 0, g: 0, b: 255 },
|
|
404
|
-
},
|
|
405
|
-
})
|
|
406
|
-
.png()
|
|
407
|
-
.toBuffer();
|
|
408
|
-
|
|
409
|
-
const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValue({
|
|
410
|
-
ok: true,
|
|
411
|
-
body: true,
|
|
412
|
-
arrayBuffer: async () => png.buffer.slice(png.byteOffset, png.byteOffset + png.byteLength),
|
|
413
|
-
headers: { get: () => "image/png" },
|
|
414
|
-
status: 200,
|
|
415
|
-
} as unknown as Response);
|
|
416
|
-
|
|
417
|
-
await dispatch("msg1");
|
|
418
|
-
|
|
419
|
-
const payload = getSingleImagePayload(sendMedia);
|
|
420
|
-
expect(payload.caption).toBe("hi");
|
|
421
|
-
expect(payload.image.length).toBeGreaterThan(0);
|
|
422
|
-
// Should not fall back to separate text reply because caption is used.
|
|
423
|
-
expect(reply).not.toHaveBeenCalled();
|
|
424
|
-
|
|
425
|
-
fetchMock.mockRestore();
|
|
426
|
-
});
|
|
427
|
-
});
|