-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathbrowserManager.test.ts
More file actions
189 lines (165 loc) · 7.3 KB
/
Copy pathbrowserManager.test.ts
File metadata and controls
189 lines (165 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
_resetAutoBrowserGpuModeCacheForTests,
buildChromeArgs,
forceReleaseBrowser,
resolveBrowserGpuMode,
} from "./browserManager.js";
describe("buildChromeArgs browser GPU mode", () => {
const base = { width: 1920, height: 1080 };
it("uses SwiftShader software GL by default for reproducible local renders", () => {
const args = buildChromeArgs(base);
expect(args).toContain("--enable-features=CanvasDrawElement");
expect(args).toContain("--use-gl=angle");
expect(args).toContain("--use-angle=swiftshader");
expect(args).toContain("--enable-unsafe-swiftshader");
expect(args).not.toContain("--enable-gpu-rasterization");
});
it("uses Metal-backed ANGLE for hardware browser GPU mode on macOS", () => {
const args = buildChromeArgs({ ...base, platform: "darwin" }, { browserGpuMode: "hardware" });
expect(args).toContain("--use-gl=angle");
expect(args).toContain("--use-angle=metal");
expect(args).toContain("--enable-gpu-rasterization");
expect(args).not.toContain("--use-angle=swiftshader");
});
it("uses D3D11-backed ANGLE for hardware browser GPU mode on Windows", () => {
const args = buildChromeArgs({ ...base, platform: "win32" }, { browserGpuMode: "hardware" });
expect(args).toContain("--use-gl=angle");
expect(args).toContain("--use-angle=d3d11");
expect(args).toContain("--enable-gpu-rasterization");
expect(args).not.toContain("--use-angle=swiftshader");
});
it("uses EGL for hardware browser GPU mode on Linux", () => {
const args = buildChromeArgs({ ...base, platform: "linux" }, { browserGpuMode: "hardware" });
expect(args).toContain("--use-gl=egl");
expect(args).toContain("--enable-gpu-rasterization");
expect(args).not.toContain("--use-gl=angle");
expect(args).not.toContain("--use-angle=swiftshader");
});
it("keeps --disable-gpu authoritative when requested", () => {
const args = buildChromeArgs(
{ ...base, platform: "darwin" },
{ browserGpuMode: "hardware", disableGpu: true },
);
expect(args).toContain("--disable-gpu");
expect(args).toContain("--use-angle=swiftshader");
expect(args).not.toContain("--use-angle=metal");
});
});
describe("buildChromeArgs compositor determinism flags", () => {
const base = { width: 1920, height: 1080 };
it("includes compositor determinism flags in screenshot mode", () => {
const args = buildChromeArgs({ ...base, captureMode: "screenshot" });
expect(args).toContain("--run-all-compositor-stages-before-draw");
expect(args).toContain("--disable-threaded-animation");
expect(args).toContain("--disable-threaded-scrolling");
expect(args).toContain("--enable-surface-synchronization");
expect(args).toContain("--disable-checker-imaging");
expect(args).toContain("--disable-image-animation-resync");
expect(args).toContain("--disable-new-content-rendering-timeout");
});
it("excludes beginFrame-exclusive flags in screenshot mode", () => {
const args = buildChromeArgs({ ...base, captureMode: "screenshot" });
expect(args).not.toContain("--deterministic-mode");
expect(args).not.toContain("--enable-begin-frame-control");
});
it("includes both compositor and beginFrame-exclusive flags in beginframe mode", () => {
const args = buildChromeArgs({ ...base, captureMode: "beginframe" });
expect(args).toContain("--run-all-compositor-stages-before-draw");
expect(args).toContain("--disable-threaded-animation");
expect(args).toContain("--enable-surface-synchronization");
expect(args).toContain("--deterministic-mode");
expect(args).toContain("--enable-begin-frame-control");
});
});
describe("resolveBrowserGpuMode", () => {
beforeEach(() => {
_resetAutoBrowserGpuModeCacheForTests();
});
afterEach(() => {
vi.restoreAllMocks();
_resetAutoBrowserGpuModeCacheForTests();
});
it("passes 'software' through unchanged without probing", async () => {
const mode = await resolveBrowserGpuMode("software");
expect(mode).toBe("software");
});
it("passes 'hardware' through unchanged without probing", async () => {
const mode = await resolveBrowserGpuMode("hardware");
expect(mode).toBe("hardware");
});
it("falls back to 'software' when the probe browser cannot launch", async () => {
// No chromePath, env unset, and (in the test env) no system Chrome to find
// → puppeteer.launch will throw → caller catches → software fallback.
// Force a definitely-missing chrome binary so the launch path errors fast.
const mode = await resolveBrowserGpuMode("auto", {
chromePath: "/definitely/not/a/real/chrome/binary",
browserTimeout: 2000,
});
expect(mode).toBe("software");
});
it("caches the probe result across calls", async () => {
const first = await resolveBrowserGpuMode("auto", {
chromePath: "/definitely/not/a/real/chrome/binary",
browserTimeout: 2000,
});
// Second call uses cache — no new launch. Assert the same answer comes back
// even with a different chromePath that would have a different probe outcome.
const second = await resolveBrowserGpuMode("auto", {
chromePath: "/another/definitely/missing/path",
browserTimeout: 2000,
});
expect(first).toBe("software");
expect(second).toBe("software");
// Reset and re-probe to confirm the test-only reset works.
_resetAutoBrowserGpuModeCacheForTests();
const third = await resolveBrowserGpuMode("hardware");
expect(third).toBe("hardware");
});
it("deduplicates concurrent auto-mode probes by caching the in-flight Promise", async () => {
// Parallel coordinator fires N workers via Promise.all — without Promise-
// level caching, a `--workers 4` render against a no-GPU host would launch
// 4 simultaneous probe Chromes. Verify all concurrent callers get the
// exact same Promise reference (proving the probe runs once, not N times).
const p1 = resolveBrowserGpuMode("auto", {
chromePath: "/definitely/not/a/real/chrome/binary",
browserTimeout: 2000,
});
const p2 = resolveBrowserGpuMode("auto", {
chromePath: "/definitely/not/a/real/chrome/binary",
browserTimeout: 2000,
});
const p3 = resolveBrowserGpuMode("auto", {
chromePath: "/definitely/not/a/real/chrome/binary",
browserTimeout: 2000,
});
expect(p1).toBe(p2);
expect(p2).toBe(p3);
const results = await Promise.all([p1, p2, p3]);
expect(results).toEqual(["software", "software", "software"]);
});
});
describe("forceReleaseBrowser", () => {
it("kills the browser process and disconnects", () => {
const killFn = vi.fn(() => true);
const disconnectFn = vi.fn();
const mockBrowser = {
process: () => ({ kill: killFn, killed: false }),
disconnect: disconnectFn,
} as any;
forceReleaseBrowser(mockBrowser);
expect(killFn).toHaveBeenCalledWith("SIGKILL");
expect(disconnectFn).toHaveBeenCalled();
});
it("tolerates an already-killed process", () => {
const killFn = vi.fn();
const disconnectFn = vi.fn();
const mockBrowser = {
process: () => ({ kill: killFn, killed: true }),
disconnect: disconnectFn,
} as any;
forceReleaseBrowser(mockBrowser);
expect(killFn).not.toHaveBeenCalled();
expect(disconnectFn).toHaveBeenCalled();
});
});