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 { |
| 2 | RelayAuthInvalidError, |
| 3 | RelayEnvironmentConnectNotAuthorizedError, |
| 4 | RelayEnvironmentEndpointTimedOutError, |
| 5 | } from "@t3tools/contracts/relay"; |
| 6 | import { EnvironmentAuthInvalidError } from "@t3tools/contracts"; |
| 7 | import { describe, expect, it } from "@effect/vitest"; |
| 8 | |
| 9 | import { |
| 10 | isHostCredentialRefusal, |
| 11 | mapManagedRelayError, |
| 12 | mapRemoteDpopEnvironmentError, |
| 13 | } from "./errors.ts"; |
| 14 | import { DPOP_UNKNOWN_HINT } from "../relay/errorPresentation.ts"; |
| 15 | import { ManagedRelayRequestFailedError } from "../relay/managedRelay.ts"; |
| 16 | import { NETWORK_BLOCKING_HINT } from "../errors/network.ts"; |
| 17 | import { RemoteEnvironmentAuthFetchError, RemoteEnvironmentAuthTimeoutError } from "../rpc/http.ts"; |
| 18 | |
| 19 | describe("mapManagedRelayError", () => { |
| 20 | it("keeps a timeout reported by the relay distinct from a local network failure", () => { |
| 21 | const relayError = new RelayEnvironmentEndpointTimedOutError({ |
| 22 | code: "environment_endpoint_timed_out", |
| 23 | traceId: "trace-server-timeout", |
| 24 | }); |
| 25 | const mapped = mapManagedRelayError( |
| 26 | new ManagedRelayRequestFailedError({ |
| 27 | action: "connect relay environment", |
| 28 | cause: relayError, |
| 29 | relayError, |
| 30 | }), |
| 31 | ); |
| 32 | expect(mapped).toMatchObject({ |
| 33 | reason: "timeout", |
| 34 | detail: "Relay timed out while contacting the environment endpoint.", |
| 35 | traceId: "trace-server-timeout", |
| 36 | }); |
| 37 | }); |
| 38 | |
| 39 | it("retries a tunnel that is still being set up, and blocks other refusals", () => { |
| 40 | const mapConnectRefusal = (reason: RelayEnvironmentConnectNotAuthorizedError["reason"]) => { |
| 41 | const relayError = new RelayEnvironmentConnectNotAuthorizedError({ |
| 42 | code: "environment_connect_not_authorized", |
| 43 | reason, |
| 44 | traceId: "trace-connect-refused", |
| 45 | }); |
| 46 | return mapManagedRelayError( |
| 47 | new ManagedRelayRequestFailedError({ |
| 48 | action: "connect relay environment", |
| 49 | cause: relayError, |
| 50 | relayError, |
| 51 | }), |
| 52 | ); |
| 53 | }; |
| 54 | |
| 55 | expect(mapConnectRefusal("managed_endpoint_allocation_not_ready")).toMatchObject({ |
| 56 | _tag: "ConnectionTransientError", |
| 57 | reason: "endpoint-unavailable", |
| 58 | detail: "The environment's T3 Connect tunnel is still being set up.", |
| 59 | traceId: "trace-connect-refused", |
| 60 | }); |
| 61 | expect(mapConnectRefusal("endpoint_provider_not_managed")).toMatchObject({ |
| 62 | _tag: "ConnectionBlockedError", |
| 63 | reason: "permission", |
| 64 | detail: expect.stringContaining("turn T3 Connect on"), |
| 65 | }); |
| 66 | expect(mapConnectRefusal(undefined)).toMatchObject({ |
| 67 | _tag: "ConnectionBlockedError", |
| 68 | reason: "permission", |
| 69 | }); |
| 70 | }); |
| 71 | |
| 72 | it("presents clock skew as one possible cause for a generic DPoP error", () => { |
| 73 | const mapped = mapManagedRelayError( |
| 74 | new ManagedRelayRequestFailedError({ |
| 75 | action: "connect relay environment", |
| 76 | cause: new Error("request failed"), |
| 77 | relayError: new RelayAuthInvalidError({ |
| 78 | code: "auth_invalid", |
| 79 | reason: "invalid_dpop", |
| 80 | traceId: "trace-1", |
| 81 | }), |
| 82 | traceId: "trace-1", |
| 83 | }), |
| 84 | ); |
| 85 | |
| 86 | expect(mapped).toMatchObject({ |
| 87 | _tag: "ConnectionBlockedError", |
| 88 | reason: "authentication", |
| 89 | detail: `Relay rejected the DPoP proof. ${DPOP_UNKNOWN_HINT}`, |
| 90 | traceId: "trace-1", |
| 91 | }); |
| 92 | }); |
| 93 | }); |
| 94 | |
| 95 | describe("mapRemoteDpopEnvironmentError", () => { |
| 96 | it.each([ |
| 97 | new RemoteEnvironmentAuthFetchError({ |
| 98 | message: "Failed to fetch remote environment endpoint.", |
| 99 | cause: new TypeError("Failed to fetch"), |
| 100 | }), |
| 101 | new RemoteEnvironmentAuthTimeoutError({ |
| 102 | requestUrl: "https://environment.example.test", |
| 103 | timeoutMs: 10_000, |
| 104 | }), |
| 105 | ])("suggests another network when the relay endpoint cannot be reached: $_tag", (error) => { |
| 106 | const mapped = mapRemoteDpopEnvironmentError(error); |
| 107 | expect(mapped).toMatchObject({ |
| 108 | _tag: "ConnectionTransientError", |
| 109 | detail: `${error.message} ${NETWORK_BLOCKING_HINT}`, |
| 110 | }); |
| 111 | }); |
| 112 | |
| 113 | it("keeps a direct host's clock refusal apart from a refused pairing", () => { |
| 114 | const mapped = mapRemoteDpopEnvironmentError( |
| 115 | new EnvironmentAuthInvalidError({ |
| 116 | code: "auth_invalid", |
| 117 | reason: "invalid_credential", |
| 118 | dpopFailureReason: "time_window", |
| 119 | traceId: "trace-clock", |
| 120 | }), |
| 121 | "direct", |
| 122 | ); |
| 123 | expect(mapped).toMatchObject({ via: "direct", failureCause: "clock_skew" }); |
| 124 | expect(isHostCredentialRefusal(mapped)).toBe(false); |
| 125 | }); |
| 126 | }); |