mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
9018ee8980
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.
357 lines
15 KiB
TypeScript
357 lines
15 KiB
TypeScript
// 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);
|
|
});
|
|
});
|