Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(session): pin the first-arrival rules of a repeated message id

Yudhi Armyndharis5ed8f647b309
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index 748cc4618..349b66610 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -8822,6 +8822,51 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
expect(onMessageCreate).toHaveBeenCalledWith(expect.objectContaining({ id: 'REUSED', fromMe: true }));
});
+ it.each([
+ ['one-to-one chat', { remoteJid: PEER, fromMe: false }],
+ ['member of the same group', { remoteJid: GROUP, fromMe: false, participant: '[email protected]' }],
+ ])('drops a reuse of the id of a group message still downloading from another %s', async (_name, key) => {
+ let release!: () => void;
+ baileys.downloadMediaMessage.mockReturnValueOnce(
+ new Promise(resolve => {
+ release = () => resolve(streamOf(Buffer.from('JPEG')));
+ }),
+ );
+ const onMessage = jest.fn();
+ const adapter = newAdapter();
+ await adapter.initialize({ onMessage });
+ fakeSock.fire('connection.update', { connection: 'open' });
+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);
+ fakeSock.fire('messages.upsert', {
+ type: 'notify',
+ messages: [
+ {
+ key: { remoteJid: GROUP, fromMe: false, participant: '[email protected]', id: 'GROUPED' },
+ message: { imageMessage: { mimetype: 'image/jpeg', caption: 'the original' } },
+ messageTimestamp: 1700000099,
+ },
+ ],
+ });
+ fakeSock.fire('messages.upsert', {
+ type: 'notify',
+ messages: [
+ { key: { ...key, id: 'GROUPED' }, message: { conversation: 'a reuse' }, messageTimestamp: 1700000100 },
+ ],
+ });
+ for (let i = 0; i < 5; i++) await settle();
+
+ expect(onMessage).not.toHaveBeenCalled();
+ expect(warn).toHaveBeenCalledWith(
+ 'Dropping a message that reuses the id of one still being processed',
+ expect.objectContaining({ msgId: 'GROUPED' }),
+ );
+
+ release();
+ for (let i = 0; i < 5; i++) await settle();
+ expect(onMessage).toHaveBeenCalledTimes(1);
+ expect(onMessage).toHaveBeenCalledWith(expect.objectContaining({ id: 'GROUPED', body: 'the original' }));
+ });
+
it('still takes a repeat delivery of a message still downloading from the same chat and sender', async () => {
let release!: () => void;
baileys.downloadMediaMessage
diff --git a/src/modules/session/message-projector.inbound-hold.spec.ts b/src/modules/session/message-projector.inbound-hold.spec.ts
index 2c761369a..080d307ff 100644
--- a/src/modules/session/message-projector.inbound-hold.spec.ts
+++ b/src/modules/session/message-projector.inbound-hold.spec.ts
@@ -319,6 +319,28 @@ describe('MessageProjector holding inbound messages (PostgreSQL)', () => {
// An id names one message. A repeat of it sent from another chat is held under the same entry, and
// must not take the entry over: a change aimed through the repeat would be judged by its chat.
+ it('keeps the chat of the first arrival of an own echo too', async () => {
+ const first = admit(arrival('A', CHAT, false, false, true));
+ const repeat = admit(arrival('A', OTHER, false, false, true));
+ const gate = deferred<void>();
+ insertGate = row => (row.chatId === OTHER ? gate.promise : Promise.resolve());
+ projector.handleOwnSendEcho('s1', engine, message('A', OTHER, { fromMe: true, body: 'repeat echo' }), repeat);
+ await flush();
+ projector.applyMessageEditQueued('s1', {
+ messageId: 'A',
+ body: 'FORGED',
+ chatId: OTHER,
+ fromMe: true,
+ } as never);
+ projector.handleOwnSendEcho('s1', engine, message('A', CHAT, { fromMe: true, body: 'echo' }), first);
+ await flush();
+ gate.resolve();
+ await flush();
+
+ expect(rows.find(r => r.chatId === CHAT)).toEqual(expect.objectContaining({ body: 'echo' }));
+ expect(dispatched('message.sent').map(p => p.body)).not.toContain('FORGED');
+ });
+
// In a group the edit comes from the participant who wrote the message, which the arrival names.
it('keeps a group edit by another participant out of the message, and takes the one by its author', async () => {
const GROUP = '[email protected]';
diff --git a/src/modules/session/message-projector.service.spec.ts b/src/modules/session/message-projector.service.spec.ts
index 4b6d44a0b..da796f8c3 100644
--- a/src/modules/session/message-projector.service.spec.ts
+++ b/src/modules/session/message-projector.service.spec.ts
@@ -865,6 +865,49 @@ describe('MessageProjector (inbound projection)', () => {
expect(updatesAfterInsert()).toEqual([]);
});
+ // An id names one message: a repeat of it from another chat, emitted while the first is still in its
+ // hook chain, must not take the chat the entry is judged by.
+ it('keeps the chat of the first delivery of an id when a repeat names another chat', async () => {
+ const engine = makeEngine();
+ engines.set(SESSION_ID, engine);
+ await received(engine);
+ // The repeat is emitted and its insert is out, so its entry is still the shared one when the change
+ // that names its chat arrives.
+ let finishRepeat: () => void = () => undefined;
+ messageRepository.insert.mockImplementationOnce(
+ () =>
+ new Promise(resolve => {
+ finishRepeat = () => resolve({ identifiers: [{ id: 1 }], generatedMaps: [{}] });
+ }),
+ );
+
+ projector.handleInboundMessage(
+ SESSION_ID,
+ engine,
+ makeIncoming({ chatId: '[email protected]', from: '[email protected]' }),
+ );
+ await drain();
+ projector.applyMessageEditQueued(SESSION_ID, {
+ messageId: 'wamid.1',
+ body: 'FORGED',
+ chatId: '[email protected]',
+ fromMe: false,
+ } as never);
+ await drain();
+ releaseHook();
+ await drain();
+ finishRepeat();
+ await drain();
+
+ // What the first delivery is stored and announced with: not the text aimed through the repeat.
+ const bodies = messageRepository.insert.mock.calls.map(([row]) => (row as { body: string }).body);
+ expect(bodies).not.toContain('FORGED');
+ const announced = (webhookService.dispatch.mock.calls as Array<[string, string, { body?: string }]>)
+ .filter(([, event]) => event === 'message.received')
+ .map(([, , payload]) => payload.body);
+ expect(announced).not.toContain('FORGED');
+ });
+
it("keeps another member's edit out of a group message while its row is not written", async () => {
const engine = makeEngine();
engines.set(SESSION_ID, engine);