Skip to content

🎥 Make videos programmatically with React

242 KB
Raw Blame
import {expect, test} from 'bun:test';
import {CanvasInternals} from '@remotion/sdk';
import type {RefObject} from 'react';
import {
	Internals,
	type _InternalTypes,
	type InteractivitySchema,
	type PropStatuses,
	type SequenceNodePath,
	type SequencePropsSubscriptionKey,
	type TSequence,
} from 'remotion';
import {NoReactInternals} from 'remotion/no-react';
import {getInspectorSelectableItems} from '../components/InspectorSequenceSection';
import {getSelectedOutlineControlLayout} from '../components/selected-outline-control-layout';
import type {SelectedOutline} from '../components/selected-outline-geometry';
import {
	cropOutlinePoints,
	getSelectedCropInfo,
	getSelectedTransformOriginInfo,
} from '../components/selected-outline-measurement';
import type {
	SelectedOutlineCropDragTarget,
	SelectedOutlineTarget,
	SelectedOutlineTransformOriginDragTarget,
} from '../components/selected-outline-types';
import {canEditSelectedOutlineCrop} from '../components/selected-outline-types';
import {
	constrainUv,
	getSelectedUvHandles,
	getUvCoordinateForPoint,
	getUvEllipseInteractiveControls,
	getUvHandleConnectionEllipses,
	getUvHandleConnectionLines,
	getUvHandlePosition,
	roundUvCoordinate,
	type SelectedOutlineUvHandle,
} from '../components/selected-outline-uv';
import {
	applySelectedOutlineTransformOriginAxisLock,
	compensateTranslateForTransformOrigin,
	getOutlineSelectionInteraction,
	getSelectedEffectFieldsBySequenceKey,
	getSelectedOutline3DRotationDragValues,
	getSelectedOutlineActiveSchema,
	getSelectedOutlineCropDragChanges,
	getSelectedOutlineCropDragValues,
	getSelectedOutlineCropFollowingTransformOrigin,
	getSelectedOutlineRotationCornerInfo,
	getSelectedOutlineRotationDeltaDegrees,
	getSelectedOutlineRotationDragChanges,
	getSelectedOutlineRotationDragValues,
	getSelectedOutlineRotationPivot,
	getSelectedOutlineScaleDragChanges,
	getSelectedOutlineScaleDragValues,
	getSelectedOutlineScaleEdgeInfo,
	getSelectedOutlineTransformOriginDragChanges,
	getSelectedOutlineTransformOriginLockedAxis,
	getSelectedSequenceKeys,
	getTransformedSvgViewportPoints,
	orderOutlinesForRendering,
	selectedOutlineTransformOriginSnapThresholdPx,
	selectedOutlineUvSnapThresholdPx,
	snapSelectedOutlineRotationDeltaDegrees,
	snapSelectedOutlineTransformOriginUv,
	snapSelectedOutlineUv,
	type SelectedOutlineRotationDragState,
	type SelectedOutlineScaleDragState,
} from '../components/SelectedOutlineOverlay';
import {getSelectedOutlineUvHandleTimelineSelection} from '../components/SelectedOutlineUvControls';
import {
	deleteSelectedTimelineItems,
	getTimelineSelectionAfterDeletingItems,
} from '../components/Timeline/delete-selected-timeline-item';
import {
	isDuplicatableEffectSelection,
	isDuplicatableSequenceRowSelection,
} from '../components/Timeline/duplicate-selected-timeline-item';
import {getTimelineSequenceTimingLimits} from '../components/Timeline/get-timeline-sequence-timing-limits';
import {
	getKeyframeClipboardDataFromSelections,
	getPasteKeyframeTarget,
	type PasteKeyframeTarget,
} from '../components/Timeline/keyframe-clipboard';
import {getTimelinePropResetTargets} from '../components/Timeline/reset-selected-timeline-props';
import {
	getPasteSequencePropTarget,
	getSequencePropClipboardDataFromSelection,
} from '../components/Timeline/sequence-prop-clipboard';
import {shouldSubscribeToSequenceProps} from '../components/Timeline/should-subscribe-to-sequence-props';
import {getTimelineSequenceSplitEligibility} from '../components/Timeline/split-selected-timeline-item';
import {parseCssRotationToEuler} from '../components/Timeline/timeline-rotation-utils';
import {
	getEasingClipboardDataFromSelection,
	getEffectPropClipboardDataFromSelection,
	getEffectsClipboardEnvelopeFromSelections,
	getPasteEffectPropTarget,
	getPasteEffectsTarget,
	getSnapshotsFromSelection,
	type PasteEffectPropTarget,
	type PasteEffectsTarget,
} from '../components/Timeline/TimelineClipboardKeybindings';
import {getSelectedKeyframeControlNodePathInfos} from '../components/Timeline/TimelineKeyframeControls';
import {
	extendTimelineMarqueeSelection,
	getAvailableTimelineSelectionState,
	getClampedTimelineMarqueePoint,
	getSelectableTimelineItems,
	getSelectableTimelineSequenceSelections,
	getTimelineMarqueeSelection,
	getTimelineRowHighlightBackground,
	getTimelineSelectionAfterInteraction,
	getTimelineSelectionFromNodePathInfo,
	getTimelineSelectionKey,
	getTimelineSequenceSelectionForEscape,
	getTimelineSequenceSelectionKey,
	isTimelineSelectionModifierEvent,
	shouldSelectTimelineRowOnPointerDown,
	TIMELINE_BACKGROUND,
	TIMELINE_EXPANDED_SELECTED_BACKGROUND,
	TIMELINE_HOVER_BACKGROUND,
	TIMELINE_SELECTED_BACKGROUND,
	TIMELINE_TICKS_BACKGROUND,
	timelineMarqueeRectsIntersect,
	type TimelineSelection,
} from '../components/Timeline/TimelineSelection';
import {
	getTimelineSequenceDurationDragChanges,
	getTimelineSequenceDurationDragTargets,
	getTimelineSequenceDurationDragValue,
	getTimelineSequenceMediaDurationDragLimits,
	getTimelineSequenceFromDragChanges,
	getTimelineSequenceFromDragDelta,
	getTimelineSequenceFromDragKeyframeMoves,
	getTimelineSequenceFromDragTargets,
	getTimelineSequenceFromDragValue,
	getTimelineSequenceLeftEdgeDragChanges,
	getTimelineSequenceLeftEdgeDragTargets,
	getTimelineSequenceLeftEdgeDragValues,
	isCascadingSequence,
	isTimelineSequenceDurationDraggable,
	isTimelineSequenceLeftEdgeDraggable,
	timelineSequenceFromDragSnapThresholdPx,
} from '../components/Timeline/TimelineSequenceRightEdgeDragHandle';
import {
	parsedTransformOriginToUv,
	parseTransformOrigin,
	serializeTransformOrigin,
} from '../components/Timeline/transform-origin-utils';
import {
	getKeyframesForTimelineEasingDrag,
	getTimelineSelectionsAfterEasingKeyframeDrag,
} from '../components/Timeline/use-timeline-keyframe-drag';
import {
	getConnectedCompositionFrame,
	getSequenceDoubleClickAction,
} from '../helpers/get-sequence-double-click-action';
import type {SequenceNodePathInfo} from '../helpers/get-timeline-sequence-sort-key';
import {
	loadEditorShowOutlinesOption,
	persistEditorShowOutlinesOption,
} from '../state/editor-outlines';
import {makeRuntimeValueStore} from './make-runtime-value-store';

const makeKey = (
	nodePath: SequenceNodePath,
	effectKeys: string[][] = [],
): SequencePropsSubscriptionKey => ({
	absolutePath: '/project/src/Comp.tsx',
	nodePath,
	sequenceKeys: ['from', 'durationInFrames'],
	effectKeys,
	videoConfigValues: null,
});

const withMockLocalStorage = (callback: () => void) => {
	const previousDescriptor = Object.getOwnPropertyDescriptor(
		globalThis,
		'localStorage',
	);
	const values = new Map<string, string>();
	const localStorageMock: Storage = {
		clear: () => values.clear(),
		getItem: (key: string) => values.get(key) ?? null,
		key: (index: number) => [...values.keys()][index] ?? null,
		removeItem: (key: string) => values.delete(key),
		setItem: (key: string, value: string) => values.set(key, value),
		get length() {
			return values.size;
		},
	};

	Object.defineProperty(globalThis, 'localStorage', {
		configurable: true,
		value: localStorageMock,
	});

	try {
		callback();
	} finally {
		if (previousDescriptor) {
			Object.defineProperty(globalThis, 'localStorage', previousDescriptor);
		} else {
			Reflect.deleteProperty(globalThis, 'localStorage');
		}
	}
};

const makeNodePathInfo = (
	nodePath: SequenceNodePath,
	auxiliaryKeys: string[],
	supportsEffects = true,
	effectKeys: string[][] = [],
): SequenceNodePathInfo => ({
	sequenceSubscriptionKey: makeKey(nodePath, effectKeys),
	auxiliaryKeys,
	index: 0,
	numberOfSequencesWithThisNodePath: 1,
	supportsEffects,
});

const makeTimelineSequence = ({
	schema,
	effects = [],
	id = 'sequence',
	overrideId = 'override',
	parentId = null,
	refForOutline = null,
	duration = 100,
	from = 0,
	premountDisplay = null,
	postmountDisplay = null,
	componentIdentity = null,
	currentRuntimeValueDotNotation = {},
	isInsideSeries = false,
	startMediaFrom = 0,
	trimBefore = null,
	type = 'sequence',
	showInTimeline = true,
	singleChildComponent,
	timelineOrder = null,
	sequencePlaybackRate = 1,
}: {
	readonly schema: InteractivitySchema;
	readonly effects?: readonly {readonly schema: InteractivitySchema}[];
	readonly id?: string;
	readonly overrideId?: string;
	readonly parentId?: string | null;
	readonly refForOutline?: RefObject<HTMLElement | null> | null;
	readonly duration?: number;
	readonly from?: number;
	readonly premountDisplay?: number | null;
	readonly postmountDisplay?: number | null;
	readonly componentIdentity?: string | null;
	readonly currentRuntimeValueDotNotation?: Record<string, unknown>;
	readonly isInsideSeries?: boolean;
	readonly startMediaFrom?: number;
	readonly trimBefore?: number | null;
	readonly type?: TSequence['type'];
	readonly showInTimeline?: boolean;
	readonly singleChildComponent?: unknown;
	readonly timelineOrder?: number | null;
	readonly sequencePlaybackRate?: number;
}): TSequence =>
	({
		type,
		from,
		trimBefore,
		duration,
		id,
		displayName: id,
		documentationLink: null,
		parent: parentId,
		showInTimeline,
		timelineOrder,
		singleChildComponent,
		loopDisplay: undefined,
		getStack: () => null,
		premountDisplay,
		postmountDisplay,
		controls: {
			schema,
			runtimeValues: makeRuntimeValueStore(currentRuntimeValueDotNotation),
			overrideId,
			supportsEffects: true,
			componentIdentity,
			componentName: '<Sequence>',
		},
		refForOutline,
		isInsideSeries,
		effects,
		effectRuntimeValues: null,
		sequencePlaybackRate,
		frozenFrame: null,
		startMediaFrom,
	}) as TSequence;

const makeDurationPropStatuses = (
	nodePaths: readonly SequencePropsSubscriptionKey[],
): PropStatuses => {
	const propStatuses: PropStatuses = {};
	for (const nodePath of nodePaths) {
		propStatuses[Internals.makeSequencePropsSubscriptionKey(nodePath)] = {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				durationInFrames: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: 100,
				},
			},
			effects: [],
		};
	}

	return propStatuses;
};

const makeFromPropStatuses = (
	nodePaths: readonly SequencePropsSubscriptionKey[],
): PropStatuses => {
	const propStatuses: PropStatuses = {};
	for (const nodePath of nodePaths) {
		propStatuses[Internals.makeSequencePropsSubscriptionKey(nodePath)] = {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				from: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: 0,
				},
			},
			effects: [],
		};
	}

	return propStatuses;
};

const makeLeftEdgePropStatuses = (
	nodePaths: readonly SequencePropsSubscriptionKey[],
	includeTrimBefore = false,
	includeTimelineRange = true,
	durationInFramesCodeValues: readonly (number | undefined)[] | null = null,
): PropStatuses => {
	const propStatuses: PropStatuses = {};
	for (const [index, nodePath] of nodePaths.entries()) {
		propStatuses[Internals.makeSequencePropsSubscriptionKey(nodePath)] = {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				...(includeTimelineRange
					? {
							durationInFrames: {
								status: 'static' as const,
								keyframeDisplayOffsetAdjustment: null,
								codeValue: durationInFramesCodeValues?.[index],
							},
							from: {
								status: 'static' as const,
								keyframeDisplayOffsetAdjustment: null,
								codeValue: 0,
							},
						}
					: {}),
				...(includeTrimBefore
					? {
							trimBefore: {
								status: 'static' as const,
								keyframeDisplayOffsetAdjustment: null,
								codeValue: 0,
							},
						}
					: {}),
			},
			effects: [],
		};
	}

	return propStatuses;
};

test('timeline marquee rectangle intersection detects overlapping targets', () => {
	expect(
		timelineMarqueeRectsIntersect(
			{left: 0, top: 0, right: 10, bottom: 10},
			{left: 5, top: 5, right: 15, bottom: 15},
		),
	).toBe(true);
	expect(
		timelineMarqueeRectsIntersect(
			{left: 0, top: 0, right: 10, bottom: 10},
			{left: 11, top: 0, right: 20, bottom: 10},
		),
	).toBe(false);
});

test('Timeline skips prop subscriptions for hidden timeline sequences', () => {
	const schema = {} satisfies InteractivitySchema;
	const visibleSequence = {
		...makeTimelineSequence({schema}),
		getStack: () => 'stack',
	};
	const hiddenSequence = {
		...visibleSequence,
		showInTimeline: false,
	};

	expect(shouldSubscribeToSequenceProps(visibleSequence, true)).toBe(true);
	expect(shouldSubscribeToSequenceProps(hiddenSequence, true)).toBe(false);
});

test('timeline marquee points are clamped to the track bounds', () => {
	expect(
		getClampedTimelineMarqueePoint({
			bounds: {left: 10, top: 20, right: 100, bottom: 200},
			x: 5,
			y: 250,
		}),
	).toEqual({x: 10, y: 200});
	expect(
		getClampedTimelineMarqueePoint({
			bounds: {left: 10, top: 20, right: 100, bottom: 200},
			x: 80,
			y: 60,
		}),
	).toEqual({x: 80, y: 60});
});

test('timeline marquee locks to sequences after capturing a sequence first', () => {
	const firstSequence = makeNodePathInfo(['body', 0], []);
	const keyframe = makeNodePathInfo(['body', 1], ['controls', 'opacity']);
	const secondSequence = makeNodePathInfo(['body', 2], []);

	const result = getTimelineMarqueeSelection({
		lockedSelectionKind: null,
		marqueeRect: {left: 0, top: 0, right: 100, bottom: 100},
		candidates: [
			{
				item: {type: 'sequence', nodePathInfo: firstSequence},
				rect: {left: 0, top: 0, right: 10, bottom: 10},
			},
			{
				item: {type: 'keyframe', nodePathInfo: keyframe, frame: 20},
				rect: {left: 20, top: 0, right: 30, bottom: 10},
			},
			{
				item: {type: 'sequence', nodePathInfo: secondSequence},
				rect: {left: 40, top: 0, right: 50, bottom: 10},
			},
		],
	});

	expect(result.lockedSelectionKind).toBe('sequence');
	expect(result.selectedItems).toEqual([
		{type: 'sequence', nodePathInfo: firstSequence},
		{type: 'sequence', nodePathInfo: secondSequence},
	]);
});

test('timeline marquee locks to keyframes and easings after capturing a keyframe first', () => {
	const keyframe = makeNodePathInfo(['body', 0], ['controls', 'opacity']);
	const sequence = makeNodePathInfo(['body', 1], []);

	const result = getTimelineMarqueeSelection({
		lockedSelectionKind: null,
		marqueeRect: {left: 0, top: 0, right: 100, bottom: 100},
		candidates: [
			{
				item: {type: 'keyframe', nodePathInfo: keyframe, frame: 20},
				rect: {left: 0, top: 0, right: 10, bottom: 10},
			},
			{
				item: {type: 'sequence', nodePathInfo: sequence},
				rect: {left: 20, top: 0, right: 30, bottom: 10},
			},
			{
				item: {
					type: 'easing',
					nodePathInfo: keyframe,
					fromFrame: 20,
					toFrame: 30,
					segmentIndex: 0,
				},
				rect: {left: 40, top: 0, right: 50, bottom: 10},
			},
			{
				item: {type: 'keyframe', nodePathInfo: keyframe, frame: 30},
				rect: {left: 60, top: 0, right: 70, bottom: 10},
			},
		],
	});

	expect(result.lockedSelectionKind).toBe('keyframes-and-easings');
	expect(result.selectedItems).toEqual([
		{type: 'keyframe', nodePathInfo: keyframe, frame: 20},
		{
			type: 'easing',
			nodePathInfo: keyframe,
			fromFrame: 20,
			toFrame: 30,
			segmentIndex: 0,
		},
		{type: 'keyframe', nodePathInfo: keyframe, frame: 30},
	]);
});

test('timeline marquee selects easing only when both endpoint keyframes intersect', () => {
	const nodePathInfo = makeNodePathInfo(['body', 0], ['controls', 'opacity']);
	const easing = {
		type: 'easing' as const,
		nodePathInfo,
		fromFrame: 20,
		toFrame: 30,
		segmentIndex: 0,
	};
	const candidates = [
		{
			item: {type: 'keyframe' as const, nodePathInfo, frame: 20},
			rect: {left: 0, top: 0, right: 10, bottom: 10},
		},
		{
			item: easing,
			rect: {left: 20, top: 0, right: 80, bottom: 10},
		},
		{
			item: {type: 'keyframe' as const, nodePathInfo, frame: 30},
			rect: {left: 90, top: 0, right: 100, bottom: 10},
		},
	];

	expect(
		getTimelineMarqueeSelection({
			lockedSelectionKind: null,
			marqueeRect: {left: 20, top: 0, right: 80, bottom: 10},
			candidates,
		}),
	).toEqual({lockedSelectionKind: null, selectedItems: []});
	expect(
		getTimelineMarqueeSelection({
			lockedSelectionKind: null,
			marqueeRect: {left: 0, top: 0, right: 100, bottom: 10},
			candidates,
		}),
	).toEqual({
		lockedSelectionKind: 'keyframes-and-easings',
		selectedItems: [candidates[0].item, easing, candidates[2].item],
	});
});

test('timeline marquee keeps its locked item kind while dragging', () => {
	const keyframe = makeNodePathInfo(['body', 0], ['controls', 'opacity']);
	const sequence = makeNodePathInfo(['body', 1], []);

	const result = getTimelineMarqueeSelection({
		lockedSelectionKind: 'keyframes-and-easings',
		marqueeRect: {left: 0, top: 0, right: 100, bottom: 100},
		candidates: [
			{
				item: {type: 'sequence', nodePathInfo: sequence},
				rect: {left: 0, top: 0, right: 10, bottom: 10},
			},
			{
				item: {type: 'keyframe', nodePathInfo: keyframe, frame: 20},
				rect: {left: 20, top: 0, right: 30, bottom: 10},
			},
		],
	});

	expect(result.lockedSelectionKind).toBe('keyframes-and-easings');
	expect(result.selectedItems).toEqual([
		{type: 'keyframe', nodePathInfo: keyframe, frame: 20},
	]);
});

test('timeline marquee can choose a new item kind after the locked kind selects nothing', () => {
	const keyframe = makeNodePathInfo(['body', 0], ['controls', 'opacity']);
	const sequence = makeNodePathInfo(['body', 1], []);

	const result = getTimelineMarqueeSelection({
		lockedSelectionKind: 'sequence',
		marqueeRect: {left: 0, top: 0, right: 100, bottom: 100},
		candidates: [
			{
				item: {type: 'keyframe', nodePathInfo: keyframe, frame: 20},
				rect: {left: 20, top: 0, right: 30, bottom: 10},
			},
			{
				item: {type: 'sequence', nodePathInfo: sequence},
				rect: {left: 120, top: 0, right: 130, bottom: 10},
			},
		],
	});

	expect(result.lockedSelectionKind).toBe('keyframes-and-easings');
	expect(result.selectedItems).toEqual([
		{type: 'keyframe', nodePathInfo: keyframe, frame: 20},
	]);
});

test('timeline marquee clears its item kind when no target is selected', () => {
	const sequence = makeNodePathInfo(['body', 0], []);

	const result = getTimelineMarqueeSelection({
		lockedSelectionKind: 'sequence',
		marqueeRect: {left: 0, top: 0, right: 100, bottom: 100},
		candidates: [
			{
				item: {type: 'sequence', nodePathInfo: sequence},
				rect: {left: 120, top: 0, right: 130, bottom: 10},
			},
		],
	});

	expect(result.lockedSelectionKind).toBe(null);
	expect(result.selectedItems).toEqual([]);
});

test('timeline marquee can extend a keyframe selection', () => {
	const nodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'style.opacity'],
	);
	const selectedKeyframe = {
		type: 'keyframe' as const,
		nodePathInfo,
		frame: 10,
	};
	const newlySelectedKeyframe = {
		type: 'keyframe' as const,
		nodePathInfo,
		frame: 20,
	};

	expect(
		extendTimelineMarqueeSelection({
			currentSelection: [selectedKeyframe],
			marqueeSelection: [newlySelectedKeyframe],
		}),
	).toEqual([selectedKeyframe, newlySelectedKeyframe]);
});

test('extending a timeline marquee preserves the selection until it intersects an item', () => {
	const nodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'style.opacity'],
	);
	const selectedKeyframe = {
		type: 'keyframe' as const,
		nodePathInfo,
		frame: 10,
	};

	expect(
		extendTimelineMarqueeSelection({
			currentSelection: [selectedKeyframe],
			marqueeSelection: [],
		}),
	).toEqual([selectedKeyframe]);
});

test('extending a timeline marquee removes duplicate selections', () => {
	const nodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'style.opacity'],
	);
	const selectedKeyframe = {
		type: 'keyframe' as const,
		nodePathInfo,
		frame: 10,
	};

	expect(
		extendTimelineMarqueeSelection({
			currentSelection: [selectedKeyframe],
			marqueeSelection: [selectedKeyframe],
		}),
	).toEqual([selectedKeyframe]);
});

test('keyframe diamond target resolution uses all selected prop rows when clicked row is selected', () => {
	const opacity = makeNodePathInfo(['body', 0], ['controls', 'style.opacity']);
	const rotate = makeNodePathInfo(['body', 0], ['controls', 'style.rotate']);

	expect(
		getSelectedKeyframeControlNodePathInfos({
			clickedNodePathInfo: opacity,
			selectedItems: [
				{type: 'sequence-prop', nodePathInfo: opacity, key: 'style.opacity'},
				{type: 'sequence-prop', nodePathInfo: rotate, key: 'style.rotate'},
			],
		}),
	).toEqual([opacity, rotate]);
});

test('keyframe diamond target resolution falls back to clicked row when it is not selected', () => {
	const opacity = makeNodePathInfo(['body', 0], ['controls', 'style.opacity']);
	const rotate = makeNodePathInfo(['body', 0], ['controls', 'style.rotate']);

	expect(
		getSelectedKeyframeControlNodePathInfos({
			clickedNodePathInfo: opacity,
			selectedItems: [
				{type: 'sequence-prop', nodePathInfo: rotate, key: 'style.rotate'},
			],
		}),
	).toEqual([opacity]);
});

test('pasting effects is blocked for sequences that do not support effects', () => {
	const unsupportedSequenceNodePathInfo = makeNodePathInfo(
		['body', 0],
		[],
		false,
	);
	const supportedSequenceNodePathInfo = makeNodePathInfo(['body', 1], [], true);

	expect(
		getPasteEffectsTarget([
			{type: 'sequence', nodePathInfo: unsupportedSequenceNodePathInfo},
		]),
	).toEqual({
		type: 'unsupported',
	} satisfies PasteEffectsTarget);
	expect(
		getPasteEffectsTarget([
			{type: 'sequence', nodePathInfo: supportedSequenceNodePathInfo},
		]),
	).toEqual({
		type: 'valid',
		nodePathInfo: supportedSequenceNodePathInfo,
	} satisfies PasteEffectsTarget);
});

test('pasting effects treats effect selections on one sequence as one target', () => {
	const firstEffectNodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '0'],
		true,
		[['0']],
	);
	const secondEffectNodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '1'],
		true,
		[['1']],
	);

	expect(
		getPasteEffectsTarget([
			{type: 'sequence-effect', nodePathInfo: firstEffectNodePathInfo, i: 0},
			{type: 'sequence-effect', nodePathInfo: secondEffectNodePathInfo, i: 1},
		]),
	).toEqual({
		type: 'valid',
		nodePathInfo: firstEffectNodePathInfo,
	} satisfies PasteEffectsTarget);
});

test('copying a keyframed effect creates a structured snapshot', () => {
	const effectNodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '0'],
		true,
		[['0']],
	);
	const nodePath = effectNodePathInfo.sequenceSubscriptionKey;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {},
			effects: [
				{
					canUpdate: true,
					callee: 'effect',
					importPath: '@remotion/effect',
					effectIndex: 0,
					props: {
						intensity: {
							status: 'keyframed',
							keyframeDisplayOffsetAdjustment: null,
							interpolationFunction: 'interpolate',
							keyframes: [
								{frame: 0, value: 10},
								{frame: 100, value: 20},
							],
							easing: [{type: 'linear'}],
							clamping: {left: 'clamp', right: 'clamp'},
							posterize: undefined,
							output: undefined,
						},
					},
				},
			],
		},
	} satisfies PropStatuses;

	expect(
		getSnapshotsFromSelection({
			selection: {
				type: 'sequence-effect',
				nodePathInfo: effectNodePathInfo,
				i: 0,
			},
			propStatuses,
		}),
	).toEqual([
		{
			callee: 'effect',
			importPath: '@remotion/effect',
			params: {
				intensity: {
					type: 'keyframed',
					interpolationFunction: 'interpolate',
					keyframes: [
						{frame: 0, value: 10},
						{frame: 100, value: 20},
					],
					easing: [{type: 'linear'}],
					clamping: {left: 'clamp', right: 'clamp'},
				},
			},
		},
	]);
});

test('cut effect clipboard metadata preserves source order', () => {
	const nodePathInfo = makeNodePathInfo(['body', 0], ['effects', '0'], true, [
		['0'],
	]);
	const blur = {
		callee: 'blur',
		importPath: '@remotion/effects/blur',
		params: {},
	};
	const brightness = {
		callee: 'brightness',
		importPath: '@remotion/effects/brightness',
		params: {},
	};
	const envelope = getEffectsClipboardEnvelopeFromSelections({
		selectedItems: [
			{type: 'sequence-effect', nodePathInfo, i: 1},
			{type: 'sequence-effect', nodePathInfo, i: 0},
		],
		payload: {
			type: 'effects-additive',
			version: 3,
			remotionClipboard: 'effects',
			effects: [blur, brightness],
		},
	});

	expect(envelope.sourceIdentity).not.toBe(null);
	expect(envelope.originalEffectIndices).toEqual([0, 1]);
	expect(envelope.payload.effects).toEqual([brightness, blur]);
});

test('copying a selected effect prop creates an effect prop payload', () => {
	const effectPropNodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '0', 'intensity'],
		true,
		[['0', 'intensity']],
	);
	const nodePath = effectPropNodePathInfo.sequenceSubscriptionKey;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {},
			effects: [
				{
					canUpdate: true,
					callee: 'halftone',
					importPath: '@remotion/effects/halftone',
					effectIndex: 0,
					props: {
						intensity: {
							status: 'keyframed',
							keyframeDisplayOffsetAdjustment: null,
							interpolationFunction: 'interpolate',
							keyframes: [
								{frame: 0, value: 10},
								{frame: 100, value: 20},
							],
							easing: [{type: 'linear'}],
							clamping: {left: 'clamp', right: 'clamp'},
							posterize: undefined,
							output: undefined,
						},
					},
				},
			],
		},
	} satisfies PropStatuses;

	expect(
		getEffectPropClipboardDataFromSelection({
			selection: {
				type: 'sequence-effect-prop',
				nodePathInfo: effectPropNodePathInfo,
				i: 0,
				key: 'intensity',
			},
			propStatuses,
		}),
	).toEqual({
		type: 'effect-prop',
		version: 1,
		remotionClipboard: 'effect-prop',
		effect: {
			callee: 'halftone',
			importPath: '@remotion/effects/halftone',
		},
		key: 'intensity',
		param: {
			type: 'keyframed',
			interpolationFunction: 'interpolate',
			keyframes: [
				{frame: 0, value: 10},
				{frame: 100, value: 20},
			],
			easing: [{type: 'linear'}],
			clamping: {left: 'clamp', right: 'clamp'},
		},
	});
});

test('copying selected keyframes preserves their frame deltas', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const nodePathInfo = makeNodePathInfo(['body', 0], ['controls', 'opacity']);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const schema = {
		opacity: {type: 'number', default: 1, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				opacity: {
					status: 'keyframed',
					keyframeDisplayOffsetAdjustment: null,
					interpolationFunction: 'interpolate',
					keyframes: [
						{frame: 10, value: 0.4},
						{frame: 30, value: 0.8},
					],
					easing: [],
					clamping: {left: 'clamp', right: 'clamp'},
					posterize: undefined,
					output: undefined,
				},
			},
			effects: [],
		},
	} satisfies PropStatuses;

	expect(
		getKeyframeClipboardDataFromSelections({
			selections: [
				{type: 'keyframe', nodePathInfo, frame: 30},
				{type: 'keyframe', nodePathInfo, frame: 10},
				{
					type: 'easing',
					nodePathInfo,
					fromFrame: 10,
					toFrame: 30,
					segmentIndex: 0,
				},
			],
			tracks: calculate({
				sequences: [makeTimelineSequence({schema, from: 20})],
				overrideIdsToNodePaths: {override: nodePath},
			}).tracks,
			propStatuses,
		}),
	).toEqual({
		type: 'keyframe',
		version: 1,
		remotionClipboard: 'keyframe',
		fieldType: 'number',
		field: {type: 'sequence', fieldKey: 'opacity'},
		keyframes: [
			{frameOffset: 0, value: 0.4},
			{frameOffset: 20, value: 0.8},
		],
		easing: [{type: 'linear'}],
	});
});

test('pasting keyframes onto a sequence targets the copied property', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const nodePathInfo = makeNodePathInfo(['body', 0], []);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const schema = {
		'style.translate': {
			type: 'translate',
			default: 'none',
		},
	} satisfies InteractivitySchema;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				'style.translate': {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: 'none',
				},
			},
			effects: [],
		},
	} satisfies PropStatuses;

	expect(
		getPasteKeyframeTarget({
			selectedItems: [{type: 'sequence', nodePathInfo}],
			payload: {
				type: 'keyframe',
				version: 1,
				remotionClipboard: 'keyframe',
				fieldType: 'translate',
				field: {type: 'sequence', fieldKey: 'style.translate'},
				keyframes: [
					{frameOffset: 0, value: '0px 0px'},
					{frameOffset: 20, value: '100px 0px'},
				],
				easing: [{type: 'linear'}],
			},
			timelinePosition: 50,
			tracks: calculate({
				sequences: [makeTimelineSequence({schema})],
				overrideIdsToNodePaths: {override: nodePath},
			}).tracks,
			propStatuses,
		}),
	).toMatchObject({
		type: 'valid',
		nodePath,
		fieldKey: 'style.translate',
		effectIndex: null,
		keyframes: [
			{sourceFrame: 50, value: '0px 0px'},
			{sourceFrame: 70, value: '100px 0px'},
		],
	});
});

test('copies a keyframed sequence prop between components with matching schemas', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const sourceNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'style.rotate'],
	);
	const targetNodePathInfo = makeNodePathInfo(['body', 1], []);
	const sourceNodePath = sourceNodePathInfo.sequenceSubscriptionKey;
	const targetNodePath = targetNodePathInfo.sequenceSubscriptionKey;
	const schema = {
		'style.rotate': {
			type: 'rotation-css',
			default: '0deg',
		},
	} satisfies InteractivitySchema;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(sourceNodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				'style.rotate': {
					status: 'keyframed',
					keyframeDisplayOffsetAdjustment: null,
					interpolationFunction: 'interpolate',
					keyframes: [
						{frame: 0, value: '0deg'},
						{frame: 30, value: '90deg'},
					],
					easing: [{type: 'linear'}],
					clamping: {left: 'extend', right: 'extend'},
					posterize: undefined,
					output: undefined,
				},
			},
			effects: [],
		},
		[Internals.makeSequencePropsSubscriptionKey(targetNodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				'style.rotate': {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: undefined,
				},
			},
			effects: [],
		},
	} satisfies PropStatuses;
	const sequences = [
		makeTimelineSequence({schema, overrideId: 'video'}),
		makeTimelineSequence({schema, overrideId: 'absolute-fill'}),
	];
	const overrideIdsToNodePaths = {
		video: sourceNodePath,
		'absolute-fill': targetNodePath,
	};
	const payload = getSequencePropClipboardDataFromSelection({
		selection: {
			type: 'sequence-prop',
			nodePathInfo: sourceNodePathInfo,
			key: 'style.rotate',
		},
		propStatuses,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths,
		}).tracks,
	});

	expect(payload).toMatchObject({
		type: 'sequence-prop',
		key: 'style.rotate',
		fieldType: 'rotation-css',
		param: {type: 'keyframed'},
	});
	if (payload === null) {
		throw new Error('Expected a sequence prop clipboard payload');
	}

	expect(
		getPasteSequencePropTarget({
			selectedItems: [{type: 'sequence', nodePathInfo: targetNodePathInfo}],
			payload,
			propStatuses,
			tracks: calculate({
				sequences,
				overrideIdsToNodePaths,
			}).tracks,
		}),
	).toMatchObject({
		type: 'valid',
		targets: [{nodePath: targetNodePath, fieldKey: 'style.rotate'}],
	});
});

