|
| 1 | +/* |
| 2 | + * chrome-for-testing.test.ts |
| 3 | + * |
| 4 | + * Copyright (C) 2026 Posit Software, PBC |
| 5 | + */ |
| 6 | + |
| 7 | +import { unitTest } from "../../test.ts"; |
| 8 | +import { assert, assertEquals } from "testing/asserts"; |
| 9 | +import { arch, os } from "../../../src/deno_ral/platform.ts"; |
| 10 | +import { join } from "../../../src/deno_ral/path.ts"; |
| 11 | +import { safeRemoveSync } from "../../../src/deno_ral/fs.ts"; |
| 12 | +import { isWindows } from "../../../src/deno_ral/platform.ts"; |
| 13 | +import { runningInCI } from "../../../src/core/ci-info.ts"; |
| 14 | +import { InstallContext } from "../../../src/tools/types.ts"; |
| 15 | +import { |
| 16 | + detectCftPlatform, |
| 17 | + downloadAndExtractCft, |
| 18 | + fetchLatestCftRelease, |
| 19 | + findCftExecutable, |
| 20 | +} from "../../../src/tools/impl/chrome-for-testing.ts"; |
| 21 | + |
| 22 | +// Step 1: detectCftPlatform() |
| 23 | +unitTest("detectCftPlatform - returns valid CftPlatform for current system", async () => { |
| 24 | + const result = detectCftPlatform(); |
| 25 | + const validPlatforms = ["linux64", "mac-arm64", "mac-x64", "win32", "win64"]; |
| 26 | + assert( |
| 27 | + validPlatforms.includes(result.platform), |
| 28 | + `Expected one of ${validPlatforms.join(", ")}, got: ${result.platform}`, |
| 29 | + ); |
| 30 | + assert(result.os.length > 0, "os should be non-empty"); |
| 31 | + assert(result.arch.length > 0, "arch should be non-empty"); |
| 32 | +}); |
| 33 | + |
| 34 | +unitTest("detectCftPlatform - returns win64 on Windows x86_64", async () => { |
| 35 | + if (os !== "windows" || arch !== "x86_64") { |
| 36 | + return; // Skip on non-Windows |
| 37 | + } |
| 38 | + const result = detectCftPlatform(); |
| 39 | + assertEquals(result.platform, "win64"); |
| 40 | + assertEquals(result.os, "windows"); |
| 41 | + assertEquals(result.arch, "x86_64"); |
| 42 | +}); |
| 43 | + |
| 44 | +// Step 2: fetchLatestCftRelease() |
| 45 | +// These tests make real HTTP calls to the CfT API — skip on CI. |
| 46 | +unitTest("fetchLatestCftRelease - returns valid version string", async () => { |
| 47 | + const release = await fetchLatestCftRelease(); |
| 48 | + assert(release.version, "version should be non-empty"); |
| 49 | + assert( |
| 50 | + /^\d+\.\d+\.\d+\.\d+$/.test(release.version), |
| 51 | + `version should match X.Y.Z.W format, got: ${release.version}`, |
| 52 | + ); |
| 53 | +}, { ignore: runningInCI() }); |
| 54 | + |
| 55 | +unitTest("fetchLatestCftRelease - has chrome-headless-shell downloads", async () => { |
| 56 | + const release = await fetchLatestCftRelease(); |
| 57 | + const downloads = release.downloads["chrome-headless-shell"]; |
| 58 | + assert(downloads, "chrome-headless-shell downloads should exist"); |
| 59 | + assert(downloads!.length > 0, "should have at least one download"); |
| 60 | +}, { ignore: runningInCI() }); |
| 61 | + |
| 62 | +unitTest("fetchLatestCftRelease - download URLs are valid", async () => { |
| 63 | + const release = await fetchLatestCftRelease(); |
| 64 | + const downloads = release.downloads["chrome-headless-shell"]!; |
| 65 | + for (const dl of downloads) { |
| 66 | + assert( |
| 67 | + dl.url.startsWith("https://storage.googleapis.com/"), |
| 68 | + `URL should start with googleapis.com, got: ${dl.url}`, |
| 69 | + ); |
| 70 | + assert( |
| 71 | + dl.url.includes(release.version), |
| 72 | + `URL should contain version ${release.version}, got: ${dl.url}`, |
| 73 | + ); |
| 74 | + } |
| 75 | +}, { ignore: runningInCI() }); |
| 76 | + |
| 77 | +// Step 3: findCftExecutable() |
| 78 | +unitTest("findCftExecutable - finds binary in CfT directory structure", async () => { |
| 79 | + const tempDir = Deno.makeTempDirSync(); |
| 80 | + try { |
| 81 | + const subdir = join(tempDir, "chrome-headless-shell-linux64"); |
| 82 | + Deno.mkdirSync(subdir); |
| 83 | + const binaryName = isWindows |
| 84 | + ? "chrome-headless-shell.exe" |
| 85 | + : "chrome-headless-shell"; |
| 86 | + const binaryPath = join(subdir, binaryName); |
| 87 | + Deno.writeTextFileSync(binaryPath, "fake binary"); |
| 88 | + |
| 89 | + const found = findCftExecutable(tempDir, "chrome-headless-shell"); |
| 90 | + assert(found !== undefined, "should find the binary"); |
| 91 | + assert( |
| 92 | + found!.endsWith(binaryName), |
| 93 | + `found path should end with ${binaryName}, got: ${found}`, |
| 94 | + ); |
| 95 | + } finally { |
| 96 | + safeRemoveSync(tempDir, { recursive: true }); |
| 97 | + } |
| 98 | +}); |
| 99 | + |
| 100 | +unitTest("findCftExecutable - returns undefined for empty directory", async () => { |
| 101 | + const tempDir = Deno.makeTempDirSync(); |
| 102 | + try { |
| 103 | + const found = findCftExecutable(tempDir, "chrome-headless-shell"); |
| 104 | + assertEquals(found, undefined); |
| 105 | + } finally { |
| 106 | + safeRemoveSync(tempDir, { recursive: true }); |
| 107 | + } |
| 108 | +}); |
| 109 | + |
| 110 | +unitTest("findCftExecutable - finds binary in nested structure", async () => { |
| 111 | + const tempDir = Deno.makeTempDirSync(); |
| 112 | + try { |
| 113 | + const nested = join(tempDir, "chrome-headless-shell-win64", "subfolder"); |
| 114 | + Deno.mkdirSync(nested, { recursive: true }); |
| 115 | + const binaryName = isWindows |
| 116 | + ? "chrome-headless-shell.exe" |
| 117 | + : "chrome-headless-shell"; |
| 118 | + const binaryPath = join(nested, binaryName); |
| 119 | + Deno.writeTextFileSync(binaryPath, "fake binary"); |
| 120 | + |
| 121 | + const found = findCftExecutable(tempDir, "chrome-headless-shell"); |
| 122 | + assert(found !== undefined, "should find the binary in nested dir"); |
| 123 | + } finally { |
| 124 | + safeRemoveSync(tempDir, { recursive: true }); |
| 125 | + } |
| 126 | +}); |
| 127 | + |
| 128 | +// Step 4: downloadAndExtractCft() — integration test, downloads ~50MB |
| 129 | +unitTest( |
| 130 | + "downloadAndExtractCft - downloads and extracts chrome-headless-shell", |
| 131 | + async () => { |
| 132 | + const release = await fetchLatestCftRelease(); |
| 133 | + const { platform } = detectCftPlatform(); |
| 134 | + const downloads = release.downloads["chrome-headless-shell"]!; |
| 135 | + const dl = downloads.find((d) => d.platform === platform); |
| 136 | + assert(dl, `No download found for platform ${platform}`); |
| 137 | + |
| 138 | + const targetDir = Deno.makeTempDirSync(); |
| 139 | + try { |
| 140 | + const mockContext: InstallContext = { |
| 141 | + workingDir: targetDir, |
| 142 | + info: (_msg: string) => {}, |
| 143 | + withSpinner: async (_options, op) => { |
| 144 | + await op(); |
| 145 | + }, |
| 146 | + error: (_msg: string) => {}, |
| 147 | + confirm: async (_msg: string) => true, |
| 148 | + download: async (_name: string, url: string, target: string) => { |
| 149 | + const resp = await fetch(url); |
| 150 | + if (!resp.ok) throw new Error(`Download failed: ${resp.status}`); |
| 151 | + const data = new Uint8Array(await resp.arrayBuffer()); |
| 152 | + Deno.writeFileSync(target, data); |
| 153 | + }, |
| 154 | + props: {}, |
| 155 | + flags: {}, |
| 156 | + }; |
| 157 | + |
| 158 | + await downloadAndExtractCft("Chrome Headless Shell", dl!.url, targetDir, mockContext, "chrome-headless-shell"); |
| 159 | + |
| 160 | + const found = findCftExecutable(targetDir, "chrome-headless-shell"); |
| 161 | + assert( |
| 162 | + found !== undefined, |
| 163 | + "should find chrome-headless-shell after extraction", |
| 164 | + ); |
| 165 | + } finally { |
| 166 | + safeRemoveSync(targetDir, { recursive: true }); |
| 167 | + } |
| 168 | + }, |
| 169 | + { |
| 170 | + ignore: runningInCI(), |
| 171 | + }, |
| 172 | +); |
0 commit comments