Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test: cover the status own-send recording and the ack-filter suppression

Yudhi Armyndharis8394054b8c5b
diff --git a/src/engine/adapters/baileys-status-own-send.spec.ts b/src/engine/adapters/baileys-status-own-send.spec.ts
new file mode 100644
index 000000000..7f510fe2b
--- /dev/null
+++ b/src/engine/adapters/baileys-status-own-send.spec.ts
@@ -0,0 +1,52 @@
+import { BaileysStatus, type BaileysStatusHost } from './baileys-status';
+import type { WASocket } from '@whiskeysockets/baileys';
+
+/**
+ * A status post and a status revoke both go through `sock.sendMessage` directly rather than the
+ * messaging delegate's chokepoint, so each has to record its own id. Without that, the library's
+ * `append` echo of the post is not recognised as this session's and is reported as an inbound
+ * message; nothing else in the suite covered either call.
+ */
+function makeStatus(sendMessage: jest.Mock): { status: BaileysStatus; remembered: string[] } {
+ const remembered: string[] = [];
+ const host = {
+ ensureReady: () => undefined,
+ getSocket: () => ({ sendMessage }) as unknown as WASocket,
+ toEngineJid: (jid: string) => jid,
+ normalizedSelfJid: () => '[email protected]',
+ toUnixSeconds: () => 1700000000,
+ rememberOwnSend: (id: string | null | undefined) => {
+ if (id) remembered.push(id);
+ },
+ } as unknown as BaileysStatusHost;
+ return { status: new BaileysStatus(host), remembered };
+}
+
+describe('BaileysStatus records the ids of the statuses this session sends', () => {
+ it('remembers the id of a posted status', async () => {
+ const sendMessage = jest.fn().mockResolvedValue({ key: { id: 'STATUS-1' }, messageTimestamp: 1700000000 });
+ const { status, remembered } = makeStatus(sendMessage);
+
+ await status.postTextStatus('hello', { recipients: ['[email protected]'] });
+
+ expect(remembered).toEqual(['STATUS-1']);
+ });
+
+ it('remembers the id of a status revoke', async () => {
+ const sendMessage = jest.fn().mockResolvedValue({ key: { id: 'REVOKE-1' } });
+ const { status, remembered } = makeStatus(sendMessage);
+
+ await status.deleteStatus('STATUS-1');
+
+ expect(remembered).toEqual(['REVOKE-1']);
+ });
+
+ it('records nothing when the send echoes no id back', async () => {
+ const sendMessage = jest.fn().mockResolvedValue(undefined);
+ const { status, remembered } = makeStatus(sendMessage);
+
+ await status.deleteStatus('STATUS-1');
+
+ expect(remembered).toEqual([]);
+ });
+});
diff --git a/src/modules/webhook/filters/filter-evaluator.spec.ts b/src/modules/webhook/filters/filter-evaluator.spec.ts
index fa084b0d4..d290dec40 100644
--- a/src/modules/webhook/filters/filter-evaluator.spec.ts
+++ b/src/modules/webhook/filters/filter-evaluator.spec.ts
@@ -243,6 +243,21 @@ describe('evaluateFilters', () => {
expect(evaluateFilters(deny, 'message.received', msg({ chatId: '[email protected]' }))).toBe(true);
});
+ // The ack and failure events carry no conversation at all: their payload is the shape the
+ // projector builds, `{ id, messageId, status, ack }`. A chatId condition therefore SUPPRESSES
+ // them rather than scoping them, which is a real surprise for anyone allowlisting a group, so
+ // it is pinned here and warned about in docs/06 rather than left to be discovered.
+ it('suppresses message.ack and message.failed, whose payload carries no conversation', () => {
+ const ackPayload = { id: 'M1', messageId: 'M1', status: 'delivered', ack: 3 };
+ const allow = filters({ field: 'chatId', operator: 'is', value: ['[email protected]'] });
+ expect(evaluateFilters(allow, 'message.ack', ackPayload)).toBe(false);
+ expect(evaluateFilters(allow, 'message.failed', { ...ackPayload, status: 'failed' })).toBe(false);
+
+ // And the exclusion direction delivers them, for the same reason: the field is not there.
+ const deny = filters({ field: 'chatId', operator: 'isNot', value: ['[email protected]'] });
+ expect(evaluateFilters(deny, 'message.ack', ackPayload)).toBe(true);
+ });
+
it('scopes message.revoked / edited payloads that only carry chatId', () => {
const f = filters({ field: 'chatId', operator: 'is', value: ['[email protected]'] });
expect(evaluateFilters(f, 'message.revoked', { chatId: '[email protected]' })).toBe(true);