import test from 'node:test'; import assert from 'node:assert/strict'; import { Window } from 'happy-dom'; import { OPENING, eyeCoverScale, globeRadiusPx, heightForGlobeRadius, openingPlaying, openingPose, openingStage, openingWanted, playOpeningIntro, } from './openingIntro.js'; const LENS = { fovy: Math.PI / 4, viewportHeight: 800 }; const TARGET = { lon: 10.4, lat: 51.1, height: 23e6, heading: 0, pitch: -1 }; const LANDED = { lon: TARGET.lon, lat: TARGET.lat, height: TARGET.height }; const END_MS = OPENING.centreMs + OPENING.flightStartMs + OPENING.flightMs + 1; test('the words fade, then the eye opens past the screen before the globe arrives', () => { const start = openingStage(0); assert.equal(start.text, 1); assert.equal(start.shut, false); assert.equal(start.pupilOpacity, 1); assert.equal(start.flight, 0); const centred = openingStage(OPENING.centreMs); assert.equal(centred.text, 0); assert.equal(centred.centre, 1); assert.equal(centred.shut, true, 'shut but for the eye'); assert.equal(centred.eyeGrowth, 0); const half = openingStage(OPENING.centreMs + OPENING.eyeMs / 2); assert.ok(half.eyeGrowth > 0 && half.eyeGrowth < 0.5, 'it speeds up'); assert.equal(half.pupilOpacity, 1, 'the planet stays in the pupil'); const open = openingStage(OPENING.centreMs + OPENING.eyeMs); assert.equal(open.eyeGrowth, 1); assert.equal(open.eyeOpen, true); assert.equal(open.eyeOpacity, 0); assert.equal(open.shutterOpacity, 0); const real = openingStage(OPENING.centreMs + OPENING.pupilMs[1]); assert.equal(real.pupilOpacity, 0); assert.ok(real.flight > 0 && real.flight < 1); assert.equal(openingStage(END_MS).done, true); }); test('the open eye reaches past the corners of any screen', () => { const eye = { width: 128, height: 80 }; for (const view of [ { width: 1280, height: 720 }, { width: 375, height: 812 }, { width: 3840, height: 1600 }, ]) { const scale = eyeCoverScale(eye, view); // The eye's opening, taken as the ellipse inside its outline, holds // the corners. const a = (eye.width / 2) * scale; const b = (eye.height / 2) * scale; assert.ok((view.width / 2 / a) ** 2 + (view.height / 2 / b) ** 2 < 1); } }); test('the flight turns west to east, slows and stops over the target', () => { const far = 4e8; const start = openingPose(0, { target: TARGET, farHeight: far }); assert.ok(Math.abs(start.height - far) < 1); assert.equal(start.lat, 0); const end = openingPose(1, { target: TARGET, farHeight: far }); assert.ok(Math.abs(end.lon - TARGET.lon) < 1e-9); assert.ok(Math.abs(end.lat - TARGET.lat) < 1e-9); assert.ok(Math.abs(end.height - TARGET.height) < 1e-3); // Unwrapped longitude falls all the way (the camera moves west, so the // surface turns east), fastest at first. let previous = Infinity; let previousStep = Infinity; for (let i = 0; i <= 10; i++) { const f = i / 10; const unwrapped = TARGET.lon + OPENING.spinDeg * (1 - (1 - (1 - f) ** 3)); assert.ok(unwrapped <= previous); if (i > 0) { const step = previous - unwrapped; assert.ok(step <= previousStep + 1e-9, 'the turn slows'); previousStep = step; } previous = unwrapped; const pose = openingPose(f, { target: TARGET, farHeight: far }); assert.ok(pose.lon >= -180 && pose.lon < 180); } }); test('the far start draws the globe the size of the pupil', () => { for (const radius of [12, 20, 64]) { const height = heightForGlobeRadius(radius, LENS); assert.ok(Math.abs(globeRadiusPx(height, LENS) - radius) < 1e-6); } assert.ok(globeRadiusPx(4e8, LENS) < globeRadiusPx(23e6, LENS)); }); test('reduced motion, Stories links and ?intro=0 open without it', () => { const win = (search, reduced = false) => ({ location: { search }, matchMedia: () => ({ matches: reduced }), }); assert.equal(openingWanted(win('')), true); assert.equal(openingWanted(win('', true)), false); assert.equal(openingWanted(win('?intro=0')), false); assert.equal(openingWanted(win('?story=abc')), false); assert.equal(openingWanted(win('?stories')), false); assert.equal(openingWanted(win('?intro=1')), true); }); // A loading screen and a camera port; the test runs the frames by hand. function stage() { const window = new Window({ width: 1280, height: 800 }); const doc = window.document; doc.body.innerHTML = `