Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(wwebjs): treat an unresolved chat as carrying no labels

Yudhi Armyndharis7fe3ccb7b0a1
diff --git a/src/engine/adapters/wwebjs-label-transport-death.spec.ts b/src/engine/adapters/wwebjs-label-transport-death.spec.ts
index c1c655b99..2e835b6cf 100644
--- a/src/engine/adapters/wwebjs-label-transport-death.spec.ts
+++ b/src/engine/adapters/wwebjs-label-transport-death.spec.ts
@@ -94,4 +94,25 @@ describe('label operations distinguish a dead page from an ordinary failure', ()
client.getLabels.mockResolvedValue([]);
await expect(labels.getLabelById('5')).resolves.toBeNull();
});
+
+ // Client.getChatById resolves undefined for a chat the page cannot resolve; dereferencing it was a
+ // TypeError, a 500 on GET and on both chat-label writes that read the current set first.
+ it('treats an unresolved chat as carrying no labels', async () => {
+ const client = {
+ getChatById: jest.fn().mockResolvedValue(undefined),
+ addOrRemoveLabels: jest.fn().mockResolvedValue(undefined),
+ };
+ const host = {
+ ensureReady: jest.fn(),
+ getClient: () => client as unknown as Client,
+ isPageTransportError: () => false,
+ reportIfPageTransportError: jest.fn(),
+ logger,
+ } as unknown as WwebjsEngineHost;
+ const labels = new WwebjsLabels(host);
+
+ await expect(labels.getChatLabels('[email protected]')).resolves.toEqual([]);
+ await expect(labels.addLabelToChat('[email protected]', '7')).resolves.toBeUndefined();
+ expect(client.addOrRemoveLabels).toHaveBeenCalledWith(['7'], ['[email protected]']);
+ });
});
diff --git a/src/engine/adapters/wwebjs-labels.ts b/src/engine/adapters/wwebjs-labels.ts
index ba9d36008..53d5d0f52 100644
--- a/src/engine/adapters/wwebjs-labels.ts
+++ b/src/engine/adapters/wwebjs-labels.ts
@@ -123,7 +123,9 @@ export class WwebjsLabels {
}
const labels = await withPage(this.host, 'getChatLabels', async () => {
const chat = await this.client().getChatById(chatId);
- return (chat as unknown as GroupChat).getLabels();
+ // getChatById resolves undefined for a chat the page cannot resolve; an unknown chat carries no
+ // labels, and the label write then matches no chat page-side, the same no-op upstream makes.
+ return chat ? (chat as unknown as GroupChat).getLabels() : [];
});
if (!labels) {
return [];