test('copying selected keyframes requires one property', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const opacityNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'opacity'],
	);
	const otherNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'otherOpacity'],
	);
	const nodePath = opacityNodePathInfo.sequenceSubscriptionKey;
	const schema = {
		opacity: {type: 'number', default: 1, hiddenFromList: false},
		otherOpacity: {type: 'number', default: 1, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				opacity: {
					status: 'keyframed',
					keyframeDisplayOffsetAdjustment: null,
					interpolationFunction: 'interpolate',
					keyframes: [{frame: 10, value: 0.4}],
					easing: [],
					clamping: {left: 'clamp', right: 'clamp'},
					posterize: undefined,
					output: undefined,
				},
				otherOpacity: {
					status: 'keyframed',
					keyframeDisplayOffsetAdjustment: null,
					interpolationFunction: 'interpolate',
					keyframes: [{frame: 30, value: 2}],
					easing: [],
					clamping: {left: 'clamp', right: 'clamp'},
					posterize: undefined,
					output: undefined,
				},
			},
			effects: [],
		},
	} satisfies PropStatuses;

	expect(
		getKeyframeClipboardDataFromSelections({
			selections: [
				{type: 'keyframe', nodePathInfo: opacityNodePathInfo, frame: 10},
				{type: 'keyframe', nodePathInfo: otherNodePathInfo, frame: 30},
			],
			tracks: calculate({
				sequences: [makeTimelineSequence({schema})],
				overrideIdsToNodePaths: {override: nodePath},
			}).tracks,
			propStatuses,
		}),
	).toBe(null);
});

test('pasting keyframes targets one selected property at the playhead', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const nodePathInfo = makeNodePathInfo(['body', 0], ['controls', 'opacity']);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const schema = {
		opacity: {type: 'number', default: 1, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				opacity: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: 1,
				},
			},
			effects: [],
		},
	} satisfies PropStatuses;

	expect(
		getPasteKeyframeTarget({
			selectedItems: [
				{type: 'keyframe', nodePathInfo, frame: 10},
				{type: 'keyframe', nodePathInfo, frame: 30},
			],
			payload: {
				type: 'keyframe',
				version: 1,
				remotionClipboard: 'keyframe',
				fieldType: 'number',
				field: null,
				keyframes: [
					{frameOffset: 0, value: 0.4},
					{frameOffset: 20, value: 0.8},
				],
				easing: [{type: 'linear'}],
			},
			timelinePosition: 50,
			tracks: calculate({
				sequences: [makeTimelineSequence({schema, from: 20})],
				overrideIdsToNodePaths: {override: nodePath},
			}).tracks,
			propStatuses,
		}),
	).toEqual({
		type: 'valid',
		fileName: '/project/src/Comp.tsx',
		nodePath,
		fieldKey: 'opacity',
		effectIndex: null,
		keyframes: [
			{sourceFrame: 50, value: 0.4},
			{sourceFrame: 70, value: 0.8},
		],
		keyframesToDelete: [],
		easing: [{type: 'linear'}],
		firstEasingSegmentIndex: 0,
		schema,
	} satisfies PasteKeyframeTarget);
});

test('pasting keyframes replaces the destination range and preserves easing', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const nodePathInfo = makeNodePathInfo(['body', 0], ['controls', 'opacity']);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const schema = {
		opacity: {type: 'number', default: 1, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				opacity: {
					status: 'keyframed',
					keyframeDisplayOffsetAdjustment: null,
					interpolationFunction: 'interpolate',
					keyframes: [
						{frame: 40, value: 0},
						{frame: 60, value: 0.5},
						{frame: 80, value: 1},
					],
					easing: [{type: 'linear'}, {type: 'linear'}],
					clamping: {left: 'clamp', right: 'clamp'},
					posterize: undefined,
					output: undefined,
				},
			},
			effects: [],
		},
	} satisfies PropStatuses;
	const copiedEasing = {
		type: 'bezier',
		x1: 0.42,
		y1: 0,
		x2: 0.58,
		y2: 1,
	} as const;

	expect(
		getPasteKeyframeTarget({
			selectedItems: [{type: 'sequence-prop', nodePathInfo, key: 'opacity'}],
			payload: {
				type: 'keyframe',
				version: 1,
				remotionClipboard: 'keyframe',
				fieldType: 'number',
				field: null,
				keyframes: [
					{frameOffset: 0, value: 0.25},
					{frameOffset: 20, value: 0.75},
				],
				easing: [copiedEasing],
			},
			timelinePosition: 50,
			tracks: calculate({
				sequences: [makeTimelineSequence({schema})],
				overrideIdsToNodePaths: {override: nodePath},
			}).tracks,
			propStatuses,
		}),
	).toEqual({
		type: 'valid',
		fileName: '/project/src/Comp.tsx',
		nodePath,
		fieldKey: 'opacity',
		effectIndex: null,
		keyframes: [
			{sourceFrame: 50, value: 0.25},
			{sourceFrame: 70, value: 0.75},
		],
		keyframesToDelete: [60],
		easing: [copiedEasing],
		firstEasingSegmentIndex: 1,
		schema,
	} satisfies PasteKeyframeTarget);
});

test('pasting a keyframe rejects an incompatible property', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const nodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'background'],
	);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const schema = {
		background: {type: 'color', default: '#ffffff'},
	} satisfies InteractivitySchema;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				background: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: '#ffffff',
				},
			},
			effects: [],
		},
	} satisfies PropStatuses;

	expect(
		getPasteKeyframeTarget({
			selectedItems: [
				{
					type: 'sequence-prop',
					nodePathInfo,
					key: 'background',
				},
			],
			payload: {
				type: 'keyframe',
				version: 1,
				remotionClipboard: 'keyframe',
				fieldType: 'number',
				field: null,
				keyframes: [{frameOffset: 0, value: 0.4}],
				easing: [],
			},
			timelinePosition: 50,
			tracks: calculate({
				sequences: [makeTimelineSequence({schema})],
				overrideIdsToNodePaths: {override: nodePath},
			}).tracks,
			propStatuses,
		}),
	).toEqual({type: 'incompatible'} satisfies PasteKeyframeTarget);
});

test('copying a selected sequence easing creates an easing payload', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const opacityNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'opacity'],
	);
	const nodePath = opacityNodePathInfo.sequenceSubscriptionKey;
	const easing = {
		type: 'bezier',
		x1: 0.42,
		y1: 0,
		x2: 0.58,
		y2: 1,
	} as const;
	const schema = {
		opacity: {type: 'number', default: 1, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				opacity: {
					status: 'keyframed',
					keyframeDisplayOffsetAdjustment: null,
					interpolationFunction: 'interpolate',
					keyframes: [
						{frame: 0, value: 0},
						{frame: 100, value: 1},
					],
					easing: [easing],
					clamping: {left: 'clamp', right: 'clamp'},
					posterize: undefined,
					output: undefined,
				},
			},
			effects: [],
		},
	} satisfies PropStatuses;

	expect(
		getEasingClipboardDataFromSelection({
			selection: {
				type: 'easing',
				nodePathInfo: opacityNodePathInfo,
				fromFrame: 0,
				toFrame: 100,
				segmentIndex: 0,
			},
			tracks: calculate({
				sequences: [makeTimelineSequence({schema})],
				overrideIdsToNodePaths: {override: nodePath},
			}).tracks,
			propStatuses,
		}),
	).toEqual({
		type: 'easing',
		version: 1,
		remotionClipboard: 'easing',
		easing,
	});
});

test('copying a selected effect easing creates an easing payload', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const effectPropNodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '0', 'intensity'],
		true,
		[['0', 'intensity']],
	);
	const nodePath = effectPropNodePathInfo.sequenceSubscriptionKey;
	const schema = {} satisfies InteractivitySchema;
	const effectSchema = {
		intensity: {type: 'number', default: 0, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {},
			effects: [
				{
					canUpdate: true,
					callee: 'halftone',
					importPath: '@remotion/effects/halftone',
					effectIndex: 0,
					props: {
						intensity: {
							status: 'keyframed',
							keyframeDisplayOffsetAdjustment: null,
							interpolationFunction: 'interpolateColors',
							keyframes: [
								{frame: 0, value: 10},
								{frame: 100, value: 20},
							],
							easing: [{type: 'linear'}],
							clamping: {left: 'clamp', right: 'clamp'},
							posterize: undefined,
							output: undefined,
						},
					},
				},
			],
		},
	} satisfies PropStatuses;

	expect(
		getEasingClipboardDataFromSelection({
			selection: {
				type: 'easing',
				nodePathInfo: effectPropNodePathInfo,
				fromFrame: 0,
				toFrame: 100,
				segmentIndex: 0,
			},
			tracks: calculate({
				sequences: [
					makeTimelineSequence({
						schema,
						effects: [{schema: effectSchema}],
					}),
				],
				overrideIdsToNodePaths: {override: nodePath},
			}).tracks,
			propStatuses,
		}),
	).toEqual({
		type: 'easing',
		version: 1,
		remotionClipboard: 'easing',
		easing: {type: 'linear'},
	});
});

test('pasting an effect prop targets a matching selected effect', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const effectNodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '1'],
		true,
		[['1']],
	);
	const nodePath = effectNodePathInfo.sequenceSubscriptionKey;
	const effectSchema = {
		intensity: {type: 'number', default: 0, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {},
			effects: [
				{
					canUpdate: true,
					callee: 'halftone',
					importPath: '@remotion/effects/halftone',
					effectIndex: 1,
					props: {
						intensity: {
							status: 'static',
							keyframeDisplayOffsetAdjustment: null,
							codeValue: 0,
						},
					},
				},
			],
		},
	} satisfies PropStatuses;

	expect(
		getPasteEffectPropTarget({
			selectedItems: [
				{type: 'sequence-effect', nodePathInfo: effectNodePathInfo, i: 1},
			],
			payload: {
				type: 'effect-prop',
				version: 1,
				remotionClipboard: 'effect-prop',
				effect: {
					callee: 'halftone',
					importPath: '@remotion/effects/halftone',
				},
				key: 'intensity',
				param: {type: 'static', value: 10},
			},
			propStatuses,
			tracks: calculate({
				sequences: [
					makeTimelineSequence({
						schema: {},
						effects: [{schema: effectSchema}, {schema: effectSchema}],
					}),
				],
				overrideIdsToNodePaths: {override: nodePath},
			}).tracks,
		}),
	).toEqual({
		type: 'valid',
		targets: [
			{
				fileName: '/project/src/Comp.tsx',
				nodePath,
				effectIndex: 1,
				fieldKey: 'intensity',
				defaultValue: '0',
				schema: effectSchema,
			},
		],
	} satisfies PasteEffectPropTarget);
});

test('pasting an effect prop targets multiple matching selected effects', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const firstNodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '0'],
		true,
		[['intensity']],
	);
	const secondNodePathInfo = makeNodePathInfo(
		['body', 1],
		['effects', '0', 'intensity'],
		true,
		[['intensity']],
	);
	const firstNodePath = firstNodePathInfo.sequenceSubscriptionKey;
	const secondNodePath = secondNodePathInfo.sequenceSubscriptionKey;
	const effectSchema = {
		intensity: {type: 'number', default: 0, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const effectStatus = {
		canUpdate: true,
		callee: 'halftone',
		importPath: '@remotion/effects/halftone',
		effectIndex: 0,
		props: {
			intensity: {
				status: 'static',
				keyframeDisplayOffsetAdjustment: null,
				codeValue: 0,
			},
		},
	} as const;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(firstNodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {},
			effects: [effectStatus],
		},
		[Internals.makeSequencePropsSubscriptionKey(secondNodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {},
			effects: [effectStatus],
		},
	} satisfies PropStatuses;

	expect(
		getPasteEffectPropTarget({
			selectedItems: [
				{
					type: 'sequence-effect',
					nodePathInfo: firstNodePathInfo,
					i: 0,
				},
				{
					type: 'sequence-effect-prop',
					nodePathInfo: secondNodePathInfo,
					i: 0,
					key: 'intensity',
				},
			],
			payload: {
				type: 'effect-prop',
				version: 1,
				remotionClipboard: 'effect-prop',
				effect: {
					callee: 'halftone',
					importPath: '@remotion/effects/halftone',
				},
				key: 'intensity',
				param: {type: 'static', value: 10},
			},
			propStatuses,
			tracks: calculate({
				sequences: [
					makeTimelineSequence({
						schema: {},
						effects: [{schema: effectSchema}],
						id: 'first',
						overrideId: 'first',
					}),
					makeTimelineSequence({
						schema: {},
						effects: [{schema: effectSchema}],
						id: 'second',
						overrideId: 'second',
					}),
				],
				overrideIdsToNodePaths: {
					first: firstNodePath,
					second: secondNodePath,
				},
			}).tracks,
		}),
	).toEqual({
		type: 'valid',
		targets: [
			{
				fileName: '/project/src/Comp.tsx',
				nodePath: firstNodePath,
				effectIndex: 0,
				fieldKey: 'intensity',
				defaultValue: '0',
				schema: effectSchema,
			},
			{
				fileName: '/project/src/Comp.tsx',
				nodePath: secondNodePath,
				effectIndex: 0,
				fieldKey: 'intensity',
				defaultValue: '0',
				schema: effectSchema,
			},
		],
	} satisfies PasteEffectPropTarget);
});

test('pasting an effect prop requires the same effect type and prop key', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const effectPropNodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '0', 'opacity'],
		true,
		[['0', 'opacity']],
	);
	const nodePath = effectPropNodePathInfo.sequenceSubscriptionKey;
	const effectSchema = {
		opacity: {type: 'number', default: 1, hiddenFromList: false},
		intensity: {type: 'number', default: 0, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {},
			effects: [
				{
					canUpdate: true,
					callee: 'blur',
					importPath: '@remotion/effects/blur',
					effectIndex: 0,
					props: {
						opacity: {
							status: 'static',
							keyframeDisplayOffsetAdjustment: null,
							codeValue: 1,
						},
						intensity: {
							status: 'static',
							keyframeDisplayOffsetAdjustment: null,
							codeValue: 0,
						},
					},
				},
			],
		},
	} satisfies PropStatuses;
	const basePayload = {
		type: 'effect-prop' as const,
		version: 1 as const,
		remotionClipboard: 'effect-prop' as const,
		effect: {
			callee: 'halftone',
			importPath: '@remotion/effects/halftone',
		},
		key: 'intensity',
		param: {type: 'static' as const, value: 10},
	};

	expect(
		getPasteEffectPropTarget({
			selectedItems: [
				{
					type: 'sequence-effect-prop',
					nodePathInfo: effectPropNodePathInfo,
					i: 0,
					key: 'opacity',
				},
			],
			payload: basePayload,
			propStatuses,
			tracks: calculate({
				sequences: [
					makeTimelineSequence({
						schema: {},
						effects: [{schema: effectSchema}],
					}),
				],
				overrideIdsToNodePaths: {override: nodePath},
			}).tracks,
		}),
	).toEqual({type: 'prop-mismatch'} satisfies PasteEffectPropTarget);

	expect(
		getPasteEffectPropTarget({
			selectedItems: [
				{type: 'sequence-effect', nodePathInfo: effectPropNodePathInfo, i: 0},
			],
			payload: basePayload,
			propStatuses,
			tracks: calculate({
				sequences: [
					makeTimelineSequence({
						schema: {},
						effects: [{schema: effectSchema}],
					}),
				],
				overrideIdsToNodePaths: {override: nodePath},
			}).tracks,
		}),
	).toEqual({type: 'effect-type-mismatch'} satisfies PasteEffectPropTarget);
});

test('Timeline duration drag applies the same delta to selected sequences', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const firstNodePathInfo = makeNodePathInfo(['body', 0], []);
	const secondNodePathInfo = makeNodePathInfo(['body', 1], []);
	const sequences = [
		makeTimelineSequence({
			schema,
			id: 'first',
			overrideId: 'first',
			duration: 40,
			from: 0,
		}),
		makeTimelineSequence({
			schema,
			id: 'second',
			overrideId: 'second',
			duration: 15,
			from: 10,
		}),
	];
	const targets = getTimelineSequenceDurationDragTargets({
		draggedNodePathInfo: firstNodePathInfo,
		draggedSequenceMediaDurationDragLimits: null,
		selectedSequenceMediaDurationDragLimits: null,
		selectedItems: [
			{type: 'sequence', nodePathInfo: firstNodePathInfo},
			{type: 'sequence', nodePathInfo: secondNodePathInfo},
		],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths: {
				first: firstNodePathInfo.sequenceSubscriptionKey,
				second: secondNodePathInfo.sequenceSubscriptionKey,
			},
		}).tracks,
		propStatuses: makeDurationPropStatuses([
			firstNodePathInfo.sequenceSubscriptionKey,
			secondNodePathInfo.sequenceSubscriptionKey,
		]),
		timelineDurationInFrames: 1000,
	});

	expect(targets?.map((target) => target.initialDuration)).toEqual([40, 15]);
	expect(targets?.map((target) => target.maximumDuration)).toEqual([1000, 990]);
	expect(
		getTimelineSequenceDurationDragChanges({
			targets: targets ?? [],
			deltaFrames: -10,
		}).map((change) => change.value),
	).toEqual([30, 5]);
	expect(
		getTimelineSequenceDurationDragChanges({
			targets: targets ?? [],
			deltaFrames: -10,
		}).map((change) => change.schema),
	).toEqual([schema, schema]);
});

test('Timeline duration drag clamps each selected media item to its asset end', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const firstNodePathInfo = makeNodePathInfo(['body', 0], []);
	const secondNodePathInfo = makeNodePathInfo(['body', 1], []);
	const sequences = [
		makeTimelineSequence({
			schema,
			id: 'first',
			overrideId: 'first',
			duration: 40,
			type: 'video',
		}),
		makeTimelineSequence({
			schema,
			id: 'second',
			overrideId: 'second',
			duration: 15,
			type: 'audio',
		}),
	];
	const common = {
		draggedNodePathInfo: firstNodePathInfo,
		draggedSequenceMediaDurationDragLimits: {
			initialDuration: 40,
			maximumDuration: 45,
		},
		selectedItems: [
			{type: 'sequence' as const, nodePathInfo: firstNodePathInfo},
			{type: 'sequence' as const, nodePathInfo: secondNodePathInfo},
		],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths: {
				first: firstNodePathInfo.sequenceSubscriptionKey,
				second: secondNodePathInfo.sequenceSubscriptionKey,
			},
		}).tracks,
		propStatuses: makeDurationPropStatuses([
			firstNodePathInfo.sequenceSubscriptionKey,
			secondNodePathInfo.sequenceSubscriptionKey,
		]),
		timelineDurationInFrames: 1000,
	};

	expect(
		getTimelineSequenceDurationDragTargets({
			...common,
			selectedSequenceMediaDurationDragLimits: null,
		}),
	).toBe(null);

	const targets = getTimelineSequenceDurationDragTargets({
		...common,
		selectedSequenceMediaDurationDragLimits: new Map([
			[
				getTimelineSequenceSelectionKey(secondNodePathInfo),
				{initialDuration: 15, maximumDuration: 18},
			],
		]),
	});
	expect(
		getTimelineSequenceDurationDragChanges({
			targets: targets ?? [],
			deltaFrames: 10,
		}).map((change) => change.value),
	).toEqual([45, 18]);
	expect(
		getTimelineSequenceDurationDragChanges({
			targets: targets ?? [],
			deltaFrames: -10,
		}).map((change) => change.value),
	).toEqual([30, 5]);
});

test('Timeline duration drag uses the declared duration for negative from values', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const nodePathInfo = makeNodePathInfo(['body', 0], []);
	const sequences = [
		makeTimelineSequence({
			schema,
			duration: 36,
			from: -3,
		}),
	];
	const targets = getTimelineSequenceDurationDragTargets({
		draggedNodePathInfo: nodePathInfo,
		draggedSequenceMediaDurationDragLimits: null,
		selectedSequenceMediaDurationDragLimits: null,
		selectedItems: [{type: 'sequence', nodePathInfo}],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths: {
				override: nodePathInfo.sequenceSubscriptionKey,
			},
		}).tracks,
		propStatuses: makeDurationPropStatuses([
			nodePathInfo.sequenceSubscriptionKey,
		]),
		timelineDurationInFrames: 1000,
	});

	expect(targets?.map((target) => target.initialDuration)).toEqual([36]);
	expect(
		getTimelineSequenceDurationDragChanges({
			targets: targets ?? [],
			deltaFrames: 0,
		}),
	).toEqual([]);
});

test.each([
	{
		name: 'negative ancestor start',
		parentFrom: -30,
		parentPlaybackRate: 1,
		parentTrimBefore: 0,
		childFrom: 0,
		childPlaybackRate: 1,
		expectedDurationInFrames: 31,
	},
	{
		name: 'positive child start under a negative ancestor',
		parentFrom: -30,
		parentPlaybackRate: 1,
		parentTrimBefore: 0,
		childFrom: 10,
		childPlaybackRate: 1,
		expectedDurationInFrames: 21,
	},
	{
		name: 'ancestor trimBefore',
		parentFrom: 0,
		parentPlaybackRate: 1,
		parentTrimBefore: 30,
		childFrom: 0,
		childPlaybackRate: 1,
		expectedDurationInFrames: 31,
	},
	{
		name: 'sped-up ancestor',
		parentFrom: -30,
		parentPlaybackRate: 2,
		parentTrimBefore: 0,
		childFrom: 0,
		childPlaybackRate: 1,
		expectedDurationInFrames: 62,
	},
	{
		name: 'sped-up ancestor with a slowed-down child',
		parentFrom: -30,
		parentPlaybackRate: 2,
		parentTrimBefore: 0,
		childFrom: 0,
		childPlaybackRate: 0.5,
		expectedDurationInFrames: 31,
	},
])('Timeline duration drag retains one visible frame: $name', (scenario) => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const nodePathInfo = makeNodePathInfo(['body', 0, 'children', 0], []);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const parent = makeTimelineSequence({
		schema,
		id: 'parent',
		overrideId: 'parent',
		from: scenario.parentFrom,
		duration: 300,
		trimBefore: scenario.parentTrimBefore,
		sequencePlaybackRate: scenario.parentPlaybackRate,
	});
	const child = makeTimelineSequence({
		schema,
		id: 'child',
		overrideId: 'child',
		parentId: parent.id,
		from: scenario.childFrom,
		duration: 60 * scenario.parentPlaybackRate,
		sequencePlaybackRate: scenario.childPlaybackRate,
	});
	const overrideIdsToNodePaths = {child: nodePath};
	const sequences = [parent, child];
	const targets = getTimelineSequenceDurationDragTargets({
		draggedNodePathInfo: nodePathInfo,
		draggedSequenceMediaDurationDragLimits: null,
		selectedSequenceMediaDurationDragLimits: null,
		selectedItems: [{type: 'sequence', nodePathInfo}],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths,
		}).tracks,
		propStatuses: makeDurationPropStatuses([nodePath]),
		timelineDurationInFrames: 600,
	});
	const changes = getTimelineSequenceDurationDragChanges({
		targets: targets ?? [],
		deltaFrames: -1000,
	});

	expect(changes.map(({fieldKey, value}) => ({fieldKey, value}))).toEqual([
		{
			fieldKey: 'durationInFrames',
			value: scenario.expectedDurationInFrames,
		},
	]);

	const resizedTracks = calculate({
		sequences: [
			parent,
			{
				...child,
				duration: Number(changes[0].value) / child.sequencePlaybackRate,
			},
		],
		overrideIdsToNodePaths,
	}).tracks;
	expect(
		resizedTracks.find((track) => track.sequence.id === child.id)?.sequence
			.duration,
	).toBe(1);
});

test('Timeline duration drag clamps to the composition end', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const nodePathInfo = makeNodePathInfo(['body', 0], []);
	const sequences = [
		makeTimelineSequence({
			schema,
			duration: 40,
			from: 30,
		}),
	];
	const targets = getTimelineSequenceDurationDragTargets({
		draggedNodePathInfo: nodePathInfo,
		draggedSequenceMediaDurationDragLimits: null,
		selectedSequenceMediaDurationDragLimits: null,
		selectedItems: [{type: 'sequence', nodePathInfo}],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths: {
				override: nodePathInfo.sequenceSubscriptionKey,
			},
		}).tracks,
		propStatuses: makeDurationPropStatuses([
			nodePathInfo.sequenceSubscriptionKey,
		]),
		timelineDurationInFrames: 180,
	});

	expect(targets?.[0].maximumDuration).toBe(150);
	expect(
		getTimelineSequenceDurationDragChanges({
			targets: targets ?? [],
			deltaFrames: 200,
		})[0].value,
	).toBe(150);
});

test('Timeline duration drag supports interactive video clips', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const nodePathInfo = makeNodePathInfo(['body', 0], []);
	const video = makeTimelineSequence({
		schema: Internals.baseSchema,
		type: 'video',
		duration: 78,
	});

	expect(isTimelineSequenceDurationDraggable(video)).toBe(true);
	const sequences = [video];
	expect(
		getTimelineSequenceDurationDragTargets({
			draggedNodePathInfo: nodePathInfo,
			draggedSequenceMediaDurationDragLimits: {
				initialDuration: 78,
				maximumDuration: 1000,
			},
			selectedSequenceMediaDurationDragLimits: null,
			selectedItems: [{type: 'sequence', nodePathInfo}],
			sequences,
			tracks: calculate({
				sequences,
				overrideIdsToNodePaths: {
					override: nodePathInfo.sequenceSubscriptionKey,
				},
			}).tracks,
			propStatuses: makeDurationPropStatuses([
				nodePathInfo.sequenceSubscriptionKey,
			]),
			timelineDurationInFrames: 1000,
		}),
	).toEqual([
		{
			fileName: nodePathInfo.sequenceSubscriptionKey.absolutePath,
			parentPlaybackRate: 1,
			initialDuration: 78,
			maximumDuration: 1000,
			minimumDuration: 1,
			minimumDurationReason: 'Minimum duration',
			maximumDurationReason: 'Composition edge',
			nodePath: nodePathInfo.sequenceSubscriptionKey,
			schema: Internals.baseSchema,
			endField: {fieldKey: 'durationInFrames', trimBefore: 0, playbackRate: 1},
		},
	]);
});

test('Timeline edge drags edit durationInFrames in the child clock', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const nodePathInfo = makeNodePathInfo(['body', 0], []);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const propStatuses: PropStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				durationInFrames: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: 20,
				},
			},
			effects: [],
		},
	};
	const sequences = [
		makeTimelineSequence({
			schema: Internals.baseSchema,
			duration: 10,
			sequencePlaybackRate: 2,
			currentRuntimeValueDotNotation: {
				durationInFrames: 20,
				playbackRate: 2,
				trimBefore: 4,
			},
		}),
	];
	const targets = getTimelineSequenceDurationDragTargets({
		draggedNodePathInfo: nodePathInfo,
		draggedSequenceMediaDurationDragLimits: null,
		selectedSequenceMediaDurationDragLimits: null,
		selectedItems: [{type: 'sequence', nodePathInfo}],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths: {override: nodePath},
		}).tracks,
		propStatuses,
		timelineDurationInFrames: 1000,
	});

	expect(targets?.[0].endField).toEqual({
		fieldKey: 'durationInFrames',
		trimBefore: 4,
		playbackRate: 2,
	});
	expect(
		getTimelineSequenceDurationDragChanges({
			targets: targets ?? [],
			deltaFrames: 2,
		})[0],
	).toMatchObject({fieldKey: 'durationInFrames', value: 24});
});

test('Left edge trim preserves a recent right edge trim in the child clock', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const nodePathInfo = makeNodePathInfo(['body', 0], []);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const sequence = makeTimelineSequence({
		schema: Internals.baseSchema,
		duration: 10,
		sequencePlaybackRate: 2,
		trimBefore: 4,
		currentRuntimeValueDotNotation: {
			durationInFrames: 20,
			playbackRate: 2,
			trimBefore: 4,
		},
	});
	const sequences = [sequence];
	const targets = getTimelineSequenceLeftEdgeDragTargets({
		draggedNodePathInfo: nodePathInfo,
		selectedItems: [{type: 'sequence', nodePathInfo}],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths: {override: nodePath},
		}).tracks,
		propStatuses: makeLeftEdgePropStatuses([nodePath], true, true, [16]),
	});

	expect(
		getTimelineSequenceLeftEdgeDragChanges({
			targets: targets ?? [],
			deltaFrames: 2,
		}).map((change) => [change.fieldKey, change.value]),
	).toEqual([
		['from', 2],
		['durationInFrames', 12],
		['trimBefore', 8],
	]);
});

test('Looping timeline items cannot be resized or split', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const nodePathInfo = makeNodePathInfo(['body', 0], []);
	const loopedSequence = {
		...makeTimelineSequence({
			schema: Internals.baseSchema,
			duration: 20,
		}),
		loopDisplay: {
			durationInFrames: 5,
			numberOfTimes: 4,
			startOffset: 0,
		},
	} satisfies TSequence;
	const selection = {
		type: 'sequence' as const,
		nodePathInfo,
	};

	expect(isTimelineSequenceDurationDraggable(loopedSequence)).toBe(false);
	const loopedVideo = {
		...loopedSequence,
		type: 'video',
		src: 'video.mp4',
		muted: false,
		startMediaFrom: 0,
		mediaFrameAtSequenceZero: 0,
		playbackRate: 1,
		frozenMediaFrame: null,
	} satisfies TSequence;
	const sequences = [loopedVideo];
	const resizeTargets = getTimelineSequenceDurationDragTargets({
		draggedNodePathInfo: nodePathInfo,
		draggedSequenceMediaDurationDragLimits: null,
		selectedSequenceMediaDurationDragLimits: null,
		selectedItems: [selection],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths: {
				override: nodePathInfo.sequenceSubscriptionKey,
			},
		}).tracks,
		propStatuses: makeDurationPropStatuses([
			nodePathInfo.sequenceSubscriptionKey,
		]),
		timelineDurationInFrames: 1000,
	});
	expect(resizeTargets).toBeNull();
	expect(
		getTimelineSequenceSplitEligibility({
			selection,
			sequence: loopedSequence,
			splitFrame: 10,
		}),
	).toEqual({canSplit: false, reason: 'Looping sequences cannot be split'});
});

test('Media duration drag limits account for a negative from', () => {
	const common = {
		cascadedStart: -192,
		displayDurationInFrames: 108,
		displayStart: 0,
		explicitDurationInFrames: 300,
		naturalMediaDuration: 33,
		timelineDurationInFrames: 1000,
	};

	expect(
		getTimelineSequenceMediaDurationDragLimits({
			...common,
			// <Video> keeps displaying its last frame.
			effectiveMaxMediaDuration: null,
		}),
	).toEqual({initialDuration: 300, maximumDuration: 225});
	expect(
		getTimelineSequenceMediaDurationDragLimits({
			...common,
			// Audio and OffthreadVideo stop at the asset end.
			effectiveMaxMediaDuration: 33,
		}),
	).toEqual({initialDuration: 300, maximumDuration: 225});
});

test('Implicit media can be shortened once its edge reaches the asset end', () => {
	const common = {
		cascadedStart: -34,
		displayDurationInFrames: 266,
		displayStart: 0,
		effectiveMaxMediaDuration: null,
		explicitDurationInFrames: null,
		timelineDurationInFrames: 300,
	};

	expect(
		getTimelineSequenceMediaDurationDragLimits({
			...common,
			// The video holds its last frame until the composition ends.
			naturalMediaDuration: 220,
		}),
	).toEqual({initialDuration: 300, maximumDuration: 254});
	expect(
		getTimelineSequenceMediaDurationDragLimits({
			...common,
			// The composition ends before the source video does.
			naturalMediaDuration: 400,
		}),
	).toEqual({initialDuration: 300, maximumDuration: 434});
});

