initMatrix: resolve sessionStorage inside the guard's try; ownPendingAges tested
All checks were successful
Build pull request / Build pull request (pull_request) Successful in 3m39s
All checks were successful
Build pull request / Build pull request (pull_request) Successful in 3m39s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
69d752ff0b
commit
bb6391a70b
2 changed files with 45 additions and 16 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { RECOVER_RELOAD_MIN_GAP_MS, reloadToRecoverSync } from './initMatrix';
|
||||
import { PAGE_ID, enqueueOutbox, failOutbox } from '../app/state/room/composerStore';
|
||||
import { RECOVER_RELOAD_MIN_GAP_MS, ownPendingAges, reloadToRecoverSync } from './initMatrix';
|
||||
|
||||
const memory = () => {
|
||||
const m = new Map<string, string>();
|
||||
|
|
@ -15,11 +16,13 @@ describe('reloadToRecoverSync', () => {
|
|||
it('reloads once, then refuses inside the gap so a down server cannot loop it', () => {
|
||||
const store = memory();
|
||||
const reload = vi.fn();
|
||||
expect(reloadToRecoverSync(1_000_000, store, reload)).toBe(true);
|
||||
expect(reloadToRecoverSync(1_000_000 + RECOVER_RELOAD_MIN_GAP_MS - 1, store, reload)).toBe(
|
||||
false
|
||||
expect(reloadToRecoverSync(1_000_000, () => store, reload)).toBe(true);
|
||||
expect(
|
||||
reloadToRecoverSync(1_000_000 + RECOVER_RELOAD_MIN_GAP_MS - 1, () => store, reload)
|
||||
).toBe(false);
|
||||
expect(reloadToRecoverSync(1_000_000 + RECOVER_RELOAD_MIN_GAP_MS, () => store, reload)).toBe(
|
||||
true
|
||||
);
|
||||
expect(reloadToRecoverSync(1_000_000 + RECOVER_RELOAD_MIN_GAP_MS, store, reload)).toBe(true);
|
||||
expect(reload).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
|
@ -27,13 +30,35 @@ describe('reloadToRecoverSync', () => {
|
|||
describe('reloadToRecoverSync with unusable storage', () => {
|
||||
it('refuses instead of throwing when sessionStorage throws', () => {
|
||||
const reload = vi.fn();
|
||||
const broken = {
|
||||
getItem: () => {
|
||||
throw new Error('denied');
|
||||
},
|
||||
setItem: () => undefined,
|
||||
const denied = () => {
|
||||
throw new Error('SecurityError');
|
||||
};
|
||||
expect(reloadToRecoverSync(1, broken, reload)).toBe(false);
|
||||
expect(reloadToRecoverSync(1, denied, reload)).toBe(false);
|
||||
expect(reload).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('ownPendingAges', () => {
|
||||
it("counts only this page's pending sends, not other pages' or failed ones", () => {
|
||||
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);
|
||||
},
|
||||
});
|
||||
const e = (txnId: string) => ({ txnId, roomId: '!r:x', content: {} });
|
||||
enqueueOutbox('@a:x', e('mine'), 1000);
|
||||
enqueueOutbox('@a:x', e('failed'), 1000);
|
||||
failOutbox('@a:x', 'failed', 'x');
|
||||
enqueueOutbox('@a:x', e('theirs'), 1000);
|
||||
const entries = JSON.parse(m.get('ratatoskr.outbox.@a:x') as string);
|
||||
entries.find((x: { txnId: string }) => x.txnId === 'theirs').tab = `${PAGE_ID}-other`;
|
||||
m.set('ratatoskr.outbox.@a:x', JSON.stringify(entries));
|
||||
expect(ownPendingAges('@a:x', 1500)).toEqual([500]);
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -62,10 +62,11 @@ export const RECOVER_RELOAD_MIN_GAP_MS = 60_000;
|
|||
/** Reload to bring a dead connection back, at most once a minute, so a server that is down cannot loop us. */
|
||||
export const reloadToRecoverSync = (
|
||||
now: number = Date.now(),
|
||||
store: Pick<Storage, 'getItem' | 'setItem'> = sessionStorage,
|
||||
getStore: () => Pick<Storage, 'getItem' | 'setItem'> = () => sessionStorage,
|
||||
reload: () => void = () => window.location.reload()
|
||||
): boolean => {
|
||||
try {
|
||||
const store = getStore();
|
||||
const last = Number(store.getItem(RECOVER_RELOAD_KEY) ?? 0);
|
||||
if (now - last < RECOVER_RELOAD_MIN_GAP_MS) return false;
|
||||
store.setItem(RECOVER_RELOAD_KEY, String(now));
|
||||
|
|
@ -76,6 +77,12 @@ export const reloadToRecoverSync = (
|
|||
return true;
|
||||
};
|
||||
|
||||
/** Ages in ms of the sends this page made that are still pending: the only ones it may judge stalled. */
|
||||
export const ownPendingAges = (userId: string, now: number = Date.now()): number[] =>
|
||||
loadOutbox(userId)
|
||||
.filter((e) => e.status === 'pending' && e.tab === PAGE_ID)
|
||||
.map((e) => now - e.ts);
|
||||
|
||||
const started = new WeakSet<MatrixClient>();
|
||||
|
||||
export const startClient = async (mx: MatrixClient) => {
|
||||
|
|
@ -84,10 +91,7 @@ export const startClient = async (mx: MatrixClient) => {
|
|||
started.add(mx);
|
||||
startResumeSync(mx);
|
||||
startWatchdog({
|
||||
pendingAges: () =>
|
||||
loadOutbox(mx.getUserId() ?? '')
|
||||
.filter((e) => e.status === 'pending' && e.tab === PAGE_ID)
|
||||
.map((e) => Date.now() - e.ts),
|
||||
pendingAges: () => ownPendingAges(mx.getUserId() ?? ''),
|
||||
isVisible: () => document.visibilityState === 'visible',
|
||||
isOnline: () => navigator.onLine,
|
||||
recover: () => reloadToRecoverSync(),
|
||||
|
|
|
|||
Loading…
Reference in a new issue