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/search.test.mjs | |
| download | worldmap-viewer-3443c1c65e9f1753e1e656b35d08416c1fa298f2.tar.gz worldmap-viewer-3443c1c65e9f1753e1e656b35d08416c1fa298f2.zip | |
worldmap-viewer: initial public history
Diffstat (limited to 'viewer/tests/search.test.mjs')
| -rw-r--r-- | viewer/tests/search.test.mjs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/viewer/tests/search.test.mjs b/viewer/tests/search.test.mjs new file mode 100644 index 0000000..12b5e83 --- /dev/null +++ b/viewer/tests/search.test.mjs @@ -0,0 +1,55 @@ +import './_planet.mjs'; +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { parseCoords, isCellId, suggest } from '../js/search.js'; + +const near = (a, b, eps) => assert.ok(Math.abs(a - b) <= eps, `${a} vs ${b}`); + +test('coordinate formats', () => { + const cases = [['40N 10W', 40, -10], ['40.5, -10.25', 40.5, -10.25], ['-12 170', -12, 170], ["40°30'N 10°15'W", 40.5, -10.25], + ['10W 40N', 40, -10], ['N40 E10', 40, 10], ['40.5 N, 10.25 E', 40.5, 10.25], ['12°30\'15"S 45°E', -(12 + 30 / 60 + 15 / 3600), 45]]; + for (const [q, lat, lon] of cases) { + const c = parseCoords(q); + assert.ok(c, q); + near(c.lat, lat, 1e-9); + near(c.lon, lon, 1e-9); + } +}); + +test('junk is rejected', () => { + for (const q of ['', 'hello', '40', '95 10', '10 200', '40N 10N', "40°75'N 10W", '40N 10W 5', 'Cell Am Zee', 'NEWS', '4O 10']) { + assert.equal(parseCoords(q), null, q); + } +}); + +test('H3 cell ids', () => { + assert.equal(isCellId('8528308ffffffff'), true); + assert.equal(isCellId(' 8528308FFFFFFFF '), true); + assert.equal(isCellId('0528308ffffffff'), false); + assert.equal(isCellId('8528308fffffff'), false); +}); + +test('suggestions: coordinates, cells, then pins', () => { + const pins = [{ id: 'pin-1', name: 'Zeta ford', lat: 1, lon: 2, lore: null, note: '' }, + { id: 'pin-2', name: 'Alpha peak', lat: 3, lon: 4, lore: 'physical-world', note: '' }]; + assert.deepEqual(suggest('40N 10W', pins).map(s => [s.kind, s.lat, s.lon]), [['coords', 40, -10]]); + assert.deepEqual(suggest('8528308ffffffff', pins).map(s => [s.kind, s.cell]), [['cell', '8528308ffffffff']]); + assert.deepEqual(suggest('a', pins).map(s => s.label), ['Alpha peak', 'Zeta ford']); + assert.deepEqual(suggest(' ', pins), []); +}); + +test('decimal commas in a two-number entry', () => { + const a = parseCoords('40,5 10,25'), b = parseCoords('40,5N 10,25W'), c = parseCoords('40,5'); + assert.deepEqual([a.lat, a.lon], [40.5, 10.25]); + assert.deepEqual([b.lat, b.lon], [40.5, -10.25]); + assert.deepEqual([c.lat, c.lon], [40, 5]); // no space: still "lat,lon" +}); + +test('decimal commas anywhere, and "lat,lon" still wins when it parses', () => { + for (const [q, lat, lon] of [['40 10,5', 40, 10.5], ['40.5 10,25', 40.5, 10.25], ['40N 10,5E', 40, 10.5], + ['40,5 10', 40.5, 10], ['40,5 E', 40, 5]]) { + const c = parseCoords(q); + assert.ok(c, q); + assert.deepEqual([c.lat, c.lon], [lat, lon], q); + } +}); |
