Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/tests/unit/tools/chrome-for-testing.test.ts
12925 views
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 { platform } = detectCftPlatform();
82
const subdir = join(tempDir, `chrome-headless-shell-${platform}`);
83
Deno.mkdirSync(subdir);
84
const binaryName = isWindows
85
? "chrome-headless-shell.exe"
86
: "chrome-headless-shell";
87
const binaryPath = join(subdir, binaryName);
88
Deno.writeTextFileSync(binaryPath, "fake binary");
89
90
const found = findCftExecutable(tempDir, "chrome-headless-shell");
91
assert(found !== undefined, "should find the binary");
92
assert(
93
found!.endsWith(binaryName),
94
`found path should end with ${binaryName}, got: ${found}`,
95
);
96
} finally {
97
safeRemoveSync(tempDir, { recursive: true });
98
}
99
});
100
101
unitTest("findCftExecutable - returns undefined for empty directory", async () => {
102
const tempDir = Deno.makeTempDirSync();
103
try {
104
const found = findCftExecutable(tempDir, "chrome-headless-shell");
105
assertEquals(found, undefined);
106
} finally {
107
safeRemoveSync(tempDir, { recursive: true });
108
}
109
});
110
111
unitTest("findCftExecutable - finds binary in nested structure", async () => {
112
const tempDir = Deno.makeTempDirSync();
113
try {
114
const { platform } = detectCftPlatform();
115
const nested = join(tempDir, `chrome-headless-shell-${platform}`, "subfolder");
116
Deno.mkdirSync(nested, { recursive: true });
117
const binaryName = isWindows
118
? "chrome-headless-shell.exe"
119
: "chrome-headless-shell";
120
const binaryPath = join(nested, binaryName);
121
Deno.writeTextFileSync(binaryPath, "fake binary");
122
123
const found = findCftExecutable(tempDir, "chrome-headless-shell");
124
assert(found !== undefined, "should find the binary in nested dir");
125
} finally {
126
safeRemoveSync(tempDir, { recursive: true });
127
}
128
});
129
130
// Step 4: downloadAndExtractCft() — integration test, downloads ~50MB
131
unitTest(
132
"downloadAndExtractCft - downloads and extracts chrome-headless-shell",
133
async () => {
134
const release = await fetchLatestCftRelease();
135
const { platform } = detectCftPlatform();
136
const downloads = release.downloads["chrome-headless-shell"]!;
137
const dl = downloads.find((d) => d.platform === platform);
138
assert(dl, `No download found for platform ${platform}`);
139
140
const targetDir = Deno.makeTempDirSync();
141
try {
142
const mockContext: InstallContext = {
143
workingDir: targetDir,
144
info: (_msg: string) => {},
145
withSpinner: async (_options, op) => {
146
await op();
147
},
148
error: (_msg: string) => {},
149
confirm: async (_msg: string) => true,
150
download: async (_name: string, url: string, target: string) => {
151
const resp = await fetch(url);
152
if (!resp.ok) throw new Error(`Download failed: ${resp.status}`);
153
const data = new Uint8Array(await resp.arrayBuffer());
154
Deno.writeFileSync(target, data);
155
},
156
props: {},
157
flags: {},
158
};
159
160
await downloadAndExtractCft("Chrome Headless Shell", dl!.url, targetDir, mockContext, "chrome-headless-shell");
161
162
const found = findCftExecutable(targetDir, "chrome-headless-shell");
163
assert(
164
found !== undefined,
165
"should find chrome-headless-shell after extraction",
166
);
167
} finally {
168
safeRemoveSync(targetDir, { recursive: true });
169
}
170
},
171
{
172
ignore: runningInCI(),
173
},
174
);
175
176