test('Timeline duration drag clamps explicit audio and video to the asset end', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	for (const type of ['audio', 'video'] as const) {
		const nodePathInfo = makeNodePathInfo(['body', type], []);
		const nodePath = nodePathInfo.sequenceSubscriptionKey;
		const sequences = [
			makeTimelineSequence({
				schema: Internals.baseSchema,
				type,
				duration: 200,
				from: -192,
			}),
		];
		const targets = getTimelineSequenceDurationDragTargets({
			draggedNodePathInfo: nodePathInfo,
			draggedSequenceMediaDurationDragLimits: {
				initialDuration: 200,
				maximumDuration: 225,
			},
			selectedSequenceMediaDurationDragLimits: null,
			selectedItems: [{type: 'sequence', nodePathInfo}],
			sequences,
			tracks: calculate({
				sequences,
				overrideIdsToNodePaths: {override: nodePath},
			}).tracks,
			propStatuses: makeDurationPropStatuses([nodePath]),
			timelineDurationInFrames: 1000,
		});

		expect(targets?.[0].maximumDuration).toBe(225);
		expect(
			getTimelineSequenceDurationDragChanges({
				targets: targets ?? [],
				deltaFrames: 100,
			})[0].value,
		).toBe(225);
	}
});

test('An overlong explicit Video can be shortened without snapping', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const nodePathInfo = makeNodePathInfo(['body', 0], []);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const sequences = [
		makeTimelineSequence({
			schema: Internals.baseSchema,
			type: 'video',
			duration: 300,
			from: -192,
		}),
	];
	const targets = getTimelineSequenceDurationDragTargets({
		draggedNodePathInfo: nodePathInfo,
		draggedSequenceMediaDurationDragLimits: {
			initialDuration: 300,
			maximumDuration: 225,
		},
		selectedSequenceMediaDurationDragLimits: null,
		selectedItems: [{type: 'sequence', nodePathInfo}],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths: {override: nodePath},
		}).tracks,
		propStatuses: makeDurationPropStatuses([nodePath]),
		timelineDurationInFrames: 1000,
	});

	expect(targets?.[0].maximumDuration).toBe(300);
	expect(
		getTimelineSequenceDurationDragChanges({
			targets: targets ?? [],
			deltaFrames: 1,
		}),
	).toEqual([]);
	expect(
		getTimelineSequenceDurationDragChanges({
			targets: targets ?? [],
			deltaFrames: -1,
		})[0].value,
	).toBe(299);
});

test('Timeline duration drag trims audio and video without an explicit duration', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	for (const type of ['audio', 'video'] as const) {
		const nodePathInfo = makeNodePathInfo(['body', type], []);
		const media = makeTimelineSequence({
			schema: Internals.baseSchema,
			type,
			duration: Infinity,
		});
		const nodePath = nodePathInfo.sequenceSubscriptionKey;
		const sequences = [media];
		const targets = getTimelineSequenceDurationDragTargets({
			draggedNodePathInfo: nodePathInfo,
			draggedSequenceMediaDurationDragLimits: {
				initialDuration: 70,
				maximumDuration: 77,
			},
			selectedSequenceMediaDurationDragLimits: null,
			selectedItems: [{type: 'sequence', nodePathInfo}],
			sequences,
			tracks: calculate({
				sequences,
				overrideIdsToNodePaths: {
					override: nodePath,
				},
			}).tracks,
			propStatuses: {
				[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
					durationDependentKeyframes: null,
					canUpdate: true,
					props: {
						durationInFrames: {
							status: 'static',
							keyframeDisplayOffsetAdjustment: null,
							codeValue: undefined,
						},
					},
					effects: [],
				},
			},
			timelineDurationInFrames: 1000,
		});

		expect(isTimelineSequenceDurationDraggable(media)).toBe(true);
		expect(targets?.[0].initialDuration).toBe(70);
		expect(targets?.[0].maximumDuration).toBe(77);
		expect(
			getTimelineSequenceDurationDragChanges({
				targets: targets ?? [],
				deltaFrames: 10,
			})[0].value,
		).toBe(77);
		expect(
			getTimelineSequenceDurationDragChanges({
				targets: targets ?? [],
				deltaFrames: -10,
			}),
		).toEqual([
			{
				fileName: nodePath.absolutePath,
				nodePath,
				fieldKey: 'durationInFrames',
				value: 60,
				defaultValue: null,
				schema: Internals.baseSchema,
			},
		]);
	}
});

test('Timeline duration drag waits for an effective implicit media duration', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const nodePathInfo = makeNodePathInfo(['body', 0], []);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const sequences = [
		makeTimelineSequence({
			schema: Internals.baseSchema,
			type: 'video',
			duration: Infinity,
		}),
	];
	expect(
		getTimelineSequenceDurationDragTargets({
			draggedNodePathInfo: nodePathInfo,
			draggedSequenceMediaDurationDragLimits: null,
			selectedSequenceMediaDurationDragLimits: null,
			selectedItems: [{type: 'sequence', nodePathInfo}],
			sequences,
			tracks: calculate({
				sequences,
				overrideIdsToNodePaths: {override: nodePath},
			}).tracks,
			propStatuses: {
				[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
					durationDependentKeyframes: null,
					canUpdate: true,
					props: {
						durationInFrames: {
							status: 'static',
							keyframeDisplayOffsetAdjustment: null,
							codeValue: undefined,
						},
					},
					effects: [],
				},
			},
			timelineDurationInFrames: 1000,
		}),
	).toBe(null);
});

test('Timeline duration drag supports interactive cascading sequence rows', () => {
	const baseSequence = makeTimelineSequence({
		schema: Internals.baseSchema,
		duration: 78,
	});
	const seriesSequence = {
		...baseSequence,
		isInsideSeries: true,
		controls: {
			...baseSequence.controls!,
			componentIdentity: 'dev.remotion.remotion.Series.Sequence',
		},
	} satisfies TSequence;

	expect(isTimelineSequenceDurationDraggable(seriesSequence)).toBe(true);
	expect(isCascadingSequence(seriesSequence)).toBe(true);
	expect(isTimelineSequenceLeftEdgeDraggable(seriesSequence)).toBe(true);

	const transitionSeriesSequence = {
		...baseSequence,
		isInsideSeries: true,
		controls: {
			...baseSequence.controls!,
			componentIdentity: 'dev.remotion.transitions.TransitionSeries.Sequence',
		},
	} satisfies TSequence;

	expect(isTimelineSequenceDurationDraggable(transitionSeriesSequence)).toBe(
		true,
	);
	expect(isCascadingSequence(transitionSeriesSequence)).toBe(true);
	expect(isTimelineSequenceLeftEdgeDraggable(transitionSeriesSequence)).toBe(
		true,
	);
});

test('Timeline duration drag clamps each selected sequence to one frame', () => {
	expect(
		getTimelineSequenceDurationDragValue({
			initialDuration: 4,
			deltaFrames: -10,
			maximumDuration: 1000,
		}),
	).toBe(1);
	expect(
		getTimelineSequenceDurationDragValue({
			initialDuration: 20,
			deltaFrames: -10,
			maximumDuration: 1000,
		}),
	).toBe(10);
});

test('TransitionSeries.Sequence resize clamps to adjacent transition durations', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const firstNodePathInfo = makeNodePathInfo(['body', 0], []);
	const nodePathInfo = makeNodePathInfo(['body', 2], []);
	const transitionSeriesSequence = (
		id: string,
		duration: number,
		overrideId: string,
		timelineOrder: number,
	) =>
		makeTimelineSequence({
			schema,
			id,
			overrideId,
			duration,
			componentIdentity: 'dev.remotion.transitions.TransitionSeries.Sequence',
			isInsideSeries: true,
			timelineOrder,
		});
	const transition = (id: string, duration: number, timelineOrder: number) =>
		makeTimelineSequence({
			schema: {},
			id,
			duration,
			componentIdentity: 'dev.remotion.transitions.TransitionSeries.Transition',
			isInsideSeries: true,
			timelineOrder,
		});
	const sequences = [
		transition('previous-transition', 8, 1),
		transitionSeriesSequence('first', 30, 'first', 0),
		transitionSeriesSequence('target', 40, 'target', 2),
		transitionSeriesSequence('last', 30, 'last', 4),
		transition('next-transition', 12, 3),
	];
	const propStatuses = makeLeftEdgePropStatuses(
		[
			firstNodePathInfo.sequenceSubscriptionKey,
			nodePathInfo.sequenceSubscriptionKey,
		],
		true,
	);
	const overrideIdsToNodePaths = {
		first: firstNodePathInfo.sequenceSubscriptionKey,
		target: nodePathInfo.sequenceSubscriptionKey,
	};
	const selectedItems = [{type: 'sequence' as const, nodePathInfo}];
	const durationTargets = getTimelineSequenceDurationDragTargets({
		draggedNodePathInfo: nodePathInfo,
		draggedSequenceMediaDurationDragLimits: null,
		selectedSequenceMediaDurationDragLimits: null,
		selectedItems,
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths,
		}).tracks,
		propStatuses,
		timelineDurationInFrames: 1000,
	});

	expect(durationTargets?.[0].minimumDuration).toBe(12);
	expect(
		getTimelineSequenceDurationDragChanges({
			targets: durationTargets ?? [],
			deltaFrames: -100,
		})[0].value,
	).toBe(12);

	const leftEdgeTargets = getTimelineSequenceLeftEdgeDragTargets({
		draggedNodePathInfo: nodePathInfo,
		selectedItems,
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths,
		}).tracks,
		propStatuses,
	});

	expect(leftEdgeTargets?.[0].ripplePrevious?.minimumDuration).toBe(8);
	expect(
		getTimelineSequenceLeftEdgeDragChanges({
			targets: leftEdgeTargets ?? [],
			deltaFrames: -100,
		}).map((change) => [change.fieldKey, change.value]),
	).toEqual([['durationInFrames', 8]]);
});

test('Timeline duration drag is blocked if one selected sequence cannot update duration', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const firstNodePathInfo = makeNodePathInfo(['body', 0], []);
	const secondNodePathInfo = makeNodePathInfo(['body', 1], []);
	const propStatuses = makeDurationPropStatuses([
		firstNodePathInfo.sequenceSubscriptionKey,
	]);

	propStatuses[
		Internals.makeSequencePropsSubscriptionKey(
			secondNodePathInfo.sequenceSubscriptionKey,
		)
	] = {
		durationDependentKeyframes: null,
		canUpdate: true,
		props: {},
		effects: [],
	};

	const sequences = [
		makeTimelineSequence({
			schema,
			id: 'first',
			overrideId: 'first',
			duration: 40,
		}),
		makeTimelineSequence({
			schema,
			id: 'second',
			overrideId: 'second',
			duration: 15,
			from: 10,
		}),
	];
	expect(
		getTimelineSequenceDurationDragTargets({
			draggedNodePathInfo: firstNodePathInfo,
			draggedSequenceMediaDurationDragLimits: null,
			selectedSequenceMediaDurationDragLimits: null,
			selectedItems: [
				{type: 'sequence', nodePathInfo: firstNodePathInfo},
				{type: 'sequence', nodePathInfo: secondNodePathInfo},
			],
			sequences,
			tracks: calculate({
				sequences,
				overrideIdsToNodePaths: {
					first: firstNodePathInfo.sequenceSubscriptionKey,
					second: secondNodePathInfo.sequenceSubscriptionKey,
				},
			}).tracks,
			propStatuses,
			timelineDurationInFrames: 1000,
		}),
	).toBe(null);
});

test('Timeline duration drag is blocked if one selected sequence duration is keyframed', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const firstNodePathInfo = makeNodePathInfo(['body', 0], []);
	const secondNodePathInfo = makeNodePathInfo(['body', 1], []);
	const propStatuses = makeDurationPropStatuses([
		firstNodePathInfo.sequenceSubscriptionKey,
		secondNodePathInfo.sequenceSubscriptionKey,
	]);

	propStatuses[
		Internals.makeSequencePropsSubscriptionKey(
			secondNodePathInfo.sequenceSubscriptionKey,
		)
	] = {
		durationDependentKeyframes: null,
		canUpdate: true,
		props: {
			durationInFrames: {
				status: 'keyframed',
				keyframeDisplayOffsetAdjustment: null,
				interpolationFunction: 'interpolate',
				keyframes: [{frame: 0, value: 15}],
				easing: [{type: 'linear'}],
				clamping: {left: 'clamp', right: 'clamp'},
				posterize: undefined,
				output: undefined,
			},
		},
		effects: [],
	};

	const sequences = [
		makeTimelineSequence({
			schema,
			id: 'first',
			overrideId: 'first',
			duration: 40,
		}),
		makeTimelineSequence({
			schema,
			id: 'second',
			overrideId: 'second',
			duration: 15,
			from: 10,
		}),
	];
	expect(
		getTimelineSequenceDurationDragTargets({
			draggedNodePathInfo: firstNodePathInfo,
			draggedSequenceMediaDurationDragLimits: null,
			selectedSequenceMediaDurationDragLimits: null,
			selectedItems: [
				{type: 'sequence', nodePathInfo: firstNodePathInfo},
				{type: 'sequence', nodePathInfo: secondNodePathInfo},
			],
			sequences,
			tracks: calculate({
				sequences,
				overrideIdsToNodePaths: {
					first: firstNodePathInfo.sequenceSubscriptionKey,
					second: secondNodePathInfo.sequenceSubscriptionKey,
				},
			}).tracks,
			propStatuses,
			timelineDurationInFrames: 1000,
		}),
	).toBe(null);
});

test('Timeline duration drag ignores selection if dragged sequence is not selected', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const firstNodePathInfo = makeNodePathInfo(['body', 0], []);
	const secondNodePathInfo = makeNodePathInfo(['body', 1], []);
	const thirdNodePathInfo = makeNodePathInfo(['body', 2], []);
	const sequences = [
		makeTimelineSequence({
			schema,
			id: 'first',
			overrideId: 'first',
			duration: 40,
		}),
		makeTimelineSequence({
			schema,
			id: 'second',
			overrideId: 'second',
			duration: 15,
			from: 10,
		}),
		makeTimelineSequence({
			schema,
			id: 'third',
			overrideId: 'third',
			duration: 25,
			from: 20,
		}),
	];
	const targets = getTimelineSequenceDurationDragTargets({
		draggedNodePathInfo: firstNodePathInfo,
		draggedSequenceMediaDurationDragLimits: null,
		selectedSequenceMediaDurationDragLimits: null,
		selectedItems: [
			{type: 'sequence', nodePathInfo: secondNodePathInfo},
			{type: 'sequence', nodePathInfo: thirdNodePathInfo},
		],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths: {
				first: firstNodePathInfo.sequenceSubscriptionKey,
				second: secondNodePathInfo.sequenceSubscriptionKey,
				third: thirdNodePathInfo.sequenceSubscriptionKey,
			},
		}).tracks,
		propStatuses: makeDurationPropStatuses([
			firstNodePathInfo.sequenceSubscriptionKey,
			secondNodePathInfo.sequenceSubscriptionKey,
			thirdNodePathInfo.sequenceSubscriptionKey,
		]),
		timelineDurationInFrames: 1000,
	});

	expect(targets?.map((target) => target.nodePath)).toEqual([
		firstNodePathInfo.sequenceSubscriptionKey,
	]);
});

test('Timeline left edge drag adjusts from, duration and trimBefore for selected sequences', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const firstNodePathInfo = makeNodePathInfo(['body', 0], []);
	const secondNodePathInfo = makeNodePathInfo(['body', 1], []);
	const sequences = [
		makeTimelineSequence({
			schema,
			id: 'first',
			overrideId: 'first',
			duration: 40,
			from: 5,
		}),
		makeTimelineSequence({
			schema,
			id: 'second',
			overrideId: 'second',
			duration: 15,
			from: 10,
			type: 'image',
		}),
	];
	const targets = getTimelineSequenceLeftEdgeDragTargets({
		draggedNodePathInfo: firstNodePathInfo,
		selectedItems: [
			{type: 'sequence', nodePathInfo: firstNodePathInfo},
			{type: 'sequence', nodePathInfo: secondNodePathInfo},
		],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths: {
				first: firstNodePathInfo.sequenceSubscriptionKey,
				second: secondNodePathInfo.sequenceSubscriptionKey,
			},
		}).tracks,
		propStatuses: makeLeftEdgePropStatuses(
			[
				firstNodePathInfo.sequenceSubscriptionKey,
				secondNodePathInfo.sequenceSubscriptionKey,
			],
			true,
		),
	});

	expect(
		getTimelineSequenceLeftEdgeDragChanges({
			targets: targets ?? [],
			deltaFrames: 6,
		}).map((change) => [change.fieldKey, change.value]),
	).toEqual([
		['from', 11],
		['durationInFrames', 34],
		['trimBefore', 6],
		['from', 16],
		['durationInFrames', 9],
		['trimBefore', 6],
	]);
});

test.each([
	{durationInFrames: null, from: 0, parentRate: 1, childRate: 1},
	{durationInFrames: 465, from: 33, parentRate: 1, childRate: 1},
	{durationInFrames: 232.5, from: 33, parentRate: 2, childRate: 0.5},
])(
	'left edge trimming uses the visible start of a clipped child: %j',
	({durationInFrames, from, parentRate, childRate}) => {
		const calculate = CanvasInternals.createTimelineCalculator();
		const schema = {} satisfies InteractivitySchema;
		const nodePathInfo = makeNodePathInfo(['body', 0, 'children', 0], []);
		const child = makeTimelineSequence({
			schema,
			id: 'captions',
			parentId: 'parent',
			from,
			duration: (durationInFrames ?? 498 * childRate) / childRate,
			trimBefore: from * childRate,
			sequencePlaybackRate: childRate,
		});
		const sequences = [
			makeTimelineSequence({
				schema,
				id: 'parent',
				overrideId: 'parent',
				from: 30,
				duration: 129 / parentRate,
				trimBefore: 369,
				sequencePlaybackRate: parentRate,
			}),
			child,
		];
		const overrideIdsToNodePaths = {
			override: nodePathInfo.sequenceSubscriptionKey,
		};
		const targets = getTimelineSequenceLeftEdgeDragTargets({
			draggedNodePathInfo: nodePathInfo,
			selectedItems: [{type: 'sequence', nodePathInfo}],
			sequences,
			tracks: calculate({
				sequences,
				overrideIdsToNodePaths,
			}).tracks,
			propStatuses: makeLeftEdgePropStatuses(
				[nodePathInfo.sequenceSubscriptionKey],
				true,
				true,
				[durationInFrames ?? undefined],
			),
		});
		const changes = getTimelineSequenceLeftEdgeDragChanges({
			targets: targets ?? [],
			deltaFrames: 6,
		});
		const values = Object.fromEntries(
			changes.map((change) => {
				if (typeof change.value !== 'number') {
					throw new Error('Expected numeric trim values');
				}

				return [change.fieldKey, change.value] as const;
			}),
		);
		expect(values).toEqual({
			from: 369 + 6 * parentRate,
			durationInFrames: (129 - 6 * parentRate) * childRate,
			trimBefore: (369 + 6 * parentRate) * childRate,
		});
		const {tracks} = calculate({
			sequences: [
				sequences[0],
				{
					...child,
					from: values.from,
					duration: values.durationInFrames / childRate,
					trimBefore: values.trimBefore,
					autoDuration: false,
				} as TSequence,
			],
			overrideIdsToNodePaths,
		});
		expect(
			tracks.find((track) => track.sequence.id === 'captions')?.sequence,
		).toMatchObject({from: 36, duration: 129 / parentRate - 6});
	},
);

test('TransitionSeries.Sequence self-trim changes its duration and trimBefore', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const nodePathInfo = makeNodePathInfo(['body', 0], []);
	const subscriptionKey = nodePathInfo.sequenceSubscriptionKey;
	const sequences = [
		makeTimelineSequence({
			schema,
			componentIdentity: 'dev.remotion.transitions.TransitionSeries.Sequence',
			duration: 40,
			from: 12,
			isInsideSeries: true,
			trimBefore: 3,
		}),
	];
	const targets = getTimelineSequenceLeftEdgeDragTargets({
		draggedNodePathInfo: nodePathInfo,
		selectedItems: [{type: 'sequence', nodePathInfo}],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths: {
				override: subscriptionKey,
			},
		}).tracks,
		propStatuses: {
			[Internals.makeSequencePropsSubscriptionKey(subscriptionKey)]: {
				durationDependentKeyframes: null,
				canUpdate: true,
				props: {
					durationInFrames: {
						status: 'static',
						keyframeDisplayOffsetAdjustment: null,
						codeValue: 40,
					},
					trimBefore: {
						status: 'static',
						keyframeDisplayOffsetAdjustment: null,
						codeValue: 3,
					},
				},
				effects: [],
			},
		},
		rippleEdit: false,
	});

	expect(targets?.[0]).toMatchObject({
		initialDuration: 40,
		initialFrom: 0,
		initialTrimBefore: 3,
		positionField: null,
	});
	expect(
		getTimelineSequenceLeftEdgeDragChanges({
			targets: targets ?? [],
			deltaFrames: 6,
		}).map((change) => [change.fieldKey, change.value]),
	).toEqual([
		['durationInFrames', 34],
		['trimBefore', 9],
	]);
});

test('Series.Sequence self-trim changes its duration and trimBefore', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const nodePathInfo = makeNodePathInfo(['body', 0], []);
	const subscriptionKey = nodePathInfo.sequenceSubscriptionKey;
	const sequences = [
		makeTimelineSequence({
			schema,
			componentIdentity: 'dev.remotion.remotion.Series.Sequence',
			duration: 40,
			from: 12,
			isInsideSeries: true,
			trimBefore: 3,
		}),
	];
	const targets = getTimelineSequenceLeftEdgeDragTargets({
		draggedNodePathInfo: nodePathInfo,
		selectedItems: [{type: 'sequence', nodePathInfo}],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths: {
				override: subscriptionKey,
			},
		}).tracks,
		propStatuses: {
			[Internals.makeSequencePropsSubscriptionKey(subscriptionKey)]: {
				durationDependentKeyframes: null,
				canUpdate: true,
				props: {
					durationInFrames: {
						status: 'static',
						keyframeDisplayOffsetAdjustment: null,
						codeValue: 40,
					},
					trimBefore: {
						status: 'static',
						keyframeDisplayOffsetAdjustment: null,
						codeValue: 3,
					},
				},
				effects: [],
			},
		},
		rippleEdit: false,
	});

	expect(targets?.[0]).toMatchObject({
		initialDuration: 40,
		initialFrom: 0,
		initialTrimBefore: 3,
		minimumDuration: 1,
		positionField: null,
	});
	expect(
		getTimelineSequenceLeftEdgeDragChanges({
			targets: targets ?? [],
			deltaFrames: 6,
		}).map((change) => [change.fieldKey, change.value]),
	).toEqual([
		['durationInFrames', 34],
		['trimBefore', 9],
	]);
});

test('Timeline left edge drag clamps trimBefore to the visible range', () => {
	expect(
		getTimelineSequenceLeftEdgeDragValues({
			initialDuration: 4,
			initialFrom: 20,
			initialTrimBefore: 2,
			deltaFrames: 10,
			playbackRate: 1,
		}),
	).toEqual({
		durationInFrames: 1,
		from: 23,
		trimBefore: 5,
	});
	expect(
		getTimelineSequenceLeftEdgeDragValues({
			initialDuration: 4,
			initialFrom: 20,
			initialTrimBefore: 2,
			deltaFrames: -10,
			playbackRate: 1,
		}),
	).toEqual({
		durationInFrames: 6,
		from: 18,
		trimBefore: 0,
	});
});

test('Timeline left edge drag scales media trimBefore by the runtime playbackRate', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const cases = [
		{componentIdentity: 'dev.remotion.media.Video', type: 'video'},
		{componentIdentity: 'dev.remotion.media.Audio', type: 'audio'},
		{componentIdentity: 'dev.remotion.gif.Gif', type: 'sequence'},
		{
			componentIdentity: 'dev.remotion.remotion.AnimatedImage',
			type: 'sequence',
		},
	] as const;

	for (const [index, testCase] of cases.entries()) {
		const nodePathInfo = makeNodePathInfo(['body', index], []);
		const sequences = [
			makeTimelineSequence({
				schema,
				componentIdentity: testCase.componentIdentity,
				currentRuntimeValueDotNotation: {playbackRate: 2},
				type: testCase.type,
			}),
		];
		const targets = getTimelineSequenceLeftEdgeDragTargets({
			draggedNodePathInfo: nodePathInfo,
			selectedItems: [{type: 'sequence', nodePathInfo}],
			sequences,
			tracks: calculate({
				sequences,
				overrideIdsToNodePaths: {
					override: nodePathInfo.sequenceSubscriptionKey,
				},
			}).tracks,
			propStatuses: makeLeftEdgePropStatuses(
				[nodePathInfo.sequenceSubscriptionKey],
				true,
			),
		});

		expect(targets?.[0].playbackRate).toBe(2);
		expect(
			getTimelineSequenceLeftEdgeDragChanges({
				targets: targets ?? [],
				deltaFrames: 6,
			}).find((change) => change.fieldKey === 'trimBefore')?.value,
		).toBe(12);
	}
});

test('Timeline left edge drag clamps scaled trimBefore at zero', () => {
	expect(
		getTimelineSequenceLeftEdgeDragValues({
			initialDuration: 10,
			initialFrom: 20,
			initialTrimBefore: 8,
			deltaFrames: -10,
			playbackRate: 2,
		}),
	).toEqual({
		durationInFrames: 14,
		from: 16,
		trimBefore: 0,
	});
});

test('Timeline left edge drag reaches exact zero with fractional playback rates', () => {
	for (const playbackRate of [0.7, 1.4, 1.55, 2.8]) {
		expect(
			getTimelineSequenceLeftEdgeDragValues({
				initialDuration: 300,
				initialFrom: 400,
				initialTrimBefore: 252,
				deltaFrames: -400,
				playbackRate,
			}).trimBefore,
		).toBe(0);
	}

	expect(
		getTimelineSequenceLeftEdgeDragValues({
			initialDuration: 300,
			initialFrom: 400,
			initialTrimBefore: 252,
			deltaFrames: -1,
			playbackRate: 1.5,
		}).trimBefore,
	).toBe(250.5);
});

test('Timeline left edge drag adjusts and clamps media trimBefore', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const nodePathInfo = makeNodePathInfo(['body', 0], []);
	const sequences = [
		makeTimelineSequence({
			schema,
			duration: 40,
			from: 12,
			startMediaFrom: 8,
			type: 'video',
		}),
	];
	const targets = getTimelineSequenceLeftEdgeDragTargets({
		draggedNodePathInfo: nodePathInfo,
		selectedItems: [{type: 'sequence', nodePathInfo}],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths: {
				override: nodePathInfo.sequenceSubscriptionKey,
			},
		}).tracks,
		propStatuses: makeLeftEdgePropStatuses(
			[nodePathInfo.sequenceSubscriptionKey],
			true,
		),
	});

	expect(targets?.map((target) => target.initialTrimBefore)).toEqual([8]);
	expect(
		getTimelineSequenceLeftEdgeDragChanges({
			targets: targets ?? [],
			deltaFrames: -20,
		}).map((change) => [change.fieldKey, change.value]),
	).toEqual([
		['from', 4],
		['durationInFrames', 48],
		['trimBefore', 0],
	]);
});

test('Timeline left edge drag is blocked without timeline range props', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const nodePathInfo = makeNodePathInfo(['body', 0], []);
	const sequences = [
		makeTimelineSequence({
			schema,
			duration: 40,
			from: 0,
			startMediaFrom: 8,
			type: 'video',
		}),
	];
	const targets = getTimelineSequenceLeftEdgeDragTargets({
		draggedNodePathInfo: nodePathInfo,
		selectedItems: [{type: 'sequence', nodePathInfo}],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths: {
				override: nodePathInfo.sequenceSubscriptionKey,
			},
		}).tracks,
		propStatuses: makeLeftEdgePropStatuses(
			[nodePathInfo.sequenceSubscriptionKey],
			true,
			false,
		),
	});

	expect(targets).toBe(null);
});

test('Timeline left edge drag is blocked if trimBefore cannot update', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const nodePathInfo = makeNodePathInfo(['body', 0], []);

	const sequences = [
		makeTimelineSequence({
			schema,
			type: 'audio',
		}),
	];
	expect(
		getTimelineSequenceLeftEdgeDragTargets({
			draggedNodePathInfo: nodePathInfo,
			selectedItems: [{type: 'sequence', nodePathInfo}],
			sequences,
			tracks: calculate({
				sequences,
				overrideIdsToNodePaths: {
					override: nodePathInfo.sequenceSubscriptionKey,
				},
			}).tracks,
			propStatuses: makeLeftEdgePropStatuses([
				nodePathInfo.sequenceSubscriptionKey,
			]),
		}),
	).toBe(null);
});

test('Timeline from drag applies the same delta to selected sequences', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const firstNodePathInfo = makeNodePathInfo(['body', 0], []);
	const secondNodePathInfo = makeNodePathInfo(['body', 1], []);
	const sequences = [
		makeTimelineSequence({
			schema,
			id: 'first',
			overrideId: 'first',
			duration: 40,
			from: 0,
		}),
		makeTimelineSequence({
			schema,
			id: 'second',
			overrideId: 'second',
			duration: 15,
			from: 10,
			type: 'audio',
		}),
	];
	const overrideIdsToNodePaths = {
		first: firstNodePathInfo.sequenceSubscriptionKey,
		second: secondNodePathInfo.sequenceSubscriptionKey,
	};
	const targets = getTimelineSequenceFromDragTargets({
		draggedNodePathInfo: firstNodePathInfo,
		selectedItems: [
			{type: 'sequence', nodePathInfo: firstNodePathInfo},
			{type: 'sequence', nodePathInfo: secondNodePathInfo},
		],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths,
		}).tracks,
		overrideIdsToNodePaths,
		propStatuses: makeFromPropStatuses([
			firstNodePathInfo.sequenceSubscriptionKey,
			secondNodePathInfo.sequenceSubscriptionKey,
		]),
	});

	expect(targets?.map((target) => target.initialFrom)).toEqual([0, 10]);

	for (const [deltaFrames, expected] of [
		[-100, 0],
		[100, 79],
	]) {
		expect(
			getTimelineSequenceFromDragDelta({
				deltaFrames,
				timelineDurationInFrames: 90,
				pxPerFrame: 10,
				snappingEnabled: true,
				targets: targets!,
			}),
		).toBe(expected);
	}

	expect(
		getTimelineSequenceFromDragChanges({
			targets: targets ?? [],
			deltaFrames: 12,
		}).map((change) => change.value),
	).toEqual([12, 22]);
});

test('Timeline from drag only moves selected sequences without selected ancestors', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const parentNodePathInfo = makeNodePathInfo(['body', 0], []);
	const childNodePathInfo = makeNodePathInfo(['body', 0, 'children', 0], []);
	const videoNodePathInfo = makeNodePathInfo(['body', 1], []);
	const sequences = [
		makeTimelineSequence({
			schema,
			id: 'parent',
			overrideId: 'parent',
			from: 10,
		}),
		makeTimelineSequence({
			schema,
			id: 'child',
			overrideId: 'child',
			parentId: 'parent',
			from: 5,
		}),
		makeTimelineSequence({
			schema,
			id: 'video',
			overrideId: 'video',
			from: 20,
			type: 'video',
		}),
	];
	const overrideIdsToNodePaths = {
		parent: parentNodePathInfo.sequenceSubscriptionKey,
		child: childNodePathInfo.sequenceSubscriptionKey,
		video: videoNodePathInfo.sequenceSubscriptionKey,
	};
	const targets = getTimelineSequenceFromDragTargets({
		draggedNodePathInfo: parentNodePathInfo,
		selectedItems: [
			{type: 'sequence', nodePathInfo: parentNodePathInfo},
			{type: 'sequence', nodePathInfo: childNodePathInfo},
			{type: 'sequence', nodePathInfo: videoNodePathInfo},
		],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths,
		}).tracks,
		overrideIdsToNodePaths,
		propStatuses: makeFromPropStatuses([
			parentNodePathInfo.sequenceSubscriptionKey,
			childNodePathInfo.sequenceSubscriptionKey,
			videoNodePathInfo.sequenceSubscriptionKey,
		]),
	});

	expect(targets?.map((target) => target.initialFrom)).toEqual([10, 20]);
	expect(
		getTimelineSequenceFromDragChanges({
			targets: targets ?? [],
			deltaFrames: -4,
		}).map((change) => change.value),
	).toEqual([6, 16]);
});

