Open History to see the last commit
1import {
2 RelayAuthInvalidError,
3 RelayEnvironmentLinkUnavailableError,
4} from "@t3tools/contracts/relay";
5import { describe, expect, it } from "@effect/vitest";
6import * as Schema from "effect/Schema";
7
8import {
9 DPOP_CLOCK_HINT,
10 DPOP_RETRY_HINT,
11 DPOP_UNKNOWN_HINT,
12 relayProtectedErrorMessage,
13} from "./errorPresentation.ts";
14
15const decodeLinkUnavailableError = Schema.decodeUnknownSync(RelayEnvironmentLinkUnavailableError);
16
17describe("relayProtectedErrorMessage", () => {
18 it("decodes older relay failures and preserves their reason-only message", () => {
19 const error = decodeLinkUnavailableError({
20 _tag: "RelayEnvironmentLinkUnavailableError",
21 code: "environment_link_unavailable",
22 reason: "managed_endpoint_provisioning_failed",
23 traceId: "trace-1",
24 });
25
26 expect(error.traceId).toBe("trace-1");
27 expect(relayProtectedErrorMessage(error)).toBe(
28 "Relay cannot provision the managed endpoint (managed_endpoint_provisioning_failed).",
29 );
30 });
31
32 it("presents clock skew as one possible cause when the relay omits the reason", () => {
33 const error = new RelayAuthInvalidError({
34 code: "auth_invalid",
35 reason: "invalid_dpop",
36 traceId: "trace-1",
37 });
38
39 expect(relayProtectedErrorMessage(error)).toBe(
40 `Relay rejected the DPoP proof. ${DPOP_UNKNOWN_HINT}`,
41 );
42 });
43
44 it("keeps the clock hint for a relay that confirms a time-window failure", () => {
45 const error = new RelayAuthInvalidError({
46 code: "auth_invalid",
47 reason: "invalid_dpop",
48 dpopFailureReason: "time_window",
49 traceId: "trace-1",
50 });
51
52 expect(relayProtectedErrorMessage(error)).toContain(DPOP_CLOCK_HINT);
53 });
54
55 it("does not blame the clock when the relay identifies another proof failure", () => {
56 const error = new RelayAuthInvalidError({
57 code: "auth_invalid",
58 reason: "invalid_dpop",
59 dpopFailureReason: "key_mismatch",
60 traceId: "trace-1",
61 });
62
63 expect(relayProtectedErrorMessage(error)).toBe(
64 `Relay rejected the DPoP proof. ${DPOP_RETRY_HINT}`,
65 );
66 });
67
68 it("preserves the existing message for other authentication failures", () => {
69 const error = new RelayAuthInvalidError({
70 code: "auth_invalid",
71 reason: "invalid_bearer",
72 traceId: "trace-1",
73 });
74
75 expect(relayProtectedErrorMessage(error)).toBe("Relay rejected the cloud session token.");
76 });
77});