Consumer-side tests: RoomOutbox as rendered; hung send to watchdog reload to resend

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Void 2026-11-13 01:02:08 +00:00
commit 7e084abf34
2 changed files with 134 additions and 0 deletions

View file

@ -0,0 +1,64 @@
import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { enqueueOutbox, failOutbox } from '../../state/room/composerStore';
import { RoomOutbox } from './RoomOutbox';
// What the person sees under the timeline for sends the server has not confirmed.
const text = (html: string) => html.replace(/<[^>]*>/g, ' ').replace(/\s+/g, ' ');
describe('RoomOutbox as rendered', () => {
beforeEach(() => {
const m = new Map<string, string>();
vi.stubGlobal('localStorage', {
getItem: (k: string) => m.get(k) ?? null,
setItem: (k: string, v: string) => {
m.set(k, v);
},
removeItem: (k: string) => {
m.delete(k);
},
});
});
afterEach(() => {
vi.unstubAllGlobals();
});
const mx = { getUserId: () => '@a:x' } as never;
const entry = (txnId: string, roomId: string, body: string) => ({
txnId,
roomId,
content: { body },
});
it('renders nothing when the room has no unconfirmed sends', () => {
enqueueOutbox('@a:x', entry('t1', '!other:x', 'elsewhere'));
expect(renderToStaticMarkup(<RoomOutbox mx={mx} roomId="!r:x" />)).toBe('');
});
it('shows a fresh send as Sending with its text and no Resend', () => {
enqueueOutbox('@a:x', entry('t1', '!r:x', 'on its way'), Date.now() + 1000);
const html = renderToStaticMarkup(<RoomOutbox mx={mx} roomId="!r:x" />);
expect(html).toContain('data-outbox-status="pending"');
expect(text(html)).toContain('Sending…');
expect(text(html)).toContain('on its way');
expect(text(html)).not.toContain('Resend');
});
it('shows a failed send with its error, Resend and Discard', () => {
enqueueOutbox('@a:x', entry('t2', '!r:x', 'did not go'));
failOutbox('@a:x', 't2', 'ECONNRESET');
const html = renderToStaticMarkup(<RoomOutbox mx={mx} roomId="!r:x" />);
expect(html).toContain('data-outbox-status="failed"');
expect(text(html)).toContain('Not sent: ECONNRESET');
expect(text(html)).toContain('Resend');
expect(text(html)).toContain('Discard');
});
it('shows a send left over from before this page load as interrupted, resendable', () => {
enqueueOutbox('@a:x', entry('t3', '!r:x', 'lost to a reload'), 5);
const html = renderToStaticMarkup(<RoomOutbox mx={mx} roomId="!r:x" />);
expect(text(html)).toContain('Interrupted by a reload');
expect(text(html)).toContain('Resend');
});
});

View file

@ -0,0 +1,70 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { loadOutbox } from '../app/state/room/composerStore';
import { resendInterrupted, sendTracked } from '../app/state/room/outboxSend';
import { RECOVER_RELOAD_MIN_GAP_MS, ownPendingAges, reloadToRecoverSync } from './initMatrix';
import { SEND_STALL_MS, WATCHDOG_INTERVAL_MS, startWatchdog } from './watchdog';
// The dead-connection story from the user's side: a send hangs, the page notices and reloads
// itself once, and the message goes out again from the outbox under its own txn id.
const memoryStore = () => {
const m = new Map<string, string>();
return {
getItem: (k: string) => m.get(k) ?? null,
setItem: (k: string, v: string) => {
m.set(k, v);
},
removeItem: (k: string) => {
m.delete(k);
},
};
};
describe('a send that hangs on a dead connection', () => {
beforeEach(() => {
vi.useFakeTimers();
vi.stubGlobal('localStorage', memoryStore());
});
afterEach(() => {
vi.useRealTimers();
vi.unstubAllGlobals();
});
it('is kept in the outbox, reloads the page once after the stall, and is resent under the same txn id', async () => {
const hung = vi.fn(() => new Promise<never>(() => {}));
const mx = {
getUserId: () => '@a:x',
makeTxnId: () => 'txn-1',
sendEvent: hung,
getRoom: () => undefined,
} as never;
const reload = vi.fn();
const session = memoryStore();
const content = { msgtype: 'm.text', body: 'hello from a dead connection' };
const stop = startWatchdog({
pendingAges: () => ownPendingAges('@a:x'),
isVisible: () => true,
isOnline: () => true,
recover: () => reloadToRecoverSync(Date.now(), () => session, reload),
});
// intentionally never settles: the request is hung
sendTracked(mx, '!r:x', content);
expect(loadOutbox('@a:x')).toMatchObject([{ txnId: 'txn-1', status: 'pending' }]);
vi.advanceTimersByTime(SEND_STALL_MS - WATCHDOG_INTERVAL_MS);
expect(reload).not.toHaveBeenCalled();
vi.advanceTimersByTime(2 * WATCHDOG_INTERVAL_MS);
expect(reload).toHaveBeenCalledTimes(1);
// the stall persists across the reload guard window: no reload loop
vi.advanceTimersByTime(RECOVER_RELOAD_MIN_GAP_MS - 2 * WATCHDOG_INTERVAL_MS);
expect(reload).toHaveBeenCalledTimes(1);
stop();
// the reloaded page: the entry is older than the page, the network works again
const send = vi.fn().mockResolvedValue({ event_id: '$e' });
const reloaded = { ...mx, sendEvent: send } as never;
await resendInterrupted(reloaded, Date.now() + 1);
expect(send).toHaveBeenCalledWith('!r:x', 'm.room.message', content, 'txn-1');
expect(loadOutbox('@a:x')).toEqual([]);
});
});