The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | import { SshHttpBridgeError } from "@t3tools/ssh/errors"; |
| 3 | import * as Cause from "effect/Cause"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Exit from "effect/Exit"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"; |
| 9 | |
| 10 | import { |
| 11 | DesktopSshEnvironmentRequestError, |
| 12 | fetchSshEnvironmentDescriptor, |
| 13 | } from "./sshEnvironment.ts"; |
| 14 | |
| 15 | function jsonResponse(request: HttpClientRequest.HttpClientRequest, body: unknown, status = 200) { |
| 16 | return HttpClientResponse.fromWeb( |
| 17 | request, |
| 18 | new Response(JSON.stringify(body), { |
| 19 | status, |
| 20 | headers: { "content-type": "application/json" }, |
| 21 | }), |
| 22 | ); |
| 23 | } |
| 24 | |
| 25 | function layerHttpClient( |
| 26 | handler: ( |
| 27 | request: HttpClientRequest.HttpClientRequest, |
| 28 | ) => Effect.Effect<HttpClientResponse.HttpClientResponse, never>, |
| 29 | ) { |
| 30 | return Layer.succeed( |
| 31 | HttpClient.HttpClient, |
| 32 | HttpClient.make((request) => handler(request)), |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | describe("SSH environment IPC", () => { |
| 37 | it.effect("rejects non-loopback HTTP endpoints before issuing a request", () => { |
| 38 | let requestCount = 0; |
| 39 | const layer = layerHttpClient((request) => |
| 40 | Effect.sync(() => { |
| 41 | requestCount += 1; |
| 42 | return jsonResponse(request, {}); |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | return Effect.gen(function* () { |
| 47 | const exit = yield* Effect.exit( |
| 48 | fetchSshEnvironmentDescriptor.handler({ |
| 49 | httpBaseUrl: "http://remote.example.com:41773/", |
| 50 | }), |
| 51 | ); |
| 52 | assert(Exit.isFailure(exit)); |
| 53 | const failure = Cause.findErrorOption(exit.cause); |
| 54 | assert(Option.isSome(failure)); |
| 55 | const error = failure.value; |
| 56 | |
| 57 | assert.instanceOf(error, DesktopSshEnvironmentRequestError); |
| 58 | assert.instanceOf(error.cause, SshHttpBridgeError); |
| 59 | assert.equal(requestCount, 0); |
| 60 | }).pipe(Effect.provide(layer)); |
| 61 | }); |
| 62 | }); |