Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(dashboard): cover unread, reload deadline and add-member cases

Yudhi Armyndharis269bab3ad081
diff --git a/dashboard/src/pages/Chats.features.test.ts b/dashboard/src/pages/Chats.features.test.ts
index 2957bf3d6..ecc89a5f9 100644
--- a/dashboard/src/pages/Chats.features.test.ts
+++ b/dashboard/src/pages/Chats.features.test.ts
@@ -74,9 +74,10 @@ let groupDescription = 'old description';
let bobIsAdmin = false;
// When true, GROUP also lists a member WhatsApp knows by a privacy id only, which carries no phone number.
let groupHasLidMember = false;
-// When true, a membership write answers HTTP 200 with its participants refused in `results`, as the route does
-// for a batch WhatsApp turns down in part.
-let refuseMember = false;
+// How a membership write is refused. The dialog sends one participant at a time, and the gateway answers 403 when
+// a write fails for every participant it was asked about ('all'). A refusal inside a 200 answer ('some') is what
+// a batch it turned down in part gets; nothing in this dialog asks for one, but the answer shape is the route's.
+let refuseMember: false | 'all' | 'some' = false;
const CONTACT = { id: '[email protected]', name: 'Bob', number: '15550002222', isMyContact: true, isBlocked: false };
const ALICE_CONTACT = { id: CHAT.id, name: 'Alice', number: '15550001111', isMyContact: true, isBlocked: false };
@@ -202,14 +203,36 @@ function installFetchStub(): void {
if (write) return gated(write[0], () => jsonResponse({ success: !declineWrites }));
if (path.startsWith(`${groupPath}/participants`)) {
const { participants } = body as { participants: string[] };
- if (path.endsWith('/promote') && !refuseMember) bobIsAdmin = true;
- if (path.endsWith('/demote') && !refuseMember) bobIsAdmin = false;
- const refusal = refuseMember ? { status: 403, message: 'not-authorized' } : {};
+ if (refuseMember === 'some') {
+ return Promise.resolve(
+ jsonResponse({
+ success: true,
+ message: 'ok',
+ results: participants.map(id => ({ id, success: false, status: 403, message: 'not-authorized' })),
+ }),
+ );
+ }
+ if (refuseMember === 'all') {
+ const op =
+ path.split('/').pop() === 'participants' ? (method === 'POST' ? 'add' : 'remove') : path.split('/').pop();
+ return Promise.resolve(
+ jsonResponse(
+ {
+ statusCode: 403,
+ message: `${op}Participants failed for all ${participants.length} participant(s) in group ${GROUP.id}: ${participants[0]} (403)`,
+ error: 'Forbidden',
+ },
+ 403,
+ ),
+ );
+ }
+ if (path.endsWith('/promote')) bobIsAdmin = true;
+ if (path.endsWith('/demote')) bobIsAdmin = false;
return Promise.resolve(
jsonResponse({
success: true,
message: 'ok',
- results: participants.map(id => ({ id, success: !refuseMember, ...refusal })),
+ results: participants.map(id => ({ id, success: true })),
}),
);
}
@@ -607,7 +630,7 @@ test('a member known only by a privacy id is not shown as a phone number', async
assert.ok(!(dialog.textContent ?? '').includes('+262'), 'a privacy id was formatted as an international number');
});
-test('a participant the gateway refuses is reported although the write answered 200', async () => {
+test('a participant the gateway refuses is reported with the reason it gave', async () => {
const { screen, fireEvent, waitFor, within } = rtl;
listGroup = true;
renderPage();
@@ -627,13 +650,47 @@ test('a participant the gateway refuses is reported although the write answered
);
assert.ok(!screen.queryByText('The action failed'), 'an accepted write was reported as failed');
- refuseMember = true;
+ refuseMember = 'all';
fireEvent.click(within(dialog).getByRole('button', { name: 'Remove admin: Bob' }));
await screen.findByText('The action failed');
- await screen.findByText('15550002222: not-authorized');
+ await screen.findByText(
+ /^demoteParticipants failed for all 1 participant\(s\) in group .*15550002222@c\.us \(403\)$/,
+ );
assert.equal(writesTo('demote'), 1);
});
+test('a participant refused inside a 200 answer is named with the reason it gave', async () => {
+ const { screen, fireEvent, within } = rtl;
+ listGroup = true;
+ renderPage();
+ const dialog = await openGroupInfo();
+ fireEvent.click(within(dialog).getByRole('tab', { name: 'Participants' }));
+
+ refuseMember = 'some';
+ fireEvent.click(within(dialog).getByRole('button', { name: 'Make admin: Bob' }));
+ await screen.findByText('The action failed');
+ await screen.findByText('15550002222: not-authorized');
+});
+
+test('a number the gateway refuses to add stays in the box, and an accepted one clears it', async () => {
+ const { screen, fireEvent, waitFor, within } = rtl;
+ listGroup = true;
+ renderPage();
+ const dialog = await openGroupInfo();
+ fireEvent.click(within(dialog).getByRole('tab', { name: 'Participants' }));
+ const box = () => within(dialog).getByLabelText('Add a number with country code') as HTMLInputElement;
+ fireEvent.change(box(), { target: { value: TYPED_NUMBER } });
+
+ refuseMember = 'all';
+ fireEvent.click(within(dialog).getByRole('button', { name: 'Add' }));
+ await screen.findByText('The action failed');
+ assert.equal(box().value, TYPED_NUMBER, 'the number the gateway refused was cleared');
+
+ refuseMember = false;
+ fireEvent.click(within(dialog).getByRole('button', { name: 'Add' }));
+ await waitFor(() => assert.equal(box().value, '', 'an accepted number stayed in the box'));
+});
+
test('a member who is no admin sees the group read-only, and a viewer key gets no write actions', async () => {
const { screen, fireEvent, within } = rtl;
listGroup = true;
@@ -1504,6 +1561,49 @@ test('Copy chat ID says so when the copy did not happen', async t => {
assert.ok(!rtl.screen.queryByText('Chat ID copied'));
});
+test('a chat marked unread is read again once it is opened', async () => {
+ const { screen, fireEvent, waitFor } = rtl;
+ const { container } = renderPage();
+ await screen.findByText('Alice');
+ pickRowAction('Alice', 'Mark as unread');
+ await screen.findByLabelText('Marked as unread');
+ fetchCalls.length = 0;
+
+ fireEvent.click(screen.getByText('Alice'));
+ await waitFor(() => assert.ok(container.querySelector('.room-header'), 'the room did not open'));
+ await waitFor(() => assert.equal(readsOf(CHAT.id).length, 1, 'opening the chat did not read it again'), {
+ timeout: 3_000,
+ });
+ assert.ok(!screen.queryByLabelText('Marked as unread'), 'the badge stayed after the chat was opened');
+});
+
+test('a message that reaches the open chat while Mark as unread is out is not read automatically', async () => {
+ const { screen, fireEvent, waitFor } = rtl;
+ const release = hold('unread');
+ const { container } = renderPage();
+ fireEvent.click(await screen.findByText('Alice'));
+ await waitFor(() => assert.ok(container.querySelector('.room-header'), 'the room did not open'));
+ await waitFor(() => assert.equal(readsOf(CHAT.id).length, 1, 'opening the chat did not mark it read'), {
+ timeout: 3_000,
+ });
+
+ pickRowAction('Alice', 'Mark as unread');
+ await waitFor(() =>
+ assert.ok(
+ fetchCalls.some(c => c.path.endsWith('/chats/unread')),
+ 'the mark was not sent',
+ ),
+ );
+ fetchCalls.length = 0;
+ receiveFromAlice('wamid.during.1', 'while the mark is out');
+ await screen.findAllByText('while the mark is out');
+ // Past the quiet window, so a read queued by that message would have gone out.
+ await new Promise(resolve => setTimeout(resolve, 1_000));
+ release();
+ await settle();
+ assert.deepEqual(readsOf(CHAT.id), [], 'the message was read automatically and undid the mark');
+});
+
test('on Baileys neither the row nor the room menu offers Clear chat or Delete chat (#1891)', async () => {
const { screen, fireEvent, waitFor, within } = rtl;
const { container } = renderPage();
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.ts
index f6449e20f..ba7200445 100644
--- a/dashboard/src/pages/Chats.test.ts
+++ b/dashboard/src/pages/Chats.test.ts
@@ -11,7 +11,7 @@
// JSX), loader hooks registered before any app-module import (see test-helpers/register-hooks),
// and JSDOM installed before importing modules that read `window` at import time.
import '../test-helpers/register-hooks.ts';
-import { test, before, afterEach } from 'node:test';
+import { test, before, afterEach, type TestContext } from 'node:test';
import assert from 'node:assert/strict';
import { createElement } from 'react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
@@ -1897,6 +1897,47 @@ test('triggers made while the trailing reload is out queue one more behind it',
assert.equal(countFetchCalls('GET', chatsPath), 3);
});
+// A background walk that never answers is cut at a deadline of the page's own, a timer of a couple of minutes.
+// Only a timer that long is held back, to be fired by hand, so the rest of the page keeps its real clock.
+function holdWalkDeadlines(t: TestContext): Array<() => void> {
+ const fire: Array<() => void> = [];
+ const realSetTimeout = globalThis.setTimeout;
+ t.mock.method(globalThis, 'setTimeout', ((callback: () => void, ms?: number, ...args: unknown[]) => {
+ // Above a healthy walk, and below the 180 s the gateway takes to answer a stalled engine.
+ if (typeof ms === 'number' && ms >= 60_000 && ms < 180_000) {
+ fire.push(callback);
+ return { unref: () => undefined } as unknown as ReturnType<typeof setTimeout>;
+ }
+ return realSetTimeout(callback, ms, ...args);
+ }) as typeof setTimeout);
+ return fire;
+}
+
+test('a background walk that never answers is abandoned at its deadline, and the reload behind it starts', async t => {
+ const { screen, waitFor } = rtl;
+ renderChats();
+ await screen.findByText('Alice');
+ const deadlines = holdWalkDeadlines(t);
+ // Every list request stays out until its signal aborts it, as a half-open connection would.
+ const signals: Array<AbortSignal | null | undefined> = [];
+ chatsResponder = (_sessionId, _offset, signal) =>
+ new Promise<Response>((_resolve, reject) => {
+ signals.push(signal);
+ signal?.addEventListener('abort', () => reject(new DOMException('aborted', 'AbortError')), { once: true });
+ });
+
+ askForReloads();
+ await waitFor(() => assert.equal(signals.length, 1));
+ assert.equal(deadlines.length, 1, 'the background walk was given no deadline');
+ await flush();
+ assert.equal(signals.length, 1, 'a trigger made while the walk was out started its own');
+
+ deadlines[0]();
+ await waitFor(() => assert.equal(signals.length, 2, 'the reload behind the stalled walk never started'));
+ assert.equal(signals[0]?.aborted, true, 'the stalled walk was left holding its request');
+ assert.equal(signals[1]?.aborted, false);
+});
+
test('a background reload that lands before the initial list is not overwritten by it', async () => {
const { screen, waitFor } = rtl;
const answers = holdChatLists();
diff --git a/dashboard/src/utils/reloadCoalescer.test.ts b/dashboard/src/utils/reloadCoalescer.test.ts
index d5b29945e..e3106bda0 100644
--- a/dashboard/src/utils/reloadCoalescer.test.ts
+++ b/dashboard/src/utils/reloadCoalescer.test.ts
@@ -220,3 +220,12 @@ test('a reload that answers in time is not aborted and leaves no deadline behind
void coalescer.run();
assert.equal(out.length, 2);
});
+
+test('cancel on a coalescer with nothing out leaves it usable, as React replays effect cleanups before any reload', () => {
+ const { reload, out } = manualReloads();
+ const coalescer = createReloadCoalescer(reload);
+ coalescer.cancel();
+ void coalescer.run();
+ // Checked before anything is awaited: a coalescer left marked as running would hold this call forever.
+ assert.equal(out.length, 1, 'a call after an idle cancel waited behind a reload that was never started');
+});