mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 17:03:15 +00:00
14443a17be
A traveller who visited only Alberta saw all of Canada colored on the zoomed-out globe. A new layer switch, Regions instead of countries, keeps the coarse regions of every country with a visited region on screen at every zoom: the engine loads and holds them (setPinnedRegions), the renderer draws them at full strength with faint inner borders, and the country itself only tints faintly beneath them once they are drawn. The switch is remembered per device, on the desktop layers dialog and the phone sheet, in all 28 languages. Also without the switch, a country with visited regions now steps far back once its regions fade in (to about a fifth instead of two thirds), so the regions really visited stand out from the rest of the country.
1260 lines
47 KiB
TypeScript
1260 lines
47 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import type { AtlasMeshManifest, GlobeEngine, GlobeEngineOptions, RegionIndex } from './GlobeEngine';
|
|
import type { DrawState, GlobeTheme } from './GlobeRenderer';
|
|
import { latLimits, MAX_ZOOM, MIN_ZOOM, RAD, xSqueeze } from './globeMath';
|
|
import type { BuiltLayer, LineMesh } from './globeMesh';
|
|
import type { MeshRequest, MeshResponse } from './globeMesh.worker';
|
|
|
|
// FE-ATLAS2-ENGINE-001 to 113: the Atlas globe engine with a fake renderer, worker,
|
|
// resize observer and animation frame, driven through its public surface.
|
|
|
|
const fakes = vi.hoisted(() => {
|
|
class FakeRenderer {
|
|
static instances: FakeRenderer[] = [];
|
|
canvas: HTMLCanvasElement;
|
|
worlds = new Map<string, BuiltLayer>();
|
|
regions = new Map<string, BuiltLayer>();
|
|
pickId = -1;
|
|
setOcean = vi.fn();
|
|
setWorld = vi.fn<(key: string, layer: BuiltLayer) => void>((key, layer) => {
|
|
this.worlds.set(key, layer);
|
|
});
|
|
hasWorld = (key: string): boolean => this.worlds.has(key);
|
|
setRegion = vi.fn<(key: string, layer: BuiltLayer, inUse?: ReadonlySet<string>) => void>((key, layer) => {
|
|
this.regions.set(key, layer);
|
|
});
|
|
hasRegion = (key: string): boolean => this.regions.has(key);
|
|
touchRegion = vi.fn<(key: string) => void>();
|
|
draw = vi.fn<(s: DrawState) => void>();
|
|
pick = vi.fn<(x: number, y: number, includeRegions: boolean) => number>(() => this.pickId);
|
|
dispose = vi.fn();
|
|
constructor(canvas: HTMLCanvasElement) {
|
|
this.canvas = canvas;
|
|
FakeRenderer.instances.push(this);
|
|
}
|
|
}
|
|
return { FakeRenderer };
|
|
});
|
|
|
|
vi.mock('./GlobeRenderer', () => ({ GlobeRenderer: fakes.FakeRenderer, MAX_FEATURES: 256 * 64 }));
|
|
|
|
type FakeRenderer = InstanceType<typeof fakes.FakeRenderer>;
|
|
|
|
class FakeWorker {
|
|
static instances: FakeWorker[] = [];
|
|
onmessage: ((e: MessageEvent<MeshResponse>) => void) | null = null;
|
|
posted: MeshRequest[] = [];
|
|
open: MeshRequest[] = [];
|
|
terminate = vi.fn();
|
|
constructor() {
|
|
FakeWorker.instances.push(this);
|
|
}
|
|
postMessage(m: MeshRequest): void {
|
|
this.posted.push(m);
|
|
this.open.push(m);
|
|
}
|
|
reply(data: MeshResponse): void {
|
|
this.onmessage?.({ data } as MessageEvent<MeshResponse>);
|
|
}
|
|
/** Answers the open jobs that match, each with a layer tagged by what it built. */
|
|
answer(match: (m: MeshRequest) => boolean = () => true): void {
|
|
const now = this.open.filter(match);
|
|
this.open = this.open.filter((m) => !match(m));
|
|
for (const m of now) this.reply({ job: m.job, layer: layerOf(m) });
|
|
}
|
|
}
|
|
|
|
class FakeResizeObserver {
|
|
static instances: FakeResizeObserver[] = [];
|
|
cb: () => void;
|
|
observe = vi.fn();
|
|
disconnect = vi.fn();
|
|
constructor(cb: () => void) {
|
|
this.cb = cb;
|
|
FakeResizeObserver.instances.push(this);
|
|
}
|
|
}
|
|
|
|
const layerOf = (m: MeshRequest): BuiltLayer =>
|
|
({ tag: `${m.object}:${m.idBase}:${m.maxEdge}` }) as unknown as BuiltLayer;
|
|
const tagOf = (l: BuiltLayer | undefined): string => (l as unknown as { tag: string } | undefined)?.tag ?? '';
|
|
|
|
const LIGHT: GlobeTheme = {
|
|
land: [1, 1, 1],
|
|
ocean: [0, 0, 1],
|
|
oceanDeep: [0, 0, 0.5],
|
|
space0: [0, 0, 0],
|
|
space1: [0, 0, 0.1],
|
|
glow: [1, 1, 1],
|
|
border: [0, 0, 0, 1],
|
|
coast: [0, 0, 0, 1],
|
|
regionBorder: [0, 0, 0, 0.5],
|
|
selection: [1, 0, 0, 1],
|
|
arc: [1, 1, 0, 1],
|
|
dark: false,
|
|
};
|
|
const DARK: GlobeTheme = { ...LIGHT, dark: true };
|
|
|
|
const TOPO = { type: 'Topology', objects: {}, arcs: [] };
|
|
|
|
function manifest(): AtlasMeshManifest {
|
|
return {
|
|
version: 1,
|
|
lods: ['l0', 'l1', 'l2'],
|
|
regionLods: ['r0', 'r1'],
|
|
world: { l0: 'w0.json', l1: 'w1.json', l2: 'w2.json' },
|
|
countries: [
|
|
{ a2: 'FR', a3: 'FRA', name: 'France', anchor: [2, 46], bbox: [-5, 42, 8, 51], area: 1 },
|
|
{ a2: null, a3: 'XKX', name: 'Nowhere', anchor: null, bbox: [0, 0, 1, 1], area: 1 },
|
|
{ a2: 'DE', a3: 'DEU', name: 'Germany', anchor: null, bbox: [6, 47, 15, 55], area: 1 },
|
|
{ a2: 'AU', a3: 'AUS', name: 'Australia', anchor: [134, -25], bbox: [113, -44, 154, -10], area: 1 },
|
|
{ a2: 'IT', a3: 'ITA', name: 'Italy', anchor: [12, 43], bbox: [7, 37, 18, 47], area: 1 },
|
|
],
|
|
regions: {
|
|
FR: { bbox: [-5, 42, 8, 51], counted: 2, total: 2, index: 'regions/FR.json' },
|
|
DE: { bbox: [6, 47, 15, 55], counted: 1, total: 1, index: 'regions/DE.json' },
|
|
AU: { bbox: [113, -44, 154, -10], counted: 1, total: 1, index: 'regions/AU.json' },
|
|
},
|
|
attribution: [],
|
|
};
|
|
}
|
|
|
|
function regionIndex(a2: string, codes: Array<[string, string | null, string | null]>): RegionIndex {
|
|
return {
|
|
bbox: [0, 0, 1, 1],
|
|
counted: codes.length,
|
|
features: codes.map(([code, name, nameEn]) => ({ code, name, nameEn, anchor: null, counted: true })),
|
|
files: { r0: `regions/${a2}.r0.json`, r1: `regions/${a2}.r1.json` },
|
|
};
|
|
}
|
|
|
|
function defaultFiles(): Record<string, unknown> {
|
|
return {
|
|
manifest: manifest(),
|
|
'world/w0.json': TOPO,
|
|
'world/w1.json': TOPO,
|
|
'world/w2.json': TOPO,
|
|
'regions/FR.json': regionIndex('FR', [
|
|
['FR-IDF', 'Île-de-France', 'Paris Region'],
|
|
['FR-ARA', 'Auvergne', null],
|
|
]),
|
|
'regions/DE.json': regionIndex('DE', [['DE-BY', 'Bayern', 'Bavaria']]),
|
|
'regions/AU.json': regionIndex('AU', [['AU-NSW', 'NSW', null]]),
|
|
'regions/FR.r0.json': TOPO,
|
|
'regions/FR.r1.json': TOPO,
|
|
'regions/DE.r0.json': TOPO,
|
|
'regions/DE.r1.json': TOPO,
|
|
};
|
|
}
|
|
|
|
let clock = 1000;
|
|
let rafQueue: FrameRequestCallback[] = [];
|
|
const engines: GlobeEngine[] = [];
|
|
|
|
function last<T>(list: T[]): T {
|
|
const v = list[list.length - 1];
|
|
if (v === undefined) throw new Error('empty list');
|
|
return v;
|
|
}
|
|
|
|
async function flush(): Promise<void> {
|
|
for (let i = 0; i < 60; i++) await Promise.resolve();
|
|
}
|
|
|
|
/** Runs up to n queued animation frames, each dt ms after the last; returns how many ran. */
|
|
function frames(n: number, dt = 16): number {
|
|
for (let i = 0; i < n; i++) {
|
|
const cb = rafQueue.shift();
|
|
if (!cb) return i;
|
|
clock += dt;
|
|
cb(clock);
|
|
}
|
|
return n;
|
|
}
|
|
|
|
function lastDraw(r: FakeRenderer): DrawState {
|
|
return last(r.draw.mock.calls)[0];
|
|
}
|
|
|
|
interface StartOptions {
|
|
initial?: GlobeEngineOptions['initial'];
|
|
theme?: GlobeTheme;
|
|
width?: number;
|
|
height?: number;
|
|
/** Changes the served files before the engine starts reading them. */
|
|
prepare?: (files: Record<string, unknown>) => void;
|
|
}
|
|
|
|
/** A manifest that never arrives, for what the engine does before it has one. */
|
|
const NEVER = new Promise<unknown>(() => undefined);
|
|
|
|
async function start(o: StartOptions = {}) {
|
|
const mod = await import('./GlobeEngine');
|
|
const files = defaultFiles();
|
|
o.prepare?.(files);
|
|
const fetchJson = vi.fn(async (path: string): Promise<unknown> => {
|
|
const v = files[path];
|
|
if (v === undefined) throw new Error(`404 ${path}`);
|
|
if (v instanceof Error) throw v;
|
|
return v;
|
|
});
|
|
const rect = { width: o.width ?? 800, height: o.height ?? 600 };
|
|
const canvas = document.createElement('canvas');
|
|
canvas.getBoundingClientRect = () =>
|
|
({ x: 0, y: 0, left: 0, top: 0, right: rect.width, bottom: rect.height, ...rect, toJSON: () => rect }) as DOMRect;
|
|
canvas.setPointerCapture = vi.fn();
|
|
const cb = {
|
|
onReady: vi.fn(),
|
|
onError: vi.fn<(err: unknown) => void>(),
|
|
onHover: vi.fn(),
|
|
onSelect: vi.fn(),
|
|
onFrame: vi.fn(),
|
|
};
|
|
const engine = new mod.GlobeEngine({ canvas, fetchJson, theme: o.theme ?? LIGHT, initial: o.initial, ...cb });
|
|
engines.push(engine);
|
|
return {
|
|
mod,
|
|
engine,
|
|
canvas,
|
|
rect,
|
|
files,
|
|
fetchJson,
|
|
cb,
|
|
renderer: last(fakes.FakeRenderer.instances),
|
|
worker: last(FakeWorker.instances),
|
|
resizeObs: last(FakeResizeObserver.instances),
|
|
};
|
|
}
|
|
|
|
type Ctx = Awaited<ReturnType<typeof start>>;
|
|
|
|
/** Lets the manifest and the world file arrive and answers the world build. */
|
|
async function boot(ctx: Ctx): Promise<void> {
|
|
await flush();
|
|
ctx.worker.answer();
|
|
await flush();
|
|
}
|
|
|
|
const fetched = (ctx: Ctx, path: string): number => ctx.fetchJson.mock.calls.filter(([p]) => p === path).length;
|
|
|
|
function pointer(canvas: HTMLCanvasElement, type: string, id: number, x: number, y: number): void {
|
|
const e = new MouseEvent(type, { clientX: x, clientY: y, bubbles: true });
|
|
Object.defineProperty(e, 'pointerId', { value: id });
|
|
canvas.dispatchEvent(e);
|
|
}
|
|
|
|
function wheel(canvas: HTMLCanvasElement, deltaY: number, x = 400, y = 300, deltaMode = 0): WheelEvent {
|
|
const e = new WheelEvent('wheel', { deltaY, deltaMode, clientX: x, clientY: y, cancelable: true });
|
|
canvas.dispatchEvent(e);
|
|
return e;
|
|
}
|
|
|
|
/** Captures window.setTimeout calls instead of running them. */
|
|
function captureTimeouts(): Array<{ cb: () => void; ms: number }> {
|
|
const pending: Array<{ cb: () => void; ms: number }> = [];
|
|
vi.spyOn(window, 'setTimeout').mockImplementation(((cb: () => void, ms?: number) => {
|
|
pending.push({ cb, ms: ms ?? 0 });
|
|
return 100000 + pending.length;
|
|
}) as unknown as typeof window.setTimeout);
|
|
return pending;
|
|
}
|
|
|
|
beforeEach(() => {
|
|
clock = 1000;
|
|
rafQueue = [];
|
|
FakeWorker.instances = [];
|
|
FakeResizeObserver.instances = [];
|
|
fakes.FakeRenderer.instances = [];
|
|
vi.spyOn(performance, 'now').mockImplementation(() => clock);
|
|
vi.stubGlobal('Worker', FakeWorker);
|
|
vi.stubGlobal('ResizeObserver', FakeResizeObserver);
|
|
vi.stubGlobal(
|
|
'requestAnimationFrame',
|
|
vi.fn((cb: FrameRequestCallback) => {
|
|
rafQueue.push(cb);
|
|
return rafQueue.length;
|
|
})
|
|
);
|
|
vi.stubGlobal('cancelAnimationFrame', vi.fn());
|
|
vi.stubGlobal('devicePixelRatio', 1);
|
|
vi.resetModules();
|
|
});
|
|
|
|
afterEach(() => {
|
|
for (const e of engines.splice(0)) e.dispose();
|
|
vi.restoreAllMocks();
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe('GlobeEngine boot', () => {
|
|
it('FE-ATLAS2-ENGINE-001: reads the manifest, builds the coarse world in the worker and reports ready', async () => {
|
|
const ctx = await start();
|
|
expect(ctx.renderer.setOcean).toHaveBeenCalledTimes(1);
|
|
expect(ctx.resizeObs.observe).toHaveBeenCalledWith(ctx.canvas);
|
|
await flush();
|
|
expect(ctx.fetchJson.mock.calls.map(([p]) => p)).toEqual(['manifest', 'world/w0.json']);
|
|
expect(ctx.worker.posted).toEqual([{ job: 1, topo: TOPO, object: 'countries', maxEdge: 8, idBase: 0 }]);
|
|
expect(ctx.cb.onReady).not.toHaveBeenCalled();
|
|
ctx.worker.answer();
|
|
await flush();
|
|
expect(ctx.engine.manifest?.countries).toHaveLength(5);
|
|
expect(tagOf(ctx.renderer.worlds.get('l0'))).toBe('countries:0:8');
|
|
expect(ctx.cb.onReady).toHaveBeenCalledTimes(1);
|
|
expect(ctx.cb.onError).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-002: a second visit in the same tab reuses the kept manifest and world without fetching', async () => {
|
|
const first = await start();
|
|
await boot(first);
|
|
const firstLayer = first.renderer.worlds.get('l0');
|
|
const otherFetch = vi.fn(async (): Promise<unknown> => null);
|
|
const canvas = document.createElement('canvas');
|
|
const onReady = vi.fn();
|
|
const second = new first.mod.GlobeEngine({ canvas, fetchJson: otherFetch, theme: LIGHT, onReady });
|
|
engines.push(second);
|
|
await flush();
|
|
expect(otherFetch).not.toHaveBeenCalled();
|
|
expect(last(FakeWorker.instances).posted).toEqual([]);
|
|
expect(last(fakes.FakeRenderer.instances).worlds.get('l0')).toBe(firstLayer);
|
|
expect(onReady).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-003: a world file that keeps failing is retried four times, then reported', async () => {
|
|
const pending = captureTimeouts();
|
|
const ctx = await start({ prepare: (f) => (f['world/w0.json'] = new Error('offline')) });
|
|
for (let i = 0; i < 4; i++) {
|
|
await flush();
|
|
const t = pending.shift();
|
|
if (!t) throw new Error('no retry scheduled');
|
|
t.cb();
|
|
}
|
|
await flush();
|
|
expect(fetched(ctx, 'world/w0.json')).toBe(5);
|
|
expect(ctx.cb.onError).toHaveBeenCalledWith(expect.objectContaining({ message: 'offline' }));
|
|
expect(ctx.cb.onReady).not.toHaveBeenCalled();
|
|
expect(pending).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('GlobeEngine boot retries', () => {
|
|
it('FE-ATLAS2-ENGINE-004: the retry pauses double, and a manifest that failed is asked for again next visit', async () => {
|
|
const pending = captureTimeouts();
|
|
const mod = await import('./GlobeEngine');
|
|
const fail = vi.fn(async (): Promise<unknown> => {
|
|
throw new Error('down');
|
|
});
|
|
const onError = vi.fn();
|
|
engines.push(
|
|
new mod.GlobeEngine({ canvas: document.createElement('canvas'), fetchJson: fail, theme: LIGHT, onError })
|
|
);
|
|
const delays: number[] = [];
|
|
for (let i = 0; i < 4; i++) {
|
|
await flush();
|
|
const t = pending.shift();
|
|
if (!t) throw new Error('no retry scheduled');
|
|
delays.push(t.ms);
|
|
t.cb();
|
|
}
|
|
await flush();
|
|
expect(delays).toEqual([1000, 2000, 4000, 8000]);
|
|
expect(fail).toHaveBeenCalledTimes(5);
|
|
expect(onError).toHaveBeenCalledWith(expect.objectContaining({ message: 'down' }));
|
|
|
|
const files = defaultFiles();
|
|
const ok = vi.fn(async (p: string): Promise<unknown> => files[p]);
|
|
const onReady = vi.fn();
|
|
engines.push(
|
|
new mod.GlobeEngine({ canvas: document.createElement('canvas'), fetchJson: ok, theme: LIGHT, onReady })
|
|
);
|
|
await flush();
|
|
expect(ok).toHaveBeenCalledWith('manifest');
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-005: one failed read followed by a good one still brings the globe up', async () => {
|
|
const pending = captureTimeouts();
|
|
const mod = await import('./GlobeEngine');
|
|
const files = defaultFiles();
|
|
let calls = 0;
|
|
const fetchJson = vi.fn(async (p: string): Promise<unknown> => {
|
|
calls++;
|
|
if (calls === 1) throw new Error('blip');
|
|
return files[p];
|
|
});
|
|
const onReady = vi.fn();
|
|
engines.push(new mod.GlobeEngine({ canvas: document.createElement('canvas'), fetchJson, theme: LIGHT, onReady }));
|
|
await flush();
|
|
expect(pending.map((t) => t.ms)).toEqual([1000]);
|
|
pending[0].cb();
|
|
await flush();
|
|
last(FakeWorker.instances).answer();
|
|
await flush();
|
|
expect(onReady).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-006: a dispose while waiting to retry stops the retries', async () => {
|
|
const pending = captureTimeouts();
|
|
const ctx = await start({ prepare: (f) => (f.manifest = new Error('gone')) });
|
|
await flush();
|
|
ctx.engine.dispose();
|
|
const t = pending.shift();
|
|
if (!t) throw new Error('no retry scheduled');
|
|
t.cb();
|
|
await flush();
|
|
expect(fetched(ctx, 'manifest')).toBe(2);
|
|
expect(ctx.cb.onError).toHaveBeenCalledWith(expect.objectContaining({ message: 'gone' }));
|
|
expect(pending).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('GlobeEngine worker builds', () => {
|
|
it('FE-ATLAS2-ENGINE-010: a build the worker refuses reports its error through onError', async () => {
|
|
const ctx = await start();
|
|
await flush();
|
|
ctx.worker.reply({ job: 1, error: 'bad topology' });
|
|
await flush();
|
|
expect(ctx.cb.onError).toHaveBeenCalledWith(expect.objectContaining({ message: 'bad topology' }));
|
|
expect(ctx.cb.onReady).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-011: a refusal without a message falls back to a generic one', async () => {
|
|
const ctx = await start();
|
|
await flush();
|
|
ctx.worker.reply({ job: 1 });
|
|
await flush();
|
|
expect(ctx.cb.onError).toHaveBeenCalledWith(expect.objectContaining({ message: 'Atlas mesh build failed' }));
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-012: an answer for an unknown job is ignored', async () => {
|
|
const ctx = await start();
|
|
await flush();
|
|
ctx.worker.reply({ job: 99, layer: layerOf(ctx.worker.posted[0]) });
|
|
await flush();
|
|
expect(ctx.renderer.setWorld).not.toHaveBeenCalled();
|
|
ctx.worker.answer();
|
|
await flush();
|
|
expect(ctx.cb.onReady).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-013: a world that arrives after dispose is not handed to the renderer', async () => {
|
|
const ctx = await start();
|
|
await flush();
|
|
ctx.engine.dispose();
|
|
ctx.worker.answer();
|
|
await flush();
|
|
expect(ctx.renderer.setWorld).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-014: zooming past 2 and 4.2 loads the finer world levels and draws them', async () => {
|
|
const ctx = await start({ initial: { zoom: 2.5 } });
|
|
await boot(ctx);
|
|
frames(1);
|
|
await flush();
|
|
expect(fetched(ctx, 'world/w1.json')).toBe(1);
|
|
expect(last(ctx.worker.posted)).toMatchObject({ object: 'countries', maxEdge: 4 });
|
|
// Until it arrives the coarse level stays on screen.
|
|
expect(lastDraw(ctx.renderer).worldKey).toBe('l0');
|
|
ctx.worker.answer();
|
|
await flush();
|
|
frames(1);
|
|
expect(lastDraw(ctx.renderer).worldKey).toBe('l1');
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-015: a finer world that fails keeps the coarser one and raises no error', async () => {
|
|
const ctx = await start({ initial: { zoom: 2.5 } });
|
|
await boot(ctx);
|
|
frames(1);
|
|
await flush();
|
|
ctx.worker.reply({ job: last(ctx.worker.posted).job, error: 'nope' });
|
|
ctx.worker.open = [];
|
|
await flush();
|
|
frames(1);
|
|
expect(lastDraw(ctx.renderer).worldKey).toBe('l0');
|
|
expect(ctx.cb.onError).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-016: with only a finer level loaded, a low zoom still draws that level', async () => {
|
|
const ctx = await start();
|
|
ctx.renderer.worlds.set('l1', { tag: 'l1' } as unknown as BuiltLayer);
|
|
frames(1);
|
|
expect(lastDraw(ctx.renderer).worldKey).toBe('l1');
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-017: before any world is loaded the draw asks for the coarse level', async () => {
|
|
const ctx = await start();
|
|
frames(1);
|
|
expect(lastDraw(ctx.renderer).worldKey).toBe('l0');
|
|
expect(ctx.cb.onFrame).toHaveBeenCalledWith(ctx.engine);
|
|
});
|
|
});
|
|
|
|
describe('GlobeEngine regions in view', () => {
|
|
it('FE-ATLAS2-ENGINE-020: zoomed in on France, the coarse regions of the countries in view load and draw', async () => {
|
|
const ctx = await start({ initial: { lon: 2, lat: 46, zoom: 6 } });
|
|
const loaded = vi.fn();
|
|
ctx.engine.onRegionLoaded = loaded;
|
|
await boot(ctx);
|
|
frames(1);
|
|
expect(ctx.renderer.touchRegion.mock.calls.map(([k]) => k)).toEqual(['FR:r0', 'FR:r1', 'DE:r0', 'DE:r1']);
|
|
await flush();
|
|
expect(ctx.engine.regionBase.get('FR')).toBe(512);
|
|
expect(ctx.engine.regionBase.get('DE')).toBe(514);
|
|
expect(fetched(ctx, 'regions/AU.json')).toBe(0);
|
|
expect(fetched(ctx, 'regions/FR.r1.json')).toBe(0);
|
|
const regionJobs = ctx.worker.posted.filter((m) => m.object === 'regions');
|
|
expect(regionJobs.map((m) => [m.idBase, m.maxEdge])).toEqual([
|
|
[512, 3],
|
|
[514, 3],
|
|
]);
|
|
ctx.worker.answer();
|
|
await flush();
|
|
const inUse = ctx.renderer.setRegion.mock.calls[0][2];
|
|
expect(inUse?.has('FR:r0')).toBe(true);
|
|
expect(inUse?.has('AU:r0')).toBe(false);
|
|
expect(loaded.mock.calls.map(([a2]) => a2)).toEqual(['FR', 'DE']);
|
|
frames(1);
|
|
const s = lastDraw(ctx.renderer);
|
|
expect(s.regionKeys).toEqual(['FR:r0', 'DE:r0']);
|
|
expect(s.regionAlpha).toBe(1);
|
|
expect(s.worldKey).toBe('l2');
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-114: pinned countries load and draw their coarse regions on the whole globe', async () => {
|
|
const ctx = await start({ initial: { lon: 2, lat: 46, zoom: 1 } });
|
|
await boot(ctx);
|
|
ctx.engine.setPinnedRegions(['FR']);
|
|
frames(1);
|
|
await flush();
|
|
expect(fetched(ctx, 'regions/FR.r0.json')).toBe(1);
|
|
expect(ctx.renderer.touchRegion.mock.calls.map(([k]) => k)).toContain('FR:r0');
|
|
ctx.worker.answer();
|
|
await flush();
|
|
const inUse = ctx.renderer.setRegion.mock.calls[0][2];
|
|
expect(inUse?.has('FR:r0')).toBe(true);
|
|
frames(1);
|
|
const pinned = lastDraw(ctx.renderer);
|
|
expect(pinned.regionAlpha).toBe(0);
|
|
expect(pinned.regionKeys).toEqual(['FR:r0']);
|
|
expect(pinned.pinnedRegions?.has('FR')).toBe(true);
|
|
|
|
ctx.engine.setPinnedRegions([]);
|
|
frames(1);
|
|
expect(lastDraw(ctx.renderer).regionKeys).toEqual([]);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-021: the view is scanned at most every 0.2 s and a loaded region is not fetched twice', async () => {
|
|
const ctx = await start({ initial: { lon: 2, lat: 46, zoom: 6 } });
|
|
await boot(ctx);
|
|
frames(1);
|
|
await flush();
|
|
ctx.worker.answer();
|
|
await flush();
|
|
const touches = ctx.renderer.touchRegion.mock.calls.length;
|
|
frames(1, 16);
|
|
expect(ctx.renderer.touchRegion.mock.calls.length).toBe(touches);
|
|
frames(1, 250);
|
|
await flush();
|
|
expect(ctx.renderer.touchRegion.mock.calls.length).toBe(touches + 4);
|
|
expect(fetched(ctx, 'regions/FR.r0.json')).toBe(1);
|
|
expect(fetched(ctx, 'regions/FR.json')).toBe(1);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-022: from zoom 7 the fine regions load too and win once the zoom passes 8', async () => {
|
|
const ctx = await start({ initial: { lon: 2, lat: 46, zoom: 9 } });
|
|
await boot(ctx);
|
|
frames(1);
|
|
await flush();
|
|
expect(fetched(ctx, 'regions/FR.r1.json')).toBe(1);
|
|
ctx.worker.answer((m) => m.object === 'regions' && m.maxEdge === 3);
|
|
await flush();
|
|
frames(1);
|
|
expect(lastDraw(ctx.renderer).regionKeys).toEqual(['FR:r0', 'DE:r0']);
|
|
ctx.worker.answer();
|
|
await flush();
|
|
frames(1);
|
|
expect(lastDraw(ctx.renderer).regionKeys).toEqual(['FR:r1', 'DE:r1']);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-023: below zoom 8 a country with only its fine regions still draws those', async () => {
|
|
const ctx = await start({ initial: { lon: 2, lat: 46, zoom: 7.5 } });
|
|
await boot(ctx);
|
|
frames(1);
|
|
await flush();
|
|
ctx.worker.answer((m) => m.object === 'regions' && m.maxEdge === 1.5);
|
|
await flush();
|
|
frames(1);
|
|
expect(lastDraw(ctx.renderer).regionKeys).toEqual(['FR:r1', 'DE:r1']);
|
|
});
|
|
});
|
|
|
|
describe('GlobeEngine region prefetch and failures', () => {
|
|
it('FE-ATLAS2-ENGINE-024: regions are fetched ahead from zoom 3 but stay undrawn until they fade in', async () => {
|
|
const ctx = await start({ initial: { lon: 2, lat: 46, zoom: 4 } });
|
|
await boot(ctx);
|
|
frames(1);
|
|
await flush();
|
|
ctx.worker.answer();
|
|
await flush();
|
|
frames(1);
|
|
expect(ctx.renderer.hasRegion('FR:r0')).toBe(true);
|
|
expect(lastDraw(ctx.renderer).regionKeys).toEqual([]);
|
|
expect(lastDraw(ctx.renderer).regionAlpha).toBe(0);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-025: a zoom heading past 3 already fetches while the camera is still lower', async () => {
|
|
const ctx = await start({ initial: { lon: 2, lat: 46, zoom: 2 } });
|
|
await boot(ctx);
|
|
ctx.engine.zoomBy(2);
|
|
frames(1);
|
|
expect(ctx.engine.zoom).toBeLessThan(3);
|
|
await flush();
|
|
expect(fetched(ctx, 'regions/FR.json')).toBe(1);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-026: a region file that fails is asked for again only after 20 s', async () => {
|
|
const ctx = await start({ initial: { lon: 2, lat: 46, zoom: 6 } });
|
|
ctx.files['regions/FR.r0.json'] = new Error('500');
|
|
await boot(ctx);
|
|
frames(1);
|
|
await flush();
|
|
expect(fetched(ctx, 'regions/FR.r0.json')).toBe(1);
|
|
expect(ctx.cb.onError).not.toHaveBeenCalled();
|
|
frames(1, 1000);
|
|
await flush();
|
|
expect(fetched(ctx, 'regions/FR.r0.json')).toBe(1);
|
|
frames(1, 21000);
|
|
await flush();
|
|
expect(fetched(ctx, 'regions/FR.r0.json')).toBe(2);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-027: a region built after dispose is dropped', async () => {
|
|
const ctx = await start({ initial: { lon: 2, lat: 46, zoom: 6 } });
|
|
await boot(ctx);
|
|
frames(1);
|
|
await flush();
|
|
ctx.engine.dispose();
|
|
ctx.worker.answer();
|
|
await flush();
|
|
expect(ctx.renderer.setRegion).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-028: a country whose regions would overflow the status texture gets no ids and no file', async () => {
|
|
const ctx = await start({ initial: { lon: 2, lat: 46, zoom: 6 } });
|
|
const huge = regionIndex('FR', []);
|
|
huge.features = Array.from({ length: 20000 }, (_, i) => ({
|
|
code: `X${i}`,
|
|
name: null,
|
|
nameEn: null,
|
|
anchor: null,
|
|
counted: false,
|
|
}));
|
|
ctx.files['regions/FR.json'] = huge;
|
|
await boot(ctx);
|
|
frames(1);
|
|
await flush();
|
|
expect(ctx.engine.regionIndex.get('FR')).toBe(huge);
|
|
expect(ctx.engine.regionBase.has('FR')).toBe(false);
|
|
expect(ctx.engine.regionBase.get('DE')).toBe(512);
|
|
expect(fetched(ctx, 'regions/FR.r0.json')).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('GlobeEngine region index', () => {
|
|
it('FE-ATLAS2-ENGINE-030: before the manifest and for a country without regions there is no index', async () => {
|
|
const waiting = await start({
|
|
prepare: (f) => {
|
|
f.manifest = NEVER;
|
|
},
|
|
});
|
|
await flush();
|
|
expect(await waiting.engine.loadRegionIndex('FR')).toBeNull();
|
|
vi.resetModules();
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
expect(await ctx.engine.loadRegionIndex('IT')).toBeNull();
|
|
expect(fetched(ctx, 'regions/FR.json')).toBe(0);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-031: an index is fetched once, kept, and two racing reads keep the first', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
const [a, b] = await Promise.all([ctx.engine.loadRegionIndex('FR'), ctx.engine.loadRegionIndex('FR')]);
|
|
expect(a).toBe(b);
|
|
expect(ctx.engine.regionBase.get('FR')).toBe(512);
|
|
expect(await ctx.engine.loadRegionIndex('DE')).not.toBeNull();
|
|
// The race did not hand out FR's ids twice.
|
|
expect(ctx.engine.regionBase.get('DE')).toBe(514);
|
|
const before = fetched(ctx, 'regions/FR.json');
|
|
expect(await ctx.engine.loadRegionIndex('FR')).toBe(a);
|
|
expect(fetched(ctx, 'regions/FR.json')).toBe(before);
|
|
});
|
|
});
|
|
|
|
describe('GlobeEngine picking', () => {
|
|
it('FE-ATLAS2-ENGINE-040: hitAt maps pick ids to countries and regions', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
await ctx.engine.loadRegionIndex('FR');
|
|
const r = ctx.renderer;
|
|
r.pickId = -1;
|
|
expect(ctx.engine.hitAt(10, 10)).toBeNull();
|
|
r.pickId = 2;
|
|
expect(ctx.engine.hitAt(10, 10)).toEqual({ kind: 'country', index: 2, a2: 'DE' });
|
|
r.pickId = 300;
|
|
expect(ctx.engine.hitAt(10, 10)).toBeNull();
|
|
r.pickId = 512;
|
|
expect(ctx.engine.hitAt(10, 10)).toEqual({ kind: 'region', a2: 'FR', code: 'FR-IDF', name: 'Paris Region' });
|
|
r.pickId = 513;
|
|
expect(ctx.engine.hitAt(10, 10)).toEqual({ kind: 'region', a2: 'FR', code: 'FR-ARA', name: 'Auvergne' });
|
|
r.pickId = 600;
|
|
expect(ctx.engine.hitAt(10, 10)).toBeNull();
|
|
expect(last(r.pick.mock.calls)).toEqual([10, 10, false]);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-041: hitAt asks for regions once they are more than half faded in', async () => {
|
|
const ctx = await start({ initial: { zoom: 5.6 } });
|
|
ctx.engine.hitAt(1, 2);
|
|
expect(last(ctx.renderer.pick.mock.calls)).toEqual([1, 2, true]);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-042: idOf gives the status id of a country or a region, -1 when unknown', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
await ctx.engine.loadRegionIndex('FR');
|
|
expect(ctx.engine.idOf({ kind: 'country', index: 4, a2: 'IT' })).toBe(4);
|
|
expect(ctx.engine.idOf({ kind: 'region', a2: 'FR', code: 'FR-ARA', name: null })).toBe(513);
|
|
expect(ctx.engine.idOf({ kind: 'region', a2: 'FR', code: 'FR-XX', name: null })).toBe(-1);
|
|
expect(ctx.engine.idOf({ kind: 'region', a2: 'IT', code: 'IT-1', name: null })).toBe(-1);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-043: focusCountry reads the middle of the view and only re-reads after the camera moved', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
ctx.renderer.pickId = 0;
|
|
expect(ctx.engine.focusCountry()).toBe('FR');
|
|
expect(last(ctx.renderer.pick.mock.calls).slice(0, 2)).toEqual([400, 300]);
|
|
const picks = ctx.renderer.pick.mock.calls.length;
|
|
ctx.renderer.pickId = 2;
|
|
ctx.engine.cam.lon = 40;
|
|
clock += 50;
|
|
expect(ctx.engine.focusCountry()).toBe('FR');
|
|
expect(ctx.renderer.pick.mock.calls.length).toBe(picks);
|
|
clock += 200;
|
|
expect(ctx.engine.focusCountry()).toBe('DE');
|
|
ctx.renderer.pickId = -1;
|
|
clock += 1000;
|
|
// Same camera: the last answer stands, however long ago it was read.
|
|
expect(ctx.engine.focusCountry()).toBe('DE');
|
|
ctx.engine.cam.lon = 50;
|
|
expect(ctx.engine.focusCountry()).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('GlobeEngine layout', () => {
|
|
it('FE-ATLAS2-ENGINE-050: a wide canvas centres a globe 0.31 of the shorter side, backed at the device ratio', async () => {
|
|
vi.stubGlobal('devicePixelRatio', 1.5);
|
|
const ctx = await start();
|
|
expect(ctx.engine.vp).toEqual({ width: 800, height: 600, baseRadius: 186, globeY: 300, flatY: 300 });
|
|
expect(ctx.canvas.width).toBe(1200);
|
|
expect(ctx.canvas.height).toBe(900);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-051: a phone canvas uses a larger globe set lower, and the backing ratio caps at 2', async () => {
|
|
vi.stubGlobal('devicePixelRatio', 3);
|
|
const ctx = await start({ width: 400, height: 800 });
|
|
expect(ctx.engine.vp.baseRadius).toBeCloseTo(168);
|
|
expect(ctx.engine.vp.globeY).toBeCloseTo(432);
|
|
expect(ctx.engine.vp.flatY).toBe(400);
|
|
expect(ctx.canvas.width).toBe(800);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-052: an empty canvas keeps a 1 px backing store', async () => {
|
|
const ctx = await start({ width: 0, height: 0 });
|
|
expect(ctx.canvas.width).toBe(1);
|
|
expect(ctx.canvas.height).toBe(1);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-053: the resize observer re-lays the view out', async () => {
|
|
const ctx = await start();
|
|
ctx.rect.width = 1000;
|
|
ctx.resizeObs.cb();
|
|
expect(ctx.engine.vp.width).toBe(1000);
|
|
expect(ctx.canvas.width).toBe(1000);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-054: a bottom inset lifts the view, a sub-pixel change is ignored', async () => {
|
|
const ctx = await start();
|
|
ctx.engine.setInsetBottom(100);
|
|
expect(ctx.engine.vp.flatY).toBe(250);
|
|
expect(ctx.engine.vp.baseRadius).toBeCloseTo(155);
|
|
const vp = ctx.engine.vp;
|
|
ctx.engine.setInsetBottom(100.5);
|
|
expect(ctx.engine.vp).toBe(vp);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-055: morph, regionAlpha and isMorphing follow the zoom', async () => {
|
|
const ctx = await start({ initial: { zoom: 2 } });
|
|
expect(ctx.engine.isMorphing()).toBe(true);
|
|
frames(1);
|
|
expect(ctx.engine.morph).toBeGreaterThan(0);
|
|
expect(ctx.engine.morph).toBeLessThan(1);
|
|
expect(ctx.engine.regionAlpha).toBe(0);
|
|
ctx.engine.cam.zoom = 5.5;
|
|
expect(ctx.engine.regionAlpha).toBeCloseTo(0.5);
|
|
expect(ctx.engine.isMorphing()).toBe(false);
|
|
ctx.engine.cam.zoom = 1;
|
|
expect(ctx.engine.isMorphing()).toBe(false);
|
|
expect(ctx.engine.zoom).toBe(1);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-056: project places the view centre at the middle of the canvas', async () => {
|
|
const ctx = await start({ initial: { lon: 20, lat: 10, zoom: 1 } });
|
|
frames(1);
|
|
const p = ctx.engine.project(20, 10);
|
|
expect(p.x).toBeCloseTo(400);
|
|
expect(p.y).toBeCloseTo(300);
|
|
expect(p.visible).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-057: a lost WebGL context is reported as an error', async () => {
|
|
const ctx = await start();
|
|
const e = new Event('webglcontextlost', { cancelable: true });
|
|
ctx.canvas.dispatchEvent(e);
|
|
expect(e.defaultPrevented).toBe(true);
|
|
expect(ctx.cb.onError).toHaveBeenCalledWith(expect.objectContaining({ message: 'WebGL context lost' }));
|
|
});
|
|
});
|
|
|
|
describe('GlobeEngine drag and click', () => {
|
|
it('FE-ATLAS2-ENGINE-060: dragging turns the globe by the drag over the radius, and lets it coast', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
frames(1);
|
|
const k = 1 / (186 * RAD);
|
|
pointer(ctx.canvas, 'pointerdown', 1, 400, 300);
|
|
expect(ctx.canvas.setPointerCapture).toHaveBeenCalledWith(1);
|
|
pointer(ctx.canvas, 'pointermove', 1, 410, 300);
|
|
expect(ctx.engine.cam.lon).toBeCloseTo(10 - 10 * k);
|
|
pointer(ctx.canvas, 'pointermove', 1, 420, 300);
|
|
const released = ctx.engine.cam.lon;
|
|
clock += 50;
|
|
pointer(ctx.canvas, 'pointerup', 1, 420, 300);
|
|
expect(ctx.cb.onSelect).not.toHaveBeenCalled();
|
|
frames(5);
|
|
expect(ctx.engine.cam.lon).toBeLessThan(released);
|
|
frames(400);
|
|
const rested = ctx.engine.cam.lon;
|
|
frames(3);
|
|
expect(ctx.engine.cam.lon).toBe(rested);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-061: dragging far down holds the latitude at the globe limit of 80', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
frames(1);
|
|
pointer(ctx.canvas, 'pointerdown', 1, 400, 100);
|
|
pointer(ctx.canvas, 'pointermove', 1, 400, 590);
|
|
expect(ctx.engine.cam.lat).toBe(80);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-062: a short tap selects what is under it', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
ctx.renderer.pickId = 0;
|
|
pointer(ctx.canvas, 'pointerdown', 1, 400, 300);
|
|
clock += 100;
|
|
pointer(ctx.canvas, 'pointerup', 1, 402, 301);
|
|
expect(ctx.cb.onSelect).toHaveBeenCalledWith({ kind: 'country', index: 0, a2: 'FR' }, 402, 301);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-063: a press held past 400 ms or an unknown pointer selects nothing', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
pointer(ctx.canvas, 'pointerup', 7, 400, 300);
|
|
pointer(ctx.canvas, 'pointerdown', 1, 400, 300);
|
|
clock += 500;
|
|
pointer(ctx.canvas, 'pointercancel', 1, 400, 300);
|
|
expect(ctx.cb.onSelect).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-064: hovering reports the hit, throttled to one pick per 45 ms', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
ctx.renderer.pickId = 2;
|
|
pointer(ctx.canvas, 'pointermove', 1, 100, 120);
|
|
expect(ctx.engine.hover).toBe(2);
|
|
expect(ctx.cb.onHover).toHaveBeenLastCalledWith({ kind: 'country', index: 2, a2: 'DE' }, 100, 120);
|
|
pointer(ctx.canvas, 'pointermove', 1, 101, 120);
|
|
expect(ctx.cb.onHover).toHaveBeenCalledTimes(1);
|
|
clock += 50;
|
|
pointer(ctx.canvas, 'pointermove', 1, 102, 120);
|
|
expect(ctx.cb.onHover).toHaveBeenCalledTimes(2);
|
|
ctx.renderer.pickId = -1;
|
|
clock += 50;
|
|
pointer(ctx.canvas, 'pointermove', 1, 300, 120);
|
|
expect(ctx.engine.hover).toBe(-1);
|
|
expect(ctx.cb.onHover).toHaveBeenLastCalledWith(null, 300, 120);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-065: pressing drops the hover, leaving clears it but not while dragging', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
ctx.renderer.pickId = 0;
|
|
pointer(ctx.canvas, 'pointermove', 1, 100, 100);
|
|
expect(ctx.engine.hover).toBe(0);
|
|
pointer(ctx.canvas, 'pointerdown', 1, 100, 100);
|
|
expect(ctx.engine.hover).toBe(-1);
|
|
expect(ctx.cb.onHover).toHaveBeenLastCalledWith(null, 0, 0);
|
|
const calls = ctx.cb.onHover.mock.calls.length;
|
|
ctx.canvas.dispatchEvent(new MouseEvent('pointerleave'));
|
|
expect(ctx.cb.onHover).toHaveBeenCalledTimes(calls);
|
|
clock += 1000;
|
|
pointer(ctx.canvas, 'pointerup', 1, 300, 300);
|
|
ctx.canvas.dispatchEvent(new MouseEvent('pointerleave'));
|
|
expect(ctx.cb.onHover).toHaveBeenCalledTimes(calls + 1);
|
|
expect(ctx.cb.onHover).toHaveBeenLastCalledWith(null, 0, 0);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-066: a double click on open water zooms in by 1.8, one on a country does not', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
ctx.renderer.pickId = 0;
|
|
ctx.canvas.dispatchEvent(new MouseEvent('dblclick', { clientX: 400, clientY: 300 }));
|
|
frames(100);
|
|
expect(ctx.engine.zoom).toBeCloseTo(1, 3);
|
|
ctx.renderer.pickId = -1;
|
|
ctx.canvas.dispatchEvent(new MouseEvent('dblclick', { clientX: 400, clientY: 300 }));
|
|
frames(100);
|
|
expect(ctx.engine.zoom).toBeCloseTo(1.8, 2);
|
|
});
|
|
});
|
|
|
|
describe('GlobeEngine pinch and wheel', () => {
|
|
it('FE-ATLAS2-ENGINE-070: two fingers zoom by the change in their distance, capped, and spring back after', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
frames(1);
|
|
pointer(ctx.canvas, 'pointerdown', 1, 300, 300);
|
|
pointer(ctx.canvas, 'pointerdown', 2, 400, 300);
|
|
pointer(ctx.canvas, 'pointermove', 2, 500, 300);
|
|
expect(ctx.engine.zoom).toBeCloseTo(2);
|
|
pointer(ctx.canvas, 'pointerdown', 3, 10, 10);
|
|
pointer(ctx.canvas, 'pointermove', 3, 20, 20);
|
|
expect(ctx.engine.zoom).toBeCloseTo(2);
|
|
pointer(ctx.canvas, 'pointerup', 3, 20, 20);
|
|
pointer(ctx.canvas, 'pointermove', 2, 5000, 300);
|
|
expect(ctx.engine.zoom).toBeCloseTo(MAX_ZOOM * 1.1);
|
|
frames(1);
|
|
expect(ctx.engine.zoom).toBeCloseTo(MAX_ZOOM * 1.1);
|
|
pointer(ctx.canvas, 'pointerup', 1, 300, 300);
|
|
pointer(ctx.canvas, 'pointerup', 2, 5000, 300);
|
|
expect(ctx.cb.onSelect).not.toHaveBeenCalled();
|
|
frames(200);
|
|
expect(ctx.engine.zoom).toBeCloseTo(MAX_ZOOM, 1);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-071: the wheel zooms smoothly and its default scroll is prevented', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
frames(1);
|
|
const e = wheel(ctx.canvas, -100);
|
|
expect(e.defaultPrevented).toBe(true);
|
|
frames(1);
|
|
expect(ctx.engine.zoom).toBeGreaterThan(1);
|
|
frames(100);
|
|
expect(ctx.engine.zoom).toBeCloseTo(Math.exp(0.16), 2);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-072: zooming out in line mode overshoots below the minimum and springs back', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
wheel(ctx.canvas, 10, 400, 300, 1);
|
|
frames(200);
|
|
expect(ctx.engine.zoom).toBeCloseTo(MIN_ZOOM, 2);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-073: on the flat map the place under the cursor stays under it while zooming', async () => {
|
|
const ctx = await start({ initial: { lon: 0, lat: 0, zoom: 4 } });
|
|
await boot(ctx);
|
|
frames(1);
|
|
const f = ctx.engine.frame;
|
|
const k = 1 / (f.scale * RAD);
|
|
const lon = ctx.engine.cam.lon + ((600 - f.originX) * k) / xSqueeze(f);
|
|
const lat = ctx.engine.cam.lat - (300 - f.originY) * k;
|
|
wheel(ctx.canvas, -100, 600, 300);
|
|
frames(100);
|
|
expect(ctx.engine.zoom).toBeCloseTo(4 * Math.exp(0.16), 2);
|
|
expect(ctx.engine.cam.lon).toBeGreaterThan(1);
|
|
const p = ctx.engine.project(lon, lat);
|
|
expect(Math.abs(p.x - 600)).toBeLessThan(3);
|
|
expect(Math.abs(p.y - 300)).toBeLessThan(3);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-074: at the maximum zoom another wheel step changes nothing', async () => {
|
|
const ctx = await start({ initial: { lon: 0, lat: 0, zoom: MAX_ZOOM } });
|
|
await boot(ctx);
|
|
frames(1);
|
|
const lon = ctx.engine.cam.lon;
|
|
wheel(ctx.canvas, -100, 700, 300);
|
|
frames(20);
|
|
expect(ctx.engine.zoom).toBe(MAX_ZOOM);
|
|
expect(ctx.engine.cam.lon).toBe(lon);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-075: zoomBy clamps the target between the minimum and maximum zoom', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
ctx.engine.zoomBy(1000);
|
|
frames(150);
|
|
expect(ctx.engine.zoom).toBeCloseTo(MAX_ZOOM, 1);
|
|
ctx.engine.zoomBy(0.0001);
|
|
frames(150);
|
|
expect(ctx.engine.zoom).toBeCloseTo(MIN_ZOOM, 2);
|
|
});
|
|
});
|
|
|
|
describe('GlobeEngine flights', () => {
|
|
it('FE-ATLAS2-ENGINE-080: flyTo takes the short way round and holds the target latitude within 70', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
ctx.engine.flyTo(350, 85);
|
|
frames(12, 100);
|
|
expect(ctx.engine.cam.lon).toBeCloseTo(-10);
|
|
expect(ctx.engine.cam.lat).toBeCloseTo(70);
|
|
expect(ctx.engine.zoom).toBeCloseTo(1);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-081: a long hop pulls the zoom back mid-flight and lands on the target zoom', async () => {
|
|
const ctx = await start({ initial: { lon: 0, lat: 0, zoom: 3 } });
|
|
await boot(ctx);
|
|
ctx.engine.flyTo(150, 0, 3, 1000);
|
|
frames(1, 500);
|
|
expect(ctx.engine.zoom).toBeCloseTo(3 - 0.35 * 3, 1);
|
|
frames(1, 600);
|
|
expect(ctx.engine.zoom).toBeCloseTo(3);
|
|
expect(ctx.engine.cam.lon).toBeCloseTo(150);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-082: flyToBox frames a country box, small boxes capped at the max zoom', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
ctx.engine.flyToBox([-5, 42, 8, 51]);
|
|
frames(12, 100);
|
|
expect(ctx.engine.zoom).toBeCloseTo((110 / 14.4) ** 0.85, 3);
|
|
expect(ctx.engine.cam.lon).toBeCloseTo(1.5);
|
|
expect(ctx.engine.cam.lat).toBeCloseTo(46.5);
|
|
ctx.engine.flyToBox([7.4, 43.7, 7.5, 43.8], 1, 7);
|
|
frames(12, 100);
|
|
expect(ctx.engine.zoom).toBeCloseTo(7);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-083: grabbing the globe stops a flight where it is', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
ctx.engine.flyTo(100, 0);
|
|
frames(1, 300);
|
|
pointer(ctx.canvas, 'pointerdown', 1, 400, 300);
|
|
const lon = ctx.engine.cam.lon;
|
|
frames(10, 100);
|
|
expect(ctx.engine.cam.lon).toBeCloseTo(lon);
|
|
expect(lon).toBeLessThan(100);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-084: the frame wraps longitude and clamps latitude to what the flat map allows', async () => {
|
|
const ctx = await start({ initial: { lon: 190, lat: 85, zoom: 6 } });
|
|
frames(1);
|
|
expect(ctx.engine.cam.lon).toBeCloseTo(-170);
|
|
expect(ctx.engine.cam.lat).toBeCloseTo(latLimits(6, ctx.engine.vp)[1]);
|
|
expect(ctx.engine.cam.lat).toBeLessThan(80);
|
|
});
|
|
});
|
|
|
|
describe('GlobeEngine draw state', () => {
|
|
it('FE-ATLAS2-ENGINE-090: ink spreads from its point for 0.75 s and then ends', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
ctx.engine.startInk(5, 2, 46);
|
|
frames(1, 100);
|
|
const ink = lastDraw(ctx.renderer).ink;
|
|
expect(ink).toMatchObject({ id: 5, lon: 2, lat: 46 });
|
|
expect(ink?.radius).toBeGreaterThan(0);
|
|
expect(ink?.radius).toBeLessThan(Math.PI);
|
|
frames(1, 800);
|
|
expect(lastDraw(ctx.renderer).ink?.radius).toBeCloseTo(Math.PI);
|
|
frames(1);
|
|
expect(lastDraw(ctx.renderer).ink).toBeNull();
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-091: selection, arcs and theme reach the renderer', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
const outline = { seg: new Float32Array(4), count: 1 } as LineMesh;
|
|
const arc = { data: new Float32Array(8), count: 1, width: 2 };
|
|
ctx.engine.setSelected(7, outline);
|
|
ctx.engine.setArcs([arc]);
|
|
ctx.engine.setTheme(DARK);
|
|
frames(1, 160);
|
|
const s = lastDraw(ctx.renderer);
|
|
expect(s.selected).toBe(7);
|
|
expect(s.selectedOutline).toBe(outline);
|
|
expect(s.arcs).toEqual([arc]);
|
|
expect(s.arcProgress).toBeCloseTo(0.1);
|
|
expect(s.theme).toBe(DARK);
|
|
expect(s.night).toBe(0.34);
|
|
expect(s.width).toBe(800);
|
|
expect(s.dpr).toBe(1);
|
|
frames(1, 2000);
|
|
expect(lastDraw(ctx.renderer).arcProgress).toBe(1.001);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-092: the sun direction is a unit vector, refreshed every 30 s', async () => {
|
|
const ctx = await start();
|
|
frames(1);
|
|
const sun = lastDraw(ctx.renderer).sun;
|
|
expect(Math.hypot(...sun)).toBeCloseTo(1);
|
|
frames(1);
|
|
expect(lastDraw(ctx.renderer).sun).toBe(sun);
|
|
frames(1, 31000);
|
|
const next = lastDraw(ctx.renderer).sun;
|
|
expect(next).not.toBe(sun);
|
|
expect(Math.hypot(...next)).toBeCloseTo(1);
|
|
expect(lastDraw(ctx.renderer).night).toBe(0.1);
|
|
});
|
|
});
|
|
|
|
describe('GlobeEngine loop', () => {
|
|
it('FE-ATLAS2-ENGINE-100: once everything rests the loop sleeps after three idle frames', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
frames(1, 4000);
|
|
expect(frames(10)).toBe(2);
|
|
expect(rafQueue).toEqual([]);
|
|
const draws = ctx.renderer.draw.mock.calls.length;
|
|
ctx.engine.zoomBy(1.2);
|
|
expect(rafQueue).toHaveLength(1);
|
|
frames(1);
|
|
expect(ctx.renderer.draw.mock.calls.length).toBe(draws + 1);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-101: a dark globe keeps its stars twinkling at a low rate while it rests', async () => {
|
|
const pending = captureTimeouts();
|
|
const ctx = await start({ theme: DARK });
|
|
await boot(ctx);
|
|
frames(1, 4000);
|
|
frames(10);
|
|
expect(rafQueue).toEqual([]);
|
|
const twinkle = pending.find((t) => t.ms === 90);
|
|
if (!twinkle) throw new Error('no twinkle timer');
|
|
twinkle.cb();
|
|
expect(rafQueue).toHaveLength(1);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-102: an ambient globe runs every frame unless reduced motion is asked for', async () => {
|
|
Object.defineProperty(document, 'hidden', { configurable: true, get: () => false });
|
|
try {
|
|
const ctx = await start();
|
|
ctx.engine.ambient = true;
|
|
await boot(ctx);
|
|
frames(1, 4000);
|
|
expect(frames(20)).toBe(20);
|
|
expect(rafQueue).toHaveLength(1);
|
|
|
|
ctx.engine.dispose();
|
|
rafQueue = [];
|
|
vi.mocked(window.matchMedia).mockImplementationOnce(
|
|
(query: string) => ({ matches: true, media: query }) as unknown as MediaQueryList
|
|
);
|
|
vi.resetModules();
|
|
const calm = await start();
|
|
calm.engine.ambient = true;
|
|
frames(1, 4000);
|
|
expect(frames(10)).toBe(2);
|
|
} finally {
|
|
Reflect.deleteProperty(document, 'hidden');
|
|
}
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-103: dispose stops the loop, the observers, the worker and the input', async () => {
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
ctx.engine.dispose();
|
|
expect(cancelAnimationFrame).toHaveBeenCalled();
|
|
expect(ctx.resizeObs.disconnect).toHaveBeenCalled();
|
|
expect(ctx.worker.terminate).toHaveBeenCalled();
|
|
expect(ctx.renderer.dispose).toHaveBeenCalled();
|
|
const draws = ctx.renderer.draw.mock.calls.length;
|
|
frames(1);
|
|
expect(ctx.renderer.draw.mock.calls.length).toBe(draws);
|
|
ctx.engine.kick();
|
|
expect(rafQueue).toEqual([]);
|
|
pointer(ctx.canvas, 'pointerdown', 1, 400, 300);
|
|
expect(ctx.canvas.setPointerCapture).not.toHaveBeenCalled();
|
|
ctx.canvas.dispatchEvent(new Event('webglcontextlost', { cancelable: true }));
|
|
expect(ctx.cb.onError).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('GlobeEngine prefetch', () => {
|
|
it('FE-ATLAS2-ENGINE-110: prefetchRegions loads the coarse regions one country at a time', async () => {
|
|
vi.stubGlobal('requestIdleCallback', undefined);
|
|
const pending = captureTimeouts();
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
ctx.engine.prefetchRegions(['FR', 'DE', 'IT']);
|
|
ctx.engine.prefetchRegions(['DE']);
|
|
expect(pending.map((t) => t.ms)).toEqual([120]);
|
|
const first = pending.shift();
|
|
first?.cb();
|
|
await flush();
|
|
expect(fetched(ctx, 'regions/FR.r0.json')).toBe(1);
|
|
expect(fetched(ctx, 'regions/DE.json')).toBe(0);
|
|
expect(pending).toEqual([]);
|
|
ctx.worker.answer();
|
|
await flush();
|
|
expect(ctx.renderer.hasRegion('FR:r0')).toBe(true);
|
|
const second = pending.shift();
|
|
second?.cb();
|
|
await flush();
|
|
ctx.worker.answer();
|
|
await flush();
|
|
expect(ctx.renderer.hasRegion('DE:r0')).toBe(true);
|
|
expect(fetched(ctx, 'regions/IT.json')).toBe(0);
|
|
expect(pending).toEqual([]);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-111: prefetch waits for an idle moment where the browser offers one', async () => {
|
|
const idle = vi.fn<(cb: () => void, o?: { timeout: number }) => number>(() => 3);
|
|
vi.stubGlobal('requestIdleCallback', idle);
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
ctx.engine.prefetchRegions(['FR']);
|
|
expect(idle).toHaveBeenCalledWith(expect.any(Function), { timeout: 1500 });
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-112: nothing is prefetched after dispose, even from a timer already set', async () => {
|
|
vi.stubGlobal('requestIdleCallback', undefined);
|
|
const pending = captureTimeouts();
|
|
const ctx = await start();
|
|
await boot(ctx);
|
|
ctx.engine.prefetchRegions(['FR']);
|
|
ctx.engine.dispose();
|
|
pending.shift()?.cb();
|
|
await flush();
|
|
ctx.engine.prefetchRegions(['DE']);
|
|
expect(pending).toEqual([]);
|
|
expect(fetched(ctx, 'regions/FR.json')).toBe(0);
|
|
});
|
|
|
|
it('FE-ATLAS2-ENGINE-113: before the manifest arrives there is nothing to prefetch', async () => {
|
|
vi.stubGlobal('requestIdleCallback', undefined);
|
|
const pending = captureTimeouts();
|
|
const ctx = await start({
|
|
prepare: (f) => {
|
|
f.manifest = NEVER;
|
|
},
|
|
});
|
|
await flush();
|
|
ctx.engine.prefetchRegions(['FR']);
|
|
expect(pending).toEqual([]);
|
|
});
|
|
});
|