sailfork / t3code-github / packages/ client-runtime/ src/ connection/ errors.test.ts

import {
  RelayAuthInvalidError,
  RelayEnvironmentConnectNotAuthorizedError,
  RelayEnvironmentEndpointTimedOutError,
} from "@t3tools/contracts/relay";
import { describe, expect, it } from "@effect/vitest";

import { mapManagedRelayError, mapRemoteDpopEnvironmentError } from "./errors.ts";
import { DPOP_UNKNOWN_HINT } from "../relay/errorPresentation.ts";
import { ManagedRelayRequestFailedError } from "../relay/managedRelay.ts";
import { NETWORK_BLOCKING_HINT } from "../errors/network.ts";
import { RemoteEnvironmentAuthFetchError, RemoteEnvironmentAuthTimeoutError } from "../rpc/http.ts";

describe("mapManagedRelayError", () => {
  it("keeps a timeout reported by the relay distinct from a local network failure", () => {
    const relayError = new RelayEnvironmentEndpointTimedOutError({
      code: "environment_endpoint_timed_out",
      traceId: "trace-server-timeout",
    });
    const mapped = mapManagedRelayError(
      new ManagedRelayRequestFailedError({
        action: "connect relay environment",
        cause: relayError,
        relayError,
      }),
    );
    expect(mapped).toMatchObject({
      reason: "timeout",
      detail: "Relay timed out while contacting the environment endpoint.",
      traceId: "trace-server-timeout",
    });
  });

  it("retries a tunnel that is still being set up, and blocks other refusals", () => {
    const mapConnectRefusal = (reason: RelayEnvironmentConnectNotAuthorizedError["reason"]) => {
      const relayError = new RelayEnvironmentConnectNotAuthorizedError({
        code: "environment_connect_not_authorized",
        reason,
        traceId: "trace-connect-refused",
      });
      return mapManagedRelayError(
        new ManagedRelayRequestFailedError({
          action: "connect relay environment",
          cause: relayError,
          relayError,
        }),
      );
    };

    expect(mapConnectRefusal("managed_endpoint_allocation_not_ready")).toMatchObject({
      _tag: "ConnectionTransientError",
      reason: "endpoint-unavailable",
      detail: "The environment's T3 Connect tunnel is still being set up.",
      traceId: "trace-connect-refused",
    });
    expect(mapConnectRefusal("endpoint_provider_not_managed")).toMatchObject({
      _tag: "ConnectionBlockedError",
      reason: "permission",
      detail: expect.stringContaining("turn T3 Connect on"),
    });
    expect(mapConnectRefusal(undefined)).toMatchObject({
      _tag: "ConnectionBlockedError",
      reason: "permission",
    });
  });

  it("presents clock skew as one possible cause for a generic DPoP error", () => {
    const mapped = mapManagedRelayError(
      new ManagedRelayRequestFailedError({
        action: "connect relay environment",
        cause: new Error("request failed"),
        relayError: new RelayAuthInvalidError({
          code: "auth_invalid",
          reason: "invalid_dpop",
          traceId: "trace-1",
        }),
        traceId: "trace-1",
      }),
    );

    expect(mapped).toMatchObject({
      _tag: "ConnectionBlockedError",
      reason: "authentication",
      detail: `Relay rejected the DPoP proof. ${DPOP_UNKNOWN_HINT}`,
      traceId: "trace-1",
    });
  });
});

describe("mapRemoteDpopEnvironmentError", () => {
  it.each([
    new RemoteEnvironmentAuthFetchError({
      message: "Failed to fetch remote environment endpoint.",
      cause: new TypeError("Failed to fetch"),
    }),
    new RemoteEnvironmentAuthTimeoutError({
      requestUrl: "https://environment.example.test",
      timeoutMs: 10_000,
    }),
  ])("suggests another network when the relay endpoint cannot be reached: $_tag", (error) => {
    const mapped = mapRemoteDpopEnvironmentError(error);
    expect(mapped).toMatchObject({
      _tag: "ConnectionTransientError",
      detail: `${error.message} ${NETWORK_BLOCKING_HINT}`,
    });
  });
});