mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +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.
120 lines
3.6 KiB
TypeScript
120 lines
3.6 KiB
TypeScript
// FE-ATLAS2-RENDER-500 to FE-ATLAS2-RENDER-503: the mesh worker, building a layer off the main thread and
|
|
// answering each job with the layer (buffers transferred) or the error.
|
|
import type { Topology } from 'topojson-specification';
|
|
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import type { MeshRequest, MeshResponse } from './globeMesh.worker';
|
|
|
|
const h = vi.hoisted(() => ({ fail: null as unknown }));
|
|
|
|
vi.mock('./globeMesh', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('./globeMesh')>();
|
|
return {
|
|
...actual,
|
|
buildLayer: (...args: Parameters<typeof actual.buildLayer>) => {
|
|
if (h.fail !== null) throw h.fail;
|
|
return actual.buildLayer(...args);
|
|
},
|
|
};
|
|
});
|
|
|
|
/** Two squares sharing one edge. */
|
|
const TWO_SQUARES: Topology = {
|
|
type: 'Topology',
|
|
arcs: [
|
|
[
|
|
[10, 0],
|
|
[10, 10],
|
|
],
|
|
[
|
|
[10, 10],
|
|
[0, 10],
|
|
[0, 0],
|
|
[10, 0],
|
|
],
|
|
[
|
|
[10, 0],
|
|
[20, 0],
|
|
[20, 10],
|
|
[10, 10],
|
|
],
|
|
],
|
|
objects: {
|
|
countries: {
|
|
type: 'GeometryCollection',
|
|
geometries: [
|
|
{ type: 'Polygon', id: 0, arcs: [[0, 1]] },
|
|
{ type: 'Polygon', id: 1, arcs: [[~0, 2]] },
|
|
],
|
|
},
|
|
},
|
|
};
|
|
|
|
type Posted = [MeshResponse, Transferable[] | undefined];
|
|
let posted: Posted[];
|
|
|
|
function send(req: MeshRequest): void {
|
|
const handler = self.onmessage;
|
|
if (!handler) throw new Error('worker installed no message handler');
|
|
handler.call(self, { data: req } as MessageEvent<MeshRequest>);
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
await import('./globeMesh.worker');
|
|
});
|
|
|
|
beforeEach(() => {
|
|
h.fail = null;
|
|
posted = [];
|
|
vi.spyOn(self, 'postMessage').mockImplementation(((msg: MeshResponse, transfer?: Transferable[]) => {
|
|
posted.push([msg, transfer]);
|
|
}) as typeof self.postMessage);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe('globeMesh.worker', () => {
|
|
it('FE-ATLAS2-RENDER-500: answers a job with the built layer, ids offset by idBase', () => {
|
|
send({ job: 7, topo: TWO_SQUARES, object: 'countries', maxEdge: 4, idBase: 100 });
|
|
expect(posted).toHaveLength(1);
|
|
const [msg] = posted[0];
|
|
expect(msg.job).toBe(7);
|
|
expect(msg.error).toBeUndefined();
|
|
const fid = Array.from(msg.layer?.fill.fid ?? []);
|
|
expect(new Set(fid)).toEqual(new Set([100, 101]));
|
|
expect(msg.layer?.fill.index.length).toBeGreaterThan(0);
|
|
// The shared edge is a border, the outer ring the coast.
|
|
expect(msg.layer?.borders.count).toBeGreaterThan(0);
|
|
expect(msg.layer?.coast.count).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('FE-ATLAS2-RENDER-501: transfers the six array buffers instead of copying them', () => {
|
|
send({ job: 1, topo: TWO_SQUARES, object: 'countries', maxEdge: 8, idBase: 0 });
|
|
const [msg, transfer] = posted[0];
|
|
const layer = msg.layer;
|
|
if (!layer) throw new Error('no layer');
|
|
expect(transfer).toEqual([
|
|
layer.fill.ll.buffer,
|
|
layer.fill.ref.buffer,
|
|
layer.fill.fid.buffer,
|
|
layer.fill.index.buffer,
|
|
layer.coast.seg.buffer,
|
|
layer.borders.seg.buffer,
|
|
]);
|
|
});
|
|
|
|
it('FE-ATLAS2-RENDER-502: answers a failing job with its error message', () => {
|
|
h.fail = new Error('bad topology');
|
|
send({ job: 3, topo: TWO_SQUARES, object: 'countries', maxEdge: 8, idBase: 0 });
|
|
expect(posted).toEqual([[{ job: 3, error: 'bad topology' }, undefined]]);
|
|
});
|
|
|
|
it('FE-ATLAS2-RENDER-503: turns a thrown non-error into text', () => {
|
|
h.fail = 'out of memory';
|
|
send({ job: 4, topo: TWO_SQUARES, object: 'countries', maxEdge: 8, idBase: 0 });
|
|
expect(posted).toEqual([[{ job: 4, error: 'out of memory' }, undefined]]);
|
|
});
|
|
});
|