test('Timeline from drag moves all owned sequence keyframes by the same delta', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {
		'style.translate': {type: 'translate', default: '0px 0px'},
		opacity: {type: 'number', default: 1, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const nodePathInfo = makeNodePathInfo(['body', 0], []);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const propStatuses = makeFromPropStatuses([nodePath]);
	propStatuses[Internals.makeSequencePropsSubscriptionKey(nodePath)] = {
		durationDependentKeyframes: null,
		canUpdate: true,
		props: {
			from: {
				status: 'static',
				keyframeDisplayOffsetAdjustment: null,
				codeValue: 0,
			},
			'style.translate': {
				status: 'keyframed',
				keyframeDisplayOffsetAdjustment: null,
				interpolationFunction: 'interpolate',
				keyframes: [
					{frame: 0, value: '0px 0px'},
					{frame: 40, value: '100px 50px'},
				],
				easing: [{type: 'linear'}],
				clamping: {left: 'extend', right: 'extend'},
				posterize: undefined,
				output: undefined,
			},
			opacity: {
				status: 'keyframed',
				keyframeDisplayOffsetAdjustment: null,
				interpolationFunction: 'interpolate',
				keyframes: [
					{frame: 10, value: 0},
					{frame: 20, value: 1},
				],
				easing: [{type: 'linear'}],
				clamping: {left: 'clamp', right: 'clamp'},
				posterize: undefined,
				output: undefined,
			},
		},
		effects: [],
	};
	const sequences = [
		makeTimelineSequence({
			schema,
			duration: 40,
			from: 0,
		}),
	];
	const overrideIdsToNodePaths = {override: nodePath};
	const targets = getTimelineSequenceFromDragTargets({
		draggedNodePathInfo: nodePathInfo,
		selectedItems: [{type: 'sequence', nodePathInfo}],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths,
		}).tracks,
		overrideIdsToNodePaths,
		propStatuses,
	});

	expect(
		getTimelineSequenceFromDragKeyframeMoves({
			targets: targets ?? [],
			deltaFrames: 12,
		}).sequenceKeyframes.map((keyframe) => [
			keyframe.fieldKey,
			keyframe.fromFrame,
			keyframe.toFrame,
		]),
	).toEqual([
		['style.translate', 0, 12],
		['style.translate', 40, 52],
		['opacity', 10, 22],
		['opacity', 20, 32],
	]);
});

test('Timeline from drag moves only descendant keyframes tied to an outer frame clock', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const parentNodePathInfo = makeNodePathInfo(['body', 0], []);
	const parentNodePath = parentNodePathInfo.sequenceSubscriptionKey;
	const outerFrameDescendantNodePath = makeNodePathInfo(
		['body', 0, 'children', 0],
		[],
	).sequenceSubscriptionKey;
	const localFrameDescendantNodePath = makeNodePathInfo(
		['body', 1],
		[],
	).sequenceSubscriptionKey;
	const propStatuses = makeFromPropStatuses([parentNodePath]);
	const keyframedColorStatus = {
		status: 'keyframed' as const,
		interpolationFunction: 'interpolateColors' as const,
		keyframes: [
			{frame: 0, value: '#0b84f3'},
			{frame: 100, value: '#f43b00'},
		],
		easing: [{type: 'linear' as const}],
		clamping: {left: 'extend' as const, right: 'extend' as const},
		posterize: undefined,
		output: undefined,
	};
	propStatuses[
		Internals.makeSequencePropsSubscriptionKey(outerFrameDescendantNodePath)
	] = {
		durationDependentKeyframes: null,
		canUpdate: true,
		props: {
			color: {
				...keyframedColorStatus,
				keyframeDisplayOffsetAdjustment: 0,
			},
		},
		effects: [],
	};
	propStatuses[
		Internals.makeSequencePropsSubscriptionKey(localFrameDescendantNodePath)
	] = {
		durationDependentKeyframes: null,
		canUpdate: true,
		props: {
			color: {
				...keyframedColorStatus,
				keyframeDisplayOffsetAdjustment: null,
			},
		},
		effects: [],
	};

	const sequences = [
		makeTimelineSequence({
			schema: {},
			id: 'parent',
			overrideId: 'parent-override',
		}),
		makeTimelineSequence({
			schema: {color: {type: 'color', default: '#000000'}},
			id: 'outer-frame-descendant',
			overrideId: 'outer-frame-descendant-override',
			parentId: 'parent',
		}),
		makeTimelineSequence({
			schema: {color: {type: 'color', default: '#000000'}},
			id: 'local-frame-descendant',
			overrideId: 'local-frame-descendant-override',
			parentId: 'parent',
		}),
	];
	const overrideIdsToNodePaths = {
		'parent-override': parentNodePath,
		'outer-frame-descendant-override': outerFrameDescendantNodePath,
		'local-frame-descendant-override': localFrameDescendantNodePath,
	};
	const targets = getTimelineSequenceFromDragTargets({
		draggedNodePathInfo: parentNodePathInfo,
		selectedItems: [{type: 'sequence', nodePathInfo: parentNodePathInfo}],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths,
		}).tracks,
		overrideIdsToNodePaths,
		propStatuses,
	});

	expect(
		getTimelineSequenceFromDragKeyframeMoves({
			targets: targets ?? [],
			deltaFrames: 10,
		}).sequenceKeyframes.map((keyframe) => ({
			nodePath: keyframe.nodePath,
			fromFrame: keyframe.fromFrame,
			toFrame: keyframe.toFrame,
			keyframeDisplayOffsetAdjustmentDelta:
				keyframe.keyframeDisplayOffsetAdjustmentDelta,
		})),
	).toEqual([
		{
			nodePath: outerFrameDescendantNodePath,
			fromFrame: 0,
			toFrame: 10,
			keyframeDisplayOffsetAdjustmentDelta: -10,
		},
		{
			nodePath: outerFrameDescendantNodePath,
			fromFrame: 100,
			toFrame: 110,
			keyframeDisplayOffsetAdjustmentDelta: -10,
		},
	]);
});

test('Timeline from drag moves owned effect keyframes by the same delta', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const effectSchema = {
		intensity: {type: 'number', default: 0, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const nodePathInfo = makeNodePathInfo(['body', 0], [], true, [
		['0', 'intensity'],
	]);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const propStatuses = makeFromPropStatuses([nodePath]);
	propStatuses[Internals.makeSequencePropsSubscriptionKey(nodePath)] = {
		durationDependentKeyframes: null,
		canUpdate: true,
		props: {
			from: {
				status: 'static',
				keyframeDisplayOffsetAdjustment: null,
				codeValue: 0,
			},
		},
		effects: [
			{
				canUpdate: true,
				callee: 'halftone',
				importPath: '@remotion/effects/halftone',
				effectIndex: 0,
				props: {
					intensity: {
						status: 'keyframed',
						keyframeDisplayOffsetAdjustment: null,
						interpolationFunction: 'interpolate',
						keyframes: [
							{frame: 5, value: 10},
							{frame: 25, value: 20},
						],
						easing: [{type: 'linear'}],
						clamping: {left: 'clamp', right: 'clamp'},
						posterize: undefined,
						output: undefined,
					},
				},
			},
		],
	};
	const sequences = [
		makeTimelineSequence({
			schema,
			effects: [{schema: effectSchema}],
		}),
	];
	const overrideIdsToNodePaths = {override: nodePath};
	const targets = getTimelineSequenceFromDragTargets({
		draggedNodePathInfo: nodePathInfo,
		selectedItems: [{type: 'sequence', nodePathInfo}],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths,
		}).tracks,
		overrideIdsToNodePaths,
		propStatuses,
	});

	expect(
		getTimelineSequenceFromDragKeyframeMoves({
			targets: targets ?? [],
			deltaFrames: -7,
		}).effectKeyframes.map((keyframe) => [
			keyframe.effectIndex,
			keyframe.fieldKey,
			keyframe.fromFrame,
			keyframe.toFrame,
		]),
	).toEqual([
		[0, 'intensity', 5, -2],
		[0, 'intensity', 25, 18],
	]);
});

test('Timeline from drag supports negative offsets', () => {
	expect(
		getTimelineSequenceFromDragValue({
			initialFrom: 4,
			deltaFrames: -10,
		}),
	).toBe(-6);
});

test('Timeline from drag snaps a root sequence to frame 0', () => {
	const nodePath = makeNodePathInfo(['body', 0], []).sequenceSubscriptionKey;
	const target = {
		parentPlaybackRate: 1,
		canSnapToTimelineStart: true,
		minimumDeltaFrames: -100,
		maximumDeltaFrames: Infinity,
		initialTimelineStart: 8,
		effectKeyframes: [],
		fileName: nodePath.absolutePath,
		initialFrom: 8,
		nodePath,
		sequenceKeyframes: [],
	};
	const pxPerFrame = timelineSequenceFromDragSnapThresholdPx / 2;

	expect(
		getTimelineSequenceFromDragDelta({
			timelineDurationInFrames: 90,
			deltaFrames: -6,
			pxPerFrame,
			snappingEnabled: true,
			targets: [target],
		}),
	).toBe(-8);
	expect(
		getTimelineSequenceFromDragDelta({
			timelineDurationInFrames: 90,
			deltaFrames: -6,
			pxPerFrame,
			snappingEnabled: false,
			targets: [target],
		}),
	).toBe(-6);
	expect(
		getTimelineSequenceFromDragDelta({
			timelineDurationInFrames: 90,
			deltaFrames: -5,
			pxPerFrame,
			snappingEnabled: true,
			targets: [target],
		}),
	).toBe(-5);
});

test('Timeline from drag does not snap nested sequences to the timeline start', () => {
	const nodePath = makeNodePathInfo(['body', 0], []).sequenceSubscriptionKey;

	expect(
		getTimelineSequenceFromDragDelta({
			timelineDurationInFrames: 90,
			deltaFrames: -6,
			pxPerFrame: timelineSequenceFromDragSnapThresholdPx / 2,
			snappingEnabled: true,
			targets: [
				{
					parentPlaybackRate: 1,
					canSnapToTimelineStart: false,
					minimumDeltaFrames: -100,
					maximumDeltaFrames: Infinity,
					initialTimelineStart: 8,
					effectKeyframes: [],
					fileName: nodePath.absolutePath,
					initialFrom: 8,
					nodePath,
					sequenceKeyframes: [],
				},
			],
		}),
	).toBe(-6);
});

test('Timeline from drag saves relative from for nested sequences', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const childNodePathInfo = makeNodePathInfo(['body', 1], []);
	const sequences = [
		makeTimelineSequence({
			schema,
			id: 'parent',
			overrideId: 'parent',
			duration: 100,
			from: 50,
		}),
		makeTimelineSequence({
			schema,
			id: 'child',
			overrideId: 'child',
			duration: 20,
			from: 10,
			parentId: 'parent',
		}),
	];
	const overrideIdsToNodePaths = {
		child: childNodePathInfo.sequenceSubscriptionKey,
	};
	const targets = getTimelineSequenceFromDragTargets({
		draggedNodePathInfo: childNodePathInfo,
		selectedItems: [{type: 'sequence', nodePathInfo: childNodePathInfo}],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths,
		}).tracks,
		overrideIdsToNodePaths,
		propStatuses: makeFromPropStatuses([
			childNodePathInfo.sequenceSubscriptionKey,
		]),
	});

	expect(targets?.map((target) => target.initialFrom)).toEqual([10]);
	expect(
		getTimelineSequenceFromDragChanges({
			targets: targets ?? [],
			deltaFrames: 5,
		}).map((change) => change.value),
	).toEqual([15]);
});

test('Timeline from drag is blocked if one selected sequence cannot update from', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const firstNodePathInfo = makeNodePathInfo(['body', 0], []);
	const secondNodePathInfo = makeNodePathInfo(['body', 1], []);
	const propStatuses = makeFromPropStatuses([
		firstNodePathInfo.sequenceSubscriptionKey,
	]);

	propStatuses[
		Internals.makeSequencePropsSubscriptionKey(
			secondNodePathInfo.sequenceSubscriptionKey,
		)
	] = {
		durationDependentKeyframes: null,
		canUpdate: true,
		props: {},
		effects: [],
	};

	const sequences = [
		makeTimelineSequence({
			schema,
			id: 'first',
			overrideId: 'first',
			duration: 40,
		}),
		makeTimelineSequence({
			schema,
			id: 'second',
			overrideId: 'second',
			duration: 15,
			from: 10,
		}),
	];
	const overrideIdsToNodePaths = {
		first: firstNodePathInfo.sequenceSubscriptionKey,
		second: secondNodePathInfo.sequenceSubscriptionKey,
	};
	expect(
		getTimelineSequenceFromDragTargets({
			draggedNodePathInfo: firstNodePathInfo,
			selectedItems: [
				{type: 'sequence', nodePathInfo: firstNodePathInfo},
				{type: 'sequence', nodePathInfo: secondNodePathInfo},
			],
			sequences,
			tracks: calculate({
				sequences,
				overrideIdsToNodePaths,
			}).tracks,
			overrideIdsToNodePaths,
			propStatuses,
		}),
	).toBe(null);
});

test('Timeline from drag removes the prop at the default value', () => {
	const nodePathInfo = makeNodePathInfo(['body', 0], []);
	const [change] = getTimelineSequenceFromDragChanges({
		targets: [
			{
				parentPlaybackRate: 1,
				canSnapToTimelineStart: true,
				minimumDeltaFrames: -100,
				maximumDeltaFrames: Infinity,
				initialTimelineStart: 8,
				effectKeyframes: [],
				fileName: nodePathInfo.sequenceSubscriptionKey.absolutePath,
				initialFrom: 5,
				nodePath: nodePathInfo.sequenceSubscriptionKey,
				sequenceKeyframes: [],
			},
		],
		deltaFrames: -5,
	});

	expect(change).toEqual({
		fileName: nodePathInfo.sequenceSubscriptionKey.absolutePath,
		nodePath: nodePathInfo.sequenceSubscriptionKey,
		fieldKey: 'from',
		value: 0,
		defaultValue: '0',
		schema: NoReactInternals.sequenceSchema,
	});
});

test('Timeline colors use the Studio palette', () => {
	expect(TIMELINE_BACKGROUND).toBe(
		'var(--remotion-studio-timeline-background-color)',
	);
	expect(TIMELINE_TICKS_BACKGROUND).not.toBe(TIMELINE_BACKGROUND);
});

test('Timeline hover highlight is weaker than selection', () => {
	expect(
		getTimelineRowHighlightBackground({
			showSelectedBackground: true,
			selected: false,
			containsSelection: false,
			hovered: true,
			selectedBackground: TIMELINE_SELECTED_BACKGROUND,
		}),
	).toBe(TIMELINE_HOVER_BACKGROUND);
	expect(
		getTimelineRowHighlightBackground({
			showSelectedBackground: true,
			selected: true,
			containsSelection: false,
			hovered: true,
			selectedBackground: TIMELINE_SELECTED_BACKGROUND,
		}),
	).toBe(TIMELINE_SELECTED_BACKGROUND);
	expect(
		getTimelineRowHighlightBackground({
			showSelectedBackground: true,
			selected: true,
			containsSelection: false,
			hovered: false,
			selectedBackground: TIMELINE_EXPANDED_SELECTED_BACKGROUND,
		}),
	).toBe(TIMELINE_EXPANDED_SELECTED_BACKGROUND);
});

test('Timeline outlines visibility is enabled by default and persisted', () => {
	withMockLocalStorage(() => {
		expect(loadEditorShowOutlinesOption()).toBe(true);

		persistEditorShowOutlinesOption(false);
		expect(loadEditorShowOutlinesOption()).toBe(false);

		persistEditorShowOutlinesOption(true);
		expect(loadEditorShowOutlinesOption()).toBe(true);
	});
});

test('Canvas outline selection uses conventional modifier keys', () => {
	expect(
		getOutlineSelectionInteraction({
			shiftKey: true,
			metaKey: false,
			ctrlKey: false,
		}),
	).toEqual({shiftKey: true, toggleKey: false});
	expect(
		getOutlineSelectionInteraction({
			shiftKey: false,
			metaKey: true,
			ctrlKey: false,
		}),
	).toEqual({shiftKey: false, toggleKey: true});
	expect(
		getOutlineSelectionInteraction({
			shiftKey: false,
			metaKey: false,
			ctrlKey: true,
		}),
	).toEqual({shiftKey: false, toggleKey: true});
});

test('Sequence double-click opens one connected composition before its source', () => {
	expect(
		getSequenceDoubleClickAction({
			button: 0,
			canOpenSource: true,
			numberOfConnectedCompositions: 1,
			sequenceWasDragged: false,
		}),
	).toBe('open-connected-composition');
	expect(
		getSequenceDoubleClickAction({
			button: 0,
			canOpenSource: false,
			numberOfConnectedCompositions: 1,
			sequenceWasDragged: false,
		}),
	).toBe('open-connected-composition');
	expect(
		getSequenceDoubleClickAction({
			button: 0,
			canOpenSource: true,
			numberOfConnectedCompositions: 0,
			sequenceWasDragged: false,
		}),
	).toBe('open-source');
	expect(
		getSequenceDoubleClickAction({
			button: 0,
			canOpenSource: true,
			numberOfConnectedCompositions: 2,
			sequenceWasDragged: false,
		}),
	).toBe('open-source');
	expect(
		getSequenceDoubleClickAction({
			button: 0,
			canOpenSource: false,
			numberOfConnectedCompositions: 2,
			sequenceWasDragged: false,
		}),
	).toBeNull();
	expect(
		getSequenceDoubleClickAction({
			button: 2,
			canOpenSource: true,
			numberOfConnectedCompositions: 1,
			sequenceWasDragged: false,
		}),
	).toBeNull();
});

test('Sequence double-click does nothing when the second press dragged the sequence', () => {
	expect(
		getSequenceDoubleClickAction({
			button: 0,
			canOpenSource: true,
			numberOfConnectedCompositions: 1,
			sequenceWasDragged: true,
		}),
	).toBeNull();
	expect(
		getSequenceDoubleClickAction({
			button: 0,
			canOpenSource: true,
			numberOfConnectedCompositions: 0,
			sequenceWasDragged: true,
		}),
	).toBeNull();
});

test('Connected composition double-click uses the visible sequence frame', () => {
	const sequence = makeTimelineSequence({
		schema: Internals.baseSchema,
		from: 20,
		duration: 40,
	});

	expect(
		getConnectedCompositionFrame({
			timelinePosition: 35,
			sequence,
			sequenceFrameOffset: 5,
		}),
	).toBe(20);
	expect(
		getConnectedCompositionFrame({
			timelinePosition: 19,
			sequence,
			sequenceFrameOffset: 5,
		}),
	).toBeNull();
	expect(
		getConnectedCompositionFrame({
			timelinePosition: 60,
			sequence,
			sequenceFrameOffset: 5,
		}),
	).toBeNull();
});

test('Connected composition double-click respects frozen frames', () => {
	const sequence = {
		...makeTimelineSequence({
			schema: Internals.baseSchema,
			from: 20,
			duration: 40,
		}),
		frozenFrame: 12,
	} satisfies TSequence;

	expect(
		getConnectedCompositionFrame({
			timelinePosition: 35,
			sequence,
			sequenceFrameOffset: 5,
		}),
	).toBe(12);
});

test('Canvas outline hit targets render nested sequences above parents', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const refForOutline = {current: null};
	const parentNodePathInfo = makeNodePathInfo(['body', 0], []);
	const childNodePathInfo = makeNodePathInfo(['body', 0, 'children', 0], []);
	const outlines = CanvasInternals.getCanvasSelectableOutlines({
		timelinePosition: 0,
		tracks: calculate({
			sequences: [
				makeTimelineSequence({
					schema,
					id: 'parent',
					overrideId: 'parent',
					refForOutline,
				}),
				makeTimelineSequence({
					schema,
					id: 'child',
					overrideId: 'child',
					parentId: 'parent',
					refForOutline,
				}),
			],
			overrideIdsToNodePaths: {
				parent: parentNodePathInfo.sequenceSubscriptionKey,
				child: childNodePathInfo.sequenceSubscriptionKey,
			},
		}).tracks,
		resolveSequenceNodePathInfo: null,
	});

	expect(outlines.map((outline) => outline.key)).toEqual([
		getTimelineSequenceSelectionKey(parentNodePathInfo),
		getTimelineSequenceSelectionKey(childNodePathInfo),
	]);
});

test('Canvas outlines exclude descendants of connected compositions', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const ConnectedChild = () => null;
	const schema = {} satisfies InteractivitySchema;
	const refForOutline = {current: null};
	const connectedNodePathInfo = makeNodePathInfo(['body', 0], []);
	const childNodePathInfo = makeNodePathInfo(['body', 0, 'children', 0], []);
	const grandchildNodePathInfo = makeNodePathInfo(
		['body', 0, 'children', 0, 'children', 0],
		[],
	);
	const siblingNodePathInfo = makeNodePathInfo(['body', 1], []);
	const connectedComposition = {
		componentFromProps: ConnectedChild,
	} as unknown as _InternalTypes['AnyComposition'];
	const outlines = CanvasInternals.getCanvasSelectableOutlines({
		timelinePosition: 0,
		tracks: calculate({
			compositions: [connectedComposition],
			sequences: [
				makeTimelineSequence({
					schema,
					id: 'connected',
					overrideId: 'connected',
					refForOutline,
					singleChildComponent: ConnectedChild,
				}),
				makeTimelineSequence({
					schema,
					id: 'child',
					overrideId: 'child',
					parentId: 'connected',
					refForOutline,
				}),
				makeTimelineSequence({
					schema,
					id: 'grandchild',
					overrideId: 'grandchild',
					parentId: 'child',
					refForOutline,
				}),
				makeTimelineSequence({
					schema,
					id: 'sibling',
					overrideId: 'sibling',
					refForOutline,
				}),
			],
			overrideIdsToNodePaths: {
				connected: connectedNodePathInfo.sequenceSubscriptionKey,
				child: childNodePathInfo.sequenceSubscriptionKey,
				grandchild: grandchildNodePathInfo.sequenceSubscriptionKey,
				sibling: siblingNodePathInfo.sequenceSubscriptionKey,
			},
		}).tracks,
		resolveSequenceNodePathInfo: null,
	});

	expect(outlines.map((outline) => outline.key)).toEqual([
		getTimelineSequenceSelectionKey(connectedNodePathInfo),
		getTimelineSequenceSelectionKey(siblingNodePathInfo),
	]);
});

test('Canvas outline rendering preserves unconstrained outline order', () => {
	const makeOutline = (key: string): SelectedOutline => ({
		key,
		dimensions: null,
		uncroppedPoints: null,
		path: null,
		points: [
			{x: 0, y: 0},
			{x: 10, y: 0},
			{x: 10, y: 10},
			{x: 0, y: 10},
		],
	});
	const makeTarget = (selected: boolean): SelectedOutlineTarget =>
		({selected}) as SelectedOutlineTarget;

	const orderedOutlines = orderOutlinesForRendering({
		outlines: [
			makeOutline('parent'),
			makeOutline('child'),
			makeOutline('sibling'),
		],
		sequences: [],
		targetsByKey: new Map([
			['parent', makeTarget(true)],
			['child', makeTarget(false)],
			['sibling', makeTarget(false)],
		]),
	});

	expect(orderedOutlines.map((outline) => outline.key)).toEqual([
		'parent',
		'child',
		'sibling',
	]);
});

const makeTestOutline = ({
	key,
	left,
	top,
	width,
	height,
}: {
	readonly key: string;
	readonly left: number;
	readonly top: number;
	readonly width: number;
	readonly height: number;
}): SelectedOutline => ({
	key,
	dimensions: {width, height},
	uncroppedPoints: null,
	path: null,
	points: [
		{x: left, y: top},
		{x: left + width, y: top},
		{x: left + width, y: top + height},
		{x: left, y: top + height},
	],
});

const makeOutlineTarget = ({
	id,
	parent,
	selected = false,
}: {
	readonly id: string;
	readonly parent: string | null;
	readonly selected?: boolean;
}): SelectedOutlineTarget =>
	({
		selected,
		sequence: {id, parent},
	}) as SelectedOutlineTarget;

const makeOutlineSequence = ({
	id,
	parent,
}: {
	readonly id: string;
	readonly parent: string | null;
}) => ({id, parent});

test('Canvas outline rendering measures each disjoint hit target only once', () => {
	let pointReads = 0;
	const outlines = Array.from({length: 1000}, (_, index): SelectedOutline => {
		const outline = makeTestOutline({
			key: String(index),
			left: (index % 40) * 20,
			top: Math.floor(index / 40) * 20,
			width: 10,
			height: 10,
		});
		return {
			...outline,
			get points() {
				pointReads++;
				return outline.points;
			},
		};
	});
	const orderedOutlines = orderOutlinesForRendering({
		outlines,
		sequences: outlines.map(({key}) =>
			makeOutlineSequence({id: key, parent: null}),
		),
		targetsByKey: new Map(
			outlines.map(({key}) => [
				key,
				makeOutlineTarget({id: key, parent: null}),
			]),
		),
	});

	expect(orderedOutlines.map(({key}) => key)).toEqual(
		outlines.map(({key}) => key),
	);
	expect(pointReads).toBeLessThanOrEqual(outlines.length * 32);
});

test('Canvas outline rendering preserves overlap and area tolerances', () => {
	const sequences = [
		makeOutlineSequence({id: 'small', parent: null}),
		makeOutlineSequence({id: 'large', parent: null}),
	];
	const targetsByKey = new Map(
		sequences.map(({id, parent}) => [id, makeOutlineTarget({id, parent})]),
	);

	for (const axis of ['left', 'top'] as const) {
		for (const gap of [0.5, 0.51]) {
			const outlines = [
				makeTestOutline({
					key: 'small',
					left: axis === 'left' ? 20 + gap : 0,
					top: axis === 'top' ? 20 + gap : 0,
					width: 10,
					height: 10,
				}),
				makeTestOutline({
					key: 'large',
					left: 0,
					top: 0,
					width: 20,
					height: 20,
				}),
			];

			expect(
				orderOutlinesForRendering({
					outlines,
					sequences,
					targetsByKey,
				}).map(({key}) => key),
			).toEqual(gap === 0.5 ? ['large', 'small'] : ['small', 'large']);
		}
	}

	for (const extraWidth of [0.05, 0.051]) {
		const outlines = [
			makeTestOutline({
				key: 'small',
				left: 0,
				top: 0,
				width: 10,
				height: 10,
			}),
			makeTestOutline({
				key: 'large',
				left: 0,
				top: 0,
				width: 10 + extraWidth,
				height: 10,
			}),
		];

		expect(
			orderOutlinesForRendering({
				outlines,
				sequences,
				targetsByKey,
			}).map(({key}) => key),
		).toEqual(extraWidth === 0.05 ? ['small', 'large'] : ['large', 'small']);
	}
});

test('Canvas outline rendering keeps smaller nested hit targets above parents', () => {
	const orderedOutlines = orderOutlinesForRendering({
		outlines: [
			makeTestOutline({
				key: 'parent',
				left: 0,
				top: 0,
				width: 100,
				height: 100,
			}),
			makeTestOutline({
				key: 'child',
				left: 25,
				top: 25,
				width: 50,
				height: 50,
			}),
		],
		sequences: [
			makeOutlineSequence({id: 'parent', parent: null}),
			makeOutlineSequence({id: 'child', parent: 'parent'}),
		],
		targetsByKey: new Map([
			['parent', makeOutlineTarget({id: 'parent', parent: null})],
			['child', makeOutlineTarget({id: 'child', parent: 'parent'})],
		]),
	});

	expect(orderedOutlines.map((outline) => outline.key)).toEqual([
		'parent',
		'child',
	]);
});

test('Canvas outline rendering puts equal-area child hit targets below parents', () => {
	const orderedOutlines = orderOutlinesForRendering({
		outlines: [
			makeTestOutline({
				key: 'parent',
				left: 0,
				top: 0,
				width: 100,
				height: 100,
			}),
			makeTestOutline({
				key: 'container',
				left: 0,
				top: 0,
				width: 100,
				height: 100,
			}),
			makeTestOutline({
				key: 'label',
				left: 25,
				top: 25,
				width: 50,
				height: 50,
			}),
		],
		sequences: [
			makeOutlineSequence({id: 'parent', parent: null}),
			makeOutlineSequence({id: 'container', parent: 'parent'}),
			makeOutlineSequence({id: 'label', parent: 'container'}),
		],
		targetsByKey: new Map([
			['parent', makeOutlineTarget({id: 'parent', parent: null})],
			['container', makeOutlineTarget({id: 'container', parent: 'parent'})],
			['label', makeOutlineTarget({id: 'label', parent: 'container'})],
		]),
	});

	expect(orderedOutlines.map((outline) => outline.key)).toEqual([
		'container',
		'parent',
		'label',
	]);
});

test('Canvas outline rendering keeps equal-area children containing selection above parents', () => {
	const outlines = [
		makeTestOutline({
			key: 'parent',
			left: 0,
			top: 0,
			width: 100,
			height: 100,
		}),
		makeTestOutline({
			key: 'child',
			left: 0,
			top: 0,
			width: 100,
			height: 100,
		}),
	];
	const sequences = [
		makeOutlineSequence({id: 'parent', parent: null}),
		makeOutlineSequence({id: 'child', parent: 'parent'}),
	];

	for (const selectionState of [{selected: true}, {containsSelection: true}]) {
		const orderedOutlines = orderOutlinesForRendering({
			outlines,
			sequences,
			targetsByKey: new Map([
				['parent', makeOutlineTarget({id: 'parent', parent: null})],
				[
					'child',
					{
						...makeOutlineTarget({id: 'child', parent: 'parent'}),
						...selectionState,
					},
				],
			]),
		});

		expect(orderedOutlines.map((outline) => outline.key)).toEqual([
			'parent',
			'child',
		]);
	}
});

test('Canvas outline rendering handles transitive subpixel equivalence', () => {
	const orderedOutlines = orderOutlinesForRendering({
		outlines: [
			makeTestOutline({
				key: 'parent',
				left: 0,
				top: 0,
				width: 100,
				height: 100,
			}),
			makeTestOutline({
				key: 'child',
				left: 0.4,
				top: 0.4,
				width: 100,
				height: 100,
			}),
			makeTestOutline({
				key: 'grandchild',
				left: 0.8,
				top: 0.8,
				width: 100,
				height: 100,
			}),
		],
		sequences: [
			makeOutlineSequence({id: 'parent', parent: null}),
			makeOutlineSequence({id: 'child', parent: 'parent'}),
			makeOutlineSequence({id: 'grandchild', parent: 'child'}),
		],
		targetsByKey: new Map([
			['parent', makeOutlineTarget({id: 'parent', parent: null})],
			['child', makeOutlineTarget({id: 'child', parent: 'parent'})],
			['grandchild', makeOutlineTarget({id: 'grandchild', parent: 'child'})],
		]),
	});

	expect(orderedOutlines.map((outline) => outline.key)).toEqual([
		'grandchild',
		'child',
		'parent',
	]);
});

test('Canvas outline equivalence ignores polygon start vertex and winding', () => {
	const parent = makeTestOutline({
		key: 'parent',
		left: 0,
		top: 0,
		width: 100,
		height: 100,
	});
	const child: SelectedOutline = {
		...makeTestOutline({
			key: 'child',
			left: 0,
			top: 0,
			width: 100,
			height: 100,
		}),
		points: [
			{x: 100, y: 100},
			{x: 100, y: 0},
			{x: 0, y: 0},
			{x: 0, y: 100},
		],
	};
	const orderedOutlines = orderOutlinesForRendering({
		outlines: [parent, child],
		sequences: [
			makeOutlineSequence({id: 'parent', parent: null}),
			makeOutlineSequence({id: 'child', parent: 'parent'}),
		],
		targetsByKey: new Map([
			['parent', makeOutlineTarget({id: 'parent', parent: null})],
			['child', makeOutlineTarget({id: 'child', parent: 'parent'})],
		]),
	});

	expect(orderedOutlines.map((outline) => outline.key)).toEqual([
		'child',
		'parent',
	]);
});

test('Canvas outline rendering keeps lower-third wrappers reachable below larger overlapping sequences', () => {
	const orderedOutlines = orderOutlinesForRendering({
		outlines: [
			makeTestOutline({
				key: 'lower-third-wrapper',
				left: 200,
				top: 854,
				width: 680,
				height: 138,
			}),
			makeTestOutline({
				key: 'overlapping-keyframed-sequence',
				left: 100,
				top: 700,
				width: 1000,
				height: 400,
			}),
			makeTestOutline({
				key: 'lower-third-container',
				left: 200,
				top: 854,
				width: 680,
				height: 138,
			}),
		],
		sequences: [
			makeOutlineSequence({id: 'lower-third-wrapper', parent: null}),
			makeOutlineSequence({
				id: 'overlapping-keyframed-sequence',
				parent: null,
			}),
			makeOutlineSequence({
				id: 'lower-third-container',
				parent: 'lower-third-wrapper',
			}),
		],
		targetsByKey: new Map([
			[
				'lower-third-wrapper',
				makeOutlineTarget({id: 'lower-third-wrapper', parent: null}),
			],
			[
				'overlapping-keyframed-sequence',
				makeOutlineTarget({
					id: 'overlapping-keyframed-sequence',
					parent: null,
					selected: true,
				}),
			],
			[
				'lower-third-container',
				makeOutlineTarget({
					id: 'lower-third-container',
					parent: 'lower-third-wrapper',
					selected: true,
				}),
			],
		]),
	});

	expect(orderedOutlines.map((outline) => outline.key)).toEqual([
		'overlapping-keyframed-sequence',
		'lower-third-wrapper',
		'lower-third-container',
	]);
});

