Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): trim the audit log search before filtering
Yudhi Armyndharis3851e80809a1
diff --git a/dashboard/src/pages/Logs.test.ts b/dashboard/src/pages/Logs.test.tsindex 13e3c0062..95588e52d 100644--- a/dashboard/src/pages/Logs.test.ts+++ b/dashboard/src/pages/Logs.test.ts@@ -65,6 +65,8 @@ let exportGrowsMidWalk = false;let listTotal: number | null = null;// When set, a request for the first on-screen page waits for it before answering.let firstPageGate: Promise<void> | null = null;+// When set, the on-screen list read fails with this status.+let listFailure: number | null = null;/** Row `i` of a table walked newest first; every row has its own id, as the gateway's rows do. */function exportRow(i: number): AuditLog {@@ -92,6 +94,9 @@ function installFetchStub(): void {const shifted = [exportRow(-1), ...[...Array(300).keys()].map(exportRow)];return Promise.resolve(jsonResponse({ data: shifted.slice(offset), total: 301 }));}+ if (listFailure && !url.includes('limit=200')) {+ return Promise.resolve(new Response(JSON.stringify({ message: 'Bad Gateway' }), { status: listFailure }));+ }// The gateway holds no error rows, so the server-side severity filter matches nothing.if (new URL(url, 'http://localhost').searchParams.get('severity') === 'error') {return Promise.resolve(jsonResponse({ data: [], total: 0 }));@@ -331,3 +336,30 @@ test('the search matches errorMessage as well as action, whatever the case', asyawait waitFor(() => assert.ok(!screen.queryByText('infra.restart'), 'the search did not filter'));assert.ok(screen.queryByText('session.stop'), 'the errorMessage match was hidden');});++test('a search of only spaces filters nothing, on screen or in the export', async () => {+ const { screen, fireEvent, waitFor } = rtl;+ const { downloads, restore } = recordDownloads();+ try {+ renderLogs();+ await screen.findByText('infra.restart');+ fireEvent.change(screen.getByPlaceholderText('Search logs...'), { target: { value: ' ' } });+ await new Promise(resolve => setTimeout(resolve, 20));+ assert.ok(screen.queryByText('infra.restart'), 'a blank search hid the rows');+ assert.ok(screen.queryByText('session.stop'), 'a blank search hid the rows');+ fireEvent.click(screen.getByRole('button', { name: 'Export CSV' }));+ await waitFor(() => assert.equal(downloads.length, 1));+ assert.equal((await downloads[0].text()).split('\n').length, 1 + LOGS.length, 'a blank search narrowed the export');+ } finally {+ restore();+ }+});++test('a search padded with spaces still matches', async () => {+ const { screen, fireEvent, waitFor } = rtl;+ renderLogs();+ await screen.findByText('infra.restart');+ fireEvent.change(screen.getByPlaceholderText('Search logs...'), { target: { value: ' session ' } });+ await waitFor(() => assert.ok(!screen.queryByText('infra.restart'), 'the search did not filter'));+ assert.ok(screen.queryByText('session.stop'), 'the padded query matched nothing');+});diff --git a/dashboard/src/pages/Logs.tsx b/dashboard/src/pages/Logs.tsxindex 84bceda99..56403bd02 100644--- a/dashboard/src/pages/Logs.tsx+++ b/dashboard/src/pages/Logs.tsx@@ -28,18 +28,17 @@ export function Logs() {const logs: AuditLog[] = data?.data ?? [];const total: number = data?.total ?? 0;- const filteredLogs = logs.filter(log => {- const matchesSearch =- log.action.toLowerCase().includes(searchQuery.toLowerCase()) ||- (log.errorMessage || '').toLowerCase().includes(searchQuery.toLowerCase());- return matchesSearch;- });+ // Trimmed once, so the table, its empty state and the export agree on whether a search is active.+ const q = searchQuery.trim().toLowerCase();+ const matchesSearch = (log: AuditLog) =>+ log.action.toLowerCase().includes(q) || (log.errorMessage || '').toLowerCase().includes(q);+ const filteredLogs = logs.filter(matchesSearch);const totalPages = Math.ceil(total / limit);// Distinguish "filters matched nothing on this page" from "there are no logs at all": the search// box only filters the fetched page (the API has no text search), so a non-match here must not// read as "no such event exists" while more pages may hold it.- const hasSearch = searchQuery.trim() !== '';+ const hasSearch = q !== '';// Severity is enforced SERVER-SIDE (the query carries it): an empty result there means no logs// match at all, which deserves different guidance than the page-local search box.const hasSeverityFilter = severityFilter !== 'all';@@ -101,10 +100,7 @@ export function Logs() {// The walk pages by offset over a live table, newest first: a row written between two pages pushes// the older ones down, so the next page starts with one already fetched. Keep each id once.const all = [...new Map(items.map(log => [log.id, log])).values()];- const q = searchQuery.toLowerCase();- const rows = q- ? all.filter(l => l.action.toLowerCase().includes(q) || (l.errorMessage || '').toLowerCase().includes(q))- : all;+ const rows = all.filter(matchesSearch);// Either stop keeps the newest rows (the API orders newest first); older ones are missing. A// narrower filter gets past the cap, only waiting gets past the throttle. The count follows the UI// language, not the browser's locale, so it reads right inside the sentence.