🎥 Make videos programmatically with React
`@remotion/studio`: Restore the persisted playback rate after a reload
SignalZerobe55ba63eb9a
diff --git a/packages/studio/src/state/playbackrate.ts b/packages/studio/src/state/playbackrate.tsindex 8fd0a296e1..c2dea1a0a6 100644--- a/packages/studio/src/state/playbackrate.ts+++ b/packages/studio/src/state/playbackrate.ts@@ -9,7 +9,7 @@ export const persistPlaybackRate = (option: number) => {};export const loadPlaybackRate = () => {- if (typeof window !== 'undefined') {+ if (typeof window === 'undefined') {return 1;}@@ -18,5 +18,8 @@ export const loadPlaybackRate = () => {return 1;}- return Number(item);+ // Only restore rates the Studio can select, so a stale or corrupted value+ // (e.g. 0 or NaN) cannot stall playback after a reload.+ const playbackRate = Number(item);+ return commonPlaybackRates.includes(playbackRate) ? playbackRate : 1;};diff --git a/packages/studio/src/test/playback-rate-persistence.test.ts b/packages/studio/src/test/playback-rate-persistence.test.tsnew file mode 100644index 0000000000..caeb8afa1d--- /dev/null+++ b/packages/studio/src/test/playback-rate-persistence.test.ts@@ -0,0 +1,85 @@+import {afterEach, beforeEach, expect, test} from 'bun:test';+import {loadPlaybackRate, persistPlaybackRate} from '../state/playbackrate';++const storage = new Map<string, string>();++// Bun runs all test files in one process, so restore the globals afterwards+// instead of leaving them deleted for the files that run next.+const stubbedGlobals = ['window', 'localStorage'] as const;+let originalDescriptors: (PropertyDescriptor | undefined)[] = [];++beforeEach(() => {+ originalDescriptors = stubbedGlobals.map((name) =>+ Object.getOwnPropertyDescriptor(globalThis, name),+ );+ storage.clear();+ Object.defineProperty(globalThis, 'window', {+ configurable: true,+ value: globalThis,+ });+ Object.defineProperty(globalThis, 'localStorage', {+ configurable: true,+ value: {+ getItem: (key: string) => storage.get(key) ?? null,+ setItem: (key: string, value: string) => {+ storage.set(key, value);+ },+ },+ });+});++afterEach(() => {+ stubbedGlobals.forEach((name, i) => {+ const descriptor = originalDescriptors[i];+ if (descriptor) {+ Object.defineProperty(globalThis, name, descriptor);+ } else {+ Reflect.deleteProperty(globalThis, name);+ }+ });+});++test('restores a persisted playback rate in the browser', () => {+ persistPlaybackRate(0.5);+ expect(loadPlaybackRate()).toBe(0.5);+});++test('restores a persisted negative playback rate', () => {+ persistPlaybackRate(-2);+ expect(loadPlaybackRate()).toBe(-2);+});++test('falls back to 1 when no playback rate was persisted', () => {+ const readKeys: string[] = [];+ Object.defineProperty(globalThis, 'localStorage', {+ configurable: true,+ value: {+ getItem: (key: string) => {+ readKeys.push(key);+ return null;+ },+ },+ });+ expect(loadPlaybackRate()).toBe(1);+ expect(readKeys).toEqual(['remotion.playbackrate']);+});++test('falls back to 1 when the persisted value is not a supported playback rate', () => {+ for (const invalid of ['abc', '', '0', 'Infinity', '3']) {+ storage.set('remotion.playbackrate', invalid);+ expect(loadPlaybackRate()).toBe(1);+ }+});++test('falls back to 1 without reading storage when there is no window', () => {+ Reflect.deleteProperty(globalThis, 'window');+ Object.defineProperty(globalThis, 'localStorage', {+ configurable: true,+ value: {+ getItem: () => {+ throw new Error('localStorage should not be read');+ },+ },+ });+ expect(loadPlaybackRate()).toBe(1);+});