|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { ahead, apply, changed, type Clip, empty, evict, joined, keyOf, type Timeline, without } from "../src" |
| 3 | + |
| 4 | +const clip = (id: string, start: number, len: number, more: Partial<Clip> = {}): Clip => ({ id, kind: "video", start, length: len, ...more }) |
| 5 | +const film = (): Timeline => ({ |
| 6 | + ...empty(30), |
| 7 | + magnet: false, |
| 8 | + tracks: [ |
| 9 | + { id: "overlay-1", kind: "overlay", clips: [clip("logo", 3600, 150)] }, |
| 10 | + { id: "main", kind: "main", clips: [clip("a", 0, 3000), clip("b", 3000, 3000)] }, |
| 11 | + { id: "sound-1", kind: "sound", clips: [clip("music", 0, 6000, { kind: "audio" })] }, |
| 12 | + ], |
| 13 | +}) |
| 14 | +const done = (t: Timeline, op: Parameters<typeof apply>[1]) => { |
| 15 | + const r = apply(t, op) |
| 16 | + if (!r.ok) throw new Error(r.why) |
| 17 | + return r.timeline |
| 18 | +} |
| 19 | + |
| 20 | +describe("TL10c: an edit drops only what it changed", () => { |
| 21 | + test("a clip moved from 2:00-2:05 to 2:10-2:15 changes those two ranges and nothing else", () => { |
| 22 | + const t = film() |
| 23 | + expect(changed(t, done(t, { op: "move", ids: ["logo"], by: 300 }))).toEqual([[3600, 3750], [3900, 4050]]) |
| 24 | + }) |
| 25 | + |
| 26 | + test("a key, a hidden track and a transition change the spans they draw; a name, a lock and a marker change nothing", () => { |
| 27 | + const t = film() |
| 28 | + expect(changed(t, done(t, { op: "key", id: "logo", prop: "scale", at: 10, value: 2 }))).toEqual([[3600, 3750]]) |
| 29 | + expect(changed(t, done(t, { op: "track", id: "overlay-1", set: { hidden: true } }))).toEqual([[3600, 3750]]) |
| 30 | + expect(changed(t, done(t, { op: "track", id: "sound-1", set: { muted: true } }))).toEqual([[0, 6000]]) |
| 31 | + expect(changed(t, done(t, { op: "transition", transition: { id: "x", kind: "dissolve", after: "a", length: 30 } }))).toEqual([[2985, 3015]]) |
| 32 | + expect(changed(t, done(t, { op: "set", id: "a", set: { name: "Opening" } }))).toEqual([]) |
| 33 | + expect(changed(t, done(t, { op: "track", id: "main", set: { locked: true, height: 80 } }))).toEqual([]) |
| 34 | + expect(changed(t, done(t, { op: "mark", marker: { id: "m", at: 100 } }))).toEqual([]) |
| 35 | + }) |
| 36 | + |
| 37 | + test("a split of footage draws the same frames: nothing is drawn again; a split of anything else restarts its right half", () => { |
| 38 | + const footage: Timeline = { ...film(), tracks: [{ id: "main", kind: "main", clips: [clip("a", 0, 3000, { in: 0, source: 9000 })] }] } |
| 39 | + expect(changed(footage, done(footage, { op: "split", at: 1000, ids: ["a"] }))).toEqual([]) |
| 40 | + // and one half deleted changes only that half (the magnet off: the other stays put) |
| 41 | + const cut = done(done(footage, { op: "split", at: 1000, ids: ["a"] }), { op: "remove", ids: ["a"] }) |
| 42 | + expect(changed(footage, cut)).toEqual([[0, 1000]]) |
| 43 | + const t = film() |
| 44 | + expect(changed(t, done(t, { op: "split", at: 1000, ids: ["a"] }))).toEqual([[0, 3000]]) |
| 45 | + }) |
| 46 | + |
| 47 | + test("a range's key is the same for two states that draw it the same, and differs when they don't", () => { |
| 48 | + const t = film() |
| 49 | + const moved = done(t, { op: "move", ids: ["logo"], by: 300 }) |
| 50 | + expect(keyOf(moved, [0, 3000])).toBe(keyOf(t, [0, 3000])) |
| 51 | + expect(keyOf(moved, [3600, 3750])).not.toBe(keyOf(t, [3600, 3750])) |
| 52 | + // undone: the frames kept before the edit are found again |
| 53 | + expect(keyOf(done(moved, { op: "move", ids: ["logo"], by: -300 }), [3600, 3750])).toBe(keyOf(t, [3600, 3750])) |
| 54 | + // the same clip over other media is another picture |
| 55 | + expect(keyOf(t, [0, 3000], () => "v2")).not.toBe(keyOf(t, [0, 3000], () => "v1")) |
| 56 | + }) |
| 57 | +}) |
| 58 | + |
| 59 | +describe("TL10d, TL10e: what is drawn next, and what goes first", () => { |
| 60 | + test("ranges join, and one taken out of another leaves the rest", () => { |
| 61 | + expect(joined([[10, 20], [0, 5], [5, 10], [30, 30], [18, 25]])).toEqual([[0, 25]]) |
| 62 | + expect(without([[0, 100]], [[20, 30], [90, 120]])).toEqual([[0, 20], [30, 90]]) |
| 63 | + }) |
| 64 | + |
| 65 | + test("from the playhead on first, nearest first, then what is behind it", () => { |
| 66 | + expect(ahead(300, [[100, 160]], 120, 60)).toEqual([[160, 220], [220, 280], [280, 300], [60, 100], [0, 60]]) |
| 67 | + expect(ahead(300, [[0, 300]], 0, 60)).toEqual([]) |
| 68 | + }) |
| 69 | + |
| 70 | + test("a closed version's frames go at once, then the least recently played, never what is under the playhead while anything else can go", () => { |
| 71 | + const kept = [ |
| 72 | + { key: "old", bytes: 40, used: 1 }, |
| 73 | + { key: "here", bytes: 40, used: 2 }, |
| 74 | + { key: "recent", bytes: 40, used: 9 }, |
| 75 | + { key: "closed", bytes: 10, used: 8, stale: true }, |
| 76 | + ] |
| 77 | + expect(evict(kept, 1000)).toEqual(["closed"]) |
| 78 | + expect(evict(kept, 90, new Set(["here"]))).toEqual(["closed", "old"]) |
| 79 | + expect(evict(kept, 30, new Set(["here"]))).toEqual(["closed", "old", "recent", "here"]) |
| 80 | + }) |
| 81 | +}) |
0 commit comments