test('Canvas outline rendering orders equal-area children deterministically with unrelated outlines', () => {
	const orderedOutlines = orderOutlinesForRendering({
		outlines: [
			makeTestOutline({
				key: 'lower-third-wrapper',
				left: 0,
				top: 0,
				width: 100,
				height: 100,
			}),
			makeTestOutline({
				key: 'unrelated-keyframed-sequence',
				left: 200,
				top: 200,
				width: 100,
				height: 100,
			}),
			makeTestOutline({
				key: 'lower-third-container',
				left: 0,
				top: 0,
				width: 100,
				height: 100,
			}),
			makeTestOutline({
				key: 'lower-third-title',
				left: 25,
				top: 25,
				width: 50,
				height: 50,
			}),
		],
		sequences: [
			makeOutlineSequence({id: 'lower-third-wrapper', parent: null}),
			makeOutlineSequence({id: 'unrelated-keyframed-sequence', parent: null}),
			makeOutlineSequence({
				id: 'lower-third-container',
				parent: 'lower-third-wrapper',
			}),
			makeOutlineSequence({
				id: 'lower-third-title',
				parent: 'lower-third-container',
			}),
		],
		targetsByKey: new Map([
			[
				'lower-third-wrapper',
				makeOutlineTarget({id: 'lower-third-wrapper', parent: null}),
			],
			[
				'unrelated-keyframed-sequence',
				makeOutlineTarget({id: 'unrelated-keyframed-sequence', parent: null}),
			],
			[
				'lower-third-container',
				makeOutlineTarget({
					id: 'lower-third-container',
					parent: 'lower-third-wrapper',
				}),
			],
			[
				'lower-third-title',
				makeOutlineTarget({
					id: 'lower-third-title',
					parent: 'lower-third-container',
				}),
			],
		]),
	});

	expect(orderedOutlines.map((outline) => outline.key)).toEqual([
		'lower-third-container',
		'lower-third-wrapper',
		'unrelated-keyframed-sequence',
		'lower-third-title',
	]);
});

test('Canvas outline rendering follows hidden intermediate ancestors', () => {
	const orderedOutlines = orderOutlinesForRendering({
		outlines: [
			makeTestOutline({
				key: 'visible-parent',
				left: 0,
				top: 0,
				width: 100,
				height: 100,
			}),
			makeTestOutline({
				key: 'visible-child',
				left: 0,
				top: 0,
				width: 100,
				height: 100,
			}),
		],
		sequences: [
			makeOutlineSequence({id: 'visible-parent', parent: null}),
			makeOutlineSequence({id: 'hidden-wrapper', parent: 'visible-parent'}),
			makeOutlineSequence({id: 'visible-child', parent: 'hidden-wrapper'}),
		],
		targetsByKey: new Map([
			[
				'visible-parent',
				makeOutlineTarget({id: 'visible-parent', parent: null}),
			],
			[
				'visible-child',
				makeOutlineTarget({id: 'visible-child', parent: 'hidden-wrapper'}),
			],
		]),
	});

	expect(orderedOutlines.map((outline) => outline.key)).toEqual([
		'visible-child',
		'visible-parent',
	]);
});

test('Canvas outline hit targets exclude sequences hidden from the timeline', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const refForOutline = {current: null};
	const hiddenNodePathInfo = makeNodePathInfo(['body', 0], []);
	const visibleNodePathInfo = makeNodePathInfo(['body', 1], []);
	const childNodePathInfo = makeNodePathInfo(['body', 0, 'children', 0], []);
	const outlines = CanvasInternals.getCanvasSelectableOutlines({
		timelinePosition: 0,
		tracks: calculate({
			sequences: [
				makeTimelineSequence({
					schema,
					id: 'hidden',
					overrideId: 'hidden',
					refForOutline,
					showInTimeline: false,
				}),
				makeTimelineSequence({
					schema,
					id: 'visible',
					overrideId: 'visible',
					refForOutline,
				}),
				makeTimelineSequence({
					schema,
					id: 'child',
					overrideId: 'child',
					parentId: 'hidden',
					refForOutline,
				}),
			],
			overrideIdsToNodePaths: {
				hidden: hiddenNodePathInfo.sequenceSubscriptionKey,
				visible: visibleNodePathInfo.sequenceSubscriptionKey,
				child: childNodePathInfo.sequenceSubscriptionKey,
			},
		}).tracks,
		resolveSequenceNodePathInfo: null,
	});

	expect(outlines.map((outline) => outline.key).sort()).toEqual(
		[
			getTimelineSequenceSelectionKey(visibleNodePathInfo),
			getTimelineSequenceSelectionKey(childNodePathInfo),
		].sort(),
	);
});

test('Canvas outline hit targets exclude premounted and postmounted sequences', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const refForOutline = {current: null};
	const premountedNodePathInfo = makeNodePathInfo(['body', 0], []);
	const activeNodePathInfo = makeNodePathInfo(['body', 1], []);
	const postmountedNodePathInfo = makeNodePathInfo(['body', 2], []);
	const outlines = CanvasInternals.getCanvasSelectableOutlines({
		timelinePosition: 10,
		tracks: calculate({
			sequences: [
				makeTimelineSequence({
					schema,
					id: 'premounted',
					overrideId: 'premounted',
					from: 20,
					duration: 10,
					premountDisplay: 10,
					refForOutline,
				}),
				makeTimelineSequence({
					schema,
					id: 'active',
					overrideId: 'active',
					from: 10,
					duration: 10,
					refForOutline,
				}),
				makeTimelineSequence({
					schema,
					id: 'postmounted',
					overrideId: 'postmounted',
					from: 0,
					duration: 10,
					postmountDisplay: 10,
					refForOutline,
				}),
			],
			overrideIdsToNodePaths: {
				premounted: premountedNodePathInfo.sequenceSubscriptionKey,
				active: activeNodePathInfo.sequenceSubscriptionKey,
				postmounted: postmountedNodePathInfo.sequenceSubscriptionKey,
			},
		}).tracks,
		resolveSequenceNodePathInfo: null,
	});

	expect(outlines.map((outline) => outline.key)).toEqual([
		getTimelineSequenceSelectionKey(activeNodePathInfo),
	]);
});

test('UV handles project semantic outline corners', () => {
	const points = [
		{x: 200, y: 200},
		{x: 100, y: 200},
		{x: 100, y: 100},
		{x: 200, y: 100},
	] as const;

	expect(getUvHandlePosition(points, [0, 0])).toEqual({x: 200, y: 200});
	expect(getUvHandlePosition(points, [1, 1])).toEqual({x: 100, y: 100});
	expect(getUvHandlePosition(points, [0.5, 0.5])).toEqual({x: 150, y: 150});
});

test('UV handles use projective projection for perspective quads', () => {
	const points = [
		{x: 0, y: 0},
		{x: 100, y: 0},
		{x: 150, y: 100},
		{x: -50, y: 100},
	] as const;

	const projectedCenter = getUvHandlePosition(points, [0.5, 0.5]);

	expect(projectedCenter.x).toBeCloseTo(50, 5);
	expect(projectedCenter.y).toBeCloseTo(100 / 3, 5);
});

test('Sequence crops are projected through perspective transforms', () => {
	const points = [
		{x: 0, y: 0},
		{x: 100, y: 0},
		{x: 150, y: 100},
		{x: -50, y: 100},
	] as const;

	const cropped = cropOutlinePoints(points, {
		left: 0.25,
		right: 0.25,
		top: 0.25,
		bottom: 0.25,
	});

	expect(cropped[0].x).toBeCloseTo(150 / 7, 5);
	expect(cropped[0].y).toBeCloseTo(100 / 7, 5);
	expect(cropped[1].x).toBeCloseTo(550 / 7, 5);
	expect(cropped[1].y).toBeCloseTo(100 / 7, 5);
	expect(cropped[2]).toEqual({x: 90, y: 60});
	expect(cropped[3]).toEqual({x: 10, y: 60});
});

test('Crop handles update normalized crop values and keep one pixel visible', () => {
	expect(
		getSelectedOutlineCropDragValues({
			crop: {
				cropLeft: 0.1,
				cropRight: 0.2,
				cropTop: 0.15,
				cropBottom: 0.25,
			},
			dimensions: {width: 100, height: 200},
			handle: 'top-right',
			uv: [0.65, 0.3],
		}),
	).toEqual({
		cropLeft: 0.1,
		cropRight: 0.35,
		cropTop: 0.3,
		cropBottom: 0.25,
	});

	expect(
		getSelectedOutlineCropDragValues({
			crop: {
				cropLeft: 0.1,
				cropRight: 0.2,
				cropTop: 0.15,
				cropBottom: 0.25,
			},
			dimensions: {width: 100, height: 200},
			handle: 'left',
			uv: [1, 0.5],
		}),
	).toEqual({
		cropLeft: 0.79,
		cropRight: 0.2,
		cropTop: 0.15,
		cropBottom: 0.25,
	});
});

test('Crop is only available when all crop fields can be edited', () => {
	const numberField = {
		type: 'number' as const,
		default: 0,
		hiddenFromList: false,
		keyframable: true,
	};
	const schema = {
		cropLeft: numberField,
		cropRight: numberField,
		cropTop: numberField,
		cropBottom: numberField,
	} satisfies InteractivitySchema;
	const propStatuses = {
		cropLeft: {
			status: 'static' as const,
			keyframeDisplayOffsetAdjustment: null,
			codeValue: 0,
		},
		cropRight: {
			status: 'static' as const,
			keyframeDisplayOffsetAdjustment: null,
			codeValue: 0,
		},
		cropTop: {
			status: 'static' as const,
			keyframeDisplayOffsetAdjustment: null,
			codeValue: 0,
		},
		cropBottom: {
			status: 'static' as const,
			keyframeDisplayOffsetAdjustment: null,
			codeValue: 0,
		},
	};

	expect(canEditSelectedOutlineCrop({schema, propStatuses})).toBe(true);
	expect(
		canEditSelectedOutlineCrop({
			schema,
			propStatuses: {
				...propStatuses,
				cropRight: {status: 'computed'},
			},
		}),
	).toBe(false);
	expect(
		canEditSelectedOutlineCrop({
			schema: {
				cropLeft: numberField,
				cropRight: numberField,
				cropTop: numberField,
			},
			propStatuses,
		}),
	).toBe(false);
});

test('Crop handle changes preserve static and keyframed field behavior', () => {
	const schema = {
		cropLeft: {
			type: 'number',
			default: 0,
			hiddenFromList: false,
			keyframable: true,
		},
		cropRight: {
			type: 'number',
			default: 0,
			hiddenFromList: false,
			keyframable: true,
		},
		cropTop: {
			type: 'number',
			default: 0,
			hiddenFromList: false,
			keyframable: true,
		},
		cropBottom: {
			type: 'number',
			default: 0,
			hiddenFromList: false,
			keyframable: true,
		},
	} satisfies InteractivitySchema;
	const nodePath = makeKey(['body', 0]);
	const staticField = (value: number) => ({
		defaultValue: 0,
		fieldSchema: schema.cropLeft,
		propStatus: {
			status: 'static' as const,
			keyframeDisplayOffsetAdjustment: null,
			codeValue: value,
		},
		value,
	});
	const target = {
		clientId: 'client',
		fields: {
			cropLeft: staticField(0.1),
			cropRight: {
				defaultValue: 0,
				fieldSchema: schema.cropRight,
				propStatus: {
					status: 'keyframed',
					keyframeDisplayOffsetAdjustment: null,
					interpolationFunction: 'interpolate',
					keyframes: [
						{frame: 0, value: 0.2},
						{frame: 20, value: 0.3},
					],
					easing: [{type: 'linear'}],
					clamping: {left: 'extend', right: 'extend'},
					posterize: undefined,
					output: undefined,
				},
				value: 0.2,
			},
			cropTop: staticField(0.15),
			cropBottom: staticField(0.25),
		},
		nodePath,
		schema,
		sourceFrame: 12,
		transformOrigin: null,
	} satisfies SelectedOutlineCropDragTarget;

	expect(
		getSelectedOutlineCropDragChanges({
			dimensions: {width: 100, height: 200},
			target,
			values: {
				cropLeft: 0.25,
				cropRight: 0.35,
				cropTop: 0.15,
				cropBottom: 0.25,
			},
		}),
	).toEqual([
		{
			type: 'static',
			fileName: '/project/src/Comp.tsx',
			nodePath,
			fieldKey: 'cropLeft',
			value: 0.25,
			defaultValue: '0',
			schema,
		},
		{
			type: 'keyframed',
			fileName: '/project/src/Comp.tsx',
			nodePath,
			fieldKey: 'cropRight',
			sourceFrame: 12,
			value: 0.35,
			schema,
		},
	]);

	expect(
		getSelectedOutlineCropDragChanges({
			dimensions: {width: 100, height: 200},
			target: {
				...target,
				transformOrigin: {
					defaultValue: '50% 50%',
					propStatus: {
						status: 'static',
						keyframeDisplayOffsetAdjustment: null,
						codeValue: undefined,
					},
					value: '50% 50%',
				},
			},
			values: {
				cropLeft: 0.2,
				cropRight: 0.2,
				cropTop: 0.15,
				cropBottom: 0.25,
			},
		}),
	).toEqual([
		{
			type: 'static',
			fileName: '/project/src/Comp.tsx',
			nodePath,
			fieldKey: 'cropLeft',
			value: 0.2,
			defaultValue: '0',
			schema,
		},
		{
			type: 'static',
			fileName: '/project/src/Comp.tsx',
			nodePath,
			fieldKey: 'style.transformOrigin',
			value: '50% 45%',
			defaultValue: '"50% 50%"',
			schema,
		},
	]);
});

test('Crop follows an absent or crop-centered static transform origin', () => {
	const initialCrop = {
		cropLeft: 0.11,
		cropRight: 0.64,
		cropTop: 0,
		cropBottom: 0.17,
	};
	const nextCrop = {...initialCrop, cropLeft: 0.21};
	const dimensions = {width: 1920, height: 1080};

	expect(
		getSelectedOutlineCropFollowingTransformOrigin({
			dimensions,
			initialCrop,
			nextCrop,
			transformOrigin: {
				defaultValue: '50% 50%',
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: undefined,
				},
				value: '50% 50%',
			},
		}),
	).toBe('28.5% 41.5%');
	expect(
		getSelectedOutlineCropFollowingTransformOrigin({
			dimensions,
			initialCrop,
			nextCrop,
			transformOrigin: {
				defaultValue: '50% 50%',
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: '23.5% 41.5% 10px',
				},
				value: '23.5% 41.5% 10px',
			},
		}),
	).toBe('28.5% 41.5% 10px');
});

test('Crop leaves custom, keyframed and computed transform origins alone', () => {
	const initialCrop = {
		cropLeft: 0.11,
		cropRight: 0.64,
		cropTop: 0,
		cropBottom: 0.17,
	};
	const base = {
		dimensions: {width: 1920, height: 1080},
		initialCrop,
		nextCrop: {...initialCrop, cropLeft: 0.21},
	};

	expect(
		getSelectedOutlineCropFollowingTransformOrigin({
			...base,
			transformOrigin: {
				defaultValue: '50% 50%',
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: '50% 50%',
				},
				value: '50% 50%',
			},
		}),
	).toBeNull();
	expect(
		getSelectedOutlineCropFollowingTransformOrigin({
			...base,
			transformOrigin: {
				defaultValue: '50% 50%',
				propStatus: {
					status: 'keyframed',
					keyframeDisplayOffsetAdjustment: null,
					interpolationFunction: 'interpolate',
					keyframes: [{frame: 0, value: '23.5% 41.5%'}],
					easing: [],
					clamping: {left: 'extend', right: 'extend'},
					posterize: undefined,
					output: undefined,
				},
				value: '23.5% 41.5%',
			},
		}),
	).toBeNull();
	expect(
		getSelectedOutlineCropFollowingTransformOrigin({
			...base,
			transformOrigin: {
				defaultValue: '50% 50%',
				propStatus: {status: 'computed'},
				value: '23.5% 41.5%',
			},
		}),
	).toBeNull();
});

test('UV handle pointer position maps back to UV coordinates', () => {
	const points = [
		{x: 20, y: 10},
		{x: 140, y: 30},
		{x: 170, y: 120},
		{x: 40, y: 100},
	] as const;
	const uv = [0.25, 0.75] as const;
	const position = getUvHandlePosition(points, uv);
	const result = getUvCoordinateForPoint(points, position);

	expect(result[0]).toBeCloseTo(uv[0], 5);
	expect(result[1]).toBeCloseTo(uv[1], 5);
});

test('Transform origin parser supports keywords, percentages, px and z', () => {
	const keyword = parseTransformOrigin('left bottom');
	expect(keyword).not.toBeNull();
	expect(
		parsedTransformOriginToUv({
			parsed: keyword!,
			width: 200,
			height: 100,
		}),
	).toEqual([0, 1]);

	const px = parseTransformOrigin('100px 25% 10px');
	expect(px).not.toBeNull();
	expect(
		parsedTransformOriginToUv({
			parsed: px!,
			width: 200,
			height: 100,
		}),
	).toEqual([0.5, 0.25]);
	expect(serializeTransformOrigin({uv: [0.5, 0.25], z: px!.z})).toBe(
		'50% 25% 10px',
	);
});

test('Transform origin parser rejects unsupported calc values', () => {
	expect(parseTransformOrigin('calc(50% + 10px) 50%')).toBeNull();
});

test('Transform origin compensation supports 2D and 3D rotation', () => {
	const next2D = compensateTranslateForTransformOrigin({
		startTranslate: [20, 30],
		deltaOrigin: [10, 5],
		rotation: {axis: [0, 0, 1], degrees: 90},
		scale: [2, 3, 1],
	});

	expect(next2D[0]).toBeCloseTo(-5, 5);
	expect(next2D[1]).toBeCloseTo(45, 5);

	const next3D = compensateTranslateForTransformOrigin({
		startTranslate: [20, 30],
		deltaOrigin: [10, 5],
		rotation: {axis: [1, 0, 0], degrees: 60},
		scale: [2, 3, 4],
	});

	expect(next3D[0]).toBeCloseTo(30, 5);
	expect(next3D[1]).toBeCloseTo(32.5, 5);
});

const makeTransformOriginDragTarget = ({
	originKeyframed,
	translateKeyframed,
}: {
	readonly originKeyframed: boolean;
	readonly translateKeyframed: boolean;
}): SelectedOutlineTransformOriginDragTarget => ({
	clientId: 'client-id',
	keyframeDisplayOffset: 0,
	keyframePlaybackRate: 1,
	nodePath: makeKey(['program', 'body', 0]),
	originDefault: '50% 50%',
	originPropStatus: originKeyframed
		? {
				status: 'keyframed',
				keyframeDisplayOffsetAdjustment: null,
				interpolationFunction: 'interpolate',
				keyframes: [
					{frame: 0, value: '50% 50%'},
					{frame: 20, value: '50% 50%'},
				],
				easing: [{type: 'linear'}],
				clamping: {left: 'extend', right: 'extend'},
				posterize: undefined,
				output: undefined,
			}
		: {
				status: 'static',
				keyframeDisplayOffsetAdjustment: null,
				codeValue: '50% 50%',
			},
	originValue: '50% 50%',
	rotateValue: '0deg',
	scaleValue: 1,
	schema: NoReactInternals.sequenceSchema,
	sourceFrame: 10,
	translateDefault: '0px 0px',
	translatePropStatus: translateKeyframed
		? {
				status: 'keyframed',
				keyframeDisplayOffsetAdjustment: null,
				interpolationFunction: 'interpolate',
				keyframes: [
					{frame: 0, value: '0px 0px'},
					{frame: 20, value: '20px 40px'},
				],
				easing: [{type: 'linear'}],
				clamping: {left: 'extend', right: 'extend'},
				posterize: undefined,
				output: undefined,
			}
		: {
				status: 'static',
				keyframeDisplayOffsetAdjustment: null,
				codeValue: '10px 20px',
			},
	translateValue: '10px 20px',
});

test('Transform origin drag keeps static properties static', () => {
	const changes = getSelectedOutlineTransformOriginDragChanges({
		target: makeTransformOriginDragTarget({
			originKeyframed: false,
			translateKeyframed: false,
		}),
		startTranslate: [10, 20, null],
		origin: '25% 75%',
		translate: '13px 24px',
	});

	expect(changes.staticChanges.map((change) => change.fieldKey)).toEqual([
		'style.transformOrigin',
		'style.translate',
	]);
	expect(changes.keyframedChanges).toEqual([]);
});

test('Transform origin drag offsets all animated translate keyframes', () => {
	const target = makeTransformOriginDragTarget({
		originKeyframed: false,
		translateKeyframed: true,
	});
	if (target.translatePropStatus.status !== 'keyframed') {
		throw new Error('Expected a keyframed translate');
	}

	const changes = getSelectedOutlineTransformOriginDragChanges({
		target: {
			...target,
			translatePropStatus: {
				...target.translatePropStatus,
				keyframes: [
					{frame: 0, value: '0px 0px 5px'},
					{frame: 20, value: '20px 40px 25px'},
				],
			},
			translateValue: '10px 20px 15px',
		},
		startTranslate: [10, 20, 15],
		origin: '25% 75%',
		translate: '13px 24px 15px',
	});

	expect(changes.staticChanges.map((change) => change.fieldKey)).toEqual([
		'style.transformOrigin',
	]);
	expect(
		changes.keyframedChanges.map(({fieldKey, sourceFrame, value}) => ({
			fieldKey,
			sourceFrame,
			value,
		})),
	).toEqual([
		{fieldKey: 'style.translate', sourceFrame: 0, value: '3px 4px 5px'},
		{fieldKey: 'style.translate', sourceFrame: 20, value: '23px 44px 25px'},
	]);
});

test('Transform origin drag does not keyframe a static translate', () => {
	const changes = getSelectedOutlineTransformOriginDragChanges({
		target: makeTransformOriginDragTarget({
			originKeyframed: true,
			translateKeyframed: false,
		}),
		startTranslate: [10, 20, null],
		origin: '25% 75%',
		translate: '13px 24px',
	});

	expect(changes.staticChanges.map((change) => change.fieldKey)).toEqual([
		'style.translate',
	]);
	expect(
		changes.keyframedChanges.map(({fieldKey, sourceFrame}) => ({
			fieldKey,
			sourceFrame,
		})),
	).toEqual([{fieldKey: 'style.transformOrigin', sourceFrame: 10}]);
});

test('Transform origin drag updates both animated properties locally', () => {
	const changes = getSelectedOutlineTransformOriginDragChanges({
		target: makeTransformOriginDragTarget({
			originKeyframed: true,
			translateKeyframed: true,
		}),
		startTranslate: [10, 20, null],
		origin: '25% 75%',
		translate: '13px 24px',
	});

	expect(changes.staticChanges).toEqual([]);
	expect(
		changes.keyframedChanges.map(({fieldKey, sourceFrame}) => ({
			fieldKey,
			sourceFrame,
		})),
	).toEqual([
		{fieldKey: 'style.transformOrigin', sourceFrame: 10},
		{fieldKey: 'style.translate', sourceFrame: 10},
	]);
});

test('Transform origin drag snaps to center, edge midpoints and corners', () => {
	const points = [
		{x: 0, y: 0},
		{x: 100, y: 0},
		{x: 100, y: 100},
		{x: 0, y: 100},
	] as const;

	expect(
		snapSelectedOutlineTransformOriginUv({
			crop: null,
			point: {x: 47, y: 53},
			points,
			thresholdPx: null,
			uv: getUvCoordinateForPoint(points, {x: 47, y: 53}),
		}),
	).toEqual([0.5, 0.5]);
	expect(
		snapSelectedOutlineTransformOriginUv({
			crop: null,
			point: {x: 52, y: 4},
			points,
			thresholdPx: null,
			uv: getUvCoordinateForPoint(points, {x: 52, y: 4}),
		}),
	).toEqual([0.5, 0]);
	expect(
		snapSelectedOutlineTransformOriginUv({
			crop: null,
			point: {x: 96, y: 49},
			points,
			thresholdPx: null,
			uv: getUvCoordinateForPoint(points, {x: 96, y: 49}),
		}),
	).toEqual([1, 0.5]);
	expect(
		snapSelectedOutlineTransformOriginUv({
			crop: null,
			point: {x: 3, y: 96},
			points,
			thresholdPx: null,
			uv: getUvCoordinateForPoint(points, {x: 3, y: 96}),
		}),
	).toEqual([0, 1]);
});

test('Transform origin drag snaps to rotated outline anchors', () => {
	const points = [
		{x: 10, y: 20},
		{x: 90, y: 50},
		{x: 70, y: 110},
		{x: -10, y: 80},
	] as const;
	const topMiddle = getUvHandlePosition(points, [0.5, 0]);
	const pointer = {x: topMiddle.x + 4, y: topMiddle.y - 3};

	expect(
		snapSelectedOutlineTransformOriginUv({
			crop: null,
			point: pointer,
			points,
			thresholdPx: null,
			uv: getUvCoordinateForPoint(points, pointer),
		}),
	).toEqual([0.5, 0]);
});

test('Transform origin drag also snaps to cropped outline anchors', () => {
	const points = [
		{x: 0, y: 0},
		{x: 100, y: 0},
		{x: 100, y: 100},
		{x: 0, y: 100},
	] as const;
	const crop = {left: 0.11, right: 0.64, top: 0, bottom: 0.17};
	const croppedSnapTargets = [
		[0.11, 0],
		[0.235, 0],
		[0.36, 0],
		[0.36, 0.415],
		[0.36, 0.83],
		[0.235, 0.83],
		[0.11, 0.83],
		[0.11, 0.415],
		[0.235, 0.415],
	] as const;

	for (const snapTarget of croppedSnapTargets) {
		const snapPoint = getUvHandlePosition(points, snapTarget);
		const pointer = {x: snapPoint.x + 2, y: snapPoint.y + 2};
		expect(
			snapSelectedOutlineTransformOriginUv({
				crop,
				point: pointer,
				points,
				thresholdPx: null,
				uv: getUvCoordinateForPoint(points, pointer),
			}),
		).toEqual(snapTarget);
	}
});

test('Transform origin drag does not snap outside the magnetic threshold', () => {
	const points = [
		{x: 0, y: 0},
		{x: 100, y: 0},
		{x: 100, y: 100},
		{x: 0, y: 100},
	] as const;
	const pointer = {
		x: 50,
		y: selectedOutlineTransformOriginSnapThresholdPx + 1,
	};
	const uv = getUvCoordinateForPoint(points, pointer);
	const snapped = snapSelectedOutlineTransformOriginUv({
		crop: null,
		point: pointer,
		points,
		thresholdPx: null,
		uv,
	});

	expect(snapped[0]).toBeCloseTo(uv[0], 5);
	expect(snapped[1]).toBeCloseTo(uv[1], 5);
});

test('UV coordinate drag snaps to outline anchors', () => {
	const points = [
		{x: 0, y: 0},
		{x: 100, y: 0},
		{x: 100, y: 100},
		{x: 0, y: 100},
	] as const;

	expect(
		snapSelectedOutlineUv({
			point: {x: 47, y: 53},
			points,
			uv: getUvCoordinateForPoint(points, {x: 47, y: 53}),
		}),
	).toEqual([0.5, 0.5]);
	expect(
		snapSelectedOutlineUv({
			point: {x: 96, y: 49},
			points,
			uv: getUvCoordinateForPoint(points, {x: 96, y: 49}),
		}),
	).toEqual([1, 0.5]);

	const pointer = {
		x: 50,
		y: selectedOutlineUvSnapThresholdPx + 1,
	};
	const uv = getUvCoordinateForPoint(points, pointer);
	const snapped = snapSelectedOutlineUv({
		point: pointer,
		points,
		uv,
	});

	expect(snapped[0]).toBeCloseTo(uv[0], 5);
	expect(snapped[1]).toBeCloseTo(uv[1], 5);
});

test('Transform origin axis locking keeps one UV axis fixed', () => {
	const dimensions = {width: 200, height: 100};
	const startUv = [0.25, 0.5] as const;
	const mostlyHorizontal = [0.5, 0.75] as const;
	const mostlyVertical = [0.35, 0.9] as const;

	expect(
		getSelectedOutlineTransformOriginLockedAxis({
			axisLocked: true,
			dimensions,
			startUv,
			uv: mostlyHorizontal,
		}),
	).toBe('x');
	expect(
		applySelectedOutlineTransformOriginAxisLock({
			lockedAxis: 'x',
			startUv,
			uv: mostlyHorizontal,
		}),
	).toEqual([0.5, 0.5]);
	expect(
		getSelectedOutlineTransformOriginLockedAxis({
			axisLocked: true,
			dimensions,
			startUv,
			uv: mostlyVertical,
		}),
	).toBe('y');
	expect(
		applySelectedOutlineTransformOriginAxisLock({
			lockedAxis: 'y',
			startUv,
			uv: mostlyVertical,
		}),
	).toEqual([0.25, 0.9]);
	expect(
		getSelectedOutlineTransformOriginLockedAxis({
			axisLocked: false,
			dimensions,
			startUv,
			uv: mostlyVertical,
		}),
	).toBeNull();
	expect(
		applySelectedOutlineTransformOriginAxisLock({
			lockedAxis: null,
			startUv,
			uv: mostlyVertical,
		}),
	).toBe(mostlyVertical);
	expect(
		getSelectedOutlineTransformOriginLockedAxis({
			axisLocked: true,
			dimensions,
			startUv,
			uv: mostlyVertical,
		}),
	).toBe('y');
});

test('UV coordinate constraints preserve precision despite schema step', () => {
	expect(
		constrainUv([0.123456, 0.987654], {
			type: 'uv-coordinate',
			default: [0.5, 0.5],
			step: 0.01,
		}),
	).toEqual([0.123456, 0.987654]);
});

test('UV coordinate constraints still clamp to schema min and max', () => {
	expect(
		constrainUv([-0.123456, 1.987654], {
			type: 'uv-coordinate',
			default: [0.5, 0.5],
			min: 0,
			max: 1,
			step: 0.01,
		}),
	).toEqual([0, 1]);
});

test('UV coordinates round to three decimals by default when dragging', () => {
	expect(
		roundUvCoordinate([0.123456, 0.987654], {
			type: 'uv-coordinate',
			default: [0.5, 0.5],
			step: 0.01,
		}),
	).toEqual([0.123, 0.988]);
});

test('UV coordinates allow finer configured steps when dragging', () => {
	expect(
		roundUvCoordinate([0.123456, 0.987654], {
			type: 'uv-coordinate',
			default: [0.5, 0.5],
			step: 0.0001,
		}),
	).toEqual([0.1235, 0.9877]);
});

test('SVG viewport outline points are projected through the screen CTM', () => {
	const points = getTransformedSvgViewportPoints({
		viewport: {x: 0, y: 0, width: 100, height: 50},
		ctm: {a: 0, b: 1, c: -1, d: 0, e: 75, f: -25},
		containerRect: {left: 10, top: 20},
	});

	expect(points).toEqual([
		{x: 65, y: -45},
		{x: 65, y: 55},
		{x: 15, y: 55},
		{x: 15, y: -45},
	]);
});

test('UV handle connection lines connect fields from schema metadata', () => {
	const points = [
		{x: 0, y: 0},
		{x: 100, y: 0},
		{x: 100, y: 100},
		{x: 0, y: 100},
	] as const;

	const lines = getUvHandleConnectionLines({
		points,
		handles: [
			{
				effectIndex: 0,
				fieldKey: 'start',
				fieldSchema: {
					type: 'uv-coordinate',
					default: [0, 0],
					visual: {
						type: 'line',
						to: 'end',
					},
				},
				value: [0.2, 0.3],
			},
			{
				effectIndex: 0,
				fieldKey: 'end',
				fieldSchema: {
					type: 'uv-coordinate',
					default: [1, 1],
				},
				value: [0.8, 0.7],
			},
		],
	});

	expect(lines).toEqual([
		{
			key: '0-start-end',
			from: {x: 20, y: 30},
			to: {x: 80, y: 70},
		},
	]);
});

test('UV handle connection lines stay within the same effect instance', () => {
	const points = [
		{x: 0, y: 0},
		{x: 100, y: 0},
		{x: 100, y: 100},
		{x: 0, y: 100},
	] as const;

	const lines = getUvHandleConnectionLines({
		points,
		handles: [
			{
				effectIndex: 0,
				fieldKey: 'start',
				fieldSchema: {
					type: 'uv-coordinate',
					default: [0, 0],
					visual: {
						type: 'line',
						to: 'end',
					},
				},
				value: [0.2, 0.3],
			},
			{
				effectIndex: 1,
				fieldKey: 'end',
				fieldSchema: {
					type: 'uv-coordinate',
					default: [1, 1],
				},
				value: [0.8, 0.7],
			},
			{
				effectIndex: 2,
				fieldKey: 'start',
				fieldSchema: {
					type: 'uv-coordinate',
					default: [0, 0],
					visual: {
						type: 'line',
						to: 'end',
					},
				},
				value: [0.2, 0.3],
			},
			{
				effectIndex: 2,
				fieldKey: 'end',
				fieldSchema: {
					type: 'uv-coordinate',
					default: [1, 1],
					visual: {
						type: 'line',
						to: 'start',
					},
				},
				value: [0.8, 0.7],
			},
		],
	});

	expect(lines.map((line) => line.key)).toEqual(['2-start-end']);
});

