Open History to see the last commit
1import { EnvironmentAuthInvalidError } from "@t3tools/contracts";
2import {
3 RelayAuthInvalidError,
4 RelayEnvironmentConnectNotAuthorizedError,
5 RelayEnvironmentEndpointTimedOutError,
6} from "@t3tools/contracts/relay";
7import { describe, expect, it } from "@effect/vitest";
8
9import {
10 mapManagedRelayError,
11 mapRemoteDpopEnvironmentError,
12 mapRemoteEnvironmentError,
13} from "./errors.ts";
14import { DPOP_RETRY_HINT, DPOP_UNKNOWN_HINT } from "../relay/errorPresentation.ts";
15import { ManagedRelayRequestFailedError } from "../relay/managedRelay.ts";
16import { NETWORK_BLOCKING_HINT } from "../errors/network.ts";
17import { RemoteEnvironmentAuthFetchError, RemoteEnvironmentAuthTimeoutError } from "../rpc/http.ts";
18
19describe("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 });
65 expect(mapConnectRefusal(undefined)).toMatchObject({
66 _tag: "ConnectionBlockedError",
67 reason: "permission",
68 });
69 });
70
71 it("presents clock skew as one possible cause for a generic DPoP error", () => {
72 const mapped = mapManagedRelayError(
73 new ManagedRelayRequestFailedError({
74 action: "connect relay environment",
75 cause: new Error("request failed"),
76 relayError: new RelayAuthInvalidError({
77 code: "auth_invalid",
78 reason: "invalid_dpop",
79 traceId: "trace-1",
80 }),
81 traceId: "trace-1",
82 }),
83 );
84
85 expect(mapped).toMatchObject({
86 _tag: "ConnectionBlockedError",
87 reason: "authentication",
88 detail: `Relay rejected the DPoP proof. ${DPOP_UNKNOWN_HINT}`,
89 traceId: "trace-1",
90 });
91 });
92
93 it("uses a neutral hint when the relay identifies a non-clock DPoP error", () => {
94 const mapped = mapManagedRelayError(
95 new ManagedRelayRequestFailedError({
96 action: "connect relay environment",
97 cause: new Error("request failed"),
98 relayError: new RelayAuthInvalidError({
99 code: "auth_invalid",
100 reason: "invalid_dpop",
101 dpopFailureReason: "key_mismatch",
102 traceId: "trace-1",
103 }),
104 }),
105 );
106
107 expect(mapped.message).toBe(`Relay rejected the DPoP proof. ${DPOP_RETRY_HINT}`);
108 });
109});
110
111describe("mapRemoteDpopEnvironmentError", () => {
112 it("keeps relay descriptor auth failures distinct from DPoP proof failures", () => {
113 const error = new EnvironmentAuthInvalidError({
114 code: "auth_invalid",
115 reason: "invalid_credential",
116 traceId: "trace-descriptor",
117 });
118 expect(mapRemoteEnvironmentError(error, "relay").message).toBe(
119 "The environment credential is invalid.",
120 );
121 expect(mapRemoteDpopEnvironmentError(error).message).toBe(
122 `The environment credential is invalid. ${DPOP_UNKNOWN_HINT}`,
123 );
124 });
125
126 it.each([
127 new RemoteEnvironmentAuthFetchError({
128 message: "Failed to fetch remote environment endpoint.",
129 cause: new TypeError("Failed to fetch"),
130 }),
131 new RemoteEnvironmentAuthTimeoutError("https://environment.example.test", 10_000),
132 ])("suggests another network when the relay endpoint cannot be reached: $_tag", (error) => {
133 const mapped = mapRemoteDpopEnvironmentError(error);
134 expect(mapped).toMatchObject({
135 _tag: "ConnectionTransientError",
136 detail: `${error.message} ${NETWORK_BLOCKING_HINT}`,
137 });
138 });
139
140 it("does not present a generic environment auth error as confirmed clock skew", () => {
141 const mapped = mapRemoteDpopEnvironmentError(
142 new EnvironmentAuthInvalidError({
143 code: "auth_invalid",
144 reason: "invalid_credential",
145 traceId: "trace-1",
146 }),
147 );
148
149 expect(mapped.message).toBe(`The environment credential is invalid. ${DPOP_UNKNOWN_HINT}`);
150 });
151
152 it("uses a neutral hint for a non-clock DPoP error from a new server", () => {
153 const mapped = mapRemoteDpopEnvironmentError(
154 new EnvironmentAuthInvalidError({
155 code: "auth_invalid",
156 reason: "invalid_credential",
157 dpopFailureReason: "key_mismatch",
158 traceId: "trace-1",
159 }),
160 );
161
162 expect(mapped.message).toBe(`The environment credential is invalid. ${DPOP_RETRY_HINT}`);
163 });
164});