|
| 1 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { heardClips } from "./projectMedia.js"; |
| 6 | + |
| 7 | +let dir = ""; |
| 8 | +afterEach(() => rmSync(dir, { recursive: true, force: true })); |
| 9 | + |
| 10 | +function project(scene: string): string { |
| 11 | + dir = mkdtempSync(join(tmpdir(), "hf-heard-")); |
| 12 | + mkdirSync(join(dir, "compositions")); |
| 13 | + writeFileSync( |
| 14 | + join(dir, "index.html"), |
| 15 | + `<!doctype html><html><body> |
| 16 | +<div id="root" data-composition-id="main" data-start="0" data-duration="30" data-width="1080" data-height="1920"> |
| 17 | + <div id="talk-host" data-composition-id="talk" data-composition-src="compositions/talk.html" |
| 18 | + data-start="10" data-duration="20" data-width="1080" data-height="1920"></div> |
| 19 | +</div></body></html>`, |
| 20 | + ); |
| 21 | + writeFileSync( |
| 22 | + join(dir, "compositions", "talk.html"), |
| 23 | + `<template id="talk-template"><div data-composition-id="talk" data-start="0" data-duration="20" data-width="1080" data-height="1920"> |
| 24 | +${scene} |
| 25 | +</div></template>`, |
| 26 | + ); |
| 27 | + return dir; |
| 28 | +} |
| 29 | + |
| 30 | +const probe = { seconds: async () => 6 }; |
| 31 | + |
| 32 | +describe("heardClips", () => { |
| 33 | + it("hears a speaking video inside a timed wrapper in a scene, where the render plays it", async () => { |
| 34 | + const clips = await heardClips( |
| 35 | + project(`<div id="wrap" class="clip" data-start="1" data-duration="8"> |
| 36 | + <video id="talk" src="../assets/talk.mp4" data-has-audio="true" data-start="0" data-duration="5"></video> |
| 37 | +</div>`), |
| 38 | + probe, |
| 39 | + ); |
| 40 | + expect(clips.map(({ src, start, end }) => ({ src, start, end }))).toEqual([ |
| 41 | + { src: "assets/talk.mp4", start: 10, end: 15 }, |
| 42 | + ]); |
| 43 | + }); |
| 44 | + |
| 45 | + it("ends a speaking clip with no set length where its media runs out, not where its scene ends", async () => { |
| 46 | + const clips = await heardClips( |
| 47 | + project( |
| 48 | + `<video id="talk" src="../assets/talk.mp4" data-has-audio="true" data-start="2" data-media-start="1"></video>`, |
| 49 | + ), |
| 50 | + probe, |
| 51 | + ); |
| 52 | + expect(clips.map(({ src, start, end }) => ({ src, start, end }))).toEqual([ |
| 53 | + { src: "assets/talk.mp4", start: 12, end: 17 }, |
| 54 | + ]); |
| 55 | + }); |
| 56 | + |
| 57 | + it("leaves out a clip that starts past its media's end, and reads each file's length once", async () => { |
| 58 | + const probed: string[] = []; |
| 59 | + const counting = { seconds: async (path: string) => (probed.push(path), 6) }; |
| 60 | + const clips = await heardClips( |
| 61 | + project(`<video id="late" src="../assets/talk.mp4" data-has-audio="true" data-start="0" data-media-start="7"></video> |
| 62 | +<video id="talk" src="../assets/talk.mp4" data-has-audio="true" data-start="8" data-duration="3"></video>`), |
| 63 | + counting, |
| 64 | + ); |
| 65 | + expect(clips.map(({ id, start, end }) => ({ id, start, end }))).toEqual([ |
| 66 | + { id: "talk-audio", start: 18, end: 21 }, |
| 67 | + ]); |
| 68 | + expect(probed).toHaveLength(1); |
| 69 | + }); |
| 70 | + |
| 71 | + it("hears a video only when it says data-has-audio and is not muted, as the render does", async () => { |
| 72 | + const clips = await heardClips( |
| 73 | + project(`<video id="broll" src="../assets/broll.mp4" data-start="0" data-duration="3"></video> |
| 74 | +<video id="quiet" src="../assets/quiet.mp4" data-has-audio="true" data-start="4" data-duration="3" muted></video> |
| 75 | +<video id="talk" src="../assets/talk.mp4" data-has-audio="true" data-start="8" data-duration="3"></video>`), |
| 76 | + probe, |
| 77 | + ); |
| 78 | + expect(clips.map(({ src, start, end }) => ({ src, start, end }))).toEqual([ |
| 79 | + { src: "assets/talk.mp4", start: 18, end: 21 }, |
| 80 | + ]); |
| 81 | + }); |
| 82 | +}); |
0 commit comments