test('UV handle connection ellipses use numeric ellipse fields from schema metadata', () => {
	const points = [
		{x: 0, y: 0},
		{x: 100, y: 0},
		{x: 100, y: 100},
		{x: 0, y: 100},
	] as const;

	const ellipses = getUvHandleConnectionEllipses({
		points,
		handles: [
			{
				effectIndex: 0,
				fieldKey: 'center',
				fieldSchema: {
					type: 'uv-coordinate',
					default: [0.5, 0.5],
					visual: {
						type: 'ellipse',
						width: 'width',
						height: 'height',
						rotation: 'rotation',
						innerScale: 'start',
					},
				},
				effectValues: {
					width: 0.6,
					height: 0.4,
					rotation: 90,
					start: 0.5,
				},
				value: [0.5, 0.5],
			},
		],
	});

	expect(ellipses).toHaveLength(2);
	expect(ellipses[0].key).toBe('0-center-width-height-rotation');
	expect(ellipses[0].points[0].x).toBeCloseTo(50);
	expect(ellipses[0].points[0].y).toBeCloseTo(80);
	expect(ellipses[0].points[16].x).toBeCloseTo(30);
	expect(ellipses[0].points[16].y).toBeCloseTo(50);
	expect(ellipses[0].points[32].x).toBeCloseTo(50);
	expect(ellipses[0].points[32].y).toBeCloseTo(20);
	expect(ellipses[0].points[48].x).toBeCloseTo(70);
	expect(ellipses[0].points[48].y).toBeCloseTo(50);
	expect(ellipses[1].key).toBe('0-center-width-height-rotation-start');
	expect(ellipses[1].points[0].x).toBeCloseTo(50);
	expect(ellipses[1].points[0].y).toBeCloseTo(65);
	expect(ellipses[1].points[16].x).toBeCloseTo(40);
	expect(ellipses[1].points[16].y).toBeCloseTo(50);
	expect(ellipses[1].points[32].x).toBeCloseTo(50);
	expect(ellipses[1].points[32].y).toBeCloseTo(35);
	expect(ellipses[1].points[48].x).toBeCloseTo(60);
	expect(ellipses[1].points[48].y).toBeCloseTo(50);
});

test('UV handle connection ellipses rotate numeric fields in pixel space', () => {
	const points = [
		{x: 0, y: 0},
		{x: 200, y: 0},
		{x: 200, y: 100},
		{x: 0, y: 100},
	] as const;

	const ellipses = getUvHandleConnectionEllipses({
		dimensions: {width: 200, height: 100},
		points,
		handles: [
			{
				effectIndex: 0,
				fieldKey: 'center',
				fieldSchema: {
					type: 'uv-coordinate',
					default: [0.5, 0.5],
					visual: {
						type: 'ellipse',
						width: 'width',
						height: 'height',
						rotation: 'rotation',
						innerScale: 'start',
					},
				},
				effectValues: {
					width: 0.6,
					height: 0.4,
					rotation: 90,
					start: 0.5,
				},
				value: [0.5, 0.5],
			},
		],
	});

	expect(ellipses).toHaveLength(2);
	expect(ellipses[0].points[0].x).toBeCloseTo(100);
	expect(ellipses[0].points[0].y).toBeCloseTo(110);
	expect(ellipses[0].points[16].x).toBeCloseTo(80);
	expect(ellipses[0].points[16].y).toBeCloseTo(50);
	expect(ellipses[0].points[32].x).toBeCloseTo(100);
	expect(ellipses[0].points[32].y).toBeCloseTo(-10);
	expect(ellipses[0].points[48].x).toBeCloseTo(120);
	expect(ellipses[0].points[48].y).toBeCloseTo(50);
	expect(ellipses[1].points[0].x).toBeCloseTo(100);
	expect(ellipses[1].points[0].y).toBeCloseTo(80);
	expect(ellipses[1].points[16].x).toBeCloseTo(90);
	expect(ellipses[1].points[16].y).toBeCloseTo(50);
	expect(ellipses[1].points[32].x).toBeCloseTo(100);
	expect(ellipses[1].points[32].y).toBeCloseTo(20);
	expect(ellipses[1].points[48].x).toBeCloseTo(110);
	expect(ellipses[1].points[48].y).toBeCloseTo(50);
});

test('UV ellipse interactive controls expose resize and rotation handles', () => {
	const points = [
		{x: 0, y: 0},
		{x: 100, y: 0},
		{x: 100, y: 100},
		{x: 0, y: 100},
	] as const;
	const nodePath = makeKey(['body', 0]);
	const schema = {
		center: {
			type: 'uv-coordinate',
			default: [0.5, 0.5],
			visual: {
				type: 'ellipse',
				width: 'width',
				height: 'height',
				rotation: 'rotation',
				innerScale: 'start',
			},
		},
		width: {
			type: 'number',
			default: 0.6,
			hiddenFromList: false,
		},
		height: {
			type: 'number',
			default: 0.4,
			hiddenFromList: false,
		},
		rotation: {
			type: 'rotation-degrees',
			default: 90,
		},
		start: {
			type: 'number',
			default: 0.5,
			hiddenFromList: false,
		},
	} as const satisfies InteractivitySchema;
	const handle = {
		clientId: 'client-id',
		effectIndex: 0,
		effectValues: {
			center: [0.5, 0.5],
			width: 0.6,
			height: 0.4,
			rotation: 90,
			start: 0.5,
		},
		ellipseControls: {
			width: {
				fieldKey: 'width',
				fieldSchema: schema.width,
				fieldDefault: schema.width.default,
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: 0.6,
				},
				value: 0.6,
			},
			height: {
				fieldKey: 'height',
				fieldSchema: schema.height,
				fieldDefault: schema.height.default,
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: 0.4,
				},
				value: 0.4,
			},
			rotation: {
				fieldKey: 'rotation',
				fieldSchema: schema.rotation,
				fieldDefault: schema.rotation.default,
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: 90,
				},
				value: 90,
			},
			start: {
				fieldKey: 'start',
				fieldSchema: schema.start,
				fieldDefault: schema.start.default,
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: 0.5,
				},
				value: 0.5,
			},
		},
		fieldDefault: schema.center.default,
		fieldKey: 'center',
		fieldSchema: schema.center,
		isSelected: true,
		nodePath,
		propStatus: {
			status: 'static',
			keyframeDisplayOffsetAdjustment: null,
			codeValue: [0.5, 0.5],
		},
		schema,
		sourceFrame: 0,
		value: [0.5, 0.5],
	} as unknown as SelectedOutlineUvHandle;

	const controls = getUvEllipseInteractiveControls({
		handles: [handle],
		points,
	});

	expect(controls).toHaveLength(1);
	expect(controls[0].center).toEqual({x: 50, y: 50});
	expect(controls[0].resize[0].axis).toBe('width');
	expect(controls[0].resize[0].position.x).toBeCloseTo(50);
	expect(controls[0].resize[0].position.y).toBeCloseTo(80);
	expect(controls[0].resize[1].axis).toBe('height');
	expect(controls[0].resize[1].position.x).toBeCloseTo(30);
	expect(controls[0].resize[1].position.y).toBeCloseTo(50);
	expect(controls[0].rotationControl?.position.x).toBeCloseTo(22);
	expect(controls[0].rotationControl?.position.y).toBeCloseTo(50);
	expect(controls[0].startControl?.position.x).toBeCloseTo(50);
	expect(controls[0].startControl?.position.y).toBeCloseTo(65);
});

test('UV ellipse interactive controls rotate numeric fields in pixel space', () => {
	const points = [
		{x: 0, y: 0},
		{x: 200, y: 0},
		{x: 200, y: 100},
		{x: 0, y: 100},
	] as const;
	const nodePath = makeKey(['body', 0]);
	const schema = {
		center: {
			type: 'uv-coordinate',
			default: [0.5, 0.5],
			visual: {
				type: 'ellipse',
				width: 'width',
				height: 'height',
				rotation: 'rotation',
				innerScale: 'start',
			},
		},
		width: {
			type: 'number',
			default: 0.6,
			hiddenFromList: false,
		},
		height: {
			type: 'number',
			default: 0.4,
			hiddenFromList: false,
		},
		rotation: {
			type: 'rotation-degrees',
			default: 90,
		},
		start: {
			type: 'number',
			default: 0.5,
			hiddenFromList: false,
		},
	} as const satisfies InteractivitySchema;
	const handle = {
		clientId: 'client-id',
		effectIndex: 0,
		effectValues: {
			center: [0.5, 0.5],
			width: 0.6,
			height: 0.4,
			rotation: 90,
			start: 0.5,
		},
		ellipseControls: {
			width: {
				fieldKey: 'width',
				fieldSchema: schema.width,
				fieldDefault: schema.width.default,
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: 0.6,
				},
				value: 0.6,
			},
			height: {
				fieldKey: 'height',
				fieldSchema: schema.height,
				fieldDefault: schema.height.default,
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: 0.4,
				},
				value: 0.4,
			},
			rotation: {
				fieldKey: 'rotation',
				fieldSchema: schema.rotation,
				fieldDefault: schema.rotation.default,
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: 90,
				},
				value: 90,
			},
			start: {
				fieldKey: 'start',
				fieldSchema: schema.start,
				fieldDefault: schema.start.default,
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: 0.5,
				},
				value: 0.5,
			},
		},
		fieldDefault: schema.center.default,
		fieldKey: 'center',
		fieldSchema: schema.center,
		isSelected: true,
		nodePath,
		propStatus: {
			status: 'static',
			keyframeDisplayOffsetAdjustment: null,
			codeValue: [0.5, 0.5],
		},
		schema,
		sourceFrame: 0,
		value: [0.5, 0.5],
	} as unknown as SelectedOutlineUvHandle;

	const controls = getUvEllipseInteractiveControls({
		dimensions: {width: 200, height: 100},
		handles: [handle],
		points,
	});

	expect(controls).toHaveLength(1);
	expect(controls[0].resize[0].position.x).toBeCloseTo(100);
	expect(controls[0].resize[0].position.y).toBeCloseTo(110);
	expect(controls[0].resize[1].position.x).toBeCloseTo(80);
	expect(controls[0].resize[1].position.y).toBeCloseTo(50);
	expect(controls[0].startControl?.position.x).toBeCloseTo(100);
	expect(controls[0].startControl?.position.y).toBeCloseTo(80);
});

const getUvHandlesForSelectedEffectChild = (selectedFieldKey: string) => {
	const sequenceNodePathInfo = makeNodePathInfo(['body', 0], []);
	const effectPropNodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '0', selectedFieldKey],
	);
	const nodePath = sequenceNodePathInfo.sequenceSubscriptionKey;
	const effectSchema = {
		start: {
			type: 'uv-coordinate',
			default: [0, 0],
			visual: {
				type: 'line',
				to: 'end',
			},
		},
		end: {
			type: 'uv-coordinate',
			default: [1, 1],
		},
		dotSize: {
			type: 'number',
			default: 10,
			hiddenFromList: false,
		},
	} as const satisfies InteractivitySchema;
	const propStatuses: PropStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {},
			effects: [
				{
					canUpdate: true,
					effectIndex: 0,
					callee: 'testEffect',
					importPath: null,
					props: {
						start: {
							status: 'static',
							keyframeDisplayOffsetAdjustment: null,
							codeValue: [0.2, 0.3],
						},
						end: {
							status: 'static',
							keyframeDisplayOffsetAdjustment: null,
							codeValue: [0.8, 0.7],
						},
						dotSize: {
							status: 'static',
							keyframeDisplayOffsetAdjustment: null,
							codeValue: 10,
						},
					},
				},
			],
		},
	};

	return getSelectedUvHandles({
		propStatuses,
		clientId: 'client-id',
		getEffectDragOverrides: () => ({}),
		nodePath,
		selectedEffects: getSelectedEffectFieldsBySequenceKey([
			{
				type: 'sequence-effect-prop',
				nodePathInfo: effectPropNodePathInfo,
				i: 0,
				key: selectedFieldKey,
			},
		]).get(getTimelineSequenceSelectionKey(sequenceNodePathInfo)),
		sequence: {
			effects: [{schema: effectSchema}],
		} as unknown as TSequence,
		sourceFrame: 0,
	});
};

test('UV handles include connected coordinates when selecting one coordinate', () => {
	const handles = getUvHandlesForSelectedEffectChild('start');

	expect(
		handles.map((handle) => ({
			fieldKey: handle.fieldKey,
			isSelected: handle.isSelected,
			value: handle.value,
		})),
	).toEqual([
		{fieldKey: 'start', isSelected: true, value: [0.2, 0.3]},
		{fieldKey: 'end', isSelected: false, value: [0.8, 0.7]},
	]);
	expect(
		getUvHandleConnectionLines({
			points: [
				{x: 0, y: 0},
				{x: 100, y: 0},
				{x: 100, y: 100},
				{x: 0, y: 100},
			],
			handles,
		}).map((line) => line.key),
	).toEqual(['0-start-end']);
});

test('UV handles show for selected non-coordinate effect children', () => {
	const handles = getUvHandlesForSelectedEffectChild('dotSize');

	expect(
		handles.map((handle) => ({
			fieldKey: handle.fieldKey,
			isSelected: handle.isSelected,
		})),
	).toEqual([
		{fieldKey: 'start', isSelected: false},
		{fieldKey: 'end', isSelected: false},
	]);
});

test('UV handle selection targets the matching effect property', () => {
	const sequenceNodePathInfo = makeNodePathInfo(['body', 0], []);

	expect(
		getSelectedOutlineUvHandleTimelineSelection({
			effectIndex: 1,
			fieldKey: 'end',
			nodePathInfo: sequenceNodePathInfo,
		}),
	).toEqual({
		type: 'sequence-effect-prop',
		nodePathInfo: {
			...sequenceNodePathInfo,
			auxiliaryKeys: ['effects', '1', 'end'],
		},
		i: 1,
		key: 'end',
	});
});

test('UV handles are requested for selected effect children', () => {
	const sequenceNodePathInfo = makeNodePathInfo(['body', 0], []);
	const effectNodePathInfo = makeNodePathInfo(['body', 0], ['effects', '1']);
	const effectPropNodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '1', 'rays'],
	);
	const selectedEffects = getSelectedEffectFieldsBySequenceKey([
		{
			type: 'sequence-effect-prop',
			nodePathInfo: effectPropNodePathInfo,
			i: 1,
			key: 'rays',
		},
	]);

	expect(
		selectedEffects
			.get(getTimelineSequenceSelectionKey(sequenceNodePathInfo))
			?.get(1),
	).toEqual({
		allFields: false,
		fieldKeys: new Set(['rays']),
	});

	const selectedWholeEffects = getSelectedEffectFieldsBySequenceKey([
		{type: 'sequence-effect', nodePathInfo: effectNodePathInfo, i: 1},
	]);

	expect(
		selectedWholeEffects
			.get(getTimelineSequenceSelectionKey(sequenceNodePathInfo))
			?.get(1),
	).toEqual({
		allFields: true,
		fieldKeys: new Set(),
	});
});

test('UV handles are requested for selected effect keyframes and easings', () => {
	const sequenceNodePathInfo = makeNodePathInfo(['body', 0], []);
	const effectPropNodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '1', 'rays'],
	);
	const sequenceKey = getTimelineSequenceSelectionKey(sequenceNodePathInfo);
	const selectedEffects = getSelectedEffectFieldsBySequenceKey([
		{
			type: 'keyframe',
			nodePathInfo: effectPropNodePathInfo,
			frame: 10,
		},
		{
			type: 'easing',
			nodePathInfo: effectPropNodePathInfo,
			fromFrame: 10,
			toFrame: 20,
			segmentIndex: 0,
		},
	]);

	expect(selectedEffects.get(sequenceKey)?.get(1)).toEqual({
		allFields: false,
		fieldKeys: new Set(['rays']),
	});
});

test('UV handles are requested for keyframed selected effect props', () => {
	const sequenceNodePathInfo = makeNodePathInfo(['body', 0], []);
	const effectPropNodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '0', 'position'],
	);
	const nodePath = sequenceNodePathInfo.sequenceSubscriptionKey;
	const effectSchema = {
		position: {
			type: 'uv-coordinate',
			default: [0, 0],
		},
	} as const satisfies InteractivitySchema;
	const propStatuses: PropStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {},
			effects: [
				{
					canUpdate: true,
					effectIndex: 0,
					callee: 'testEffect',
					importPath: null,
					props: {
						position: {
							status: 'keyframed',
							keyframeDisplayOffsetAdjustment: null,
							interpolationFunction: 'interpolate',
							keyframes: [
								{frame: 0, value: [0, 0]},
								{frame: 100, value: [1, 1]},
							],
							easing: [{type: 'linear'}],
							clamping: {left: 'extend', right: 'extend'},
							posterize: undefined,
							output: undefined,
						},
					},
				},
			],
		},
	};

	const handles = getSelectedUvHandles({
		propStatuses,
		clientId: 'client-id',
		getEffectDragOverrides: () => ({}),
		nodePath,
		selectedEffects: getSelectedEffectFieldsBySequenceKey([
			{
				type: 'sequence-effect-prop',
				nodePathInfo: effectPropNodePathInfo,
				i: 0,
				key: 'position',
			},
		]).get(getTimelineSequenceSelectionKey(sequenceNodePathInfo)),
		sequence: {
			effects: [{schema: effectSchema}],
		} as unknown as TSequence,
		sourceFrame: 50,
	});

	expect(handles).toHaveLength(1);
	expect(handles[0].propStatus.status).toBe('keyframed');
	expect(handles[0].sourceFrame).toBe(50);
	expect(handles[0].value).toEqual([0.5, 0.5]);
});

test('Transform origin easing selection targets the transform origin handle', () => {
	const transformOriginNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'style', 'transformOrigin'],
	);

	expect(
		getSelectedTransformOriginInfo([
			{
				type: 'easing',
				nodePathInfo: transformOriginNodePathInfo,
				fromFrame: 12,
				toFrame: 24,
				segmentIndex: 0,
			},
		]),
	).toEqual({
		sequenceKey: getTimelineSequenceSelectionKey(transformOriginNodePathInfo),
		displayFrame: 12,
	});
});

test('Crop prop and keyframe selections target crop handles', () => {
	const cropNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'cropLeft'],
	);
	const sequenceKey = getTimelineSequenceSelectionKey(cropNodePathInfo);

	expect(
		getSelectedCropInfo([
			{
				type: 'sequence-prop',
				nodePathInfo: cropNodePathInfo,
				key: 'cropLeft',
			},
		]),
	).toEqual({sequenceKey, displayFrame: null});
	expect(
		getSelectedCropInfo([
			{
				type: 'keyframe',
				nodePathInfo: cropNodePathInfo,
				frame: 18,
			},
		]),
	).toEqual({sequenceKey, displayFrame: 18});
	expect(
		getSelectedCropInfo([
			{
				type: 'sequence-prop',
				nodePathInfo: cropNodePathInfo,
				key: 'style.opacity',
			},
		]),
	).toBe(null);
});

test('Escape moves crop and rotation selections to the sequence', () => {
	const sequenceNodePathInfo = makeNodePathInfo(['body', 0], []);
	const expectedSelection = {
		type: 'sequence' as const,
		nodePathInfo: sequenceNodePathInfo,
	};

	expect(
		getTimelineSequenceSelectionForEscape([
			{
				type: 'sequence-prop',
				nodePathInfo: makeNodePathInfo(['body', 0], ['controls', 'cropLeft']),
				key: 'cropLeft',
			},
		]),
	).toEqual(expectedSelection);
	expect(
		getTimelineSequenceSelectionForEscape([
			{
				type: 'sequence-prop',
				nodePathInfo: makeNodePathInfo(
					['body', 0],
					['controls', 'style', 'rotate'],
				),
				key: 'style.rotate',
			},
		]),
	).toEqual(expectedSelection);
	expect(
		getTimelineSequenceSelectionForEscape([
			{
				type: 'keyframe',
				nodePathInfo: makeNodePathInfo(
					['body', 0],
					['controls', 'style', 'rotate'],
				),
				frame: 10,
			},
		]),
	).toEqual(expectedSelection);
	expect(
		getTimelineSequenceSelectionForEscape([
			{
				type: 'easing',
				nodePathInfo: makeNodePathInfo(['body', 0], ['controls', 'cropBottom']),
				fromFrame: 10,
				toFrame: 20,
				segmentIndex: 0,
			},
		]),
	).toEqual(expectedSelection);
	expect(
		getTimelineSequenceSelectionForEscape([
			{
				type: 'sequence-prop',
				nodePathInfo: makeNodePathInfo(
					['body', 0],
					['controls', 'style', 'opacity'],
				),
				key: 'style.opacity',
			},
		]),
	).toBe(null);
	expect(
		getTimelineSequenceSelectionForEscape([
			{
				type: 'sequence',
				nodePathInfo: sequenceNodePathInfo,
			},
		]),
	).toBe(null);
});

test('selected sequence keys only include exact sequence selections', () => {
	const sequenceNodePathInfo = makeNodePathInfo(['body', 0], []);
	const effectPropNodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '1', 'rays'],
	);
	const sequenceKey = getTimelineSequenceSelectionKey(sequenceNodePathInfo);

	expect(
		getSelectedSequenceKeys([
			{
				type: 'sequence-effect-prop',
				nodePathInfo: effectPropNodePathInfo,
				i: 1,
				key: 'rays',
			},
		]).has(sequenceKey),
	).toBe(false);

	expect(
		getSelectedSequenceKeys([
			{type: 'sequence', nodePathInfo: sequenceNodePathInfo},
		]).has(sequenceKey),
	).toBe(true);
});

test('Cmd+A selection only targets selectable timeline sequences', () => {
	const sequenceNodePathInfo = makeNodePathInfo(['body', 0], []);
	const effectNodePathInfo = makeNodePathInfo(['body', 1], ['effects', '0']);

	expect(
		getSelectableTimelineSequenceSelections([
			{nodePathInfo: null},
			{nodePathInfo: sequenceNodePathInfo},
			{nodePathInfo: effectNodePathInfo},
		]),
	).toEqual([
		{
			type: 'sequence',
			nodePathInfo: sequenceNodePathInfo,
		},
	]);
});

test('Derived selectable timeline items follow expanded timeline order', () => {
	const schema = {
		opacity: {type: 'number', default: 1, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const sequenceNodePathInfo = makeNodePathInfo(['body', 0], []);
	const opacityNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'opacity'],
	);
	const nodePath = sequenceNodePathInfo.sequenceSubscriptionKey;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				opacity: {
					status: 'keyframed',
					keyframeDisplayOffsetAdjustment: null,
					interpolationFunction: 'interpolate',
					keyframes: [
						{frame: 10, value: 0},
						{frame: 20, value: 1},
					],
					easing: [{type: 'linear'}],
					clamping: {left: 'extend', right: 'extend'},
					posterize: undefined,
					output: undefined,
				},
			},
			effects: [],
		},
	} satisfies PropStatuses;

	expect(
		getSelectableTimelineItems({
			getDragOverrides: () => ({}),
			getEffectDragOverrides: () => ({}),
			getIsExpanded: () => true,
			propStatuses,
			selectedItems: [],
			timeline: [
				{
					cascadedStart: 0,
					localStart: 0,
					parentVisibleStart: 0,
					parentVisibleEnd: null,
					depth: 0,
					keyframeDisplayOffset: 0,
					keyframePlaybackRate: 1,
					nodePathInfo: sequenceNodePathInfo,
					sequence: {...makeTimelineSequence({schema}), loopDisplay: undefined},
					sequenceFrameOffset: 0,
				},
			],
			timelinePosition: 10,
		}).map(getTimelineSelectionKey),
	).toEqual([
		getTimelineSelectionKey({
			type: 'sequence',
			nodePathInfo: sequenceNodePathInfo,
		}),
		getTimelineSelectionKey({
			type: 'sequence-prop',
			nodePathInfo: opacityNodePathInfo,
			key: 'opacity',
		}),
		getTimelineSelectionKey({
			type: 'easing',
			nodePathInfo: opacityNodePathInfo,
			fromFrame: 10,
			toFrame: 20,
			segmentIndex: 0,
		}),
		getTimelineSelectionKey({
			type: 'keyframe',
			nodePathInfo: opacityNodePathInfo,
			frame: 10,
		}),
		getTimelineSelectionKey({
			type: 'keyframe',
			nodePathInfo: opacityNodePathInfo,
			frame: 20,
		}),
	]);
});

test('Cmd+D duplicates selected timeline sequence and effect rows', () => {
	const sequenceNodePathInfo = makeNodePathInfo(['body', 0], []);
	const effectNodePathInfo = makeNodePathInfo(['body', 1], ['effects', '0']);

	expect(
		[
			{type: 'sequence' as const, nodePathInfo: sequenceNodePathInfo},
			{
				type: 'sequence-effect' as const,
				nodePathInfo: effectNodePathInfo,
				i: 0,
			},
			{
				type: 'keyframe' as const,
				nodePathInfo: sequenceNodePathInfo,
				frame: 12,
			},
		].filter(isDuplicatableSequenceRowSelection),
	).toEqual([
		{
			type: 'sequence',
			nodePathInfo: sequenceNodePathInfo,
		},
	]);

	expect(
		[
			{type: 'sequence' as const, nodePathInfo: sequenceNodePathInfo},
			{
				type: 'sequence-effect' as const,
				nodePathInfo: effectNodePathInfo,
				i: 0,
			},
			{
				type: 'keyframe' as const,
				nodePathInfo: sequenceNodePathInfo,
				frame: 12,
			},
		].filter(isDuplicatableEffectSelection),
	).toEqual([
		{
			type: 'sequence-effect',
			nodePathInfo: effectNodePathInfo,
			i: 0,
		},
	]);
});

test('Backspace reset targets multiple selected sequence props', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {
		opacity: {type: 'number', default: 1, hiddenFromList: false},
		'style.rotate': {type: 'rotation-css', default: '0deg'},
	} satisfies InteractivitySchema;
	const opacityNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'opacity'],
	);
	const rotateNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'style.rotate'],
	);
	const nodePath = opacityNodePathInfo.sequenceSubscriptionKey;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				opacity: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: 0.5,
				},
				'style.rotate': {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: '45deg',
				},
			},
			effects: [],
		},
	} satisfies PropStatuses;

	const resetTargets = getTimelinePropResetTargets({
		selections: [
			{
				type: 'sequence-prop',
				nodePathInfo: opacityNodePathInfo,
				key: 'opacity',
			},
			{
				type: 'sequence-prop',
				nodePathInfo: rotateNodePathInfo,
				key: 'style.rotate',
			},
		],
		tracks: calculate({
			sequences: [makeTimelineSequence({schema})],
			overrideIdsToNodePaths: {override: nodePath},
		}).tracks,
		propStatuses,
	});

	expect(resetTargets?.map((target) => target.fieldKey)).toEqual([
		'opacity',
		'style.rotate',
	]);
	expect(resetTargets?.map((target) => target.value)).toEqual([1, '0deg']);
});

test('Backspace reset targets stroke with the SVG default', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {
		stroke: {type: 'color', default: 'none'},
	} satisfies InteractivitySchema;
	const strokeNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'stroke'],
	);
	const nodePath = strokeNodePathInfo.sequenceSubscriptionKey;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				stroke: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: '#ff0000',
				},
			},
			effects: [],
		},
	} satisfies PropStatuses;

	const resetTargets = getTimelinePropResetTargets({
		selections: [
			{
				type: 'sequence-prop',
				nodePathInfo: strokeNodePathInfo,
				key: 'stroke',
			},
		],
		tracks: calculate({
			sequences: [makeTimelineSequence({schema})],
			overrideIdsToNodePaths: {override: nodePath},
		}).tracks,
		propStatuses,
	});

	expect(resetTargets).toEqual([
		{
			type: 'sequence-prop',
			fileName: '/project/src/Comp.tsx',
			nodePath,
			fieldKey: 'stroke',
			value: 'none',
			defaultValue: '"none"',
			schema,
		},
	]);
});

test('Backspace reset targets border radius with the CSS default', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const radiusNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'style.borderRadius'],
	);
	const nodePath = radiusNodePathInfo.sequenceSubscriptionKey;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				'style.borderRadius': {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: 24,
				},
			},
			effects: [],
		},
	} satisfies PropStatuses;

	const resetTargets = getTimelinePropResetTargets({
		selections: [
			{
				type: 'sequence-prop',
				nodePathInfo: radiusNodePathInfo,
				key: 'style.borderRadius',
			},
		],
		tracks: calculate({
			sequences: [
				makeTimelineSequence({schema: NoReactInternals.sequenceSchema}),
			],
			overrideIdsToNodePaths: {override: nodePath},
		}).tracks,
		propStatuses,
	});

	expect(resetTargets).toEqual([
		{
			type: 'sequence-prop',
			fileName: '/project/src/Comp.tsx',
			nodePath,
			fieldKey: 'style.borderRadius',
			value: 0,
			defaultValue: '0',
			schema: NoReactInternals.sequenceSchema,
		},
	]);
});

test('Backspace reset targets selected keyframed sequence props', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {
		opacity: {type: 'number', default: 1, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const opacityNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'opacity'],
	);
	const nodePath = opacityNodePathInfo.sequenceSubscriptionKey;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				opacity: {
					status: 'keyframed',
					keyframeDisplayOffsetAdjustment: null,
					interpolationFunction: 'interpolate',
					keyframes: [
						{frame: 0, value: 0},
						{frame: 20, value: 0.5},
					],
					easing: [{type: 'linear'}],
					clamping: {left: 'extend', right: 'extend'},
					posterize: undefined,
					output: undefined,
				},
			},
			effects: [],
		},
	} satisfies PropStatuses;

	const resetTargets = getTimelinePropResetTargets({
		selections: [
			{
				type: 'sequence-prop',
				nodePathInfo: opacityNodePathInfo,
				key: 'opacity',
			},
		],
		tracks: calculate({
			sequences: [makeTimelineSequence({schema})],
			overrideIdsToNodePaths: {override: nodePath},
		}).tracks,
		propStatuses,
	});

	expect(resetTargets).toEqual([
		{
			type: 'sequence-prop',
			fileName: '/project/src/Comp.tsx',
			nodePath,
			fieldKey: 'opacity',
			value: 1,
			defaultValue: '1',
			schema,
		},
	]);
});

test('Backspace reset skips selected computed sequence props with defaults', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {
		'style.scale': {type: 'scale', default: 1, max: 100},
	} satisfies InteractivitySchema;
	const scaleNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'style.scale'],
	);
	const nodePath = scaleNodePathInfo.sequenceSubscriptionKey;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				'style.scale': {
					status: 'computed',
				},
			},
			effects: [],
		},
	} satisfies PropStatuses;

	const resetTargets = getTimelinePropResetTargets({
		selections: [
			{
				type: 'sequence-prop',
				nodePathInfo: scaleNodePathInfo,
				key: 'style.scale',
			},
		],
		tracks: calculate({
			sequences: [makeTimelineSequence({schema})],
			overrideIdsToNodePaths: {override: nodePath},
		}).tracks,
		propStatuses,
	});

	expect(resetTargets).toEqual([]);
});

test('Backspace reset targets flattened built-in keyframed sequence style props', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const opacityNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'style.opacity'],
	);
	const nodePath = opacityNodePathInfo.sequenceSubscriptionKey;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				'style.opacity': {
					status: 'keyframed',
					keyframeDisplayOffsetAdjustment: null,
					interpolationFunction: 'interpolate',
					keyframes: [
						{frame: 0, value: 0},
						{frame: 20, value: 0.5},
					],
					easing: [{type: 'linear'}],
					clamping: {left: 'extend', right: 'extend'},
					posterize: undefined,
					output: undefined,
				},
			},
			effects: [],
		},
	} satisfies PropStatuses;

	const resetTargets = getTimelinePropResetTargets({
		selections: [
			{
				type: 'sequence-prop',
				nodePathInfo: opacityNodePathInfo,
				key: 'style.opacity',
			},
		],
		tracks: calculate({
			sequences: [
				makeTimelineSequence({schema: NoReactInternals.sequenceSchema}),
			],
			overrideIdsToNodePaths: {override: nodePath},
		}).tracks,
		propStatuses,
	});

	expect(resetTargets).toEqual([
		{
			type: 'sequence-prop',
			fileName: '/project/src/Comp.tsx',
			nodePath,
			fieldKey: 'style.opacity',
			value: 1,
			defaultValue: '1',
			schema: NoReactInternals.sequenceSchema,
		},
	]);
});

