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 | // @effect-diagnostics-next-line nodeBuiltinImport:off - The HTTPS test server needs certificate bytes. |
| 2 | import * as NodeFS from "node:fs"; |
| 3 | // @effect-diagnostics-next-line nodeBuiltinImport:off - A peer with a certificate this machine does not trust. |
| 4 | import * as NodeHttps from "node:https"; |
| 5 | import { NodeHttpServer } from "@effect/platform-node"; |
| 6 | import { type ExecutionEnvironmentDescriptor, OrchestratorMcpFailure } from "@t3tools/contracts"; |
| 7 | import { expect, it } from "@effect/vitest"; |
| 8 | import * as Context from "effect/Context"; |
| 9 | import * as DateTime from "effect/DateTime"; |
| 10 | import * as Deferred from "effect/Deferred"; |
| 11 | import * as Duration from "effect/Duration"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as Fiber from "effect/Fiber"; |
| 14 | import * as Layer from "effect/Layer"; |
| 15 | import * as Option from "effect/Option"; |
| 16 | import * as Queue from "effect/Queue"; |
| 17 | import * as Ref from "effect/Ref"; |
| 18 | import * as Schema from "effect/Schema"; |
| 19 | import * as HttpRouter from "effect/http/HttpRouter"; |
| 20 | import * as SqlClient from "effect/sql/SqlClient"; |
| 21 | import * as TestClock from "effect/testing/TestClock"; |
| 22 | import { Tool, Toolkit } from "effect/ai"; |
| 23 | import { |
| 24 | FetchHttpClient, |
| 25 | HttpClient, |
| 26 | HttpServer, |
| 27 | HttpServerRequest, |
| 28 | HttpServerResponse, |
| 29 | } from "effect/http"; |
| 30 | |
| 31 | import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; |
| 32 | import * as McpHttpServer from "../mcp/McpHttpServer.ts"; |
| 33 | import * as McpInvocationContext from "../mcp/McpInvocationContext.ts"; |
| 34 | import * as McpToolAccess from "../mcp/McpToolAccess.ts"; |
| 35 | import * as PeerLinks from "./PeerLinks.ts"; |
| 36 | import * as ServerSettings from "../serverSettings.ts"; |
| 37 | import { |
| 38 | descriptorOf, |
| 39 | layerLinkingEnvironment, |
| 40 | servePeer, |
| 41 | type ServedPeer, |
| 42 | } from "./PeerLinks.testkit.ts"; |
| 43 | import * as PeerMcpClient from "./PeerMcpClient.ts"; |
| 44 | |
| 45 | import { layerReaderDatabase, traceSqlRouting } from "../persistence/testkit/SqliteRouting.ts"; |
| 46 | |
| 47 | /** What B's probe saw: the calling client and the modes it may start work with. */ |
| 48 | const Probe = Schema.Struct({ caller: Schema.String, modes: Schema.String }); |
| 49 | |
| 50 | const ProbeToolkit = Toolkit.make( |
| 51 | Tool.make("probe", { |
| 52 | description: "Reports the calling client and the modes it may start work with.", |
| 53 | success: Probe, |
| 54 | failure: OrchestratorMcpFailure, |
| 55 | failureMode: "return", |
| 56 | dependencies: [McpInvocationContext.McpInvocationContext], |
| 57 | }), |
| 58 | Tool.make("refuse", { |
| 59 | description: "Always refuses.", |
| 60 | success: Probe, |
| 61 | failure: OrchestratorMcpFailure, |
| 62 | failureMode: "return", |
| 63 | }), |
| 64 | ); |
| 65 | |
| 66 | const probeHandlers = McpToolAccess.toLayer(ProbeToolkit, { |
| 67 | probe: McpToolAccess.reads(() => |
| 68 | McpInvocationContext.McpInvocationContext.pipe( |
| 69 | Effect.map((scope) => { |
| 70 | const modes = McpInvocationContext.clientModeCeiling(scope.client); |
| 71 | return { |
| 72 | caller: scope.client?.label ?? "none", |
| 73 | modes: `${modes.runtimeMode}/${modes.interactionMode}`, |
| 74 | }; |
| 75 | }), |
| 76 | ), |
| 77 | ), |
| 78 | refuse: McpToolAccess.reads(() => |
| 79 | Effect.fail( |
| 80 | new OrchestratorMcpFailure({ code: "thread_not_found", message: "No such thread." }), |
| 81 | ), |
| 82 | ), |
| 83 | }); |
| 84 | |
| 85 | const serveB = (descriptor: ExecutionEnvironmentDescriptor) => |
| 86 | servePeer(descriptor, McpHttpServer.toolkitRegistration(ProbeToolkit, probeHandlers)); |
| 87 | |
| 88 | /** Environment A, which links to B. */ |
| 89 | const makeA = ( |
| 90 | descriptor: ExecutionEnvironmentDescriptor, |
| 91 | secrets?: typeof ServerSecretStore.layer, |
| 92 | ) => |
| 93 | layerLinkingEnvironment(descriptor, secrets).pipe( |
| 94 | Layer.build, |
| 95 | Effect.map((context) => ({ |
| 96 | links: Context.get(context, PeerLinks.PeerLinks), |
| 97 | peer: Context.get(context, PeerMcpClient.PeerMcpClient), |
| 98 | sql: Context.get(context, SqlClient.SqlClient), |
| 99 | settings: Context.get(context, ServerSettings.ServerSettingsService), |
| 100 | })), |
| 101 | ); |
| 102 | |
| 103 | const laptop = descriptorOf("environment-laptop", "Laptop"); |
| 104 | const box = descriptorOf("environment-box", "Box"); |
| 105 | const probe = ( |
| 106 | a: Effect.Success<ReturnType<typeof makeA>>, |
| 107 | limits: PeerMcpClient.PeerCallLimits = { runtimeMode: "full-access", interactionMode: "default" }, |
| 108 | tool = "probe", |
| 109 | ) => |
| 110 | a.peer.call({ |
| 111 | environmentId: box.environmentId, |
| 112 | tool, |
| 113 | arguments: {}, |
| 114 | limits, |
| 115 | success: Probe, |
| 116 | }); |
| 117 | |
| 118 | const linkedSessions = (b: ServedPeer) => b.linkedSessions; |
| 119 | |
| 120 | /** Another environment on a socket of its own, answering as `descriptor`. */ |
| 121 | const serveApart = (descriptor: ExecutionEnvironmentDescriptor) => |
| 122 | Layer.build(Layer.fresh(NodeHttpServer.layerTest)).pipe( |
| 123 | Effect.flatMap((server) => serveB(descriptor).pipe(Effect.provideContext(server))), |
| 124 | ); |
| 125 | |
| 126 | it.effect("routes peer reads to readers and sees uncommitted link updates", () => |
| 127 | Effect.gen(function* () { |
| 128 | const context = yield* layerLinkingEnvironment( |
| 129 | laptop, |
| 130 | ServerSecretStore.layer, |
| 131 | layerReaderDatabase, |
| 132 | ).pipe(Layer.build); |
| 133 | const links = Context.get(context, PeerLinks.PeerLinks); |
| 134 | const sql = Context.get(context, SqlClient.SqlClient); |
| 135 | yield* sql` |
| 136 | INSERT INTO peer_environment_links ( |
| 137 | environment_id, secret_name, label, urls_json, access, linked_at, expires_at, fenced |
| 138 | ) VALUES ( |
| 139 | ${box.environmentId}, 'test-secret', 'Box', '[]', 'auto', |
| 140 | '2026-01-01T00:00:00.000Z', '2026-01-02T00:00:00.000Z', 1 |
| 141 | ) |
| 142 | `; |
| 143 | const { queries, trace } = traceSqlRouting(); |
| 144 | yield* links.list.pipe(trace); |
| 145 | expect(Option.getOrThrow(yield* links.get(box.environmentId).pipe(trace)).label).toBe("Box"); |
| 146 | const reads = queries.filter(({ query }) => |
| 147 | query.includes("SELECT * FROM peer_environment_links"), |
| 148 | ); |
| 149 | expect(reads).toHaveLength(2); |
| 150 | expect(reads.map((row) => row.connection)).toEqual(["reader", "reader"]); |
| 151 | yield* sql.withTransaction( |
| 152 | Effect.gen(function* () { |
| 153 | yield* sql`UPDATE peer_environment_links SET label = 'Uncommitted box'`; |
| 154 | expect(Option.getOrThrow(yield* links.get(box.environmentId)).label).toBe( |
| 155 | "Uncommitted box", |
| 156 | ); |
| 157 | expect((yield* links.list)[0]?.label).toBe("Uncommitted box"); |
| 158 | }), |
| 159 | ); |
| 160 | expect(yield* links.unlink(box.environmentId)).toBe(true); |
| 161 | expect(yield* links.list).toEqual([]); |
| 162 | }), |
| 163 | ); |
| 164 | |
| 165 | it.effect("links with a pairing code, and the peer holds its agents to the limits they carry", () => |
| 166 | Effect.scoped( |
| 167 | Effect.gen(function* () { |
| 168 | const b = yield* serveB(box); |
| 169 | const a = yield* makeA(laptop); |
| 170 | |
| 171 | const refused = yield* a.links |
| 172 | .link({ url: b.url, pairingCode: "not-a-code", access: "auto" }) |
| 173 | .pipe(Effect.flip); |
| 174 | expect(refused.reason).toBe("pairing_rejected"); |
| 175 | expect(yield* linkedSessions(b)).toEqual([]); |
| 176 | |
| 177 | const pairing = yield* b.auth.issuePairingCredential({ |
| 178 | access: "auto", |
| 179 | purpose: "peer-link", |
| 180 | }); |
| 181 | const linked = yield* a.links.link({ |
| 182 | url: b.url, |
| 183 | pairingCode: pairing.credential, |
| 184 | access: "auto", |
| 185 | }); |
| 186 | expect(linked).toMatchObject({ |
| 187 | environmentId: box.environmentId, |
| 188 | label: "Box", |
| 189 | urls: [b.url], |
| 190 | access: "auto", |
| 191 | }); |
| 192 | // B lists the link under A's name, where it can be revoked. |
| 193 | expect((yield* linkedSessions(b)).map((session) => session.client.label)).toEqual([ |
| 194 | "T3 Code · Laptop", |
| 195 | ]); |
| 196 | expect( |
| 197 | (yield* a.links.list).map(({ environmentId, status }) => [environmentId, status]), |
| 198 | ).toEqual([[box.environmentId, "reachable"]]); |
| 199 | |
| 200 | // The caller's limits narrow the link's access; they never widen it. |
| 201 | expect( |
| 202 | yield* probe(a, { runtimeMode: "approval-required", interactionMode: "plan" }), |
| 203 | ).toEqual({ caller: "T3 Code · Laptop", modes: "approval-required/plan" }); |
| 204 | expect(yield* probe(a)).toEqual({ caller: "T3 Code · Laptop", modes: "auto/default" }); |
| 205 | |
| 206 | // A refusal keeps the peer's reason. |
| 207 | const notFound = yield* probe(a, undefined, "refuse").pipe(Effect.flip); |
| 208 | expect(notFound).toMatchObject({ code: "thread_not_found", message: "No such thread." }); |
| 209 | }), |
| 210 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 211 | ); |
| 212 | |
| 213 | it.effect( |
| 214 | "each machine's Linked environments beta gates links, and a link waits while it is off", |
| 215 | () => |
| 216 | Effect.scoped( |
| 217 | Effect.gen(function* () { |
| 218 | const b = yield* serveB(box); |
| 219 | const a = yield* makeA(laptop); |
| 220 | const pairing = yield* b.auth.issuePairingCredential(); |
| 221 | const link = a.links.link({ url: b.url, pairingCode: pairing.credential, access: "auto" }); |
| 222 | |
| 223 | yield* a.settings.updateSettings({ linkedEnvironmentsEnabled: false }); |
| 224 | expect(yield* link.pipe(Effect.flip)).toMatchObject({ |
| 225 | reason: "disabled", |
| 226 | message: PeerLinks.LINKED_ENVIRONMENTS_OFF, |
| 227 | }); |
| 228 | expect(yield* Ref.get(b.bearers)).toEqual([]); |
| 229 | |
| 230 | yield* a.settings.updateSettings({ linkedEnvironmentsEnabled: true }); |
| 231 | yield* b.settings.updateSettings({ linkedEnvironmentsEnabled: false }); |
| 232 | const refusedThere = yield* link.pipe(Effect.flip); |
| 233 | expect(refusedThere.reason).toBe("pairing_rejected"); |
| 234 | expect(refusedThere.message).toContain("Linked environments is off on that machine"); |
| 235 | expect(yield* linkedSessions(b)).toEqual([]); |
| 236 | |
| 237 | yield* b.settings.updateSettings({ linkedEnvironmentsEnabled: true }); |
| 238 | yield* link; |
| 239 | yield* b.settings.updateSettings({ linkedEnvironmentsEnabled: false }); |
| 240 | // Not final, so a task already running there picks up again once it is back on. |
| 241 | const waiting = yield* probe(a).pipe(Effect.flip); |
| 242 | expect(waiting.code).toBe("orchestration_error"); |
| 243 | expect(waiting.message).toContain("has Linked environments turned off"); |
| 244 | yield* b.settings.updateSettings({ linkedEnvironmentsEnabled: true }); |
| 245 | expect(yield* probe(a)).toEqual({ caller: "T3 Code · Laptop", modes: "auto/default" }); |
| 246 | }), |
| 247 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 248 | ); |
| 249 | |
| 250 | it.effect("a link whose row cannot be stored keeps none of its token", () => |
| 251 | Effect.scoped( |
| 252 | Effect.gen(function* () { |
| 253 | const b = yield* serveB(box); |
| 254 | const held = yield* Ref.make<ReadonlySet<string>>(new Set()); |
| 255 | const noted = Layer.effect( |
| 256 | ServerSecretStore.ServerSecretStore, |
| 257 | Effect.map(ServerSecretStore.make, (store) => |
| 258 | ServerSecretStore.ServerSecretStore.of({ |
| 259 | ...store, |
| 260 | set: (name, value) => |
| 261 | store |
| 262 | .set(name, value) |
| 263 | .pipe(Effect.andThen(Ref.update(held, (names) => new Set([...names, name])))), |
| 264 | remove: (name) => |
| 265 | store |
| 266 | .remove(name) |
| 267 | .pipe( |
| 268 | Effect.andThen( |
| 269 | Ref.update(held, (names) => new Set([...names].filter((at) => at !== name))), |
| 270 | ), |
| 271 | ), |
| 272 | }), |
| 273 | ), |
| 274 | ); |
| 275 | const a = yield* makeA(laptop, noted); |
| 276 | yield* a.sql` |
| 277 | CREATE TRIGGER refuse_link BEFORE INSERT ON peer_environment_links |
| 278 | BEGIN SELECT RAISE(ABORT, 'database or disk is full'); END |
| 279 | `; |
| 280 | const pairing = yield* b.auth.issuePairingCredential(); |
| 281 | const failed = yield* a.links |
| 282 | .link({ url: b.url, pairingCode: pairing.credential, access: "auto" }) |
| 283 | .pipe(Effect.flip); |
| 284 | expect(failed.reason).toBe("storage"); |
| 285 | expect(yield* Ref.get(held)).toEqual(new Set()); |
| 286 | }), |
| 287 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 288 | ); |
| 289 | |
| 290 | it.effect("two links racing to one peer leave the stored access matching the token", () => |
| 291 | Effect.scoped( |
| 292 | Effect.gen(function* () { |
| 293 | const b = yield* serveB(box); |
| 294 | const a = yield* makeA(laptop); |
| 295 | const [first, second] = yield* Effect.all( |
| 296 | [b.auth.issuePairingCredential(), b.auth.issuePairingCredential()], |
| 297 | { concurrency: 2 }, |
| 298 | ); |
| 299 | yield* Effect.all( |
| 300 | [ |
| 301 | a.links.link({ url: b.url, pairingCode: first.credential, access: "read-only" }), |
| 302 | a.links.link({ url: b.url, pairingCode: second.credential, access: "full-access" }), |
| 303 | ], |
| 304 | { concurrency: 2 }, |
| 305 | ); |
| 306 | const [stored] = yield* a.links.list; |
| 307 | // The token sent is the one issued with the access the row records. |
| 308 | const { modes } = yield* probe(a); |
| 309 | expect(modes).toBe( |
| 310 | stored!.access === "read-only" ? "approval-required/default" : "full-access/default", |
| 311 | ); |
| 312 | }), |
| 313 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 314 | ); |
| 315 | |
| 316 | /** A bare HTTP server on a socket of its own, answering with `handle`. */ |
| 317 | const serveRaw = <E, R>( |
| 318 | handle: ( |
| 319 | request: HttpServerRequest.HttpServerRequest, |
| 320 | ) => Effect.Effect<HttpServerResponse.HttpServerResponse, E, R>, |
| 321 | ) => |
| 322 | Layer.build(Layer.fresh(NodeHttpServer.layerTest)).pipe( |
| 323 | Effect.flatMap((server) => |
| 324 | Effect.gen(function* () { |
| 325 | yield* HttpServer.serve(Effect.flatMap(HttpServerRequest.HttpServerRequest, handle)).pipe( |
| 326 | Layer.build, |
| 327 | ); |
| 328 | const address = (yield* HttpServer.HttpServer).address; |
| 329 | if (address._tag === "UnixPathAddress") return yield* Effect.die("expected a TCP address"); |
| 330 | return `http://127.0.0.1:${address.port}`; |
| 331 | }).pipe(Effect.provideContext(server)), |
| 332 | ), |
| 333 | ); |
| 334 | |
| 335 | it.effect("never follows a peer's redirect with the pairing code", () => |
| 336 | Effect.scoped( |
| 337 | Effect.gen(function* () { |
| 338 | const b = yield* serveB(box); |
| 339 | const received = yield* Ref.make<ReadonlyArray<string>>([]); |
| 340 | const elsewhere = yield* serveRaw((request) => |
| 341 | Ref.update(received, (seen) => [...seen, `${request.method} ${request.url}`]).pipe( |
| 342 | Effect.as(HttpServerResponse.empty({ status: 404 })), |
| 343 | ), |
| 344 | ); |
| 345 | // Answers as B, then sends every linking request on to another address. |
| 346 | const front = yield* serveRaw((request) => |
| 347 | request.url.startsWith("/.well-known/t3/environment") |
| 348 | ? HttpClient.get(`${b.url}${request.url}`).pipe( |
| 349 | Effect.flatMap((response) => response.text), |
| 350 | Effect.map((body) => |
| 351 | HttpServerResponse.text(body, { contentType: "application/json" }), |
| 352 | ), |
| 353 | Effect.provide(FetchHttpClient.layer), |
| 354 | Effect.orDie, |
| 355 | ) |
| 356 | : Effect.succeed( |
| 357 | HttpServerResponse.empty({ |
| 358 | status: 307, |
| 359 | headers: { location: `${elsewhere}${request.url}` }, |
| 360 | }), |
| 361 | ), |
| 362 | ); |
| 363 | const a = yield* makeA(laptop); |
| 364 | const pairing = yield* b.auth.issuePairingCredential(); |
| 365 | yield* a.links |
| 366 | .link({ url: front, pairingCode: pairing.credential, access: "auto" }) |
| 367 | .pipe(Effect.flip); |
| 368 | expect(yield* Ref.get(received)).toEqual([]); |
| 369 | expect(yield* a.links.list).toEqual([]); |
| 370 | }), |
| 371 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 372 | ); |
| 373 | |
| 374 | it.effect("never sends a link's token to an address that stops answering as that peer", () => |
| 375 | Effect.scoped( |
| 376 | Effect.gen(function* () { |
| 377 | const b = yield* serveB(box); |
| 378 | const a = yield* makeA(laptop); |
| 379 | const pairing = yield* b.auth.issuePairingCredential(); |
| 380 | yield* a.links.link({ url: b.url, pairingCode: pairing.credential, access: "auto" }); |
| 381 | yield* probe(a); |
| 382 | expect((yield* Ref.get(b.bearers)).length).toBeGreaterThan(0); |
| 383 | |
| 384 | // The address now answers as another environment, as a reused LAN address would. |
| 385 | yield* Ref.set(b.descriptor, descriptorOf("environment-impostor", "Impostor")); |
| 386 | yield* Ref.set(b.bearers, []); |
| 387 | const failed = yield* probe(a).pipe(Effect.flip); |
| 388 | expect(failed.message).toContain("Box did not answer"); |
| 389 | expect(yield* Ref.get(b.bearers)).toEqual([]); |
| 390 | expect((yield* a.links.list).map((link) => link.status)).toEqual(["unreachable"]); |
| 391 | }), |
| 392 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 393 | ); |
| 394 | |
| 395 | it.effect("lists a peer asleep at every address as unreachable within one probe budget", () => |
| 396 | Effect.scoped( |
| 397 | Effect.gen(function* () { |
| 398 | const b = yield* serveB(box); |
| 399 | // Another address of the same machine, asleep too. |
| 400 | const wake = yield* Deferred.make<void>(); |
| 401 | const elsewhere = yield* serveRaw(() => |
| 402 | Deferred.await(wake).pipe(Effect.as(HttpServerResponse.empty({ status: 503 }))), |
| 403 | ); |
| 404 | yield* Effect.addFinalizer(() => Deferred.succeed(wake, undefined)); |
| 405 | const a = yield* makeA(laptop); |
| 406 | const pairing = yield* b.auth.issuePairingCredential(); |
| 407 | yield* a.links.link({ |
| 408 | url: b.url, |
| 409 | alternateUrls: [elsewhere], |
| 410 | pairingCode: pairing.credential, |
| 411 | access: "auto", |
| 412 | }); |
| 413 | |
| 414 | yield* Ref.set(b.asleep, true); |
| 415 | const listing = yield* a.links.list.pipe(Effect.forkScoped); |
| 416 | yield* Queue.take(b.unanswered); |
| 417 | yield* TestClock.adjust(Duration.seconds(3)); |
| 418 | const listed = yield* Fiber.join(listing); |
| 419 | expect(listed.map((link) => link.status)).toEqual(["unreachable"]); |
| 420 | }), |
| 421 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 422 | ); |
| 423 | |
| 424 | it.effect("lists a peer as reachable at another address while its first one sleeps", () => |
| 425 | Effect.scoped( |
| 426 | Effect.gen(function* () { |
| 427 | const b = yield* serveB(box); |
| 428 | const described = yield* HttpClient.get(`${b.url}/.well-known/t3/environment`).pipe( |
| 429 | Effect.flatMap((response) => response.text), |
| 430 | Effect.provide(FetchHttpClient.layer), |
| 431 | ); |
| 432 | // Another address of the same machine that answers. |
| 433 | const elsewhere = yield* serveRaw(() => |
| 434 | Effect.succeed(HttpServerResponse.text(described, { contentType: "application/json" })), |
| 435 | ); |
| 436 | const a = yield* makeA(laptop); |
| 437 | const pairing = yield* b.auth.issuePairingCredential(); |
| 438 | yield* a.links.link({ |
| 439 | url: b.url, |
| 440 | alternateUrls: [elsewhere], |
| 441 | pairingCode: pairing.credential, |
| 442 | access: "auto", |
| 443 | }); |
| 444 | |
| 445 | yield* Ref.set(b.asleep, true); |
| 446 | expect((yield* a.links.list).map((link) => link.status)).toEqual(["reachable"]); |
| 447 | }), |
| 448 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 449 | ); |
| 450 | |
| 451 | it.effect("resolves a link at its answering address without waiting out a sleeping first", () => |
| 452 | Effect.scoped( |
| 453 | Effect.gen(function* () { |
| 454 | const b = yield* serveB(box); |
| 455 | const described = yield* HttpClient.get(`${b.url}/.well-known/t3/environment`).pipe( |
| 456 | Effect.flatMap((response) => response.text), |
| 457 | Effect.provide(FetchHttpClient.layer), |
| 458 | ); |
| 459 | const elsewhere = yield* serveRaw(() => |
| 460 | Effect.succeed(HttpServerResponse.text(described, { contentType: "application/json" })), |
| 461 | ); |
| 462 | const a = yield* makeA(laptop); |
| 463 | const pairing = yield* b.auth.issuePairingCredential(); |
| 464 | yield* a.links.link({ |
| 465 | url: b.url, |
| 466 | alternateUrls: [elsewhere], |
| 467 | pairingCode: pairing.credential, |
| 468 | access: "auto", |
| 469 | }); |
| 470 | |
| 471 | yield* Ref.set(b.asleep, true); |
| 472 | const first = yield* a.links.resolve(box.environmentId).pipe(Effect.forkScoped); |
| 473 | yield* Queue.take(b.unanswered); |
| 474 | yield* TestClock.adjust(Duration.seconds(3)); |
| 475 | expect((yield* Fiber.join(first)).url).toBe(elsewhere); |
| 476 | |
| 477 | // The clock no longer moves: a resolve that probes the sleeping address first never returns. |
| 478 | expect((yield* a.links.resolve(box.environmentId)).url).toBe(elsewhere); |
| 479 | }), |
| 480 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 481 | ); |
| 482 | |
| 483 | it.effect("cancelling a descriptor probe aborts its request and leaves later resolves fresh", () => |
| 484 | Effect.scoped( |
| 485 | Effect.gen(function* () { |
| 486 | const hold = yield* Ref.make(false); |
| 487 | const requests = yield* Ref.make(0); |
| 488 | const entered = yield* Deferred.make<void>(); |
| 489 | const interrupted = yield* Deferred.make<void>(); |
| 490 | const holdDescriptor = HttpRouter.middleware( |
| 491 | (httpEffect) => |
| 492 | Effect.gen(function* () { |
| 493 | const request = yield* HttpServerRequest.HttpServerRequest; |
| 494 | if (request.url.startsWith("/.well-known/t3/environment")) { |
| 495 | yield* Ref.update(requests, (count) => count + 1); |
| 496 | if (yield* Ref.get(hold)) { |
| 497 | return yield* Deferred.succeed(entered, undefined).pipe( |
| 498 | Effect.andThen(Effect.never), |
| 499 | Effect.onInterrupt(() => |
| 500 | Deferred.succeed(interrupted, undefined).pipe(Effect.asVoid), |
| 501 | ), |
| 502 | Effect.interruptible, |
| 503 | ); |
| 504 | } |
| 505 | } |
| 506 | return yield* httpEffect; |
| 507 | }), |
| 508 | { global: true }, |
| 509 | ); |
| 510 | const b = yield* servePeer( |
| 511 | box, |
| 512 | Layer.merge(McpHttpServer.toolkitRegistration(ProbeToolkit, probeHandlers), holdDescriptor), |
| 513 | ); |
| 514 | const a = yield* makeA(laptop); |
| 515 | const pairing = yield* b.auth.issuePairingCredential(); |
| 516 | yield* a.links.link({ url: b.url, pairingCode: pairing.credential, access: "auto" }); |
| 517 | yield* Ref.set(b.bearers, []); |
| 518 | yield* Ref.set(requests, 0); |
| 519 | yield* Ref.set(hold, true); |
| 520 | |
| 521 | const resolving = yield* a.links.resolve(box.environmentId).pipe(Effect.forkScoped); |
| 522 | yield* Deferred.await(entered); |
| 523 | yield* Fiber.interrupt(resolving); |
| 524 | yield* Deferred.await(interrupted); |
| 525 | |
| 526 | yield* Ref.set(hold, false); |
| 527 | expect((yield* a.links.resolve(box.environmentId)).url).toBe(b.url); |
| 528 | expect(yield* Ref.get(requests)).toBe(2); |
| 529 | expect(yield* Ref.get(b.bearers)).toEqual([]); |
| 530 | }), |
| 531 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 532 | ); |
| 533 | |
| 534 | it.live("one dropped descriptor request does not list a live peer as unreachable", () => |
| 535 | Effect.scoped( |
| 536 | Effect.gen(function* () { |
| 537 | const dropNext = yield* Ref.make(false); |
| 538 | const dropOnce = HttpRouter.middleware( |
| 539 | (httpEffect) => |
| 540 | Effect.gen(function* () { |
| 541 | const request = yield* HttpServerRequest.HttpServerRequest; |
| 542 | if ( |
| 543 | request.url.startsWith("/.well-known/t3/environment") && |
| 544 | (yield* Ref.getAndSet(dropNext, false)) |
| 545 | ) { |
| 546 | return HttpServerResponse.empty({ status: 503 }); |
| 547 | } |
| 548 | return yield* httpEffect; |
| 549 | }), |
| 550 | { global: true }, |
| 551 | ); |
| 552 | const b = yield* servePeer( |
| 553 | box, |
| 554 | Layer.merge(McpHttpServer.toolkitRegistration(ProbeToolkit, probeHandlers), dropOnce), |
| 555 | ); |
| 556 | const a = yield* makeA(laptop); |
| 557 | const pairing = yield* b.auth.issuePairingCredential(); |
| 558 | yield* a.links.link({ url: b.url, pairingCode: pairing.credential, access: "auto" }); |
| 559 | |
| 560 | yield* Ref.set(dropNext, true); |
| 561 | expect((yield* a.links.list).map((link) => link.status)).toEqual(["reachable"]); |
| 562 | }), |
| 563 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 564 | ); |
| 565 | |
| 566 | it.effect("reads one stored link without asking the peer", () => |
| 567 | Effect.scoped( |
| 568 | Effect.gen(function* () { |
| 569 | const b = yield* serveB(box); |
| 570 | const a = yield* makeA(laptop); |
| 571 | const pairing = yield* b.auth.issuePairingCredential(); |
| 572 | yield* a.links.link({ url: b.url, pairingCode: pairing.credential, access: "auto" }); |
| 573 | yield* Ref.set(b.probes, 0); |
| 574 | |
| 575 | const stored = yield* a.links.get(box.environmentId); |
| 576 | expect(Option.map(stored, (link) => link.label)).toEqual(Option.some("Box")); |
| 577 | expect(Option.isNone(yield* a.links.get(laptop.environmentId))).toBe(true); |
| 578 | expect(yield* a.links.names).toEqual([{ environmentId: box.environmentId, label: "Box" }]); |
| 579 | expect(yield* Ref.get(b.probes)).toBe(0); |
| 580 | |
| 581 | // An older answer that lands after a newer one never moves the expiry back. |
| 582 | const expiresAt = Option.getOrThrow(stored).expiresAt; |
| 583 | const later = DateTime.add(expiresAt, { days: 20 }); |
| 584 | yield* a.links.recordExpiry(box.environmentId, later); |
| 585 | yield* a.links.recordExpiry(box.environmentId, DateTime.add(expiresAt, { days: 10 })); |
| 586 | expect( |
| 587 | Option.map(yield* a.links.get(box.environmentId), (link) => |
| 588 | DateTime.formatIso(link.expiresAt), |
| 589 | ), |
| 590 | ).toEqual(Option.some(DateTime.formatIso(later))); |
| 591 | }), |
| 592 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 593 | ); |
| 594 | |
| 595 | it.effect("a linked peer that is renamed shows its new name", () => |
| 596 | Effect.scoped( |
| 597 | Effect.gen(function* () { |
| 598 | const b = yield* serveB(box); |
| 599 | const a = yield* makeA(laptop); |
| 600 | const pairing = yield* b.auth.issuePairingCredential(); |
| 601 | yield* a.links.link({ url: b.url, pairingCode: pairing.credential, access: "auto" }); |
| 602 | |
| 603 | yield* Ref.set(b.descriptor, descriptorOf(box.environmentId, "Box Mini")); |
| 604 | expect((yield* a.links.resolve(box.environmentId)).link.label).toBe("Box Mini"); |
| 605 | const stored = yield* a.links.get(box.environmentId); |
| 606 | expect(Option.map(stored, (link) => link.label)).toEqual(Option.some("Box Mini")); |
| 607 | |
| 608 | yield* Ref.set(b.descriptor, descriptorOf(box.environmentId, "Box Studio")); |
| 609 | expect((yield* a.links.list).map((link) => link.label)).toEqual(["Box Studio"]); |
| 610 | const storedAfterList = yield* a.links.get(box.environmentId); |
| 611 | expect(Option.map(storedAfterList, (link) => link.label)).toEqual(Option.some("Box Studio")); |
| 612 | }), |
| 613 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 614 | ); |
| 615 | |
| 616 | it.effect( |
| 617 | "a link forgotten while it resolves says it is not linked, not that it lost its credential", |
| 618 | () => |
| 619 | Effect.scoped( |
| 620 | Effect.gen(function* () { |
| 621 | const b = yield* serveB(box); |
| 622 | const beforeGet = yield* Ref.make<Effect.Effect<void>>(Effect.void); |
| 623 | const racing = Layer.effect( |
| 624 | ServerSecretStore.ServerSecretStore, |
| 625 | Effect.map(ServerSecretStore.make, (store) => |
| 626 | ServerSecretStore.ServerSecretStore.of({ |
| 627 | ...store, |
| 628 | get: (name) => |
| 629 | Ref.getAndSet(beforeGet, Effect.void).pipe( |
| 630 | Effect.flatten, |
| 631 | Effect.andThen(store.get(name)), |
| 632 | ), |
| 633 | }), |
| 634 | ), |
| 635 | ); |
| 636 | const a = yield* makeA(laptop, racing); |
| 637 | const pairing = yield* b.auth.issuePairingCredential(); |
| 638 | yield* a.links.link({ url: b.url, pairingCode: pairing.credential, access: "auto" }); |
| 639 | |
| 640 | yield* Ref.set(beforeGet, Effect.orDie(Effect.asVoid(a.links.unlink(box.environmentId)))); |
| 641 | const forgotten = yield* probe(a).pipe(Effect.flip); |
| 642 | expect(forgotten.message).toContain("not linked to"); |
| 643 | }), |
| 644 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 645 | ); |
| 646 | |
| 647 | it.effect("tells the agent to link again once the peer revokes the link or it expires", () => |
| 648 | Effect.scoped( |
| 649 | Effect.gen(function* () { |
| 650 | const b = yield* serveB(box); |
| 651 | const a = yield* makeA(laptop); |
| 652 | const pairing = yield* b.auth.issuePairingCredential(); |
| 653 | yield* a.links.link({ url: b.url, pairingCode: pairing.credential, access: "auto" }); |
| 654 | yield* probe(a); |
| 655 | |
| 656 | const [session] = yield* linkedSessions(b); |
| 657 | yield* b.auth.revokeSession(session!.sessionId); |
| 658 | const revoked = yield* probe(a).pipe(Effect.flip); |
| 659 | expect(revoked.code).toBe("capability_denied"); |
| 660 | expect(revoked.message).toContain("revoked there"); |
| 661 | |
| 662 | yield* a.links.link({ |
| 663 | url: b.url, |
| 664 | pairingCode: (yield* b.auth.issuePairingCredential()).credential, |
| 665 | access: "auto", |
| 666 | }); |
| 667 | // A link made with a peer that later answers without the fence, as |
| 668 | // one made before the fence or a peer moved back to such a version. |
| 669 | yield* Ref.set(b.descriptor, descriptorOf("environment-box", "Box", { linkFence: false })); |
| 670 | yield* Ref.set(b.bearers, []); |
| 671 | const unfenced = yield* probe(a).pipe(Effect.flip); |
| 672 | expect(unfenced.code).toBe("capability_denied"); |
| 673 | expect(unfenced.message).toContain("cannot keep a linked agent's limits"); |
| 674 | expect(yield* Ref.get(b.bearers)).toEqual([]); |
| 675 | yield* Ref.set(b.descriptor, box); |
| 676 | |
| 677 | // A link stored before links were marked: its session there is not a |
| 678 | // link's, so the peer would not fence its work even though it can. |
| 679 | yield* a.sql`UPDATE peer_environment_links SET fenced = 0`; |
| 680 | // Linking again is its only way back, as for an expired link. |
| 681 | expect((yield* a.links.list).map((link) => link.status)).toEqual(["expired"]); |
| 682 | const legacy = yield* probe(a).pipe(Effect.flip); |
| 683 | expect(legacy.code).toBe("capability_denied"); |
| 684 | expect(legacy.message).toContain("Link it again"); |
| 685 | expect(yield* Ref.get(b.bearers)).toEqual([]); |
| 686 | yield* a.links.link({ |
| 687 | url: b.url, |
| 688 | pairingCode: (yield* b.auth.issuePairingCredential()).credential, |
| 689 | access: "auto", |
| 690 | }); |
| 691 | yield* probe(a); |
| 692 | |
| 693 | yield* TestClock.adjust(Duration.days(31)); |
| 694 | expect((yield* a.links.list).map((link) => link.status)).toEqual(["expired"]); |
| 695 | expect(yield* a.links.names).toEqual([]); |
| 696 | yield* Ref.set(b.bearers, []); |
| 697 | const expired = yield* probe(a).pipe(Effect.flip); |
| 698 | expect(expired.code).toBe("capability_denied"); |
| 699 | expect(expired.message).toContain("expired"); |
| 700 | expect(yield* Ref.get(b.bearers)).toEqual([]); |
| 701 | }), |
| 702 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 703 | ); |
| 704 | |
| 705 | it.effect( |
| 706 | "refuses to link itself, plain http off this machine and the tailnet, or a peer that cannot hold a linked agent to its limits", |
| 707 | () => |
| 708 | Effect.scoped( |
| 709 | Effect.gen(function* () { |
| 710 | const b = yield* serveB( |
| 711 | descriptorOf("environment-box", "Box", { mcpModeLimitHeader: false }), |
| 712 | ); |
| 713 | const a = yield* makeA(laptop); |
| 714 | const pairing = yield* b.auth.issuePairingCredential(); |
| 715 | const older = yield* a.links |
| 716 | .link({ url: b.url, pairingCode: pairing.credential, access: "auto" }) |
| 717 | .pipe(Effect.flip); |
| 718 | expect(older.reason).toBe("incompatible"); |
| 719 | // Nor one whose link's work could reach the user's own threads there. |
| 720 | yield* Ref.set(b.descriptor, descriptorOf("environment-box", "Box", { linkFence: false })); |
| 721 | const unfenced = yield* a.links |
| 722 | .link({ url: b.url, pairingCode: pairing.credential, access: "auto" }) |
| 723 | .pipe(Effect.flip); |
| 724 | expect(unfenced.reason).toBe("incompatible"); |
| 725 | |
| 726 | // Plain http to a LAN address would carry the code and token in clear. |
| 727 | const lan = yield* a.links |
| 728 | .link({ |
| 729 | url: "http://192.168.1.20:3773", |
| 730 | pairingCode: pairing.credential, |
| 731 | access: "auto", |
| 732 | }) |
| 733 | .pipe(Effect.flip); |
| 734 | expect(lan.message).toContain("plain http"); |
| 735 | const lanAlternate = yield* a.links |
| 736 | .link({ |
| 737 | url: b.url, |
| 738 | alternateUrls: ["http://192.168.1.20:3773"], |
| 739 | pairingCode: pairing.credential, |
| 740 | access: "auto", |
| 741 | }) |
| 742 | .pipe(Effect.flip); |
| 743 | expect(lanAlternate.message).toContain("plain http"); |
| 744 | |
| 745 | yield* Ref.set(b.descriptor, laptop); |
| 746 | const self = yield* a.links |
| 747 | .link({ url: b.url, pairingCode: pairing.credential, access: "auto" }) |
| 748 | .pipe(Effect.flip); |
| 749 | expect(self.reason).toBe("self"); |
| 750 | // Neither attempt spent the pairing code on a session. |
| 751 | expect(yield* linkedSessions(b)).toEqual([]); |
| 752 | }), |
| 753 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 754 | ); |
| 755 | |
| 756 | it.effect( |
| 757 | "linking a picked machine skips an address that answers as another, and never signs in there", |
| 758 | () => |
| 759 | Effect.scoped( |
| 760 | Effect.gen(function* () { |
| 761 | const impostor = yield* serveApart(descriptorOf("environment-impostor", "Impostor")); |
| 762 | const b = yield* serveB(box); |
| 763 | const a = yield* makeA(laptop); |
| 764 | const pairing = yield* b.auth.issuePairingCredential(); |
| 765 | const linked = yield* a.links.link({ |
| 766 | url: impostor.url, |
| 767 | alternateUrls: [b.url], |
| 768 | pairingCode: pairing.credential, |
| 769 | access: "auto", |
| 770 | expectedEnvironmentId: box.environmentId, |
| 771 | }); |
| 772 | expect(linked).toMatchObject({ environmentId: box.environmentId, urls: [b.url] }); |
| 773 | // A picked machine is never linked at this machine's own loopback. |
| 774 | const own = yield* a.links |
| 775 | .link({ |
| 776 | url: b.url.replace("127.0.0.1", "localhost"), |
| 777 | pairingCode: pairing.credential, |
| 778 | access: "auto", |
| 779 | expectedEnvironmentId: box.environmentId, |
| 780 | }) |
| 781 | .pipe(Effect.flip); |
| 782 | expect(own.message).toContain("is this machine"); |
| 783 | yield* probe(a); |
| 784 | expect(yield* linkedSessions(impostor)).toEqual([]); |
| 785 | expect(yield* Ref.get(impostor.bearers)).toEqual([]); |
| 786 | expect((yield* linkedSessions(b)).length).toBe(1); |
| 787 | |
| 788 | // An address alone that answers as the linked box never takes over its link. |
| 789 | yield* Ref.set(impostor.descriptor, box); |
| 790 | const second = yield* impostor.auth.issuePairingCredential(); |
| 791 | const taken = yield* a.links |
| 792 | .link({ url: impostor.url, pairingCode: second.credential, access: "auto" }) |
| 793 | .pipe(Effect.flip); |
| 794 | expect(taken.message).toContain("already linked"); |
| 795 | expect(yield* linkedSessions(impostor)).toEqual([]); |
| 796 | expect(Option.getOrUndefined(yield* a.links.get(box.environmentId))?.urls).toEqual([b.url]); |
| 797 | // The address the link already has still renews it. |
| 798 | const renewal = yield* b.auth.issuePairingCredential(); |
| 799 | expect( |
| 800 | yield* a.links.link({ url: b.url, pairingCode: renewal.credential, access: "auto" }), |
| 801 | ).toMatchObject({ urls: [b.url] }); |
| 802 | }), |
| 803 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 804 | ); |
| 805 | |
| 806 | it.effect("linking a picked machine that answers nowhere names it and links nothing", () => |
| 807 | Effect.scoped( |
| 808 | Effect.gen(function* () { |
| 809 | const impostor = yield* serveApart(descriptorOf("environment-impostor", "Impostor")); |
| 810 | const a = yield* makeA(laptop); |
| 811 | const failed = yield* a.links |
| 812 | .link({ |
| 813 | url: impostor.url, |
| 814 | alternateUrls: ["http://192.168.1.20:3773"], |
| 815 | pairingCode: "unused", |
| 816 | access: "auto", |
| 817 | expectedEnvironmentId: box.environmentId, |
| 818 | expectedLabel: "Box", |
| 819 | }) |
| 820 | .pipe(Effect.flip); |
| 821 | expect(failed.reason).toBe("unreachable"); |
| 822 | expect(failed.answeredAs).toBe("Impostor"); |
| 823 | expect(failed.message).toBe( |
| 824 | `This environment could not reach Box: ${new URL(impostor.url).origin} answered as another environment, Impostor. http://192.168.1.20:3773 is plain http, which would send the link's credentials unencrypted. Use its https address, or its Tailscale address.`, |
| 825 | ); |
| 826 | expect(yield* a.links.list).toEqual([]); |
| 827 | expect(yield* linkedSessions(impostor)).toEqual([]); |
| 828 | |
| 829 | // Every address refused: the message says why. |
| 830 | const refused = yield* a.links |
| 831 | .link({ |
| 832 | url: "http://192.168.1.20:3773", |
| 833 | pairingCode: "unused", |
| 834 | access: "auto", |
| 835 | expectedEnvironmentId: box.environmentId, |
| 836 | expectedLabel: "Box", |
| 837 | }) |
| 838 | .pipe(Effect.flip); |
| 839 | expect(refused.message).toContain("could not reach Box"); |
| 840 | expect(refused.message).toContain("plain http"); |
| 841 | }), |
| 842 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 843 | ); |
| 844 | |
| 845 | it.live("linking an address with nothing listening says so, unlike one that is not T3 Code", () => |
| 846 | Effect.scoped( |
| 847 | Effect.gen(function* () { |
| 848 | const closed = yield* Effect.scoped( |
| 849 | serveRaw(() => Effect.succeed(HttpServerResponse.empty({ status: 404 }))), |
| 850 | ); |
| 851 | const notT3 = yield* serveRaw(() => |
| 852 | Effect.succeed( |
| 853 | HttpServerResponse.text("<h1>Not Found</h1>", { status: 404, contentType: "text/html" }), |
| 854 | ), |
| 855 | ); |
| 856 | const a = yield* makeA(laptop); |
| 857 | const linking = (url: string) => |
| 858 | a.links.link({ url, pairingCode: "unused", access: "auto" }).pipe(Effect.flip); |
| 859 | expect((yield* linking(closed)).message).toBe(`${closed} did not answer (ECONNREFUSED).`); |
| 860 | expect((yield* linking(notT3)).message).toBe( |
| 861 | `${notT3} answered, but not as a T3 Code environment this version understands.`, |
| 862 | ); |
| 863 | expect(yield* a.links.list).toEqual([]); |
| 864 | }), |
| 865 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 866 | ); |
| 867 | |
| 868 | /** An https listener whose certificate this machine does not trust. */ |
| 869 | const serveUntrusted = Layer.build( |
| 870 | Layer.fresh( |
| 871 | NodeHttpServer.layerServer( |
| 872 | () => |
| 873 | NodeHttps.createServer({ |
| 874 | key: NodeFS.readFileSync(new URL("./fixtures/localhost-key.pem", import.meta.url)), |
| 875 | cert: NodeFS.readFileSync(new URL("./fixtures/localhost-cert.pem", import.meta.url)), |
| 876 | }), |
| 877 | { port: 0, host: "127.0.0.1" }, |
| 878 | ), |
| 879 | ), |
| 880 | ).pipe( |
| 881 | Effect.map((server) => { |
| 882 | const address = Context.get(server, HttpServer.HttpServer).address; |
| 883 | if (address._tag === "UnixPathAddress") throw new Error("expected a TCP address"); |
| 884 | return `https://127.0.0.1:${address.port}`; |
| 885 | }), |
| 886 | ); |
| 887 | |
| 888 | it.effect("names an untrusted certificate instead of calling the peer something else", () => |
| 889 | Effect.scoped( |
| 890 | Effect.gen(function* () { |
| 891 | const untrusted = yield* serveUntrusted; |
| 892 | const a = yield* makeA(laptop); |
| 893 | const failed = yield* a.links |
| 894 | .link({ url: untrusted, pairingCode: "unused", access: "auto" }) |
| 895 | .pipe(Effect.flip); |
| 896 | expect(failed.reason).toBe("unreachable"); |
| 897 | expect(failed.message).toContain("certificate this machine rejected"); |
| 898 | expect(failed.message).toContain("DEPTH_ZERO_SELF_SIGNED_CERT"); |
| 899 | expect(yield* a.links.list).toEqual([]); |
| 900 | |
| 901 | // A stored link tries its next address past an untrusted one. |
| 902 | const b = yield* serveB(box); |
| 903 | const pairing = yield* b.auth.issuePairingCredential(); |
| 904 | yield* a.links.link({ url: b.url, pairingCode: pairing.credential, access: "auto" }); |
| 905 | yield* a.sql` |
| 906 | UPDATE peer_environment_links |
| 907 | SET urls_json = ${JSON.stringify([untrusted, b.url])} |
| 908 | WHERE environment_id = ${box.environmentId} |
| 909 | `; |
| 910 | expect((yield* a.links.resolve(box.environmentId)).url).toBe(b.url); |
| 911 | expect((yield* a.links.list).map((link) => link.status)).toEqual(["reachable"]); |
| 912 | }), |
| 913 | ).pipe(Effect.provide(NodeHttpServer.layerTest)), |
| 914 | ); |
| 915 | |
| 916 | it("reads Node's TLS verification code from anywhere in a cause chain", () => { |
| 917 | for (const code of [ |
| 918 | "DEPTH_ZERO_SELF_SIGNED_CERT", |
| 919 | "UNABLE_TO_VERIFY_LEAF_SIGNATURE", |
| 920 | "CERT_HAS_EXPIRED", |
| 921 | "ERR_TLS_CERT_ALTNAME_INVALID", |
| 922 | "HOSTNAME_MISMATCH", |
| 923 | "INVALID_CA", |
| 924 | "INVALID_PURPOSE", |
| 925 | "PATH_LENGTH_EXCEEDED", |
| 926 | ]) { |
| 927 | expect(PeerLinks.certificateErrorCode({ code })).toBe(code); |
| 928 | } |
| 929 | const wrapped = { message: "fetch failed", cause: { cause: { code: "INVALID_CA" } } }; |
| 930 | expect(PeerLinks.certificateErrorCode(wrapped)).toBe("INVALID_CA"); |
| 931 | expect(PeerLinks.certificateErrorCode({ cause: { code: "ECONNREFUSED" } })).toBeUndefined(); |
| 932 | expect(PeerLinks.certificateErrorCode(new Error("timed out"))).toBeUndefined(); |
| 933 | }); |