import {expect, test} from 'bun:test'; import type {Size} from '@remotion/player'; import { calculateStudioCanvasTransformation, calculateStudioScale, getStudioCurrentScaleContext, STUDIO_FIT_PADDING, } from '../helpers/studio-fit-padding'; const makeCanvasSize = (width: number, height: number): Size => ({ width, height, left: 0, top: 0, windowSize: {width, height}, refresh: () => undefined, }); test('adds an 8px horizontal gutter when fitting by width', () => { const canvasSize = makeCanvasSize(1000, 600); const transformation = calculateStudioCanvasTransformation({ canvasSize, compositionHeight: 1080, compositionWidth: 1920, previewSize: 'auto', }); expect(transformation.scale).toBeCloseTo((1000 - 16) / 1920); expect(transformation.centerX).toBe( Math.round((1000 - 1920 * transformation.scaleX) / 2), ); expect(transformation.centerX).toBeGreaterThanOrEqual(STUDIO_FIT_PADDING); expect(transformation.centerY).toBe( Math.round((600 - 1080 * transformation.scale) / 2), ); }); test('adds an 8px vertical gutter when fitting by height', () => { const canvasSize = makeCanvasSize(1000, 500); const transformation = calculateStudioCanvasTransformation({ canvasSize, compositionHeight: 1080, compositionWidth: 1920, previewSize: 'auto', }); expect(transformation.scale).toBeCloseTo((500 - 16) / 1080); expect(transformation.centerX).toBe( Math.round((1000 - 1920 * transformation.scale) / 2), ); expect(transformation.centerY).toBe(STUDIO_FIT_PADDING); }); test('does not add padding to explicit zoom levels', () => { const canvasSize = makeCanvasSize(1000, 600); const transformation = calculateStudioCanvasTransformation({ canvasSize, compositionHeight: 1080, compositionWidth: 1920, previewSize: 0.5, }); expect(transformation.scale).toBe(0.5); expect(transformation.centerX).toBe(20); expect(transformation.centerY).toBe(30); expect( calculateStudioScale({ canvasSize, compositionHeight: 1080, compositionWidth: 1920, previewSize: 0.5, }), ).toBe(0.5); }); test('keeps the layout size separate from the auto-fit size', () => { const canvasSize = makeCanvasSize(1000, 600); const context = getStudioCurrentScaleContext(canvasSize); expect(context.canvasSize).toBe(canvasSize); expect(context.canvasSizeForAuto.width).toBe(1000 - 16); expect(context.canvasSizeForAuto.height).toBe(600 - 16); }); test('snaps the fitted composition edges to whole pixels', () => { const canvasSize = makeCanvasSize(547, 315); const transformation = calculateStudioCanvasTransformation({ canvasSize, compositionHeight: 1080, compositionWidth: 1920, previewSize: 'auto', }); const height = 1080 * transformation.scale; expect(transformation.centerY).toBe(Math.round(transformation.centerY)); expect(height).toBeCloseTo(Math.round(height), 10); expect(transformation.centerX).toBe(Math.round(transformation.centerX)); expect(height).toBeLessThanOrEqual(315 - 16); }); test('snaps the fitted composition width to whole pixels', () => { const canvasSize = makeCanvasSize(1100, 700); const transformation = calculateStudioCanvasTransformation({ canvasSize, compositionHeight: 1080, compositionWidth: 1920, previewSize: 'auto', }); const width = 1920 * transformation.scaleX; expect(width).toBeCloseTo(Math.round(width), 10); expect(Math.abs(width - 1920 * transformation.scale)).toBeLessThan(1); });