test('Backspace reset skips keyframed sequence props without defaults', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {
		opacity: {type: 'number', default: undefined, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const opacityNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'opacity'],
	);
	const nodePath = opacityNodePathInfo.sequenceSubscriptionKey;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				opacity: {
					status: 'keyframed',
					keyframeDisplayOffsetAdjustment: null,
					interpolationFunction: 'interpolate',
					keyframes: [
						{frame: 0, value: 0},
						{frame: 20, value: 0.5},
					],
					easing: [{type: 'linear'}],
					clamping: {left: 'extend', right: 'extend'},
					posterize: undefined,
					output: undefined,
				},
			},
			effects: [],
		},
	} satisfies PropStatuses;

	const resetTargets = getTimelinePropResetTargets({
		selections: [
			{
				type: 'sequence-prop',
				nodePathInfo: opacityNodePathInfo,
				key: 'opacity',
			},
		],
		tracks: calculate({
			sequences: [makeTimelineSequence({schema})],
			overrideIdsToNodePaths: {override: nodePath},
		}).tracks,
		propStatuses,
	});

	expect(resetTargets).toEqual([]);
});

test('Selected outline active schema exposes default Sequence translate controls', () => {
	const activeSchema = getSelectedOutlineActiveSchema({
		schema: NoReactInternals.sequenceSchema,
		currentRuntimeValueDotNotation: {
			layout: undefined,
			'style.translate': undefined,
		},
		dragOverrides: {},
		propStatus: {
			layout: {
				status: 'static',
				keyframeDisplayOffsetAdjustment: null,
				codeValue: undefined,
			},
			'style.translate': {
				status: 'static',
				keyframeDisplayOffsetAdjustment: null,
				codeValue: undefined,
			},
		},
		frame: 0,
	});

	const translateField = activeSchema['style.translate'];
	expect(translateField?.type).toBe('translate');
	if (translateField?.type !== 'translate') {
		throw new Error('Expected style.translate to be active');
	}

	expect(translateField.default).toBe('0px 0px');
});

test('Selected outline edge dragging scales one axis when scale is unlinked', () => {
	const schema = {
		'style.scale': {type: 'scale', default: 1, max: 100},
	} satisfies InteractivitySchema;
	const nodePath = makeKey(['body', 0]);
	const dragStates = [
		{
			defaultValue: JSON.stringify(1),
			key: Internals.makeSequencePropsSubscriptionKey(nodePath),
			sourceFrame: 0,
			startX: 2,
			startY: 3,
			startZ: 1,
			target: {
				clientId: 'client',
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: '2 3',
				},
				fieldDefault: 1,
				fieldSchema: schema['style.scale'],
				keyframeDisplayOffset: 0,
				keyframePlaybackRate: 1,
				linked: false,
				nodePath,
				schema,
			},
		},
	] satisfies SelectedOutlineScaleDragState[];

	const lastValues = getSelectedOutlineScaleDragValues({
		dragStates,
		axis: 'x',
		scaleFactor: 1.25,
	});

	expect(lastValues.get(dragStates[0].key)).toBe('2.5 3');
	expect(
		getSelectedOutlineScaleDragChanges({
			dragStates,
			lastValues,
		}),
	).toEqual([
		{
			type: 'static',
			fileName: '/project/src/Comp.tsx',
			nodePath,
			fieldKey: 'style.scale',
			value: '2.5 3',
			defaultValue: JSON.stringify(1),
			schema,
		},
	]);

	const negativeValues = getSelectedOutlineScaleDragValues({
		dragStates,
		axis: 'x',
		scaleFactor: -0.5,
	});

	expect(negativeValues.get(dragStates[0].key)).toBe('-1 3');
});

test('Selected outline edge dragging rounds scale values', () => {
	const schema = {
		'style.scale': {type: 'scale', default: 1, max: 100},
	} satisfies InteractivitySchema;
	const nodePath = makeKey(['body', 0]);
	const dragStates = [
		{
			defaultValue: JSON.stringify(1),
			key: Internals.makeSequencePropsSubscriptionKey(nodePath),
			sourceFrame: 0,
			startX: 2,
			startY: 3,
			startZ: 1,
			target: {
				clientId: 'client',
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: '2 3',
				},
				fieldDefault: 1,
				fieldSchema: schema['style.scale'],
				keyframeDisplayOffset: 0,
				keyframePlaybackRate: 1,
				linked: false,
				nodePath,
				schema,
			},
		},
	] satisfies SelectedOutlineScaleDragState[];

	const lastValues = getSelectedOutlineScaleDragValues({
		dragStates,
		axis: 'x',
		scaleFactor: 1 / 3,
	});

	expect(lastValues.get(dragStates[0].key)).toBe('0.667 3');
});

test('Selected outline edge dragging preserves aspect ratio when scale is linked', () => {
	const schema = {
		'style.scale': {type: 'scale', default: 1, max: 100},
	} satisfies InteractivitySchema;
	const nodePath = makeKey(['body', 0]);
	const dragStates = [
		{
			defaultValue: JSON.stringify(1),
			key: Internals.makeSequencePropsSubscriptionKey(nodePath),
			sourceFrame: 0,
			startX: 2,
			startY: 3,
			startZ: 1,
			target: {
				clientId: 'client',
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: '2 3',
				},
				fieldDefault: 1,
				fieldSchema: schema['style.scale'],
				keyframeDisplayOffset: 0,
				keyframePlaybackRate: 1,
				linked: true,
				nodePath,
				schema,
			},
		},
	] satisfies SelectedOutlineScaleDragState[];

	const lastValues = getSelectedOutlineScaleDragValues({
		dragStates,
		axis: 'x',
		scaleFactor: 1.25,
	});

	expect(lastValues.get(dragStates[0].key)).toBe('2.5 3.75');
});

test('Selected outline corner dragging rotates selected sequences', () => {
	const schema = {
		'style.rotate': {type: 'rotation-css', default: '0deg'},
	} satisfies InteractivitySchema;
	const firstNodePath = makeKey(['body', 0]);
	const secondNodePath = makeKey(['body', 1]);
	const dragStates = [
		{
			defaultValue: JSON.stringify('0deg'),
			key: Internals.makeSequencePropsSubscriptionKey(firstNodePath),
			sourceFrame: 12,
			startDegrees: 45,
			startRotation: [0, 0, 45],
			startValue: '45deg',
			target: {
				clientId: 'client',
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: '45deg',
				},
				fieldDefault: '0deg',
				fieldSchema: schema['style.rotate'],
				keyframeDisplayOffset: 30,
				keyframePlaybackRate: 1,
				nodePath: firstNodePath,
				schema,
				transform3DMode: false,
				transformOriginValue: '50% 50%',
			},
		},
		{
			defaultValue: JSON.stringify('0deg'),
			key: Internals.makeSequencePropsSubscriptionKey(secondNodePath),
			sourceFrame: 12,
			startDegrees: -10,
			startRotation: [0, 0, -10],
			startValue: '-10deg',
			target: {
				clientId: 'client',
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: '-10deg',
				},
				fieldDefault: '0deg',
				fieldSchema: schema['style.rotate'],
				keyframeDisplayOffset: 30,
				keyframePlaybackRate: 1,
				nodePath: secondNodePath,
				schema,
				transform3DMode: false,
				transformOriginValue: '50% 50%',
			},
		},
	] satisfies SelectedOutlineRotationDragState[];

	const lastValues = getSelectedOutlineRotationDragValues({
		dragStates,
		rotationDeltaDegrees: 90,
	});

	expect(lastValues.get(dragStates[0].key)).toBe('135deg');
	expect(lastValues.get(dragStates[1].key)).toBe('80deg');
	expect(
		getSelectedOutlineRotationDragChanges({
			dragStates,
			lastValues,
		}),
	).toEqual([
		{
			type: 'static',
			fileName: '/project/src/Comp.tsx',
			nodePath: firstNodePath,
			fieldKey: 'style.rotate',
			value: '135deg',
			defaultValue: JSON.stringify('0deg'),
			schema,
		},
		{
			type: 'static',
			fileName: '/project/src/Comp.tsx',
			nodePath: secondNodePath,
			fieldKey: 'style.rotate',
			value: '80deg',
			defaultValue: JSON.stringify('0deg'),
			schema,
		},
	]);
});

test('Selected outline corner dragging rounds rotation values', () => {
	const schema = {
		'style.rotate': {type: 'rotation-css', default: '0deg'},
	} satisfies InteractivitySchema;
	const nodePath = makeKey(['body', 0]);
	const dragStates = [
		{
			defaultValue: JSON.stringify('0deg'),
			key: Internals.makeSequencePropsSubscriptionKey(nodePath),
			sourceFrame: 12,
			startDegrees: 32,
			startRotation: [0, 0, 32],
			startValue: '32deg',
			target: {
				clientId: 'client',
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: '32deg',
				},
				fieldDefault: '0deg',
				fieldSchema: schema['style.rotate'],
				keyframeDisplayOffset: 30,
				keyframePlaybackRate: 1,
				nodePath,
				schema,
				transform3DMode: false,
				transformOriginValue: '50% 50%',
			},
		},
	] satisfies SelectedOutlineRotationDragState[];

	const lastValues = getSelectedOutlineRotationDragValues({
		dragStates,
		rotationDeltaDegrees: 0.480832,
	});

	expect(lastValues.get(dragStates[0].key)).toBe('32.5deg');
});

test('Selected outline canvas dragging changes X and Y rotation while preserving Z', () => {
	const schema = {
		'style.rotate': {type: 'rotation-css', default: '0deg'},
	} satisfies InteractivitySchema;
	const nodePath = makeKey(['body', 0]);
	const dragStates = [
		{
			defaultValue: JSON.stringify('0deg'),
			key: Internals.makeSequencePropsSubscriptionKey(nodePath),
			sourceFrame: 12,
			startDegrees: 30,
			startRotation: [10, 20, 30],
			startValue: '0.386017 0.438014 0.811871 38.630009deg',
			target: {
				clientId: 'client',
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: '0.386017 0.438014 0.811871 38.630009deg',
				},
				fieldDefault: '0deg',
				fieldSchema: schema['style.rotate'],
				keyframeDisplayOffset: 30,
				keyframePlaybackRate: 1,
				nodePath,
				schema,
				transform3DMode: true,
				transformOriginValue: '50% 50%',
			},
		},
	] satisfies SelectedOutlineRotationDragState[];

	const lastValues = getSelectedOutline3DRotationDragValues({
		dragStates,
		rotationXDeltaDegrees: -15,
		rotationYDeltaDegrees: 25,
	});
	const rotation = parseCssRotationToEuler(lastValues.get(dragStates[0].key)!);

	expect(rotation[0]).toBeCloseTo(-5, 4);
	expect(rotation[1]).toBeCloseTo(45, 4);
	expect(rotation[2]).toBeCloseTo(30, 4);
});

test('Selected outline corner dragging changes Z rotation while preserving X and Y', () => {
	const schema = {
		'style.rotate': {type: 'rotation-css', default: '0deg'},
	} satisfies InteractivitySchema;
	const nodePath = makeKey(['body', 0]);
	const dragStates = [
		{
			defaultValue: JSON.stringify('0deg'),
			key: Internals.makeSequencePropsSubscriptionKey(nodePath),
			sourceFrame: 12,
			startDegrees: 38.630009,
			startRotation: [10, 20, 30],
			startValue: '0.386017 0.438014 0.811871 38.630009deg',
			target: {
				clientId: 'client',
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: '0.386017 0.438014 0.811871 38.630009deg',
				},
				fieldDefault: '0deg',
				fieldSchema: schema['style.rotate'],
				keyframeDisplayOffset: 30,
				keyframePlaybackRate: 1,
				nodePath,
				schema,
				transform3DMode: true,
				transformOriginValue: '50% 50%',
			},
		},
	] satisfies SelectedOutlineRotationDragState[];

	const lastValues = getSelectedOutlineRotationDragValues({
		dragStates,
		rotationDeltaDegrees: 15,
	});
	const rotation = parseCssRotationToEuler(lastValues.get(dragStates[0].key)!);

	expect(rotation[0]).toBeCloseTo(10, 4);
	expect(rotation[1]).toBeCloseTo(20, 4);
	expect(rotation[2]).toBeCloseTo(45, 4);
	expect(
		snapSelectedOutlineRotationDeltaDegrees({
			dragStates,
			rotationDeltaDegrees: 8,
		}),
	).toBe(15);
});

test('Selected outline corner dragging snaps rotation to 15 degree increments', () => {
	const schema = {
		'style.rotate': {type: 'rotation-css', default: '0deg'},
	} satisfies InteractivitySchema;
	const nodePath = makeKey(['body', 0]);
	const dragStates = [
		{
			defaultValue: JSON.stringify('0deg'),
			key: Internals.makeSequencePropsSubscriptionKey(nodePath),
			sourceFrame: 12,
			startDegrees: 32,
			startRotation: [0, 0, 32],
			startValue: '32deg',
			target: {
				clientId: 'client',
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: '32deg',
				},
				fieldDefault: '0deg',
				fieldSchema: schema['style.rotate'],
				keyframeDisplayOffset: 30,
				keyframePlaybackRate: 1,
				nodePath,
				schema,
				transform3DMode: false,
				transformOriginValue: '50% 50%',
			},
		},
	] satisfies SelectedOutlineRotationDragState[];

	const rotationDeltaDegrees = snapSelectedOutlineRotationDeltaDegrees({
		dragStates,
		rotationDeltaDegrees: 8,
	});
	const lastValues = getSelectedOutlineRotationDragValues({
		dragStates,
		rotationDeltaDegrees,
	});

	expect(rotationDeltaDegrees).toBe(13);
	expect(lastValues.get(dragStates[0].key)).toBe('45deg');
});

test('Selected outline corner dragging snaps selected rotations from the first drag state', () => {
	const schema = {
		'style.rotate': {type: 'rotation-css', default: '0deg'},
	} satisfies InteractivitySchema;
	const firstNodePath = makeKey(['body', 0]);
	const secondNodePath = makeKey(['body', 1]);
	const dragStates = [
		{
			defaultValue: JSON.stringify('0deg'),
			key: Internals.makeSequencePropsSubscriptionKey(firstNodePath),
			sourceFrame: 12,
			startDegrees: 32,
			startRotation: [0, 0, 32],
			startValue: '32deg',
			target: {
				clientId: 'client',
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: '32deg',
				},
				fieldDefault: '0deg',
				fieldSchema: schema['style.rotate'],
				keyframeDisplayOffset: 30,
				keyframePlaybackRate: 1,
				nodePath: firstNodePath,
				schema,
				transform3DMode: false,
				transformOriginValue: '50% 50%',
			},
		},
		{
			defaultValue: JSON.stringify('0deg'),
			key: Internals.makeSequencePropsSubscriptionKey(secondNodePath),
			sourceFrame: 12,
			startDegrees: -10,
			startRotation: [0, 0, -10],
			startValue: '-10deg',
			target: {
				clientId: 'client',
				propStatus: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: '-10deg',
				},
				fieldDefault: '0deg',
				fieldSchema: schema['style.rotate'],
				keyframeDisplayOffset: 30,
				keyframePlaybackRate: 1,
				nodePath: secondNodePath,
				schema,
				transform3DMode: false,
				transformOriginValue: '50% 50%',
			},
		},
	] satisfies SelectedOutlineRotationDragState[];

	const rotationDeltaDegrees = snapSelectedOutlineRotationDeltaDegrees({
		dragStates,
		rotationDeltaDegrees: 8,
	});
	const lastValues = getSelectedOutlineRotationDragValues({
		dragStates,
		rotationDeltaDegrees,
	});

	expect(rotationDeltaDegrees).toBe(13);
	expect(lastValues.get(dragStates[0].key)).toBe('45deg');
	expect(lastValues.get(dragStates[1].key)).toBe('3deg');
});

test('Selected outline corner dragging keyframed rotation adds a keyframe at the source frame', () => {
	const schema = {
		'style.rotate': {type: 'rotation-css', default: '0deg'},
	} satisfies InteractivitySchema;
	const nodePath = makeKey(['body', 0]);
	const dragStates = [
		{
			defaultValue: JSON.stringify('0deg'),
			key: Internals.makeSequencePropsSubscriptionKey(nodePath),
			sourceFrame: 20,
			startDegrees: 45,
			startRotation: [0, 0, 45],
			startValue: '45deg',
			target: {
				clientId: 'client',
				propStatus: {
					status: 'keyframed',
					keyframeDisplayOffsetAdjustment: null,
					interpolationFunction: 'interpolate',
					keyframes: [
						{frame: 0, value: '0deg'},
						{frame: 40, value: '90deg'},
					],
					easing: [{type: 'linear'}],
					clamping: {left: 'extend', right: 'extend'},
					posterize: undefined,
					output: undefined,
				},
				fieldDefault: '0deg',
				fieldSchema: schema['style.rotate'],
				keyframeDisplayOffset: 30,
				keyframePlaybackRate: 1,
				nodePath,
				schema,
				transform3DMode: false,
				transformOriginValue: '50% 50%',
			},
		},
	] satisfies SelectedOutlineRotationDragState[];

	const lastValues = getSelectedOutlineRotationDragValues({
		dragStates,
		rotationDeltaDegrees: 15,
	});

	expect(lastValues.get(dragStates[0].key)).toBe('60deg');
	expect(
		getSelectedOutlineRotationDragChanges({
			dragStates,
			lastValues,
		}),
	).toEqual([
		{
			type: 'keyframed',
			fileName: '/project/src/Comp.tsx',
			nodePath,
			fieldKey: 'style.rotate',
			sourceFrame: 20,
			value: '60deg',
			schema,
		},
	]);
	expect(
		getSelectedOutlineRotationDragChanges({
			dragStates,
			lastValues: new Map([[dragStates[0].key, '45deg']]),
		}),
	).toEqual([]);
});

test('Selected outline rotation delta does not jump at the angle wrap-around', () => {
	const firstStep = getSelectedOutlineRotationDeltaDegrees({
		from: 170,
		to: 175,
	});
	const secondStep = getSelectedOutlineRotationDeltaDegrees({
		from: 175,
		to: -170,
	});

	expect(firstStep + secondStep).toBe(20);
});

test('Selected outline rotation corners use the outline corners and center', () => {
	const points = [
		{x: 0, y: 0},
		{x: 100, y: 0},
		{x: 100, y: 50},
		{x: 0, y: 50},
	] as const;
	const topRight = getSelectedOutlineRotationCornerInfo(points, 'top-right');

	expect(topRight.point).toEqual({x: 100, y: 0});
	expect(topRight.center).toEqual({x: 50, y: 25});
	expect(topRight.cursor).toContain('data:image/svg+xml');
	expect(decodeURIComponent(topRight.cursor)).toContain(
		'<svg width="24" height="24"',
	);
	expect(topRight.cursor).toContain('") 12 12, alias');
});

test('Selected outline controls adapt to the screen-space outline size', () => {
	const normal = getSelectedOutlineControlLayout([
		{x: 0, y: 0},
		{x: 100, y: 0},
		{x: 100, y: 50},
		{x: 0, y: 50},
	]);
	const small = getSelectedOutlineControlLayout([
		{x: 0, y: 0},
		{x: 24, y: 0},
		{x: 24, y: 24},
		{x: 0, y: 24},
	]);
	const tiny = getSelectedOutlineControlLayout([
		{x: 0, y: 0},
		{x: 8, y: 0},
		{x: 8, y: 8},
		{x: 0, y: 8},
	]);
	const thin = getSelectedOutlineControlLayout([
		{x: 0, y: 0},
		{x: 8, y: 0},
		{x: 8, y: 40},
		{x: 0, y: 40},
	]);
	const flat = getSelectedOutlineControlLayout([
		{x: 0, y: 0},
		{x: 40, y: 0},
		{x: 40, y: 8},
		{x: 0, y: 8},
	]);

	expect(normal.scaleEdges).toEqual(['top', 'right', 'bottom', 'left']);
	expect(normal.scaleHitWidth).toEqual({horizontal: 9, vertical: 9});
	expect(normal.rotationHandleRadius).toBe(6.75);
	expect(normal.rotationCorners).toHaveLength(4);
	expect(normal.rotationCorners[0].point.x).toBeCloseTo(-4.773);
	expect(normal.rotationCorners[0].point.y).toBeCloseTo(-4.773);
	expect(small.scaleHitWidth).toEqual({horizontal: 4.5, vertical: 4.5});
	expect(small.rotationHandleRadius).toBe(3.375);
	expect(small.rotationCorners).toHaveLength(4);
	expect(tiny.scaleEdges).toEqual([]);
	expect(tiny.rotationCorners).toEqual([]);
	expect(thin.scaleEdges).toEqual(['top', 'bottom']);
	expect(thin.scaleHitWidth).toEqual({horizontal: 9, vertical: 4.5});
	expect(thin.rotationCorners).toEqual([]);
	expect(flat.scaleEdges).toEqual(['right', 'left']);
	expect(flat.scaleHitWidth).toEqual({horizontal: 4.5, vertical: 9});
	expect(flat.rotationCorners).toEqual([]);
});

test('Selected outline rotation pivot follows transform origin', () => {
	const points = [
		{x: 0, y: 0},
		{x: 100, y: 0},
		{x: 100, y: 50},
		{x: 0, y: 50},
	] as const;
	const dimensions = {width: 100, height: 50};

	expect(
		getSelectedOutlineRotationPivot({
			dimensions,
			points,
			transformOriginValue: 'center',
		}),
	).toEqual({x: 50, y: 25});
	expect(
		getSelectedOutlineRotationPivot({
			dimensions,
			points,
			transformOriginValue: 'left bottom',
		}),
	).toEqual({x: 0, y: 50});
	expect(
		getSelectedOutlineRotationPivot({
			dimensions,
			points,
			transformOriginValue: '25px top',
		}),
	).toEqual({x: 25, y: 0});
	expect(
		getSelectedOutlineRotationPivot({
			dimensions,
			points,
			transformOriginValue: 'calc(50% + 1px) center',
		}),
	).toEqual({x: 50, y: 25});
	expect(
		getSelectedOutlineRotationPivot({
			dimensions: null,
			points,
			transformOriginValue: 'left top',
		}),
	).toEqual({x: 50, y: 25});
});

test('Selected outline rotation cursors use the outline rotation', () => {
	const points = [
		{x: 0, y: 0},
		{x: 100, y: 0},
		{x: 100, y: 100},
		{x: 0, y: 100},
	] as const;

	expect(
		getSelectedOutlineRotationCornerInfo(points, 'top-left').cursorDegrees,
	).toBe(270);
	expect(
		getSelectedOutlineRotationCornerInfo(points, 'top-right').cursorDegrees,
	).toBe(0);
	expect(
		getSelectedOutlineRotationCornerInfo(points, 'bottom-right').cursorDegrees,
	).toBe(90);
	expect(
		getSelectedOutlineRotationCornerInfo(points, 'bottom-left').cursorDegrees,
	).toBe(180);
});

test('Selected outline rotation cursor angle is independent from aspect ratio', () => {
	const points = [
		{x: 0, y: 0},
		{x: 160, y: 0},
		{x: 160, y: 90},
		{x: 0, y: 90},
	] as const;

	expect(
		getSelectedOutlineRotationCornerInfo(points, 'top-right').cursorDegrees,
	).toBe(0);
	expect(
		getSelectedOutlineRotationCornerInfo(points, 'bottom-left').cursorDegrees,
	).toBe(180);
});

test('Selected outline scale edges project pointer movement onto the edge normal', () => {
	const points = [
		{x: 0, y: 0},
		{x: 100, y: 0},
		{x: 100, y: 50},
		{x: 0, y: 50},
	] as const;
	const right = getSelectedOutlineScaleEdgeInfo(points, 'right');
	const top = getSelectedOutlineScaleEdgeInfo(points, 'top');

	expect(right?.axis).toBe('x');
	expect(right?.cursor).toBe('ew-resize');
	expect(right?.extent).toBe(100);
	expect(right?.normal).toEqual({x: 1, y: 0});
	expect(top?.axis).toBe('y');
	expect(top?.cursor).toBe('ns-resize');
	expect(top?.extent).toBe(50);
	expect(top?.normal).toEqual({x: 0, y: -1});
});

test('Selected outline scale edge cursors follow rotated outlines', () => {
	const rotated90Degrees = [
		{x: 0, y: 0},
		{x: 0, y: 100},
		{x: -50, y: 100},
		{x: -50, y: 0},
	] as const;
	const rotated45Degrees = [
		{x: 0, y: 0},
		{x: 100, y: 100},
		{x: 50, y: 150},
		{x: -50, y: 50},
	] as const;

	expect(
		getSelectedOutlineScaleEdgeInfo(rotated90Degrees, 'right')?.cursor,
	).toBe('ns-resize');
	expect(getSelectedOutlineScaleEdgeInfo(rotated90Degrees, 'top')?.cursor).toBe(
		'ew-resize',
	);
	expect(
		getSelectedOutlineScaleEdgeInfo(rotated45Degrees, 'right')?.cursor,
	).toBe('nwse-resize');
	expect(getSelectedOutlineScaleEdgeInfo(rotated45Degrees, 'top')?.cursor).toBe(
		'nesw-resize',
	);
});

test('Backspace reset targets selected effect props', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const effectSchema = {
		intensity: {type: 'number', default: 0, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const nodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '0', 'intensity'],
	);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {},
			effects: [
				{
					canUpdate: true,
					callee: 'effect',
					importPath: null,
					effectIndex: 0,
					props: {
						intensity: {
							status: 'static',
							keyframeDisplayOffsetAdjustment: null,
							codeValue: 10,
						},
					},
				},
			],
		},
	} satisfies PropStatuses;

	const resetTargets = getTimelinePropResetTargets({
		selections: [
			{
				type: 'sequence-effect-prop',
				nodePathInfo,
				i: 0,
				key: 'intensity',
			},
		],
		tracks: calculate({
			sequences: [
				makeTimelineSequence({
					schema,
					effects: [{schema: effectSchema}],
				}),
			],
			overrideIdsToNodePaths: {override: nodePath},
		}).tracks,
		propStatuses,
	});

	expect(resetTargets).toEqual([
		{
			type: 'effect-prop',
			fileName: '/project/src/Comp.tsx',
			nodePath,
			effectIndex: 0,
			fieldKey: 'intensity',
			value: 0,
			defaultValue: '0',
			schema: effectSchema,
		},
	]);
});

test('Backspace reset targets active enum variant effect props', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const effectSchema = {
		colorMode: {
			type: 'enum',
			default: 'solid' as const,
			variants: {
				solid: {
					dotColor: {
						type: 'color',
						default: 'red',
					},
				},
				source: {},
			},
		},
	} satisfies InteractivitySchema;
	const nodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '0', 'dotColor'],
	);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {},
			effects: [
				{
					canUpdate: true,
					callee: 'effect',
					importPath: null,
					effectIndex: 0,
					props: {
						colorMode: {
							status: 'static',
							keyframeDisplayOffsetAdjustment: null,
							codeValue: 'solid',
						},
						dotColor: {
							status: 'static',
							keyframeDisplayOffsetAdjustment: null,
							codeValue: 'blue',
						},
					},
				},
			],
		},
	} satisfies PropStatuses;

	const resetTargets = getTimelinePropResetTargets({
		selections: [
			{
				type: 'sequence-effect-prop',
				nodePathInfo,
				i: 0,
				key: 'dotColor',
			},
		],
		tracks: calculate({
			sequences: [
				makeTimelineSequence({
					schema,
					effects: [{schema: effectSchema}],
				}),
			],
			overrideIdsToNodePaths: {override: nodePath},
		}).tracks,
		propStatuses,
	});

	expect(resetTargets).toEqual([
		{
			type: 'effect-prop',
			fileName: '/project/src/Comp.tsx',
			nodePath,
			effectIndex: 0,
			fieldKey: 'dotColor',
			value: 'red',
			defaultValue: '"red"',
			schema: effectSchema,
		},
	]);
});

test('Backspace reset targets selected static blur radius with default', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const effectSchema = {
		radius: {type: 'number', default: 40, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const nodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '0', 'radius'],
	);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {},
			effects: [
				{
					canUpdate: true,
					callee: 'blur',
					importPath: null,
					effectIndex: 0,
					props: {
						radius: {
							status: 'static',
							keyframeDisplayOffsetAdjustment: null,
							codeValue: 24,
						},
					},
				},
			],
		},
	} satisfies PropStatuses;

	const resetTargets = getTimelinePropResetTargets({
		selections: [
			{
				type: 'sequence-effect-prop',
				nodePathInfo,
				i: 0,
				key: 'radius',
			},
		],
		tracks: calculate({
			sequences: [
				makeTimelineSequence({
					schema,
					effects: [{schema: effectSchema}],
				}),
			],
			overrideIdsToNodePaths: {override: nodePath},
		}).tracks,
		propStatuses,
	});

	expect(resetTargets).toEqual([
		{
			type: 'effect-prop',
			fileName: '/project/src/Comp.tsx',
			nodePath,
			effectIndex: 0,
			fieldKey: 'radius',
			value: 40,
			defaultValue: '40',
			schema: effectSchema,
		},
	]);
});

test('Backspace reset targets selected keyframed effect props', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const effectSchema = {
		intensity: {type: 'number', default: 0, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const nodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '0', 'intensity'],
	);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {},
			effects: [
				{
					canUpdate: true,
					callee: 'effect',
					importPath: null,
					effectIndex: 0,
					props: {
						intensity: {
							status: 'keyframed',
							keyframeDisplayOffsetAdjustment: null,
							interpolationFunction: 'interpolate',
							keyframes: [
								{frame: 0, value: 10},
								{frame: 20, value: 20},
							],
							easing: [{type: 'linear'}],
							clamping: {left: 'extend', right: 'extend'},
							posterize: undefined,
							output: undefined,
						},
					},
				},
			],
		},
	} satisfies PropStatuses;

	const resetTargets = getTimelinePropResetTargets({
		selections: [
			{
				type: 'sequence-effect-prop',
				nodePathInfo,
				i: 0,
				key: 'intensity',
			},
		],
		tracks: calculate({
			sequences: [
				makeTimelineSequence({
					schema,
					effects: [{schema: effectSchema}],
				}),
			],
			overrideIdsToNodePaths: {override: nodePath},
		}).tracks,
		propStatuses,
	});

	expect(resetTargets).toEqual([
		{
			type: 'effect-prop',
			fileName: '/project/src/Comp.tsx',
			nodePath,
			effectIndex: 0,
			fieldKey: 'intensity',
			value: 0,
			defaultValue: '0',
			schema: effectSchema,
		},
	]);
});

test('Backspace reset skips keyframed effect props without defaults', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {} satisfies InteractivitySchema;
	const effectSchema = {
		intensity: {type: 'number', default: undefined, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const nodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '0', 'intensity'],
	);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {},
			effects: [
				{
					canUpdate: true,
					callee: 'effect',
					importPath: null,
					effectIndex: 0,
					props: {
						intensity: {
							status: 'keyframed',
							keyframeDisplayOffsetAdjustment: null,
							interpolationFunction: 'interpolate',
							keyframes: [
								{frame: 0, value: 10},
								{frame: 20, value: 20},
							],
							easing: [{type: 'linear'}],
							clamping: {left: 'extend', right: 'extend'},
							posterize: undefined,
							output: undefined,
						},
					},
				},
			],
		},
	} satisfies PropStatuses;

	const resetTargets = getTimelinePropResetTargets({
		selections: [
			{
				type: 'sequence-effect-prop',
				nodePathInfo,
				i: 0,
				key: 'intensity',
			},
		],
		tracks: calculate({
			sequences: [
				makeTimelineSequence({
					schema,
					effects: [{schema: effectSchema}],
				}),
			],
			overrideIdsToNodePaths: {override: nodePath},
		}).tracks,
		propStatuses,
	});

	expect(resetTargets).toEqual([]);
});

test('Backspace reset targets mixed selected sequence and effect props', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {
		opacity: {type: 'number', default: 1, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const effectSchema = {
		intensity: {type: 'number', default: 0, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const opacityNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'opacity'],
	);
	const intensityNodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '0', 'intensity'],
	);
	const nodePath = opacityNodePathInfo.sequenceSubscriptionKey;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				opacity: {
					status: 'static',
					keyframeDisplayOffsetAdjustment: null,
					codeValue: 0.5,
				},
			},
			effects: [
				{
					canUpdate: true,
					callee: 'effect',
					importPath: null,
					effectIndex: 0,
					props: {
						intensity: {
							status: 'static',
							keyframeDisplayOffsetAdjustment: null,
							codeValue: 10,
						},
					},
				},
			],
		},
	} satisfies PropStatuses;

	const resetTargets = getTimelinePropResetTargets({
		selections: [
			{
				type: 'sequence-prop',
				nodePathInfo: opacityNodePathInfo,
				key: 'opacity',
			},
			{
				type: 'sequence-effect-prop',
				nodePathInfo: intensityNodePathInfo,
				i: 0,
				key: 'intensity',
			},
		],
		tracks: calculate({
			sequences: [
				makeTimelineSequence({
					schema,
					effects: [{schema: effectSchema}],
				}),
			],
			overrideIdsToNodePaths: {override: nodePath},
		}).tracks,
		propStatuses,
	});

	expect(resetTargets?.map((target) => target.type)).toEqual([
		'sequence-prop',
		'effect-prop',
	]);
	expect(resetTargets?.map((target) => target.fieldKey)).toEqual([
		'opacity',
		'intensity',
	]);
	expect(resetTargets?.map((target) => target.value)).toEqual([1, 0]);
});

