Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(wwebjs): fetch the default history page for an unusable limit

Yudhi Armyndharisd499b9153177
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
index 4fc8dc515..d884e8fb5 100644
--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
@@ -658,6 +658,26 @@ describe('WhatsAppWebJsAdapter.getChatHistory enrichment (parity with the live p
return adapter;
};
+ // Chat.fetchMessages only caps the page when `limit > 0` (Chat.js), so a NaN, null or 0 limit
+ // returned every loaded message. The channel read already substitutes the default for the same case.
+ it.each([Number.NaN, null, 0, -5])('fetches the default page for a limit of %p, not every message', async limit => {
+ const chat = { fetchMessages: jest.fn().mockResolvedValue([]) };
+ const client = { getChatById: jest.fn().mockResolvedValue(chat) };
+
+ await readyAdapter(client).getChatHistory('[email protected]', limit as number, false);
+
+ expect(chat.fetchMessages).toHaveBeenCalledWith({ limit: 50 });
+ });
+
+ it('passes a usable limit through, truncated', async () => {
+ const chat = { fetchMessages: jest.fn().mockResolvedValue([]) };
+ const client = { getChatById: jest.fn().mockResolvedValue(chat) };
+
+ await readyAdapter(client).getChatHistory('[email protected]', 7.9, false);
+
+ expect(chat.fetchMessages).toHaveBeenCalledWith({ limit: 7 });
+ });
+
it('populates location coordinates and resolves the quoted message for historical messages', async () => {
const locMsg = {
id: { _serialized: 'M1' },
diff --git a/src/engine/adapters/wwebjs-messaging.ts b/src/engine/adapters/wwebjs-messaging.ts
index ac816e19d..15c928de6 100644
--- a/src/engine/adapters/wwebjs-messaging.ts
+++ b/src/engine/adapters/wwebjs-messaging.ts
@@ -713,6 +713,9 @@ export class WwebjsMessaging {
signal?: AbortSignal,
): Promise<IncomingMessage[]> {
this.host.ensureReady();
+ // Chat.fetchMessages only caps the page when `limit > 0` (Chat.js), so a 0/negative/NaN limit fails
+ // OPEN and returns every loaded message. Substitute the default, as getChannelMessages does.
+ const safeLimit = Number.isFinite(limit) && limit >= 1 ? Math.trunc(limit) : 50;
const messages = await this.withPage('getChatHistory', async () => {
const chat = await this.client().getChatById(chatId);
// Unknown chat: getChatById resolves undefined rather than throwing. A chat this account cannot
@@ -721,7 +724,7 @@ export class WwebjsMessaging {
if (!chat) {
return [];
}
- return chat.fetchMessages({ limit });
+ return chat.fetchMessages({ limit: safeLimit });
});
const results: IncomingMessage[] = [];
// Aggregate base64 budget across the whole pass: the per-message cap bounds ONE blob, but without