diff --git a/packages/studio/tests/chromeExecutable.test.ts b/packages/studio/tests/chromeExecutable.test.ts new file mode 100644 index 0000000000..2170accbda --- /dev/null +++ b/packages/studio/tests/chromeExecutable.test.ts @@ -0,0 +1,44 @@ +import { join } from "node:path"; +import { describe, expect, it } from "vitest"; +import { resolveChromeExecutable } from "./e2e/chrome-executable.mjs"; + +const SYSTEM = "/usr/bin/google-chrome"; +// Joined as the finder joins it, so the path matches on a Windows runner too. +const DOWNLOADED = join("/cache", "chrome", "linux-155.0.8059.39", "chrome-linux64", "chrome"); +const OLD = () => "Google Chrome 113.0.5672.63"; + +describe("the Chrome Studio's browser test scripts launch", () => { + // fails without the fix: the scripts' own list tried a system Chrome first and never read its version. + it("is the downloaded Chrome, even beside a system Chrome", () => { + const found = resolveChromeExecutable( + { PUPPETEER_CACHE_DIR: "/cache" }, + (path) => path === SYSTEM || path === DOWNLOADED, + "linux", + OLD, + () => ["linux-155.0.8059.39"], + ); + expect(found).toBe(DOWNLOADED); + }); + + it("is never a system Chrome too old for Studio", () => { + const found = resolveChromeExecutable( + { PUPPETEER_CACHE_DIR: "/cache" }, + (path) => path === SYSTEM, + "linux", + OLD, + () => [], + ); + expect(found).toBeUndefined(); + }); + + it("is the one an environment variable names, before either", () => { + const found = resolveChromeExecutable( + { PUPPETEER_CACHE_DIR: "/cache", CHROME_PATH: "/pinned/chrome" }, + () => true, + "linux", + OLD, + () => ["linux-155.0.8059.39"], + ); + expect(found).toBe("/pinned/chrome"); + }); +}); diff --git a/packages/studio/tests/e2e/chrome-executable.mjs b/packages/studio/tests/e2e/chrome-executable.mjs index 8fc89f490e..c1cd7dd998 100644 --- a/packages/studio/tests/e2e/chrome-executable.mjs +++ b/packages/studio/tests/e2e/chrome-executable.mjs @@ -1,28 +1,8 @@ -import { existsSync, readdirSync } from "node:fs"; -import { homedir } from "node:os"; -import { join } from "node:path"; import puppeteer from "puppeteer-core"; +import { findSystemChrome } from "../../vite.chrome.ts"; -/** Resolve the same Chrome binary for every local Studio browser acceptance test. */ -export function resolveChromeExecutable() { - const chromeRoot = join(homedir(), ".cache", "puppeteer", "chrome"); - const builds = existsSync(chromeRoot) ? readdirSync(chromeRoot).sort().reverse() : []; - const installed = builds.flatMap((build) => - [ - "chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing", - "chrome-mac-x64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing", - "chrome-linux64/chrome", - ].map((relative) => join(chromeRoot, build, relative)), - ); - return [ - process.env.PUPPETEER_EXECUTABLE_PATH, - process.env.CHROME_PATH, - "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", - "/usr/bin/google-chrome", - "/usr/bin/chromium", - ...installed, - ].find((candidate) => candidate && existsSync(candidate)); -} +/** The Chrome every Studio browser acceptance test launches: the one Studio's dev server picks. */ +export const resolveChromeExecutable = findSystemChrome; /** Headless Chrome for a Studio browser gate; exits 2 when no Chrome is installed. */ export async function launchStudioChrome() { diff --git a/packages/studio/vite.browser.ts b/packages/studio/vite.browser.ts index 01bb18453d..50fa3e6caa 100644 --- a/packages/studio/vite.browser.ts +++ b/packages/studio/vite.browser.ts @@ -1,9 +1,5 @@ // Shared Puppeteer browser management and thumbnail generation for Studio dev server. -import { existsSync, readdirSync } from "node:fs"; -import { execFileSync } from "node:child_process"; -import { homedir } from "node:os"; -import { join, win32 as pathWin32 } from "node:path"; import { getElementScreenshotClip, grantCaptureLoads, @@ -12,131 +8,15 @@ import { type ScreenshotClip, } from "@hyperframes/studio-server"; import { createStudioDevRenderBodyScripts } from "./vite.studioMotion"; +import { browserVersion, findSystemChrome } from "./vite.chrome"; import { seekThumbnailPreview } from "./vite.thumbnail"; +export { browserVersion, findSystemChrome }; + let browser: import("puppeteer-core").Browser | null = null; let browserLaunch: Promise | null = null; let describeBrowser = () => "unknown browser"; -function systemChromePaths(env: NodeJS.ProcessEnv, platform: NodeJS.Platform): string[] { - if (platform === "win32") { - const programFiles = env["PROGRAMFILES"] ?? "C:\\Program Files"; - const programFilesX86 = env["PROGRAMFILES(X86)"] ?? "C:\\Program Files (x86)"; - const localAppData = env["LOCALAPPDATA"]; - return [ - pathWin32.join(programFiles, "Google", "Chrome", "Application", "chrome.exe"), - pathWin32.join(programFilesX86, "Google", "Chrome", "Application", "chrome.exe"), - ...(localAppData - ? [pathWin32.join(localAppData, "Google", "Chrome", "Application", "chrome.exe")] - : []), - ]; - } - if (platform === "darwin") { - return [ - "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", - "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary", - "/Applications/Chromium.app/Contents/MacOS/Chromium", - "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser", - "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge", - ]; - } - return [ - "/usr/bin/google-chrome", - "/usr/bin/google-chrome-stable", - "/usr/bin/chromium", - "/usr/bin/chromium-browser", - ]; -} - -function findPuppeteerCacheChrome( - env: NodeJS.ProcessEnv, - pathExists: (path: string) => boolean, - platform: NodeJS.Platform, - readDirectories: (path: string) => string[] = readdirSync, -): string | undefined { - const chromeRoot = join( - env["PUPPETEER_CACHE_DIR"] ?? join(homedir(), ".cache", "puppeteer"), - "chrome", - ); - let versionDirs: string[]; - try { - versionDirs = readDirectories(chromeRoot); - } catch { - return undefined; - } - const buildOrder = (directory: string): number => - (directory.split("-").pop() ?? "") - .split(".") - .map((part) => Number.parseInt(part, 10) || 0) - .reduce((order, part) => order * 100000 + part, 0); - const relativeCandidates = - platform === "win32" - ? ["chrome-win64/chrome.exe"] - : [ - "chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing", - "chrome-mac-x64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing", - "chrome-linux64/chrome", - ]; - for (const directory of versionDirs.sort((left, right) => buildOrder(right) - buildOrder(left))) { - for (const relativePath of relativeCandidates) { - const executable = join(chromeRoot, directory, relativePath); - if (pathExists(executable)) return executable; - } - } - return undefined; -} - -export function browserVersion( - executable: string, - run: typeof execFileSync = execFileSync, - platform: NodeJS.Platform = process.platform, -): string | undefined { - const options = { encoding: "utf8" as const, timeout: 5_000, windowsHide: true }; - const env = { ...process.env, HYPERFRAMES_CHROME_VERSION_PATH: executable }; - const script = - "(Get-Item -LiteralPath $env:HYPERFRAMES_CHROME_VERSION_PATH).VersionInfo.ProductVersion"; - try { - if (platform !== "win32") return run(executable, ["--version"], options).trim() || undefined; - const v = run("powershell.exe", ["-NoProfile", "-Command", script], { ...options, env }).trim(); - return /^\d+(\.\d+){3}$/.test(v) ? `Chrome/${v}` : undefined; - } catch { - return undefined; - } -} - -function supportedSystemChrome( - candidates: string[], - versionOf: (path: string) => string | undefined, -): string | undefined { - return candidates.find((candidate) => { - const match = versionOf(candidate)?.match(/(?:Chrome|Chromium)[ /](\d+)/i); - return match ? Number(match[1]) >= 134 : false; - }); -} - -/** Resolve explicit overrides, Puppeteer's managed browser, then compatible system installs. */ -export function findSystemChrome( - env: NodeJS.ProcessEnv = process.env, - pathExists: (path: string) => boolean = existsSync, - platform: NodeJS.Platform = process.platform, - versionOf: (path: string) => string | undefined = (path) => - browserVersion(path, undefined, platform), - readDirectories: (path: string) => string[] = readdirSync, -): string | undefined { - const override = [ - env["HYPERFRAMES_BROWSER_PATH"], - env["PRODUCER_HEADLESS_SHELL_PATH"], - env["PUPPETEER_EXECUTABLE_PATH"], - env["CHROME_PATH"], - env["CHROME_BIN"], - ].find((candidate): candidate is string => Boolean(candidate) && pathExists(candidate)); - return ( - override ?? - findPuppeteerCacheChrome(env, pathExists, platform, readDirectories) ?? - supportedSystemChrome(systemChromePaths(env, platform).filter(pathExists), versionOf) - ); -} - /** Reading the version blocks the server (a PowerShell start on Windows), so only a failure's wording pays for it. */ function versionWhenAsked(executable: string): () => string { let description: string | undefined; diff --git a/packages/studio/vite.chrome.ts b/packages/studio/vite.chrome.ts new file mode 100644 index 0000000000..282799e636 --- /dev/null +++ b/packages/studio/vite.chrome.ts @@ -0,0 +1,126 @@ +// Which Chrome Studio launches, for its dev server and its browser tests. No project imports: plain Node loads this +// file for the scripts in tests/e2e. + +import { execFileSync } from "node:child_process"; +import { existsSync, readdirSync } from "node:fs"; +import { homedir } from "node:os"; +import { join, win32 as pathWin32 } from "node:path"; + +function systemChromePaths(env: NodeJS.ProcessEnv, platform: NodeJS.Platform): string[] { + if (platform === "win32") { + const programFiles = env["PROGRAMFILES"] ?? "C:\\Program Files"; + const programFilesX86 = env["PROGRAMFILES(X86)"] ?? "C:\\Program Files (x86)"; + const localAppData = env["LOCALAPPDATA"]; + return [ + pathWin32.join(programFiles, "Google", "Chrome", "Application", "chrome.exe"), + pathWin32.join(programFilesX86, "Google", "Chrome", "Application", "chrome.exe"), + ...(localAppData + ? [pathWin32.join(localAppData, "Google", "Chrome", "Application", "chrome.exe")] + : []), + ]; + } + if (platform === "darwin") { + return [ + "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", + "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary", + "/Applications/Chromium.app/Contents/MacOS/Chromium", + "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser", + "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge", + ]; + } + return [ + "/usr/bin/google-chrome", + "/usr/bin/google-chrome-stable", + "/usr/bin/chromium", + "/usr/bin/chromium-browser", + ]; +} + +function findPuppeteerCacheChrome( + env: NodeJS.ProcessEnv, + pathExists: (path: string) => boolean, + platform: NodeJS.Platform, + readDirectories: (path: string) => string[] = readdirSync, +): string | undefined { + const chromeRoot = join( + env["PUPPETEER_CACHE_DIR"] ?? join(homedir(), ".cache", "puppeteer"), + "chrome", + ); + let versionDirs: string[]; + try { + versionDirs = readDirectories(chromeRoot); + } catch { + return undefined; + } + const buildOrder = (directory: string): number => + (directory.split("-").pop() ?? "") + .split(".") + .map((part) => Number.parseInt(part, 10) || 0) + .reduce((order, part) => order * 100000 + part, 0); + const relativeCandidates = + platform === "win32" + ? ["chrome-win64/chrome.exe"] + : [ + "chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing", + "chrome-mac-x64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing", + "chrome-linux64/chrome", + ]; + for (const directory of versionDirs.sort((left, right) => buildOrder(right) - buildOrder(left))) { + for (const relativePath of relativeCandidates) { + const executable = join(chromeRoot, directory, relativePath); + if (pathExists(executable)) return executable; + } + } + return undefined; +} + +export function browserVersion( + executable: string, + run: typeof execFileSync = execFileSync, + platform: NodeJS.Platform = process.platform, +): string | undefined { + const options = { encoding: "utf8" as const, timeout: 5_000, windowsHide: true }; + const env = { ...process.env, HYPERFRAMES_CHROME_VERSION_PATH: executable }; + const script = + "(Get-Item -LiteralPath $env:HYPERFRAMES_CHROME_VERSION_PATH).VersionInfo.ProductVersion"; + try { + if (platform !== "win32") return run(executable, ["--version"], options).trim() || undefined; + const v = run("powershell.exe", ["-NoProfile", "-Command", script], { ...options, env }).trim(); + return /^\d+(\.\d+){3}$/.test(v) ? `Chrome/${v}` : undefined; + } catch { + return undefined; + } +} + +function supportedSystemChrome( + candidates: string[], + versionOf: (path: string) => string | undefined, +): string | undefined { + return candidates.find((candidate) => { + const match = versionOf(candidate)?.match(/(?:Chrome|Chromium)[ /](\d+)/i); + return match ? Number(match[1]) >= 134 : false; + }); +} + +/** Resolve explicit overrides, Puppeteer's managed browser, then compatible system installs. */ +export function findSystemChrome( + env: NodeJS.ProcessEnv = process.env, + pathExists: (path: string) => boolean = existsSync, + platform: NodeJS.Platform = process.platform, + versionOf: (path: string) => string | undefined = (path) => + browserVersion(path, undefined, platform), + readDirectories: (path: string) => string[] = readdirSync, +): string | undefined { + const override = [ + env["HYPERFRAMES_BROWSER_PATH"], + env["PRODUCER_HEADLESS_SHELL_PATH"], + env["PUPPETEER_EXECUTABLE_PATH"], + env["CHROME_PATH"], + env["CHROME_BIN"], + ].find((candidate): candidate is string => Boolean(candidate) && pathExists(candidate)); + return ( + override ?? + findPuppeteerCacheChrome(env, pathExists, platform, readDirectories) ?? + supportedSystemChrome(systemChromePaths(env, platform).filter(pathExists), versionOf) + ); +}