test('Backspace reset ignores incompatible mixed prop selections', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {
		opacity: {type: 'number', default: 1, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const opacityNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'opacity'],
	);

	expect(
		getTimelinePropResetTargets({
			selections: [
				{
					type: 'sequence-prop',
					nodePathInfo: opacityNodePathInfo,
					key: 'opacity',
				},
				{
					type: 'keyframe',
					nodePathInfo: opacityNodePathInfo,
					frame: 12,
				},
			],
			tracks: calculate({
				sequences: [makeTimelineSequence({schema})],
				overrideIdsToNodePaths: {
					override: opacityNodePathInfo.sequenceSubscriptionKey,
				},
			}).tracks,
			propStatuses: makeDurationPropStatuses([
				opacityNodePathInfo.sequenceSubscriptionKey,
			]),
		}),
	).toBe(null);
});

test('Deleting unsupported mixed timeline selection types returns null', () => {
	const sequenceNodePathInfo = makeNodePathInfo(['body', 0], []);
	const effectNodePathInfo = makeNodePathInfo(['body', 1], ['effects', '0']);
	const confirm = () => Promise.resolve(true);

	expect(
		deleteSelectedTimelineItems({
			selections: [
				{type: 'sequence', nodePathInfo: sequenceNodePathInfo},
				{
					type: 'sequence-effect',
					nodePathInfo: effectNodePathInfo,
					i: 0,
				},
			],
			tracks: [],
			setPropStatuses: () => undefined,
			clientId: 'client',
			confirm,
			propStatuses: {},
			timelinePosition: 0,
		}),
	).toBe(null);
});

test('Deleting selected effects keeps their parent sequence selected', () => {
	const effectNodePathInfo = makeNodePathInfo(['body', 0], ['effects', '1']);
	const result = getTimelineSelectionAfterDeletingItems({
		selections: [
			{
				type: 'sequence-effect',
				nodePathInfo: effectNodePathInfo,
				i: 1,
			},
		],
	});

	expect(result).toEqual([
		{
			type: 'sequence',
			nodePathInfo: {
				...effectNodePathInfo,
				auxiliaryKeys: [],
			},
		},
	]);
});

test('Deleting selected all-effects rows keeps their parent sequence selected', () => {
	const effectsNodePathInfo = makeNodePathInfo(['body', 0], ['effects']);
	const result = getTimelineSelectionAfterDeletingItems({
		selections: [
			{
				type: 'sequence-all-effects',
				nodePathInfo: effectsNodePathInfo,
			},
		],
	});

	expect(result).toEqual([
		{
			type: 'sequence',
			nodePathInfo: {
				...effectsNodePathInfo,
				auxiliaryKeys: [],
			},
		},
	]);
});

test('Deleting selected sequences still clears selection', () => {
	const sequenceNodePathInfo = makeNodePathInfo(['body', 0], []);

	expect(
		getTimelineSelectionAfterDeletingItems({
			selections: [{type: 'sequence', nodePathInfo: sequenceNodePathInfo}],
		}),
	).toEqual([]);
});

test('Deleting a selected keyframe selects its property', () => {
	const opacityNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'opacity'],
	);

	expect(
		getTimelineSelectionAfterDeletingItems({
			selections: [
				{
					type: 'keyframe',
					nodePathInfo: opacityNodePathInfo,
					frame: 10,
				},
			],
		}),
	).toEqual([
		{
			type: 'sequence-prop',
			nodePathInfo: opacityNodePathInfo,
			key: 'opacity',
		},
	]);
});

test('Deleting keyframes across properties selects each affected property once', () => {
	const opacityNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'style.opacity'],
	);
	const effectNodePathInfo = makeNodePathInfo(
		['body', 1],
		['effects', '1', 'radius'],
	);

	expect(
		getTimelineSelectionAfterDeletingItems({
			selections: [opacityNodePathInfo, effectNodePathInfo].flatMap(
				(nodePathInfo): TimelineSelection[] => [
					{type: 'keyframe', nodePathInfo, frame: 10},
					{type: 'keyframe', nodePathInfo, frame: 20},
					{
						type: 'easing',
						nodePathInfo,
						fromFrame: 10,
						toFrame: 20,
						segmentIndex: 1,
					},
				],
			),
		}),
	).toEqual([
		{
			type: 'sequence-prop',
			nodePathInfo: opacityNodePathInfo,
			key: 'style.opacity',
		},
		{
			type: 'sequence-effect-prop',
			nodePathInfo: effectNodePathInfo,
			i: 1,
			key: 'radius',
		},
	]);
});

test('Deleting selected keyframes ignores selected easings', async () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {
		opacity: {type: 'number', default: 1, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const opacityNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'opacity'],
	);
	const nodePath = opacityNodePathInfo.sequenceSubscriptionKey;
	const fetchCalls: unknown[] = [];
	const previousFetch = globalThis.fetch;
	globalThis.fetch = ((input, init) => {
		fetchCalls.push({
			input,
			body:
				typeof init?.body === 'string'
					? JSON.parse(init.body)
					: (init?.body ?? null),
		});

		return Promise.resolve({
			json: () => Promise.resolve({success: true, data: {}}),
		} as Response);
	}) as typeof fetch;

	try {
		const result = await deleteSelectedTimelineItems({
			selections: [
				{
					type: 'keyframe',
					nodePathInfo: opacityNodePathInfo,
					frame: 12,
				},
				{
					type: 'easing',
					nodePathInfo: opacityNodePathInfo,
					fromFrame: 12,
					toFrame: 24,
					segmentIndex: 0,
				},
			],
			tracks: calculate({
				sequences: [makeTimelineSequence({schema})],
				overrideIdsToNodePaths: {override: nodePath},
			}).tracks,
			setPropStatuses: () => undefined,
			clientId: 'client',
			confirm: () => Promise.resolve(true),
			propStatuses: {},
			timelinePosition: 0,
		});

		expect(result).toBe(true);
		expect(fetchCalls).toEqual([
			{
				input: '/api/delete-keyframes',
				body: {
					sequenceKeyframes: [
						{
							fileName: '/project/src/Comp.tsx',
							nodePath,
							key: 'opacity',
							frame: 12,
							schema,
							valueWhenLastKeyframeDeleted: null,
						},
					],
					effectKeyframes: [],
					clientId: 'client',
				},
			},
		]);
	} finally {
		globalThis.fetch = previousFetch;
	}
});

test('Deleting all selected keyframes preserves the value at the playhead', async () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const schema = {
		opacity: {type: 'number', default: 1, hiddenFromList: false},
	} satisfies InteractivitySchema;
	const opacityNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'opacity'],
	);
	const nodePath = opacityNodePathInfo.sequenceSubscriptionKey;
	const propStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {
				opacity: {
					status: 'keyframed',
					keyframeDisplayOffsetAdjustment: null,
					interpolationFunction: 'interpolate',
					keyframes: [
						{frame: 0, value: 0},
						{frame: 10, value: 1},
					],
					easing: [{type: 'linear'}],
					clamping: {left: 'extend', right: 'extend'},
					posterize: undefined,
					output: undefined,
				},
			},
			effects: [],
		},
	} satisfies PropStatuses;
	const fetchCalls: unknown[] = [];
	const previousFetch = globalThis.fetch;
	globalThis.fetch = ((input, init) => {
		fetchCalls.push({
			input,
			body:
				typeof init?.body === 'string'
					? JSON.parse(init.body)
					: (init?.body ?? null),
		});

		return Promise.resolve({
			json: () => Promise.resolve({success: true, data: {}}),
		} as Response);
	}) as typeof fetch;

	try {
		const result = await deleteSelectedTimelineItems({
			selections: [
				{
					type: 'keyframe',
					nodePathInfo: opacityNodePathInfo,
					frame: 0,
				},
				{
					type: 'keyframe',
					nodePathInfo: opacityNodePathInfo,
					frame: 10,
				},
			],
			tracks: calculate({
				sequences: [makeTimelineSequence({schema})],
				overrideIdsToNodePaths: {override: nodePath},
			}).tracks,
			setPropStatuses: () => undefined,
			clientId: 'client',
			confirm: () => Promise.resolve(true),
			propStatuses,
			timelinePosition: 5,
		});

		expect(result).toBe(true);
		expect(fetchCalls).toEqual([
			{
				input: '/api/delete-keyframes',
				body: {
					sequenceKeyframes: [
						{
							fileName: '/project/src/Comp.tsx',
							nodePath,
							key: 'opacity',
							frame: 0,
							schema,
							valueWhenLastKeyframeDeleted: 0.5,
						},
						{
							fileName: '/project/src/Comp.tsx',
							nodePath,
							key: 'opacity',
							frame: 10,
							schema,
							valueWhenLastKeyframeDeleted: 0.5,
						},
					],
					effectKeyframes: [],
					clientId: 'client',
				},
			},
		]);
	} finally {
		globalThis.fetch = previousFetch;
	}
});

test('Child timeline selections resolve to the parent sequence selection key', () => {
	const sequenceNodePathInfo = makeNodePathInfo(['body', 0], []);
	const effectNodePathInfo = makeNodePathInfo(['body', 0], ['effects', '0']);
	const propNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'opacity'],
	);

	expect(getTimelineSequenceSelectionKey(effectNodePathInfo)).toBe(
		getTimelineSequenceSelectionKey(sequenceNodePathInfo),
	);
	expect(getTimelineSequenceSelectionKey(propNodePathInfo)).toBe(
		getTimelineSequenceSelectionKey(sequenceNodePathInfo),
	);
});

test('Timeline row selections use explicit item variants', () => {
	const sequenceNodePathInfo = makeNodePathInfo(['body', 0], []);
	const sequencePropNodePathInfo = makeNodePathInfo(
		['body', 0],
		['controls', 'opacity'],
	);
	const allEffectsNodePathInfo = makeNodePathInfo(['body', 0], ['effects']);
	const effectNodePathInfo = makeNodePathInfo(['body', 0], ['effects', '1']);
	const effectPropNodePathInfo = makeNodePathInfo(
		['body', 0],
		['effects', '1', 'blur'],
	);

	expect(getTimelineSelectionFromNodePathInfo(sequenceNodePathInfo)).toEqual({
		type: 'sequence',
		nodePathInfo: sequenceNodePathInfo,
	});
	expect(
		getTimelineSelectionFromNodePathInfo(sequencePropNodePathInfo),
	).toEqual({
		type: 'sequence-prop',
		nodePathInfo: sequencePropNodePathInfo,
		key: 'opacity',
	});
	expect(getTimelineSelectionFromNodePathInfo(allEffectsNodePathInfo)).toEqual({
		type: 'sequence-all-effects',
		nodePathInfo: allEffectsNodePathInfo,
	});
	expect(getTimelineSelectionFromNodePathInfo(effectNodePathInfo)).toEqual({
		type: 'sequence-effect',
		nodePathInfo: effectNodePathInfo,
		i: 1,
	});
	expect(getTimelineSelectionFromNodePathInfo(effectPropNodePathInfo)).toEqual({
		type: 'sequence-effect-prop',
		nodePathInfo: effectPropNodePathInfo,
		i: 1,
		key: 'blur',
	});
	expect(
		getTimelineSelectionFromNodePathInfo(
			makeNodePathInfo(['body', 0], ['effects', 'not-a-number']),
		),
	).toBe(null);
});

test('Cmd/Ctrl+click toggles row selections', () => {
	const rowA = {
		type: 'sequence' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], []),
	};
	const rowB = {
		type: 'sequence' as const,
		nodePathInfo: makeNodePathInfo(['body', 1], []),
	};
	const allSelectableItems = [rowA, rowB];

	expect(
		getTimelineSelectionAfterInteraction({
			currentState: {
				selectedItems: [rowA],
				anchor: rowA,
			},
			clickedItem: rowB,
			interaction: {shiftKey: false, toggleKey: true},
			allSelectableItems,
		}),
	).toEqual({
		selectedItems: [rowA, rowB],
		anchor: rowB,
	});

	expect(
		getTimelineSelectionAfterInteraction({
			currentState: {
				selectedItems: [rowA, rowB],
				anchor: rowB,
			},
			clickedItem: rowA,
			interaction: {shiftKey: false, toggleKey: true},
			allSelectableItems,
		}),
	).toEqual({
		selectedItems: [rowB],
		anchor: rowA,
	});
});

test('Cmd/Ctrl+click mixes compatible prop row selections', () => {
	const sequenceProp = {
		type: 'sequence-prop' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], ['controls', 'opacity']),
		key: 'opacity',
	};
	const effectProp = {
		type: 'sequence-effect-prop' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], ['effects', '0', 'intensity']),
		i: 0,
		key: 'intensity',
	};
	const allSelectableItems = [sequenceProp, effectProp];

	expect(
		getTimelineSelectionAfterInteraction({
			currentState: {
				selectedItems: [sequenceProp],
				anchor: sequenceProp,
			},
			clickedItem: effectProp,
			interaction: {shiftKey: false, toggleKey: true},
			allSelectableItems,
		}),
	).toEqual({
		selectedItems: [sequenceProp, effectProp],
		anchor: effectProp,
	});

	expect(
		getTimelineSelectionAfterInteraction({
			currentState: {
				selectedItems: [sequenceProp, effectProp],
				anchor: effectProp,
			},
			clickedItem: effectProp,
			interaction: {shiftKey: false, toggleKey: true},
			allSelectableItems,
		}),
	).toEqual({
		selectedItems: [sequenceProp],
		anchor: effectProp,
	});
});

test('Cmd/Ctrl+click mixes compatible keyframe and easing selections', () => {
	const keyframe = {
		type: 'keyframe' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], ['controls', 'opacity']),
		frame: 10,
	};
	const easing = {
		type: 'easing' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], ['controls', 'opacity']),
		fromFrame: 10,
		toFrame: 20,
		segmentIndex: 0,
	};

	expect(
		getTimelineSelectionAfterInteraction({
			currentState: {
				selectedItems: [keyframe],
				anchor: keyframe,
			},
			clickedItem: easing,
			interaction: {shiftKey: false, toggleKey: true},
			allSelectableItems: [keyframe, easing],
		}),
	).toEqual({
		selectedItems: [keyframe, easing],
		anchor: easing,
	});
});

test('Cmd/Ctrl+click replaces incompatible mixed selections', () => {
	const sequenceProp = {
		type: 'sequence-prop' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], ['controls', 'opacity']),
		key: 'opacity',
	};
	const keyframe = {
		type: 'keyframe' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], ['controls', 'opacity']),
		frame: 10,
	};

	expect(
		getTimelineSelectionAfterInteraction({
			currentState: {
				selectedItems: [sequenceProp],
				anchor: sequenceProp,
			},
			clickedItem: keyframe,
			interaction: {shiftKey: false, toggleKey: true},
			allSelectableItems: [sequenceProp, keyframe],
		}),
	).toEqual({
		selectedItems: [keyframe],
		anchor: keyframe,
	});
});

test('Guide selections are single selections and incompatible with timeline items', () => {
	const guideA = {type: 'guide' as const, guideId: 'guide-a'};
	const guideB = {type: 'guide' as const, guideId: 'guide-b'};
	const rowA = {
		type: 'sequence' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], []),
	};

	expect(getTimelineSelectionKey(guideA)).toBe('guide.guide-a');
	expect(
		getTimelineSelectionAfterInteraction({
			currentState: {
				selectedItems: [rowA],
				anchor: rowA,
			},
			clickedItem: guideA,
			interaction: {shiftKey: false, toggleKey: true},
			allSelectableItems: [rowA, guideA, guideB],
		}),
	).toEqual({
		selectedItems: [guideA],
		anchor: guideA,
	});
	expect(
		getTimelineSelectionAfterInteraction({
			currentState: {
				selectedItems: [guideA],
				anchor: guideA,
			},
			clickedItem: guideB,
			interaction: {shiftKey: true, toggleKey: false},
			allSelectableItems: [rowA, guideA, guideB],
		}),
	).toEqual({
		selectedItems: [guideB],
		anchor: guideB,
	});
});

test('Shift+click selects a contiguous row range from the anchor', () => {
	const rowA = {
		type: 'sequence' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], []),
	};
	const rowB = {
		type: 'sequence' as const,
		nodePathInfo: makeNodePathInfo(['body', 1], []),
	};
	const rowC = {
		type: 'sequence' as const,
		nodePathInfo: makeNodePathInfo(['body', 2], []),
	};
	const allSelectableItems = [rowA, rowB, rowC];

	expect(
		getTimelineSelectionAfterInteraction({
			currentState: {
				selectedItems: [rowA],
				anchor: rowA,
			},
			clickedItem: rowC,
			interaction: {shiftKey: true, toggleKey: false},
			allSelectableItems,
		}),
	).toEqual({
		selectedItems: [rowA, rowB, rowC],
		anchor: rowA,
	});
});

test('Shift+click can select an inspector effect range', () => {
	const effectA = makeNodePathInfo(['body', 0], ['effects', '0']);
	const effectB = makeNodePathInfo(['body', 0], ['effects', '1']);
	const effectC = makeNodePathInfo(['body', 0], ['effects', '2']);
	const allSelectableItems = getInspectorSelectableItems(
		[effectA, effectB, effectC].map((nodePathInfo) => ({
			depth: 0,
			node: {
				kind: 'group' as const,
				nodePathInfo,
				label: 'Effect',
				effectInfo: {
					documentationLink: null,
					effectIndex: Number(nodePathInfo.auxiliaryKeys[1]),
					effectSchema: {},
				},
				children: [],
			},
		})),
	);

	expect(
		getTimelineSelectionAfterInteraction({
			currentState: {
				selectedItems: [{type: 'sequence-effect', nodePathInfo: effectA, i: 0}],
				anchor: {type: 'sequence-effect', nodePathInfo: effectA, i: 0},
			},
			clickedItem: {type: 'sequence-effect', nodePathInfo: effectC, i: 2},
			interaction: {shiftKey: true, toggleKey: false},
			allSelectableItems,
		}),
	).toEqual({
		selectedItems: [
			{type: 'sequence-effect', nodePathInfo: effectA, i: 0},
			{type: 'sequence-effect', nodePathInfo: effectB, i: 1},
			{type: 'sequence-effect', nodePathInfo: effectC, i: 2},
		],
		anchor: {type: 'sequence-effect', nodePathInfo: effectA, i: 0},
	});
});

test('Shift+click with no matching anchor falls back to single selection', () => {
	const rowA = {
		type: 'sequence' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], []),
	};
	const keyframe = {
		type: 'keyframe' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], []),
		frame: 10,
	};

	expect(
		getTimelineSelectionAfterInteraction({
			currentState: {
				selectedItems: [keyframe],
				anchor: keyframe,
			},
			clickedItem: rowA,
			interaction: {shiftKey: true, toggleKey: false},
			allSelectableItems: [rowA],
		}),
	).toEqual({
		selectedItems: [rowA],
		anchor: rowA,
	});
});

test('Selecting an easing segment replaces keyframe selection with the new type', () => {
	const keyframe = {
		type: 'keyframe' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], []),
		frame: 10,
	};
	const easing = {
		type: 'easing' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], []),
		fromFrame: 10,
		toFrame: 20,
		segmentIndex: 0,
	};

	expect(
		getTimelineSelectionAfterInteraction({
			currentState: {
				selectedItems: [keyframe],
				anchor: keyframe,
			},
			clickedItem: easing,
			interaction: {shiftKey: false, toggleKey: false},
			allSelectableItems: [keyframe, easing],
		}),
	).toEqual({
		selectedItems: [easing],
		anchor: easing,
	});
});

test('Dragging an easing segment drags selected keyframes', () => {
	const selectedKeyframeA = {
		type: 'keyframe' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], ['controls', 'opacity']),
		frame: 10,
	};
	const selectedKeyframeB = {
		type: 'keyframe' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], ['controls', 'opacity']),
		frame: 30,
	};
	const easing = {
		type: 'easing' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], ['controls', 'opacity']),
		fromFrame: 10,
		toFrame: 20,
		segmentIndex: 0,
	};

	expect(
		getKeyframesForTimelineEasingDrag({
			currentSelections: [selectedKeyframeA, selectedKeyframeB],
			interaction: {shiftKey: false, toggleKey: false},
			selectionItem: easing,
			selected: false,
		}),
	).toEqual([selectedKeyframeA, selectedKeyframeB]);
});

test('Dragging only selected easing segments drags connected keyframes', () => {
	const easingA = {
		type: 'easing' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], ['controls', 'opacity']),
		fromFrame: 10,
		toFrame: 20,
		segmentIndex: 0,
	};
	const easingB = {
		type: 'easing' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], ['controls', 'opacity']),
		fromFrame: 20,
		toFrame: 30,
		segmentIndex: 1,
	};

	expect(
		getKeyframesForTimelineEasingDrag({
			currentSelections: [easingA, easingB],
			interaction: {shiftKey: false, toggleKey: false},
			selectionItem: easingA,
			selected: true,
		}),
	).toEqual([
		{
			type: 'keyframe',
			nodePathInfo: easingA.nodePathInfo,
			frame: 10,
		},
		{
			type: 'keyframe',
			nodePathInfo: easingA.nodePathInfo,
			frame: 20,
		},
		{
			type: 'keyframe',
			nodePathInfo: easingB.nodePathInfo,
			frame: 30,
		},
	]);
});

test('Easing selection keys follow segment identity while frames move', () => {
	const nodePathInfo = makeNodePathInfo(['body', 0], ['controls', 'opacity']);

	expect(
		getTimelineSelectionKey({
			type: 'easing',
			nodePathInfo,
			fromFrame: 10,
			toFrame: 20,
			segmentIndex: 0,
		}),
	).toBe(
		getTimelineSelectionKey({
			type: 'easing',
			nodePathInfo,
			fromFrame: 15,
			toFrame: 25,
			segmentIndex: 0,
		}),
	);
});

test('Easing keyframe drag preserves selected item types at moved frames', () => {
	const nodePathInfo = makeNodePathInfo(['body', 0], ['controls', 'opacity']);
	const selectedKeyframe = {
		type: 'keyframe' as const,
		nodePathInfo,
		frame: 10,
	};
	const selectedEasing = {
		type: 'easing' as const,
		nodePathInfo,
		fromFrame: 10,
		toFrame: 20,
		segmentIndex: 0,
	};

	expect(
		getTimelineSelectionsAfterEasingKeyframeDrag({
			delta: 5,
			selections: [selectedKeyframe, selectedEasing],
			targets: [
				{nodePathInfo, frame: 10},
				{nodePathInfo, frame: 20},
			],
		}),
	).toEqual([
		{
			...selectedKeyframe,
			frame: 15,
		},
		{
			...selectedEasing,
			fromFrame: 15,
			toFrame: 25,
		},
	]);
});

test('Unavailable timeline selections are removed from the active selection state', () => {
	const availableRow = {
		type: 'sequence' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], []),
	};
	const unavailableRow = {
		type: 'sequence' as const,
		nodePathInfo: makeNodePathInfo(['body', 1], []),
	};

	expect(
		getAvailableTimelineSelectionState({
			availableKeys: new Set([getTimelineSelectionKey(availableRow)]),
			state: {
				selectedItems: [availableRow, unavailableRow],
				anchor: unavailableRow,
			},
		}),
	).toEqual({
		selectedItems: [availableRow],
		anchor: null,
	});
});

test('Available timeline selections are refreshed with the latest selectable item', () => {
	const staleRow = {
		type: 'sequence' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], [], true, [['oldEffect']]),
	};
	const currentRow = {
		type: 'sequence' as const,
		nodePathInfo: makeNodePathInfo(['body', 0], [], true, [
			['oldEffect'],
			['newEffect'],
		]),
	};
	const currentRowKey = getTimelineSelectionKey(currentRow);

	const result = getAvailableTimelineSelectionState({
		availableKeys: new Set([currentRowKey]),
		availableItemsByKey: new Map([[currentRowKey, currentRow]]),
		state: {
			selectedItems: [staleRow],
			anchor: staleRow,
		},
	});

	expect(result.selectedItems[0]).toBe(currentRow);
	expect(result.anchor).toBe(currentRow);
});

test('Unavailable timeline selections become no active selection', () => {
	const unavailableRow = {
		type: 'sequence' as const,
		nodePathInfo: makeNodePathInfo(['body', 1], []),
	};

	expect(
		getAvailableTimelineSelectionState({
			availableKeys: new Set(),
			state: {
				selectedItems: [unavailableRow],
				anchor: unavailableRow,
			},
		}),
	).toEqual({
		selectedItems: [],
		anchor: null,
	});
});

test('Timeline double-click actions ignore selection modifier clicks', () => {
	expect(
		isTimelineSelectionModifierEvent({
			shiftKey: true,
			metaKey: false,
			ctrlKey: false,
		}),
	).toBe(true);
	expect(
		isTimelineSelectionModifierEvent({
			shiftKey: false,
			metaKey: true,
			ctrlKey: false,
		}),
	).toBe(true);
	expect(
		isTimelineSelectionModifierEvent({
			shiftKey: false,
			metaKey: false,
			ctrlKey: true,
		}),
	).toBe(true);
	expect(
		isTimelineSelectionModifierEvent({
			shiftKey: false,
			metaKey: false,
			ctrlKey: false,
		}),
	).toBe(false);
});

test('Selected timeline rows do not reselect on pointer down without modifiers', () => {
	expect(
		shouldSelectTimelineRowOnPointerDown({
			selected: true,
			shiftKey: false,
			metaKey: false,
			ctrlKey: false,
		}),
	).toBe(false);
	expect(
		shouldSelectTimelineRowOnPointerDown({
			selected: false,
			shiftKey: false,
			metaKey: false,
			ctrlKey: false,
		}),
	).toBe(true);
	expect(
		shouldSelectTimelineRowOnPointerDown({
			selected: true,
			shiftKey: false,
			metaKey: true,
			ctrlKey: false,
		}),
	).toBe(true);
});

test.each([
	{
		name: 'negative ancestor',
		parentFrom: -30,
		parentDuration: 300,
		parentTrimBefore: 0,
		childFrom: 10,
		childDuration: 60,
		deltaFrames: -10,
		expectedFrom: 0,
	},
	{
		name: 'trimmed ancestor',
		parentFrom: 0,
		parentDuration: 300,
		parentTrimBefore: 30,
		childFrom: 10,
		childDuration: 60,
		deltaFrames: -10,
		expectedFrom: 0,
	},
	{
		name: 'clipped trailing edge',
		parentFrom: 0,
		parentDuration: 60,
		parentTrimBefore: 0,
		childFrom: 40,
		childDuration: 40,
		deltaFrames: 10,
		expectedFrom: 50,
	},
])('Timeline moves preserve existing ancestor clipping: $name', (scenario) => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const nodePathInfo = makeNodePathInfo(['body', 0, 'children', 0], []);
	const nodePath = nodePathInfo.sequenceSubscriptionKey;
	const parent = makeTimelineSequence({
		schema: {},
		id: 'parent',
		overrideId: 'parent',
		from: scenario.parentFrom,
		duration: scenario.parentDuration,
		trimBefore: scenario.parentTrimBefore,
	});
	const child = makeTimelineSequence({
		schema: {},
		id: 'child',
		overrideId: 'child',
		parentId: parent.id,
		from: scenario.childFrom,
		duration: scenario.childDuration,
	});
	const sequences = [parent, child];
	const overrideIdsToNodePaths = {child: nodePath};
	const propStatuses = makeFromPropStatuses([nodePath]);
	const {tracks} = calculate({sequences, overrideIdsToNodePaths});
	const limits = getTimelineSequenceTimingLimits({
		track: tracks.find((t) => t.sequence.id === child.id)!,
		tracks,
		sequences,
		propStatuses,
		timelineDurationInFrames: 600,
		movingSequenceIds: null,
	});
	// Re-entering the existing From in the inspector must preserve it.
	expect(
		Math.max(limits.minimumFrom, Math.min(limits.maximumFrom, child.from)),
	).toBe(child.from);
	expect(scenario.expectedFrom).toBeGreaterThanOrEqual(limits.minimumFrom);
	expect(scenario.expectedFrom).toBeLessThanOrEqual(limits.maximumFrom);
	const targets = getTimelineSequenceFromDragTargets({
		draggedNodePathInfo: nodePathInfo,
		selectedItems: [],
		sequences,
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths,
		}).tracks,
		overrideIdsToNodePaths,
		propStatuses,
	});
	expect(targets).not.toBeNull();
	const deltaFrames = getTimelineSequenceFromDragDelta({
		targets: targets!,
		deltaFrames: scenario.deltaFrames,
		timelineDurationInFrames: 600,
		pxPerFrame: 10,
		snappingEnabled: false,
	});
	const changes = getTimelineSequenceFromDragChanges({
		targets: targets!,
		deltaFrames,
	});
	expect(changes.map((c) => c.value)).toEqual([scenario.expectedFrom]);
	const movedTracks = calculate({
		sequences: [parent, {...child, from: Number(changes[0].value)}],
		overrideIdsToNodePaths,
	}).tracks;
	expect(
		movedTracks.find((t) => t.sequence.id === child.id)!.sequence.duration,
	).toBeGreaterThanOrEqual(1);
});

test.each([
	{parentPlaybackRate: 1, media: false},
	{parentPlaybackRate: 2, media: false},
	{parentPlaybackRate: 1, media: true},
])(
	'Timeline moves stop at Track neighbours after timing edits: %j',
	({parentPlaybackRate, media}) => {
		const calculate = CanvasInternals.createTimelineCalculator();
		const nodePathInfo = makeNodePathInfo(['body', 0, 'children', 1], []);
		const nodePath = nodePathInfo.sequenceSubscriptionKey;
		const parent = makeTimelineSequence({
			schema: {},
			id: 'track',
			overrideId: 'track',
			from: 30,
			duration: 400,
			sequencePlaybackRate: parentPlaybackRate,
			componentIdentity: 'dev.remotion.remotion.Track',
		});
		parent.timelineTrack = {
			id: parent.id,
			role: 'track',
			name: 'Track',
			seriesOffset: null,
		};
		const previous = makeTimelineSequence({
			schema: {},
			id: 'previous',
			overrideId: 'previous',
			parentId: parent.id,
			duration: 80,
		});
		const child = makeTimelineSequence({
			schema: Internals.sequenceSchema,
			id: 'child',
			overrideId: 'child',
			parentId: parent.id,
			from: 100,
			duration: media ? 34 : 40,
			type: media ? 'video' : 'sequence',
			componentIdentity: media ? 'dev.remotion.media.Video' : null,
			currentRuntimeValueDotNotation: media
				? {durationInFrames: 42, playbackRate: 2, trimBefore: 30}
				: {},
		});
		const next = makeTimelineSequence({
			schema: {},
			id: 'next',
			overrideId: 'next',
			parentId: parent.id,
			from: 160,
			duration: 60,
		});
		for (const item of [previous, child, next])
			item.timelineTrack = {
				id: parent.id,
				role: 'clip',
				name: 'Track',
				seriesOffset: null,
			};
		const sequences = [parent, previous, child, next];
		const overrideIdsToNodePaths = {child: nodePath};
		const propStatuses = makeFromPropStatuses([nodePath]);
		if (media) {
			const status =
				propStatuses[Internals.makeSequencePropsSubscriptionKey(nodePath)];
			if (!status.canUpdate) throw new Error('Expected editable media');
			status.props.durationInFrames = {
				status: 'static',
				codeValue: 68,
				keyframeDisplayOffsetAdjustment: null,
			};
		}

		const targets = getTimelineSequenceFromDragTargets({
			draggedNodePathInfo: nodePathInfo,
			selectedItems: [],
			sequences,
			tracks: calculate({
				sequences,
				overrideIdsToNodePaths,
			}).tracks,
			overrideIdsToNodePaths,
			propStatuses,
		});
		expect(targets).not.toBeNull();
		for (const [requestedDelta, expectedFrom] of [
			[-1000, 80],
			[1000, media ? 126 : 120],
		]) {
			const deltaFrames = getTimelineSequenceFromDragDelta({
				targets: targets!,
				deltaFrames: requestedDelta,
				timelineDurationInFrames: 600,
				pxPerFrame: 10,
				snappingEnabled: false,
			});
			const changes = getTimelineSequenceFromDragChanges({
				targets: targets!,
				deltaFrames,
			});
			expect(changes.map((c) => c.value)).toEqual([expectedFrom]);
			const movedTracks = calculate({
				sequences: [
					parent,
					previous,
					{...child, from: Number(changes[0].value)},
					next,
				],
				overrideIdsToNodePaths,
			}).tracks;
			const moved = movedTracks.find(
				(t) => t.sequence.id === child.id,
			)!.sequence;
			const before = movedTracks.find(
				(t) => t.sequence.id === previous.id,
			)!.sequence;
			const after = movedTracks.find(
				(t) => t.sequence.id === next.id,
			)!.sequence;
			expect(moved.from).toBeGreaterThanOrEqual(before.from + before.duration);
			expect(moved.from + moved.duration).toBeLessThanOrEqual(after.from);
		}
	},
);