Spaces:
Running
Running
File size: 5,868 Bytes
057a205 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | const ROOT = require('path').join(__dirname, '..') + '/';
global.window = global;
// --- fake clock + rAF
let now = 0, queue = [], nextId = 1;
global.requestAnimationFrame = (cb) => { const id = nextId++; queue.push({ id, cb }); return id; };
global.cancelAnimationFrame = (id) => { queue = queue.filter(q => q.id !== id); };
function tick(ms) { now += ms; const q = queue; queue = []; q.forEach(e => e.cb(now)); }
let reduced = false; global.matchMedia = () => ({ matches: reduced });
// --- fake manifest + textures
const manifest = { layers: [
{ id: 'dss2-color', file: 'dss2-color.jpg', sha256: 'aaaaaaaaaa', hips_record: { obs_copyright: 'STScI' } },
{ id: '2mass-color', file: '2mass-color.jpg', sha256: 'bbbbbbbbbb', hips_record: { obs_copyright: 'UMass & IPAC' } },
{ id: 'allwise-color', file: 'allwise-color.jpg', credit_note: 'look up credit' },
{ id: 'bad-layer', file: 'bad.jpg' } ] };
global.fetch = async (u) => ({ ok: true, json: async () => manifest });
const pending = {}; // url -> resolver, lets tests control load order
global.THREE = { TextureLoader: function () { return { load(url, ok, _p, err) {
const tex = { url, disposed: false, dispose() { this.disposed = true; } };
if (url.includes('bad.jpg')) { Promise.resolve().then(() => err()); return; }
if (global.manualLoad) { pending[url.split('?')[0]] = () => ok(tex); } else Promise.resolve().then(() => ok(tex));
} }; } };
// --- fake view that records every call
const log = [];
const short = (u) => u.split('/').pop().split('?')[0].replace('.jpg', '');
const view = { base: null, overlay: null, opacity: 0, lookCalls: 0,
setLook() { this.lookCalls++; }, setFov() { this.lookCalls++; },
setSkyTexture(t) { this.base = t; log.push('base:' + short(t.url)); },
setOverlayTexture(t) { this.overlay = t; this.opacity = 0; log.push('overlay:' + short(t.url)); },
setOverlayOpacity(a) { this.opacity = a; },
commitOverlay() { if (this.base) this.base.dispose(); this.base = this.overlay; this.overlay = null; log.push('commit'); },
cancelOverlay() { this.overlay = null; log.push('cancel'); } };
require(ROOT + 'dome-layers.js');
let bad = 0; const ok = (c, m) => { if (!c) bad++; console.log((c ? 'PASS ' : 'FAIL ') + m); };
const flush = () => new Promise(r => setImmediate(r));
let finished = false;
process.on('exit', () => { if (!finished) { console.log('FAIL test did not complete (deadlock)'); process.exitCode = 1; } });
(async () => {
const statuses = [], changes = [];
const L = new DomeLayers(view, { fadeMs: 700, onStatus: s => statuses.push(s.state + ':' + s.id), onChange: l => changes.push(l.id) });
const layers = await L.load();
ok(layers.length === 4 && layers[1].url === 'sky_layers/2mass-color.jpg?v=bbbbbbbb', 'manifest parsed, cache-busting ?v=hash added');
ok(L.credit(L.byId['dss2-color']) === 'STScI' && L.credit(L.byId['allwise-color']) === 'look up credit', 'credit from HiPS record, else build note');
ok((await L.show('nope')).ok === false, 'unknown layer id is rejected, not thrown');
let r = await L.show('2mass-color'); await flush();
ok(r.ok && L.current.id === '2mass-color' && log.join() === 'base:2mass-color', 'first layer shown directly with no fade');
const p = L.show('dss2-color'); await flush();
ok(view.overlay && view.overlay.url.startsWith('sky_layers/dss2-color') && view.opacity === 0, 'second layer starts on the overlay at opacity 0');
tick(16); tick(200); const mid = view.opacity; tick(200); const mid2 = view.opacity;
ok(mid > 0 && mid2 > mid && mid2 < 1, 'opacity rises smoothly (' + mid.toFixed(2) + ' -> ' + mid2.toFixed(2) + ')');
ok(L.current.id === '2mass-color', 'old layer stays "current" until the fade completes');
tick(500); r = await p;
ok(r.ok && L.current.id === 'dss2-color' && log[log.length - 1] === 'commit' && view.overlay === null, 'fade completes, overlay committed to base');
ok(changes.join() === '2mass-color,dss2-color', 'onChange fired once per layer');
const before = L.current.id, baseTex = view.base;
r = await L.show('bad-layer'); await flush();
ok(r.ok === false && L.current.id === before && view.base === baseTex && statuses.includes('error:bad-layer'), 'failed load: previous layer stays, status = error');
ok((await L.show('dss2-color')).unchanged === true, 'showing the current layer again is a no-op');
// rapid switching with out-of-order loads
global.manualLoad = true; log.length = 0;
const pa = L.show('2mass-color'), pb = L.show('allwise-color');
await flush(); pending['sky_layers/allwise-color.jpg'](); await flush(); // newest arrives first
tick(16); tick(800); await flush();
pending['sky_layers/2mass-color.jpg'](); await flush(); tick(16); tick(900); await flush(); // stale one arrives late
const ra = await pa, rb = await pb;
ok(rb.ok && ra.stale === true && L.current.id === 'allwise-color', 'rapid switch: stale (older) load is ignored, newest wins');
ok(!log.includes('overlay:2mass-color'), 'stale texture never reached the screen');
global.manualLoad = false;
// switching mid-fade settles the first fade, then fades onward
log.length = 0;
const p1 = L.show('2mass-color'); await flush(); tick(16); tick(100);
const p2 = L.show('dss2-color'); await flush(); tick(16); tick(900);
await p1; r = await p2;
ok(L.current.id === 'dss2-color' && log.filter(x => x === 'commit').length === 2, 'switching mid-fade commits the first, then fades to the second');
reduced = true; log.length = 0;
r = await L.show('2mass-color'); await flush();
ok(r.ok && L.current.id === '2mass-color' && !log.some(x => x.startsWith('overlay')), 'prefers-reduced-motion: instant swap, no fade');
reduced = false;
ok(view.lookCalls === 0, 'camera never touched by any layer change');
finished = true; L.dispose(); console.log(bad ? bad + ' FAILED' : 'ALL PASS'); process.exit(bad ? 1 : 0);
})();
|