summaryrefslogtreecommitdiff
path: root/apps/web/src/lib/animation/__tests__/transform-keyframes.test.ts
blob: 629bd0d6a19a8df1211cdc9e8503f18cdab5720a (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import { describe, expect, test } from "bun:test";
import type { ElementAnimations } from "@/types/animation";
import {
	clampAnimationsToDuration,
	getElementKeyframes,
	getKeyframeAtTime,
	hasKeyframesForPath,
	getChannelValueAtTime,
	getElementLocalTime,
	resolveTransformAtTime,
	splitAnimationsAtTime,
} from "@/lib/animation";

describe("transform keyframe evaluation", () => {
	test("uses fallback value when channel is missing", () => {
		const value = getChannelValueAtTime({
			channel: undefined,
			time: 1,
			fallbackValue: 42,
		});
		expect(value).toBe(42);
	});

	test("returns boundary value when time is within epsilon of first/last keyframe", () => {
		const channel = {
			valueKind: "number" as const,
			keyframes: [
				{ id: "a", time: 0, value: 10, interpolation: "linear" as const },
				{ id: "b", time: 2, value: 30, interpolation: "linear" as const },
			],
		};
		expect(
			getChannelValueAtTime({
				channel,
				time: 0.0008,
				fallbackValue: 0,
			}),
		).toBe(10);
		expect(
			getChannelValueAtTime({
				channel,
				time: 1.9992,
				fallbackValue: 0,
			}),
		).toBe(30);
	});

	test("interpolates linear channels", () => {
		const value = getChannelValueAtTime({
			channel: {
				valueKind: "number",
				keyframes: [
					{ id: "a", time: 0, value: 10, interpolation: "linear" },
					{ id: "b", time: 2, value: 30, interpolation: "linear" },
				],
			},
			time: 1,
			fallbackValue: 0,
		});
		expect(value).toBe(20);
	});

	test("clamps local time to [0, duration]", () => {
		expect(
			getElementLocalTime({
				timelineTime: 2,
				elementStartTime: 5,
				elementDuration: 4,
			}),
		).toBe(0);
		expect(
			getElementLocalTime({
				timelineTime: 12,
				elementStartTime: 5,
				elementDuration: 4,
			}),
		).toBe(4);
		expect(
			getElementLocalTime({
				timelineTime: 7,
				elementStartTime: 5,
				elementDuration: 4,
			}),
		).toBe(2);
	});

	test("uses hold interpolation from the left keyframe", () => {
		const value = getChannelValueAtTime({
			channel: {
				valueKind: "number",
				keyframes: [
					{ id: "a", time: 0, value: 10, interpolation: "hold" },
					{ id: "b", time: 2, value: 30, interpolation: "linear" },
				],
			},
			time: 1,
			fallbackValue: 0,
		});
		expect(value).toBe(10);
	});

	test("resolves transform by mixing animated and fallback properties", () => {
		const animations: ElementAnimations = {
			channels: {
				"transform.position.x": {
					valueKind: "number",
					keyframes: [
						{ id: "a", time: 0, value: 0, interpolation: "linear" },
						{ id: "b", time: 4, value: 80, interpolation: "linear" },
					],
				},
				"transform.scale": {
					valueKind: "number",
					keyframes: [{ id: "c", time: 0, value: 2, interpolation: "hold" }],
				},
			},
		};
		const resolvedTransform = resolveTransformAtTime({
			baseTransform: {
				position: { x: 10, y: 20 },
				scale: 1,
				rotate: 15,
			},
			animations,
			localTime: 2,
		});
		expect(resolvedTransform).toEqual({
			position: { x: 40, y: 20 },
			scale: 2,
			rotate: 15,
		});
	});
});

describe("transform keyframe mutation utilities", () => {
	test("splits channels and rebases right side times", () => {
		const animations: ElementAnimations = {
			channels: {
				"transform.scale": {
					valueKind: "number",
					keyframes: [
						{ id: "a", time: 0, value: 1, interpolation: "linear" },
						{ id: "b", time: 2, value: 2, interpolation: "linear" },
						{ id: "c", time: 6, value: 4, interpolation: "linear" },
					],
				},
			},
		};
		const { leftAnimations, rightAnimations } = splitAnimationsAtTime({
			animations,
			splitTime: 4,
		});

		expect(
			leftAnimations?.channels["transform.scale"]?.keyframes.map(
				(keyframe) => keyframe.time,
			),
		).toEqual([0, 2, 4]);
		expect(
			rightAnimations?.channels["transform.scale"]?.keyframes.map(
				(keyframe) => keyframe.time,
			),
		).toEqual([0, 2]);
		expect(
			rightAnimations?.channels["transform.scale"]?.keyframes[0]?.value,
		).toBe(3);
	});

	test("clamps channels to updated element duration", () => {
		const animations: ElementAnimations = {
			channels: {
				"transform.rotate": {
					valueKind: "number",
					keyframes: [
						{ id: "a", time: 0, value: 0, interpolation: "linear" },
						{ id: "b", time: 2, value: 20, interpolation: "linear" },
						{ id: "c", time: 5, value: 50, interpolation: "linear" },
					],
				},
			},
		};
		const clampedAnimations = clampAnimationsToDuration({
			animations,
			duration: 2,
		});
		expect(
			clampedAnimations?.channels["transform.rotate"]?.keyframes.map(
				(keyframe) => keyframe.time,
			),
		).toEqual([0, 2]);
	});
});

describe("typed channel interpolation", () => {
	test("interpolates color channels from hex keyframes", () => {
		const value = getChannelValueAtTime({
			channel: {
				valueKind: "color",
				keyframes: [
					{ id: "a", time: 0, value: "#000000", interpolation: "linear" },
					{ id: "b", time: 1, value: "#ffffff", interpolation: "linear" },
				],
			},
			time: 0.5,
			fallbackValue: "#000000",
		});
		expect(typeof value).toBe("string");
		expect(value).toContain("rgba(");
	});

	test("uses hold behavior for discrete channels", () => {
		const value = getChannelValueAtTime({
			channel: {
				valueKind: "discrete",
				keyframes: [
					{ id: "a", time: 0, value: "normal", interpolation: "hold" },
					{ id: "b", time: 2, value: "multiply", interpolation: "hold" },
				],
			},
			time: 1.2,
			fallbackValue: "normal",
		});
		expect(value).toBe("normal");
	});
});

describe("keyframe query helpers", () => {
	test("getElementKeyframes returns flat list of all keyframes across channels", () => {
		const animations: ElementAnimations = {
			channels: {
				"transform.position.x": {
					valueKind: "number",
					keyframes: [{ id: "x-1", time: 1, value: 64, interpolation: "linear" }],
				},
				opacity: {
					valueKind: "number",
					keyframes: [{ id: "o-1", time: 0, value: 1, interpolation: "linear" }],
				},
			},
		};

		const keyframes = getElementKeyframes({ animations });
		expect(keyframes).toHaveLength(2);
		expect(keyframes.map((keyframe) => keyframe.propertyPath).sort()).toEqual([
			"opacity",
			"transform.position.x",
		]);
	});

	test("getElementKeyframes returns empty array when animations are missing or channels are empty", () => {
		expect(getElementKeyframes({ animations: undefined })).toEqual([]);
		expect(
			getElementKeyframes({
				animations: {
					channels: { opacity: { valueKind: "number", keyframes: [] } },
				},
			}),
		).toEqual([]);
	});

	test("hasKeyframesForPath returns true only for paths with keyframes", () => {
		const animations: ElementAnimations = {
			channels: {
				"transform.position.x": {
					valueKind: "number",
					keyframes: [{ id: "x-1", time: 1, value: 64, interpolation: "linear" }],
				},
				"transform.position.y": {
					valueKind: "number",
					keyframes: [],
				},
			},
		};

		expect(
			hasKeyframesForPath({ animations, propertyPath: "transform.position.x" }),
		).toBe(true);
		expect(
			hasKeyframesForPath({ animations, propertyPath: "transform.position.y" }),
		).toBe(false);
	});

	test("getKeyframeAtTime finds keyframe within epsilon and returns full object", () => {
		const animations: ElementAnimations = {
			channels: {
				"transform.rotate": {
					valueKind: "number",
					keyframes: [
						{ id: "r-1", time: 1, value: 15, interpolation: "linear" },
						{ id: "r-2", time: 2, value: 30, interpolation: "linear" },
					],
				},
			},
		};

		const found = getKeyframeAtTime({
			animations,
			propertyPath: "transform.rotate",
			time: 1.0008,
		});
		expect(found?.id).toBe("r-1");
		expect(found?.value).toBe(15);
		expect(found?.propertyPath).toBe("transform.rotate");

		expect(
			getKeyframeAtTime({
				animations,
				propertyPath: "transform.rotate",
				time: 1.01,
			}),
		).toBeNull();
	});
});