From 2be84ea50f71b031040ee021994dc568c1389679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 21 Nov 2026 21:49:25 +0000 Subject: [PATCH] fix(studio): browser test scripts launch the Chrome Studio itself picks (#8399) * fix(studio): browser test scripts launch the Chrome Studio itself picks The scripts in tests/e2e chose Chrome from their own list: a system Chrome before the downloaded one, and no version check, so an old system Chrome hung every script at browser start. They now use Studio's one finder (an environment variable first, then the downloaded Chrome, then a system Chrome new enough), moved to a file plain Node can load. * test(studio): the Chrome finder test builds its path as the finder does The finder joins the cache path with the host's separator, so on a Windows runner the stand-in never saw the path the test spelled with forward slashes. --- .../studio/tests/chromeExecutable.test.ts | 44 ++++++ .../studio/tests/e2e/chrome-executable.mjs | 26 +--- packages/studio/vite.browser.ts | 126 +----------------- packages/studio/vite.chrome.ts | 126 ++++++++++++++++++ 4 files changed, 176 insertions(+), 146 deletions(-) create mode 100644 packages/studio/tests/chromeExecutable.test.ts create mode 100644 packages/studio/vite.chrome.ts 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) + ); +}