Open History to see the last commit
1import { EnvironmentAuthInvalidError } from "@t3tools/contracts";
2import { RelayAuthInvalidError } from "@t3tools/contracts/relay";
3import { describe, expect, it } from "@effect/vitest";
4
5import { mapManagedRelayError, mapRemoteDpopEnvironmentError } from "./errors.ts";
6import { DPOP_RETRY_HINT, DPOP_UNKNOWN_HINT } from "../relay/errorPresentation.ts";
7import { ManagedRelayRequestFailedError } from "../relay/managedRelay.ts";
8
9describe("mapManagedRelayError", () => {
10 it("presents clock skew as one possible cause for a generic DPoP error", () => {
11 const mapped = mapManagedRelayError(
12 new ManagedRelayRequestFailedError({
13 action: "connect relay environment",
14 cause: new Error("request failed"),
15 relayError: new RelayAuthInvalidError({
16 code: "auth_invalid",
17 reason: "invalid_dpop",
18 traceId: "trace-1",
19 }),
20 traceId: "trace-1",
21 }),
22 );
23
24 expect(mapped).toMatchObject({
25 _tag: "ConnectionBlockedError",
26 reason: "authentication",
27 detail: `Relay rejected the DPoP proof. ${DPOP_UNKNOWN_HINT}`,
28 traceId: "trace-1",
29 });
30 });
31
32 it("uses a neutral hint when the relay identifies a non-clock DPoP error", () => {
33 const mapped = mapManagedRelayError(
34 new ManagedRelayRequestFailedError({
35 action: "connect relay environment",
36 cause: new Error("request failed"),
37 relayError: new RelayAuthInvalidError({
38 code: "auth_invalid",
39 reason: "invalid_dpop",
40 dpopFailureReason: "key_mismatch",
41 traceId: "trace-1",
42 }),
43 }),
44 );
45
46 expect(mapped.message).toBe(`Relay rejected the DPoP proof. ${DPOP_RETRY_HINT}`);
47 });
48});
49
50describe("mapRemoteDpopEnvironmentError", () => {
51 it("does not present a generic environment auth error as confirmed clock skew", () => {
52 const mapped = mapRemoteDpopEnvironmentError(
53 new EnvironmentAuthInvalidError({
54 code: "auth_invalid",
55 reason: "invalid_credential",
56 traceId: "trace-1",
57 }),
58 );
59
60 expect(mapped.message).toBe(`The environment credential is invalid. ${DPOP_UNKNOWN_HINT}`);
61 });
62
63 it("uses a neutral hint for a non-clock DPoP error from a new server", () => {
64 const mapped = mapRemoteDpopEnvironmentError(
65 new EnvironmentAuthInvalidError({
66 code: "auth_invalid",
67 reason: "invalid_credential",
68 dpopFailureReason: "key_mismatch",
69 traceId: "trace-1",
70 }),
71 );
72
73 expect(mapped.message).toBe(`The environment credential is invalid. ${DPOP_RETRY_HINT}`);
74 });
75});