@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.
Files changed (159) hide show
  1. package/action-runtime-api.ts +1 -0
  2. package/action-runtime.runtime.ts +1 -0
  3. package/api.ts +67 -0
  4. package/auth-presence.ts +80 -0
  5. package/autobot.plugin.json +23 -0
  6. package/channel-config-api.ts +1 -0
  7. package/channel-plugin-api.ts +3 -0
  8. package/config-api.ts +4 -0
  9. package/constants.ts +1 -0
  10. package/contract-api.ts +29 -0
  11. package/directory-contract-api.ts +4 -0
  12. package/doctor-contract-api.ts +8 -0
  13. package/index.ts +16 -0
  14. package/legacy-session-surface-api.ts +6 -0
  15. package/legacy-state-migrations-api.ts +1 -0
  16. package/light-runtime-api.ts +12 -0
  17. package/login-qr-api.ts +1 -0
  18. package/login-qr-runtime.ts +23 -0
  19. package/outbound-payload-test-api.ts +1 -0
  20. package/package.json +76 -0
  21. package/runtime-api.ts +84 -0
  22. package/secret-contract-api.ts +4 -0
  23. package/security-contract-api.ts +4 -0
  24. package/setup-entry.ts +21 -0
  25. package/setup-plugin-api.ts +3 -0
  26. package/src/account-config.ts +77 -0
  27. package/src/account-ids.ts +17 -0
  28. package/src/account-types.ts +5 -0
  29. package/src/accounts.ts +176 -0
  30. package/src/action-runtime-target-auth.ts +27 -0
  31. package/src/action-runtime.ts +76 -0
  32. package/src/active-listener.ts +17 -0
  33. package/src/agent-tools-login.ts +113 -0
  34. package/src/approval-auth.ts +27 -0
  35. package/src/auth-store.runtime.ts +1 -0
  36. package/src/auth-store.ts +494 -0
  37. package/src/auto-reply/config.runtime.ts +16 -0
  38. package/src/auto-reply/constants.ts +1 -0
  39. package/src/auto-reply/deliver-reply.ts +332 -0
  40. package/src/auto-reply/loggers.ts +6 -0
  41. package/src/auto-reply/mentions.ts +131 -0
  42. package/src/auto-reply/monitor/ack-reaction.ts +99 -0
  43. package/src/auto-reply/monitor/audio-preflight.runtime.ts +9 -0
  44. package/src/auto-reply/monitor/broadcast.ts +153 -0
  45. package/src/auto-reply/monitor/commands.ts +19 -0
  46. package/src/auto-reply/monitor/echo.ts +64 -0
  47. package/src/auto-reply/monitor/group-activation.runtime.ts +1 -0
  48. package/src/auto-reply/monitor/group-activation.ts +73 -0
  49. package/src/auto-reply/monitor/group-gating.runtime.ts +8 -0
  50. package/src/auto-reply/monitor/group-gating.ts +218 -0
  51. package/src/auto-reply/monitor/group-members.ts +65 -0
  52. package/src/auto-reply/monitor/inbound-context.ts +92 -0
  53. package/src/auto-reply/monitor/inbound-dispatch.runtime.ts +22 -0
  54. package/src/auto-reply/monitor/inbound-dispatch.ts +749 -0
  55. package/src/auto-reply/monitor/last-route.ts +61 -0
  56. package/src/auto-reply/monitor/listener-log.ts +28 -0
  57. package/src/auto-reply/monitor/message-line.runtime.ts +38 -0
  58. package/src/auto-reply/monitor/message-line.ts +54 -0
  59. package/src/auto-reply/monitor/on-message.ts +333 -0
  60. package/src/auto-reply/monitor/peer.ts +17 -0
  61. package/src/auto-reply/monitor/process-message.ts +584 -0
  62. package/src/auto-reply/monitor/runtime-api.ts +36 -0
  63. package/src/auto-reply/monitor/status-reaction.ts +108 -0
  64. package/src/auto-reply/monitor-state.ts +114 -0
  65. package/src/auto-reply/monitor.ts +720 -0
  66. package/src/auto-reply/reply-resolver.runtime.ts +1 -0
  67. package/src/auto-reply/types.ts +48 -0
  68. package/src/auto-reply/util.ts +62 -0
  69. package/src/auto-reply.impl.ts +6 -0
  70. package/src/auto-reply.ts +1 -0
  71. package/src/channel-actions.runtime.ts +7 -0
  72. package/src/channel-actions.ts +85 -0
  73. package/src/channel-outbound.ts +87 -0
  74. package/src/channel-react-action.runtime.ts +10 -0
  75. package/src/channel-react-action.ts +247 -0
  76. package/src/channel.runtime.ts +117 -0
  77. package/src/channel.setup.ts +32 -0
  78. package/src/channel.ts +356 -0
  79. package/src/command-policy.ts +7 -0
  80. package/src/config-accessors.ts +22 -0
  81. package/src/config-schema.ts +6 -0
  82. package/src/config-ui-hints.ts +24 -0
  83. package/src/connection-controller-registry.ts +49 -0
  84. package/src/connection-controller.ts +680 -0
  85. package/src/creds-files.ts +19 -0
  86. package/src/creds-persistence.ts +71 -0
  87. package/src/directory-config.ts +40 -0
  88. package/src/doctor-contract.ts +11 -0
  89. package/src/doctor.ts +56 -0
  90. package/src/document-filename.ts +17 -0
  91. package/src/group-intro.ts +15 -0
  92. package/src/group-policy.ts +40 -0
  93. package/src/group-session-contract.ts +20 -0
  94. package/src/group-session-key.ts +42 -0
  95. package/src/heartbeat.ts +34 -0
  96. package/src/identity.ts +164 -0
  97. package/src/inbound/access-control.ts +187 -0
  98. package/src/inbound/dedupe.ts +132 -0
  99. package/src/inbound/extract.ts +484 -0
  100. package/src/inbound/lifecycle.ts +39 -0
  101. package/src/inbound/media.ts +128 -0
  102. package/src/inbound/monitor.ts +1042 -0
  103. package/src/inbound/outbound-mentions.ts +260 -0
  104. package/src/inbound/runtime-api.ts +7 -0
  105. package/src/inbound/save-media.runtime.ts +1 -0
  106. package/src/inbound/send-api.ts +203 -0
  107. package/src/inbound/send-result.ts +109 -0
  108. package/src/inbound/types.ts +107 -0
  109. package/src/inbound-policy.ts +215 -0
  110. package/src/inbound.ts +9 -0
  111. package/src/login-qr.ts +542 -0
  112. package/src/login.ts +83 -0
  113. package/src/media.ts +10 -0
  114. package/src/monitor-inbox.allows-messages-from-senders-allowfrom-list.test-support.ts +417 -0
  115. package/src/monitor-inbox.append-upsert.test-support.ts +133 -0
  116. package/src/monitor-inbox.blocks-messages-from-unauthorized-senders-not-allowfrom.test-support.ts +418 -0
  117. package/src/monitor-inbox.captures-media-path-image-messages.test-support.ts +308 -0
  118. package/src/monitor-inbox.streams-inbound-messages.test-support.ts +824 -0
  119. package/src/normalize-target.ts +148 -0
  120. package/src/normalize.ts +8 -0
  121. package/src/outbound-adapter.ts +36 -0
  122. package/src/outbound-base.ts +256 -0
  123. package/src/outbound-media-contract.ts +307 -0
  124. package/src/outbound-media.runtime.ts +41 -0
  125. package/src/outbound-send-deps.ts +1 -0
  126. package/src/outbound-test-support.ts +16 -0
  127. package/src/qa-driver.runtime.ts +189 -0
  128. package/src/qr-image.ts +1 -0
  129. package/src/qr-terminal.ts +1 -0
  130. package/src/quoted-message.ts +184 -0
  131. package/src/reaction-level.ts +24 -0
  132. package/src/reconnect.ts +55 -0
  133. package/src/resolve-outbound-target.ts +58 -0
  134. package/src/runtime-api.ts +59 -0
  135. package/src/runtime-group-policy.ts +16 -0
  136. package/src/runtime.ts +9 -0
  137. package/src/security-contract.ts +47 -0
  138. package/src/security-fix.ts +71 -0
  139. package/src/send.ts +342 -0
  140. package/src/session-contract.ts +43 -0
  141. package/src/session-errors.ts +125 -0
  142. package/src/session-route.ts +32 -0
  143. package/src/session.runtime.ts +8 -0
  144. package/src/session.ts +327 -0
  145. package/src/setup-core.ts +52 -0
  146. package/src/setup-finalize.ts +450 -0
  147. package/src/setup-surface.ts +71 -0
  148. package/src/setup-test-helpers.ts +217 -0
  149. package/src/shared.ts +291 -0
  150. package/src/socket-timing.ts +38 -0
  151. package/src/state-migrations.ts +55 -0
  152. package/src/status-issues.ts +185 -0
  153. package/src/system-prompt.ts +31 -0
  154. package/src/targets-runtime.ts +221 -0
  155. package/src/text-runtime.ts +18 -0
  156. package/src/vcard.ts +84 -0
  157. package/targets.ts +5 -0
  158. package/test-api.ts +2 -0
  159. package/tsconfig.json +16 -0
