import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import vm from 'node:vm'; const source = fs.readFileSync(process.env.HEADING_SCRIPT || new URL('../assets/custom/js/app.js', import.meta.url), 'utf8'); // Exercise the shipped heading module, not a rewritten copy of its logic. const start = source.indexOf('var vK="h1, h2, h3, h4"'); const end = source.indexOf('var PH={}', start); assert.ok(start >= 0 && end > start, 'heading module boundaries must exist'); const moduleSource = source.slice(start, end); function fixture({ reduced = false, spans = true } = {}) { const calls = [], observers = [], clones = [], words = []; const node = (name) => ({ name, isConnected: true, style: {}, setAttribute() {}, closest() { return null; } }); const heading = node('heading'); const headingParts = spans ? [node('title'), node('subtitle')] : [heading]; heading.querySelectorAll = () => spans ? headingParts : []; const paragraph = node('paragraph'); paragraph.cloneNode = () => { const clone = node('paragraph clone'); clones.push(clone); return clone; }; const group = node('group'); group.querySelector = () => heading; group.querySelectorAll = () => [paragraph]; group.insertBefore = () => {}; paragraph.parentElement = group; const record = (method, targets, props) => { calls.push({ method, targets: targets.map(x => x.name), props }); for (const target of targets) { for (const key of ['opacity', 'xPercent', 'yPercent', 'filter']) { if (key in props) target.style[key] = props[key]; } } }; vm.runInNewContext(moduleSource + '\ndK(group,{group:"hero"});', { group, Zq: reduced, CSS: { escape: x => x }, document: { querySelectorAll: () => [group], createElement: () => ({ style: {}, appendChild() {} }), }, L$: class { constructor() { this.words = [node('word 1'), node('word 2')]; words.push(...this.words); } }, n: { set: (targets, props) => record('set', targets, props), to: (targets, props) => record('to', targets, props), fromTo: (targets, from, props) => { record('from', targets, from); record('to', targets, props); }, killTweensOf: targets => calls.push({ method: 'kill', targets: targets.map(x => x.name) }), }, l1: (target, options) => observers.push(options), }); return { group, headingParts, paragraph, words, clones, calls, observers, enter(direction = 1) { observers.forEach(o => o.callback({ isIn: true, direction })); }, leave() { observers.forEach(o => o.callback({ isIn: false, direction: 1 })); }, }; } for (const spans of [true, false]) { test(`reduced motion keeps ${spans ? 'split' : 'plain'} headings and original copy visible`, () => { const f = fixture({ reduced: true, spans }); f.enter(); f.leave(); f.enter(-1); for (const element of [...f.headingParts, f.paragraph]) { assert.notEqual(element.style.opacity, 0, `${element.name} must not be hidden`); assert.notEqual(element.style.opacity, '0', `${element.name} must not be hidden`); assert.ok(!element.style.xPercent && !element.style.yPercent, 'no animation offsets'); } assert.equal(f.clones.length, 0, 'static copy must not be split or cloned'); assert.equal(f.observers.length, 0, 'scroll must not hide static headings again'); assert.equal(f.calls.filter(c => c.method === 'to' || c.method === 'from').length, 0); }); } test('normal motion retains heading reveal, paragraph stagger, and replay on scroll', () => { const f = fixture(); assert.equal(f.observers.length, 1); assert.equal(f.clones.length, 1); assert.equal(f.headingParts[0].style.opacity, 0); assert.equal(f.headingParts[0].style.xPercent, 20); f.enter(); const headingTween = f.calls.find(c => c.method === 'to' && c.targets[0] === 'title'); assert.equal(headingTween.props.opacity, 1); assert.equal(headingTween.props.filter, 'blur(0px)'); assert.equal(headingTween.props.stagger.each, 0.2); assert.equal(headingTween.props.stagger.from, 'start'); assert.equal(headingTween.props.delay, 0.1); const paragraphTween = f.calls.find(c => c.method === 'to' && c.targets[0] === 'word 1'); assert.equal(paragraphTween.props.stagger.each, 0.04); assert.ok(Math.abs(paragraphTween.props.delay - 0.15) < 1e-9); assert.equal(f.group._hgAnimated, true); f.leave(); assert.equal(f.headingParts[0].style.opacity, 0); f.enter(-1); assert.equal(f.headingParts[0].style.opacity, 1); assert.equal(f.calls.filter(c => c.method === 'to' && c.targets[0] === 'title').at(-1).props.stagger.from, 'end'); });