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

main
ts 103 lines 3.55 KB
Raw
1 import { assert, it } from "@effect/vitest";
2 import * as HostProcess from "@t3tools/shared/HostProcess";
3 import * as Effect from "effect/Effect";
4 import * as Fiber from "effect/Fiber";
5 import * as FileSystem from "effect/FileSystem";
6 import * as Layer from "effect/Layer";
7 import * as Sink from "effect/Sink";
8 import * as Stream from "effect/Stream";
9 import * as TestClock from "effect/testing/TestClock";
10 import { ChildProcessSpawner } from "effect/process";
11
12 import * as ProcessRunner from "../processRunner.ts";
13 import { resolveServerEnvironmentLabel } from "./ServerEnvironmentLabel.ts";
14
15 const neverExits = ChildProcessSpawner.make(() =>
16 Effect.succeed(
17 ChildProcessSpawner.makeHandle({
18 pid: ChildProcessSpawner.ProcessId(1),
19 exitCode: Effect.never,
20 isRunning: Effect.succeed(true),
21 kill: () => Effect.void,
22 unref: Effect.succeed(Effect.void),
23 stdin: Sink.drain,
24 stdout: Stream.never,
25 stderr: Stream.never,
26 all: Stream.empty,
27 getInputFd: () => Sink.drain,
28 getOutputFd: () => Stream.empty,
29 }),
30 ),
31 );
32
33 it.effect("falls back to the hostname when hostnamectl never answers", () =>
34 Effect.gen(function* () {
35 // Without /etc/machine-info the label comes from hostnamectl, which waits on D-Bus.
36 const label = yield* resolveServerEnvironmentLabel({ cwdBaseName: "project" }).pipe(
37 Effect.provideService(HostProcess.Platform, "linux"),
38 Effect.provideService(HostProcess.Hostname, "devbox"),
39 Effect.provide(
40 Layer.mergeAll(
41 FileSystem.layerNoop({ exists: () => Effect.succeed(false) }),
42 ProcessRunner.layer.pipe(
43 Layer.provide(Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, neverExits)),
44 ),
45 ),
46 ),
47 Effect.forkChild,
48 );
49 yield* TestClock.adjust("5 seconds");
50 assert.equal(yield* Fiber.join(label), "devbox");
51 }),
52 );
53
54 const exitsWithFailure = ProcessRunner.ProcessRunner.of({
55 run: () =>
56 Effect.succeed({
57 stdout: "",
58 stderr: "",
59 code: ChildProcessSpawner.ExitCode(1),
60 timedOut: false,
61 stdoutTruncated: false,
62 stderrTruncated: false,
63 stdoutInvalidUtf8: false,
64 stderrInvalidUtf8: false,
65 }),
66 });
67
68 it.effect("qualifies the inherited Windows hostname only inside WSL", () =>
69 Effect.gen(function* () {
70 const cases = [
71 {
72 kernel: "6.6.87.2-microsoft-standard-WSL2",
73 machineInfo: null,
74 expected: "DESKTOP-PC WSL (Ubuntu)",
75 },
76 { kernel: "6.8.0-45-generic", machineInfo: null, expected: "DESKTOP-PC" },
77 {
78 kernel: "6.6.87.2-microsoft-standard-WSL2",
79 machineInfo: 'PRETTY_HOSTNAME="Work WSL"',
80 expected: "Work WSL",
81 },
82 ];
83 for (const { kernel, machineInfo, expected } of cases) {
84 const files: Record<string, string | null> = {
85 "/proc/sys/kernel/osrelease": `${kernel}\n`,
86 "/etc/machine-info": machineInfo,
87 };
88 const label = yield* resolveServerEnvironmentLabel({ cwdBaseName: "project" }).pipe(
89 Effect.provideService(HostProcess.Platform, "linux"),
90 Effect.provideService(HostProcess.Hostname, "DESKTOP-PC"),
91 Effect.provideService(HostProcess.Environment, { WSL_DISTRO_NAME: "Ubuntu" }),
92 Effect.provideService(ProcessRunner.ProcessRunner, exitsWithFailure),
93 Effect.provide(
94 FileSystem.layerNoop({
95 exists: (path) => Effect.succeed(files[path] != null),
96 readFileString: (path) => Effect.succeed(files[path] ?? ""),
97 }),
98 ),
99 );
100 assert.equal(label, expected, kernel);
101 }
102 }),
103 );