Open History to see the last commit
1import { EnvironmentAuthInvalidError } from "@t3tools/contracts";
2import {
3 RelayAuthInvalidError,
4 RelayEnvironmentEndpointTimedOutError,
5} from "@t3tools/contracts/relay";
6import { describe, expect, it } from "@effect/vitest";
7
8import {
9 mapManagedRelayError,
10 mapRemoteDpopEnvironmentError,
11 mapRemoteEnvironmentError,
12} from "./errors.ts";
13import { DPOP_RETRY_HINT, DPOP_UNKNOWN_HINT } from "../relay/errorPresentation.ts";
14import { ManagedRelayRequestFailedError } from "../relay/managedRelay.ts";
15import { NETWORK_BLOCKING_HINT } from "../errors/network.ts";
16import { RemoteEnvironmentAuthFetchError, RemoteEnvironmentAuthTimeoutError } from "../rpc/http.ts";
17
18describe("mapManagedRelayError", () => {
19 it("keeps a timeout reported by the relay distinct from a local network failure", () => {
20 const relayError = new RelayEnvironmentEndpointTimedOutError({
21 code: "environment_endpoint_timed_out",
22 traceId: "trace-server-timeout",
23 });
24 const mapped = mapManagedRelayError(
25 new ManagedRelayRequestFailedError({
26 action: "connect relay environment",
27 cause: relayError,
28 relayError,
29 }),
30 );
31 expect(mapped).toMatchObject({
32 reason: "timeout",
33 detail: "Relay timed out while contacting the environment endpoint.",
34 traceId: "trace-server-timeout",
35 });
36 });
37
38 it("presents clock skew as one possible cause for a generic DPoP error", () => {
39 const mapped = mapManagedRelayError(
40 new ManagedRelayRequestFailedError({
41 action: "connect relay environment",
42 cause: new Error("request failed"),
43 relayError: new RelayAuthInvalidError({
44 code: "auth_invalid",
45 reason: "invalid_dpop",
46 traceId: "trace-1",
47 }),
48 traceId: "trace-1",
49 }),
50 );
51
52 expect(mapped).toMatchObject({
53 _tag: "ConnectionBlockedError",
54 reason: "authentication",
55 detail: `Relay rejected the DPoP proof. ${DPOP_UNKNOWN_HINT}`,
56 traceId: "trace-1",
57 });
58 });
59
60 it("uses a neutral hint when the relay identifies a non-clock DPoP error", () => {
61 const mapped = mapManagedRelayError(
62 new ManagedRelayRequestFailedError({
63 action: "connect relay environment",
64 cause: new Error("request failed"),
65 relayError: new RelayAuthInvalidError({
66 code: "auth_invalid",
67 reason: "invalid_dpop",
68 dpopFailureReason: "key_mismatch",
69 traceId: "trace-1",
70 }),
71 }),
72 );
73
74 expect(mapped.message).toBe(`Relay rejected the DPoP proof. ${DPOP_RETRY_HINT}`);
75 });
76});
77
78describe("mapRemoteDpopEnvironmentError", () => {
79 it("keeps relay descriptor auth failures distinct from DPoP proof failures", () => {
80 const error = new EnvironmentAuthInvalidError({
81 code: "auth_invalid",
82 reason: "invalid_credential",
83 traceId: "trace-descriptor",
84 });
85 expect(mapRemoteEnvironmentError(error, "relay").message).toBe(
86 "The environment credential is invalid.",
87 );
88 expect(mapRemoteDpopEnvironmentError(error).message).toBe(
89 `The environment credential is invalid. ${DPOP_UNKNOWN_HINT}`,
90 );
91 });
92
93 it.each([
94 new RemoteEnvironmentAuthFetchError({
95 message: "Failed to fetch remote environment endpoint.",
96 cause: new TypeError("Failed to fetch"),
97 }),
98 new RemoteEnvironmentAuthTimeoutError("https://environment.example.test", 10_000),
99 ])("suggests another network when the relay endpoint cannot be reached: $_tag", (error) => {
100 const mapped = mapRemoteDpopEnvironmentError(error);
101 expect(mapped).toMatchObject({
102 _tag: "ConnectionTransientError",
103 detail: `${error.message} ${NETWORK_BLOCKING_HINT}`,
104 });
105 });
106
107 it("does not present a generic environment auth error as confirmed clock skew", () => {
108 const mapped = mapRemoteDpopEnvironmentError(
109 new EnvironmentAuthInvalidError({
110 code: "auth_invalid",
111 reason: "invalid_credential",
112 traceId: "trace-1",
113 }),
114 );
115
116 expect(mapped.message).toBe(`The environment credential is invalid. ${DPOP_UNKNOWN_HINT}`);
117 });
118
119 it("uses a neutral hint for a non-clock DPoP error from a new server", () => {
120 const mapped = mapRemoteDpopEnvironmentError(
121 new EnvironmentAuthInvalidError({
122 code: "auth_invalid",
123 reason: "invalid_credential",
124 dpopFailureReason: "key_mismatch",
125 traceId: "trace-1",
126 }),
127 );
128
129 expect(mapped.message).toBe(`The environment credential is invalid. ${DPOP_RETRY_HINT}`);
130 });
131});