import { readFileSync } from 'node:fs'; import { createHash } from 'node:crypto'; import { devices } from '@playwright/test'; import { browserStabilityArgs } from './playwright-runtime.mjs'; // The review JSON is also read by Python; keep dimensions and device choices there. const config = JSON.parse( readFileSync(new URL('./ui-review/config.json', import.meta.url), 'utf8'), ); const aliases = { 'desktop-boundary': 'laptop', 'laptop-hd': 'hd', 'laptop-wxga': 'wxga', }; export const browserChannel = config.browser.channel; export const defaultProjectViewports = ['desktop', 'mobile', 'mobile-393']; export const projectViewports = [ ...defaultProjectViewports, 'tablet', 'desktop-boundary', 'laptop-hd', 'laptop-wxga', 'qhd', 'uhd', ]; export function deviceOptions(name) { const key = aliases[name] || name; const size = config.viewports[key]; if (!size) throw new Error(`Unknown viewport: ${name}`); const descriptor = devices[ config.browser[ key.startsWith('mobile') ? 'mobileDevice' : 'desktopDevice' ] ]; if (!descriptor) throw new Error(`Unknown device profile for ${name}`); const { defaultBrowserType, ...context } = descriptor; return { ...context, viewport: { width: size[0], height: size[1] } }; } export function contextOptions(name, reducedMotion = 'reduce') { return { ...deviceOptions(name), locale: config.browser.locale, timezoneId: config.browser.timezone, colorScheme: 'light', reducedMotion, }; } export function environmentFingerprint(name, reducedMotion = 'reduce') { return createHash('sha256') .update( JSON.stringify({ version: 2, channel: browserChannel, options: contextOptions(name, reducedMotion), launchArgs: browserStabilityArgs, screenshotScale: 'css', }), ) .digest('hex'); }