Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(engine): cover the wwjs archived/pinned/muted mapping
Surya3d6fb0fc5db2
diff --git a/src/engine/adapters/wwebjs-chat-state.spec.ts b/src/engine/adapters/wwebjs-chat-state.spec.tsnew file mode 100644index 000000000..bcf72e510--- /dev/null+++ b/src/engine/adapters/wwebjs-chat-state.spec.ts@@ -0,0 +1,86 @@+import type { Client } from 'whatsapp-web.js';+import { WwebjsChats } from './wwebjs-chats';+import { WwebjsLabels } from './wwebjs-labels';+import { createLogger } from '../../common/services/logger.service';+import { type WwebjsEngineHost } from './wwebjs-host';+import { type WwebjsMessaging } from './wwebjs-messaging';++/**+ * `archived`, `pinned` and `muted` on the whatsapp-web.js side of `ChatSummary`.+ *+ * Both places that build a summary have to carry the three, and they are built independently:+ * `getChats` maps the library's `Chat` model, while `getChatsByLabel` maps the entries+ * `getChatsByLabelId` returns. A field added to one and forgotten in the other is invisible until+ * a Business account lists a label, so both mappers are held to the same shape here.+ *+ * The library reports mute as a verdict already — `Chat.isMuted` — so unlike Baileys there is no+ * end-time comparison to make; `muteExpiration` is the raw stamp behind it and is deliberately not+ * what this maps. Each field is read through `Boolean()` because the page can leave any of them+ * undefined on a chat it built from a partial record, and `undefined` must land as `false` rather+ * than escape into a required boolean.+ */++const logger = createLogger('wwebjs-chat-state.spec');++function makeHost(client: Record<string, unknown>): WwebjsEngineHost {+ return {+ ensureReady: jest.fn(),+ getClient: () => client as unknown as Client,+ logger,+ isPageTransportError: () => false,+ reportIfPageTransportError: jest.fn(),+ } as unknown as WwebjsEngineHost;+}++const RAW = {+ id: { _serialized: '[email protected]' },+ name: 'Alice',+ isGroup: false,+ unreadCount: 0,+ timestamp: 1_700_000_000,+};++describe('WwebjsChats.getChats chat state', () => {+ const listWith = async (over: Record<string, unknown>) => {+ const client = { getChats: jest.fn().mockResolvedValue([{ ...RAW, ...over }]) };+ const chats = new WwebjsChats(makeHost(client), {} as unknown as WwebjsMessaging);+ return (await chats.getChats())[0];+ };++ it('carries archived, pinned and muted through from the library model', async () => {+ const summary = await listWith({ archived: true, pinned: true, isMuted: true });+ expect(summary).toMatchObject({ archived: true, pinned: true, muted: true });+ });++ it('reports false, not undefined, when the page left the fields unset', async () => {+ const summary = await listWith({});+ // Required fields on ChatSummary: a caller reading `chat.muted` must never get undefined and+ // silently treat "unknown" as a mute.+ expect(summary).toMatchObject({ archived: false, pinned: false, muted: false });+ });++ // Chat.isMuted is the verdict; muteExpiration is the stamp behind it. Mapping the stamp instead+ // would report every chat that has ever been muted as muted.+ it('reads the verdict, not the expiry stamp behind it', async () => {+ const summary = await listWith({ isMuted: false, muteExpiration: 1_700_000_000 });+ expect(summary.muted).toBe(false);+ });+});++describe('WwebjsLabels.getChatsByLabel chat state', () => {+ const listWith = async (over: Record<string, unknown>) => {+ const client = { getChatsByLabelId: jest.fn().mockResolvedValue([{ ...RAW, ...over }]) };+ const labels = new WwebjsLabels(makeHost(client));+ return (await labels.getChatsByLabel('1'))[0];+ };++ it('carries the same three fields as getChats', async () => {+ const summary = await listWith({ archived: true, pinned: true, isMuted: true });+ expect(summary).toMatchObject({ archived: true, pinned: true, muted: true });+ });++ it('reports false when the label entry left them unset', async () => {+ const summary = await listWith({});+ expect(summary).toMatchObject({ archived: false, pinned: false, muted: false });+ });+});