|
| 1 | +// Frame 0 of a real render shows the starting state that a sub-composition's timeline sets at |
| 2 | +// t=0, not the page's pre-timeline styles. Needs Chrome + ffmpeg: integration lane. |
| 3 | + |
| 4 | +import { copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 5 | +import { tmpdir } from "node:os"; |
| 6 | +import { join, resolve } from "node:path"; |
| 7 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 8 | +import { resolveConfig } from "@hyperframes/engine"; |
| 9 | +import { createRenderJob, executeRenderJob } from "../renderOrchestrator.js"; |
| 10 | +import { HAS_FFMPEG, frameLevels } from "./frameLevelsTestFixtures.js"; |
| 11 | + |
| 12 | +const WIDTH = 160; |
| 13 | +const HEIGHT = 90; |
| 14 | +const FPS = 30; |
| 15 | +const REVEAL_FRAME = 15; |
| 16 | + |
| 17 | +const INDEX = `<!DOCTYPE html> |
| 18 | +<html lang="en"> |
| 19 | +<head> |
| 20 | + <meta charset="UTF-8" /> |
| 21 | + <script src="assets/gsap.min.js"></script> |
| 22 | + <style>* { margin: 0; padding: 0; } body { overflow: hidden; background: #000; }</style> |
| 23 | +</head> |
| 24 | +<body> |
| 25 | + <div id="root" data-composition-id="main" data-start="0" data-duration="1" |
| 26 | + data-width="${WIDTH}" data-height="${HEIGHT}" data-fps="${FPS}"> |
| 27 | + <div id="s-words" data-composition-id="words" data-composition-src="compositions/words.html" |
| 28 | + data-start="0" data-duration="1" data-track-index="0" |
| 29 | + data-width="${WIDTH}" data-height="${HEIGHT}"></div> |
| 30 | + </div> |
| 31 | + <script>window.__timelines["main"] = gsap.timeline({ paused: true });</script> |
| 32 | +</body> |
| 33 | +</html> |
| 34 | +`; |
| 35 | + |
| 36 | +// The words have no hidden CSS state: only the timeline's t=0 steps hide them. |
| 37 | +const WORDS = `<!DOCTYPE html> |
| 38 | +<html> |
| 39 | +<body> |
| 40 | +<template id="words-template"> |
| 41 | + <style> |
| 42 | + @font-face { font-family: "Fixture"; src: url("assets/fixture.woff2") format("woff2"); font-weight: 500; } |
| 43 | + #words { position: absolute; inset: 0; background: #000; } |
| 44 | + #words .w { position: absolute; top: 20px; font: 500 40px "Fixture", serif; color: #fff; } |
| 45 | + </style> |
| 46 | + <div id="words" data-composition-id="words" data-width="${WIDTH}" data-height="${HEIGHT}" data-duration="1"> |
| 47 | + <span class="w">Aa</span><span class="w">Bb</span> |
| 48 | + </div> |
| 49 | + <script> |
| 50 | + (() => { |
| 51 | + const tl = gsap.timeline({ paused: true }); |
| 52 | + const words = [...document.querySelectorAll("#words .w")]; |
| 53 | + words.forEach((w) => tl.set(w, { visibility: "hidden" }, 0)); |
| 54 | + words.forEach((w, i) => tl.set(w, { visibility: "visible", left: 10 + i * 70 }, ${REVEAL_FRAME / FPS})); |
| 55 | + tl.set({}, {}, 1); |
| 56 | + window.__timelines["words"] = tl; |
| 57 | + })(); |
| 58 | + </script> |
| 59 | +</template> |
| 60 | +</body> |
| 61 | +</html> |
| 62 | +`; |
| 63 | + |
| 64 | +let root: string; |
| 65 | + |
| 66 | +describe.skipIf(!HAS_FFMPEG)( |
| 67 | + "first frame of a real render — sub-composition starting state", |
| 68 | + () => { |
| 69 | + let levels: number[]; |
| 70 | + |
| 71 | + beforeAll(async () => { |
| 72 | + root = mkdtempSync(join(tmpdir(), "hf-first-frame-")); |
| 73 | + mkdirSync(join(root, "assets"), { recursive: true }); |
| 74 | + mkdirSync(join(root, "compositions"), { recursive: true }); |
| 75 | + copyFileSync( |
| 76 | + resolve(import.meta.dirname, "../../../../core/node_modules/gsap/dist/gsap.min.js"), |
| 77 | + join(root, "assets/gsap.min.js"), |
| 78 | + ); |
| 79 | + copyFileSync( |
| 80 | + resolve( |
| 81 | + import.meta.dirname, |
| 82 | + "../../../tests/heygen-promo-preview-assets/src/TT_Norms_Pro_Medium.woff2", |
| 83 | + ), |
| 84 | + join(root, "assets/fixture.woff2"), |
| 85 | + ); |
| 86 | + writeFileSync(join(root, "index.html"), INDEX); |
| 87 | + writeFileSync(join(root, "compositions/words.html"), WORDS); |
| 88 | + const job = createRenderJob({ |
| 89 | + fps: FPS, |
| 90 | + quality: "draft", |
| 91 | + format: "mp4", |
| 92 | + hdrMode: "force-sdr", |
| 93 | + workers: 1, |
| 94 | + producerConfig: resolveConfig({ browserGpuMode: "software" }), |
| 95 | + }); |
| 96 | + const output = join(root, "out.mp4"); |
| 97 | + await executeRenderJob(job, root, output); |
| 98 | + levels = frameLevels(output, WIDTH, HEIGHT); |
| 99 | + }, 120_000); |
| 100 | + |
| 101 | + afterAll(() => { |
| 102 | + if (root) rmSync(root, { recursive: true, force: true }); |
| 103 | + }); |
| 104 | + |
| 105 | + it("frame 0 matches frame 1: the words are already hidden", () => { |
| 106 | + expect(levels[0]).toBeCloseTo(levels[1]!, 0); |
| 107 | + expect(levels[0]).toBeLessThan(1); |
| 108 | + }); |
| 109 | + |
| 110 | + it("the words appear when the timeline reveals them", () => { |
| 111 | + expect(levels[REVEAL_FRAME]! - levels[0]!).toBeGreaterThan(2); |
| 112 | + }); |
| 113 | + }, |
| 114 | +); |
0 commit comments