|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { findClipForAsset, isPointerClick, DRAG_THRESHOLD_PX } from "./assetClickBehavior"; |
| 3 | +import type { TimelineElement } from "../player/store/playerStore"; |
| 4 | + |
| 5 | +// Minimal TimelineElement factory — only the fields the function inspects. |
| 6 | +function makeEl(overrides: Partial<TimelineElement> & { id: string }): TimelineElement { |
| 7 | + return { |
| 8 | + tag: "div", |
| 9 | + start: 0, |
| 10 | + duration: 5, |
| 11 | + track: 0, |
| 12 | + ...overrides, |
| 13 | + }; |
| 14 | +} |
| 15 | + |
| 16 | +describe("findClipForAsset", () => { |
| 17 | + it("returns null when elements array is empty", () => { |
| 18 | + expect(findClipForAsset([], "assets/foo.mp4")).toBeNull(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("returns null when no element matches", () => { |
| 22 | + const el = makeEl({ id: "el1", src: "assets/other.mp4" }); |
| 23 | + expect(findClipForAsset([el], "assets/foo.mp4")).toBeNull(); |
| 24 | + }); |
| 25 | + |
| 26 | + it("matches a bare relative src against the project-relative asset path", () => { |
| 27 | + const el = makeEl({ id: "el1", src: "assets/clip.mp4", start: 2 }); |
| 28 | + expect(findClipForAsset([el], "assets/clip.mp4")).toBe(el); |
| 29 | + }); |
| 30 | + |
| 31 | + it("matches a src with a ./ prefix", () => { |
| 32 | + const el = makeEl({ id: "el1", src: "./assets/logo.png" }); |
| 33 | + expect(findClipForAsset([el], "assets/logo.png")).toBe(el); |
| 34 | + }); |
| 35 | + |
| 36 | + it("matches a server-relative /api/projects/…/preview/ src", () => { |
| 37 | + const el = makeEl({ id: "el1", src: "/api/projects/demo/preview/assets/bgm.mp3" }); |
| 38 | + expect(findClipForAsset([el], "assets/bgm.mp3")).toBe(el); |
| 39 | + }); |
| 40 | + |
| 41 | + it("matches a fully-absolute URL (as produced by the core runtime)", () => { |
| 42 | + const el = makeEl({ |
| 43 | + id: "el1", |
| 44 | + src: "http://localhost:3012/api/projects/demo/preview/assets/clip.mp4", |
| 45 | + }); |
| 46 | + expect(findClipForAsset([el], "assets/clip.mp4")).toBe(el); |
| 47 | + }); |
| 48 | + |
| 49 | + it("decodes percent-encoded filenames when matching", () => { |
| 50 | + const el = makeEl({ |
| 51 | + id: "el1", |
| 52 | + src: "http://localhost:3012/api/projects/p/preview/assets/my%20file%20(1).mp4", |
| 53 | + }); |
| 54 | + expect(findClipForAsset([el], "assets/my file (1).mp4")).toBe(el); |
| 55 | + }); |
| 56 | + |
| 57 | + it("strips query strings from src before matching", () => { |
| 58 | + const el = makeEl({ id: "el1", src: "assets/clip.mp4?v=2" }); |
| 59 | + expect(findClipForAsset([el], "assets/clip.mp4")).toBe(el); |
| 60 | + }); |
| 61 | + |
| 62 | + it("returns the element with the earliest start when multiple clips match", () => { |
| 63 | + const later = makeEl({ id: "late", src: "assets/clip.mp4", start: 10 }); |
| 64 | + const earlier = makeEl({ id: "early", src: "assets/clip.mp4", start: 2 }); |
| 65 | + const first = makeEl({ id: "first", src: "assets/clip.mp4", start: 0 }); |
| 66 | + expect(findClipForAsset([later, earlier, first], "assets/clip.mp4")).toBe(first); |
| 67 | + }); |
| 68 | + |
| 69 | + it("prefers the key over id when the element has both", () => { |
| 70 | + // findClipForAsset returns the element object itself; callers do `clip.key ?? clip.id` |
| 71 | + const el = makeEl({ id: "el1", key: "clip-key", src: "assets/img.png" }); |
| 72 | + const found = findClipForAsset([el], "assets/img.png"); |
| 73 | + expect(found?.key).toBe("clip-key"); |
| 74 | + }); |
| 75 | + |
| 76 | + it("skips elements with no src", () => { |
| 77 | + const noSrc = makeEl({ id: "nosrc" }); |
| 78 | + const withSrc = makeEl({ id: "withsrc", src: "assets/img.png" }); |
| 79 | + expect(findClipForAsset([noSrc, withSrc], "assets/img.png")).toBe(withSrc); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe("isPointerClick", () => { |
| 84 | + it("returns true for zero movement", () => { |
| 85 | + expect(isPointerClick(0, 0)).toBe(true); |
| 86 | + }); |
| 87 | + |
| 88 | + it("returns true for movement within the threshold", () => { |
| 89 | + expect(isPointerClick(DRAG_THRESHOLD_PX - 1, 0)).toBe(true); |
| 90 | + expect(isPointerClick(0, DRAG_THRESHOLD_PX - 1)).toBe(true); |
| 91 | + expect(isPointerClick(DRAG_THRESHOLD_PX - 1, DRAG_THRESHOLD_PX - 1)).toBe(true); |
| 92 | + }); |
| 93 | + |
| 94 | + it("returns false at or beyond the threshold", () => { |
| 95 | + expect(isPointerClick(DRAG_THRESHOLD_PX, 0)).toBe(false); |
| 96 | + expect(isPointerClick(0, DRAG_THRESHOLD_PX)).toBe(false); |
| 97 | + expect(isPointerClick(DRAG_THRESHOLD_PX + 10, 0)).toBe(false); |
| 98 | + }); |
| 99 | + |
| 100 | + it("handles negative movement (pointer moved left/up)", () => { |
| 101 | + expect(isPointerClick(-(DRAG_THRESHOLD_PX - 1), 0)).toBe(true); |
| 102 | + expect(isPointerClick(-DRAG_THRESHOLD_PX, 0)).toBe(false); |
| 103 | + }); |
| 104 | +}); |
0 commit comments