mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
test(atlas): cover the new Atlas from engine to hub views
SonarCloud measured 16.9% coverage on this PR's new code. Tests now cover the WebGL globe engine (boot, caching, input, camera, regions, prefetch) and renderer (against a fake WebGL2 context that reads the real shaders), the paint, labels, sky and pins, the central useAtlasGlobe hook and the hub hooks, the snapshot, account queue and Atlas repos, every desktop and phone hub view, the server's people, flights and mesh routes, the shared badge rules, and the new view transition, warm bags and kept planner maps.
This commit is contained in:
@@ -0,0 +1,356 @@
|
||||
// FE-ATLAS2-DATA-001 to FE-ATLAS2-DATA-030: the Atlas reads with the queued
|
||||
// writes laid over them, and a write that went through laid into the kept reads.
|
||||
import { continentForCountry } from '@trek/shared';
|
||||
import 'fake-indexeddb/auto';
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
|
||||
import { clearAll, offlineDb, type MutationStatus, type UserQueuedMutation } from '../db/offlineDb';
|
||||
import { ATLAS_RESOURCE, keepAtlasPart, keptAtlasPart, reconcileAtlasWrite, withWaitingWrites } from './atlasSnapshot';
|
||||
|
||||
let seq = 0;
|
||||
|
||||
/** A row in the account queue, the way userMutationQueue stores it. */
|
||||
async function queued(
|
||||
over: Partial<UserQueuedMutation> & Pick<UserQueuedMutation, 'resource' | 'entity' | 'method'>
|
||||
): Promise<UserQueuedMutation> {
|
||||
seq += 1;
|
||||
const row: UserQueuedMutation = {
|
||||
id: `row-${seq}`,
|
||||
url: '/addons/atlas/x',
|
||||
ids: {},
|
||||
body: null,
|
||||
createdAt: 1000 + seq,
|
||||
status: 'pending' as MutationStatus,
|
||||
attempts: 0,
|
||||
lastError: null,
|
||||
schemaVersion: 1,
|
||||
buildVersion: '',
|
||||
...over,
|
||||
};
|
||||
await offlineDb.userMutationQueue.put(row);
|
||||
return row;
|
||||
}
|
||||
|
||||
const country = (code: string, method: 'POST' | 'DELETE' = 'POST') => ({
|
||||
resource: ATLAS_RESOURCE.country,
|
||||
entity: `${ATLAS_RESOURCE.country}:${code}`,
|
||||
method,
|
||||
});
|
||||
|
||||
const region = (code: string, method: 'POST' | 'DELETE', body: unknown = null) => ({
|
||||
resource: ATLAS_RESOURCE.region,
|
||||
entity: `${ATLAS_RESOURCE.region}:${code}`,
|
||||
method,
|
||||
body,
|
||||
});
|
||||
|
||||
interface Stats {
|
||||
countries: Array<{ code: string; status?: string; placeCount?: number; tripCount?: number }>;
|
||||
stats?: Record<string, number | undefined>;
|
||||
continents?: Record<string, number>;
|
||||
continentsPlanned?: Record<string, number>;
|
||||
}
|
||||
|
||||
function baseStats(): Stats {
|
||||
return {
|
||||
countries: [
|
||||
{ code: 'PT', status: 'visited', placeCount: 3, tripCount: 1 },
|
||||
{ code: 'ES', status: 'planned', placeCount: 0, tripCount: 0 },
|
||||
],
|
||||
stats: { totalCountries: 1, totalCountriesPlanned: 1 },
|
||||
continents: { Europe: 1 },
|
||||
continentsPlanned: { Europe: 1 },
|
||||
};
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
await clearAll();
|
||||
seq = 0;
|
||||
});
|
||||
|
||||
describe('keeping a read', () => {
|
||||
it('FE-ATLAS2-DATA-001: a kept part reads back as stored, a part never kept is undefined', async () => {
|
||||
expect(await keptAtlasPart('stats')).toBeUndefined();
|
||||
await keepAtlasPart('stats', { countries: [] });
|
||||
expect((await keptAtlasPart('stats'))?.data).toEqual({ countries: [] });
|
||||
await keepAtlasPart('stats', { countries: [{ code: 'DE' }] });
|
||||
expect((await keptAtlasPart('stats'))?.data).toEqual({ countries: [{ code: 'DE' }] });
|
||||
});
|
||||
});
|
||||
|
||||
describe('countries over the stats read', () => {
|
||||
it('FE-ATLAS2-DATA-002: no waiting writes leave the read as it is', async () => {
|
||||
const data = baseStats();
|
||||
expect(await withWaitingWrites('stats', data)).toBe(data);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-003: marking a new country adds it visited and counts its continent', async () => {
|
||||
await queued(country('JP'));
|
||||
const out = (await withWaitingWrites('stats', baseStats())) as Stats;
|
||||
const jp = out.countries.find((c) => c.code === 'JP');
|
||||
expect(jp).toMatchObject({ code: 'JP', status: 'visited', placeCount: 0, tripCount: 0 });
|
||||
expect(out.stats?.totalCountries).toBe(2);
|
||||
expect(out.stats?.totalCountriesPlanned).toBe(1);
|
||||
expect(out.continents?.[continentForCountry('JP')]).toBe(1);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-004: marking a planned country moves it from the planned tally to the visited one', async () => {
|
||||
await queued(country('ES'));
|
||||
const out = (await withWaitingWrites('stats', baseStats())) as Stats;
|
||||
expect(out.countries.find((c) => c.code === 'ES')?.status).toBe('visited');
|
||||
expect(out.countries).toHaveLength(2);
|
||||
expect(out.stats).toMatchObject({ totalCountries: 2, totalCountriesPlanned: 0 });
|
||||
expect(out.continents?.Europe).toBe(2);
|
||||
expect(out.continentsPlanned?.Europe).toBe(0);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-005: marking a country already visited changes nothing, also one without a status', async () => {
|
||||
await queued(country('PT'));
|
||||
const data = baseStats();
|
||||
expect(await withWaitingWrites('stats', data)).toBe(data);
|
||||
|
||||
await clearAll();
|
||||
const noStatus: Stats = { countries: [{ code: 'FR' }], stats: { totalCountries: 1 } };
|
||||
await queued(country('FR'));
|
||||
expect(await withWaitingWrites('stats', noStatus)).toEqual(noStatus);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-006: a read without a countries list is not touched', async () => {
|
||||
await queued(country('JP'));
|
||||
await queued(country('PT', 'DELETE'));
|
||||
expect(await withWaitingWrites('stats', null)).toBeNull();
|
||||
expect(await withWaitingWrites('stats', { other: true })).toEqual({ other: true });
|
||||
expect(await withWaitingWrites('stats', [1, 2])).toEqual([1, 2]);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-007: marking into a read without totals starts the counts at zero', async () => {
|
||||
await queued(country('ES'));
|
||||
const out = (await withWaitingWrites('stats', {
|
||||
countries: [{ code: 'ES', status: 'planned' }],
|
||||
})) as Stats;
|
||||
expect(out.stats).toEqual({ totalCountries: 1, totalCountriesPlanned: 0 });
|
||||
expect(out.continents).toEqual({ Europe: 1 });
|
||||
expect(out.continentsPlanned).toEqual({ Europe: 0 });
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-008: unmarking a country with no trip and no place drops it and its counts', async () => {
|
||||
const data: Stats = {
|
||||
countries: [
|
||||
{ code: 'PT', status: 'visited', placeCount: 3, tripCount: 1 },
|
||||
{ code: 'IS', status: 'visited', placeCount: 0, tripCount: 0 },
|
||||
],
|
||||
stats: { totalCountries: 2 },
|
||||
continents: { Europe: 2 },
|
||||
};
|
||||
await queued(country('IS', 'DELETE'));
|
||||
const out = (await withWaitingWrites('stats', data)) as Stats;
|
||||
expect(out.countries.map((c) => c.code)).toEqual(['PT']);
|
||||
expect(out.stats?.totalCountries).toBe(1);
|
||||
expect(out.continents?.Europe).toBe(1);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-009: unmarking keeps a country that has places or trips, is unknown, or is only planned', async () => {
|
||||
const data = baseStats();
|
||||
await queued(country('PT', 'DELETE'));
|
||||
await queued(country('XX', 'DELETE'));
|
||||
await queued(country('ES', 'DELETE'));
|
||||
expect(await withWaitingWrites('stats', data)).toBe(data);
|
||||
|
||||
const tripsOnly: Stats = { countries: [{ code: 'IT', status: 'visited', tripCount: 2 }] };
|
||||
await queued(country('IT', 'DELETE'));
|
||||
expect(await withWaitingWrites('stats', tripsOnly)).toBe(tripsOnly);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-010: unmarking a country with no status counts it as visited and drops it, never below zero', async () => {
|
||||
await queued(country('IS', 'DELETE'));
|
||||
const out = (await withWaitingWrites('stats', { countries: [{ code: 'IS' }] })) as Stats;
|
||||
expect(out.countries).toEqual([]);
|
||||
expect(out.stats?.totalCountries).toBe(0);
|
||||
expect(out.continents?.Europe).toBe(0);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-011: a mark and its unmark laid over in order cancel out', async () => {
|
||||
await queued(country('JP'));
|
||||
await queued(country('JP', 'DELETE'));
|
||||
const out = (await withWaitingWrites('stats', baseStats())) as Stats;
|
||||
expect(out.countries.map((c) => c.code)).toEqual(['PT', 'ES']);
|
||||
expect(out.stats?.totalCountries).toBe(1);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-012: a country write with another method, or another resource, leaves the stats alone', async () => {
|
||||
await queued({ ...country('JP'), method: 'PUT' });
|
||||
await queued({ resource: ATLAS_RESOURCE.bucket, entity: `${ATLAS_RESOURCE.bucket}:3`, method: 'DELETE' });
|
||||
const data = baseStats();
|
||||
expect(await withWaitingWrites('stats', data)).toBe(data);
|
||||
});
|
||||
});
|
||||
|
||||
describe('which queued rows are laid over', () => {
|
||||
it('FE-ATLAS2-DATA-013: a parked row, and a write of another feature, are not laid over', async () => {
|
||||
await queued({ ...country('JP'), status: 'failed' });
|
||||
await queued({ resource: 'journey', entity: 'journey:JP', method: 'POST' });
|
||||
const data = baseStats();
|
||||
expect(await withWaitingWrites('stats', data)).toBe(data);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-014: a row that is syncing is still laid over', async () => {
|
||||
await queued({ ...country('JP'), status: 'syncing' });
|
||||
const out = (await withWaitingWrites('stats', baseStats())) as Stats;
|
||||
expect(out.countries.some((c) => c.code === 'JP')).toBe(true);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-015: writes are laid over oldest first, whatever order they were stored in', async () => {
|
||||
await queued({ ...country('JP', 'DELETE'), createdAt: 50 });
|
||||
await queued({ ...country('JP'), createdAt: 10 });
|
||||
const out = (await withWaitingWrites('stats', baseStats())) as Stats;
|
||||
// Mark at 10, unmark at 50: JP ends up gone.
|
||||
expect(out.countries.some((c) => c.code === 'JP')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('regions', () => {
|
||||
const regions = () => ({
|
||||
PT: [
|
||||
{ code: 'PT-11', name: 'Lisboa', placeCount: 4, status: 'visited' },
|
||||
{ code: 'PT-13', name: 'Porto', placeCount: 0, status: 'visited', manuallyMarked: true },
|
||||
],
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-016: marking a new region adds it under its country, named from the body', async () => {
|
||||
await queued(region('ES-CT', 'POST', { name: 'Catalunya', country_code: 'ES' }));
|
||||
const out = (await withWaitingWrites('regions', regions())) as Record<string, unknown[]>;
|
||||
expect(out.ES).toEqual([
|
||||
{ code: 'ES-CT', name: 'Catalunya', placeCount: 0, status: 'visited', manuallyMarked: true },
|
||||
]);
|
||||
expect(out.PT).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-017: a region mark without a name in the body is named by its code', async () => {
|
||||
await queued(region('ES-MD', 'POST', { country_code: 'ES' }));
|
||||
const out = (await withWaitingWrites('regions', {})) as Record<string, Array<{ name: string }>>;
|
||||
expect(out.ES[0].name).toBe('ES-MD');
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-018: marking a known region by hand keeps its places and sets the manual flag', async () => {
|
||||
await queued(region('PT-11', 'POST', { name: 'Lisboa', country_code: 'PT' }));
|
||||
const out = (await withWaitingWrites('regions', regions())) as ReturnType<typeof regions>;
|
||||
expect(out.PT[0]).toEqual({
|
||||
code: 'PT-11',
|
||||
name: 'Lisboa',
|
||||
placeCount: 4,
|
||||
status: 'visited',
|
||||
manuallyMarked: true,
|
||||
});
|
||||
expect(out.PT).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-019: marking a region already marked by hand changes nothing', async () => {
|
||||
await queued(region('PT-13', 'POST', { name: 'Porto', country_code: 'PT' }));
|
||||
const data = regions();
|
||||
expect(await withWaitingWrites('regions', data)).toBe(data);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-020: a region mark without a country code, or over a read that is no map, is ignored', async () => {
|
||||
await queued(region('ES-CT', 'POST', { name: 'Catalunya' }));
|
||||
await queued(region('ES-AN', 'POST', 'not an object'));
|
||||
const data = regions();
|
||||
expect(await withWaitingWrites('regions', data)).toBe(data);
|
||||
await clearAll();
|
||||
await queued(region('ES-CT', 'POST', { name: 'Catalunya', country_code: 'ES' }));
|
||||
expect(await withWaitingWrites('regions', [1])).toEqual([1]);
|
||||
expect(await withWaitingWrites('regions', null)).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-021: unmarking a region with places keeps it, only the manual flag goes', async () => {
|
||||
await queued(region('PT-11', 'DELETE'));
|
||||
const out = (await withWaitingWrites('regions', regions())) as ReturnType<typeof regions>;
|
||||
expect(out.PT[0]).toMatchObject({ code: 'PT-11', manuallyMarked: false, placeCount: 4 });
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-022: unmarking a region without places drops it', async () => {
|
||||
await queued(region('PT-13', 'DELETE'));
|
||||
const out = (await withWaitingWrites('regions', regions())) as ReturnType<typeof regions>;
|
||||
expect(out.PT.map((r) => r.code)).toEqual(['PT-11']);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-023: unmarking an unknown region, or over a read that is no map, changes nothing', async () => {
|
||||
await queued(region('DE-BY', 'DELETE'));
|
||||
const data = regions();
|
||||
expect(await withWaitingWrites('regions', data)).toBe(data);
|
||||
expect(await withWaitingWrites('regions', null)).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-024: a country write or a PUT on a region leaves the regions read alone', async () => {
|
||||
await queued(country('PT', 'DELETE'));
|
||||
await queued({ ...region('PT-11', 'POST', { country_code: 'PT' }), method: 'PUT' });
|
||||
const data = regions();
|
||||
expect(await withWaitingWrites('regions', data)).toBe(data);
|
||||
});
|
||||
});
|
||||
|
||||
describe('the bucket list and the other reads', () => {
|
||||
const bucket = { resource: ATLAS_RESOURCE.bucket, entity: `${ATLAS_RESOURCE.bucket}:7`, method: 'DELETE' as const };
|
||||
|
||||
it('FE-ATLAS2-DATA-025: a queued wish delete hides the wish', async () => {
|
||||
await queued(bucket);
|
||||
const out = await withWaitingWrites('bucketList', [{ id: 7 }, { id: 8 }, 'junk']);
|
||||
expect(out).toEqual([{ id: 8 }, 'junk']);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-026: a bucket POST, a country write, or a read that is no list leave the bucket list alone', async () => {
|
||||
await queued({ ...bucket, method: 'POST' });
|
||||
await queued(country('PT'));
|
||||
const list = [{ id: 7 }];
|
||||
expect(await withWaitingWrites('bucketList', list)).toBe(list);
|
||||
await queued(bucket);
|
||||
expect(await withWaitingWrites('bucketList', { id: 7 })).toEqual({ id: 7 });
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-027: the flights read is never changed by a write', async () => {
|
||||
await queued(country('PT'));
|
||||
await queued(bucket);
|
||||
const flights = { routes: [] };
|
||||
expect(await withWaitingWrites('flights', flights)).toBe(flights);
|
||||
});
|
||||
});
|
||||
|
||||
describe('a write that went through', () => {
|
||||
it('FE-ATLAS2-DATA-028: it is laid into every kept read it changes, and the others stay as they were', async () => {
|
||||
await keepAtlasPart('stats', baseStats());
|
||||
await keepAtlasPart('flights', { routes: [] });
|
||||
await keepAtlasPart('bucketList', [{ id: 7 }, { id: 9 }]);
|
||||
|
||||
await reconcileAtlasWrite({ ...country('JP'), ids: {}, body: null, createdAt: 1 });
|
||||
await reconcileAtlasWrite({
|
||||
resource: ATLAS_RESOURCE.bucket,
|
||||
entity: `${ATLAS_RESOURCE.bucket}:9`,
|
||||
method: 'DELETE',
|
||||
ids: {},
|
||||
body: null,
|
||||
createdAt: 2,
|
||||
answer: { success: true },
|
||||
});
|
||||
|
||||
const stats = (await keptAtlasPart('stats'))?.data as Stats;
|
||||
expect(stats.countries.map((c) => c.code)).toEqual(['PT', 'ES', 'JP']);
|
||||
expect((await keptAtlasPart('flights'))?.data).toEqual({ routes: [] });
|
||||
expect((await keptAtlasPart('bucketList'))?.data).toEqual([{ id: 7 }]);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-029: laying the same write in twice changes nothing more', async () => {
|
||||
await keepAtlasPart('regions', {});
|
||||
const w = { ...region('ES-CT', 'POST', { name: 'Catalunya', country_code: 'ES' }), ids: {}, createdAt: 1 };
|
||||
await reconcileAtlasWrite(w);
|
||||
const once = (await keptAtlasPart('regions'))?.data;
|
||||
await reconcileAtlasWrite(w);
|
||||
expect((await keptAtlasPart('regions'))?.data).toEqual(once);
|
||||
expect(once).toEqual({
|
||||
ES: [{ code: 'ES-CT', name: 'Catalunya', placeCount: 0, status: 'visited', manuallyMarked: true }],
|
||||
});
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-030: with nothing kept there is nothing to change', async () => {
|
||||
await reconcileAtlasWrite({ ...country('JP'), ids: {}, body: null, createdAt: 1 });
|
||||
expect(await offlineDb.atlasSnapshot.count()).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
// FE-ATLAS2-DATA-080 to FE-ATLAS2-DATA-082: the replay rules both write queues share.
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import {
|
||||
BUILD_VERSION,
|
||||
isRetryableStatus,
|
||||
MAX_SERVER_ERROR_ATTEMPTS,
|
||||
SERVER_ERROR_BACKOFF_MS,
|
||||
STUCK_SYNCING_MS,
|
||||
} from './queuePolicy';
|
||||
|
||||
describe('queuePolicy', () => {
|
||||
it('FE-ATLAS2-DATA-080: only an expired login, a timeout, too-early and rate limiting are worth waiting out', () => {
|
||||
for (const status of [401, 408, 425, 429]) expect(isRetryableStatus(status)).toBe(true);
|
||||
for (const status of [undefined, 200, 400, 403, 404, 409, 422, 500, 503])
|
||||
expect(isRetryableStatus(status)).toBe(false);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-081: the gap doubles from 30 s up to 32 minutes before the last server-error try', () => {
|
||||
expect(MAX_SERVER_ERROR_ATTEMPTS).toBe(8);
|
||||
expect(SERVER_ERROR_BACKOFF_MS).toBe(30_000);
|
||||
const lastGap = SERVER_ERROR_BACKOFF_MS * 2 ** (MAX_SERVER_ERROR_ATTEMPTS - 2);
|
||||
expect(lastGap).toBe(32 * 60_000);
|
||||
expect(STUCK_SYNCING_MS).toBe(60_000);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-082: the build stamp is a string, empty where the bundler defined none', () => {
|
||||
expect(typeof BUILD_VERSION).toBe('string');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,572 @@
|
||||
// FE-ATLAS2-DATA-040 to FE-ATLAS2-DATA-075: the account's own write queue (the
|
||||
// Atlas) - what has to wait, how a pass replays, and how it settles each answer.
|
||||
import 'fake-indexeddb/auto';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { server } from '../../tests/helpers/msw/server';
|
||||
import { clearAll, MUTATION_SCHEMA_VERSION, offlineDb, type UserQueuedMutation } from '../db/offlineDb';
|
||||
import { keepAtlasPart, keptAtlasPart } from './atlasSnapshot';
|
||||
import { setAuthed } from './authGate';
|
||||
import { mutationQueue } from './mutationQueue';
|
||||
import { _resetNetworkMode, setForcedOffline } from './networkMode';
|
||||
import { MAX_SERVER_ERROR_ATTEMPTS, SERVER_ERROR_BACKOFF_MS, STUCK_SYNCING_MS } from './queuePolicy';
|
||||
import { userMutationQueue } from './userMutationQueue';
|
||||
|
||||
function setOnline(value: boolean): void {
|
||||
Object.defineProperty(navigator, 'onLine', { value, writable: true, configurable: true });
|
||||
}
|
||||
|
||||
let seq = 0;
|
||||
|
||||
/** A row as the queue stores it, written straight into Dexie. */
|
||||
async function row(over: Partial<UserQueuedMutation> = {}): Promise<UserQueuedMutation> {
|
||||
seq += 1;
|
||||
const r: UserQueuedMutation = {
|
||||
id: `id-${seq}`,
|
||||
method: 'POST',
|
||||
url: `/addons/atlas/country/C${seq}/mark`,
|
||||
ids: {},
|
||||
body: null,
|
||||
resource: 'atlasCountry',
|
||||
entity: `atlasCountry:C${seq}`,
|
||||
createdAt: 100 + seq,
|
||||
status: 'pending',
|
||||
attempts: 0,
|
||||
lastError: null,
|
||||
schemaVersion: MUTATION_SCHEMA_VERSION,
|
||||
buildVersion: '',
|
||||
...over,
|
||||
};
|
||||
await offlineDb.userMutationQueue.put(r);
|
||||
return r;
|
||||
}
|
||||
|
||||
interface Sent {
|
||||
method: string;
|
||||
path: string;
|
||||
key: string | null;
|
||||
body: unknown;
|
||||
}
|
||||
|
||||
let sent: Sent[];
|
||||
|
||||
/** Answer every request below /api/addons/atlas with `answer(path)`, recording what came in. */
|
||||
function serve(answer: (path: string, method: string) => Response | Promise<Response>): void {
|
||||
server.use(
|
||||
http.all('/api/addons/atlas/*', async ({ request }) => {
|
||||
const path = new URL(request.url).pathname;
|
||||
const text = await request.text();
|
||||
sent.push({
|
||||
method: request.method,
|
||||
path,
|
||||
key: request.headers.get('X-Idempotency-Key'),
|
||||
body: text ? (JSON.parse(text) as unknown) : null,
|
||||
});
|
||||
return answer(path, request.method);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
const get = (id: string) => offlineDb.userMutationQueue.get(id);
|
||||
|
||||
beforeEach(async () => {
|
||||
await clearAll();
|
||||
mutationQueue._resetFlushing();
|
||||
_resetNetworkMode();
|
||||
setAuthed(true);
|
||||
setOnline(true);
|
||||
seq = 0;
|
||||
sent = [];
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
setAuthed(false);
|
||||
setOnline(true);
|
||||
_resetNetworkMode();
|
||||
});
|
||||
|
||||
describe('enqueue', () => {
|
||||
it('FE-ATLAS2-DATA-040: a queued write is stamped pending, with the format and a FIFO time', async () => {
|
||||
vi.spyOn(Date, 'now').mockReturnValue(5000);
|
||||
const id = await userMutationQueue.enqueue({
|
||||
id: 'a',
|
||||
method: 'POST',
|
||||
url: '/addons/atlas/country/DE/mark',
|
||||
ids: {},
|
||||
body: null,
|
||||
resource: 'atlasCountry',
|
||||
entity: 'atlasCountry:DE',
|
||||
});
|
||||
await userMutationQueue.enqueue({
|
||||
id: 'b',
|
||||
method: 'DELETE',
|
||||
url: '/addons/atlas/country/DE/mark',
|
||||
ids: {},
|
||||
body: null,
|
||||
resource: 'atlasCountry',
|
||||
entity: 'atlasCountry:DE',
|
||||
});
|
||||
|
||||
expect(id).toBe('a');
|
||||
const a = await get('a');
|
||||
expect(a).toMatchObject({
|
||||
status: 'pending',
|
||||
attempts: 0,
|
||||
lastError: null,
|
||||
schemaVersion: MUTATION_SCHEMA_VERSION,
|
||||
createdAt: 5000,
|
||||
});
|
||||
expect(typeof a?.buildVersion).toBe('string');
|
||||
// Same millisecond: the second one still sorts after the first.
|
||||
expect((await get('b'))?.createdAt).toBe(5001);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-041: the time counter starts over after a reset', async () => {
|
||||
vi.spyOn(Date, 'now').mockReturnValue(10);
|
||||
const input = {
|
||||
method: 'POST' as const,
|
||||
url: '/x',
|
||||
ids: {},
|
||||
body: null,
|
||||
resource: 'atlasCountry',
|
||||
entity: 'atlasCountry:X',
|
||||
};
|
||||
await userMutationQueue.enqueue({ ...input, id: 'one' });
|
||||
await userMutationQueue.enqueue({ ...input, id: 'two' });
|
||||
userMutationQueue._reset();
|
||||
await userMutationQueue.enqueue({ ...input, id: 'three' });
|
||||
expect((await get('two'))?.createdAt).toBe(11);
|
||||
expect((await get('three'))?.createdAt).toBe(10);
|
||||
});
|
||||
});
|
||||
|
||||
describe('mustQueue', () => {
|
||||
it('FE-ATLAS2-DATA-042: online with an empty queue a write goes straight out', async () => {
|
||||
expect(await userMutationQueue.mustQueue('atlasCountry:DE', [])).toBe(false);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-043: offline, by the device or by the switch, every write waits', async () => {
|
||||
setOnline(false);
|
||||
expect(await userMutationQueue.mustQueue('atlasCountry:DE', [])).toBe(true);
|
||||
setOnline(true);
|
||||
setForcedOffline(true);
|
||||
expect(await userMutationQueue.mustQueue(null, [])).toBe(true);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-044: a write naming a create still waiting here (negative id) waits', async () => {
|
||||
expect(await userMutationQueue.mustQueue('atlasBucket:-3', [5, -3])).toBe(true);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-045: any pending or syncing row puts a write in line, whatever its entity', async () => {
|
||||
await row({ entity: 'atlasCountry:FR', status: 'syncing' });
|
||||
expect(await userMutationQueue.mustQueue('atlasCountry:DE', [])).toBe(true);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-046: a parked row holds back only its own entity', async () => {
|
||||
await row({ entity: 'atlasCountry:FR', status: 'failed' });
|
||||
expect(await userMutationQueue.mustQueue('atlasCountry:DE', [])).toBe(false);
|
||||
expect(await userMutationQueue.mustQueue('atlasCountry:FR', [])).toBe(true);
|
||||
expect(await userMutationQueue.mustQueue(null, [])).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('flushPass replays', () => {
|
||||
it('FE-ATLAS2-DATA-047: a pending write is sent with its id as idempotency key, dropped and laid into the kept reads', async () => {
|
||||
await keepAtlasPart('stats', { countries: [], stats: { totalCountries: 0 } });
|
||||
serve(() => HttpResponse.json({ success: true }));
|
||||
const r = await row({ url: '/addons/atlas/country/DE/mark', entity: 'atlasCountry:DE', body: { a: 1 } });
|
||||
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
expect(sent).toEqual([{ method: 'POST', path: '/api/addons/atlas/country/DE/mark', key: r.id, body: { a: 1 } }]);
|
||||
expect(await get(r.id)).toBeUndefined();
|
||||
const stats = (await keptAtlasPart('stats'))?.data as { countries: Array<{ code: string }> };
|
||||
expect(stats.countries.map((c) => c.code)).toEqual(['DE']);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-048: rows go out oldest first', async () => {
|
||||
serve(() => HttpResponse.json({ success: true }));
|
||||
await row({ url: '/addons/atlas/country/B/mark', createdAt: 20 });
|
||||
await row({ url: '/addons/atlas/country/A/mark', createdAt: 10 });
|
||||
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
expect(sent.map((s) => s.path)).toEqual(['/api/addons/atlas/country/A/mark', '/api/addons/atlas/country/B/mark']);
|
||||
expect(await offlineDb.userMutationQueue.count()).toBe(0);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-049: a write that is no Atlas write is sent and dropped without touching the kept reads', async () => {
|
||||
await keepAtlasPart('bucketList', [{ id: 1 }]);
|
||||
serve(() => HttpResponse.json({ ok: true }));
|
||||
const r = await row({ method: 'DELETE', url: '/addons/atlas/bucket-list/1', resource: 'other', entity: 'other:1' });
|
||||
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
expect(sent).toHaveLength(1);
|
||||
expect(await get(r.id)).toBeUndefined();
|
||||
expect((await keptAtlasPart('bucketList'))?.data).toEqual([{ id: 1 }]);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-050: a create learns its real id, and the later writes naming it use it, in memory and on disk', async () => {
|
||||
serve((path, method) => {
|
||||
if (method === 'POST') return HttpResponse.json({ id: 42 });
|
||||
if (path.endsWith('/42') && method === 'PUT') return HttpResponse.json({ ok: true });
|
||||
return HttpResponse.json({ error: 'down' }, { status: 503 });
|
||||
});
|
||||
const create = await row({
|
||||
url: '/addons/atlas/bucket-list',
|
||||
resource: 'atlasBucket',
|
||||
entity: 'atlasBucket:-5',
|
||||
tempId: -5,
|
||||
createdAt: 1,
|
||||
});
|
||||
const edit = await row({
|
||||
method: 'PUT',
|
||||
url: '/addons/atlas/bucket-list/{id}',
|
||||
ids: { id: -5 },
|
||||
resource: 'atlasBucket',
|
||||
entity: 'atlasBucket:-5',
|
||||
createdAt: 2,
|
||||
});
|
||||
const remove = await row({
|
||||
method: 'DELETE',
|
||||
url: '/addons/atlas/bucket-list/{id}/thing',
|
||||
ids: { id: -5, other: 3 },
|
||||
resource: 'atlasBucket',
|
||||
entity: 'atlasBucket:-5',
|
||||
createdAt: 3,
|
||||
});
|
||||
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
expect(sent.map((s) => `${s.method} ${s.path}`)).toEqual([
|
||||
'POST /api/addons/atlas/bucket-list',
|
||||
'PUT /api/addons/atlas/bucket-list/42',
|
||||
'DELETE /api/addons/atlas/bucket-list/42/thing',
|
||||
]);
|
||||
expect(await get(create.id)).toBeUndefined();
|
||||
expect(await get(edit.id)).toBeUndefined();
|
||||
// The delete met a 503 and waits, already naming the real id.
|
||||
expect(await get(remove.id)).toMatchObject({
|
||||
ids: { id: 42, other: 3 },
|
||||
entity: 'atlasBucket:42',
|
||||
status: 'pending',
|
||||
});
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-051: a create answered without a numeric id teaches nothing; its dependent is parked', async () => {
|
||||
serve(() => HttpResponse.json({ id: 'not-a-number' }));
|
||||
await row({ url: '/addons/atlas/bucket-list', resource: 'atlasBucket', entity: 'atlasBucket:-1', tempId: -1 });
|
||||
const dep = await row({
|
||||
method: 'DELETE',
|
||||
url: '/addons/atlas/bucket-list/{id}',
|
||||
ids: { id: -1 },
|
||||
resource: 'atlasBucket',
|
||||
entity: 'atlasBucket:-1',
|
||||
});
|
||||
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
expect(sent).toHaveLength(1);
|
||||
expect(await get(dep.id)).toMatchObject({
|
||||
status: 'failed',
|
||||
attempts: 1,
|
||||
lastError: 'unresolved temp id (dependent create did not sync)',
|
||||
});
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-052: a write naming a create that never synced is parked and holds back its entity, not the others', async () => {
|
||||
serve(() => HttpResponse.json({ success: true }));
|
||||
const orphan = await row({
|
||||
method: 'DELETE',
|
||||
url: '/addons/atlas/bucket-list/{id}',
|
||||
ids: { id: -9 },
|
||||
resource: 'atlasBucket',
|
||||
entity: 'atlasBucket:-9',
|
||||
createdAt: 1,
|
||||
});
|
||||
const behind = await row({ resource: 'atlasBucket', entity: 'atlasBucket:-9', url: '/x/behind', createdAt: 2 });
|
||||
const other = await row({ url: '/addons/atlas/country/PT/mark', entity: 'atlasCountry:PT', createdAt: 3 });
|
||||
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
expect((await get(orphan.id))?.status).toBe('failed');
|
||||
expect((await get(behind.id))?.status).toBe('pending');
|
||||
expect(await get(other.id)).toBeUndefined();
|
||||
expect(sent.map((s) => s.path)).toEqual(['/api/addons/atlas/country/PT/mark']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('flushPass settles a failed replay', () => {
|
||||
it('FE-ATLAS2-DATA-053: a delete answered 404 did its job and is dropped; the pass goes on', async () => {
|
||||
serve((path) =>
|
||||
path.includes('bucket-list')
|
||||
? HttpResponse.json({ error: 'gone' }, { status: 404 })
|
||||
: HttpResponse.json({ success: true })
|
||||
);
|
||||
const del = await row({ method: 'DELETE', url: '/addons/atlas/bucket-list/7', entity: 'atlasBucket:7' });
|
||||
const next = await row();
|
||||
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
expect(await get(del.id)).toBeUndefined();
|
||||
expect(await get(next.id)).toBeUndefined();
|
||||
expect(sent).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-054: a refusal (4xx) is parked and holds back the later writes to that entity only', async () => {
|
||||
serve((path) =>
|
||||
path.includes('/DE/') ? HttpResponse.json({ error: 'no' }, { status: 409 }) : HttpResponse.json({ success: true })
|
||||
);
|
||||
const refused = await row({ url: '/addons/atlas/country/DE/mark', entity: 'atlasCountry:DE', createdAt: 1 });
|
||||
const sameEntity = await row({
|
||||
method: 'DELETE',
|
||||
url: '/addons/atlas/country/DE/mark',
|
||||
entity: 'atlasCountry:DE',
|
||||
createdAt: 2,
|
||||
});
|
||||
const otherEntity = await row({ url: '/addons/atlas/country/FR/mark', entity: 'atlasCountry:FR', createdAt: 3 });
|
||||
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
const parked = await get(refused.id);
|
||||
expect(parked).toMatchObject({ status: 'failed', attempts: 1 });
|
||||
expect(parked?.lastError).toContain('409');
|
||||
expect(parked?.retryAfter).toBeUndefined();
|
||||
expect((await get(sameEntity.id))?.status).toBe('pending');
|
||||
expect(await get(otherEntity.id)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-055: a 404 on a POST is a refusal, not a success', async () => {
|
||||
serve(() => HttpResponse.json({ error: 'missing' }, { status: 404 }));
|
||||
const r = await row();
|
||||
await userMutationQueue.flushPass();
|
||||
expect((await get(r.id))?.status).toBe('failed');
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-056: a retryable status puts the row back in line and stops the pass', async () => {
|
||||
serve(() => HttpResponse.json({ error: 'slow' }, { status: 408 }));
|
||||
const first = await row({ attempts: 2, createdAt: 1 });
|
||||
const second = await row({ createdAt: 2 });
|
||||
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
expect(sent).toHaveLength(1);
|
||||
const back = await get(first.id);
|
||||
expect(back).toMatchObject({ status: 'pending', attempts: 3 });
|
||||
expect(back?.lastError).toContain('408');
|
||||
expect(back?.retryAfter).toBeUndefined();
|
||||
expect((await get(second.id))?.status).toBe('pending');
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-057: a request that never reaches the server goes back in line and stops the pass', async () => {
|
||||
serve(() => HttpResponse.error());
|
||||
const first = await row({ createdAt: 1 });
|
||||
await row({ createdAt: 2 });
|
||||
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
expect(sent).toHaveLength(1);
|
||||
expect(await get(first.id)).toMatchObject({ status: 'pending', attempts: 1 });
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-058: a server error is retried later with a growing gap and stops the pass', async () => {
|
||||
serve(() => HttpResponse.json({ error: 'boom' }, { status: 500 }));
|
||||
const before = Date.now();
|
||||
const first = await row({ attempts: 2, createdAt: 1 });
|
||||
await row({ createdAt: 2 });
|
||||
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
expect(sent).toHaveLength(1);
|
||||
const back = await get(first.id);
|
||||
expect(back).toMatchObject({ status: 'pending', attempts: 3 });
|
||||
// Third try: 30 s doubled twice.
|
||||
expect(back?.retryAfter ?? 0).toBeGreaterThanOrEqual(before + SERVER_ERROR_BACKOFF_MS * 4);
|
||||
expect(back?.retryAfter ?? 0).toBeLessThan(Date.now() + SERVER_ERROR_BACKOFF_MS * 4 + 1);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-059: a server error on the last allowed try parks the write', async () => {
|
||||
serve(() => HttpResponse.json({ error: 'boom' }, { status: 502 }));
|
||||
const r = await row({ attempts: MAX_SERVER_ERROR_ATTEMPTS - 1 });
|
||||
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
expect(await get(r.id)).toMatchObject({ status: 'failed', attempts: MAX_SERVER_ERROR_ATTEMPTS });
|
||||
});
|
||||
});
|
||||
|
||||
describe('flushPass skips and stops', () => {
|
||||
it('FE-ATLAS2-DATA-060: a row whose retry time has not come stops the pass before sending', async () => {
|
||||
serve(() => HttpResponse.json({ success: true }));
|
||||
const waiting = await row({ retryAfter: Date.now() + 60_000, createdAt: 1 });
|
||||
await row({ createdAt: 2 });
|
||||
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
expect(sent).toHaveLength(0);
|
||||
expect((await get(waiting.id))?.status).toBe('pending');
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-061: a row whose retry time has passed is sent', async () => {
|
||||
serve(() => HttpResponse.json({ success: true }));
|
||||
const r = await row({ retryAfter: Date.now() - 1 });
|
||||
await userMutationQueue.flushPass();
|
||||
expect(sent).toHaveLength(1);
|
||||
expect(await get(r.id)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-062: a row from a newer format is left alone and holds back its entity', async () => {
|
||||
serve(() => HttpResponse.json({ success: true }));
|
||||
const newer = await row({ schemaVersion: MUTATION_SCHEMA_VERSION + 1, entity: 'atlasCountry:DE', createdAt: 1 });
|
||||
const behind = await row({ entity: 'atlasCountry:DE', createdAt: 2 });
|
||||
const other = await row({ entity: 'atlasCountry:FR', url: '/addons/atlas/country/FR/mark', createdAt: 3 });
|
||||
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
expect(await get(newer.id)).toMatchObject({ status: 'pending', attempts: 0 });
|
||||
expect((await get(behind.id))?.status).toBe('pending');
|
||||
expect(await get(other.id)).toBeUndefined();
|
||||
expect(sent.map((s) => s.path)).toEqual(['/api/addons/atlas/country/FR/mark']);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-063: a row parked earlier holds back the later writes to its entity, an older one still goes', async () => {
|
||||
serve(() => HttpResponse.json({ success: true }));
|
||||
await row({ status: 'failed', entity: 'atlasCountry:DE', createdAt: 10 });
|
||||
await row({ status: 'failed', entity: 'atlasCountry:DE', createdAt: 5 });
|
||||
const older = await row({ entity: 'atlasCountry:DE', url: '/addons/atlas/country/DE/older', createdAt: 1 });
|
||||
const later = await row({ entity: 'atlasCountry:DE', createdAt: 20 });
|
||||
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
expect(await get(older.id)).toBeUndefined();
|
||||
expect((await get(later.id))?.status).toBe('pending');
|
||||
expect(sent.map((s) => s.path)).toEqual(['/api/addons/atlas/country/DE/older']);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-064: a row stuck on syncing past the limit is replayed, a fresh one is not', async () => {
|
||||
serve(() => HttpResponse.json({ success: true }));
|
||||
const stuck = await row({ status: 'syncing', syncingSince: Date.now() - STUCK_SYNCING_MS - 1000 });
|
||||
const stuckUnstamped = await row({ status: 'syncing' });
|
||||
const fresh = await row({ status: 'syncing', syncingSince: Date.now() + 60_000 });
|
||||
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
expect(await get(stuck.id)).toBeUndefined();
|
||||
expect(await get(stuckUnstamped.id)).toBeUndefined();
|
||||
expect((await get(fresh.id))?.status).toBe('syncing');
|
||||
expect(sent).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-065: logged out or offline, a pass sends nothing', async () => {
|
||||
serve(() => HttpResponse.json({ success: true }));
|
||||
const r = await row();
|
||||
setAuthed(false);
|
||||
await userMutationQueue.flushPass();
|
||||
setAuthed(true);
|
||||
setForcedOffline(true);
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
expect(sent).toHaveLength(0);
|
||||
expect((await get(r.id))?.status).toBe('pending');
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-066: a logout during the pass stops it before the next row', async () => {
|
||||
serve(() => {
|
||||
setAuthed(false);
|
||||
return HttpResponse.json({ success: true });
|
||||
});
|
||||
await row({ createdAt: 1 });
|
||||
const second = await row({ createdAt: 2 });
|
||||
|
||||
await userMutationQueue.flushPass();
|
||||
|
||||
expect(sent).toHaveLength(1);
|
||||
expect((await get(second.id))?.status).toBe('pending');
|
||||
});
|
||||
});
|
||||
|
||||
describe('count, retry, discard', () => {
|
||||
it('FE-ATLAS2-DATA-067: count reads the rows in the given states', async () => {
|
||||
await row({ status: 'pending' });
|
||||
await row({ status: 'syncing' });
|
||||
await row({ status: 'failed' });
|
||||
expect(await userMutationQueue.count(['pending', 'syncing'])).toBe(2);
|
||||
expect(await userMutationQueue.count(['failed'])).toBe(1);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-068: retryFailed puts every parked row back in line from a fresh start', async () => {
|
||||
const parked = await row({ status: 'failed', attempts: 8, lastError: 'x', retryAfter: 99 });
|
||||
const pending = await row({ attempts: 2 });
|
||||
await userMutationQueue.retryFailed();
|
||||
expect(await get(parked.id)).toMatchObject({ status: 'pending', attempts: 0, lastError: null });
|
||||
expect((await get(parked.id))?.retryAfter).toBeUndefined();
|
||||
expect((await get(pending.id))?.attempts).toBe(2);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-069: discardFailed drops the parked rows only', async () => {
|
||||
const parked = await row({ status: 'failed' });
|
||||
const pending = await row();
|
||||
await userMutationQueue.discardFailed();
|
||||
expect(await get(parked.id)).toBeUndefined();
|
||||
expect(await get(pending.id)).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('through the trip queue', () => {
|
||||
it('FE-ATLAS2-DATA-070: the badge counts the account rows beside the trip rows', async () => {
|
||||
await row({ status: 'pending' });
|
||||
await row({ status: 'syncing' });
|
||||
await row({ status: 'failed' });
|
||||
expect(await mutationQueue.pendingCount()).toBe(2);
|
||||
expect(await mutationQueue.failedCount()).toBe(1);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-071: flush replays the account queue too', async () => {
|
||||
serve(() => HttpResponse.json({ success: true }));
|
||||
const r = await row();
|
||||
await mutationQueue.flush();
|
||||
expect(await get(r.id)).toBeUndefined();
|
||||
expect(sent).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-072: retryFailed puts a parked account row back and sends it', async () => {
|
||||
serve(() => HttpResponse.json({ success: true }));
|
||||
const r = await row({ status: 'failed', attempts: 3 });
|
||||
await mutationQueue.retryFailed();
|
||||
expect(await get(r.id)).toBeUndefined();
|
||||
expect(sent).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-073: discardFailed drops a parked account row and lets the one behind it go out', async () => {
|
||||
serve(() => HttpResponse.json({ success: true }));
|
||||
const parked = await row({ status: 'failed', entity: 'atlasCountry:DE', createdAt: 1 });
|
||||
const behind = await row({ entity: 'atlasCountry:DE', createdAt: 2 });
|
||||
await mutationQueue.discardFailed();
|
||||
expect(await get(parked.id)).toBeUndefined();
|
||||
expect(await get(behind.id)).toBeUndefined();
|
||||
expect(sent).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-074: logged out, flush leaves the account queue alone', async () => {
|
||||
serve(() => HttpResponse.json({ success: true }));
|
||||
const r = await row();
|
||||
setAuthed(false);
|
||||
await mutationQueue.flush();
|
||||
expect((await get(r.id))?.status).toBe('pending');
|
||||
expect(sent).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-DATA-075: the trip queue reset also resets the account time counter', async () => {
|
||||
vi.spyOn(Date, 'now').mockReturnValue(7);
|
||||
const input = { method: 'POST' as const, url: '/x', ids: {}, body: null, resource: 'atlasCountry', entity: 'e' };
|
||||
await userMutationQueue.enqueue({ ...input, id: 'p' });
|
||||
await userMutationQueue.enqueue({ ...input, id: 'q' });
|
||||
mutationQueue._resetFlushing();
|
||||
await userMutationQueue.enqueue({ ...input, id: 'r' });
|
||||
expect((await get('q'))?.createdAt).toBe(8);
|
||||
expect((await get('r'))?.createdAt).toBe(7);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user