Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): ignore a batch poll that answers after polling stopped

Yudhi Armyndharisf5a97abffd93
diff --git a/dashboard/src/pages/MessageTester.test.ts b/dashboard/src/pages/MessageTester.test.ts
index e259b6d5b..ea7449ab8 100644
--- a/dashboard/src/pages/MessageTester.test.ts
+++ b/dashboard/src/pages/MessageTester.test.ts
@@ -405,6 +405,66 @@ test('a bulk send that resolves after the page is left starts no progress pollin
assert.equal(pollers.length, 0);
});
+test('a progress poll that answers after Cancel does not undo the cancel', async () => {
+ const progress = { total: 1, sent: 0, failed: 0, pending: 0, cancelled: 0 };
+ let answerPoll: (response: Response) => void = () => {};
+ globalThis.fetch = ((input: RequestInfo | URL): Promise<Response> => {
+ const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
+ if (url.endsWith('/sessions')) {
+ return Promise.resolve(jsonResponse([{ id: 's1', name: 'Main', status: 'ready', phone: '15550000000' }]));
+ }
+ if (url.endsWith('/messages/send-bulk')) {
+ return Promise.resolve(jsonResponse({ batchId: 'b1', status: 'pending', totalMessages: 1 }, 202));
+ }
+ if (url.endsWith('/messages/batch/b1/cancel')) {
+ return Promise.resolve(
+ jsonResponse({ batchId: 'b1', status: 'cancelled', progress: { ...progress, cancelled: 1 }, results: [] }),
+ );
+ }
+ if (url.endsWith('/messages/batch/b1')) {
+ return new Promise<Response>(resolve => {
+ answerPoll = resolve;
+ });
+ }
+ return Promise.resolve(jsonResponse([]));
+ }) as typeof fetch;
+
+ // Run the progress poll by hand instead of waiting out its 2 s interval.
+ const polls: Array<() => void> = [];
+ const timers: ReturnType<typeof setInterval>[] = [];
+ const realSetInterval = globalThis.setInterval;
+ globalThis.setInterval = ((handler: () => void, ms?: number) => {
+ const id = ms === 2000 ? realSetInterval(() => {}, 2 ** 30) : realSetInterval(handler, ms);
+ if (ms === 2000) polls.push(handler);
+ timers.push(id);
+ return id;
+ }) as typeof setInterval;
+ try {
+ const container = await renderBulkAsWriter();
+ type(container, '#mt-11', '15550000001');
+ rtl.fireEvent.change(rtl.screen.getByPlaceholderText('Enter your message here...'), { target: { value: 'hi' } });
+ await rtl.waitFor(() => assert.equal(sendButton().disabled, false));
+ rtl.fireEvent.click(sendButton());
+ const cancel = await rtl.screen.findByRole('button', { name: 'Cancel Batch' });
+ const badge = () => container.querySelector('.batch-badge')?.textContent;
+
+ polls[0]();
+ rtl.fireEvent.click(cancel);
+ await rtl.waitFor(() => assert.equal(badge(), 'Cancelled'));
+ await rtl.act(async () => {
+ answerPoll(
+ jsonResponse({ batchId: 'b1', status: 'processing', progress: { ...progress, pending: 1 }, results: [] }),
+ );
+ await new Promise(resolve => setTimeout(resolve, 50));
+ });
+
+ assert.equal(badge(), 'Cancelled', 'a poll that answered after the cancel put the batch back to processing');
+ } finally {
+ globalThis.setInterval = realSetInterval;
+ timers.forEach(clearInterval);
+ }
+});
+
test('an inline file too large for the recipient count keeps Send disabled', async () => {
stubGateway();
const container = await renderBulkAsWriter();
diff --git a/dashboard/src/pages/MessageTester.tsx b/dashboard/src/pages/MessageTester.tsx
index 1ab3d59c6..bdcc0a08c 100644
--- a/dashboard/src/pages/MessageTester.tsx
+++ b/dashboard/src/pages/MessageTester.tsx
@@ -212,15 +212,19 @@ export function MessageTester() {
const startBatchPolling = (batchSessionId: string, batchId: string) => {
stopBatchPolling();
if (unmountedRef.current) return;
- batchPollRef.current = setInterval(async () => {
+ const timer = setInterval(async () => {
try {
const status = await messageApi.getBatchStatus(batchSessionId, batchId);
+ // Polling was stopped (a cancel, a terminal status, a new batch, unmount) while this read
+ // was in flight: its snapshot is older than what is on screen.
+ if (batchPollRef.current !== timer) return;
setBatchStatus(status);
if (TERMINAL_BATCH_STATUSES.includes(status.status)) stopBatchPolling();
} catch {
// A transient poll failure (network blip, backend restart) must not kill progress tracking.
}
}, 2000);
+ batchPollRef.current = timer;
};
const handleBulkFileChange = (e: ChangeEvent<HTMLInputElement>) => {