The best way to control Claude Code, Codex and more from any computer or phone

main
ts 116 lines 4.59 KB
Raw
1 import { describe, expect, it } from "@effect/vitest";
2 import * as Effect from "effect/Effect";
3 import * as FileSystem from "effect/FileSystem";
4 import * as Layer from "effect/Layer";
5 import * as PlatformError from "effect/PlatformError";
6 import { ChildProcessSpawner } from "effect/process";
7
8 import * as HostProcess from "@t3tools/shared/HostProcess";
9 import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
10 import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
11 import { makeGhProcess } from "./gitHubCliProcess.ts";
12
13 function spawnedBusAddress(input: {
14 readonly platform?: NodeJS.Platform;
15 readonly address?: string;
16 readonly spawnAddress?: string;
17 /** File contents by path; a path not listed does not exist. */
18 readonly files?: Readonly<Record<string, string>>;
19 /** A path that exists but cannot be stat'ed or read. */
20 readonly unreadable?: string;
21 }) {
22 const files = input.files ?? {};
23 const denied = (path: string, method: string) =>
24 Effect.fail(
25 PlatformError.systemError({
26 _tag: path === input.unreadable ? "PermissionDenied" : "NotFound",
27 module: "FileSystem",
28 method,
29 pathOrDescriptor: path,
30 }),
31 );
32 const envs: Array<NodeJS.ProcessEnv | undefined> = [];
33 const run: SourceControlHost.SourceControlHost["Service"]["process"]["run"] = (spawn) =>
34 Effect.sync(() => {
35 envs.push(spawn.env);
36 return {
37 exitCode: ChildProcessSpawner.ExitCode(0),
38 stdout: "",
39 stderr: "",
40 stdoutTruncated: false,
41 stderrTruncated: false,
42 };
43 });
44 return Effect.gen(function* () {
45 const gh = yield* makeGhProcess;
46 yield* gh.run({
47 operation: "test",
48 command: "gh",
49 args: ["auth", "status"],
50 cwd: "/",
51 ...(input.spawnAddress === undefined
52 ? {}
53 : { env: { DBUS_SESSION_BUS_ADDRESS: input.spawnAddress } }),
54 });
55 return envs[0]?.DBUS_SESSION_BUS_ADDRESS;
56 }).pipe(
57 Effect.provide(
58 Layer.mergeAll(
59 TestSourceControlHost.layer({ process: { run } }),
60 FileSystem.layerNoop({
61 exists: (path) =>
62 path === input.unreadable ? denied(path, "exists") : Effect.succeed(path in files),
63 readFileString: (path) =>
64 path in files ? Effect.succeed(files[path]!) : denied(path, "readFileString"),
65 }),
66 ),
67 ),
68 Effect.provideService(HostProcess.Platform, input.platform ?? "linux"),
69 Effect.provideService(HostProcess.UserId, 1000),
70 Effect.provideService(
71 HostProcess.Environment,
72 input.address === undefined ? {} : { DBUS_SESSION_BUS_ADDRESS: input.address },
73 ),
74 );
75 }
76
77 describe("makeGhProcess", () => {
78 it.effect("turns the bus off when godbus would have to autolaunch one", () =>
79 Effect.gen(function* () {
80 expect(yield* spawnedBusAddress({})).toBe("disabled:");
81 expect(yield* spawnedBusAddress({ address: "autolaunch:" })).toBe("disabled:");
82 expect(yield* spawnedBusAddress({ spawnAddress: "autolaunch:" })).toBe("disabled:");
83 // A spawn that clears the host's address leaves godbus nothing to use either.
84 expect(yield* spawnedBusAddress({ address: "unix:path=/tmp/bus", spawnAddress: "" })).toBe(
85 "disabled:",
86 );
87 // godbus ignores a `dbus-session` file it cannot read or that names no address.
88 const session = "/run/user/1000/dbus-session";
89 expect(yield* spawnedBusAddress({ unreadable: session })).toBe("disabled:");
90 expect(yield* spawnedBusAddress({ files: { [session]: "garbage\n" } })).toBe("disabled:");
91 expect(
92 yield* spawnedBusAddress({ files: { [session]: "DBUS_SESSION_BUS_ADDRESS=\r\n" } }),
93 ).toBe("disabled:");
94 }),
95 );
96
97 it.effect("leaves a bus godbus can find or was given", () =>
98 Effect.gen(function* () {
99 expect(yield* spawnedBusAddress({ files: { "/run/user/1000/bus": "" } })).toBeUndefined();
100 expect(
101 yield* spawnedBusAddress({
102 files: {
103 "/run/user/1000/dbus-session": "DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-x\n",
104 },
105 }),
106 ).toBeUndefined();
107 expect(yield* spawnedBusAddress({ address: "unix:path=/tmp/bus" })).toBeUndefined();
108 expect(yield* spawnedBusAddress({ spawnAddress: "unix:path=/tmp/bus" })).toBe(
109 "unix:path=/tmp/bus",
110 );
111 expect(yield* spawnedBusAddress({ platform: "darwin" })).toBeUndefined();
112 // godbus counts a socket it cannot stat as present, so an unreadable one is left alone too.
113 expect(yield* spawnedBusAddress({ unreadable: "/run/user/1000/bus" })).toBeUndefined();
114 }),
115 );
116 });