The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { expect, it } from "@effect/vitest"; |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Sink from "effect/Sink"; |
| 5 | import * as Stream from "effect/Stream"; |
| 6 | import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 7 | import { |
| 8 | isLocalSshDeviceHost, |
| 9 | LocalDeviceHostAddresses, |
| 10 | remoteSshDeviceHosts, |
| 11 | } from "./localSshDeviceHost.ts"; |
| 12 | |
| 13 | const host = (target: string, port?: number) => ({ |
| 14 | kind: "ssh" as const, |
| 15 | id: target, |
| 16 | label: target, |
| 17 | target, |
| 18 | ...(port ? { port } : {}), |
| 19 | }); |
| 20 | const spawner = ChildProcessSpawner.make((command) => |
| 21 | Effect.gen(function* () { |
| 22 | if (command._tag !== "StandardCommand") return yield* Effect.die("Unexpected command"); |
| 23 | expect(command.args).toContain("-G"); |
| 24 | const target = command.args.map(String).at(-1); |
| 25 | const configs: Record<string, string> = { |
| 26 | "mac-mini": "hostname 100.65.180.100\nport 22\n", |
| 27 | remote: "hostname 192.0.2.1\nport 22\n", |
| 28 | loopback: "hostname 127.0.1.1\nport 22\n", |
| 29 | ipv6: "hostname ::1\nport 22\n", |
| 30 | mapped: "hostname ::ffff:127.0.0.1\nport 22\n", |
| 31 | forwarded: "hostname 127.0.0.1\nport 2222\n", |
| 32 | proxy: "hostname 127.0.0.1\nport 22\nproxyjump bastion\n", |
| 33 | command: "hostname 127.0.0.1\nport 22\nproxycommand nc remote 22\n", |
| 34 | unresolved: "hostname example.invalid\nport 22\n", |
| 35 | }; |
| 36 | return ChildProcessSpawner.makeHandle({ |
| 37 | pid: ChildProcessSpawner.ProcessId(123), |
| 38 | stdout: Stream.make(new TextEncoder().encode(configs[target ?? ""] ?? "")), |
| 39 | stderr: Stream.empty, |
| 40 | all: Stream.empty, |
| 41 | exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)), |
| 42 | isRunning: Effect.succeed(false), |
| 43 | kill: () => Effect.void, |
| 44 | stdin: Sink.drain, |
| 45 | getInputFd: () => Sink.drain, |
| 46 | getOutputFd: () => Stream.empty, |
| 47 | unref: Effect.succeed(Effect.void), |
| 48 | }); |
| 49 | }), |
| 50 | ); |
| 51 | const provide = <A, E>( |
| 52 | effect: Effect.Effect<A, E, Effect.Services<ReturnType<typeof isLocalSshDeviceHost>>>, |
| 53 | ) => |
| 54 | effect.pipe( |
| 55 | Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 56 | Effect.provideService(LocalDeviceHostAddresses, new Set(["100.65.180.100"])), |
| 57 | Effect.provide(NodeServices.layer), |
| 58 | ); |
| 59 | |
| 60 | it.effect("skips SSH aliases resolving to this machine, including loopback", () => |
| 61 | provide( |
| 62 | Effect.gen(function* () { |
| 63 | for (const target of ["mac-mini", "loopback", "ipv6", "mapped"]) { |
| 64 | expect(yield* isLocalSshDeviceHost(host(target))).toBe(true); |
| 65 | } |
| 66 | }), |
| 67 | ), |
| 68 | ); |
| 69 | |
| 70 | it.effect("keeps remote, forwarded, proxied, and unresolved destinations", () => |
| 71 | provide( |
| 72 | Effect.gen(function* () { |
| 73 | for (const target of ["remote", "forwarded", "proxy", "command", "unresolved"]) { |
| 74 | expect(yield* isLocalSshDeviceHost(host(target))).toBe(false); |
| 75 | } |
| 76 | }), |
| 77 | ), |
| 78 | ); |
| 79 | |
| 80 | it.effect("removes only self targets from a fanned-out host list", () => |
| 81 | provide( |
| 82 | Effect.gen(function* () { |
| 83 | const eas = { |
| 84 | kind: "eas" as const, |
| 85 | id: "eas", |
| 86 | label: "Expo Simulators", |
| 87 | accessToken: "", |
| 88 | maxSessionMinutes: 30, |
| 89 | idleMinutes: 10, |
| 90 | maxConcurrentSessions: 2, |
| 91 | }; |
| 92 | expect( |
| 93 | yield* remoteSshDeviceHosts([host("mac-mini"), eas, host("remote"), host("forwarded")]), |
| 94 | ).toEqual([eas, host("remote"), host("forwarded")]); |
| 95 | }), |
| 96 | ), |
| 97 | ); |