import test from 'node:test'; import assert from 'node:assert/strict'; import { bracket, intentAt, sample, screenAngle, stateAt } from '../src/lookup.ts'; import { animate, ReviewClock, seekFromInput } from '../src/clock.ts'; import { actorStyle, shapePath, SHAPES } from '../src/visual.ts'; import { parseRoute, segmentHash } from '../src/route.ts'; import { panBox, viewportScale, zoomBox } from '../src/geometry.ts'; import type { Intent, State, Track } from '../src/contract.ts'; const track: Track = {t_s: [0, 1, 2], u_m: [0, 1, 3], v_m: [2, 2, 4], yaw_rad: [0, .5, 1], distance_m: [0, 1, 3.5], speed_mps: [1, 2.5, 0], cam_yaw_rad: [1, 2, 3], cam_pitch_rad: [0, 0, 0]}; test('隐藏或未布局的SVG保留有效尺度;除法溢出、平移和缩放拒绝非有限数', () => { const box = [0, 0, 20, 10], previous = .04; assert.equal(Math.max(box[2] / 0, box[3] / 0), Infinity); // Old formula fails. for (const rect of [{width: 0, height: 900}, {width: 1440, height: 0}, {width: NaN, height: 900}, {width: Infinity, height: 900}, {width: 1e-323, height: 900}]) { assert.equal(viewportScale(box, rect, previous), previous); } assert.equal(viewportScale(box, {width: 1000, height: 500}, previous), .02); assert.equal(panBox(box, Infinity, 1, previous), box); assert.equal(panBox(box, 1e308, 1, 1e308), box); assert.equal(zoomBox(box, 20, NaN), box); assert.equal(zoomBox(box, 20, .8, [Infinity, 0]), box); assert(zoomBox(box, 20, .8).every(Number.isFinite)); }); test('位置、路程、机身/镜头朝向插值;速度只查后端表', () => { assert.deepEqual(bracket(track.t_s, .5), [0, .5]); assert.deepEqual(sample(track, .5), {u: .5, v: 2, yaw: .25, distance: .5, speed: 1, cameraYaw: 1.5, cameraPitch: 0}); assert.equal(sample(track, 1).speed, 2.5); assert.equal(sample(track, 2).speed, 0); assert.equal(sample(track, -1).u, 0); assert.equal(sample(track, 100).u, 3); assert.throws(() => bracket([], 0)); assert.throws(() => sample(track, NaN)); }); test('状态半开区间,末端闭合;重叠让行意图优先', () => { const rows: State[] = [{start_s: 0, end_s: 1, state: 'moving', reason: 'r', intent_index: 0}, {start_s: 1, end_s: 2, state: 'idle_remainder', reason: 'r', intent_index: 1}]; assert.equal(stateAt(rows, 1)?.state, 'idle_remainder'); assert.equal(stateAt(rows, 2)?.state, 'idle_remainder'); assert.equal(stateAt(rows, -1), undefined); const base: Intent = {start_s: 0, end_s: 2, kind: 'go_to', status: 'completed', intent_index: 0, target: null, reason: null, discard_reason: null}; assert.equal(intentAt([base, {...base, end_s: 1, kind: 'wait'}], .5)?.kind, 'wait'); assert.equal(intentAt([base, {...base, end_s: 1, kind: 'wait'}], 1)?.kind, 'go_to'); }); test('唯一播放时钟、1/4/10 倍速、边界和逐控制帧', () => { const clock = new ReviewClock([0, 360], 20), observed: number[] = []; const unsubscribe = clock.subscribe(s => observed.push(s.time)); clock.setPlaying(true); clock.advance(500); assert.equal(clock.snapshot().time, .5); clock.setSpeed(4); clock.advance(500); assert.equal(clock.snapshot().time, 2.5); clock.setSpeed(10); clock.advance(500); assert.equal(clock.snapshot().time, 7.5); clock.frame(1); assert.equal(clock.snapshot().time, 7.55); assert.equal(clock.snapshot().playing, false); clock.frame(-1); assert.equal(clock.snapshot().time, 7.5); clock.seek(.03); clock.frame(1); assert.equal(clock.snapshot().time, .05); clock.seek(5.02); clock.frame(-1); assert.equal(clock.snapshot().time, 5); clock.seek(359); clock.setPlaying(true); clock.advance(1000); assert.equal(clock.snapshot().time, 360); assert.equal(clock.snapshot().playing, false); clock.setPlaying(true); assert.equal(clock.snapshot().time, 0); clock.seek(-2); assert.equal(clock.snapshot().time, 0); clock.seek(NaN); assert.equal(clock.snapshot().time, 0); assert.throws(() => clock.setSpeed(3)); unsubscribe(); const count = observed.length; clock.seek(9); assert.equal(observed.length, count); }); test('滑条先读输入再暂停;旧处理器被同步订阅写回,负对照确实失败', () => { const requested = 136.834, clock = new ReviewClock([0, 360], 20), input = {value: ''}; clock.seek(37.25); const unsubscribe = clock.subscribe(({time}) => {input.value = String(time);}); input.value = String(requested); clock.setPlaying(false); clock.seek(Number(input.value)); const oldActual = clock.snapshot().time; assert.throws(() => assert.equal(oldActual, requested)); assert.equal(oldActual, 37.25); input.value = String(requested); seekFromInput(clock, input); assert.equal(clock.snapshot().time, requested); assert.equal(input.value, String(requested)); console.log(JSON.stringify({name: 'slider-synchronous-writeback-regression', requested_s: requested, old_actual_s: oldActual, fixed_actual_s: clock.snapshot().time, negative_control_rejected: true})); unsubscribe(); }); test('形状按类别、颜色按实例:移动主体圆、观察者三角,前六实例颜色各不相同', () => { const kinds = ['robot', 'humanoid', 'humanoid', 'humanoid', 'observer_embodied', 'observer_virtual']; const styles = kinds.map((kind, order) => actorStyle({order, kind})); assert.equal(new Set(styles.map(s => s.color)).size, 6); assert.deepEqual(styles.map(s => s.shape), ['circle', 'circle', 'circle', 'circle', 'triangle', 'triangle']); assert.deepEqual(actorStyle({order: 0, kind: 'robot'}), actorStyle({order: 0, kind: 'robot'})); for (const shape of SHAPES) {assert.match(shapePath(shape), /^M/); assert.match(shapePath(shape), /Z$/);} assert.throws(() => actorStyle({order: -1, kind: 'robot'})); }); test('暂停没有 RAF,恢复只开一个循环,卸载取消自有帧', () => { const clock = new ReviewClock([0, 360], 20), callbacks = new Map void>(); let next = 0; const stop = animate(clock, {request: cb => {callbacks.set(++next, cb); return next;}, cancel: id => {callbacks.delete(id);}}); assert.equal(callbacks.size, 0); clock.setSpeed(10); clock.seek(10); assert.equal(callbacks.size, 0); clock.setPlaying(true); assert.equal(callbacks.size, 1); clock.setSpeed(4); assert.equal(callbacks.size, 1); const fire = (time: number) => {const [id, callback] = [...callbacks][0]; callbacks.delete(id); callback(time);}; fire(1000); fire(1500); assert.equal(clock.snapshot().time, 12); clock.setPlaying(false); assert.equal(callbacks.size, 0); clock.setPlaying(true); assert.equal(callbacks.size, 1); stop(); assert.equal(callbacks.size, 0); }); test('视频拥有时钟时撤销 RAF;释放后恢复且不发布额外时刻', () => { const clock = new ReviewClock([0, 360], 20), owner = {}, other = {}; const callbacks = new Map void>(); let next = 0, publications = 0; const unsubscribe = clock.subscribe(() => {publications++;}); const stop = animate(clock, {request: cb => {callbacks.set(++next, cb); return next;}, cancel: id => {callbacks.delete(id);}}); clock.setPlaying(true); assert.equal(callbacks.size, 1); const beforeDrive = publications; clock.drive(owner); assert.equal(callbacks.size, 0, '视频驱动期间不得保留空 RAF 循环'); assert.equal(publications, beforeDrive, '切换驱动不触发画面订阅者重入'); clock.present(owner, 12.5); assert.equal(clock.snapshot().time, 12.5); assert.equal(callbacks.size, 0); clock.undrive(other); assert.equal(callbacks.size, 0); clock.undrive(owner); assert.equal(callbacks.size, 1); const fire = (time: number) => {const [id, callback] = [...callbacks][0]; callbacks.delete(id); callback(time);}; fire(10000); fire(10100); assert.equal(clock.snapshot().time, 12.6); clock.drive(owner); assert.equal(callbacks.size, 0); clock.setPlaying(false); clock.undrive(owner); assert.equal(callbacks.size, 0); clock.setPlaying(true); assert.equal(callbacks.size, 1); stop(); clock.drive(owner); clock.undrive(owner); assert.equal(callbacks.size, 0); unsubscribe(); }); test('视频先接管再播放也不会创建 RAF;卸载后释放不重启', () => { const clock = new ReviewClock([0, 360], 20), owner = {}; let requests = 0; clock.drive(owner); const stop = animate(clock, {request: () => ++requests, cancel: () => {}}); clock.setPlaying(true); clock.present(owner, 3); assert.equal(requests, 0); stop(); clock.undrive(owner); assert.equal(requests, 0); }); test('按输入坐标约定画朝向,正 v 在屏幕上向上', () => { assert.equal(screenAngle(Math.PI / 2, {yaw_zero: '+u', yaw_positive: 'counterclockwise'}), -90); assert.equal(screenAngle(Math.PI / 2, {yaw_zero: '+u', yaw_positive: 'clockwise'}), 90); assert.equal(screenAngle(0, {yaw_zero: '+v', yaw_positive: 'counterclockwise'}), -90); }); test('段号、时刻和主体筛选地址可分享;非法时刻不带 NaN', () => { const hash = segmentHash('根/段 空格', 19.375, ['actor-000', 'observer-001']); assert.deepEqual(parseRoute(hash), {page: 'segment', id: '根/段 空格', time: 19.375, actors: ['actor-000', 'observer-001']}); assert.deepEqual(parseRoute(segmentHash('r/.', 0, [])), {page: 'segment', id: 'r/.', time: 0, actors: []}); assert.deepEqual(parseRoute('#/segments'), {page: 'segments'}); assert.deepEqual(parseRoute('#/segments/%ff'), {page: 'segments'}); assert.equal((parseRoute('#/segments/a?t=bad') as {time: number}).time, 0); });