// 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(); return { ...actual, buildLayer: (...args: Parameters) => { 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); } 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]]); }); });