import assert from 'node:assert/strict' import { test } from 'node:test' import { createDetailMotion } from '../src/utils/detailMotion.ts' class FakeAnimation extends EventTarget { cancelled = false cancel() { this.cancelled = true; this.dispatchEvent(new Event('cancel')) } finish() { this.dispatchEvent(new Event('finish')) } } class FakeElement { animations = [] animate(frames, options) { const animation = new FakeAnimation() Object.assign(animation, { frames, options }) this.animations.push(animation) return animation } getAnimations() { return this.animations.filter(a => !a.cancelled) } } const frames = [{ height: '186px' }, { height: '429px' }] test('completed height effects are removed so content can shrink and nested records can grow', () => { const motion = createDetailMotion(() => false) const element = new FakeElement() let done = 0 motion.play(element, frames, { duration: 420 }, () => done++) element.animations[0].finish() assert.equal(done, 1) assert.equal(element.getAnimations().length, 0) }) test('rapid reversal finishes the previous transition once and ignores its stale finish event', () => { const motion = createDetailMotion(() => false) const element = new FakeElement() let first = 0, second = 0 motion.play(element, frames, {}, () => first++) const old = element.animations[0] motion.play(element, frames.toReversed(), {}, () => second++) old.finish() assert.equal(first, 1) assert.equal(second, 0) assert.equal(element.getAnimations().length, 1) element.animations[1].finish() assert.equal(second, 1) assert.equal(element.getAnimations().length, 0) }) test('explicit cancellation removes effects and calls completion only once', () => { const motion = createDetailMotion(() => false) const element = new FakeElement() let done = 0 motion.play(element, frames, {}, () => done++) motion.cancel(element) motion.cancel(element) element.animations[0].finish() assert.equal(done, 1) assert.equal(element.getAnimations().length, 0) }) test('reduced motion skips animations and still completes', () => { const motion = createDetailMotion(() => true) const element = new FakeElement() let done = 0 motion.play(element, frames, {}, () => done++) assert.equal(done, 1) assert.equal(element.animations.length, 0) }) test('external cancellation and disposal release all owned animations', () => { const motion = createDetailMotion(() => false) const a = new FakeElement(), b = new FakeElement() let done = 0 motion.play(a, frames, {}, () => done++) a.animations[0].cancel() motion.play(a, frames, {}, () => done++) motion.play(b, frames, {}, () => done++) motion.dispose() assert.equal(done, 3) assert.equal(a.getAnimations().length + b.getAnimations().length, 0) })