Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(dashboard): pin the guards of the pure message and form helpers

Yudhi Armyndharis8e9b6e2beb75
diff --git a/dashboard/src/utils/archivedMediaLoad.test.ts b/dashboard/src/utils/archivedMediaLoad.test.ts
index 4d322e8c4..02b216dc2 100644
--- a/dashboard/src/utils/archivedMediaLoad.test.ts
+++ b/dashboard/src/utils/archivedMediaLoad.test.ts
@@ -97,3 +97,39 @@ test('any other failure is not retried', async () => {
);
assert.equal(calls, 1);
});
+
+test('a load whose signal is already aborted rejects without starting', async () => {
+ const controller = new AbortController();
+ controller.abort();
+ let ran = false;
+ await assert.rejects(
+ loadArchivedMedia(controller.signal, async () => {
+ ran = true;
+ return 'x';
+ }),
+ );
+ assert.equal(ran, false);
+});
+
+test('a load that leaves the queue takes only its own turn with it', async () => {
+ const ran: number[] = [];
+ const controllers = Array.from({ length: 4 }, () => new AbortController());
+ const loads = controllers.map((controller, i) =>
+ loadArchivedMedia(controller.signal, async () => {
+ ran.push(i);
+ return i;
+ }).catch(() => -1),
+ );
+ controllers[2].abort();
+ // A turn dropped with it would leave the fourth load pending for good.
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ const outcome = await Promise.race([
+ Promise.all(loads),
+ new Promise(resolve => {
+ timer = setTimeout(() => resolve('timeout'), 3000);
+ }),
+ ]);
+ clearTimeout(timer);
+ assert.notEqual(outcome, 'timeout');
+ assert.deepEqual(ran, [0, 1, 3]);
+});
diff --git a/dashboard/src/utils/chatMessages.test.ts b/dashboard/src/utils/chatMessages.test.ts
index 6fb76bbed..c0a8b8ef2 100644
--- a/dashboard/src/utils/chatMessages.test.ts
+++ b/dashboard/src/utils/chatMessages.test.ts
@@ -422,7 +422,7 @@ test('senderKey prefers the participant JID and falls back to the display name',
assert.equal(senderKey({}), undefined);
});
-import { capMediaPayloads, MEDIA_PAYLOAD_CACHE_LIMIT } from './chatMessages.ts';
+import { capMediaPayloads, MEDIA_PAYLOAD_CACHE_LIMIT, mergeDeliveryStatus } from './chatMessages.ts';
const mediaMsg = (id: string, data?: string): ChatMessageView =>
msg({ id, type: 'image', metadata: { media: { mimetype: 'image/jpeg', filename: `${id}.jpg`, data } } });
@@ -742,3 +742,30 @@ test('legacy poll rows inherit available history choices without replacing store
const revoked = mergeChatMessages([{ ...stored, type: 'revoked', body: '', metadata: undefined }], [historical])[0];
assert.equal(revoked.metadata?.poll, undefined);
});
+
+test('capMediaPayloads: archived documents are download buttons, not previews, and are not counted', () => {
+ const document = (id: string): ChatMessageView =>
+ msg({ id, type: 'document', metadata: { media: { mimetype: 'application/pdf', omitted: true, archived: true } } });
+ const images = Array.from({ length: MEDIA_PAYLOAD_CACHE_LIMIT }, (_, i) => mediaMsg(`m-${i}`, 'X'));
+ const list = [...Array.from({ length: 5 }, (_, i) => document(`d-${i}`)), ...images];
+ assert.equal(capMediaPayloads(list), list, 'only the images count, and they fit');
+});
+
+test('capMediaPayloads: omitted markers that follow a full set of payloads strip none of them', () => {
+ const images = Array.from({ length: MEDIA_PAYLOAD_CACHE_LIMIT }, (_, i) => mediaMsg(`m-${i}`, 'X'));
+ const omitted = Array.from({ length: 120 }, (_, i) =>
+ msg({ id: `o-${i}`, type: 'image', metadata: { media: { mimetype: 'image/jpeg', omitted: true } } }),
+ );
+ const list = [...images, ...omitted];
+ assert.equal(capMediaPayloads(list), list);
+});
+
+test('mergeDeliveryStatus: a late lower tick never downgrades the shown one, and failed applies only before delivery', () => {
+ assert.equal(mergeDeliveryStatus('delivered', 'sent'), 'delivered');
+ assert.equal(mergeDeliveryStatus('read', 'delivered'), 'read');
+ assert.equal(mergeDeliveryStatus('sent', 'delivered'), 'delivered');
+ assert.equal(mergeDeliveryStatus('pending', 'sent'), 'sent');
+ assert.equal(mergeDeliveryStatus('delivered', 'failed'), 'delivered');
+ assert.equal(mergeDeliveryStatus('sent', 'failed'), 'failed');
+ assert.equal(mergeDeliveryStatus('failed', 'read'), 'failed');
+});
diff --git a/dashboard/src/utils/fetchAllPages.test.ts b/dashboard/src/utils/fetchAllPages.test.ts
index a9e423076..06364116f 100644
--- a/dashboard/src/utils/fetchAllPages.test.ts
+++ b/dashboard/src/utils/fetchAllPages.test.ts
@@ -1,6 +1,6 @@
import { test, before, beforeEach } from 'node:test';
import assert from 'node:assert/strict';
-import { fetchAllPages } from './fetchAllPages.ts';
+import { fetchAllPages, retryThrottled } from './fetchAllPages.ts';
before(async () => {
const { installJsdomGlobals } = await import('../test-helpers/jsdom.ts');
@@ -252,3 +252,23 @@ test('a walk does not send its retry when the key is signed out during the throt
);
assert.deepEqual(sentWith, ['key-a', 'key-a'], 'the retry went out after the sign-out');
});
+
+test('the throttle backoff doubles on each retry', async () => {
+ const delays: number[] = [];
+ const realSetTimeout = globalThis.setTimeout;
+ globalThis.setTimeout = ((fn: () => void, ms?: number) => {
+ delays.push(ms ?? 0);
+ return realSetTimeout(fn, 0);
+ }) as typeof setTimeout;
+ try {
+ await assert.rejects(
+ retryThrottled(async () => {
+ throw httpError(429);
+ }, 40),
+ { status: 429 },
+ );
+ } finally {
+ globalThis.setTimeout = realSetTimeout;
+ }
+ assert.deepEqual(delays, [40, 80]);
+});
diff --git a/dashboard/src/utils/messageFormatter.test.ts b/dashboard/src/utils/messageFormatter.test.ts
index dca94f7e5..cc09bf519 100644
--- a/dashboard/src/utils/messageFormatter.test.ts
+++ b/dashboard/src/utils/messageFormatter.test.ts
@@ -181,3 +181,11 @@ test('a mention that fills a *bold* span on its own resolves and keeps the bold'
{ type: 'bold', children: [{ type: 'mention', value: '@Ravi' }] },
]);
});
+
+test('text right after a closing triple backtick is kept', () => {
+ assert.deepEqual(parseMessageBody('```a```b'), [{ type: 'codeblock', value: 'a' }, text('b')]);
+});
+
+test('a space right after the opening marker keeps the span literal even when a closer follows', () => {
+ assert.deepEqual(parseMessageBody('* a*'), [text('* a*')]);
+});
diff --git a/dashboard/src/utils/modalA11y.test.ts b/dashboard/src/utils/modalA11y.test.ts
index 1402aecf4..90908f7e8 100644
--- a/dashboard/src/utils/modalA11y.test.ts
+++ b/dashboard/src/utils/modalA11y.test.ts
@@ -219,3 +219,31 @@ test('the scroll lock is released once the last dialog closes, whatever the clos
unbindNested();
assert.equal(doc.body.style.overflow, 'auto');
});
+
+test('Tab and Shift+Tab on a control that is neither first nor last are left to the browser', () => {
+ const { doc, card, focusables } = makeWorld(3);
+ const unbind = bindModalA11y(doc as never, card as never, () => {});
+ const [first, middle, last] = focusables;
+
+ middle.focus();
+ const forward = makeKeyEvent('Tab');
+ doc.dispatchKey(forward);
+ assert.equal(forward.defaultPrevented, false);
+ assert.equal(doc.activeElement, middle as never);
+
+ const backward = makeKeyEvent('Tab', true);
+ doc.dispatchKey(backward);
+ assert.equal(backward.defaultPrevented, false);
+ assert.equal(doc.activeElement, middle as never);
+
+ // Neither end is touched by the other direction either.
+ last.focus();
+ const backFromLast = makeKeyEvent('Tab', true);
+ doc.dispatchKey(backFromLast);
+ assert.equal(backFromLast.defaultPrevented, false);
+ first.focus();
+ const forwardFromFirst = makeKeyEvent('Tab');
+ doc.dispatchKey(forwardFromFirst);
+ assert.equal(forwardFromFirst.defaultPrevented, false);
+ unbind();
+});
diff --git a/dashboard/src/utils/sessionForm.test.ts b/dashboard/src/utils/sessionForm.test.ts
index 0ec50ab7f..f4ce2de6e 100644
--- a/dashboard/src/utils/sessionForm.test.ts
+++ b/dashboard/src/utils/sessionForm.test.ts
@@ -6,6 +6,7 @@ import {
isValidPairingPhone,
matchesStatusFilter,
filterSessions,
+ isValidProxyUrl,
} from './sessionForm.ts';
test('a valid session name reports no issues', () => {
@@ -103,3 +104,12 @@ test('filterSessions combines the search box and the status group', () => {
'id matches too',
);
});
+
+test('a proxy URL is one of the four schemes the gateway takes, and blank or malformed text is refused', () => {
+ for (const url of ['http://proxy:3128', 'https://proxy', 'socks4://10.0.0.1:1080', ' socks5://p:1 ']) {
+ assert.equal(isValidProxyUrl(url), true, url);
+ }
+ for (const url of ['', ' ', 'not a url', 'ftp://proxy', 'proxy:3128']) {
+ assert.equal(isValidProxyUrl(url), false, JSON.stringify(url));
+ }
+});
diff --git a/dashboard/src/utils/webhookAuth.test.ts b/dashboard/src/utils/webhookAuth.test.ts
index 24f0574ce..dc226aa0c 100644
--- a/dashboard/src/utils/webhookAuth.test.ts
+++ b/dashboard/src/utils/webhookAuth.test.ts
@@ -99,3 +99,9 @@ test('a generated secret is 64 hex characters and passes the length check', () =
assert.equal(secretError(secret), null);
assert.notEqual(generateSecret(), secret);
});
+
+test('a header value is refused above U+00FF and accepted at it, which goes out as one Latin-1 byte', () => {
+ const edge = String.fromCharCode(0xff);
+ assert.equal(err([{ name: 'X-A', value: `a${edge}` }]), null);
+ assert.notEqual(err([{ name: 'X-A', value: `a${String.fromCharCode(0x100)}` }]), null);
+});