Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(dashboard): cover mention rendering where the views wire it
Yudhi Armyndharis32d932c88e23
diff --git a/dashboard/src/components/chats/ChatThread.test.ts b/dashboard/src/components/chats/ChatThread.test.tsindex 8437c223d..0d7b75c65 100644--- a/dashboard/src/components/chats/ChatThread.test.ts+++ b/dashboard/src/components/chats/ChatThread.test.ts@@ -1,5 +1,6 @@// Render test for the chat thread's write actions. The gateway answers reply, react, delete and a-// prompt-button tap only for an operator key, so a read-only key must not be offered them.+// prompt-button tap only for an operator key, so a read-only key must not be offered them. It also+// checks the thread, not just the helpers, resolves an @mention in a body and in a quote.import '../../test-helpers/register-hooks.ts';import { test, before, afterEach } from 'node:test';import assert from 'node:assert/strict';@@ -58,6 +59,7 @@ const PROMPT: ChatMessageView = {function renderThread(role: string,messages: ChatMessageView[] = [PROMPT],+ activeChat: Chat = CHAT,): { clicks: string[]; container: HTMLElement } {window.sessionStorage.setItem('openwa_user_role', role);const clicks: string[] = [];@@ -68,7 +70,7 @@ function renderThread(null,createElement(ChatThread, {sessionId: 's1',- activeChat: CHAT,+ activeChat,messages,loadingMessages: false,messagesError: false,@@ -120,3 +122,32 @@ test('an optimistic bubble with no WhatsApp id yet offers no reply, react or delrtl.cleanup();}});++test('an @mention of a participant who posted in the thread shows their first name, in the body and the quote', () => {+ const GROUP: Chat = { ...CHAT, id: '[email protected]', name: 'Team', isGroup: true, kind: 'group' };+ const fromBob: ChatMessageView = {+ ...PROMPT,+ id: 'db-bob',+ waMessageId: 'wamid.bob',+ chatId: GROUP.id,+ from: GROUP.id,+ author: '[email protected]',+ chatName: 'Bob Smith',+ body: 'hello',+ metadata: undefined,+ };+ const mentioning: ChatMessageView = {+ ...fromBob,+ id: 'db-mention',+ waMessageId: 'wamid.mention',+ author: '[email protected]',+ chatName: 'Ann',+ body: 'thanks @15551230000',+ timestamp: 1_700_000_001,+ metadata: { quotedMessage: { id: 'wamid.bob', body: 'ping @15551230000' } },+ };+ const { container } = renderThread('viewer', [fromBob, mentioning], GROUP);+ const body = [...container.querySelectorAll('.message-text')].find(el => el.textContent?.startsWith('thanks'));+ assert.equal(body?.querySelector('bdi')?.textContent, '@Bob');+ assert.equal(container.querySelector('.quote-body bdi')?.textContent, '@Bob');+});diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.tsindex 1ad765ae1..58518e81d 100644--- a/dashboard/src/pages/Chats.test.ts+++ b/dashboard/src/pages/Chats.test.ts@@ -15,7 +15,8 @@ import { test, before, afterEach } from 'node:test';import assert from 'node:assert/strict';import { createElement } from 'react';import { QueryClient, QueryClientProvider } from '@tanstack/react-query';-import type { Session, Chat, ChatMessage, SearchHit } from '../services/api';+import type { Session, Chat, ChatMessage, SearchHit, Channel, ChannelMessage, StatusUpdate } from '../services/api';+import { MENTION_CLOSE, MENTION_OPEN } from '../utils/messageFormatter.ts';import type { installJsdomGlobals as installJsdomGlobalsFn } from '../test-helpers/jsdom.ts';// socket.io-client resolves to a double under this runner (see vite-shim-hooks.mjs), which is what// lets a test deliver a server frame to the page's realtime handlers.@@ -45,6 +46,13 @@ let thirdSessionStatus: Session['status'] | null = null;// Hits the global search answers with.let searchHits: SearchHit[] = [];+// The engine the gateway reports: only whatsapp-web.js lists channels.+let engineType = 'baileys';+// The subscribed channels, the posts every channel's feed answers with, and the stored statuses.+let channels: Channel[] = [];+let channelPosts: ChannelMessage[] = [];+let statuses: StatusUpdate[] = [];+// Answers a session's chat list in place of the fixture, keyed by the session id in the URL (before// the rewrite below folds session-2 onto session-1), so a test can land two lists in any order.let chatsResponder: ((sessionId: string) => Promise<Response>) | null = null;@@ -285,10 +293,13 @@ function installFetchStub(): void {if (window.sessionStorage.getItem('openwa_user_role') !== 'admin') {return Promise.resolve(jsonResponse({ message: 'Insufficient permissions. Required: admin' }, 403));}- return Promise.resolve(jsonResponse({ engineType: 'baileys' }));+ return Promise.resolve(jsonResponse({ engineType }));+ }+ if (method === 'GET' && path.startsWith(`/api/sessions/${SESSION.id}/channels/`)) {+ return Promise.resolve(jsonResponse(channelPosts));}if (method === 'GET' && path === `/api/sessions/${SESSION.id}/channels`) {- return Promise.resolve(jsonResponse([CHANNEL]));+ return Promise.resolve(jsonResponse(channels));}if (method === 'GET' && path === `/api/sessions/${SESSION.id}/chats`) {if (chatsResponder) return chatsResponder(url.includes(`/sessions/${SESSION_2.id}/`) ? SESSION_2.id : SESSION.id);@@ -338,7 +349,7 @@ function installFetchStub(): void {return Promise.resolve(jsonResponse([]));}if (method === 'GET' && path === `/api/sessions/${SESSION.id}/status`) {- return Promise.resolve(jsonResponse({ statuses: [] }));+ return Promise.resolve(jsonResponse({ statuses }));}if (method === 'POST' && path === `/api/sessions/${SESSION.id}/chats/read`) {return Promise.resolve(jsonResponse({ success: true }));@@ -418,6 +429,10 @@ afterEach(() => {chatsResponder = null;thirdSessionStatus = null;searchHits = [];+ engineType = 'baileys';+ channels = [CHANNEL];+ channelPosts = [];+ statuses = [];});function renderChats(): { container: HTMLElement } {@@ -1560,6 +1575,47 @@ test('the media viewer saves an image under its file name, not its caption', asyassert.equal(links[0].download, 'photo.png');});+test('a channel post or status caption carrying mention delimiters renders them as nothing, not as a mention', async () => {+ const { screen, fireEvent, waitFor } = rtl;+ // Only resolveMentions may place the delimiters; raw text carrying them is stripped before render.+ const raw = `${MENTION_OPEN}@Mallory${MENTION_CLOSE} says hi`;+ engineType = 'whatsapp-web.js';+ window.sessionStorage.setItem('openwa_engine_type', engineType);+ channels = [{ id: '120363000000000001@newsletter', name: 'News' }];+ channelPosts = [{ id: 'post-1', body: raw, timestamp: 1_700_000_000, hasMedia: false }];+ const now = Date.now();+ statuses = [+ {+ id: 'status-1',+ contact: { id: CONTACT.id, name: 'Bob' },+ type: 'image',+ caption: raw,+ timestamp: new Date(now).toISOString(),+ expiresAt: new Date(now + 86_400_000).toISOString(),+ },+ ];+ const { container } = renderChats();+ await screen.findByText('Alice');+ const bubbleText = async (): Promise<Element> =>+ waitFor(() => {+ const found = container.querySelector('.channel-room .message-bubble .message-text');+ assert.ok(found, 'the post did not render');+ return found;+ });++ fireEvent.click(screen.getByRole('tab', { name: 'Channels' }));+ fireEvent.click(await screen.findByText('News'));+ const post = await bubbleText();+ assert.equal(post.textContent, '@Mallory says hi');+ assert.ok(!post.querySelector('bdi'), 'the channel post rendered a mention');++ fireEvent.click(screen.getByRole('tab', { name: 'Status' }));+ fireEvent.click(await screen.findByText('Bob'));+ const caption = await bubbleText();+ assert.equal(caption.textContent, '@Mallory says hi');+ assert.ok(!caption.querySelector('bdi'), 'the status caption rendered a mention');+});+test('two media downloads in flight do not clobber each other', async () => {const { screen, fireEvent, within, waitFor } = rtl;resetFetchCalls();