Spaces:
Running
Running
Download tests/scene_test.js from AkshithSurya/spherex-projects: direct link, hf CLI and curl.
- Browser
- Download file 4.29 kB
-
https://huggingface.co/spaces/AkshithSurya/spherex-projects/resolve/main/tests/scene_test.js
- Command line
-
hf download hf://spaces/AkshithSurya/spherex-projects/tests/scene_test.js
-
curl -L -o scene_test.js https://huggingface.co/spaces/AkshithSurya/spherex-projects/resolve/main/tests/scene_test.js
4.29 kB
| const ROOT = require('path').join(__dirname, '..') + '/'; | |
| // Stub THREE/DOM: any property is a constructor/function returning another stub. Catches typos & flow bugs in OUR code. | |
| function stub(name) { | |
| const f = function () { return stub(name + '()'); }; | |
| return new Proxy(f, { | |
| get: (t, p) => p === 'then' ? undefined : (p === Symbol.toPrimitive ? () => 1 : (p in t ? t[p] : stub(name + '.' + String(p)))), | |
| set: () => true, construct: () => stub('new ' + name), apply: () => stub(name + '()') | |
| }); | |
| } | |
| const calls = { render: 0 }; | |
| global.window = global; | |
| global.addEventListener = () => {}; global.removeEventListener = () => {}; | |
| let roUsed = 0; | |
| if (process.env.WITH_RO) global.ResizeObserver = function () { roUsed++; return { observe() {}, disconnect() {} }; }; | |
| global.WebGLRenderingContext = function () {}; | |
| global.devicePixelRatio = 2; | |
| global.requestAnimationFrame = (cb) => { setImmediate(cb); return 1; }; | |
| global.cancelAnimationFrame = () => {}; | |
| const listeners = {}; | |
| global.document = { | |
| hidden: false, | |
| addEventListener: (n, f) => { listeners[n] = f; }, removeEventListener: () => {}, | |
| createElement: () => ({ getContext: () => stub('ctx'), style: {}, setAttribute() {}, addEventListener() {}, removeEventListener() {}, parentNode: null }) | |
| }; | |
| const T = new Proxy({}, { get: (t, p) => (p in t ? t[p] : stub('THREE.' + String(p))), set: (t, p, v) => { t[p] = v; return true; } }); | |
| T.WebGLRenderer = function () { | |
| return { domElement: { style: {}, setAttribute() {}, addEventListener() {}, removeEventListener() {}, parentNode: { removeChild() {} } }, | |
| setPixelRatio() {}, setClearColor() {}, setSize() {}, render() { calls.render++; }, dispose() {}, forceContextLoss() {}, | |
| capabilities: { getMaxAnisotropy: () => 16 } }; | |
| }; | |
| T.Scene = function () { return { add() {}, traverse(fn) { fn({ geometry: { dispose() {} }, material: { dispose() {} } }); } }; }; | |
| T.PerspectiveCamera = function () { return { rotation: { order: '' }, aspect: 1, fov: 70, updateProjectionMatrix() {} }; }; | |
| T.CanvasTexture = function () { return { dispose() {} }; }; | |
| global.THREE = T; | |
| require(ROOT + 'dome.js'); | |
| const container = { clientWidth: 800, clientHeight: 600, appendChild() {} }; | |
| let frames = 0; | |
| const v = new DomeView(container, { onFrame: () => frames++ }); | |
| const assert = (c, m) => { if (!c) { console.log('FAIL', m); process.exitCode = 1; } else console.log('PASS', m); }; | |
| assert(v.supported === true, 'constructor builds scene when THREE + WebGL present'); | |
| v.setLook(30, 95); assert(v.pitch === 89, 'pitch clamped to 89'); | |
| v.setLook(30, -120); assert(v.pitch === -89, 'pitch clamped to -89'); | |
| v.setFov(500); assert(v.fov === 120, 'fov clamped to 120'); | |
| v.setFov(1); assert(v.fov === 5, 'fov clamped to 5'); | |
| v.setSkyTexture(DomeView.makeGraticuleTexture(512)); assert(!!v._skyTexture, 'texture accepted, graticule generated'); | |
| v.requestRender(); v.requestRender(); v.requestRender(); | |
| setImmediate(() => setImmediate(() => { | |
| assert(frames >= 1 && calls.render === frames, 'render-on-demand rendered (' + frames + ' frame for many requests)'); | |
| if (process.env.WITH_RO) assert(roUsed === 1, 'ResizeObserver path used'); | |
| v.setExposure(10); assert(v.exposure === 4, 'exposure clamps at 4'); | |
| v.setExposure(0.1); assert(v.exposure === 0.4, 'exposure clamps at 0.4'); | |
| v.setExposure(1.7); assert(v.exposure === 1.7, 'exposure accepts a normal value'); | |
| v.setActive(false); const f0 = frames; v.requestRender(); await_next(() => { | |
| assert(frames === f0, 'setActive(false): no drawing while hidden'); | |
| v.setActive(true); await_next(() => { assert(frames > f0, 'setActive(true): drawing resumes'); finish(); }); | |
| }); | |
| function await_next(fn) { setImmediate(() => setImmediate(fn)); } | |
| function finish() { | |
| v.dispose(); assert(v._disposed, 'dispose completes without error'); | |
| v.setLook(1, 1); v.requestRender(); assert(true, 'calls after dispose are safe no-ops'); | |
| // unsupported path | |
| delete global.WebGLRenderingContext; | |
| let msg = null; const failEls = []; | |
| const c2 = { clientWidth: 1, clientHeight: 1, appendChild: (e) => failEls.push(e) }; | |
| const v2 = new DomeView(c2, { onUnsupported: (m) => { msg = m; } }); | |
| assert(v2.supported === false && /unavailable/.test(msg) && failEls.length === 1, 'no-WebGL path shows message + calls onUnsupported'); | |
| } | |
| })); | |