|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { |
| 3 | + clampGroupMoveDelta, |
| 4 | + isMultiDragActive, |
| 5 | + isMultiDragPassenger, |
| 6 | + multiDragDeltaSeconds, |
| 7 | + multiDragPassengerOffsetPx, |
| 8 | + type MultiDragPreviewInput, |
| 9 | +} from "./timelineMultiDragPreview"; |
| 10 | + |
| 11 | +const base = (over: Partial<MultiDragPreviewInput> = {}): MultiDragPreviewInput => ({ |
| 12 | + dragStarted: true, |
| 13 | + draggedKey: "a", |
| 14 | + draggedOriginStart: 2, |
| 15 | + draggedPreviewStart: 5, |
| 16 | + selectedKeys: new Set(["a", "b", "c"]), |
| 17 | + ...over, |
| 18 | +}); |
| 19 | + |
| 20 | +describe("isMultiDragActive", () => { |
| 21 | + it("is active when a started drag's clip is part of a 2+ selection", () => { |
| 22 | + expect(isMultiDragActive(base())).toBe(true); |
| 23 | + }); |
| 24 | + |
| 25 | + it("is inactive before the drag starts", () => { |
| 26 | + expect(isMultiDragActive(base({ dragStarted: false }))).toBe(false); |
| 27 | + }); |
| 28 | + |
| 29 | + it("is inactive for a single-clip selection (single-drag behavior)", () => { |
| 30 | + expect(isMultiDragActive(base({ selectedKeys: new Set(["a"]) }))).toBe(false); |
| 31 | + }); |
| 32 | + |
| 33 | + it("is inactive when the dragged clip is not itself selected", () => { |
| 34 | + expect(isMultiDragActive(base({ draggedKey: "z" }))).toBe(false); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe("multiDragDeltaSeconds (the one formation delta)", () => { |
| 39 | + it("is the grabbed clip's preview − origin start when active", () => { |
| 40 | + // The preview start is already group-clamped upstream, so this delta is the |
| 41 | + // clamped delta every member (ghost + passengers) moves by. |
| 42 | + expect(multiDragDeltaSeconds(base())).toBe(3); |
| 43 | + }); |
| 44 | + |
| 45 | + it("supports a leftward (negative) delta", () => { |
| 46 | + expect(multiDragDeltaSeconds(base({ draggedPreviewStart: 0.5 }))).toBeCloseTo(-1.5); |
| 47 | + }); |
| 48 | + |
| 49 | + it("is zero when no multi-drag is active", () => { |
| 50 | + expect(multiDragDeltaSeconds(base({ selectedKeys: new Set(["a"]) }))).toBe(0); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe("isMultiDragPassenger", () => { |
| 55 | + it("marks a selected non-dragged clip as a passenger", () => { |
| 56 | + expect(isMultiDragPassenger("b", base())).toBe(true); |
| 57 | + expect(isMultiDragPassenger("c", base())).toBe(true); |
| 58 | + }); |
| 59 | + |
| 60 | + it("never marks the dragged clip itself (it is the free ghost)", () => { |
| 61 | + expect(isMultiDragPassenger("a", base())).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it("never marks an unselected clip", () => { |
| 65 | + expect(isMultiDragPassenger("d", base())).toBe(false); |
| 66 | + }); |
| 67 | + |
| 68 | + it("marks nothing when the drag is a single-drag", () => { |
| 69 | + const single = base({ selectedKeys: new Set(["a"]) }); |
| 70 | + expect(isMultiDragPassenger("b", single)).toBe(false); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe("multiDragPassengerOffsetPx (rigid: every passenger shares the delta)", () => { |
| 75 | + it("converts the one formation delta to pixels for every passenger", () => { |
| 76 | + // Both passengers move by the SAME 3s × 100pps = 300px — spacing locked. |
| 77 | + expect(multiDragPassengerOffsetPx("b", 100, base())).toBe(300); |
| 78 | + expect(multiDragPassengerOffsetPx("c", 100, base())).toBe(300); |
| 79 | + }); |
| 80 | + |
| 81 | + it("is zero for the dragged clip and for non-passengers", () => { |
| 82 | + expect(multiDragPassengerOffsetPx("a", 100, base())).toBe(0); |
| 83 | + expect(multiDragPassengerOffsetPx("d", 100, base())).toBe(0); |
| 84 | + }); |
| 85 | + |
| 86 | + it("is zero for a non-finite pps", () => { |
| 87 | + expect(multiDragPassengerOffsetPx("b", Number.NaN, base())).toBe(0); |
| 88 | + }); |
| 89 | + |
| 90 | + it("follows a leftward delta", () => { |
| 91 | + expect(multiDragPassengerOffsetPx("c", 50, base({ draggedPreviewStart: 0 }))).toBe(-100); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +describe("clampGroupMoveDelta (rigid group move)", () => { |
| 96 | + it("passes a rightward delta through unchanged (no right wall)", () => { |
| 97 | + expect(clampGroupMoveDelta(3, [2, 5, 9])).toBe(3); |
| 98 | + expect(clampGroupMoveDelta(1000, [0, 4])).toBe(1000); |
| 99 | + }); |
| 100 | + |
| 101 | + it("passes a leftward delta through when no member would cross 0", () => { |
| 102 | + // Leftmost member at 5, moving left by 3 → 2 ≥ 0, so unclamped. |
| 103 | + expect(clampGroupMoveDelta(-3, [5, 8, 12])).toBe(-3); |
| 104 | + }); |
| 105 | + |
| 106 | + it("clamps a leftward delta so the leftmost member stops exactly at 0", () => { |
| 107 | + // Leftmost at 2 → the furthest left the group can move is -2 (that member → 0). |
| 108 | + // A pointer asking for -5 is clamped to -2: the grabbed clip stops with the |
| 109 | + // formation instead of out-running it. |
| 110 | + expect(clampGroupMoveDelta(-5, [2, 6, 10])).toBe(-2); |
| 111 | + }); |
| 112 | + |
| 113 | + it("is bounded by the MOST-constrained (leftmost) member, not the grabbed one", () => { |
| 114 | + // Grabbed clip is at 10; a passenger at 1 is the constraint. Max left = -1. |
| 115 | + expect(clampGroupMoveDelta(-8, [10, 1, 4])).toBe(-1); |
| 116 | + }); |
| 117 | + |
| 118 | + it("already-at-0 member forbids any leftward move", () => { |
| 119 | + expect(clampGroupMoveDelta(-4, [0, 3, 7])).toBe(0); |
| 120 | + // rightward still allowed |
| 121 | + expect(clampGroupMoveDelta(2, [0, 3, 7])).toBe(2); |
| 122 | + }); |
| 123 | + |
| 124 | + it("returns the raw delta for an empty formation", () => { |
| 125 | + expect(clampGroupMoveDelta(-9, [])).toBe(-9); |
| 126 | + }); |
| 127 | +}); |
0 commit comments