|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { describe, expect, it } from "@effect/vitest"; |
| 3 | +import { |
| 4 | + HostProcessArguments, |
| 5 | + HostProcessEnvironment, |
| 6 | + HostProcessExecutablePath, |
| 7 | + HostProcessIsExecutable, |
| 8 | + HostProcessPlatform, |
| 9 | + HostProcessUserId, |
| 10 | +} from "@t3tools/shared/hostProcess"; |
| 11 | +import * as Effect from "effect/Effect"; |
| 12 | +import * as FileSystem from "effect/FileSystem"; |
| 13 | +import * as Layer from "effect/Layer"; |
| 14 | +import * as PlatformError from "effect/PlatformError"; |
| 15 | +import * as Sink from "effect/Sink"; |
| 16 | +import * as Stream from "effect/Stream"; |
| 17 | +import * as TestConsole from "effect/testing/TestConsole"; |
| 18 | +import { Command } from "effect/cli"; |
| 19 | +import * as ChildProcess from "effect/process/ChildProcess"; |
| 20 | +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 21 | + |
| 22 | +import { browserCommand } from "./browser.ts"; |
| 23 | + |
| 24 | +const runSetup = (input: { |
| 25 | + readonly font: string | undefined; |
| 26 | + readonly root?: boolean; |
| 27 | + readonly apt?: boolean; |
| 28 | + readonly installedBrowser?: boolean; |
| 29 | + readonly missingLibraries?: boolean; |
| 30 | + readonly platform?: NodeJS.Platform; |
| 31 | +}) => { |
| 32 | + const commands: Array<ReadonlyArray<string>> = []; |
| 33 | + const browser = "/t3/tools/chrome-headless-shell/linux64/154/chrome-headless-shell"; |
| 34 | + const spawner = ChildProcessSpawner.make((command) => |
| 35 | + Effect.gen(function* () { |
| 36 | + if (!ChildProcess.isStandardCommand(command)) return yield* Effect.die("Unexpected pipeline"); |
| 37 | + commands.push([command.command, ...command.args]); |
| 38 | + if (command.command === "fc-match" && input.font === undefined) { |
| 39 | + return yield* PlatformError.systemError({ |
| 40 | + _tag: "NotFound", |
| 41 | + module: "ChildProcess", |
| 42 | + method: "spawn", |
| 43 | + }); |
| 44 | + } |
| 45 | + const output = |
| 46 | + command.command === "fc-match" |
| 47 | + ? (input.font ?? "") |
| 48 | + : command.command === browser && input.missingLibraries |
| 49 | + ? "error while loading shared libraries: libnss3.so: cannot open shared object file" |
| 50 | + : command.command === "ldd" |
| 51 | + ? "libnss3.so => not found\n" |
| 52 | + : "Chrome 154\n"; |
| 53 | + return ChildProcessSpawner.makeHandle({ |
| 54 | + pid: ChildProcessSpawner.ProcessId(1), |
| 55 | + exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)), |
| 56 | + isRunning: Effect.succeed(false), |
| 57 | + kill: () => Effect.void, |
| 58 | + unref: Effect.succeed(Effect.void), |
| 59 | + stdin: Sink.drain, |
| 60 | + stdout: Stream.make(new TextEncoder().encode(output)), |
| 61 | + stderr: Stream.empty, |
| 62 | + all: Stream.make(new TextEncoder().encode(output)), |
| 63 | + getInputFd: () => Sink.drain, |
| 64 | + getOutputFd: () => Stream.empty, |
| 65 | + }); |
| 66 | + }), |
| 67 | + ); |
| 68 | + const fs = FileSystem.makeNoop({ |
| 69 | + exists: (path) => |
| 70 | + Effect.succeed(path === "/usr/bin/apt-get" ? (input.apt ?? true) : path === browser), |
| 71 | + readFileString: () => Effect.succeed("0"), |
| 72 | + readDirectory: (path) => |
| 73 | + Effect.succeed( |
| 74 | + input.installedBrowser === false |
| 75 | + ? [] |
| 76 | + : path.endsWith("chrome-headless-shell") |
| 77 | + ? ["linux64"] |
| 78 | + : ["154"], |
| 79 | + ), |
| 80 | + }); |
| 81 | + return Effect.gen(function* () { |
| 82 | + yield* Command.runWith(browserCommand, { version: "0.0.0" })(["setup", "--base-dir", "/t3"]); |
| 83 | + return { commands, output: (yield* TestConsole.logLines).join("\n") }; |
| 84 | + }).pipe( |
| 85 | + Effect.provideService(FileSystem.FileSystem, fs), |
| 86 | + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 87 | + Effect.provideService(HostProcessPlatform, input.platform ?? "linux"), |
| 88 | + Effect.provideService(HostProcessUserId, input.root ? 0 : 1000), |
| 89 | + Effect.provideService(HostProcessEnvironment, { PATH: "" }), |
| 90 | + Effect.provideService(HostProcessArguments, ["/usr/bin/node", "/t3/bin.mjs"]), |
| 91 | + Effect.provideService(HostProcessExecutablePath, "/usr/bin/node"), |
| 92 | + Effect.provideService(HostProcessIsExecutable, false), |
| 93 | + Effect.provide(Layer.mergeAll(NodeServices.layer, TestConsole.layer)), |
| 94 | + ); |
| 95 | +}; |
| 96 | + |
| 97 | +describe("t3 browser setup fonts", () => { |
| 98 | + it.effect.each([undefined, ""])( |
| 99 | + "reports missing fonts even when Chrome starts (fc-match: %s)", |
| 100 | + (font) => |
| 101 | + Effect.gen(function* () { |
| 102 | + const { commands, output } = yield* runSetup({ font }); |
| 103 | + expect(output).toContain("needs fontconfig and system fonts"); |
| 104 | + expect(output).toContain("sudo t3 browser setup"); |
| 105 | + expect(output).not.toContain("This host is ready"); |
| 106 | + expect(commands.some(([name]) => name === "apt-get")).toBe(false); |
| 107 | + }), |
| 108 | + ); |
| 109 | + |
| 110 | + it.effect("installs fonts before the browser's first use", () => |
| 111 | + Effect.gen(function* () { |
| 112 | + const { commands, output } = yield* runSetup({ |
| 113 | + font: undefined, |
| 114 | + root: true, |
| 115 | + installedBrowser: false, |
| 116 | + }); |
| 117 | + expect(commands.filter(([name]) => name === "apt-get")).toEqual([ |
| 118 | + ["apt-get", "update"], |
| 119 | + ["apt-get", "install", "-y", "--no-install-recommends", "fontconfig", "fonts-liberation"], |
| 120 | + ]); |
| 121 | + expect(output).toContain("Restart the T3 server"); |
| 122 | + expect(output).toContain("This host is ready"); |
| 123 | + }), |
| 124 | + ); |
| 125 | + |
| 126 | + it.effect("leaves hosts with working fonts alone", () => |
| 127 | + Effect.gen(function* () { |
| 128 | + const { commands, output } = yield* runSetup({ |
| 129 | + font: "/usr/share/fonts/sans.ttf", |
| 130 | + root: true, |
| 131 | + }); |
| 132 | + expect(commands.some(([name]) => name === "apt-get")).toBe(false); |
| 133 | + expect(output).toContain("This host is ready"); |
| 134 | + }), |
| 135 | + ); |
| 136 | + |
| 137 | + it.effect("installs missing libraries and fonts with one package-list refresh", () => |
| 138 | + Effect.gen(function* () { |
| 139 | + const { commands } = yield* runSetup({ font: "", root: true, missingLibraries: true }); |
| 140 | + const packages = commands |
| 141 | + .filter(([name, action]) => name === "apt-get" && action === "install") |
| 142 | + .flatMap((command) => command.slice(2)); |
| 143 | + expect(packages).toContain("libnss3"); |
| 144 | + expect(packages).toContain("fonts-liberation"); |
| 145 | + expect( |
| 146 | + commands.filter(([name, action]) => name === "apt-get" && action === "update"), |
| 147 | + ).toHaveLength(1); |
| 148 | + }), |
| 149 | + ); |
| 150 | + |
| 151 | + it.effect("gives package-manager guidance without apt instead of reporting ready", () => |
| 152 | + Effect.gen(function* () { |
| 153 | + const { commands, output } = yield* runSetup({ font: "", root: true, apt: false }); |
| 154 | + expect(output).toContain("fontconfig and system fonts"); |
| 155 | + expect(output).toContain("Install them with your package manager"); |
| 156 | + expect(output).not.toContain("This host is ready"); |
| 157 | + expect(commands.some(([name]) => name === "apt-get")).toBe(false); |
| 158 | + }), |
| 159 | + ); |
| 160 | + |
| 161 | + it.effect("does not check fonts on macOS", () => |
| 162 | + Effect.gen(function* () { |
| 163 | + const { commands, output } = yield* runSetup({ font: undefined, platform: "darwin" }); |
| 164 | + expect(commands).toEqual([]); |
| 165 | + expect(output).toContain("Nothing to set up"); |
| 166 | + }), |
| 167 | + ); |
| 168 | +}); |
0 commit comments