@skycodee/baileys 8.0.1 → 8.0.2
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/package.json +4 -4
- package/src/Defaults/baileys-version.json +1 -0
- package/src/Defaults/index.ts +149 -0
- package/src/Signal/Group/ciphertext-message.ts +9 -0
- package/src/Signal/Group/group-session-builder.ts +56 -0
- package/src/Signal/Group/group_cipher.ts +117 -0
- package/src/Signal/Group/index.ts +11 -0
- package/src/Signal/Group/keyhelper.ts +28 -0
- package/src/Signal/Group/sender-chain-key.ts +34 -0
- package/src/Signal/Group/sender-key-distribution-message.ts +95 -0
- package/src/Signal/Group/sender-key-message.ts +96 -0
- package/src/Signal/Group/sender-key-name.ts +66 -0
- package/src/Signal/Group/sender-key-record.ts +69 -0
- package/src/Signal/Group/sender-key-state.ts +134 -0
- package/src/Signal/Group/sender-message-key.ts +36 -0
- package/src/Signal/libsignal.ts +521 -0
- package/src/Signal/lid-mapping.ts +328 -0
- package/src/Socket/Client/index.ts +2 -0
- package/src/Socket/Client/types.ts +22 -0
- package/src/Socket/Client/websocket.ts +63 -0
- package/src/Socket/business.ts +424 -0
- package/src/Socket/chats.ts +1272 -0
- package/src/Socket/communities.ts +477 -0
- package/src/Socket/groups.ts +361 -0
- package/src/Socket/index.ts +15 -0
- package/src/Socket/messages-recv.ts +1668 -0
- package/src/Socket/messages-send.ts +1296 -0
- package/src/Socket/mex.ts +58 -0
- package/src/Socket/newsletter.ts +229 -0
- package/src/Socket/socket.ts +1184 -0
- package/src/Types/Auth.ts +116 -0
- package/src/Types/Bussines.ts +20 -0
- package/src/Types/Call.ts +15 -0
- package/src/Types/Chat.ts +138 -0
- package/src/Types/Contact.ts +24 -0
- package/src/Types/Events.ts +159 -0
- package/src/Types/GroupMetadata.ts +70 -0
- package/src/Types/Label.ts +48 -0
- package/src/Types/LabelAssociation.ts +35 -0
- package/src/Types/Message.ts +380 -0
- package/src/Types/Newsletter.ts +98 -0
- package/src/Types/Product.ts +85 -0
- package/src/Types/Signal.ts +76 -0
- package/src/Types/Socket.ts +150 -0
- package/src/Types/State.ts +43 -0
- package/src/Types/USync.ts +27 -0
- package/src/Types/globals.d.ts +8 -0
- package/src/Types/index.ts +67 -0
- package/src/Utils/auth-utils.ts +368 -0
- package/src/Utils/browser-utils.ts +31 -0
- package/src/Utils/business.ts +286 -0
- package/src/Utils/chat-utils.ts +974 -0
- package/src/Utils/crypto.ts +147 -0
- package/src/Utils/decode-wa-message.ts +355 -0
- package/src/Utils/event-buffer.ts +692 -0
- package/src/Utils/generics.ts +474 -0
- package/src/Utils/history.ts +161 -0
- package/src/Utils/identity-change-handler.ts +83 -0
- package/src/Utils/index.ts +19 -0
- package/src/Utils/link-preview.ts +111 -0
- package/src/Utils/logger.ts +13 -0
- package/src/Utils/lt-hash.ts +8 -0
- package/src/Utils/make-mutex.ts +42 -0
- package/src/Utils/message-retry-manager.ts +295 -0
- package/src/Utils/messages-media.ts +984 -0
- package/src/Utils/messages.ts +1097 -0
- package/src/Utils/noise-handler.ts +268 -0
- package/src/Utils/pre-key-manager.ts +126 -0
- package/src/Utils/process-message.ts +670 -0
- package/src/Utils/reporting-utils.ts +344 -0
- package/src/Utils/signal.ts +214 -0
- package/src/Utils/sync-action-utils.ts +74 -0
- package/src/Utils/tc-token-utils.ts +34 -0
- package/src/Utils/use-multi-file-auth-state.ts +136 -0
- package/src/Utils/validate-connection.ts +253 -0
- package/src/WABinary/constants.ts +1305 -0
- package/src/WABinary/decode.ts +308 -0
- package/src/WABinary/encode.ts +258 -0
- package/src/WABinary/generic-utils.ts +141 -0
- package/src/WABinary/index.ts +5 -0
- package/src/WABinary/jid-utils.ts +128 -0
- package/src/WABinary/types.ts +17 -0
- package/src/WAM/BinaryInfo.ts +12 -0
- package/src/WAM/constants.ts +22889 -0
- package/src/WAM/encode.ts +169 -0
- package/src/WAM/index.ts +3 -0
- package/src/WAUSync/Protocols/USyncContactProtocol.ts +32 -0
- package/src/WAUSync/Protocols/USyncDeviceProtocol.ts +78 -0
- package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.ts +35 -0
- package/src/WAUSync/Protocols/USyncStatusProtocol.ts +44 -0
- package/src/WAUSync/Protocols/UsyncBotProfileProtocol.ts +76 -0
- package/src/WAUSync/Protocols/UsyncLIDProtocol.ts +33 -0
- package/src/WAUSync/Protocols/index.ts +4 -0
- package/src/WAUSync/USyncQuery.ts +133 -0
- package/src/WAUSync/USyncUser.ts +32 -0
- package/src/WAUSync/index.ts +3 -0
- package/src/__tests__/Signal/Group/sender-key-state-regression.test.ts +22 -0
- package/src/__tests__/Signal/lid-mapping.test.ts +47 -0
- package/src/__tests__/Socket/identity-change-handling.test.ts +207 -0
- package/src/__tests__/TestUtils/session.ts +47 -0
- package/src/__tests__/Utils/generics.test.ts +70 -0
- package/src/__tests__/Utils/history.test.ts +362 -0
- package/src/__tests__/Utils/messages-media.test.ts +405 -0
- package/src/__tests__/Utils/noise-handler.test.ts +339 -0
- package/src/__tests__/Utils/process-message.test.ts +138 -0
- package/src/__tests__/Utils/process-sync-action.test.ts +359 -0
- package/src/__tests__/Utils/reporting-utils.test.ts +160 -0
- package/src/__tests__/Utils/signal.test.ts +69 -0
- package/src/__tests__/Utils/sync-action-utils.test.ts +183 -0
- package/src/__tests__/binary/connection-deadlock.test.ts +54 -0
- package/src/__tests__/e2e/fetch-wa-web-version.test-e2e.ts +60 -0
- package/src/__tests__/e2e/send-receive-message.test-e2e.ts +644 -0
- package/src/__tests__/proto-tojson-long.test.ts +31 -0
- package/src/index.ts +13 -0
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
import { jest } from '@jest/globals'
|
|
2
|
+
import { proto } from '../../../WAProto/index.js'
|
|
3
|
+
import type { BaileysEventEmitter, ChatMutation, Contact } from '../../Types'
|
|
4
|
+
import { LabelAssociationType } from '../../Types/LabelAssociation'
|
|
5
|
+
import { processSyncAction } from '../../Utils/chat-utils'
|
|
6
|
+
import type { ILogger } from '../../Utils/logger'
|
|
7
|
+
|
|
8
|
+
const createMockEventEmitter = () => {
|
|
9
|
+
const emittedEvents: Array<{ event: string; data: unknown }> = []
|
|
10
|
+
const emit = jest.fn((event: string, data: unknown) => {
|
|
11
|
+
emittedEvents.push({ event, data })
|
|
12
|
+
return true
|
|
13
|
+
})
|
|
14
|
+
return {
|
|
15
|
+
emit,
|
|
16
|
+
emittedEvents,
|
|
17
|
+
on: jest.fn(),
|
|
18
|
+
off: jest.fn(),
|
|
19
|
+
removeAllListeners: jest.fn()
|
|
20
|
+
} as unknown as BaileysEventEmitter & { emittedEvents: typeof emittedEvents }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const createMockLogger = (): ILogger =>
|
|
24
|
+
({
|
|
25
|
+
warn: jest.fn(),
|
|
26
|
+
info: jest.fn(),
|
|
27
|
+
debug: jest.fn(),
|
|
28
|
+
error: jest.fn(),
|
|
29
|
+
trace: jest.fn(),
|
|
30
|
+
child: jest.fn(function (this: ILogger) {
|
|
31
|
+
return this
|
|
32
|
+
}),
|
|
33
|
+
level: 'silent'
|
|
34
|
+
}) as unknown as ILogger
|
|
35
|
+
|
|
36
|
+
const createSyncAction = (
|
|
37
|
+
action: proto.ISyncActionValue,
|
|
38
|
+
index: string[] = ['type', 'id', 'msgId', '0']
|
|
39
|
+
): ChatMutation => ({
|
|
40
|
+
syncAction: { value: action },
|
|
41
|
+
index
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const mockMe: Contact = { id: 'me@s.whatsapp.net', name: 'Test User' }
|
|
45
|
+
|
|
46
|
+
describe('processSyncAction', () => {
|
|
47
|
+
let ev: ReturnType<typeof createMockEventEmitter>
|
|
48
|
+
let logger: ILogger
|
|
49
|
+
|
|
50
|
+
beforeEach(() => {
|
|
51
|
+
jest.clearAllMocks()
|
|
52
|
+
ev = createMockEventEmitter()
|
|
53
|
+
logger = createMockLogger()
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
describe('muteAction', () => {
|
|
57
|
+
it('emits chats.update with muteEndTime when muted', () => {
|
|
58
|
+
const syncAction = createSyncAction({ muteAction: { muted: true, muteEndTimestamp: 1700000000 } }, [
|
|
59
|
+
'mute',
|
|
60
|
+
'chat123@s.whatsapp.net'
|
|
61
|
+
])
|
|
62
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
63
|
+
expect(ev.emit).toHaveBeenCalledWith(
|
|
64
|
+
'chats.update',
|
|
65
|
+
expect.arrayContaining([expect.objectContaining({ id: 'chat123@s.whatsapp.net', muteEndTime: 1700000000 })])
|
|
66
|
+
)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('emits null muteEndTime when unmuted', () => {
|
|
70
|
+
const syncAction = createSyncAction({ muteAction: { muted: false, muteEndTimestamp: 0 } }, [
|
|
71
|
+
'mute',
|
|
72
|
+
'chat123@s.whatsapp.net'
|
|
73
|
+
])
|
|
74
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
75
|
+
expect(ev.emit).toHaveBeenCalledWith(
|
|
76
|
+
'chats.update',
|
|
77
|
+
expect.arrayContaining([expect.objectContaining({ muteEndTime: null })])
|
|
78
|
+
)
|
|
79
|
+
})
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
describe('archiveChatAction', () => {
|
|
83
|
+
it('emits chats.update with archived true/false', () => {
|
|
84
|
+
const archived = createSyncAction({ archiveChatAction: { archived: true } }, ['archive', 'chat@s.whatsapp.net'])
|
|
85
|
+
processSyncAction(archived, ev, mockMe, undefined, logger)
|
|
86
|
+
expect(ev.emit).toHaveBeenCalledWith(
|
|
87
|
+
'chats.update',
|
|
88
|
+
expect.arrayContaining([expect.objectContaining({ archived: true })])
|
|
89
|
+
)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('handles type fallback without archiveChatAction', () => {
|
|
93
|
+
const syncAction = createSyncAction({}, ['archive', 'chat@s.whatsapp.net'])
|
|
94
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
95
|
+
expect(ev.emit).toHaveBeenCalledWith(
|
|
96
|
+
'chats.update',
|
|
97
|
+
expect.arrayContaining([expect.objectContaining({ archived: true })])
|
|
98
|
+
)
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
describe('markChatAsReadAction', () => {
|
|
103
|
+
it('emits unreadCount 0 when read is true', () => {
|
|
104
|
+
const read = createSyncAction({ markChatAsReadAction: { read: true } }, ['markRead', 'chat@s.whatsapp.net'])
|
|
105
|
+
processSyncAction(read, ev, mockMe, undefined, logger)
|
|
106
|
+
expect(ev.emit).toHaveBeenCalledWith(
|
|
107
|
+
'chats.update',
|
|
108
|
+
expect.arrayContaining([expect.objectContaining({ unreadCount: 0 })])
|
|
109
|
+
)
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('emits unreadCount -1 when read is false', () => {
|
|
113
|
+
const unread = createSyncAction({ markChatAsReadAction: { read: false } }, ['markRead', 'chat@s.whatsapp.net'])
|
|
114
|
+
processSyncAction(unread, ev, mockMe, undefined, logger)
|
|
115
|
+
expect(ev.emit).toHaveBeenCalledWith(
|
|
116
|
+
'chats.update',
|
|
117
|
+
expect.arrayContaining([expect.objectContaining({ unreadCount: -1 })])
|
|
118
|
+
)
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it('emits null unreadCount during initial sync when already read', () => {
|
|
122
|
+
const syncAction = createSyncAction({ markChatAsReadAction: { read: true } }, ['markRead', 'chat@s.whatsapp.net'])
|
|
123
|
+
processSyncAction(syncAction, ev, mockMe, { accountSettings: { unarchiveChats: false } }, logger)
|
|
124
|
+
expect(ev.emit).toHaveBeenCalledWith(
|
|
125
|
+
'chats.update',
|
|
126
|
+
expect.arrayContaining([expect.objectContaining({ unreadCount: null })])
|
|
127
|
+
)
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
describe('deleteMessageForMeAction', () => {
|
|
132
|
+
it('emits messages.delete with correct key', () => {
|
|
133
|
+
const syncAction = createSyncAction({ deleteMessageForMeAction: { deleteMedia: false } }, [
|
|
134
|
+
'deleteMessageForMe',
|
|
135
|
+
'chat@s.whatsapp.net',
|
|
136
|
+
'msg456',
|
|
137
|
+
'1'
|
|
138
|
+
])
|
|
139
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
140
|
+
expect(ev.emit).toHaveBeenCalledWith('messages.delete', {
|
|
141
|
+
keys: [{ remoteJid: 'chat@s.whatsapp.net', id: 'msg456', fromMe: true }]
|
|
142
|
+
})
|
|
143
|
+
})
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
describe('contactAction', () => {
|
|
147
|
+
it('emits contacts.upsert and lid-mapping.update for PN user with LID', () => {
|
|
148
|
+
const syncAction = createSyncAction({ contactAction: { fullName: 'John', lidJid: '123@lid', pnJid: null } }, [
|
|
149
|
+
'contact',
|
|
150
|
+
'5511999@s.whatsapp.net'
|
|
151
|
+
])
|
|
152
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
153
|
+
expect(ev.emit).toHaveBeenCalledWith('contacts.upsert', [
|
|
154
|
+
{
|
|
155
|
+
id: '5511999@s.whatsapp.net',
|
|
156
|
+
name: 'John',
|
|
157
|
+
lid: '123@lid',
|
|
158
|
+
phoneNumber: '5511999@s.whatsapp.net'
|
|
159
|
+
}
|
|
160
|
+
])
|
|
161
|
+
expect(ev.emit).toHaveBeenCalledWith('lid-mapping.update', {
|
|
162
|
+
lid: '123@lid',
|
|
163
|
+
pn: '5511999@s.whatsapp.net'
|
|
164
|
+
})
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
it('does not emit events when id is missing', () => {
|
|
168
|
+
const syncAction = createSyncAction({ contactAction: { fullName: 'John', lidJid: '123@lid', pnJid: null } }, [
|
|
169
|
+
'contact',
|
|
170
|
+
''
|
|
171
|
+
])
|
|
172
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
173
|
+
expect(ev.emittedEvents.filter(e => e.event === 'contacts.upsert')).toHaveLength(0)
|
|
174
|
+
})
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
describe('pushNameSetting', () => {
|
|
178
|
+
it('emits creds.update when name differs', () => {
|
|
179
|
+
const syncAction = createSyncAction({ pushNameSetting: { name: 'New' } }, ['pushName'])
|
|
180
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
181
|
+
expect(ev.emit).toHaveBeenCalledWith('creds.update', { me: { ...mockMe, name: 'New' } })
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
it('does not emit when name is same or empty', () => {
|
|
185
|
+
const same = createSyncAction({ pushNameSetting: { name: 'Test User' } }, ['pushName'])
|
|
186
|
+
processSyncAction(same, ev, mockMe, undefined, logger)
|
|
187
|
+
expect(ev.emit).not.toHaveBeenCalled()
|
|
188
|
+
})
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
describe('pinAction', () => {
|
|
192
|
+
it('emits chats.update with pinned timestamp or null', () => {
|
|
193
|
+
const syncAction: ChatMutation = {
|
|
194
|
+
syncAction: { value: { pinAction: { pinned: true }, timestamp: 1700000000 } },
|
|
195
|
+
index: ['pin', 'chat@s.whatsapp.net']
|
|
196
|
+
}
|
|
197
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
198
|
+
expect(ev.emit).toHaveBeenCalledWith(
|
|
199
|
+
'chats.update',
|
|
200
|
+
expect.arrayContaining([expect.objectContaining({ pinned: 1700000000 })])
|
|
201
|
+
)
|
|
202
|
+
})
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
describe('starAction', () => {
|
|
206
|
+
it('emits messages.update with starred value', () => {
|
|
207
|
+
const syncAction = createSyncAction({ starAction: { starred: true } }, [
|
|
208
|
+
'star',
|
|
209
|
+
'chat@s.whatsapp.net',
|
|
210
|
+
'msg',
|
|
211
|
+
'1'
|
|
212
|
+
])
|
|
213
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
214
|
+
expect(ev.emit).toHaveBeenCalledWith('messages.update', [
|
|
215
|
+
{
|
|
216
|
+
key: { remoteJid: 'chat@s.whatsapp.net', id: 'msg', fromMe: true },
|
|
217
|
+
update: { starred: true }
|
|
218
|
+
}
|
|
219
|
+
])
|
|
220
|
+
})
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
describe('deleteChatAction', () => {
|
|
224
|
+
it('emits chats.delete when not initial sync', () => {
|
|
225
|
+
const syncAction = createSyncAction({ deleteChatAction: { messageRange: null } }, [
|
|
226
|
+
'deleteChat',
|
|
227
|
+
'chat@s.whatsapp.net'
|
|
228
|
+
])
|
|
229
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
230
|
+
expect(ev.emit).toHaveBeenCalledWith('chats.delete', ['chat@s.whatsapp.net'])
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
it('does NOT emit during initial sync', () => {
|
|
234
|
+
const syncAction = createSyncAction({ deleteChatAction: { messageRange: null } }, [
|
|
235
|
+
'deleteChat',
|
|
236
|
+
'chat@s.whatsapp.net'
|
|
237
|
+
])
|
|
238
|
+
processSyncAction(syncAction, ev, mockMe, { accountSettings: { unarchiveChats: false } }, logger)
|
|
239
|
+
expect(ev.emit).not.toHaveBeenCalled()
|
|
240
|
+
})
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
describe('labelEditAction', () => {
|
|
244
|
+
it('emits labels.edit', () => {
|
|
245
|
+
const syncAction = createSyncAction(
|
|
246
|
+
{ labelEditAction: { name: 'Important', color: 1, deleted: false, predefinedId: 5 } },
|
|
247
|
+
['label', 'label123']
|
|
248
|
+
)
|
|
249
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
250
|
+
expect(ev.emit).toHaveBeenCalledWith('labels.edit', {
|
|
251
|
+
id: 'label123',
|
|
252
|
+
name: 'Important',
|
|
253
|
+
color: 1,
|
|
254
|
+
deleted: false,
|
|
255
|
+
predefinedId: '5'
|
|
256
|
+
})
|
|
257
|
+
})
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
describe('labelAssociationAction', () => {
|
|
261
|
+
it('emits labels.association for chat label', () => {
|
|
262
|
+
const syncAction = createSyncAction({ labelAssociationAction: { labeled: true } }, [
|
|
263
|
+
LabelAssociationType.Chat,
|
|
264
|
+
'label123',
|
|
265
|
+
'chat@s.whatsapp.net'
|
|
266
|
+
])
|
|
267
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
268
|
+
expect(ev.emit).toHaveBeenCalledWith('labels.association', {
|
|
269
|
+
type: 'add',
|
|
270
|
+
association: { type: LabelAssociationType.Chat, chatId: 'chat@s.whatsapp.net', labelId: 'label123' }
|
|
271
|
+
})
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
it('emits labels.association for message label', () => {
|
|
275
|
+
const syncAction = createSyncAction({ labelAssociationAction: { labeled: true } }, [
|
|
276
|
+
LabelAssociationType.Message,
|
|
277
|
+
'label123',
|
|
278
|
+
'chat@s.whatsapp.net',
|
|
279
|
+
'msg789'
|
|
280
|
+
])
|
|
281
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
282
|
+
expect(ev.emit).toHaveBeenCalledWith('labels.association', {
|
|
283
|
+
type: 'add',
|
|
284
|
+
association: {
|
|
285
|
+
type: LabelAssociationType.Message,
|
|
286
|
+
chatId: 'chat@s.whatsapp.net',
|
|
287
|
+
messageId: 'msg789',
|
|
288
|
+
labelId: 'label123'
|
|
289
|
+
}
|
|
290
|
+
})
|
|
291
|
+
})
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
describe('pnForLidChatAction', () => {
|
|
295
|
+
it('emits lid-mapping.update when pnJid is present', () => {
|
|
296
|
+
const syncAction = createSyncAction({ pnForLidChatAction: { pnJid: '5511999@s.whatsapp.net' } }, [
|
|
297
|
+
'pnForLid',
|
|
298
|
+
'123@lid'
|
|
299
|
+
])
|
|
300
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
301
|
+
expect(ev.emit).toHaveBeenCalledWith('lid-mapping.update', { lid: '123@lid', pn: '5511999@s.whatsapp.net' })
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
it('does not emit when pnJid is missing', () => {
|
|
305
|
+
const syncAction = createSyncAction({ pnForLidChatAction: { pnJid: '' } }, ['pnForLid', '123@lid'])
|
|
306
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
307
|
+
expect(ev.emit).not.toHaveBeenCalled()
|
|
308
|
+
})
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
describe('lockChatAction', () => {
|
|
312
|
+
it('emits chats.lock', () => {
|
|
313
|
+
const syncAction = createSyncAction({ lockChatAction: { locked: true } }, ['lockChat', 'chat@s.whatsapp.net'])
|
|
314
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
315
|
+
expect(ev.emit).toHaveBeenCalledWith('chats.lock', { id: 'chat@s.whatsapp.net', locked: true })
|
|
316
|
+
})
|
|
317
|
+
})
|
|
318
|
+
|
|
319
|
+
describe('lidContactAction', () => {
|
|
320
|
+
it('emits contacts.upsert with LID contact', () => {
|
|
321
|
+
const syncAction = createSyncAction({ lidContactAction: { fullName: 'LID Contact' } }, ['lidContact', '123@lid'])
|
|
322
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
323
|
+
expect(ev.emit).toHaveBeenCalledWith('contacts.upsert', [
|
|
324
|
+
{
|
|
325
|
+
id: '123@lid',
|
|
326
|
+
name: 'LID Contact',
|
|
327
|
+
lid: '123@lid',
|
|
328
|
+
phoneNumber: undefined
|
|
329
|
+
}
|
|
330
|
+
])
|
|
331
|
+
})
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
describe('settings actions', () => {
|
|
335
|
+
it('localeSetting emits settings.update', () => {
|
|
336
|
+
const syncAction = createSyncAction({ localeSetting: { locale: 'en-US' } }, ['locale'])
|
|
337
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
338
|
+
expect(ev.emit).toHaveBeenCalledWith('settings.update', { setting: 'locale', value: 'en-US' })
|
|
339
|
+
})
|
|
340
|
+
|
|
341
|
+
it('unarchiveChatsSetting emits creds.update', () => {
|
|
342
|
+
const syncAction = createSyncAction({ unarchiveChatsSetting: { unarchiveChats: true } }, ['unarchiveChats'])
|
|
343
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
344
|
+
expect(ev.emit).toHaveBeenCalledWith('creds.update', { accountSettings: { unarchiveChats: true } })
|
|
345
|
+
})
|
|
346
|
+
})
|
|
347
|
+
|
|
348
|
+
describe('unprocessable actions', () => {
|
|
349
|
+
it('logs debug for unknown action', () => {
|
|
350
|
+
const syncAction = createSyncAction({ unknownAction: {} } as unknown as proto.ISyncActionValue, [
|
|
351
|
+
'unknown',
|
|
352
|
+
'id123'
|
|
353
|
+
])
|
|
354
|
+
processSyncAction(syncAction, ev, mockMe, undefined, logger)
|
|
355
|
+
expect(logger.debug).toHaveBeenCalledWith({ syncAction, id: 'id123' }, 'unprocessable update')
|
|
356
|
+
expect(ev.emit).not.toHaveBeenCalled()
|
|
357
|
+
})
|
|
358
|
+
})
|
|
359
|
+
})
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { randomBytes } from 'crypto'
|
|
2
|
+
import { proto } from '../../../WAProto'
|
|
3
|
+
import type { WAMessageKey } from '../../Types'
|
|
4
|
+
import { getMessageReportingToken, shouldIncludeReportingToken } from '../../Utils/reporting-utils'
|
|
5
|
+
import type { BinaryNode } from '../../WABinary'
|
|
6
|
+
|
|
7
|
+
describe('Reporting Utils', () => {
|
|
8
|
+
describe('shouldIncludeReportingToken', () => {
|
|
9
|
+
const includedMessages: [string, proto.IMessage][] = [
|
|
10
|
+
['conversation', { conversation: 'Hello' }],
|
|
11
|
+
['extendedTextMessage', { extendedTextMessage: { text: 'Link' } }],
|
|
12
|
+
['imageMessage', { imageMessage: { url: 'url', mimetype: 'image/jpeg' } }],
|
|
13
|
+
['videoMessage', { videoMessage: { url: 'url', mimetype: 'video/mp4' } }],
|
|
14
|
+
['documentMessage', { documentMessage: { url: 'url', mimetype: 'application/pdf' } }],
|
|
15
|
+
['audioMessage', { audioMessage: { url: 'url', mimetype: 'audio/ogg' } }],
|
|
16
|
+
['stickerMessage', { stickerMessage: { url: 'url', mimetype: 'image/webp' } }]
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
const excludedMessages: [string, proto.IMessage][] = [
|
|
20
|
+
['reactionMessage', { reactionMessage: { key: { id: 'id' }, text: '👍' } }],
|
|
21
|
+
[
|
|
22
|
+
'encReactionMessage',
|
|
23
|
+
{
|
|
24
|
+
encReactionMessage: { targetMessageKey: { id: 'id' }, encPayload: Buffer.from('x'), encIv: Buffer.from('x') }
|
|
25
|
+
}
|
|
26
|
+
],
|
|
27
|
+
[
|
|
28
|
+
'pollUpdateMessage',
|
|
29
|
+
{
|
|
30
|
+
pollUpdateMessage: {
|
|
31
|
+
pollCreationMessageKey: { id: 'id' },
|
|
32
|
+
vote: { encPayload: Buffer.from('x'), encIv: Buffer.from('x') }
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
],
|
|
36
|
+
[
|
|
37
|
+
'encEventResponseMessage',
|
|
38
|
+
{
|
|
39
|
+
encEventResponseMessage: {
|
|
40
|
+
eventCreationMessageKey: { id: 'id' },
|
|
41
|
+
encPayload: Buffer.from('x'),
|
|
42
|
+
encIv: Buffer.from('x')
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
]
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
it.each(includedMessages)('should return true for %s', (_, message) => {
|
|
49
|
+
expect(shouldIncludeReportingToken(message)).toBe(true)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it.each(excludedMessages)('should return false for %s', (_, message) => {
|
|
53
|
+
expect(shouldIncludeReportingToken(message)).toBe(false)
|
|
54
|
+
})
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
describe('getMessageReportingToken', () => {
|
|
58
|
+
const createKey = (overrides?: Partial<WAMessageKey>): WAMessageKey => ({
|
|
59
|
+
id: 'test-id',
|
|
60
|
+
fromMe: true,
|
|
61
|
+
remoteJid: '123@s.whatsapp.net',
|
|
62
|
+
...overrides
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
const withSecret = (content: proto.IMessage, secret = randomBytes(32)): proto.IMessage => ({
|
|
66
|
+
...content,
|
|
67
|
+
messageContextInfo: { messageSecret: secret }
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
const encode = (msg: proto.IMessage) => Buffer.from(proto.Message.encode(msg).finish())
|
|
71
|
+
const getToken = (result: BinaryNode | null): Buffer | undefined => {
|
|
72
|
+
const content = result?.content
|
|
73
|
+
if (Array.isArray(content) && content[0]) {
|
|
74
|
+
return content[0].content as Buffer | undefined
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return undefined
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
it('should return null when message secret is missing', async () => {
|
|
81
|
+
const msg: proto.IMessage = { conversation: 'Hello' }
|
|
82
|
+
expect(await getMessageReportingToken(encode(msg), msg, createKey())).toBeNull()
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('should return null when message key id is missing', async () => {
|
|
86
|
+
const msg = withSecret({ conversation: 'Hello' })
|
|
87
|
+
expect(await getMessageReportingToken(encode(msg), msg, createKey({ id: '' }))).toBeNull()
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('should return valid reporting node structure', async () => {
|
|
91
|
+
const msg = withSecret({ conversation: 'Hello' })
|
|
92
|
+
const result = await getMessageReportingToken(encode(msg), msg, createKey())
|
|
93
|
+
|
|
94
|
+
expect(result).toMatchObject({ tag: 'reporting', attrs: {} })
|
|
95
|
+
const content = result?.content as any[]
|
|
96
|
+
expect(content).toHaveLength(1)
|
|
97
|
+
expect(content[0]).toMatchObject({ tag: 'reporting_token', attrs: { v: '2' } })
|
|
98
|
+
expect(content[0].content).toHaveLength(16)
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('should produce consistent tokens for the same input', async () => {
|
|
102
|
+
const secret = randomBytes(32)
|
|
103
|
+
const msg = withSecret({ conversation: 'Test' }, secret)
|
|
104
|
+
const encoded = encode(msg)
|
|
105
|
+
const key = createKey()
|
|
106
|
+
|
|
107
|
+
const token1 = getToken(await getMessageReportingToken(encoded, msg, key))
|
|
108
|
+
const token2 = getToken(await getMessageReportingToken(encoded, msg, key))
|
|
109
|
+
expect(token1).toEqual(token2)
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
const uniquenessTests: [string, () => [proto.IMessage, WAMessageKey, proto.IMessage, WAMessageKey]][] = [
|
|
113
|
+
[
|
|
114
|
+
'different secrets',
|
|
115
|
+
() => {
|
|
116
|
+
const msg1 = withSecret({ conversation: 'Same' }, randomBytes(32))
|
|
117
|
+
const msg2 = withSecret({ conversation: 'Same' }, randomBytes(32))
|
|
118
|
+
return [msg1, createKey(), msg2, createKey()]
|
|
119
|
+
}
|
|
120
|
+
],
|
|
121
|
+
[
|
|
122
|
+
'different message IDs',
|
|
123
|
+
() => {
|
|
124
|
+
const secret = randomBytes(32)
|
|
125
|
+
const msg = withSecret({ conversation: 'Same' }, secret)
|
|
126
|
+
return [msg, createKey({ id: 'id-1' }), msg, createKey({ id: 'id-2' })]
|
|
127
|
+
}
|
|
128
|
+
],
|
|
129
|
+
[
|
|
130
|
+
'different remoteJids',
|
|
131
|
+
() => {
|
|
132
|
+
const secret = randomBytes(32)
|
|
133
|
+
const msg = withSecret({ conversation: 'Same' }, secret)
|
|
134
|
+
return [
|
|
135
|
+
msg,
|
|
136
|
+
createKey({ remoteJid: '111@s.whatsapp.net' }),
|
|
137
|
+
msg,
|
|
138
|
+
createKey({ remoteJid: '222@s.whatsapp.net' })
|
|
139
|
+
]
|
|
140
|
+
}
|
|
141
|
+
]
|
|
142
|
+
]
|
|
143
|
+
|
|
144
|
+
it.each(uniquenessTests)('should produce different tokens for %s', async (_, setup) => {
|
|
145
|
+
const [msg1, key1, msg2, key2] = setup()
|
|
146
|
+
const token1 = getToken(await getMessageReportingToken(encode(msg1), msg1, key1))
|
|
147
|
+
const token2 = getToken(await getMessageReportingToken(encode(msg2), msg2, key2))
|
|
148
|
+
expect(token1).not.toEqual(token2)
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
it.each([
|
|
152
|
+
['group messages', { remoteJid: '123@g.us', participant: '456@s.whatsapp.net' }],
|
|
153
|
+
['incoming messages', { fromMe: false }]
|
|
154
|
+
])('should handle %s', async (_, keyOverrides) => {
|
|
155
|
+
const msg = withSecret({ conversation: 'Test' })
|
|
156
|
+
const result = await getMessageReportingToken(encode(msg), msg, createKey(keyOverrides))
|
|
157
|
+
expect(result?.tag).toBe('reporting')
|
|
158
|
+
})
|
|
159
|
+
})
|
|
160
|
+
})
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { jest } from '@jest/globals'
|
|
2
|
+
import type { SignalRepositoryWithLIDStore } from '../../Types'
|
|
3
|
+
import { parseAndInjectE2ESessions } from '../../Utils/signal'
|
|
4
|
+
import type { BinaryNode } from '../../WABinary/types'
|
|
5
|
+
|
|
6
|
+
describe('parseAndInjectE2ESessions', () => {
|
|
7
|
+
it('should process all user node', async () => {
|
|
8
|
+
const mockRepository = {
|
|
9
|
+
injectE2ESession: jest.fn<SignalRepositoryWithLIDStore['injectE2ESession']>()
|
|
10
|
+
} as jest.Mocked<Pick<SignalRepositoryWithLIDStore, 'injectE2ESession'>>
|
|
11
|
+
|
|
12
|
+
mockRepository.injectE2ESession.mockResolvedValue(undefined)
|
|
13
|
+
|
|
14
|
+
const createUserNode = (jid: string): BinaryNode => ({
|
|
15
|
+
tag: 'user',
|
|
16
|
+
attrs: { jid },
|
|
17
|
+
content: [
|
|
18
|
+
{
|
|
19
|
+
tag: 'skey',
|
|
20
|
+
attrs: {},
|
|
21
|
+
content: [
|
|
22
|
+
{ tag: 'id', attrs: {}, content: Buffer.from([0, 0, 1]) },
|
|
23
|
+
{ tag: 'value', attrs: {}, content: Buffer.alloc(33) },
|
|
24
|
+
{ tag: 'signature', attrs: {}, content: Buffer.alloc(64) }
|
|
25
|
+
]
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
tag: 'key',
|
|
29
|
+
attrs: {},
|
|
30
|
+
content: [
|
|
31
|
+
{ tag: 'id', attrs: {}, content: Buffer.from([0, 0, 2]) },
|
|
32
|
+
{ tag: 'value', attrs: {}, content: Buffer.alloc(33) }
|
|
33
|
+
]
|
|
34
|
+
},
|
|
35
|
+
{ tag: 'identity', attrs: {}, content: Buffer.alloc(32) },
|
|
36
|
+
{ tag: 'registration', attrs: {}, content: Buffer.alloc(4) }
|
|
37
|
+
]
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
const mockNode: BinaryNode = {
|
|
41
|
+
tag: 'iq',
|
|
42
|
+
attrs: {},
|
|
43
|
+
content: [
|
|
44
|
+
{
|
|
45
|
+
tag: 'list',
|
|
46
|
+
attrs: {},
|
|
47
|
+
content: [
|
|
48
|
+
createUserNode('user1@s.whatsapp.net'),
|
|
49
|
+
createUserNode('user2@s.whatsapp.net'),
|
|
50
|
+
createUserNode('user3@s.whatsapp.net')
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
await parseAndInjectE2ESessions(mockNode, mockRepository as any)
|
|
57
|
+
|
|
58
|
+
expect(mockRepository.injectE2ESession).toHaveBeenCalledTimes(3)
|
|
59
|
+
expect(mockRepository.injectE2ESession).toHaveBeenCalledWith(
|
|
60
|
+
expect.objectContaining({ jid: 'user1@s.whatsapp.net' })
|
|
61
|
+
)
|
|
62
|
+
expect(mockRepository.injectE2ESession).toHaveBeenCalledWith(
|
|
63
|
+
expect.objectContaining({ jid: 'user2@s.whatsapp.net' })
|
|
64
|
+
)
|
|
65
|
+
expect(mockRepository.injectE2ESession).toHaveBeenCalledWith(
|
|
66
|
+
expect.objectContaining({ jid: 'user3@s.whatsapp.net' })
|
|
67
|
+
)
|
|
68
|
+
})
|
|
69
|
+
})
|