The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { expect, it } from "@effect/vitest"; |
| 3 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as PlatformError from "effect/PlatformError"; |
| 8 | import * as Sink from "effect/Sink"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import * as TestConsole from "effect/testing/TestConsole"; |
| 11 | import { Command } from "effect/cli"; |
| 12 | import { ChildProcessSpawner } from "effect/process"; |
| 13 | |
| 14 | import packageJson from "../../package.json" with { type: "json" }; |
| 15 | import * as PreviewBrowserHost from "../preview/PreviewBrowserHost.ts"; |
| 16 | import { browserCommand } from "./browser.ts"; |
| 17 | |
| 18 | const USERNS_RESTRICTION = "/proc/sys/kernel/apparmor_restrict_unprivileged_userns"; |
| 19 | const BROWSER_ROOT = "/home/me/.t3/tools/chrome-headless-shell"; |
| 20 | const BROWSER = `${BROWSER_ROOT}/linux64/154/chrome-headless-shell`; |
| 21 | |
| 22 | const notFound = (method: string, path: string) => |
| 23 | PlatformError.systemError({ |
| 24 | _tag: "NotFound", |
| 25 | module: "FileSystem", |
| 26 | method, |
| 27 | pathOrDescriptor: path, |
| 28 | }); |
| 29 | |
| 30 | /** An Ubuntu host with the userns restriction on, the browser installed, and a system font. */ |
| 31 | const runSetupAsRoot = (input: { readonly profile: string | undefined }) => { |
| 32 | const files = new Map([ |
| 33 | [USERNS_RESTRICTION, "1\n"], |
| 34 | [BROWSER, ""], |
| 35 | ]); |
| 36 | if (input.profile !== undefined) { |
| 37 | files.set(PreviewBrowserHost.APPARMOR_PROFILE_PATH, input.profile); |
| 38 | } |
| 39 | const directories = new Map([ |
| 40 | [BROWSER_ROOT, ["linux64"]], |
| 41 | [`${BROWSER_ROOT}/linux64`, ["154"]], |
| 42 | ]); |
| 43 | const commands: Array<string> = []; |
| 44 | const fileSystem = FileSystem.makeNoop({ |
| 45 | exists: (path) => Effect.succeed(files.has(path)), |
| 46 | readFileString: (path) => { |
| 47 | const content = files.get(path); |
| 48 | return content === undefined |
| 49 | ? Effect.fail(notFound("readFileString", path)) |
| 50 | : Effect.succeed(content); |
| 51 | }, |
| 52 | readDirectory: (path) => { |
| 53 | const entries = directories.get(path); |
| 54 | return entries === undefined |
| 55 | ? Effect.fail(notFound("readDirectory", path)) |
| 56 | : Effect.succeed(entries); |
| 57 | }, |
| 58 | writeFileString: (path, content) => Effect.sync(() => void files.set(path, content)), |
| 59 | }); |
| 60 | const spawner = ChildProcessSpawner.make((command) => |
| 61 | Effect.sync(() => { |
| 62 | const { command: name, args } = command as unknown as { |
| 63 | readonly command: string; |
| 64 | readonly args: ReadonlyArray<string>; |
| 65 | }; |
| 66 | commands.push([name, ...args].join(" ")); |
| 67 | const output = |
| 68 | name === "fc-match" ? "/usr/share/fonts/sans.ttf" : name === BROWSER ? "Chrome 154\n" : ""; |
| 69 | return ChildProcessSpawner.makeHandle({ |
| 70 | pid: ChildProcessSpawner.ProcessId(1), |
| 71 | exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)), |
| 72 | isRunning: Effect.succeed(false), |
| 73 | kill: () => Effect.void, |
| 74 | unref: Effect.succeed(Effect.void), |
| 75 | stdin: Sink.drain, |
| 76 | stdout: Stream.make(new TextEncoder().encode(output)), |
| 77 | stderr: Stream.empty, |
| 78 | all: Stream.make(new TextEncoder().encode(output)), |
| 79 | getInputFd: () => Sink.drain, |
| 80 | getOutputFd: () => Stream.empty, |
| 81 | }); |
| 82 | }), |
| 83 | ); |
| 84 | return Effect.gen(function* () { |
| 85 | yield* Command.runWith(browserCommand, { version: packageJson.version })([ |
| 86 | "setup", |
| 87 | "--base-dir", |
| 88 | "/home/me/.t3", |
| 89 | ]); |
| 90 | return { |
| 91 | output: (yield* TestConsole.logLines).join("\n"), |
| 92 | profile: files.get(PreviewBrowserHost.APPARMOR_PROFILE_PATH), |
| 93 | loads: commands.filter((command) => command.startsWith("apparmor_parser")), |
| 94 | }; |
| 95 | }).pipe( |
| 96 | Effect.provideService(HostProcess.Platform, "linux"), |
| 97 | Effect.provideService(HostProcess.UserId, 0), |
| 98 | Effect.provideService(HostProcess.Environment, { SUDO_USER: "me" }), |
| 99 | Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 100 | Effect.provideService(FileSystem.FileSystem, fileSystem), |
| 101 | Effect.provide(Layer.mergeAll(NodeServices.layer, Layer.fresh(TestConsole.layer))), |
| 102 | ); |
| 103 | }; |
| 104 | |
| 105 | const LOAD = `apparmor_parser -r ${PreviewBrowserHost.APPARMOR_PROFILE_PATH}`; |
| 106 | |
| 107 | const CUSTOMIZED_PROFILE = PreviewBrowserHost.APPARMOR_PROFILE.replace( |
| 108 | " userns,\n", |
| 109 | " userns,\n deny /tmp/t3-admin-private/** rw,\n", |
| 110 | ); |
| 111 | |
| 112 | it.effect.each([ |
| 113 | { |
| 114 | label: "reloads a profile that is on disk", |
| 115 | profile: PreviewBrowserHost.APPARMOR_PROFILE, |
| 116 | written: PreviewBrowserHost.APPARMOR_PROFILE, |
| 117 | said: "Reloaded the AppArmor profile", |
| 118 | }, |
| 119 | { |
| 120 | label: "writes and loads a missing profile", |
| 121 | profile: undefined, |
| 122 | written: PreviewBrowserHost.APPARMOR_PROFILE, |
| 123 | said: "Wrote and loaded", |
| 124 | }, |
| 125 | { |
| 126 | label: "reloads a customized profile without rewriting it", |
| 127 | profile: CUSTOMIZED_PROFILE, |
| 128 | written: CUSTOMIZED_PROFILE, |
| 129 | said: "Reloaded the AppArmor profile", |
| 130 | }, |
| 131 | ])("t3 browser setup as root $label", (input) => |
| 132 | Effect.gen(function* () { |
| 133 | const result = yield* runSetupAsRoot({ profile: input.profile }); |
| 134 | expect(result.loads).toEqual([LOAD]); |
| 135 | expect(result.profile).toBe(input.written); |
| 136 | expect(result.output).toContain(input.said); |
| 137 | expect(result.output).toContain("This host is ready for T3's browser."); |
| 138 | }), |
| 139 | ); |