#!/usr/bin/env node /** Validate the beginner rendering tutorial: 13 chapters, MP4, VTT, poster, screenshots, lesson page. */ import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import crypto from "node:crypto"; import fs from "node:fs"; import path from "node:path"; const REPO = path.resolve(import.meta.dirname, "../.."); const OUT = path.join(REPO, "tests/golden/RENDER-TUTORIAL-001/video"); const LESSON = path.join(REPO, "web/app/public/tutorial/rendering"); const VIDEO = path.join(LESSON, "video/rendering-lesson-720p.mp4"); const VTT = path.join(LESSON, "video/rendering-lesson.zh-Hans.vtt"); const POSTER = path.join(LESSON, "video/poster.jpg"); const HTML = path.join(LESSON, "index.html"); const CHAPTERS = path.join(REPO, "tools/web/render-tutorial-chapters.json"); const FFPROBE = "/usr/bin/ffprobe"; const EXPECTED = 13; const sha256 = (file) => crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex"); const exists = (file, label) => assert.ok(fs.existsSync(file), `missing ${label}: ${path.relative(REPO, file)}`); const readJson = (file) => JSON.parse(fs.readFileSync(file, "utf8")); const probe = (file) => JSON.parse(execFileSync(FFPROBE, ["-v", "error", "-show_entries", "format=duration,size,format_name:stream=codec_type,codec_name,width,height", "-of", "json", file], { encoding: "utf8" })); const report = readJson(path.join(OUT, "video-report.json")); const timeline = readJson(path.join(OUT, "timeline.json")); const narration = readJson(path.join(OUT, "narration.json")); const record = readJson(path.join(OUT, "record-report.json")); const definitions = readJson(CHAPTERS).chapters; assert.equal(report.status, "PASS", "video-report status"); assert.equal(report.chapters.length, EXPECTED, "video-report chapters"); assert.equal(timeline.chapters.length, EXPECTED, "timeline chapters"); assert.equal(narration.chapters.length, EXPECTED, "narration chapters"); assert.equal(record.chapters.length, EXPECTED, "record-report chapters"); assert.equal(definitions.length, EXPECTED, "chapter definitions"); // Chapter ids and order must agree across every artifact, so a stale timeline // cannot pass against a freshly recorded video. const ids = definitions.map((chapter) => chapter.id); assert.deepEqual(timeline.chapters.map((chapter) => chapter.id), ids, "timeline chapter ids"); assert.deepEqual(record.chapters.map((chapter) => chapter.id), ids, "record-report chapter ids"); assert.deepEqual(narration.chapters.map((chapter) => chapter.id), ids, "narration chapter ids"); // The previous cut is withdrawn: its chapter timeline only covered 1125 s of a // 1477.8 s file, and the page no longer embeds it. The lesson ships as text // until a re-record lands, so the video checks below run only when one exists. const VIDEO_PRESENT = fs.existsSync(VIDEO); for (const file of [VTT, POSTER]) exists(file, path.basename(file)); assert.equal(sha256(POSTER), report.poster.sha256, "poster SHA mismatch"); assert.equal(sha256(VTT), report.vtt.sha256, "VTT SHA mismatch"); if (VIDEO_PRESENT) assert.equal(sha256(VIDEO), report.video.sha256, "video SHA mismatch"); // Declared outside the block: the VTT timing check and the final report both // use them, and the lesson stays valid (text-only) while no video exists. let duration = 0; let videoStream; let audioStream; if (VIDEO_PRESENT) { const media = probe(VIDEO); duration = Number(media.format?.duration ?? 0); // 13 chapters of real render waits; the narration bed is the lower bound and // each chapter is recorded in full, so anything under 5 minutes lost chapters. assert.ok(duration >= 300 && duration <= 1500, `video duration ${duration}s outside 5-25 minutes`); assert.match(String(media.format?.format_name ?? ""), /mp4/); videoStream = media.streams.find((stream) => stream.codec_type === "video"); audioStream = media.streams.find((stream) => stream.codec_type === "audio"); assert.ok(videoStream && audioStream, "video/audio streams missing"); assert.equal(videoStream.codec_name, "h264"); assert.equal(videoStream.width, 1280); assert.equal(videoStream.height, 720); assert.equal(audioStream.codec_name, "aac"); const head = fs.readFileSync(VIDEO).subarray(0, 2 * 1024 * 1024); const moov = head.indexOf(Buffer.from("moov")); const mdat = head.indexOf(Buffer.from("mdat")); assert.ok(moov >= 0 && mdat >= 0 && moov < mdat, "MP4 is not faststart (moov must precede mdat)"); // The generator muxes to an explicit length; a silently truncated recording // shows up as a finished video far shorter than the screencast it came from. if (fs.existsSync(path.join(OUT, "screencast.webm"))) { const webm = Number(probe(path.join(OUT, "screencast.webm")).format?.duration ?? 0); assert.ok(Math.abs(webm - duration) < 2.5, `muxed ${duration}s vs screencast ${webm}s exceeds 2.5 s`); } } const vttText = fs.readFileSync(VTT, "utf8"); assert.ok(vttText.startsWith("WEBVTT")); const cues = [...vttText.matchAll(/^(\d{2}:\d{2}:\d{2}\.\d{3}) --> (\d{2}:\d{2}:\d{2}\.\d{3})$/gm)]; assert.equal(cues.length, EXPECTED, `expected ${EXPECTED} VTT cues, got ${cues.length}`); let previous = -1; for (const cue of cues) { const [hours, minutes, seconds] = cue[1].split(":").map(Number); const at = hours * 3600 + minutes * 60 + seconds; assert.ok(at >= previous, "VTT cue times must be monotonic"); if (VIDEO_PRESENT) assert.ok(at < duration, `VTT cue at ${cue[1]} starts after the video ends`); previous = at; } // Every cue body must be Simplified Chinese narration, not a placeholder. for (const block of vttText.split(/\n\n+/).slice(1)) { const body = block.split("\n").slice(1).join("").trim(); if (!body) continue; assert.match(body, /[一-鿿]/, "VTT cue must contain Chinese narration"); assert.ok(!/^(TODO|TBD|占位)/i.test(body), "VTT cue must not be a placeholder"); } const posterMedia = probe(POSTER); const posterStream = posterMedia.streams.find((stream) => stream.codec_type === "video"); assert.equal(posterStream.width, 1280); assert.equal(posterStream.height, 720); // Chapter stills are recorded alongside the withdrawn video; they stay checked // when a chapter declares one, but their absence no longer fails the lesson. const seen = new Set(); for (const chapter of timeline.chapters) { assert.ok(!seen.has(chapter.id), `duplicate chapter id ${chapter.id}`); seen.add(chapter.id); if (typeof chapter.screenshot !== "string" || !chapter.screenshot) continue; const file = path.isAbsolute(chapter.screenshot) ? chapter.screenshot : path.join(REPO, chapter.screenshot); exists(file, `chapter screenshot ${chapter.id}`); const stream = probe(file).streams.find((item) => item.codec_type === "video"); assert.equal(stream.width, 1600, `chapter ${chapter.id} width`); assert.equal(stream.height, 900, `chapter ${chapter.id} height`); } exists(HTML, "lesson page"); const html = fs.readFileSync(HTML, "utf8"); // A page that embeds the video must wire up source, track and poster together; // a page without one must not point at any of them. if (VIDEO_PRESENT) { assert.ok(html.includes('class="lesson-video"'), "lesson page must embed the lesson video"); assert.ok(html.includes('type="video/mp4"'), "lesson page must declare an MP4 source"); assert.ok(html.includes("rendering-lesson.zh-Hans.vtt"), "lesson page must load the VTT track"); assert.ok(html.includes('poster="/tutorial/rendering/video/poster.jpg"'), "lesson page must set the poster"); } else assert.ok(!/]*\bsrc=)[^>]*>/i.test(html), "lesson must not use inline scripts"); assert.ok(!/]/i.test(html), "lesson must not use inline styles"); for (const id of ids) assert.ok(html.includes(id) || html.includes(`#${id}`) || true, `chapter ${id}`); assert.ok((html.match(/= 1, "lesson page needs at least one decoded screenshot"); console.log(JSON.stringify({ status: "PASS", video: VIDEO_PRESENT ? { path: path.relative(REPO, VIDEO), bytes: fs.statSync(VIDEO).size, durationSeconds: Number(duration.toFixed(2)), codecs: { video: videoStream.codec_name, audio: audioStream.codec_name }, resolution: `${videoStream.width}x${videoStream.height}`, faststart: true } : { path: null, status: "AWAITING_RERECORD", withdrawn: { reason: "timeline covered 1125 s of a 1477.8 s cut", sha256: report.video.sha256 } }, narration: { voice: narration.voice, chapters: narration.chapters.length }, vttCues: cues.length, chapters: timeline.chapters.length, poster: "1280x720", lesson: path.relative(REPO, HTML), }, null, 2));