|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { parseHTML } from "linkedom"; |
| 3 | +import { |
| 4 | + applyFreezeFrameToHtml, |
| 5 | + freezeExtractArgs, |
| 6 | + freezeFrameMediaTime, |
| 7 | + freezeStillFileName, |
| 8 | + readFreezeSource, |
| 9 | +} from "./freezeFrame.js"; |
| 10 | + |
| 11 | +const rampLane = JSON.stringify({ |
| 12 | + version: 1, |
| 13 | + lanes: [ |
| 14 | + { |
| 15 | + target: "rate", |
| 16 | + points: [ |
| 17 | + { t: 0, v: 1 }, |
| 18 | + { t: 2, v: 3 }, |
| 19 | + ], |
| 20 | + }, |
| 21 | + ], |
| 22 | +}); |
| 23 | + |
| 24 | +describe("freezeFrameMediaTime", () => { |
| 25 | + it("is the in-point plus clip time at normal speed", () => { |
| 26 | + expect( |
| 27 | + freezeFrameMediaTime({ |
| 28 | + clipStart: 1, |
| 29 | + playhead: 3.2, |
| 30 | + mediaStart: 5, |
| 31 | + playbackRate: 1, |
| 32 | + automation: null, |
| 33 | + }), |
| 34 | + ).toBeCloseTo(7.2, 6); |
| 35 | + }); |
| 36 | + |
| 37 | + it("scales by a constant playback rate", () => { |
| 38 | + expect( |
| 39 | + freezeFrameMediaTime({ |
| 40 | + clipStart: 0, |
| 41 | + playhead: 2, |
| 42 | + mediaStart: 1, |
| 43 | + playbackRate: 2, |
| 44 | + automation: null, |
| 45 | + }), |
| 46 | + ).toBeCloseTo(5, 6); |
| 47 | + }); |
| 48 | + |
| 49 | + it("integrates a rate lane, which overrides the constant", () => { |
| 50 | + // 1x to 3x over 2s consumes 2 * (3 - 1) / ln 3 source seconds. |
| 51 | + expect( |
| 52 | + freezeFrameMediaTime({ |
| 53 | + clipStart: 0, |
| 54 | + playhead: 2, |
| 55 | + mediaStart: 0.5, |
| 56 | + playbackRate: 4, |
| 57 | + automation: rampLane, |
| 58 | + }), |
| 59 | + ).toBeCloseTo(0.5 + 3.641, 2); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe("freezeExtractArgs", () => { |
| 64 | + it("seeks before the input and writes one frame, never over an existing file", () => { |
| 65 | + expect(freezeExtractArgs("/p/a.mp4", 7.2004, "/p/assets/freeze/a-3200.png")).toEqual([ |
| 66 | + "-n", |
| 67 | + "-ss", |
| 68 | + "7.2", |
| 69 | + "-i", |
| 70 | + "/p/a.mp4", |
| 71 | + "-frames:v", |
| 72 | + "1", |
| 73 | + "/p/assets/freeze/a-3200.png", |
| 74 | + ]); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +const project = `<div data-composition-id="main" data-start="0" data-duration="10"> |
| 79 | +<video id="talk" class="clip" src="talk.mp4" data-start="1" data-duration="6" data-media-start="2" data-track-index="0" data-link="L" style="left:10px;clip-path:inset(5px)"></video> |
| 80 | +<img id="later" class="clip" src="x.png" data-start="8" data-duration="1" data-track-index="0"> |
| 81 | +<div id="other" class="clip" data-start="5" data-duration="2" data-track-index="2"></div> |
| 82 | +<audio id="talk-audio" class="clip" src="talk.mp4" data-start="1" data-duration="6" data-media-start="2" data-track-index="1" data-link="L"></audio> |
| 83 | +</div>`; |
| 84 | + |
| 85 | +const at = (html: string, id: string) => |
| 86 | + parseHTML(`<html><body>${html}</body></html>`).document.getElementById(id); |
| 87 | +const timing = (html: string, id: string) => { |
| 88 | + const el = at(html, id); |
| 89 | + return [el?.getAttribute("data-start"), el?.getAttribute("data-duration")]; |
| 90 | +}; |
| 91 | + |
| 92 | +describe("readFreezeSource", () => { |
| 93 | + it("reads the frame under the playhead from the source attributes", () => { |
| 94 | + expect(readFreezeSource(project, { id: "talk" }, 3.2)).toEqual({ |
| 95 | + id: "talk", |
| 96 | + src: "talk.mp4", |
| 97 | + mediaTime: 4.2, |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it("reads the in-point as playback does: data-playback-start before data-media-start", () => { |
| 102 | + const both = project.replace( |
| 103 | + 'data-media-start="2"', |
| 104 | + 'data-media-start="2" data-playback-start="5"', |
| 105 | + ); |
| 106 | + expect(readFreezeSource(both, { id: "talk" }, 3.2)?.mediaTime).toBe(7.2); |
| 107 | + }); |
| 108 | + |
| 109 | + it("refuses a playhead outside the clip or a non-video", () => { |
| 110 | + expect(readFreezeSource(project, { id: "talk" }, 0.5)).toBeNull(); |
| 111 | + expect(readFreezeSource(project, { id: "later" }, 8.5)).toBeNull(); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +describe("applyFreezeFrameToHtml", () => { |
| 116 | + const result = applyFreezeFrameToHtml(project, { |
| 117 | + target: { id: "talk" }, |
| 118 | + playhead: 3.2, |
| 119 | + imageSrc: "assets/freeze/talk-3200.png", |
| 120 | + }); |
| 121 | + const html = result?.html ?? ""; |
| 122 | + |
| 123 | + it("splits the video and inserts a 2 s still with the video's box styles", () => { |
| 124 | + expect(result?.freezeId).toBe("talk-freeze"); |
| 125 | + expect(timing(html, "talk")).toEqual(["1", "2.2"]); |
| 126 | + const still = at(html, "talk-freeze"); |
| 127 | + expect(still?.tagName).toBe("IMG"); |
| 128 | + expect(timing(html, "talk-freeze")).toEqual(["3.2", "2"]); |
| 129 | + expect(still?.getAttribute("style")).toBe("left:10px;clip-path:inset(5px)"); |
| 130 | + expect(still?.getAttribute("data-track-index")).toBe("0"); |
| 131 | + expect(still?.getAttribute("data-timeline-label")).toBe("Freeze"); |
| 132 | + expect(still?.hasAttribute("data-link")).toBe(false); |
| 133 | + }); |
| 134 | + |
| 135 | + it("moves the right half and the rest of the track right by the hold", () => { |
| 136 | + expect(timing(html, "talk-split")).toEqual(["5.2", "3.8"]); |
| 137 | + expect(at(html, "talk-split")?.getAttribute("data-media-start")).toBe("4.2"); |
| 138 | + expect(timing(html, "later")).toEqual(["10", "1"]); |
| 139 | + expect(timing(html, "other")).toEqual(["5", "2"]); |
| 140 | + }); |
| 141 | + |
| 142 | + it("splits the linked audio, leaving a silent gap, and links the right halves together", () => { |
| 143 | + expect(timing(html, "talk-audio")).toEqual(["1", "2.2"]); |
| 144 | + expect(timing(html, "talk-audio-split")).toEqual(["5.2", "3.8"]); |
| 145 | + expect(at(html, "talk-split")?.getAttribute("data-link")).toBe("L-2"); |
| 146 | + expect(at(html, "talk-audio-split")?.getAttribute("data-link")).toBe("L-2"); |
| 147 | + expect(at(html, "talk-audio")?.getAttribute("data-link")).toBe("L"); |
| 148 | + }); |
| 149 | + |
| 150 | + it("grows the root to fit the pushed clips", () => { |
| 151 | + expect( |
| 152 | + parseHTML(`<html><body>${html}</body></html>`) |
| 153 | + .document.querySelector("[data-composition-id]") |
| 154 | + ?.getAttribute("data-duration"), |
| 155 | + ).toBe("11"); |
| 156 | + }); |
| 157 | + |
| 158 | + it("returns null when the playhead is not inside the video", () => { |
| 159 | + expect( |
| 160 | + applyFreezeFrameToHtml(project, { target: { id: "talk" }, playhead: 9, imageSrc: "x.png" }), |
| 161 | + ).toBeNull(); |
| 162 | + }); |
| 163 | +}); |
| 164 | + |
| 165 | +describe("freezeStillFileName", () => { |
| 166 | + it("reduces a clip id to one safe filename component", () => { |
| 167 | + expect(freezeStillFileName("talk", 2.5, "t0")).toMatch(/^talk-[0-9a-f]{10}-2500-t0\.png$/); |
| 168 | + expect(freezeStillFileName("../../etc/x", 1, "t0")).toMatch( |
| 169 | + /^______etc_x-[0-9a-f]{10}-1000-t0\.png$/, |
| 170 | + ); |
| 171 | + expect(freezeStillFileName("a\\b:c", 1, "t0")).toMatch(/^a_b_c-[0-9a-f]{10}-1000-t0\.png$/); |
| 172 | + expect(freezeStillFileName("", 1, "t0")).toMatch(/^clip-[0-9a-f]{10}-1000-t0\.png$/); |
| 173 | + }); |
| 174 | + |
| 175 | + it("keeps ids that sanitise or truncate alike apart", () => { |
| 176 | + expect(freezeStillFileName("a.b", 2.5, "t0")).not.toBe(freezeStillFileName("a_b", 2.5, "t0")); |
| 177 | + const prefix = "v".repeat(80); |
| 178 | + expect(freezeStillFileName(`${prefix}1`, 2.5, "t0")).not.toBe( |
| 179 | + freezeStillFileName(`${prefix}2`, 2.5, "t0"), |
| 180 | + ); |
| 181 | + }); |
| 182 | + |
| 183 | + it("names every extraction of one clip at one time differently", () => { |
| 184 | + expect(freezeStillFileName("talk", 2.5)).not.toBe(freezeStillFileName("talk", 2.5)); |
| 185 | + }); |
| 186 | +}); |
0 commit comments