Spaces:
Sleeping
Sleeping
Download tests/test_slice_navigation.cjs from HarryDeepWise/web_demo: direct link, hf CLI and curl.
- Browser
- Download file 4.4 kB
-
https://huggingface.co/spaces/HarryDeepWise/web_demo/resolve/main/tests/test_slice_navigation.cjs
- Command line
-
hf download hf://spaces/HarryDeepWise/web_demo/tests/test_slice_navigation.cjs
-
curl -L -o test_slice_navigation.cjs https://huggingface.co/spaces/HarryDeepWise/web_demo/resolve/main/tests/test_slice_navigation.cjs
4.4 kB
| const assert = require('node:assert/strict'); | |
| const fs = require('node:fs'); | |
| const path = require('node:path'); | |
| const vm = require('node:vm'); | |
| const { test } = require('node:test'); | |
| function setup() { | |
| let now = 0; | |
| const context = vm.createContext({ | |
| document: { getElementById() { | |
| return { | |
| min: '0', max: '9', value: '5', disabled: false, textContent: '5', listeners: {}, | |
| addEventListener(type, handler, options) { this.listeners[type] = { handler, options }; }, | |
| }; | |
| } }, | |
| performance: { now: () => now }, | |
| }); | |
| const source = fs.readFileSync(path.join(__dirname, '../frontend/app.js'), 'utf8'); | |
| // Exercise actual application handlers without booting the UI or calling APIs. | |
| vm.runInContext(source.replace(/^init\(\);$/m, '') + ` | |
| globalThis.api = { state, views, setViewSlice, draws: 0 }; | |
| drawAll = () => { api.draws++; }; | |
| state.volume = {}; | |
| state.sessionReady = true; | |
| Object.keys(views).forEach(name => { | |
| state.slices[name] = 5; | |
| bindCanvas(name, views[name].canvas); | |
| }); | |
| `, context); | |
| const api = context.api; | |
| api.wheel = (name, deltaY, time, extra = {}) => { | |
| now = time; | |
| const event = { | |
| deltaY, deltaX: 0, deltaMode: 0, ctrlKey: false, prevented: false, | |
| preventDefault() { this.prevented = true; }, ...extra, | |
| }; | |
| api.views[name].canvas.listeners.wheel.handler(event); | |
| return event; | |
| }; | |
| return api; | |
| } | |
| test('all views navigate independently and synchronize slider/label', () => { | |
| for (const name of ['axial', 'coronal', 'sagittal']) { | |
| const api = setup(); | |
| assert.equal(api.views[name].canvas.listeners.wheel.options.passive, false); | |
| assert.equal(api.wheel(name, 120, 0).prevented, true); | |
| assert.equal(api.state.slices[name], 6); | |
| assert.equal(api.views[name].slider.value, '6'); | |
| assert.equal(api.views[name].indexLabel.textContent, '6'); | |
| for (const other of Object.keys(api.views).filter(key => key !== name)) { | |
| assert.equal(api.state.slices[other], 5); | |
| } | |
| api.wheel(name, -120, 100); | |
| assert.equal(api.state.slices[name], 5); | |
| } | |
| }); | |
| test('boundaries clamp without extra redraws or page scrolling', () => { | |
| const api = setup(); | |
| api.setViewSlice('axial', 999); | |
| assert.equal(api.state.slices.axial, 9); | |
| const draws = api.draws; | |
| assert.equal(api.wheel('axial', 120, 0).prevented, true); | |
| assert.equal(api.draws, draws); | |
| api.setViewSlice('axial', -999); | |
| api.wheel('axial', -120, 100); | |
| assert.equal(api.state.slices.axial, 0); | |
| }); | |
| test('trackpad deltas accumulate and fast wheel spins remain responsive', () => { | |
| const api = setup(); | |
| for (let i = 0; i < 4; i++) api.wheel('axial', 10, i * 10); | |
| assert.equal(api.state.slices.axial, 6); | |
| for (let i = 40; i <= 200; i += 10) api.wheel('axial', 120, i); | |
| assert.equal(api.state.slices.axial, 9); | |
| }); | |
| test('partial deltas reset on idle, reversal and pointer leave', () => { | |
| const api = setup(); | |
| api.wheel('axial', 30, 0); | |
| api.wheel('axial', 20, 300); | |
| assert.equal(api.state.slices.axial, 5); | |
| api.wheel('axial', -30, 310); | |
| api.wheel('axial', -10, 320); | |
| assert.equal(api.state.slices.axial, 4); | |
| api.wheel('axial', 30, 500); | |
| api.views.axial.canvas.listeners.pointerleave.handler(); | |
| api.wheel('axial', 20, 510); | |
| assert.equal(api.state.slices.axial, 4); | |
| }); | |
| test('line and page delta modes move one slice per event', () => { | |
| for (const deltaMode of [1, 2]) { | |
| const api = setup(); | |
| api.wheel('coronal', 1, 0, { deltaMode }); | |
| assert.equal(api.state.slices.coronal, 6); | |
| } | |
| }); | |
| test('loading, inference, expired sessions and disabled sliders block navigation', () => { | |
| for (const lock of ['busy', 'session', 'slider', 'volume']) { | |
| const api = setup(); | |
| if (lock === 'busy') api.state.busy = true; | |
| if (lock === 'session') api.state.sessionReady = false; | |
| if (lock === 'slider') api.views.axial.slider.disabled = true; | |
| if (lock === 'volume') api.state.volume = null; | |
| api.wheel('axial', 120, 0); | |
| api.setViewSlice('axial', 8); | |
| assert.equal(api.state.slices.axial, 5); | |
| assert.equal(api.draws, 0); | |
| } | |
| }); | |
| test('zoom and horizontal gestures are not intercepted', () => { | |
| const api = setup(); | |
| assert.equal(api.wheel('axial', 120, 0, { ctrlKey: true }).prevented, false); | |
| assert.equal(api.wheel('axial', 10, 100, { deltaX: 100 }).prevented, false); | |
| assert.equal(api.state.slices.axial, 5); | |
| }); | |