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

PreviewBrowserHost.test.ts

133 lines5.0 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.
20​
21const LOADER_ERROR =
22 "chrome-headless-shell: error while loading shared libraries: libglib-2.0.so.0: cannot open shared object file: No such file or directory";
23​
24const hostReporting = (output: { readonly version: string; readonly ldd: string }) => {
25 const commands: Array<string> = [];
26 const spawner = ChildProcessSpawner.make((command) =>
27 Effect.sync(() => {
28 const { command: name, args } = command as unknown as {
29 readonly command: string;
30 readonly args: ReadonlyArray<string>;
31 };
32 commands.push([name, ...args].join(" "));
33 return ChildProcessSpawner.makeHandle({
34 pid: ChildProcessSpawner.ProcessId(1),
35 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
36 exitStatus: Effect.succeed({ _tag: "Exited", code: ChildProcessSpawner.ExitCode(0) }),
37 isRunning: Effect.succeed(false),
38 kill: () => Effect.void,
39 unref: Effect.succeed(Effect.void),
40 stdin: Sink.drain,
41 stdout: Stream.empty,
42 stderr: Stream.empty,
43 all: Stream.make(new TextEncoder().encode(name === "ldd" ? output.ldd : output.version)),
44 getInputFd: () => Sink.drain,
45 getOutputFd: () => Stream.empty,
46 });
47 }),
48 );
49 return { spawner, commands };
50};
51​
52const diagnose = (input: {
53 platform: NodeJS.Platform;
54 output: string;
55 ldd: string;
56 version?: string;
57 uid?: number;
58}) => {
59 const { spawner, commands } = hostReporting({
60 ldd: input.ldd,
61 version: input.version ?? LOADER_ERROR,
62 });
63 return PreviewBrowserHost.diagnoseLaunchFailure({
64 executable: "/home/me/.t3/tools/chrome-headless-shell/linux64/154/chrome-headless-shell",
65 output: input.output,
66 setupCommand: "sudo t3 browser setup",
67 }).pipe(
68 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
69 Effect.provideService(HostProcess.Platform, input.platform),
70 Effect.provideService(HostProcess.UserId, input.uid ?? 1000),
71 Effect.map((error) => ({ error, commands })),
72 );
73};
74​
75describe("diagnoseLaunchFailure", () => {
76 it.effect("explains how to restart a root server without Chrome's sandbox", () =>
77 Effect.gen(function* () {
78 const { error, commands } = yield* diagnose({
79 platform: "linux",
80 uid: 0,
81 output: PreviewBrowserHost.NO_SANDBOX_SIGNATURE,
82 ldd: "",
83 });
84 expect(error).toBeInstanceOf(PreviewBrowserHost.PreviewBrowserRootError);
85 expect(error?.message).toContain("T3 server is running as root");
86 expect(error?.message).toContain("T3 server's environment and restart the server");
87 expect(error?.message).toContain("T3CODE_SERVER_BROWSER_SANDBOX=0 t3 serve");
88 expect(error?.message).toContain("Environment=T3CODE_SERVER_BROWSER_SANDBOX=0");
89 expect(error?.message).toContain("this disables Chrome's sandbox");
90 expect(commands).toEqual([]);
91 }),
92 );
93​
94 it.effect("keeps host setup guidance for a nonroot sandbox failure", () =>
95 Effect.gen(function* () {
96 const { error } = yield* diagnose({
97 platform: "linux",
98 output: PreviewBrowserHost.NO_SANDBOX_SIGNATURE,
99 ldd: "",
100 });
101 expect(error).toBeInstanceOf(PreviewBrowserHost.PreviewBrowserSandboxError);
102 expect(error?.message).toContain("Run `sudo t3 browser setup` on the host");
103 }),
104 );
105​
106 it.effect("names every library the loader cannot find", () =>
107 Effect.gen(function* () {
108 const { error } = yield* diagnose({
109 platform: "linux",
110 output: "error while loading shared libraries: libglib-2.0.so.0",
111 ldd: LDD_IN_BARE_DEBIAN,
112 });
113 expect(error).toMatchObject({
114 _tag: "PreviewBrowserLibrariesError",
115 libraries: ["libglib-2.0.so.0", "libnss3.so", "libX11.so.6"],
116 });
117 expect(error?.message).toContain("Run `sudo t3 browser setup` on the host");
118 }),
119 );
120​
121 it.effect("trusts the browser's own start over ldd", () =>
122 Effect.gen(function* () {
123 // NixOS: ldd lists libraries that nix-ld still provides to the real loader.
124 const { error } = yield* diagnose({
125 platform: "linux",
126 output: "crashed",
127 ldd: LDD_IN_BARE_DEBIAN,
128 version: "Google Chrome for Testing 154.0.8037.92\n",
129 });
130 expect(error).toBeUndefined();
131 }),
132 );
133});