Files
FlockPal/frontend/test/photoRefresh.test.ts
T
Corey Blais 693a30f437
Deploy / deploy-dev (push) Skipped
Deploy / deploy-prod (push) Successful in 3m57s
Invites and fixed image caching
2026-10-09 20:16:22 -04:00

61 lines
2.0 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { createPhotoRefresher } from '../src/photoRefresh.ts';
test('simultaneous image errors share a refresh, then can renew again after cooldown', async () => {
let now = 0;
let calls = 0;
let finish!: (photos: { id: string; photoDataUrl: string }[]) => void;
const applied: unknown[] = [];
const refresher = createPhotoRefresher({
now: () => now,
load: async () => { calls++; return new Promise((resolve) => { finish = resolve; }); },
apply: (photos) => applied.push(photos),
});
const first = refresher.refresh();
await refresher.refresh();
assert.equal(calls, 1);
finish([{ id: 'bird', photoDataUrl: 'renewed' }]);
await first;
assert.equal(applied.length, 1);
await refresher.refresh();
assert.equal(calls, 1);
now = 30_000;
const next = refresher.refresh();
finish([]);
await next;
assert.equal(calls, 2);
});
test('logout or flock switching aborts requests and discards stale photo responses', async () => {
let finish!: (photos: { id: string; photoDataUrl: string }[]) => void;
let signal!: AbortSignal;
let applied = false;
const refresher = createPhotoRefresher({
load: async (requestSignal) => { signal = requestSignal; return new Promise((resolve) => { finish = resolve; }); },
apply: () => { applied = true; },
});
const pending = refresher.refresh();
refresher.dispose();
finish([{ id: 'old-flock-bird', photoDataUrl: 'stale' }]);
await pending;
assert.equal(signal.aborted, true);
assert.equal(applied, false);
});
test('a failed refresh can recover on a later timer, focus, or reconnect', async () => {
let now = 0;
let attempts = 0;
let applied = false;
const refresher = createPhotoRefresher({
now: () => now,
load: async () => { if (++attempts === 1) throw new Error('offline'); return []; },
apply: () => { applied = true; },
});
await refresher.refresh();
assert.equal(applied, false);
now = 30_000;
await refresher.refresh();
assert.equal(applied, true);
});