import {expect, test} from 'bun:test'; import {existsSync, readFileSync} from 'fs'; import path from 'path'; import {LambdaClientInternals} from '@remotion/lambda-client'; import {openBrowser} from '@remotion/renderer'; import {VERSION} from 'remotion/version'; test( 'Bundle studio', async () => { const browser = openBrowser('chrome'); const tab = await ( await browser ).newPage({ context: () => null, logLevel: 'info', indent: false, pageIndex: 0, onBrowserLog: null, onLog: console.log, }); const folder = path.join(process.cwd(), '..', 'example', 'build'); const indexHtmlExists = existsSync(path.join(folder, 'index.html')); if (!indexHtmlExists) { throw new Error('index.html does not exist in the build folder'); } const bundleJs = existsSync(path.join(folder, 'bundle.js')); if (!bundleJs) { throw new Error('bundle.js does not exist in the build folder'); } const contents = readFileSync(path.join(folder, 'bundle.js'), 'utf-8'); if (contents.includes('PreviewToolbar') || contents.includes('TopPanel')) { throw new Error('Studio was bundled'); } // remotion/no-react must not pull in another stateful delay-render module. expect( contents.match(/window\.remotion_delayRenderHandles = \[\]/g), ).toHaveLength(1); const indexHtmlContent = readFileSync( path.join(folder, 'index.html'), 'utf-8', ); const version = LambdaClientInternals.getRemotionVersionFromIndexHtml(indexHtmlContent); expect(version).toBe(VERSION); const server = Bun.serve({ port: 0, fetch: async (request) => { const url = new URL(request.url); const requestedPath = url.pathname === '/' ? 'index.html' : url.pathname.slice(1); const filePath = path.resolve(folder, requestedPath); if (!filePath.startsWith(`${folder}${path.sep}`)) { return new Response('Forbidden', {status: 403}); } const file = Bun.file(filePath); if (!(await file.exists())) { return new Response('Not found', {status: 404}); } return new Response(file); }, }); await tab.goto({ url: `http://localhost:${server.port}/`, timeout: 10000, options: {}, }); const initialState = await tab.evaluateHandle(async () => { for (let attempt = 0; attempt < 100; attempt++) { if ( document.querySelector('[data-compname="WidthHeight"]') && window.location.search === '' ) { return 'ready'; } await new Promise((resolve) => setTimeout(resolve, 100)); } return 'timed-out'; }); expect(initialState.toString()).toBe('ready'); const compositionSelected = await tab.evaluateHandle(async () => { const composition = document.querySelector( '[data-compname="WidthHeight"]', ); composition?.click(); for (let attempt = 0; attempt < 100; attempt++) { if (window.location.search === '?/WidthHeight') { return 'selected'; } await new Promise((resolve) => setTimeout(resolve, 100)); } return 'timed-out'; }); expect(compositionSelected.toString()).toBe('selected'); const result = await tab.evaluateHandle(() => { return document.querySelectorAll('.css-reset').length; }); expect(result.toString()).toBeGreaterThan(1); const sourceLocation = await tab.evaluateHandle(async () => { let copied = ''; const originalWriteText = navigator.clipboard.writeText; navigator.clipboard.writeText = async (text) => { copied = text; }; try { let openedInspector = false; for (let attempt = 0; attempt < 100; attempt++) { const inspector = document.querySelector( '[aria-label="Inspector source location"]', ); const copy = inspector?.querySelector( '[aria-label="Copy context for agents"]', ); copy?.click(); if (copied.includes('Root.tsx:')) { return copied; } if (!openedInspector && !inspector) { const toggle = document.querySelector( '[data-sidebar-toggle="right"]', ); if (toggle) { openedInspector = true; toggle.click(); } } await new Promise((resolve) => setTimeout(resolve, 100)); } return null; } finally { navigator.clipboard.writeText = originalWriteText; } }); expect(sourceLocation.toString()).toMatch(/WidthHeight in .*Root\.tsx:\d+/); const sequenceSourceLocation = await tab.evaluateHandle(async () => { let copied = ''; const originalWriteText = navigator.clipboard.writeText; navigator.clipboard.writeText = async (text) => { copied = text; }; try { const label = document.querySelector( '[aria-label=""]', ); label?.click(); for (let attempt = 0; attempt < 100; attempt++) { const copy = document.querySelector( '[aria-label="Inspector source location"] [aria-label="Copy context for agents"]', ); copy?.click(); if (copied.includes('WidthHeightSequences.tsx:')) { return copied; } await new Promise((resolve) => setTimeout(resolve, 100)); } return null; } finally { navigator.clipboard.writeText = originalWriteText; } }); expect(sequenceSourceLocation.toString()).toMatch( / in .*WidthHeightSequences\.tsx:\d+/, ); const fontLoaded = await tab.evaluateHandle(() => { let loaded = false; document.fonts.forEach((font) => { if (font.family === 'Bangers' && font.status === 'loaded') { loaded = true; } }); return loaded; }); expect(String(fontLoaded.toString())).toBe('true'); const orphanedFontTimeouts = await tab.evaluateHandle(() => { return Object.values(window.remotion_delayRenderTimeouts).filter( (timeout) => timeout.label?.startsWith('Loading font Bangers'), ).length; }); expect(Number(orphanedFontTimeouts.toString())).toBe(0); await Promise.all([ initialState.dispose(), compositionSelected.dispose(), result.dispose(), sourceLocation.dispose(), sequenceSourceLocation.dispose(), fontLoaded.dispose(), orphanedFontTimeouts.dispose(), ]); await tab.close(); await (await browser).close({silent: false}); await server.stop(true); }, {timeout: 20_000}, );