@@ -0,0 +1,417 @@
1
+ import "./monitor-inbox.test-harness.js";
2
+ import { describe, expect, it, vi } from "vitest";
3
+ import {
4
+ buildNotifyMessageUpsert,
5
+ expectPairingPromptSent,
6
+ getRecordChannelActivityMock,
7
+ installWebMonitorInboxUnitTestHooks,
8
+ mockLoadConfig,
9
+ settleInboundWork,
10
+ startInboxMonitor,
11
+ upsertPairingRequestMock,
12
+ waitForMessageCalls,
13
+ } from "./monitor-inbox.test-harness.js";
14
+
15
+ const nowSeconds = (offsetMs = 0) => Math.floor((Date.now() + offsetMs) / 1000);
16
+ const DEFAULT_MESSAGES_CFG = {
17
+ messagePrefix: undefined,
18
+ responsePrefix: undefined,
19
+ } as const;
20
+
21
+ function createAllowListConfig(allowFrom: string[]) {
22
+ return {
23
+ channels: {
24
+ whatsapp: {
25
+ allowFrom,
26
+ },
27
+ },
28
+ messages: DEFAULT_MESSAGES_CFG,
29
+ };
30
+ }
31
+
32
+ async function openInboxMonitor(onMessage = vi.fn()) {
33
+ const { listener, sock } = await startInboxMonitor(onMessage);
34
+ return { onMessage, listener, sock };
35
+ }
36
+
37
+ function expectOnlyOutboundChannelActivity(accountId = "default") {
38
+ const recordChannelActivityMock = getRecordChannelActivityMock();
39
+ expect(recordChannelActivityMock).toHaveBeenCalledWith({
40
+ channel: "whatsapp",
41
+ accountId,
42
+ direction: "outbound",
43
+ });
44
+ expect(recordChannelActivityMock).not.toHaveBeenCalledWith({
45
+ channel: "whatsapp",
46
+ accountId,
47
+ direction: "inbound",
48
+ });
49
+ }
50
+
51
+ async function expectOutboundDmSkipsPairing(params: {
52
+ selfChatMode: boolean;
53
+ messageId: string;
54
+ body: string;
55
+ }) {
56
+ mockLoadConfig.mockReturnValue({
57
+ channels: {
58
+ whatsapp: {
59
+ dmPolicy: "pairing",
60
+ selfChatMode: params.selfChatMode,
61
+ },
62
+ },
63
+ messages: DEFAULT_MESSAGES_CFG,
64
+ });
65
+
66
+ const onMessage = vi.fn();
67
+ const { listener, sock } = await startInboxMonitor(onMessage);
68
+
69
+ try {
70
+ sock.ev.emit("messages.upsert", {
71
+ type: "notify",
72
+ messages: [
73
+ {
74
+ key: {
75
+ id: params.messageId,
76
+ fromMe: true,
77
+ remoteJid: "999@s.whatsapp.net",
78
+ },
79
+ message: { conversation: params.body },
80
+ messageTimestamp: nowSeconds(),
81
+ },
82
+ ],
83
+ });
84
+ await settleInboundWork();
85
+
86
+ expect(onMessage).not.toHaveBeenCalled();
87
+ expect(upsertPairingRequestMock).not.toHaveBeenCalled();
88
+ expect(sock.sendMessage).not.toHaveBeenCalled();
89
+ } finally {
90
+ mockLoadConfig.mockReturnValue({
91
+ channels: { whatsapp: { allowFrom: ["*"] } },
92
+ messages: DEFAULT_MESSAGES_CFG,
93
+ });
94
+ await listener.close();
95
+ }
96
+ }
97
+
98
+ describe("web monitor inbox", () => {
99
+ installWebMonitorInboxUnitTestHooks();
100
+
101
+ it("allows messages from senders in allowFrom list", async () => {
102
+ mockLoadConfig.mockReturnValue(createAllowListConfig(["+111", "+999"]));
103
+
104
+ const { onMessage, listener, sock } = await openInboxMonitor();
105
+
106
+ const upsert = buildNotifyMessageUpsert({
107
+ id: "auth1",
108
+ remoteJid: "999@s.whatsapp.net",
109
+ text: "authorized message",
110
+ timestamp: nowSeconds(60_000),
111
+ });
112
+
113
+ sock.ev.emit("messages.upsert", upsert);
114
+ await waitForMessageCalls(onMessage, 1);
115
+
116
+ // Should call onMessage for authorized senders
117
+ expect(onMessage).toHaveBeenCalledWith(
118
+ expect.objectContaining({
119
+ body: "authorized message",
120
+ from: "+999",
121
+ senderE164: "+999",
122
+ }),
123
+ );
124
+
125
+ await listener.close();
126
+ });
127
+
128
+ it("allows same-phone messages even if not in allowFrom", async () => {
129
+ // Same-phone mode: when from === selfJid, should always be allowed
130
+ // This allows users to message themselves even with restrictive allowFrom
131
+ mockLoadConfig.mockReturnValue(createAllowListConfig(["+111"]));
132
+
133
+ const { onMessage, listener, sock } = await openInboxMonitor();
134
+
135
+ // Message from self (sock.user.id is "123@s.whatsapp.net" in mock)
136
+ const upsert = buildNotifyMessageUpsert({
137
+ id: "self1",
138
+ remoteJid: "123@s.whatsapp.net",
139
+ text: "self message",
140
+ timestamp: nowSeconds(60_000),
141
+ });
142
+
143
+ sock.ev.emit("messages.upsert", upsert);
144
+ await waitForMessageCalls(onMessage, 1);
145
+
146
+ // Should allow self-messages even if not in allowFrom
147
+ expect(onMessage).toHaveBeenCalledWith(
148
+ expect.objectContaining({ body: "self message", from: "+123" }),
149
+ );
150
+
151
+ await listener.close();
152
+ });
153
+
154
+ it("locks down when no config is present (pairing for unknown senders)", async () => {
155
+ // No config file => locked-down defaults apply (pairing for unknown senders)
156
+ mockLoadConfig.mockReturnValue({});
157
+ upsertPairingRequestMock
158
+ .mockResolvedValueOnce({ code: "PAIRCODE", created: true })
159
+ .mockResolvedValueOnce({ code: "PAIRCODE", created: false });
160
+
161
+ const { onMessage, listener, sock } = await openInboxMonitor();
162
+
163
+ // Message from someone else should be blocked
164
+ const upsertBlocked = buildNotifyMessageUpsert({
165
+ id: "no-config-1",
166
+ remoteJid: "999@s.whatsapp.net",
167
+ text: "ping",
168
+ timestamp: nowSeconds(),
169
+ });
170
+
171
+ sock.ev.emit("messages.upsert", upsertBlocked);
172
+ await vi.waitFor(
173
+ () => {
174
+ expect(sock.sendMessage).toHaveBeenCalledTimes(1);
175
+ },
176
+ { timeout: 5_000, interval: 5 },
177
+ );
178
+ expect(onMessage).not.toHaveBeenCalled();
179
+ expectPairingPromptSent(sock, "999@s.whatsapp.net", "+999");
180
+
181
+ const upsertBlockedAgain = buildNotifyMessageUpsert({
182
+ id: "no-config-1b",
183
+ remoteJid: "999@s.whatsapp.net",
184
+ text: "ping again",
185
+ timestamp: nowSeconds(),
186
+ });
187
+
188
+ sock.ev.emit("messages.upsert", upsertBlockedAgain);
189
+ await settleInboundWork();
190
+ expect(onMessage).not.toHaveBeenCalled();
191
+ expect(sock.sendMessage).toHaveBeenCalledTimes(1);
192
+
193
+ // Message from self should be allowed
194
+ const upsertSelf = buildNotifyMessageUpsert({
195
+ id: "no-config-2",
196
+ remoteJid: "123@s.whatsapp.net",
197
+ text: "self ping",
198
+ timestamp: nowSeconds(),
199
+ });
200
+
201
+ sock.ev.emit("messages.upsert", upsertSelf);
202
+ await waitForMessageCalls(onMessage, 1);
203
+
204
+ expect(onMessage).toHaveBeenCalledTimes(1);
205
+ expect(onMessage).toHaveBeenCalledWith(
206
+ expect.objectContaining({
207
+ body: "self ping",
208
+ from: "+123",
209
+ to: "+123",
210
+ }),
211
+ );
212
+
213
+ await listener.close();
214
+ });
215
+
216
+ it("skips pairing replies for outbound DMs in same-phone mode", async () => {
217
+ await expectOutboundDmSkipsPairing({
218
+ selfChatMode: true,
219
+ messageId: "fromme-1",
220
+ body: "hello",
221
+ });
222
+ });
223
+
224
+ it("skips pairing replies for outbound DMs when same-phone mode is disabled", async () => {
225
+ await expectOutboundDmSkipsPairing({
226
+ selfChatMode: false,
227
+ messageId: "fromme-2",
228
+ body: "hello again",
229
+ });
230
+ });
231
+
232
+ it("allows owner fromMe group commands when they were not sent by the gateway", async () => {
233
+ mockLoadConfig.mockReturnValue({
234
+ channels: {
235
+ whatsapp: {
236
+ groupPolicy: "open",
237
+ allowFrom: ["+123"],
238
+ },
239
+ },
240
+ messages: DEFAULT_MESSAGES_CFG,
241
+ });
242
+
243
+ const { onMessage, listener, sock } = await openInboxMonitor();
244
+
245
+ sock.ev.emit("messages.upsert", {
246
+ type: "notify",
247
+ messages: [
248
+ {
249
+ key: {
250
+ id: "owner-group-1",
251
+ fromMe: true,
252
+ remoteJid: "120363@g.us",
253
+ participant: "123@s.whatsapp.net",
254
+ },
255
+ message: { conversation: "/status" },
256
+ messageTimestamp: nowSeconds(),
257
+ pushName: "Owner",
258
+ },
259
+ ],
260
+ });
261
+ await waitForMessageCalls(onMessage, 1);
262
+
263
+ expect(onMessage).toHaveBeenCalledWith(
264
+ expect.objectContaining({
265
+ body: "/status",
266
+ chatType: "group",
267
+ from: "120363@g.us",
268
+ fromMe: true,
269
+ senderE164: "+123",
270
+ }),
271
+ );
272
+
273
+ await listener.close();
274
+ });
275
+
276
+ it("filters group fromMe echoes only when the gateway sent the matching message id", async () => {
277
+ mockLoadConfig.mockReturnValue({
278
+ channels: {
279
+ whatsapp: {
280
+ groupPolicy: "open",
281
+ allowFrom: ["+123"],
282
+ },
283
+ },
284
+ messages: DEFAULT_MESSAGES_CFG,
285
+ });
286
+
287
+ const onMessage = vi.fn();
288
+ const { listener, sock } = await startInboxMonitor(onMessage);
289
+
290
+ sock.sendMessage.mockResolvedValueOnce({ key: { id: "bot-group-echo-1" } });
291
+ await listener.sendMessage("120363@g.us", "gateway echo candidate");
292
+
293
+ sock.ev.emit("messages.upsert", {
294
+ type: "notify",
295
+ messages: [
296
+ {
297
+ key: {
298
+ id: "bot-group-echo-1",
299
+ fromMe: true,
300
+ remoteJid: "120363@g.us",
301
+ participant: "123@s.whatsapp.net",
302
+ },
303
+ message: { conversation: "gateway echo candidate" },
304
+ messageTimestamp: nowSeconds(),
305
+ pushName: "Owner",
306
+ },
307
+ ],
308
+ });
309
+ await settleInboundWork();
310
+
311
+ expect(onMessage).not.toHaveBeenCalled();
312
+ expectOnlyOutboundChannelActivity();
313
+
314
+ await listener.close();
315
+ });
316
+
317
+ it("filters self-chat DM fromMe echoes when the gateway sent the matching message id", async () => {
318
+ mockLoadConfig.mockReturnValue({
319
+ channels: {
320
+ whatsapp: {
321
+ selfChatMode: true,
322
+ allowFrom: ["+123"],
323
+ },
324
+ },
325
+ messages: DEFAULT_MESSAGES_CFG,
326
+ });
327
+
328
+ const onMessage = vi.fn();
329
+ const { listener, sock } = await startInboxMonitor(onMessage);
330
+
331
+ sock.sendMessage.mockResolvedValueOnce({ key: { id: "bot-dm-echo-1" } });
332
+ await listener.sendMessage("123@s.whatsapp.net", "self-chat reply");
333
+
334
+ sock.ev.emit("messages.upsert", {
335
+ type: "notify",
336
+ messages: [
337
+ {
338
+ key: {
339
+ id: "bot-dm-echo-1",
340
+ fromMe: true,
341
+ remoteJid: "123@s.whatsapp.net",
342
+ },
343
+ message: { conversation: "self-chat reply" },
344
+ messageTimestamp: nowSeconds(),
345
+ pushName: "Owner",
346
+ },
347
+ ],
348
+ });
349
+ await settleInboundWork();
350
+
351
+ expect(onMessage).not.toHaveBeenCalled();
352
+ expectOnlyOutboundChannelActivity();
353
+
354
+ await listener.close();
355
+ });
356
+
357
+ it("handles append messages by marking them read but skipping auto-reply", async () => {
358
+ const { onMessage, listener, sock } = await openInboxMonitor();
359
+ const staleTs = Math.floor(Date.now() / 1000) - 300;
360
+
361
+ const upsert = {
362
+ type: "append",
363
+ messages: [
364
+ {
365
+ key: {
366
+ id: "history1",
367
+ fromMe: false,
368
+ remoteJid: "999@s.whatsapp.net",
369
+ },
370
+ message: { conversation: "old message" },
371
+ messageTimestamp: staleTs,
372
+ pushName: "History Sender",
373
+ },
374
+ ],
375
+ };
376
+
377
+ sock.ev.emit("messages.upsert", upsert);
378
+ await vi.waitFor(
379
+ () => {
380
+ expect(sock.readMessages).toHaveBeenCalledWith([
381
+ {
382
+ remoteJid: "999@s.whatsapp.net",
383
+ id: "history1",
384
+ participant: undefined,
385
+ fromMe: false,
386
+ },
387
+ ]);
388
+ },
389
+ { timeout: 5_000, interval: 5 },
390
+ );
391
+
392
+ // Verify it WAS NOT passed to onMessage
393
+ expect(onMessage).not.toHaveBeenCalled();
394
+
395
+ await listener.close();
396
+ });
397
+
398
+ it("normalizes participant phone numbers to JIDs in sendReaction", async () => {
399
+ const { listener, sock } = await startInboxMonitor(vi.fn());
400
+
401
+ await listener.sendReaction("12345@g.us", "msg123", "👍", false, "+6421000000");
402
+
403
+ expect(sock.sendMessage).toHaveBeenCalledWith("12345@g.us", {
404
+ react: {
405
+ text: "👍",
406
+ key: {
407
+ remoteJid: "12345@g.us",
408
+ id: "msg123",
409
+ fromMe: false,
410
+ participant: "6421000000@s.whatsapp.net",
411
+ },
412
+ },
413
+ });
414
+
415
+ await listener.close();
416
+ });
417
+ });
@@ -0,0 +1,133 @@
1
+ import "./monitor-inbox.test-harness.js";
2
+ import { describe, expect, it, vi } from "vitest";
3
+ import {
4
+ installWebMonitorInboxUnitTestHooks,
5
+ settleInboundWork,
6
+ startInboxMonitor,
7
+ waitForMessageCalls,
8
+ } from "./monitor-inbox.test-harness.js";
9
+
10
+ describe("append upsert handling (#20952)", () => {
11
+ installWebMonitorInboxUnitTestHooks();
12
+
13
+ it("processes recent append messages (within 60s of connect)", async () => {
14
+ const onMessage = vi.fn(async () => {});
15
+ const { listener, sock } = await startInboxMonitor(onMessage);
16
+
17
+ // Timestamp ~5 seconds ago — recent, should be processed.
18
+ const recentTs = Math.floor(Date.now() / 1000) - 5;
19
+ sock.ev.emit("messages.upsert", {
20
+ type: "append",
21
+ messages: [
22
+ {
23
+ key: { id: "recent-1", fromMe: false, remoteJid: "120363@g.us" },
24
+ message: { conversation: "hello from group" },
25
+ messageTimestamp: recentTs,
26
+ pushName: "Tester",
27
+ },
28
+ ],
29
+ });
30
+ await waitForMessageCalls(onMessage, 1);
31
+
32
+ expect(onMessage).toHaveBeenCalledTimes(1);
33
+
34
+ await listener.close();
35
+ });
36
+
37
+ it("skips stale append messages (older than 60s before connect)", async () => {
38
+ const onMessage = vi.fn(async () => {});
39
+ const { listener, sock } = await startInboxMonitor(onMessage);
40
+
41
+ // Timestamp 5 minutes ago — stale history sync, should be skipped.
42
+ const staleTs = Math.floor(Date.now() / 1000) - 300;
43
+ sock.ev.emit("messages.upsert", {
44
+ type: "append",
45
+ messages: [
46
+ {
47
+ key: { id: "stale-1", fromMe: false, remoteJid: "120363@g.us" },
48
+ message: { conversation: "old history sync" },
49
+ messageTimestamp: staleTs,
50
+ pushName: "OldTester",
51
+ },
52
+ ],
53
+ });
54
+ await settleInboundWork();
55
+
56
+ expect(onMessage).not.toHaveBeenCalled();
57
+
58
+ await listener.close();
59
+ });
60
+
61
+ it("skips append messages with NaN/non-finite timestamps", async () => {
62
+ const onMessage = vi.fn(async () => {});
63
+ const { listener, sock } = await startInboxMonitor(onMessage);
64
+
65
+ // NaN timestamp should be treated as 0 (stale) and skipped.
66
+ sock.ev.emit("messages.upsert", {
67
+ type: "append",
68
+ messages: [
69
+ {
70
+ key: { id: "nan-1", fromMe: false, remoteJid: "120363@g.us" },
71
+ message: { conversation: "bad timestamp" },
72
+ messageTimestamp: Number.NaN,
73
+ pushName: "BadTs",
74
+ },
75
+ ],
76
+ });
77
+ await settleInboundWork();
78
+
79
+ expect(onMessage).not.toHaveBeenCalled();
80
+
81
+ await listener.close();
82
+ });
83
+
84
+ it("handles Long-like protobuf timestamps correctly", async () => {
85
+ const onMessage = vi.fn(async () => {});
86
+ const { listener, sock } = await startInboxMonitor(onMessage);
87
+
88
+ // Baileys can deliver messageTimestamp as a Long object (from protobufjs).
89
+ // Number(longObj) calls valueOf() and returns the numeric value.
90
+ const recentTs = Math.floor(Date.now() / 1000) - 5;
91
+ const longLike = { low: recentTs, high: 0, unsigned: true, valueOf: () => recentTs };
92
+ sock.ev.emit("messages.upsert", {
93
+ type: "append",
94
+ messages: [
95
+ {
96
+ key: { id: "long-1", fromMe: false, remoteJid: "120363@g.us" },
97
+ message: { conversation: "long timestamp" },
98
+ messageTimestamp: longLike,
99
+ pushName: "LongTs",
100
+ },
101
+ ],
102
+ });
103
+ await waitForMessageCalls(onMessage, 1);
104
+
105
+ expect(onMessage).toHaveBeenCalledTimes(1);
106
+
107
+ await listener.close();
108
+ });
109
+
110
+ it("always processes notify messages regardless of timestamp", async () => {
111
+ const onMessage = vi.fn(async () => {});
112
+ const { listener, sock } = await startInboxMonitor(onMessage);
113
+
114
+ // Very old timestamp but type=notify — should always be processed.
115
+ const oldTs = Math.floor(Date.now() / 1000) - 86400;
116
+ sock.ev.emit("messages.upsert", {
117
+ type: "notify",
118
+ messages: [
119
+ {
120
+ key: { id: "notify-1", fromMe: false, remoteJid: "999@s.whatsapp.net" },
121
+ message: { conversation: "normal message" },
122
+ messageTimestamp: oldTs,
123
+ pushName: "User",
124
+ },
125
+ ],
126
+ });
127
+ await waitForMessageCalls(onMessage, 1);
128
+
129
+ expect(onMessage).toHaveBeenCalledTimes(1);
130
+
131
+ await listener.close();
132
+ });
133
+ });