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

PreviewBrowserHost.test.ts

105 lines4.0 KB

1import { describe, expect, it } from "@effect/vitest";
2import { HostProcessPlatform } 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​
23const lddReporting = (stdout: string) => {
24 const commands: Array<string> = [];
25 const spawner = ChildProcessSpawner.make((command) =>
26 Effect.sync(() => {
27 const { command: name, args } = command as unknown as {
28 readonly command: string;
29 readonly args: ReadonlyArray<string>;
30 };
31 commands.push([name, ...args].join(" "));
32 return ChildProcessSpawner.makeHandle({
33 pid: ChildProcessSpawner.ProcessId(1),
34 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
35 isRunning: Effect.succeed(false),
36 kill: () => Effect.void,
37 unref: Effect.succeed(Effect.void),
38 stdin: Sink.drain,
39 stdout: Stream.make(new TextEncoder().encode(stdout)),
40 stderr: Stream.empty,
41 all: Stream.empty,
42 getInputFd: () => Sink.drain,
43 getOutputFd: () => Stream.empty,
44 });
45 }),
46 );
47 return { spawner, commands };
48};
49​
50const diagnose = (input: { platform: NodeJS.Platform; output: string; ldd: string }) => {
51 const { spawner, commands } = lddReporting(input.ldd);
52 return PreviewBrowserHost.diagnoseLaunchFailure({
53 executable: "/home/me/.t3/tools/chrome-headless-shell/linux64/154/chrome-headless-shell",
54 output: input.output,
55 setupCommand: "sudo t3 browser setup",
56 }).pipe(
57 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
58 Effect.provideService(HostProcessPlatform, input.platform),
59 Effect.map((error) => ({ error, commands })),
60 );
61};
62​
63describe("diagnoseLaunchFailure", () => {
64 it.effect("turns Chrome's sandbox abort into the AppArmor steps", () =>
65 Effect.gen(function* () {
66 const { error, commands } = yield* diagnose({
67 platform: "linux",
68 output: SANDBOX_ABORT,
69 ldd: "",
70 });
71 expect(error?._tag).toBe("PreviewBrowserSandboxError");
72 expect(error?.message).toContain("Run `sudo t3 browser setup` on the host");
73 expect(commands).toEqual([]);
74 }),
75 );
76​
77 it.effect("names every library the loader cannot find", () =>
78 Effect.gen(function* () {
79 const { error } = yield* diagnose({
80 platform: "linux",
81 output: "error while loading shared libraries: libglib-2.0.so.0",
82 ldd: LDD_IN_BARE_DEBIAN,
83 });
84 expect(error).toMatchObject({
85 _tag: "PreviewBrowserLibrariesError",
86 libraries: ["libglib-2.0.so.0", "libnss3.so", "libX11.so.6"],
87 });
88 expect(error?.message).toContain("Run `sudo t3 browser setup` on the host");
89 }),
90 );
91​
92 it.effect("leaves other failures alone", () =>
93 Effect.gen(function* () {
94 const linux = yield* diagnose({
95 platform: "linux",
96 output: "crashed",
97 ldd: "\tlibc.so.6 => /lib/libc.so.6\n",
98 });
99 expect(linux.error).toBeUndefined();
100 const mac = yield* diagnose({ platform: "darwin", output: "crashed", ldd: "" });
101 expect(mac.error).toBeUndefined();
102 expect(mac.commands).toEqual([]);
103 }),
104 );
105});