🎥 Make videos programmatically with React
Test keyframe trimming, lazy loading, and tooltip anchoring
JonnyBurgerc7c4d8e19554
diff --git a/packages/studio/src/test/keyframe-trim-constraints.test.ts b/packages/studio/src/test/keyframe-trim-constraints.test.tsnew file mode 100644index 0000000000..9f870a8aa7--- /dev/null+++ b/packages/studio/src/test/keyframe-trim-constraints.test.ts@@ -0,0 +1,371 @@+import {expect, test} from 'bun:test';+import {CodemodsInternals, getNodeProps, getNodes} from '@remotion/codemods';+import type {EventSourceEvent} from '@remotion/studio-shared';+import type {+ OverrideIdToNodePaths,+ PropStatuses,+ SequencePropsSubscriptionKey,+ TSequence,+} from 'remotion';+import {Internals, interpolate} from 'remotion';+import {loadKeyframeTrimConstraints} from '../components/Timeline/load-keyframe-trim-constraints';+import {+ getTimelineSequenceDurationDragChanges,+ getTimelineSequenceDurationDragTargets,+} from '../components/Timeline/TimelineSequenceRightEdgeDragHandle';+import {calculateTimeline} from '../helpers/calculate-timeline';+import {makeBrowserStudioOperations} from './make-browser-studio-operations';+import {makeRuntimeValueStore} from './make-runtime-value-store';++const config = (durationInFrames: number) => ({+ durationInFrames,+ fps: 30,+ width: 1920,+ height: 1080,+});++const makeSequence = ({+ id,+ parent,+ duration,+ contextDuration,+ runtime,+}: {+ id: string;+ parent: string | null;+ duration: number;+ contextDuration: number;+ runtime: Record<string, unknown>;+}): TSequence => ({+ id,+ parent,+ duration,+ from: 0,+ trimBefore: 0,+ type: 'sequence',+ sequencePlaybackRate: 1,+ displayName: id,+ documentationLink: null,+ showInTimeline: true,+ timelineOrder: null,+ loopDisplay: undefined,+ getStack: () => null,+ premountDisplay: null,+ postmountDisplay: null,+ refForOutline: null,+ effects: [],+ effectRuntimeValues: null,+ isInsideSeries: false,+ frozenFrame: null,+ controls: {+ schema: {},+ runtimeValues: makeRuntimeValueStore(runtime),+ overrideId: id,+ supportsEffects: true,+ componentIdentity: 'dev.remotion.remotion.Sequence',+ componentName: '<Sequence>',+ videoConfigValues: config(contextDuration),+ },+});++const makeFixture = (+ range: string,+ placement: 'child' | 'property' | 'effect',+ duration: number,+) => {+ const animation = 'interpolate(frame, frames, [0, 9, 9, 0])';+ const files: Record<string, string> = {+ '/trim-test/parent.tsx': `import {Sequence} from 'remotion'; export const App = () => <Sequence durationInFrames={${duration}}><Content /></Sequence>;`,+ '/trim-test/content.tsx': `import {AbsoluteFill, interpolate, useCurrentFrame, useVideoConfig} from 'remotion';+import {blur} from '@remotion/effects';+export const Content = () => {+ const {durationInFrames} = useVideoConfig(); const frame = useCurrentFrame(); const frames = ${range};+ return <AbsoluteFill ${placement === 'property' ? `opacity={${animation}}` : ''} ${placement === 'effect' ? `effects={[blur({radius: ${animation}})]}` : ''}>+ ${placement === 'child' ? `<circle r={${animation}} />` : ''}</AbsoluteFill>;+};`,+ '/trim-test/unrelated.tsx': `import {Sequence} from 'remotion'; export const Other = () => <Sequence durationInFrames={90} />;`,+ };+ const project = {rootDir: '/trim-test', files};+ const mappings: OverrideIdToNodePaths = {};+ const propStatuses: PropStatuses = {};+ for (const [id, fileName] of [+ ['parent', 'parent'],+ ['child', 'content'],+ ['unrelated', 'unrelated'],+ ]) {+ const node = getNodes({project, filePath: `/trim-test/${fileName}.tsx`})[0];+ const nodePath: SequencePropsSubscriptionKey = {+ absolutePath: node.filePath,+ nodePath: node.nodePath,+ sequenceKeys: ['durationInFrames'],+ effectKeys: [],+ videoConfigValues: null,+ };+ mappings[id] = nodePath;+ propStatuses[Internals.makeSequencePropsSubscriptionKey(nodePath)] =+ getNodeProps({+ project,+ node,+ keys: nodePath.sequenceKeys,+ effectKeys: [],+ });+ }++ const sequences = [+ makeSequence({+ id: 'parent',+ parent: null,+ duration,+ contextDuration: 1000,+ runtime: {durationInFrames: duration},+ }),+ makeSequence({+ id: 'child',+ parent: 'parent',+ duration,+ contextDuration: duration,+ runtime: {},+ }),+ makeSequence({+ id: 'unrelated',+ parent: null,+ duration: 90,+ contextDuration: 1000,+ runtime: {durationInFrames: 90},+ }),+ ];+ return {files, mappings, propStatuses, sequences};+};++const prepareSource = (fixture: ReturnType<typeof makeFixture>) => {+ for (const nodePath of Object.values(fixture.mappings)) {+ const key = Internals.makeSequencePropsSubscriptionKey(nodePath);+ const status = fixture.propStatuses[key];+ if (!status?.canUpdate) throw new Error('Expected editable fixture');+ expect(status.durationDependentKeyframes).toBeNull();+ fixture.propStatuses[key] = {+ ...status,+ durationDependentKeyframes:+ CodemodsInternals.getDurationDependentKeyframesFromContent({+ fileContents: fixture.files[nodePath.absolutePath],+ nodePath: nodePath.nodePath,+ }),+ };+ }+};++const getTargets = (+ fixture: ReturnType<typeof makeFixture>,+ propStatuses: PropStatuses,+ id: string,+) => {+ const tracks = calculateTimeline({+ sequences: fixture.sequences,+ overrideIdsToNodePaths: fixture.mappings,+ });+ const nodePathInfo = tracks.find(+ (track) => track.sequence.id === id,+ )?.nodePathInfo;+ if (!nodePathInfo) throw new Error('Expected fixture track');+ return getTimelineSequenceDurationDragTargets({+ draggedNodePathInfo: nodePathInfo,+ draggedSequenceMediaDurationDragLimits: null,+ selectedSequenceMediaDurationDragLimits: null,+ selectedItems: [],+ sequences: fixture.sequences,+ overrideIdsToNodePaths: fixture.mappings,+ propStatuses,+ timelineDurationInFrames: 1000,+ })!;+};++test.each(['child', 'property', 'effect'] as const)(+ 'trimming respects %s keyframes',+ (placement) => {+ const fixture = makeFixture(+ '[10, 21, durationInFrames - 18, durationInFrames - 8]',+ placement,+ 90,+ );+ prepareSource(fixture);+ const changes = getTimelineSequenceDurationDragChanges({+ targets: getTargets(fixture, fixture.propStatuses, 'parent'),+ deltaFrames: -100,+ });+ expect(changes).toMatchObject([+ {+ fileName: '/trim-test/parent.tsx',+ fieldKey: 'durationInFrames',+ value: 40,+ },+ ]);+ expect(interpolate(21, [10, 21, 22, 32], [0, 9, 9, 0])).toBe(9);+ expect(() => interpolate(21, [10, 21, 21, 31], [0, 9, 9, 0])).toThrow(+ 'strictly',+ );+ },+);++test.each([+ ['[0, 10, durationInFrames, 90]', 11, 89],+ ['[0, 10, durationInFrames / 2, durationInFrames - 8]', 21, 1000],+ ['[0, 10, 90 - durationInFrames, durationInFrames]', 46, 79],+ [+ '[0, 10, Math.max(20, durationInFrames - 18), durationInFrames - 8]',+ 60,+ 60,+ ],+] as const)('clamps both ends for %s', (range, minimum, maximum) => {+ const fixture = makeFixture(range, 'child', 60);+ prepareSource(fixture);+ const targets = getTargets(fixture, fixture.propStatuses, 'parent');+ expect(targets[0].minimumDuration).toBe(minimum);+ expect(targets[0].maximumDuration).toBe(maximum);+ for (const [deltaFrames, value] of [+ [-1000, minimum],+ [1000, maximum],+ ]) {+ const changes = getTimelineSequenceDurationDragChanges({+ targets,+ deltaFrames,+ });+ // A locked range produces no source edit.+ expect(changes[0]?.value ?? 60).toBe(value);+ }++ if (minimum === maximum)+ expect(targets[0].minimumDurationReason).toBe('Computed keyframes');+});++test('converts descendant constraints through offsets and playback rates', () => {+ const fixture = makeFixture(+ '[10, 21, durationInFrames - 18, durationInFrames - 8]',+ 'child',+ 90,+ );+ prepareSource(fixture);+ fixture.sequences[0] = {+ ...fixture.sequences[0],+ parent: 'outer',+ from: 10,+ duration: 45,+ sequencePlaybackRate: 2,+ };+ fixture.sequences.unshift(+ makeSequence({+ id: 'outer',+ parent: null,+ duration: 200,+ contextDuration: 1000,+ runtime: {durationInFrames: 200},+ }),+ );+ fixture.mappings.outer = {+ ...fixture.mappings.parent,+ absolutePath: '/trim-test/outer.tsx',+ };+ fixture.propStatuses[+ Internals.makeSequencePropsSubscriptionKey(fixture.mappings.outer)+ ] = {+ canUpdate: true,+ durationDependentKeyframes: null,+ effects: [],+ props: {+ durationInFrames: {+ status: 'static',+ codeValue: 200,+ keyframeDisplayOffsetAdjustment: null,+ },+ },+ };+ const targets = getTargets(fixture, fixture.propStatuses, 'outer');+ expect(+ getTimelineSequenceDurationDragChanges({targets, deltaFrames: -1000})[0]+ .value,+ ).toBe(30);+});++test('loads only affected source, reuses it, and refreshes constraints after source changes', async () => {+ const fixture = makeFixture(+ '[10, 21, durationInFrames - 18, durationInFrames - 8]',+ 'child',+ 90,+ );+ const previousOperations = window.remotion_browserStudio;+ const reads: string[] = [];+ let emit: (event: EventSourceEvent) => void = () => {+ throw new Error('Not subscribed');+ };++ let pendingRead: Promise<string | null> | null = null;+ // Only host file I/O and watcher events are faked; parsing and trim math stay real.+ window.remotion_browserStudio = makeBrowserStudioOperations({+ subscribeToEvent: (listener) => {+ emit = listener;+ return () => undefined;+ },+ getFileSource: (fileName) => {+ reads.push(fileName);+ return pendingRead ?? Promise.resolve(fixture.files[fileName] ?? null);+ },+ });+ const load = () =>+ loadKeyframeTrimConstraints({+ propStatuses: fixture.propStatuses,+ overrideIdsToNodePaths: fixture.mappings,+ sequences: fixture.sequences,+ affectedNodePaths: [fixture.mappings.parent],+ });+ const sourceChanged = () =>+ emit({+ type: 'sequence-props-updated',+ fileName: './content.tsx',+ nodePath: fixture.mappings.child,+ result:+ fixture.propStatuses[+ Internals.makeSequencePropsSubscriptionKey(fixture.mappings.child)+ ]!,+ });+ try {+ const [first, concurrent] = await Promise.all([load(), load()]);+ expect(reads.sort()).toEqual([+ '/trim-test/content.tsx',+ '/trim-test/parent.tsx',+ ]);+ expect(getTargets(fixture, first, 'parent')[0].minimumDuration).toBe(40);+ expect(getTargets(fixture, concurrent, 'parent')[0].minimumDuration).toBe(+ 40,+ );+ await load();+ expect(reads).toHaveLength(2);++ fixture.files['/trim-test/content.tsx'] = fixture.files[+ '/trim-test/content.tsx'+ ].replace('[10, 21,', '[10, 31,');+ sourceChanged();+ const refreshed = await load();+ expect(reads.slice(2)).toEqual(['/trim-test/content.tsx']);+ expect(getTargets(fixture, refreshed, 'parent')[0].minimumDuration).toBe(+ 50,+ );++ let finishRead!: (source: string) => void;+ pendingRead = new Promise((resolve) => {+ finishRead = resolve;+ });+ sourceChanged();+ const stale = load();+ sourceChanged();+ finishRead(fixture.files['/trim-test/content.tsx']);+ await expect(stale).rejects.toThrow('Source changed');+ pendingRead = Promise.resolve(null);+ await expect(load()).rejects.toThrow('Could not load');+ pendingRead = null;+ expect(getTargets(fixture, await load(), 'parent')[0].minimumDuration).toBe(+ 50,+ );+ } finally {+ emit({type: 'hmr', hmrEvent: {action: 'building'}});+ window.remotion_browserStudio = previousOperations;+ }+});diff --git a/packages/studio/src/test/prepared-pointer-session.test.ts b/packages/studio/src/test/prepared-pointer-session.test.tsnew file mode 100644index 0000000000..5d60474750--- /dev/null+++ b/packages/studio/src/test/prepared-pointer-session.test.ts@@ -0,0 +1,131 @@+import {expect, test} from 'bun:test';+import {startPreparedPointerSession} from '../helpers/start-prepared-pointer-session';++test.each(['held', 'released'] as const)(+ 'buffers a %s drag until preparation finishes',+ async (state) => {+ const releaseEarly = state === 'released';+ const target = document.createElement('div');+ let ready!: (value: number) => void;+ const preparation = new Promise<number>((resolve) => {+ ready = resolve;+ });+ const events: string[] = [];+ const stop = startPreparedPointerSession({+ event: new PointerEvent('pointerdown', {+ pointerId: 1,+ button: 0,+ buttons: 1,+ }),+ captureTarget: target,+ prepare: () => preparation,+ onReady: (minimum) => {+ events.push(`ready:${minimum}`);+ return {+ onMove: (event) => events.push(`move:${event.clientX}`),+ onEnd: (reason, event) => events.push(`${reason}:${event?.clientX}`),+ };+ },+ onCancel: () => events.push('cancel'),+ onError: () => events.push('error'),+ });+ try {+ for (const clientX of [10, 20])+ window.dispatchEvent(+ new PointerEvent('pointermove', {pointerId: 1, buttons: 1, clientX}),+ );+ if (releaseEarly)+ window.dispatchEvent(+ new PointerEvent('pointerup', {+ pointerId: 1,+ buttons: 0,+ clientX: 30,+ }),+ );+ expect(events).toEqual([]);+ ready(40);+ await preparation;+ expect(events).toEqual(+ releaseEarly ? ['ready:40', 'pointerup:30'] : ['ready:40', 'move:20'],+ );+ if (!releaseEarly)+ window.dispatchEvent(+ new PointerEvent('pointerup', {+ pointerId: 1,+ buttons: 0,+ clientX: 30,+ }),+ );+ expect(events.at(-1)).toBe('pointerup:30');+ } finally {+ stop();+ }+ },+);++test('cancelling pending preparation cannot restart a completed gesture', async () => {+ for (const cancellation of ['manual', 'pointercancel', 'blur'] as const) {+ let ready!: (value: number) => void;+ const preparation = new Promise<number>((resolve) => {+ ready = resolve;+ });+ const events: string[] = [];+ const stop = startPreparedPointerSession({+ event: new PointerEvent('pointerdown', {+ pointerId: 1,+ button: 0,+ buttons: 1,+ }),+ captureTarget: document.createElement('div'),+ prepare: () => preparation,+ onReady: () => {+ events.push('ready');+ return {+ onMove: () => events.push('move'),+ onEnd: () => events.push('end'),+ };+ },+ onCancel: () => events.push('cancel'),+ onError: () => events.push('error'),+ });+ if (cancellation === 'manual') stop();+ else if (cancellation === 'blur') window.dispatchEvent(new Event('blur'));+ else+ window.dispatchEvent(new PointerEvent('pointercancel', {pointerId: 1}));+ ready(40);+ await preparation;+ stop();+ expect(events).toEqual(['cancel']);+ }+});++test('failed preparation cancels once and reports the error after an early release', async () => {+ let fail!: (error: Error) => void;+ const preparation = new Promise<never>((_resolve, reject) => {+ fail = reject;+ });+ const events: unknown[] = [];+ const error = new Error('Source unavailable');+ const stop = startPreparedPointerSession({+ event: new PointerEvent('pointerdown', {+ pointerId: 1,+ button: 0,+ buttons: 1,+ }),+ captureTarget: document.createElement('div'),+ prepare: () => preparation,+ onReady: () => {+ throw new Error('Must not begin a failed drag');+ },+ onCancel: () => events.push('cancel'),+ onError: (failure) => events.push(failure),+ });+ window.dispatchEvent(+ new PointerEvent('pointerup', {pointerId: 1, buttons: 0}),+ );+ fail(error);+ await preparation.catch(() => undefined);+ await Promise.resolve();+ stop();+ expect(events).toEqual(['cancel', error]);+});diff --git a/packages/studio/src/test/timeline-trim-tooltip.test.tsx b/packages/studio/src/test/timeline-trim-tooltip.test.tsxnew file mode 100644index 0000000000..850414e9d2--- /dev/null+++ b/packages/studio/src/test/timeline-trim-tooltip.test.tsx@@ -0,0 +1,87 @@+import {expect, test} from 'bun:test';+import {cleanup, render} from '@testing-library/react';+import {portals} from '../components/Menu/portals';+import type {TimelineTrimLimitReason} from '../components/Timeline/timeline-trim-limit-reason';+import {+ TimelineTrimTooltip,+ type TimelineTrimTooltipState,+} from '../components/Timeline/TimelineTrimTooltip';++test('trim feedback starts centered, grows left, and hides natural boundary reasons', () => {+ const portal = document.createElement('div');+ document.body.appendChild(portal);+ const previousPortal = portals[0];+ portals[0] = portal;+ const state: TimelineTrimTooltipState = {+ x: 300,+ y: 300,+ deltaFrames: -2,+ edgeFrame: 48,+ limitReason: null,+ };+ try {+ const view = render(<TimelineTrimTooltip state={state} fps={30} />);+ const tooltip = () => portal.firstElementChild as HTMLDivElement;+ const right = () =>+ parseFloat(tooltip().style.left) + parseFloat(tooltip().style.width);+ const initialWidth = parseFloat(tooltip().style.width);+ const initialRight = right();+ expect(parseFloat(tooltip().style.left) + initialWidth / 2).toBe(state.x);++ for (const reason of [+ 'Keyframe constraint',+ 'Computed keyframes',+ 'Unknown media duration',+ 'Transition limit',+ ] as const) {+ view.rerender(+ <TimelineTrimTooltip+ state={{...state, limitReason: reason}}+ fps={30}+ />,+ );+ expect(tooltip().textContent).toContain(reason);+ expect(parseFloat(tooltip().style.width)).toBeGreaterThan(initialWidth);+ expect(right()).toBe(initialRight);+ expect(parseFloat(tooltip().style.top)).toBeLessThan(state.y - 40);+ }++ const hidden: TimelineTrimLimitReason[] = [+ 'Minimum duration',+ 'Source start',+ 'Source end',+ 'Parent edge',+ 'Composition edge',+ 'Adjacent clip',+ 'Content end',+ 'Trim limit',+ ];+ for (const reason of hidden) {+ view.rerender(+ <TimelineTrimTooltip+ state={{...state, x: 330, limitReason: reason}}+ fps={30}+ />,+ );+ expect(tooltip().textContent).not.toContain(reason);+ expect(parseFloat(tooltip().style.width)).toBe(initialWidth);+ expect(right()).toBe(initialRight + 30);+ expect(parseFloat(tooltip().style.top)).toBe(state.y - 40);+ }++ view.unmount();+ render(+ <TimelineTrimTooltip+ state={{...state, limitReason: 'Keyframe constraint'}}+ fps={30}+ />,+ );+ expect(+ parseFloat(tooltip().style.left) + parseFloat(tooltip().style.width) / 2,+ ).toBe(state.x);+ } finally {+ cleanup();+ portals[0] = previousPortal;+ portal.remove();+ }+});