Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PreviewBrowserHost.test.ts

149 lines5.5 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as HostProcess from "@t3tools/shared/HostProcess";
3import * as Effect from "effect/Effect";
4import * as Sink from "effect/Sink";
5import * as Stream from "effect/Stream";
6import { ChildProcessSpawner } from "effect/process";
7​
8import * as PreviewBrowserHost from "./PreviewBrowserHost.ts";
9​
10// What `ldd` printed for the pinned headless shell in a bare debian:trixie-slim container.
11const LDD_IN_BARE_DEBIAN = `\tlinux-vdso.so.1 (0x00007ffd2d1f8000)
12\tlibdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f0c1b6a1000)
13\tlibglib-2.0.so.0 => not found
14\tlibnss3.so => not found
15\tlibX11.so.6 => not found
16\tlibc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f0c1b4c0000)
17`;
18​
19// Chrome's own abort on Ubuntu 26.04 with unprivileged user namespaces restricted.
20const SANDBOX_ABORT =
21 "[1005/163106.285700:FATAL:content/browser/zygote_host/zygote_host_impl_linux.cc:129] No usable sandbox! If you are running on Ubuntu 23.10+ or another Linux distro that has disabled unprivileged user namespaces with AppArmor, see https://chromium.googlesource.com/";
22​
23// What the real loader prints when the browser's `--version` cannot start it.
24const LOADER_ERROR =
25 "chrome-headless-shell: error while loading shared libraries: libglib-2.0.so.0: cannot open shared object file: No such file or directory";
26​
27const hostReporting = (output: { readonly version: string; readonly ldd: string }) => {
28 const commands: Array<string> = [];
29 const spawner = ChildProcessSpawner.make((command) =>
30 Effect.sync(() => {
31 const { command: name, args } = command as unknown as {
32 readonly command: string;
33 readonly args: ReadonlyArray<string>;
34 };
35 commands.push([name, ...args].join(" "));
36 return ChildProcessSpawner.makeHandle({
37 pid: ChildProcessSpawner.ProcessId(1),
38 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
39 isRunning: Effect.succeed(false),
40 kill: () => Effect.void,
41 unref: Effect.succeed(Effect.void),
42 stdin: Sink.drain,
43 stdout: Stream.empty,
44 stderr: Stream.empty,
45 all: Stream.make(new TextEncoder().encode(name === "ldd" ? output.ldd : output.version)),
46 getInputFd: () => Sink.drain,
47 getOutputFd: () => Stream.empty,
48 });
49 }),
50 );
51 return { spawner, commands };
52};
53​
54const diagnose = (input: {
55 platform: NodeJS.Platform;
56 output: string;
57 ldd: string;
58 version?: string;
59 uid?: number;
60}) => {
61 const { spawner, commands } = hostReporting({
62 ldd: input.ldd,
63 version: input.version ?? LOADER_ERROR,
64 });
65 return PreviewBrowserHost.diagnoseLaunchFailure({
66 executable: "/home/me/.t3/tools/chrome-headless-shell/linux64/154/chrome-headless-shell",
67 output: input.output,
68 setupCommand: "sudo t3 browser setup",
69 }).pipe(
70 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
71 Effect.provideService(HostProcess.Platform, input.platform),
72 Effect.provideService(HostProcess.UserId, input.uid ?? 1000),
73 Effect.map((error) => ({ error, commands })),
74 );
75};
76​
77describe("diagnoseLaunchFailure", () => {
78 it.effect("turns Chrome's sandbox abort into the AppArmor steps", () =>
79 Effect.gen(function* () {
80 const { error, commands } = yield* diagnose({
81 platform: "linux",
82 output: SANDBOX_ABORT,
83 ldd: "",
84 });
85 expect(error?._tag).toBe("PreviewBrowserSandboxError");
86 expect(error?.message).toContain("Run `sudo t3 browser setup` on the host");
87 expect(commands).toEqual([]);
88 }),
89 );
90​
91 it.effect("tells a root server to drop root, not to run setup", () =>
92 Effect.gen(function* () {
93 const { error, commands } = yield* diagnose({
94 platform: "linux",
95 output: PreviewBrowserHost.NO_SANDBOX_SIGNATURE,
96 ldd: "",
97 uid: 0,
98 });
99 expect(error?._tag).toBe("PreviewBrowserRootError");
100 expect(error?.message).toContain("`User=`");
101 expect(error?.message).toContain("`T3CODE_SERVER_BROWSER_SANDBOX=0`");
102 expect(error?.message).not.toMatch(/AppArmor|browser setup/);
103 expect(commands).toEqual([]);
104 }),
105 );
106​
107 it.effect("names every library the loader cannot find", () =>
108 Effect.gen(function* () {
109 const { error } = yield* diagnose({
110 platform: "linux",
111 output: "error while loading shared libraries: libglib-2.0.so.0",
112 ldd: LDD_IN_BARE_DEBIAN,
113 });
114 expect(error).toMatchObject({
115 _tag: "PreviewBrowserLibrariesError",
116 libraries: ["libglib-2.0.so.0", "libnss3.so", "libX11.so.6"],
117 });
118 expect(error?.message).toContain("Run `sudo t3 browser setup` on the host");
119 }),
120 );
121​
122 it.effect("trusts the browser's own start over ldd", () =>
123 Effect.gen(function* () {
124 // NixOS: ldd lists libraries that nix-ld still provides to the real loader.
125 const { error } = yield* diagnose({
126 platform: "linux",
127 output: "crashed",
128 ldd: LDD_IN_BARE_DEBIAN,
129 version: "Google Chrome for Testing 154.0.8037.92\n",
130 });
131 expect(error).toBeUndefined();
132 }),
133 );
134​
135 it.effect("leaves other failures alone", () =>
136 Effect.gen(function* () {
137 const linux = yield* diagnose({
138 platform: "linux",
139 output: "crashed",
140 ldd: "\tlibc.so.6 => /lib/libc.so.6\n",
141 version: "Google Chrome for Testing 154.0.8037.92\n",
142 });
143 expect(linux.error).toBeUndefined();
144 const mac = yield* diagnose({ platform: "darwin", output: "crashed", ldd: "" });
145 expect(mac.error).toBeUndefined();
146 expect(mac.commands).toEqual([]);
147 }),
148 );
149});