Spaces:
Running
Running
Download tests/select_test.js from AkshithSurya/spherex-projects: direct link, hf CLI and curl.
- Browser
- Download file 12.6 kB
-
https://huggingface.co/spaces/AkshithSurya/spherex-projects/resolve/main/tests/select_test.js
- Command line
-
hf download hf://spaces/AkshithSurya/spherex-projects/tests/select_test.js
-
curl -L -o select_test.js https://huggingface.co/spaces/AkshithSurya/spherex-projects/resolve/main/tests/select_test.js
12.6 kB
| const ROOT = require('path').join(__dirname, '..') + '/'; | |
| global.window = global; | |
| const C = require(ROOT + 'dome-coords.js'); global.DomeCoords = C; | |
| const S = require(ROOT + 'app-state.js'); | |
| // ---- recording fake THREE | |
| const mkV = (x = 0, y = 0, z = 0) => ({ x, y, z, isV: true }); | |
| function mkObj(name) { const o = { name, children: [], parent: null, visible: true, renderOrder: 0, position: { x: 0, y: 0, z: 0, set(x, y, z) { this.x = x; this.y = y; this.z = z; } }, | |
| quaternion: { setFromUnitVectors(a, b) { this.from = a; this.to = b; } }, | |
| add(c) { c.parent = this; this.children.push(c); }, remove(c) { this.children = this.children.filter(x => x !== c); c.parent = null; }, | |
| traverse(fn) { fn(this); this.children.forEach(c => c.traverse(fn)); } }; return o; } | |
| const disposed = { geo: 0, mat: 0 }; | |
| global.THREE = { Vector3: function (x, y, z) { return mkV(x, y, z); }, Color: function (c) { return { c, convertSRGBToLinear() { this.lin = true; return this; } }; }, | |
| Group: function () { return mkObj('group'); }, LineLoop: function (g, m) { return Object.assign(mkObj('loop'), { geometry: g, material: m }); }, | |
| LineSegments: function (g, m) { return Object.assign(mkObj('segs'), { geometry: g, material: m }); }, | |
| BufferGeometry: function () { return { pts: null, setFromPoints(p) { this.pts = p; return this; }, dispose() { disposed.geo++; } }; }, | |
| LineBasicMaterial: function (o) { return Object.assign({ dispose() { disposed.mat++; } }, o); } }; | |
| require(ROOT + 'dome-select.js'); | |
| const OBS = { lat: -30, lst: 40 }; | |
| const keyHandlers = {}; | |
| function mkView(obs) { return { supported: true, observer: obs === undefined ? OBS : obs, skyGroup: mkObj('sky'), renders: 0, requestRender() { this.renders++; }, | |
| container: { addEventListener: (n, f) => { keyHandlers[n] = f; }, removeEventListener: (n) => { delete keyHandlers[n]; } } }; } | |
| // ---- controllable fetch | |
| let calls = []; | |
| const fetchFn = (url, init) => new Promise((resolve, reject) => { const c = { url, init, resolve, reject }; calls.push(c); | |
| if (init && init.signal) init.signal.addEventListener('abort', () => reject(new Error('aborted'))); }); | |
| const reply = (c, count) => c.resolve({ ok: true, json: async () => ({ count, observations: new Array(count).fill({}) }) }); | |
| const flush = () => new Promise(r => setImmediate(r)); | |
| let bad = 0; const ok = (c, m) => { if (!c) bad++; console.log((c ? 'PASS ' : 'FAIL ') + m); }; | |
| const near = (a, b, t) => Math.abs(a - b) < (t || 1e-9); | |
| const worldOf = (ra, dec, k) => { const w = C.raDecToWorld(ra, dec, OBS); return { x: w.x * k, y: w.y * k, z: w.z * k }; }; | |
| (async () => { | |
| const M42 = [83.8221, -5.3911]; | |
| // pick an observer moment where M42 is above the horizon | |
| OBS.lst = 83.8221 + 20; // hour angle +20 deg: well up in the sky for lat -30 | |
| const infos = []; const state = S.create(); const view = mkView(); | |
| let nextWorld = null; | |
| const sel = new DomeSelect(view, { state, fetchFn, screenToWorld: () => nextWorld, getLayer: () => '2mass-color', onChange: i => infos.push(i), timeoutMs: 60 }); | |
| nextWorld = worldOf(M42[0], M42[1], 3.7); // deliberately not unit length | |
| const r = sel.handleClick({ clientX: 1, clientY: 2 }); | |
| ok(near(r.ra, M42[0], 1e-7) && near(r.dec, M42[1], 1e-7), 'click on M42 -> selection RA/Dec equal M42 (' + r.ra.toFixed(4) + ', ' + r.dec.toFixed(4) + ')'); | |
| const aa = C.altAz(M42[0], M42[1], OBS); | |
| ok(near(r.alt, aa.alt, 1e-7) && near(r.az, aa.az, 1e-7) && r.alt > 0, 'alt/az recorded and above the horizon (alt ' + r.alt.toFixed(1) + ', az ' + r.az.toFixed(0) + ')'); | |
| const g = C.raDecToGalactic(M42[0], M42[1]); ok(near(r.l, g.l, 1e-9) && near(r.b, g.b, 1e-9) && r.layer === '2mass-color' && r.lookup === 'pending' && r.count === null, 'galactic l/b, layer and pending lookup recorded'); | |
| ok(state.get().selection.ra === r.ra && near(state.get().ra, r.ra), 'state store updated (selection and target)'); | |
| // marker consistency: local position, rotated by the observer matrix, must equal the clicked world direction | |
| const m = view.skyGroup.children[0], len = Math.hypot(m.position.x, m.position.y, m.position.z); | |
| const wm = C.worldToRaDec; const M = C.observerMatrix(OBS), p = m.position; | |
| const wx = M[0][0] * p.x + M[0][1] * p.y + M[0][2] * p.z, wy = M[1][0] * p.x + M[1][1] * p.y + M[1][2] * p.z, wz = M[2][0] * p.x + M[2][1] * p.y + M[2][2] * p.z; | |
| const cw = nextWorld, cl = Math.hypot(cw.x, cw.y, cw.z); | |
| ok(near(len, 492.5, 1e-6) && near(wx / len, cw.x / cl) && near(wy / len, cw.y / cl) && near(wz / len, cw.z / cl), 'marker sits exactly on the clicked direction once the sky rotation is applied'); | |
| ok(m.name === 'selection.marker' && m.quaternion.to && near(m.quaternion.to.y, C.raDecToDir(M42[0], M42[1]).y), 'marker ring is oriented along the sky direction (faces the viewer)'); | |
| ok(m.children[0].geometry.pts.length === 48 && m.children[1].geometry.pts.length === 8, 'marker = 48-point ring + 4 ticks'); | |
| // lookup request | |
| ok(calls.length === 1 && calls[0].url === '/api/observations/search' && calls[0].init.method === 'POST', 'one POST to /api/observations/search'); | |
| const body = JSON.parse(calls[0].init.body); ok(near(body.ra, r.ra) && near(body.dec, r.dec) && body.radius === 0.25 && body.include_remote === false, 'request body: ra, dec, radius 0.25, no remote'); | |
| ok(infos[infos.length - 1].text === 'Checking the local SPHEREx archive\u2026', 'shows "checking" while waiting'); | |
| reply(calls[0], 3); await flush(); | |
| ok(infos[infos.length - 1].text === '3 local SPHEREx frames nearby' && sel.selection.count === 3 && state.get().selection.count === 3, 'count 3 -> "3 local SPHEREx frames nearby"; store updated'); | |
| nextWorld = worldOf(100, -30, 1); sel.handleClick({ clientX: 0, clientY: 0 }); reply(calls[1], 1); await flush(); | |
| ok(infos[infos.length - 1].text === '1 local SPHEREx frame nearby', 'count 1 is singular'); | |
| ok(view.skyGroup.children.filter(c => c.name === 'selection.marker').length === 1, 'a new click replaces the marker (still exactly one)'); | |
| nextWorld = worldOf(100, -30, 1); sel.handleClick({ clientX: 0, clientY: 0 }); reply(calls[2], 0); await flush(); | |
| ok(infos[infos.length - 1].text === 'No local SPHEREx frames near this point' && sel.selection.lookup === 'ok', 'count 0 -> "No local SPHEREx frames near this point"'); | |
| // failures keep the marker and coordinates (contract section 9) | |
| nextWorld = worldOf(150, -20, 1); sel.handleClick({ clientX: 0, clientY: 0 }); calls[3].resolve({ ok: false, status: 500, json: async () => ({}) }); await flush(); | |
| ok(infos[infos.length - 1].text === 'Lookup unavailable' && sel.selection.lookup === 'error' && !!sel.marker && near(sel.selection.ra, C.worldToRaDec(nextWorld, OBS).ra), 'HTTP 500 -> "Lookup unavailable", marker and coordinates kept'); | |
| nextWorld = worldOf(150, -20, 1); sel.handleClick({ clientX: 0, clientY: 0 }); calls[4].reject(new Error('network')); await flush(); | |
| ok(sel.selection.lookup === 'error', 'network failure -> "Lookup unavailable"'); | |
| nextWorld = worldOf(150, -20, 1); sel.handleClick({ clientX: 0, clientY: 0 }); calls[5].resolve({ ok: true, json: async () => ({ weird: true }) }); await flush(); | |
| ok(sel.selection.lookup === 'error', 'malformed response -> "Lookup unavailable"'); | |
| nextWorld = worldOf(150, -20, 1); sel.handleClick({ clientX: 0, clientY: 0 }); await new Promise(r => setTimeout(r, 120)); await flush(); | |
| ok(sel.selection.lookup === 'error', 'a lookup that never answers times out -> "Lookup unavailable"'); | |
| // stale answers | |
| calls = []; nextWorld = worldOf(60, -40, 1); sel.handleClick({ clientX: 0, clientY: 0 }); nextWorld = worldOf(70, -42, 1); const second = sel.handleClick({ clientX: 0, clientY: 0 }); | |
| reply(calls[1], 5); await flush(); reply(calls[0], 9); await flush(); | |
| ok(sel.selection.count === 5 && near(sel.selection.ra, second.ra), 'an older answer arriving late is ignored (kept the newest: 5)'); | |
| // ground | |
| let ground = 0; sel.o.onGround = () => ground++; const keep = JSON.stringify(sel.selection), nCalls = calls.length; | |
| nextWorld = { x: 3, y: -1, z: -5 }; const gr = sel.handleClick({ clientX: 0, clientY: 0 }); | |
| ok(gr.ground === true && ground === 1 && JSON.stringify(sel.selection) === keep && calls.length === nCalls && infos[infos.length - 1].ground === true, 'click on the ground: no selection change, no lookup, onGround fired'); | |
| nextWorld = { x: 3, y: 0, z: -5 }; ok(sel.handleClick({ clientX: 0, clientY: 0 }).ground === true, 'exactly on the horizon counts as ground'); | |
| // clear + Escape + stale-after-clear | |
| calls = []; nextWorld = worldOf(60, -40, 1); sel.handleClick({ clientX: 0, clientY: 0 }); const c0 = calls[0]; | |
| keyHandlers.keydown({ key: 'x', preventDefault() {} }); ok(!!sel.selection, 'other keys do not clear'); | |
| let prevented = false; keyHandlers.keydown({ key: 'Escape', preventDefault() { prevented = true; } }); | |
| ok(sel.selection === null && !sel.marker && state.get().selection === null && prevented, 'Escape clears selection, marker and stored selection'); | |
| reply(c0, 7); await flush(); ok(sel.selection === null && infos[infos.length - 1].selection === null, 'an answer arriving after clear is ignored'); | |
| // restore (coming back from Professional Mode) | |
| calls = []; const saved = { ra: 83.8221, dec: -5.3911, l: 209, b: -19, count: 4, lookup: 'ok', radius: 0.25 }; | |
| sel.restore(saved); ok(!!sel.marker && sel.selection.count === 4 && calls.length === 0 && state.get().selection.count === 4, 'restore of a finished lookup: marker back, count kept, no new request'); | |
| sel.restore({ ra: 83.8221, dec: -5.3911, count: null, lookup: 'pending', radius: 0.25 }); ok(calls.length === 1, 'restore of an unfinished lookup re-asks the server'); | |
| // field outline | |
| const data = require(ROOT + 'sky_layers/field_polys.json'); | |
| const loadP = sel.loadOutline('sky_layers/field_polys.json'); calls[calls.length - 1].resolve({ ok: true, json: async () => data }); | |
| ok(await loadP === true && !!sel.outline, 'outline loads'); | |
| const [fl, hl] = sel.outline.children; | |
| ok(fl.geometry.pts.length === 434 * 4 * 2 && hl.geometry.pts.length === data.hull.length * 2, 'draws all 434 frames (4 edges each) and the outline (' + data.hull.length + ' edges)'); | |
| const v0 = fl.geometry.pts[0], d0 = C.raDecToDir(data.frames[0].p[0][0], data.frames[0].p[0][1]); | |
| ok(near(v0.x, d0.x * 495, 1e-6) && near(v0.y, d0.y * 495, 1e-6) && near(v0.z, d0.z * 495, 1e-6), 'first frame vertex is at the right sky position, radius 495'); | |
| ok(fl.material.opacity < hl.material.opacity && fl.material.color.lin === true, 'frames dimmer than the outline; colour converted for sRGB output'); | |
| sel.setOutlineVisible(false); ok(sel.outline.visible === false, 'outline can be hidden'); | |
| const fail = sel.loadOutline('x'); calls[calls.length - 1].resolve({ ok: false, status: 404 }); ok(await fail === false, 'a missing outline file is not fatal (returns false)'); | |
| // selectAt (named objects) | |
| calls = []; const st2 = S.create(); const v3 = mkView(); const sel4 = new DomeSelect(v3, { state: st2, fetchFn, screenToWorld: () => nextWorld }); | |
| OBS.lst = 83.8221 + 20; | |
| const named = sel4.selectAt(83.8221, -5.3911, { name: 'M42' }); | |
| ok(named && named.name === 'M42' && near(named.ra, 83.8221) && named.alt > 0 && st2.get().targetName === 'M42' && st2.get().selection.name === 'M42', 'selectAt: named object selected, name kept, store targetName set'); | |
| ok(calls.length === 1 && !!sel4.marker, 'selectAt: marker placed and lookup started'); | |
| OBS.lst = 83.8221 + 20 + 180; // now below the horizon | |
| const hidden = sel4.selectAt(83.8221, -5.3911, { name: 'M42' }); | |
| ok(hidden === null && sel4.selection.name === 'M42', 'selectAt below the horizon returns null and does not change the selection'); | |
| OBS.lst = 83.8221 + 20; nextWorld = worldOf(100, -30, 1); sel4.handleClick({ clientX: 0, clientY: 0 }); | |
| ok(st2.get().targetName === null, 'a plain click clears the target name'); | |
| // no observer = celestial-fixed mode: no ground rule | |
| const view2 = mkView(null); const sel2 = new DomeSelect(view2, { fetchFn, screenToWorld: () => ({ x: 0, y: -1, z: 0.0001 }) }); | |
| const r2 = sel2.handleClick({ clientX: 0, clientY: 0 }); ok(r2.dec < -89 && !r2.ground && r2.alt === undefined, 'without an observer there is no ground and no alt/az'); | |
| const sel3 = new DomeSelect({ supported: false }, {}); ok(sel3.handleClick({}) === null, 'unsupported view: click does nothing'); | |
| disposed.geo = disposed.mat = 0; sel.dispose(); ok(!view.skyGroup.children.length && !keyHandlers.keydown && disposed.geo > 0, 'dispose removes marker, outline and listeners'); | |
| console.log(bad ? bad + ' FAILED' : 'ALL PASS'); process.exit(bad ? 1 : 0); | |
| })(); | |