|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { HostProcessPlatform } from "@t3tools/shared/hostProcess"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as Sink from "effect/Sink"; |
| 5 | +import * as Stream from "effect/Stream"; |
| 6 | +import { ChildProcessSpawner } from "effect/process"; |
| 7 | + |
| 8 | +import * as PreviewBrowserHost from "./PreviewBrowserHost.ts"; |
| 9 | + |
| 10 | +// What `ldd` printed for the pinned headless shell in a bare debian:trixie-slim container. |
| 11 | +const 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 | +const 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 | +const 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 | + |
| 50 | +const 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 | + }).pipe( |
| 56 | + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 57 | + Effect.provideService(HostProcessPlatform, input.platform), |
| 58 | + Effect.map((error) => ({ error, commands })), |
| 59 | + ); |
| 60 | +}; |
| 61 | + |
| 62 | +describe("diagnoseLaunchFailure", () => { |
| 63 | + it.effect("turns Chrome's sandbox abort into the AppArmor steps", () => |
| 64 | + Effect.gen(function* () { |
| 65 | + const { error, commands } = yield* diagnose({ |
| 66 | + platform: "linux", |
| 67 | + output: SANDBOX_ABORT, |
| 68 | + ldd: "", |
| 69 | + }); |
| 70 | + expect(error?._tag).toBe("PreviewBrowserSandboxError"); |
| 71 | + expect(error?.message).toContain("AppArmor"); |
| 72 | + expect(error?.message).toContain("T3CODE_SERVER_BROWSER_SANDBOX=0"); |
| 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("apt-get install"); |
| 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 | +}); |
0 commit comments