summaryrefslogtreecommitdiff
path: root/apps/web/src/timeline/controllers/__tests__/drag-drop-controller-transition.test.ts
blob: b24f9ad9baf593405ec8ba1c667b2d2583071d03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { describe, expect, mock, test } from "bun:test";
import type { DragEvent } from "react";
import type { SceneTracks, VideoElement, VideoTrack } from "@/timeline";
import {
	DragDropController,
	type DragDropConfig,
	type DragDropConfigRef,
} from "@/timeline/controllers/drag-drop-controller";
import { TimelineDragSource } from "@/timeline/drag-source";
import { mediaTime, TICKS_PER_SECOND, ZERO_MEDIA_TIME } from "@/wasm";

function buildVideo({
	id,
	start,
}: {
	id: string;
	start: number;
}): VideoElement {
	return {
		id,
		type: "video",
		name: id,
		mediaId: `media-${id}`,
		startTime: mediaTime({ ticks: start }),
		duration: mediaTime({ ticks: TICKS_PER_SECOND }),
		trimStart: ZERO_MEDIA_TIME,
		trimEnd: ZERO_MEDIA_TIME,
		params: { opacity: id === "incoming" ? 0.8 : 1 },
	};
}

function buildTracks(): SceneTracks {
	const main: VideoTrack = {
		id: "visual",
		type: "video",
		name: "Visual",
		elements: [
			buildVideo({ id: "outgoing", start: 0 }),
			buildVideo({ id: "incoming", start: TICKS_PER_SECOND }),
		],
		muted: false,
		hidden: false,
	};
	return { overlay: [], main, audio: [] };
}

function buildDataTransfer(): DataTransfer {
	return {
		types: [],
		files: [],
		dropEffect: "none",
		effectAllowed: "none",
		setData: mock(() => {}),
	} as unknown as DataTransfer;
}

function buildDragEvent({
	clientX,
	dataTransfer,
}: {
	clientX: number;
	dataTransfer: DataTransfer;
}): DragEvent {
	return {
		clientX,
		clientY: 10,
		dataTransfer,
		preventDefault: mock(() => {}),
	} as unknown as DragEvent;
}

function setup() {
	const dragSource = new TimelineDragSource();
	const updateElements = mock<DragDropConfig["updateElements"]>(() => {});
	const insertElement = mock<DragDropConfig["insertElement"]>(() => {});
	const executeCommand = mock<DragDropConfig["executeCommand"]>(() => {});
	const tracksElement = {
		scrollLeft: 0,
		scrollTop: 0,
		getBoundingClientRect: () => ({ left: 0, top: 0 }),
	} as unknown as HTMLDivElement;
	const config: DragDropConfig = {
		zoomLevel: 1,
		getContainerEl: () => tracksElement,
		getHeaderEl: () => null,
		getTracksScrollEl: () => tracksElement,
		getActiveProjectFps: () => null,
		getActiveProjectId: () => null,
		getSceneTracks: buildTracks,
		getCurrentPlayheadTime: () => ZERO_MEDIA_TIME,
		getMediaAssets: () => [],
		dragSource,
		addMediaAsset: () => Promise.resolve(null),
		executeCommand,
		insertElement,
		updateElements,
		addClipEffect: mock(() => {}),
	};
	const configRef: DragDropConfigRef = { current: config };
	return {
		controller: new DragDropController({ configRef }),
		dragSource,
		updateElements,
		insertElement,
		executeCommand,
	};
}

function beginTransitionDrag(
	dragSource: TimelineDragSource,
	dataTransfer: DataTransfer,
) {
	dragSource.begin({
		dataTransfer,
		dragData: {
			type: "transition",
			id: "push-left",
			name: "Slide left",
			transitionKind: "push-left",
			inDuration: 0.45,
			outDuration: 0,
		},
	});
}

describe("DragDropController transition drops", () => {
	test("hovers and applies a transition only to the incoming clip", () => {
		const {
			controller,
			dragSource,
			updateElements,
			insertElement,
			executeCommand,
		} = setup();
		const dataTransfer = buildDataTransfer();
		beginTransitionDrag(dragSource, dataTransfer);

		controller.onDragOver(buildDragEvent({ clientX: 50, dataTransfer }));

		expect(dataTransfer.dropEffect).toBe("copy");
		expect(controller.dropTarget?.targetCut).toEqual({
			trackId: "visual",
			outgoingElementId: "outgoing",
			incomingElementId: "incoming",
			time: mediaTime({ ticks: TICKS_PER_SECOND }),
		});

		controller.onDrop(buildDragEvent({ clientX: 50, dataTransfer }));

		expect(updateElements).toHaveBeenCalledTimes(1);
		expect(updateElements).toHaveBeenCalledWith({
			updates: [
				{
					trackId: "visual",
					elementId: "incoming",
					patch: {
						params: {
							opacity: 0.8,
							"transition.kind": "push-left",
							"transition.inDuration": 0.45,
							"transition.outDuration": 0,
						},
					},
				},
			],
		});
		expect(insertElement).not.toHaveBeenCalled();
		expect(executeCommand).not.toHaveBeenCalled();
	});

	test("does nothing when the transition has no cut target", () => {
		const {
			controller,
			dragSource,
			updateElements,
			insertElement,
			executeCommand,
		} = setup();
		const dataTransfer = buildDataTransfer();
		beginTransitionDrag(dragSource, dataTransfer);

		controller.onDragOver(buildDragEvent({ clientX: 80, dataTransfer }));
		expect(controller.dropTarget).toBeNull();
		controller.onDrop(buildDragEvent({ clientX: 80, dataTransfer }));

		expect(updateElements).not.toHaveBeenCalled();
		expect(insertElement).not.toHaveBeenCalled();
		expect(executeCommand).not.toHaveBeenCalled();
	});
});