diff options
| author | godosa <godosa@godosa.eu> | 2026-10-07 00:14:38 +0200 |
|---|---|---|
| committer | godosa <godosa@godosa.eu> | 2026-10-07 00:14:38 +0200 |
| commit | 3443c1c65e9f1753e1e656b35d08416c1fa298f2 (patch) | |
| tree | 4e43236f460145a4d75d1b4616dcb7aa6ef08f51 /viewer/tests/exports.test.mjs | |
| download | worldmap-viewer-3443c1c65e9f1753e1e656b35d08416c1fa298f2.tar.gz worldmap-viewer-3443c1c65e9f1753e1e656b35d08416c1fa298f2.zip | |
worldmap-viewer: initial public history
Diffstat (limited to 'viewer/tests/exports.test.mjs')
| -rw-r--r-- | viewer/tests/exports.test.mjs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/viewer/tests/exports.test.mjs b/viewer/tests/exports.test.mjs new file mode 100644 index 0000000..c3735fb --- /dev/null +++ b/viewer/tests/exports.test.mjs @@ -0,0 +1,46 @@ +import './_planet.mjs'; +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { samplesFor, squareOutline, exportSizeMB, exportStatusText } from '../js/exports.js'; + +test('samples: the smallest 2^n + 1 covering the size (as the server)', () => { + assert.equal(samplesFor(40, 10), 4097); + assert.equal(samplesFor(100, 2000), 65); + assert.equal(samplesFor(5, 20), 257); +}); + +test('the square outline: corners size/√2 from the centre, closed', () => { + const R = 12742, pts = squareOutline(0, 0, 100); + assert.deepEqual(pts[0], pts[pts.length - 1]); + const d = p => R * Math.acos(Math.cos(p.lat * Math.PI / 180) * Math.cos(p.lon * Math.PI / 180)); + assert.ok(Math.abs(d(pts[0]) - 50 * Math.SQRT2) < 0.05); + const north = pts.reduce((a, b) => (b.lat > a.lat ? b : a)); + assert.ok(Math.abs(north.lat * Math.PI / 180 * R - 50) < 0.1, 'the north edge 50 km up'); +}); + +test('size estimate and status', () => { + assert.ok(exportSizeMB(4097) > 100 && exportSizeMB(4097) < 200, "the island export was 128 MB"); + assert.equal(exportStatusText({ state: 'running', name: 'x', fraction: 0.5 }), 'Exporting x… 50%'); + assert.equal(exportStatusText({ state: 'failed', name: 'x', error: 'boom' }), 'Export x failed: boom'); + assert.equal(exportStatusText({ state: 'idle' }), ''); +}); + +test('polling while an export runs keeps what is being typed', async () => { + const { installFakeDom, FakeEl } = await import('./fakedom.mjs'); + installFakeDom(); + let calls = 0; + globalThis.fetch = async () => ({ ok: true, status: 200, headers: { get: () => 'application/json' }, + json: async () => ({ exports: [], status: { state: 'running', name: 'x', fraction: 0.1 * ++calls } }) }); + const { ExportPanel } = await import('../js/exportpanel.js'); + const box = new FakeEl('section'); + const p = new ExportPanel(box, { getCenter: () => ({ lat: 1, lon: 2 }), onSquare: () => {} }); + p.visible = true; + await p.load(); + const input = box.find(e => e.tag === 'input'); + input.value = 'my-area'; + await p.load(); // a poll + clearTimeout(p.timer); + assert.equal(box.find(e => e.tag === 'input'), input, 'the same input, not rebuilt'); + assert.equal(input.value, 'my-area'); + assert.match(box.textContent, /Exporting x… 20%/); +}); |
