Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(message): pin history query parsing and the disconnect abort

Yudhi Armyndharisfa1b7090be66
diff --git a/src/modules/message/message.controller.spec.ts b/src/modules/message/message.controller.spec.ts
index 67db8c510..404e8fcf6 100644
--- a/src/modules/message/message.controller.spec.ts
+++ b/src/modules/message/message.controller.spec.ts
@@ -3,6 +3,7 @@ import { RESPONSE_PASSTHROUGH_METADATA } from '@nestjs/common/constants';
import { Test } from '@nestjs/testing';
import request from 'supertest';
import type { Server } from 'http';
+import { EventEmitter } from 'events';
import { MessageController } from './message.controller';
import { MessageService } from './message.service';
import { BulkMessageService } from './bulk-message.service';
@@ -258,6 +259,137 @@ describe('MessageController - stored history for a chat-restricted key', () => {
});
});
+/**
+ * The stored list narrows by sender, window and page with options the controller parses itself: a
+ * dropped `from` returns every sender's history to a caller that asked for one.
+ */
+describe('MessageController - stored list options', () => {
+ const getMessages = jest.fn().mockResolvedValue({ messages: [], total: 0 });
+ const controller = new MessageController(
+ { getMessages } as unknown as MessageService,
+ {} as unknown as BulkMessageService,
+ new ChatScopeService(),
+ );
+
+ beforeEach(() => getMessages.mockClear());
+
+ it('passes the sender filter, the page window and the chat to the service', async () => {
+ await controller.getMessages('s1', '[email protected]', '[email protected]', '25', '50');
+
+ expect(getMessages).toHaveBeenCalledWith(
+ 's1',
+ expect.objectContaining({ chatId: '[email protected]', from: '[email protected]', limit: 25, offset: 50 }),
+ );
+ });
+
+ it('leaves the sender filter, limit and offset unset when none is sent', async () => {
+ await controller.getMessages('s1');
+
+ expect(getMessages).toHaveBeenCalledWith(
+ 's1',
+ expect.objectContaining({ chatId: undefined, from: undefined, limit: undefined, offset: undefined }),
+ );
+ });
+});
+
+/**
+ * The live history route reads from WhatsApp on demand, and `includeMedia` can mean dozens of multi-MB
+ * downloads, so what the query turns on and what stops the loop when the client goes away are both
+ * behaviour a caller depends on, and neither shows in the service's own tests.
+ */
+describe('MessageController - live chat history', () => {
+ const getChatHistory = jest.fn().mockResolvedValue([]);
+ const controller = new MessageController(
+ { getChatHistory } as unknown as MessageService,
+ {} as unknown as BulkMessageService,
+ new ChatScopeService(),
+ );
+
+ type ServiceArgs = [
+ sessionId: string,
+ chatId: string,
+ limit: number | undefined,
+ includeMedia: boolean,
+ deep: boolean,
+ signal: AbortSignal,
+ ];
+ const call = async (query: { limit?: string; includeMedia?: string; deep?: string }, res = new EventEmitter()) => {
+ getChatHistory.mockClear();
+ await controller.getChatHistory(
+ 's1',
+ query.limit,
+ query.includeMedia,
+ query.deep,
+ res as unknown as Response,
+ );
+ return getChatHistory.mock.calls[0] as ServiceArgs;
+ };
+
+ it('passes the session and the chat through', async () => {
+ const [sessionId, chatId] = await call({});
+ expect({ sessionId, chatId }).toEqual({ sessionId: 's1', chatId: '[email protected]' });
+ });
+
+ // 'true' and '1' are the two spellings the API spec accepts; any other string leaves the flag off.
+ it.each([
+ ['true', true],
+ ['1', true],
+ ['false', false],
+ ['0', false],
+ ['yes', false],
+ ['TRUE', false],
+ ['', false],
+ [undefined, false],
+ ])('reads includeMedia=%p as %p', async (raw, expected) => {
+ const [, , , includeMedia, deep] = await call({ includeMedia: raw });
+ expect({ includeMedia, deep }).toEqual({ includeMedia: expected, deep: false });
+ });
+
+ it.each([
+ ['true', true],
+ ['1', true],
+ ['false', false],
+ ['0', false],
+ ['yes', false],
+ ['TRUE', false],
+ ['', false],
+ [undefined, false],
+ ])('reads deep=%p as %p', async (raw, expected) => {
+ const [, , , includeMedia, deep] = await call({ deep: raw });
+ expect({ includeMedia, deep }).toEqual({ includeMedia: false, deep: expected });
+ });
+
+ it.each([
+ ['25', 25],
+ ['abc', undefined],
+ ['', undefined],
+ [undefined, undefined],
+ ])('reads limit=%p as %p, leaving a non-number to the service default', async (raw, expected) => {
+ const [, , limit] = await call({ limit: raw });
+ expect(limit).toBe(expected);
+ });
+
+ it('aborts the signal handed to the service when the client disconnects', async () => {
+ const res = new EventEmitter();
+ const [, , , , , signal] = await call({ includeMedia: 'true' }, res);
+
+ expect(signal).toBeInstanceOf(AbortSignal);
+ expect(signal.aborted).toBe(false);
+ res.emit('close');
+ expect(signal.aborted).toBe(true);
+ });
+
+ it('hands over a live signal even when there is no response to listen on', async () => {
+ getChatHistory.mockClear();
+ await controller.getChatHistory('s1', '[email protected]');
+
+ const signal = (getChatHistory.mock.calls[0] as ServiceArgs)[5];
+ expect(signal).toBeInstanceOf(AbortSignal);
+ expect(signal.aborted).toBe(false);
+ });
+});
+
/**
* A caller may pick its own batchId. 'history' shares the two-segment shape of ':chatId/history',
* and an id with a reserved character must survive the statusUrl handed back on creation.