@gakr-gakr/whatsapp 0.1.0
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/action-runtime-api.ts +1 -0
- package/action-runtime.runtime.ts +1 -0
- package/api.ts +67 -0
- package/auth-presence.ts +80 -0
- package/autobot.plugin.json +23 -0
- package/channel-config-api.ts +1 -0
- package/channel-plugin-api.ts +3 -0
- package/config-api.ts +4 -0
- package/constants.ts +1 -0
- package/contract-api.ts +29 -0
- package/directory-contract-api.ts +4 -0
- package/doctor-contract-api.ts +8 -0
- package/index.ts +16 -0
- package/legacy-session-surface-api.ts +6 -0
- package/legacy-state-migrations-api.ts +1 -0
- package/light-runtime-api.ts +12 -0
- package/login-qr-api.ts +1 -0
- package/login-qr-runtime.ts +23 -0
- package/outbound-payload-test-api.ts +1 -0
- package/package.json +76 -0
- package/runtime-api.ts +84 -0
- package/secret-contract-api.ts +4 -0
- package/security-contract-api.ts +4 -0
- package/setup-entry.ts +21 -0
- package/setup-plugin-api.ts +3 -0
- package/src/account-config.ts +77 -0
- package/src/account-ids.ts +17 -0
- package/src/account-types.ts +5 -0
- package/src/accounts.ts +176 -0
- package/src/action-runtime-target-auth.ts +27 -0
- package/src/action-runtime.ts +76 -0
- package/src/active-listener.ts +17 -0
- package/src/agent-tools-login.ts +113 -0
- package/src/approval-auth.ts +27 -0
- package/src/auth-store.runtime.ts +1 -0
- package/src/auth-store.ts +494 -0
- package/src/auto-reply/config.runtime.ts +16 -0
- package/src/auto-reply/constants.ts +1 -0
- package/src/auto-reply/deliver-reply.ts +332 -0
- package/src/auto-reply/loggers.ts +6 -0
- package/src/auto-reply/mentions.ts +131 -0
- package/src/auto-reply/monitor/ack-reaction.ts +99 -0
- package/src/auto-reply/monitor/audio-preflight.runtime.ts +9 -0
- package/src/auto-reply/monitor/broadcast.ts +153 -0
- package/src/auto-reply/monitor/commands.ts +19 -0
- package/src/auto-reply/monitor/echo.ts +64 -0
- package/src/auto-reply/monitor/group-activation.runtime.ts +1 -0
- package/src/auto-reply/monitor/group-activation.ts +73 -0
- package/src/auto-reply/monitor/group-gating.runtime.ts +8 -0
- package/src/auto-reply/monitor/group-gating.ts +218 -0
- package/src/auto-reply/monitor/group-members.ts +65 -0
- package/src/auto-reply/monitor/inbound-context.ts +92 -0
- package/src/auto-reply/monitor/inbound-dispatch.runtime.ts +22 -0
- package/src/auto-reply/monitor/inbound-dispatch.ts +749 -0
- package/src/auto-reply/monitor/last-route.ts +61 -0
- package/src/auto-reply/monitor/listener-log.ts +28 -0
- package/src/auto-reply/monitor/message-line.runtime.ts +38 -0
- package/src/auto-reply/monitor/message-line.ts +54 -0
- package/src/auto-reply/monitor/on-message.ts +333 -0
- package/src/auto-reply/monitor/peer.ts +17 -0
- package/src/auto-reply/monitor/process-message.ts +584 -0
- package/src/auto-reply/monitor/runtime-api.ts +36 -0
- package/src/auto-reply/monitor/status-reaction.ts +108 -0
- package/src/auto-reply/monitor-state.ts +114 -0
- package/src/auto-reply/monitor.ts +720 -0
- package/src/auto-reply/reply-resolver.runtime.ts +1 -0
- package/src/auto-reply/types.ts +48 -0
- package/src/auto-reply/util.ts +62 -0
- package/src/auto-reply.impl.ts +6 -0
- package/src/auto-reply.ts +1 -0
- package/src/channel-actions.runtime.ts +7 -0
- package/src/channel-actions.ts +85 -0
- package/src/channel-outbound.ts +87 -0
- package/src/channel-react-action.runtime.ts +10 -0
- package/src/channel-react-action.ts +247 -0
- package/src/channel.runtime.ts +117 -0
- package/src/channel.setup.ts +32 -0
- package/src/channel.ts +356 -0
- package/src/command-policy.ts +7 -0
- package/src/config-accessors.ts +22 -0
- package/src/config-schema.ts +6 -0
- package/src/config-ui-hints.ts +24 -0
- package/src/connection-controller-registry.ts +49 -0
- package/src/connection-controller.ts +680 -0
- package/src/creds-files.ts +19 -0
- package/src/creds-persistence.ts +71 -0
- package/src/directory-config.ts +40 -0
- package/src/doctor-contract.ts +11 -0
- package/src/doctor.ts +56 -0
- package/src/document-filename.ts +17 -0
- package/src/group-intro.ts +15 -0
- package/src/group-policy.ts +40 -0
- package/src/group-session-contract.ts +20 -0
- package/src/group-session-key.ts +42 -0
- package/src/heartbeat.ts +34 -0
- package/src/identity.ts +164 -0
- package/src/inbound/access-control.ts +187 -0
- package/src/inbound/dedupe.ts +132 -0
- package/src/inbound/extract.ts +484 -0
- package/src/inbound/lifecycle.ts +39 -0
- package/src/inbound/media.ts +128 -0
- package/src/inbound/monitor.ts +1042 -0
- package/src/inbound/outbound-mentions.ts +260 -0
- package/src/inbound/runtime-api.ts +7 -0
- package/src/inbound/save-media.runtime.ts +1 -0
- package/src/inbound/send-api.ts +203 -0
- package/src/inbound/send-result.ts +109 -0
- package/src/inbound/types.ts +107 -0
- package/src/inbound-policy.ts +215 -0
- package/src/inbound.ts +9 -0
- package/src/login-qr.ts +542 -0
- package/src/login.ts +83 -0
- package/src/media.ts +10 -0
- package/src/monitor-inbox.allows-messages-from-senders-allowfrom-list.test-support.ts +417 -0
- package/src/monitor-inbox.append-upsert.test-support.ts +133 -0
- package/src/monitor-inbox.blocks-messages-from-unauthorized-senders-not-allowfrom.test-support.ts +418 -0
- package/src/monitor-inbox.captures-media-path-image-messages.test-support.ts +308 -0
- package/src/monitor-inbox.streams-inbound-messages.test-support.ts +824 -0
- package/src/normalize-target.ts +148 -0
- package/src/normalize.ts +8 -0
- package/src/outbound-adapter.ts +36 -0
- package/src/outbound-base.ts +256 -0
- package/src/outbound-media-contract.ts +307 -0
- package/src/outbound-media.runtime.ts +41 -0
- package/src/outbound-send-deps.ts +1 -0
- package/src/outbound-test-support.ts +16 -0
- package/src/qa-driver.runtime.ts +189 -0
- package/src/qr-image.ts +1 -0
- package/src/qr-terminal.ts +1 -0
- package/src/quoted-message.ts +184 -0
- package/src/reaction-level.ts +24 -0
- package/src/reconnect.ts +55 -0
- package/src/resolve-outbound-target.ts +58 -0
- package/src/runtime-api.ts +59 -0
- package/src/runtime-group-policy.ts +16 -0
- package/src/runtime.ts +9 -0
- package/src/security-contract.ts +47 -0
- package/src/security-fix.ts +71 -0
- package/src/send.ts +342 -0
- package/src/session-contract.ts +43 -0
- package/src/session-errors.ts +125 -0
- package/src/session-route.ts +32 -0
- package/src/session.runtime.ts +8 -0
- package/src/session.ts +327 -0
- package/src/setup-core.ts +52 -0
- package/src/setup-finalize.ts +450 -0
- package/src/setup-surface.ts +71 -0
- package/src/setup-test-helpers.ts +217 -0
- package/src/shared.ts +291 -0
- package/src/socket-timing.ts +38 -0
- package/src/state-migrations.ts +55 -0
- package/src/status-issues.ts +185 -0
- package/src/system-prompt.ts +31 -0
- package/src/targets-runtime.ts +221 -0
- package/src/text-runtime.ts +18 -0
- package/src/vcard.ts +84 -0
- package/targets.ts +5 -0
- package/test-api.ts +2 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,824 @@
|
|
|
1
|
+
import fsSync from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import "./monitor-inbox.test-harness.js";
|
|
4
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
5
|
+
import { WhatsAppRetryableInboundError } from "./inbound/dedupe.js";
|
|
6
|
+
import { WHATSAPP_GROUP_METADATA_CACHE_MAX_ENTRIES } from "./inbound/monitor.js";
|
|
7
|
+
import {
|
|
8
|
+
type InboxMonitorOptions,
|
|
9
|
+
InboxOnMessage,
|
|
10
|
+
buildNotifyMessageUpsert,
|
|
11
|
+
getAuthDir,
|
|
12
|
+
getSock,
|
|
13
|
+
installWebMonitorInboxUnitTestHooks,
|
|
14
|
+
startInboxMonitor,
|
|
15
|
+
waitForMessageCalls,
|
|
16
|
+
} from "./monitor-inbox.test-harness.js";
|
|
17
|
+
|
|
18
|
+
const { sleepWithAbortMock } = vi.hoisted(() => ({
|
|
19
|
+
sleepWithAbortMock: vi.fn(async (_ms: number, _signal?: AbortSignal) => undefined),
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
vi.mock("./reconnect.js", async () => {
|
|
23
|
+
const actual = await vi.importActual<typeof import("./reconnect.js")>("./reconnect.js");
|
|
24
|
+
return {
|
|
25
|
+
...actual,
|
|
26
|
+
sleepWithAbort: (ms: number, signal?: AbortSignal) => sleepWithAbortMock(ms, signal),
|
|
27
|
+
};
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
let nextMessageSequence = 0;
|
|
31
|
+
|
|
32
|
+
function nextMessageId(label: string): string {
|
|
33
|
+
nextMessageSequence += 1;
|
|
34
|
+
return `${label}-${nextMessageSequence}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function createSocketRef(): NonNullable<InboxMonitorOptions["socketRef"]> {
|
|
38
|
+
return { current: null };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function inboundMessage(onMessage: ReturnType<typeof vi.fn>, index = 0): Record<string, unknown> {
|
|
42
|
+
const msg = onMessage.mock.calls[index]?.[0];
|
|
43
|
+
expect(msg).toBeDefined();
|
|
44
|
+
return msg as Record<string, unknown>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function primeInboundReplyHandle(params: {
|
|
48
|
+
onMessage: ReturnType<typeof vi.fn>;
|
|
49
|
+
socketRef: NonNullable<InboxMonitorOptions["socketRef"]>;
|
|
50
|
+
upsertId: string;
|
|
51
|
+
retryPolicy: NonNullable<InboxMonitorOptions["disconnectRetryPolicy"]>;
|
|
52
|
+
useCurrentSock?: boolean;
|
|
53
|
+
}) {
|
|
54
|
+
const { listener, sock } = await startInboxMonitor(params.onMessage as InboxOnMessage, {
|
|
55
|
+
socketRef: params.socketRef,
|
|
56
|
+
shouldRetryDisconnect: () => true,
|
|
57
|
+
disconnectRetryPolicy: params.retryPolicy,
|
|
58
|
+
});
|
|
59
|
+
const sourceSock = params.useCurrentSock ? getSock() : sock;
|
|
60
|
+
sourceSock.ev.emit(
|
|
61
|
+
"messages.upsert",
|
|
62
|
+
buildNotifyMessageUpsert({
|
|
63
|
+
id: nextMessageId(params.upsertId),
|
|
64
|
+
remoteJid: "999@s.whatsapp.net",
|
|
65
|
+
text: "ping",
|
|
66
|
+
timestamp: 1_700_000_000,
|
|
67
|
+
pushName: "Tester",
|
|
68
|
+
}),
|
|
69
|
+
);
|
|
70
|
+
await waitForMessageCalls(params.onMessage, 1);
|
|
71
|
+
|
|
72
|
+
const inbound = inboundMessage(params.onMessage) as {
|
|
73
|
+
reply: (text: string) => Promise<void>;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
return { listener, sock, inbound };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
describe("web monitor inbox", () => {
|
|
80
|
+
installWebMonitorInboxUnitTestHooks();
|
|
81
|
+
|
|
82
|
+
beforeEach(() => {
|
|
83
|
+
sleepWithAbortMock.mockReset();
|
|
84
|
+
sleepWithAbortMock.mockImplementation(async (_ms: number, _signal?: AbortSignal) => undefined);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
async function expectQuotedReplyContext(quotedMessage: unknown) {
|
|
88
|
+
const onMessage = vi.fn(async (msg) => {
|
|
89
|
+
await msg.reply("pong");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const { listener, sock } = await startInboxMonitor(onMessage as InboxOnMessage);
|
|
93
|
+
const upsert = {
|
|
94
|
+
type: "notify",
|
|
95
|
+
messages: [
|
|
96
|
+
{
|
|
97
|
+
key: {
|
|
98
|
+
id: nextMessageId("quoted"),
|
|
99
|
+
fromMe: false,
|
|
100
|
+
remoteJid: "999@s.whatsapp.net",
|
|
101
|
+
},
|
|
102
|
+
message: {
|
|
103
|
+
extendedTextMessage: {
|
|
104
|
+
text: "reply",
|
|
105
|
+
contextInfo: {
|
|
106
|
+
stanzaId: "q1",
|
|
107
|
+
participant: "111@s.whatsapp.net",
|
|
108
|
+
quotedMessage,
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
messageTimestamp: 1_700_000_000,
|
|
113
|
+
pushName: "Tester",
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
sock.ev.emit("messages.upsert", upsert);
|
|
119
|
+
await waitForMessageCalls(onMessage, 1);
|
|
120
|
+
|
|
121
|
+
const inbound = inboundMessage(onMessage);
|
|
122
|
+
expect(inbound.replyToId).toBe("q1");
|
|
123
|
+
expect(inbound.replyToBody).toBe("original");
|
|
124
|
+
expect(inbound.replyToSender).toBe("+111");
|
|
125
|
+
const sender = inbound.sender as { e164?: string; name?: string };
|
|
126
|
+
expect(sender.e164).toBe("+999");
|
|
127
|
+
expect(sender.name).toBe("Tester");
|
|
128
|
+
const replyTo = inbound.replyTo as {
|
|
129
|
+
body?: string;
|
|
130
|
+
id?: string;
|
|
131
|
+
sender?: { e164?: string; jid?: string; label?: string };
|
|
132
|
+
};
|
|
133
|
+
expect(replyTo.id).toBe("q1");
|
|
134
|
+
expect(replyTo.body).toBe("original");
|
|
135
|
+
expect(replyTo.sender?.jid).toBe("111@s.whatsapp.net");
|
|
136
|
+
expect(replyTo.sender?.e164).toBe("+111");
|
|
137
|
+
expect(replyTo.sender?.label).toBe("+111");
|
|
138
|
+
const self = inbound.self as { e164?: string; jid?: string };
|
|
139
|
+
expect(self.jid).toBe("123@s.whatsapp.net");
|
|
140
|
+
expect(self.e164).toBe("+123");
|
|
141
|
+
expect(sock.sendMessage).toHaveBeenCalledWith("999@s.whatsapp.net", {
|
|
142
|
+
text: "pong",
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
await listener.close();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
it("streams inbound messages", async () => {
|
|
149
|
+
const onMessage = vi.fn(async (msg) => {
|
|
150
|
+
await msg.sendComposing();
|
|
151
|
+
await msg.reply("pong");
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
const { listener, sock } = await startInboxMonitor(onMessage as InboxOnMessage);
|
|
155
|
+
expect(sock.sendPresenceUpdate).toHaveBeenNthCalledWith(1, "available");
|
|
156
|
+
const messageId = nextMessageId("stream");
|
|
157
|
+
const upsert = buildNotifyMessageUpsert({
|
|
158
|
+
id: messageId,
|
|
159
|
+
remoteJid: "999@s.whatsapp.net",
|
|
160
|
+
text: "ping",
|
|
161
|
+
timestamp: 1_700_000_000,
|
|
162
|
+
pushName: "Tester",
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
sock.ev.emit("messages.upsert", upsert);
|
|
166
|
+
await waitForMessageCalls(onMessage, 1);
|
|
167
|
+
|
|
168
|
+
const inbound = inboundMessage(onMessage);
|
|
169
|
+
expect(inbound.body).toBe("ping");
|
|
170
|
+
expect(inbound.from).toBe("+999");
|
|
171
|
+
expect(inbound.to).toBe("+123");
|
|
172
|
+
expect(sock.readMessages).toHaveBeenCalledWith([
|
|
173
|
+
{
|
|
174
|
+
remoteJid: "999@s.whatsapp.net",
|
|
175
|
+
id: messageId,
|
|
176
|
+
participant: undefined,
|
|
177
|
+
fromMe: false,
|
|
178
|
+
},
|
|
179
|
+
]);
|
|
180
|
+
expect(sock.sendPresenceUpdate).toHaveBeenCalledWith("available");
|
|
181
|
+
expect(sock.sendPresenceUpdate).toHaveBeenCalledWith("composing", "999@s.whatsapp.net");
|
|
182
|
+
expect(sock.sendMessage).toHaveBeenCalledWith("999@s.whatsapp.net", {
|
|
183
|
+
text: "pong",
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
await listener.close();
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("stays unavailable on connect in self-chat mode", async () => {
|
|
190
|
+
const { listener, sock } = await startInboxMonitor(vi.fn(async () => {}) as InboxOnMessage, {
|
|
191
|
+
selfChatMode: true,
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
expect(sock.sendPresenceUpdate).toHaveBeenNthCalledWith(1, "unavailable");
|
|
195
|
+
|
|
196
|
+
await listener.close();
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("hydrates participating groups once after connect", async () => {
|
|
200
|
+
const { listener, sock } = await startInboxMonitor(vi.fn(async () => {}) as InboxOnMessage);
|
|
201
|
+
|
|
202
|
+
expect(sock.groupFetchAllParticipating).toHaveBeenCalledTimes(1);
|
|
203
|
+
|
|
204
|
+
await listener.close();
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("continues when group hydration fails on connect", async () => {
|
|
208
|
+
const sock = getSock();
|
|
209
|
+
sock.groupFetchAllParticipating.mockRejectedValueOnce(new Error("no groups"));
|
|
210
|
+
|
|
211
|
+
const { listener } = await startInboxMonitor(vi.fn(async () => {}) as InboxOnMessage);
|
|
212
|
+
|
|
213
|
+
expect(sock.groupFetchAllParticipating).toHaveBeenCalledTimes(1);
|
|
214
|
+
expect(sock.sendPresenceUpdate).toHaveBeenNthCalledWith(1, "available");
|
|
215
|
+
|
|
216
|
+
await listener.close();
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("keeps group inbound alive with cached metadata after reconnect-time metadata fetch failures", async () => {
|
|
220
|
+
const groupMetadataCache: NonNullable<InboxMonitorOptions["groupMetadataCache"]> = new Map();
|
|
221
|
+
const onMessage = vi.fn(async (_msg: Parameters<InboxOnMessage>[0]) => {
|
|
222
|
+
return;
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
const firstSock = getSock();
|
|
226
|
+
firstSock.groupFetchAllParticipating.mockResolvedValueOnce({
|
|
227
|
+
"123@g.us": {
|
|
228
|
+
id: "123@g.us",
|
|
229
|
+
subject: "Recovered Group",
|
|
230
|
+
owner: undefined,
|
|
231
|
+
participants: [{ id: "444@s.whatsapp.net" }],
|
|
232
|
+
},
|
|
233
|
+
});
|
|
234
|
+
const first = await startInboxMonitor(onMessage as InboxOnMessage, {
|
|
235
|
+
groupMetadataCache,
|
|
236
|
+
});
|
|
237
|
+
await vi.waitFor(() => {
|
|
238
|
+
expect(groupMetadataCache.get("123@g.us")?.subject).toBe("Recovered Group");
|
|
239
|
+
});
|
|
240
|
+
expect(
|
|
241
|
+
(groupMetadataCache.get("123@g.us") as Record<string, unknown>)?.participants,
|
|
242
|
+
).toBeUndefined();
|
|
243
|
+
await first.listener.close();
|
|
244
|
+
|
|
245
|
+
const second = await startInboxMonitor(onMessage as InboxOnMessage, {
|
|
246
|
+
groupMetadataCache,
|
|
247
|
+
});
|
|
248
|
+
second.sock.groupMetadata.mockRejectedValueOnce(new Error("408 timed out"));
|
|
249
|
+
second.sock.ev.emit(
|
|
250
|
+
"messages.upsert",
|
|
251
|
+
buildNotifyMessageUpsert({
|
|
252
|
+
id: nextMessageId("group-reconnect-cache"),
|
|
253
|
+
remoteJid: "123@g.us",
|
|
254
|
+
participant: "444@s.whatsapp.net",
|
|
255
|
+
text: "ping",
|
|
256
|
+
timestamp: 1_700_000_000,
|
|
257
|
+
}),
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
await waitForMessageCalls(onMessage, 1);
|
|
261
|
+
const inbound = inboundMessage(onMessage);
|
|
262
|
+
expect(inbound.body).toBe("ping");
|
|
263
|
+
expect(inbound.from).toBe("123@g.us");
|
|
264
|
+
expect(inbound.groupSubject).toBe("Recovered Group");
|
|
265
|
+
expect(inbound.senderE164).toBe("+444");
|
|
266
|
+
expect(inbound.chatType).toBe("group");
|
|
267
|
+
expect(inbound.groupParticipants).toBeUndefined();
|
|
268
|
+
|
|
269
|
+
await second.listener.close();
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("bounds cached group metadata kept across reconnects", async () => {
|
|
273
|
+
const groupMetadataCache: NonNullable<InboxMonitorOptions["groupMetadataCache"]> = new Map();
|
|
274
|
+
const groups = Object.fromEntries(
|
|
275
|
+
Array.from({ length: WHATSAPP_GROUP_METADATA_CACHE_MAX_ENTRIES + 2 }, (_, index) => [
|
|
276
|
+
`${index}@g.us`,
|
|
277
|
+
{
|
|
278
|
+
id: `${index}@g.us`,
|
|
279
|
+
subject: `Group ${index}`,
|
|
280
|
+
owner: undefined,
|
|
281
|
+
participants: [],
|
|
282
|
+
},
|
|
283
|
+
]),
|
|
284
|
+
);
|
|
285
|
+
const sock = getSock();
|
|
286
|
+
sock.groupFetchAllParticipating.mockResolvedValueOnce(groups);
|
|
287
|
+
|
|
288
|
+
const { listener } = await startInboxMonitor(vi.fn(async () => {}) as InboxOnMessage, {
|
|
289
|
+
groupMetadataCache,
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
await vi.waitFor(() => {
|
|
293
|
+
expect(groupMetadataCache.size).toBe(WHATSAPP_GROUP_METADATA_CACHE_MAX_ENTRIES);
|
|
294
|
+
});
|
|
295
|
+
expect(groupMetadataCache.has("0@g.us")).toBe(false);
|
|
296
|
+
expect(groupMetadataCache.has(`${WHATSAPP_GROUP_METADATA_CACHE_MAX_ENTRIES + 1}@g.us`)).toBe(
|
|
297
|
+
true,
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
await listener.close();
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("does not block inbound listeners while group hydration is pending", async () => {
|
|
304
|
+
let resolveHydration!: () => void;
|
|
305
|
+
const sock = getSock();
|
|
306
|
+
const pendingHydration = new Promise<Record<string, never>>((resolve) => {
|
|
307
|
+
resolveHydration = () => resolve({});
|
|
308
|
+
});
|
|
309
|
+
sock.groupFetchAllParticipating.mockImplementationOnce(() => pendingHydration);
|
|
310
|
+
const onMessage = vi.fn(async () => {
|
|
311
|
+
return;
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
const { listener } = await startInboxMonitor(onMessage as InboxOnMessage);
|
|
315
|
+
sock.ev.emit(
|
|
316
|
+
"messages.upsert",
|
|
317
|
+
buildNotifyMessageUpsert({
|
|
318
|
+
id: nextMessageId("pending-hydration"),
|
|
319
|
+
remoteJid: "999@s.whatsapp.net",
|
|
320
|
+
text: "ping",
|
|
321
|
+
timestamp: 1_700_000_000,
|
|
322
|
+
pushName: "Tester",
|
|
323
|
+
}),
|
|
324
|
+
);
|
|
325
|
+
await waitForMessageCalls(onMessage, 1);
|
|
326
|
+
|
|
327
|
+
resolveHydration();
|
|
328
|
+
await listener.close();
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it("uses a replacement socket for replies created before reconnect", async () => {
|
|
332
|
+
const onMessage = vi.fn(async () => undefined);
|
|
333
|
+
const socketRef: NonNullable<InboxMonitorOptions["socketRef"]> = { current: null };
|
|
334
|
+
|
|
335
|
+
const { listener, sock } = await startInboxMonitor(onMessage as InboxOnMessage, { socketRef });
|
|
336
|
+
sock.ev.emit(
|
|
337
|
+
"messages.upsert",
|
|
338
|
+
buildNotifyMessageUpsert({
|
|
339
|
+
id: nextMessageId("replacement-socket"),
|
|
340
|
+
remoteJid: "999@s.whatsapp.net",
|
|
341
|
+
text: "ping",
|
|
342
|
+
timestamp: 1_700_000_000,
|
|
343
|
+
pushName: "Tester",
|
|
344
|
+
}),
|
|
345
|
+
);
|
|
346
|
+
await waitForMessageCalls(onMessage, 1);
|
|
347
|
+
|
|
348
|
+
const inbound = inboundMessage(onMessage) as {
|
|
349
|
+
reply: (text: string) => Promise<void>;
|
|
350
|
+
sendMedia: (payload: Record<string, unknown>) => Promise<void>;
|
|
351
|
+
sendComposing: () => Promise<void>;
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
const replacementSock = {
|
|
355
|
+
sendMessage: vi.fn(async () => undefined),
|
|
356
|
+
sendPresenceUpdate: vi.fn(async () => undefined),
|
|
357
|
+
};
|
|
358
|
+
socketRef.current = replacementSock as unknown as NonNullable<
|
|
359
|
+
InboxMonitorOptions["socketRef"]
|
|
360
|
+
>["current"];
|
|
361
|
+
|
|
362
|
+
await inbound.reply("pong");
|
|
363
|
+
await inbound.sendMedia({ text: "after-reconnect" });
|
|
364
|
+
await inbound.sendComposing();
|
|
365
|
+
|
|
366
|
+
expect(replacementSock.sendMessage).toHaveBeenNthCalledWith(1, "999@s.whatsapp.net", {
|
|
367
|
+
text: "pong",
|
|
368
|
+
});
|
|
369
|
+
expect(replacementSock.sendMessage).toHaveBeenNthCalledWith(2, "999@s.whatsapp.net", {
|
|
370
|
+
text: "after-reconnect",
|
|
371
|
+
});
|
|
372
|
+
expect(replacementSock.sendPresenceUpdate).toHaveBeenCalledWith(
|
|
373
|
+
"composing",
|
|
374
|
+
"999@s.whatsapp.net",
|
|
375
|
+
);
|
|
376
|
+
expect(sock.sendMessage).not.toHaveBeenCalled();
|
|
377
|
+
|
|
378
|
+
await listener.close();
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it("waits for a replacement socket before sending replies", async () => {
|
|
382
|
+
const onMessage = vi.fn(async () => undefined);
|
|
383
|
+
const socketRef = createSocketRef();
|
|
384
|
+
const { listener, sock, inbound } = await primeInboundReplyHandle({
|
|
385
|
+
onMessage,
|
|
386
|
+
socketRef,
|
|
387
|
+
upsertId: "reconnect-gap",
|
|
388
|
+
retryPolicy: {
|
|
389
|
+
initialMs: 10,
|
|
390
|
+
maxMs: 10,
|
|
391
|
+
factor: 1,
|
|
392
|
+
jitter: 0,
|
|
393
|
+
maxAttempts: 2,
|
|
394
|
+
},
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
const replacementSock = {
|
|
398
|
+
sendMessage: vi.fn(async () => undefined),
|
|
399
|
+
sendPresenceUpdate: vi.fn(async () => undefined),
|
|
400
|
+
};
|
|
401
|
+
socketRef.current = null;
|
|
402
|
+
sleepWithAbortMock.mockImplementationOnce(async () => {
|
|
403
|
+
socketRef.current = replacementSock as unknown as NonNullable<
|
|
404
|
+
InboxMonitorOptions["socketRef"]
|
|
405
|
+
>["current"];
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
await inbound?.reply("pong");
|
|
409
|
+
|
|
410
|
+
expect(sleepWithAbortMock).toHaveBeenCalledWith(10, undefined);
|
|
411
|
+
expect(replacementSock.sendMessage).toHaveBeenCalledWith("999@s.whatsapp.net", {
|
|
412
|
+
text: "pong",
|
|
413
|
+
});
|
|
414
|
+
expect(sock.sendMessage).not.toHaveBeenCalled();
|
|
415
|
+
|
|
416
|
+
await listener.close();
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
it("flushes pending debounced inbound batches after close", async () => {
|
|
420
|
+
vi.useFakeTimers();
|
|
421
|
+
try {
|
|
422
|
+
const onMessage = vi.fn(async () => undefined);
|
|
423
|
+
const { listener, sock } = await startInboxMonitor(onMessage as InboxOnMessage, {
|
|
424
|
+
debounceMs: 50,
|
|
425
|
+
});
|
|
426
|
+
sock.ev.emit(
|
|
427
|
+
"messages.upsert",
|
|
428
|
+
buildNotifyMessageUpsert({
|
|
429
|
+
id: nextMessageId("debounce-close-1"),
|
|
430
|
+
remoteJid: "999@s.whatsapp.net",
|
|
431
|
+
text: "first",
|
|
432
|
+
timestamp: 1_700_000_000,
|
|
433
|
+
pushName: "Tester",
|
|
434
|
+
}),
|
|
435
|
+
);
|
|
436
|
+
sock.ev.emit(
|
|
437
|
+
"messages.upsert",
|
|
438
|
+
buildNotifyMessageUpsert({
|
|
439
|
+
id: nextMessageId("debounce-close-2"),
|
|
440
|
+
remoteJid: "999@s.whatsapp.net",
|
|
441
|
+
text: "second",
|
|
442
|
+
timestamp: 1_700_000_001,
|
|
443
|
+
pushName: "Tester",
|
|
444
|
+
}),
|
|
445
|
+
);
|
|
446
|
+
|
|
447
|
+
await listener.close();
|
|
448
|
+
await vi.advanceTimersByTimeAsync(50);
|
|
449
|
+
await waitForMessageCalls(onMessage, 1);
|
|
450
|
+
expect(inboundMessage(onMessage).body).toBe("first\nsecond");
|
|
451
|
+
} finally {
|
|
452
|
+
vi.useRealTimers();
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
it("lets a drained debounced inbound reply before closing the socket", async () => {
|
|
457
|
+
vi.useFakeTimers();
|
|
458
|
+
try {
|
|
459
|
+
const onMessage = vi.fn(async (msg) => {
|
|
460
|
+
await msg.reply("pong");
|
|
461
|
+
await msg.sendMedia({ text: "media" });
|
|
462
|
+
});
|
|
463
|
+
const { listener, sock } = await startInboxMonitor(onMessage as InboxOnMessage, {
|
|
464
|
+
debounceMs: 50,
|
|
465
|
+
});
|
|
466
|
+
sock.ev.emit(
|
|
467
|
+
"messages.upsert",
|
|
468
|
+
buildNotifyMessageUpsert({
|
|
469
|
+
id: nextMessageId("debounce-close-reply-1"),
|
|
470
|
+
remoteJid: "999@s.whatsapp.net",
|
|
471
|
+
text: "first",
|
|
472
|
+
timestamp: 1_700_000_000,
|
|
473
|
+
pushName: "Tester",
|
|
474
|
+
}),
|
|
475
|
+
);
|
|
476
|
+
sock.ev.emit(
|
|
477
|
+
"messages.upsert",
|
|
478
|
+
buildNotifyMessageUpsert({
|
|
479
|
+
id: nextMessageId("debounce-close-reply-2"),
|
|
480
|
+
remoteJid: "999@s.whatsapp.net",
|
|
481
|
+
text: "second",
|
|
482
|
+
timestamp: 1_700_000_001,
|
|
483
|
+
pushName: "Tester",
|
|
484
|
+
}),
|
|
485
|
+
);
|
|
486
|
+
|
|
487
|
+
await listener.close();
|
|
488
|
+
|
|
489
|
+
expect(onMessage).toHaveBeenCalledTimes(1);
|
|
490
|
+
expect(inboundMessage(onMessage).body).toBe("first\nsecond");
|
|
491
|
+
expect(sock.sendMessage).toHaveBeenNthCalledWith(1, "999@s.whatsapp.net", {
|
|
492
|
+
text: "pong",
|
|
493
|
+
});
|
|
494
|
+
expect(sock.sendMessage).toHaveBeenNthCalledWith(2, "999@s.whatsapp.net", {
|
|
495
|
+
text: "media",
|
|
496
|
+
});
|
|
497
|
+
expect(sock.end).toHaveBeenCalledTimes(1);
|
|
498
|
+
expect(sock.sendMessage.mock.invocationCallOrder.at(-1)).toBeLessThan(
|
|
499
|
+
sock.end.mock.invocationCallOrder.at(0),
|
|
500
|
+
);
|
|
501
|
+
} finally {
|
|
502
|
+
vi.useRealTimers();
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
it("waits for in-flight inbound handlers before draining on close", async () => {
|
|
507
|
+
vi.useFakeTimers();
|
|
508
|
+
try {
|
|
509
|
+
const onMessage = vi.fn(async (msg) => {
|
|
510
|
+
await msg.reply("pong");
|
|
511
|
+
});
|
|
512
|
+
const { listener, sock } = await startInboxMonitor(onMessage as InboxOnMessage, {
|
|
513
|
+
debounceMs: 50,
|
|
514
|
+
});
|
|
515
|
+
let releaseRead: (() => void) | undefined;
|
|
516
|
+
const readGate = new Promise<void>((resolve) => {
|
|
517
|
+
releaseRead = resolve;
|
|
518
|
+
});
|
|
519
|
+
const readStarted = new Promise<void>((resolve) => {
|
|
520
|
+
sock.readMessages.mockImplementationOnce(async () => {
|
|
521
|
+
resolve();
|
|
522
|
+
await readGate;
|
|
523
|
+
});
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
sock.ev.emit(
|
|
527
|
+
"messages.upsert",
|
|
528
|
+
buildNotifyMessageUpsert({
|
|
529
|
+
id: nextMessageId("debounce-close-inflight"),
|
|
530
|
+
remoteJid: "999@s.whatsapp.net",
|
|
531
|
+
text: "first",
|
|
532
|
+
timestamp: 1_700_000_000,
|
|
533
|
+
pushName: "Tester",
|
|
534
|
+
}),
|
|
535
|
+
);
|
|
536
|
+
|
|
537
|
+
await readStarted;
|
|
538
|
+
const closePromise = listener.close();
|
|
539
|
+
await Promise.resolve();
|
|
540
|
+
|
|
541
|
+
expect(sock.end).not.toHaveBeenCalled();
|
|
542
|
+
|
|
543
|
+
if (!releaseRead) {
|
|
544
|
+
throw new Error("Expected read receipt release callback to be initialized");
|
|
545
|
+
}
|
|
546
|
+
releaseRead();
|
|
547
|
+
await closePromise;
|
|
548
|
+
|
|
549
|
+
expect(onMessage).toHaveBeenCalledTimes(1);
|
|
550
|
+
expect(sock.sendMessage).toHaveBeenCalledWith("999@s.whatsapp.net", {
|
|
551
|
+
text: "pong",
|
|
552
|
+
});
|
|
553
|
+
expect(sock.end).toHaveBeenCalledTimes(1);
|
|
554
|
+
expect(sock.sendMessage.mock.invocationCallOrder.at(0)).toBeLessThan(
|
|
555
|
+
sock.end.mock.invocationCallOrder.at(0),
|
|
556
|
+
);
|
|
557
|
+
} finally {
|
|
558
|
+
vi.useRealTimers();
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
it("retries timed-out sends on the same socket without clearing the socket ref", async () => {
|
|
563
|
+
const onMessage = vi.fn(async () => undefined);
|
|
564
|
+
const socketRef = createSocketRef();
|
|
565
|
+
const { listener, sock, inbound } = await primeInboundReplyHandle({
|
|
566
|
+
onMessage,
|
|
567
|
+
socketRef,
|
|
568
|
+
upsertId: "timeout-retry",
|
|
569
|
+
retryPolicy: {
|
|
570
|
+
initialMs: 1,
|
|
571
|
+
maxMs: 1,
|
|
572
|
+
factor: 1,
|
|
573
|
+
jitter: 0,
|
|
574
|
+
maxAttempts: 2,
|
|
575
|
+
},
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
sock.sendMessage
|
|
579
|
+
.mockRejectedValueOnce(new Error("operation timed out"))
|
|
580
|
+
.mockResolvedValueOnce({ key: { id: "after-timeout" } });
|
|
581
|
+
|
|
582
|
+
await inbound?.reply("pong");
|
|
583
|
+
|
|
584
|
+
expect(sock.sendMessage).toHaveBeenNthCalledWith(1, "999@s.whatsapp.net", {
|
|
585
|
+
text: "pong",
|
|
586
|
+
});
|
|
587
|
+
expect(sock.sendMessage).toHaveBeenNthCalledWith(2, "999@s.whatsapp.net", {
|
|
588
|
+
text: "pong",
|
|
589
|
+
});
|
|
590
|
+
expect(socketRef.current).toBe(sock);
|
|
591
|
+
expect(sleepWithAbortMock).toHaveBeenCalledTimes(1);
|
|
592
|
+
|
|
593
|
+
await listener.close();
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
it("bounds reconnect-gap retries even when reconnect attempts are unlimited", async () => {
|
|
597
|
+
const onMessage = vi.fn(async () => undefined);
|
|
598
|
+
const socketRef = createSocketRef();
|
|
599
|
+
const { listener, inbound } = await primeInboundReplyHandle({
|
|
600
|
+
onMessage,
|
|
601
|
+
socketRef,
|
|
602
|
+
upsertId: "unlimited-reconnect-send-bound",
|
|
603
|
+
retryPolicy: {
|
|
604
|
+
initialMs: 1,
|
|
605
|
+
maxMs: 1,
|
|
606
|
+
factor: 1,
|
|
607
|
+
jitter: 0,
|
|
608
|
+
maxAttempts: 0,
|
|
609
|
+
},
|
|
610
|
+
useCurrentSock: true,
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
socketRef.current = null;
|
|
614
|
+
|
|
615
|
+
await expect(inbound?.reply("pong")).rejects.toThrow(
|
|
616
|
+
"no active socket - reconnection in progress",
|
|
617
|
+
);
|
|
618
|
+
expect(sleepWithAbortMock).toHaveBeenCalledTimes(11);
|
|
619
|
+
|
|
620
|
+
await listener.close();
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
it("deduplicates redelivered messages by id", async () => {
|
|
624
|
+
const onMessage = vi.fn(async () => {
|
|
625
|
+
return;
|
|
626
|
+
});
|
|
627
|
+
|
|
628
|
+
const { listener, sock } = await startInboxMonitor(onMessage as InboxOnMessage);
|
|
629
|
+
const upsert = buildNotifyMessageUpsert({
|
|
630
|
+
id: nextMessageId("dedupe"),
|
|
631
|
+
remoteJid: "999@s.whatsapp.net",
|
|
632
|
+
text: "ping",
|
|
633
|
+
timestamp: 1_700_000_000,
|
|
634
|
+
pushName: "Tester",
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
sock.ev.emit("messages.upsert", upsert);
|
|
638
|
+
sock.ev.emit("messages.upsert", upsert);
|
|
639
|
+
await waitForMessageCalls(onMessage, 1);
|
|
640
|
+
|
|
641
|
+
expect(onMessage).toHaveBeenCalledTimes(1);
|
|
642
|
+
|
|
643
|
+
await listener.close();
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
it("retries redelivered messages after an explicit retryable inbound failure", async () => {
|
|
647
|
+
let attempts = 0;
|
|
648
|
+
const onMessage = vi.fn(async () => {
|
|
649
|
+
attempts += 1;
|
|
650
|
+
if (attempts === 1) {
|
|
651
|
+
throw new WhatsAppRetryableInboundError("retry me");
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
const { listener, sock } = await startInboxMonitor(onMessage as InboxOnMessage);
|
|
656
|
+
const upsert = buildNotifyMessageUpsert({
|
|
657
|
+
id: nextMessageId("retryable-dedupe"),
|
|
658
|
+
remoteJid: "999@s.whatsapp.net",
|
|
659
|
+
text: "ping",
|
|
660
|
+
timestamp: 1_700_000_000,
|
|
661
|
+
pushName: "Tester",
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
sock.ev.emit("messages.upsert", upsert);
|
|
665
|
+
await waitForMessageCalls(onMessage, 1);
|
|
666
|
+
|
|
667
|
+
sock.ev.emit("messages.upsert", upsert);
|
|
668
|
+
await waitForMessageCalls(onMessage, 2);
|
|
669
|
+
|
|
670
|
+
await listener.close();
|
|
671
|
+
});
|
|
672
|
+
|
|
673
|
+
it("resolves LID JIDs using Baileys LID mapping store", async () => {
|
|
674
|
+
const onMessage = vi.fn(async () => {
|
|
675
|
+
return;
|
|
676
|
+
});
|
|
677
|
+
|
|
678
|
+
const { listener, sock } = await startInboxMonitor(onMessage as InboxOnMessage);
|
|
679
|
+
const getPNForLID = vi.spyOn(sock.signalRepository.lidMapping, "getPNForLID");
|
|
680
|
+
sock.signalRepository.lidMapping.getPNForLID.mockResolvedValueOnce("999:0@s.whatsapp.net");
|
|
681
|
+
const upsert = buildNotifyMessageUpsert({
|
|
682
|
+
id: nextMessageId("lid-store"),
|
|
683
|
+
remoteJid: "999@lid",
|
|
684
|
+
text: "ping",
|
|
685
|
+
timestamp: 1_700_000_000,
|
|
686
|
+
pushName: "Tester",
|
|
687
|
+
});
|
|
688
|
+
|
|
689
|
+
sock.ev.emit("messages.upsert", upsert);
|
|
690
|
+
await waitForMessageCalls(onMessage, 1);
|
|
691
|
+
|
|
692
|
+
expect(getPNForLID).toHaveBeenCalledWith("999@lid");
|
|
693
|
+
const inbound = inboundMessage(onMessage);
|
|
694
|
+
expect(inbound.body).toBe("ping");
|
|
695
|
+
expect(inbound.from).toBe("+999");
|
|
696
|
+
expect(inbound.to).toBe("+123");
|
|
697
|
+
|
|
698
|
+
await listener.close();
|
|
699
|
+
});
|
|
700
|
+
|
|
701
|
+
it("resolves LID JIDs via authDir mapping files", async () => {
|
|
702
|
+
const onMessage = vi.fn(async () => {
|
|
703
|
+
return;
|
|
704
|
+
});
|
|
705
|
+
fsSync.writeFileSync(
|
|
706
|
+
path.join(getAuthDir(), "lid-mapping-555_reverse.json"),
|
|
707
|
+
JSON.stringify("1555"),
|
|
708
|
+
);
|
|
709
|
+
|
|
710
|
+
const { listener, sock } = await startInboxMonitor(onMessage as InboxOnMessage);
|
|
711
|
+
const getPNForLID = vi.spyOn(sock.signalRepository.lidMapping, "getPNForLID");
|
|
712
|
+
const upsert = buildNotifyMessageUpsert({
|
|
713
|
+
id: nextMessageId("lid-authdir"),
|
|
714
|
+
remoteJid: "555@lid",
|
|
715
|
+
text: "ping",
|
|
716
|
+
timestamp: 1_700_000_000,
|
|
717
|
+
pushName: "Tester",
|
|
718
|
+
});
|
|
719
|
+
|
|
720
|
+
sock.ev.emit("messages.upsert", upsert);
|
|
721
|
+
await waitForMessageCalls(onMessage, 1);
|
|
722
|
+
|
|
723
|
+
const inbound = inboundMessage(onMessage);
|
|
724
|
+
expect(inbound.body).toBe("ping");
|
|
725
|
+
expect(inbound.from).toBe("+1555");
|
|
726
|
+
expect(inbound.to).toBe("+123");
|
|
727
|
+
expect(getPNForLID).not.toHaveBeenCalled();
|
|
728
|
+
|
|
729
|
+
await listener.close();
|
|
730
|
+
});
|
|
731
|
+
|
|
732
|
+
it("resolves group participant LID JIDs via Baileys mapping", async () => {
|
|
733
|
+
const onMessage = vi.fn(async () => {
|
|
734
|
+
return;
|
|
735
|
+
});
|
|
736
|
+
|
|
737
|
+
const { listener, sock } = await startInboxMonitor(onMessage as InboxOnMessage);
|
|
738
|
+
const getPNForLID = vi.spyOn(sock.signalRepository.lidMapping, "getPNForLID");
|
|
739
|
+
sock.signalRepository.lidMapping.getPNForLID.mockResolvedValueOnce("444:0@s.whatsapp.net");
|
|
740
|
+
const upsert = buildNotifyMessageUpsert({
|
|
741
|
+
id: nextMessageId("group-lid"),
|
|
742
|
+
remoteJid: "123@g.us",
|
|
743
|
+
participant: "444@lid",
|
|
744
|
+
text: "ping",
|
|
745
|
+
timestamp: 1_700_000_000,
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
sock.ev.emit("messages.upsert", upsert);
|
|
749
|
+
await waitForMessageCalls(onMessage, 1);
|
|
750
|
+
|
|
751
|
+
expect(getPNForLID).toHaveBeenCalledWith("444@lid");
|
|
752
|
+
const inbound = inboundMessage(onMessage);
|
|
753
|
+
expect(inbound.body).toBe("ping");
|
|
754
|
+
expect(inbound.from).toBe("123@g.us");
|
|
755
|
+
expect(inbound.senderE164).toBe("+444");
|
|
756
|
+
expect(inbound.chatType).toBe("group");
|
|
757
|
+
|
|
758
|
+
await listener.close();
|
|
759
|
+
});
|
|
760
|
+
|
|
761
|
+
it("does not block follow-up messages when handler is pending", async () => {
|
|
762
|
+
let resolveFirst: (() => void) | null = null;
|
|
763
|
+
const onMessage = vi.fn(async () => {
|
|
764
|
+
if (!resolveFirst) {
|
|
765
|
+
await new Promise<void>((resolve) => {
|
|
766
|
+
resolveFirst = resolve;
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
const { listener, sock } = await startInboxMonitor(onMessage as InboxOnMessage);
|
|
772
|
+
const upsert = {
|
|
773
|
+
type: "notify",
|
|
774
|
+
messages: [
|
|
775
|
+
{
|
|
776
|
+
key: { id: "abc1", fromMe: false, remoteJid: "999@s.whatsapp.net" },
|
|
777
|
+
message: { conversation: "ping" },
|
|
778
|
+
messageTimestamp: 1_700_000_000,
|
|
779
|
+
},
|
|
780
|
+
{
|
|
781
|
+
key: { id: "abc2", fromMe: false, remoteJid: "999@s.whatsapp.net" },
|
|
782
|
+
message: { conversation: "pong" },
|
|
783
|
+
messageTimestamp: 1_700_000_001,
|
|
784
|
+
},
|
|
785
|
+
],
|
|
786
|
+
};
|
|
787
|
+
|
|
788
|
+
sock.ev.emit("messages.upsert", upsert);
|
|
789
|
+
await waitForMessageCalls(onMessage, 2);
|
|
790
|
+
|
|
791
|
+
expect(onMessage).toHaveBeenCalledTimes(2);
|
|
792
|
+
|
|
793
|
+
(resolveFirst as (() => void) | null)?.();
|
|
794
|
+
await listener.close();
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
it("captures reply context from quoted messages", async () => {
|
|
798
|
+
await expectQuotedReplyContext({ conversation: "original" });
|
|
799
|
+
});
|
|
800
|
+
|
|
801
|
+
it("captures reply context from wrapped quoted messages", async () => {
|
|
802
|
+
await expectQuotedReplyContext({
|
|
803
|
+
viewOnceMessageV2Extension: {
|
|
804
|
+
message: { conversation: "original" },
|
|
805
|
+
},
|
|
806
|
+
});
|
|
807
|
+
});
|
|
808
|
+
|
|
809
|
+
it("captures reply context from botInvokeMessage wrapped quoted messages", async () => {
|
|
810
|
+
await expectQuotedReplyContext({
|
|
811
|
+
botInvokeMessage: {
|
|
812
|
+
message: { conversation: "original" },
|
|
813
|
+
},
|
|
814
|
+
});
|
|
815
|
+
});
|
|
816
|
+
|
|
817
|
+
it("captures reply context from groupMentionedMessage wrapped quoted messages", async () => {
|
|
818
|
+
await expectQuotedReplyContext({
|
|
819
|
+
groupMentionedMessage: {
|
|
820
|
+
message: { conversation: "original" },
|
|
821
|
+
},
|
|
822
|
+
});
|
|
823
|
+
});
|
|
824
|
+
});
|