@@ -5,6 +5,7 @@ import type {
55 RelayEnvironmentStatusResponse ,
66} from "@t3tools/contracts/relay" ;
77import { describe , expect , it } from "@effect/vitest" ;
8+ import * as Cause from "effect/Cause" ;
89import * as Effect from "effect/Effect" ;
910import * as Fiber from "effect/Fiber" ;
1011import * as Layer from "effect/Layer" ;
@@ -17,6 +18,11 @@ import {
1718 createManagedRelayQueryManager ,
1819 createManagedRelaySession ,
1920 managedRelayAccountChanges ,
21+ ManagedRelaySessionUnavailableError ,
22+ ManagedRelayStatusEndpointMismatchError ,
23+ ManagedRelayStatusEnvironmentMismatchError ,
24+ ManagedRelayTokenReadError ,
25+ ManagedRelayTokenUnavailableError ,
2026 type ManagedRelayQueryEvent ,
2127 managedRelaySessionAtom ,
2228 readManagedRelaySnapshotState ,
@@ -109,6 +115,65 @@ function clerkToken(expiresAtSeconds: number): string {
109115describe ( "createManagedRelayQueryManager" , ( ) => {
110116 afterEach ( resetRegistry ) ;
111117
118+ it ( "redacts endpoint mismatch secrets while retaining correlation fields" , ( ) => {
119+ const expectedHttpBaseUrl =
120+ "https://expected-user:[email protected] /private/catalog?token=expected-secret#expected-fragment" ; 121+ const expectedWsBaseUrl =
122+ "wss://expected-user:[email protected] /private/socket?token=expected-secret#expected-fragment" ; 123+ const actualHttpBaseUrl =
124+ "https://actual-user:[email protected] /private/catalog?token=actual-secret#actual-fragment" ; 125+ const actualWsBaseUrl =
126+ "wss://actual-user:[email protected] /private/socket?token=actual-secret#actual-fragment" ; 127+
128+ const error = ManagedRelayStatusEndpointMismatchError . fromEndpoints ( {
129+ environmentId : environment . environmentId ,
130+ expectedEndpoint : {
131+ providerKind : "cloudflare_tunnel" ,
132+ httpBaseUrl : expectedHttpBaseUrl ,
133+ wsBaseUrl : expectedWsBaseUrl ,
134+ } ,
135+ actualEndpoint : {
136+ providerKind : "cloudflare_tunnel" ,
137+ httpBaseUrl : actualHttpBaseUrl ,
138+ wsBaseUrl : actualWsBaseUrl ,
139+ } ,
140+ } ) ;
141+
142+ expect ( error ) . toMatchObject ( {
143+ expectedProviderKind : "cloudflare_tunnel" ,
144+ expectedHttpBaseUrlInputLength : expectedHttpBaseUrl . length ,
145+ expectedHttpBaseUrlProtocol : "https:" ,
146+ expectedHttpBaseUrlHostname : "expected.example.test" ,
147+ expectedWsBaseUrlInputLength : expectedWsBaseUrl . length ,
148+ expectedWsBaseUrlProtocol : "wss:" ,
149+ expectedWsBaseUrlHostname : "expected.example.test" ,
150+ actualProviderKind : "cloudflare_tunnel" ,
151+ actualHttpBaseUrlInputLength : actualHttpBaseUrl . length ,
152+ actualHttpBaseUrlProtocol : "https:" ,
153+ actualHttpBaseUrlHostname : "actual.example.test" ,
154+ actualWsBaseUrlInputLength : actualWsBaseUrl . length ,
155+ actualWsBaseUrlProtocol : "wss:" ,
156+ actualWsBaseUrlHostname : "actual.example.test" ,
157+ } ) ;
158+ expect ( error ) . not . toHaveProperty ( "expectedEndpoint" ) ;
159+ expect ( error ) . not . toHaveProperty ( "actualEndpoint" ) ;
160+ const exposedText = `${ Object . values ( error ) . join ( " " ) } ${ error . message } ` ;
161+ for ( const secret of [
162+ "expected-user" ,
163+ "expected-password" ,
164+ "/private/catalog" ,
165+ "/private/socket" ,
166+ "expected-secret" ,
167+ "expected-fragment" ,
168+ "actual-user" ,
169+ "actual-password" ,
170+ "actual-secret" ,
171+ "actual-fragment" ,
172+ ] ) {
173+ expect ( exposedText ) . not . toContain ( secret ) ;
174+ }
175+ } ) ;
176+
112177 it . effect ( "waits for the current cloud session before reading its token" , ( ) =>
113178 Effect . gen ( function * ( ) {
114179 const tokenFiber = yield * waitForManagedRelayClerkToken ( registry ) . pipe ( Effect . forkChild ) ;
@@ -148,6 +213,62 @@ describe("createManagedRelayQueryManager", () => {
148213 } ) ,
149214 ) ;
150215
216+ it . effect ( "preserves token provider failure context" , ( ) =>
217+ Effect . gen ( function * ( ) {
218+ const cause = new Error ( "Clerk session failed" ) ;
219+ const session = createManagedRelaySession ( {
220+ accountId : "account-1" ,
221+ readClerkToken : ( ) => Promise . reject ( cause ) ,
222+ } ) ;
223+
224+ const error = yield * Effect . flip ( session . readClerkToken ( ) ) ;
225+
226+ expect ( error ) . toBeInstanceOf ( ManagedRelayTokenReadError ) ;
227+ expect ( error ) . toMatchObject ( {
228+ _tag : "ManagedRelayTokenReadError" ,
229+ accountId : "account-1" ,
230+ cause,
231+ } ) ;
232+ } ) ,
233+ ) ;
234+
235+ it . effect ( "distinguishes unavailable tokens from unavailable sessions" , ( ) =>
236+ Effect . gen ( function * ( ) {
237+ setManagedRelaySession ( registry , {
238+ accountId : "account-1" ,
239+ readClerkToken : ( ) => Promise . resolve ( null ) ,
240+ } ) ;
241+
242+ const tokenError = yield * Effect . flip ( waitForManagedRelayClerkToken ( registry ) ) ;
243+ expect ( tokenError ) . toBeInstanceOf ( ManagedRelayTokenUnavailableError ) ;
244+ expect ( tokenError ) . toMatchObject ( {
245+ _tag : "ManagedRelayTokenUnavailableError" ,
246+ accountId : "account-1" ,
247+ } ) ;
248+
249+ setManagedRelaySession ( registry , null ) ;
250+ const manager = createManager ( ) ;
251+ const atom = manager . environmentsAtom ( "account-1" ) ;
252+ registry . get ( atom ) ;
253+
254+ yield * Effect . promise ( ( ) =>
255+ vi . waitFor ( ( ) => {
256+ const result = registry . get ( atom ) ;
257+ expect ( result . _tag ) . toBe ( "Failure" ) ;
258+ if ( result . _tag !== "Failure" ) {
259+ return ;
260+ }
261+ const error = Cause . squash ( result . cause ) ;
262+ expect ( error ) . toBeInstanceOf ( ManagedRelaySessionUnavailableError ) ;
263+ expect ( error ) . toMatchObject ( {
264+ _tag : "ManagedRelaySessionUnavailableError" ,
265+ requestedAccountId : "account-1" ,
266+ } ) ;
267+ } ) ,
268+ ) ;
269+ } ) ,
270+ ) ;
271+
151272 it . effect ( "updates the token provider without replacing a same-account session" , ( ) =>
152273 Effect . gen ( function * ( ) {
153274 const firstRead = vi . fn ( ( ) => Promise . resolve < string | null > ( null ) ) ;
@@ -197,30 +318,38 @@ describe("createManagedRelayQueryManager", () => {
197318 } ) ,
198319 ) ;
199320
200- it ( "emits credential changes only when the managed relay account changes" , async ( ) => {
201- setManagedRelaySession ( registry , {
202- accountId : "account-1" ,
203- readClerkToken : ( ) => Promise . resolve ( "first-token" ) ,
204- } ) ;
205- const changes = Effect . runPromise (
206- managedRelayAccountChanges ( registry ) . pipe ( Stream . take ( 2 ) , Stream . runCollect ) ,
207- ) ;
208- await vi . waitFor ( ( ) => {
209- expect ( registry . getNodes ( ) . get ( managedRelaySessionAtom ) ?. listeners . size ) . toBeGreaterThan ( 0 ) ;
210- } ) ;
321+ it . effect ( "emits credential changes only when the managed relay account changes" , ( ) =>
322+ Effect . gen ( function * ( ) {
323+ setManagedRelaySession ( registry , {
324+ accountId : "account-1" ,
325+ readClerkToken : ( ) => Promise . resolve ( "first-token" ) ,
326+ } ) ;
327+ const changes = yield * managedRelayAccountChanges ( registry ) . pipe (
328+ Stream . take ( 2 ) ,
329+ Stream . runCollect ,
330+ Effect . forkChild ,
331+ ) ;
332+ yield * Effect . promise ( ( ) =>
333+ vi . waitFor ( ( ) => {
334+ expect ( registry . getNodes ( ) . get ( managedRelaySessionAtom ) ?. listeners . size ) . toBeGreaterThan (
335+ 0 ,
336+ ) ;
337+ } ) ,
338+ ) ;
211339
212- setManagedRelaySession ( registry , {
213- accountId : "account-1" ,
214- readClerkToken : ( ) => Promise . resolve ( "refreshed-token" ) ,
215- } ) ;
216- setManagedRelaySession ( registry , {
217- accountId : "account-2" ,
218- readClerkToken : ( ) => Promise . resolve ( "second-token" ) ,
219- } ) ;
220- setManagedRelaySession ( registry , null ) ;
340+ setManagedRelaySession ( registry , {
341+ accountId : "account-1" ,
342+ readClerkToken : ( ) => Promise . resolve ( "refreshed-token" ) ,
343+ } ) ;
344+ setManagedRelaySession ( registry , {
345+ accountId : "account-2" ,
346+ readClerkToken : ( ) => Promise . resolve ( "second-token" ) ,
347+ } ) ;
348+ setManagedRelaySession ( registry , null ) ;
221349
222- expect ( Array . from ( await changes ) ) . toEqual ( [ "account-2" , null ] ) ;
223- } ) ;
350+ expect ( Array . from ( yield * Fiber . join ( changes ) ) ) . toEqual ( [ "account-2" , null ] ) ;
351+ } ) ,
352+ ) ;
224353
225354 it ( "shares one Clerk token read across concurrent relay list and status queries" , async ( ) => {
226355 const secondEnvironment = {
@@ -349,8 +478,20 @@ describe("createManagedRelayQueryManager", () => {
349478
350479 registry . get ( atom ) ;
351480 await vi . waitFor ( ( ) => {
352- expect ( readManagedRelaySnapshotState ( registry . get ( atom ) ) . error ) . toBe (
353- "Relay returned status for a different environment." ,
481+ const result = registry . get ( atom ) ;
482+ expect ( result . _tag ) . toBe ( "Failure" ) ;
483+ if ( result . _tag !== "Failure" ) {
484+ return ;
485+ }
486+ const error = Cause . squash ( result . cause ) ;
487+ expect ( error ) . toBeInstanceOf ( ManagedRelayStatusEnvironmentMismatchError ) ;
488+ expect ( error ) . toMatchObject ( {
489+ _tag : "ManagedRelayStatusEnvironmentMismatchError" ,
490+ expectedEnvironmentId : environment . environmentId ,
491+ actualEnvironmentId : mismatchedStatus . environmentId ,
492+ } ) ;
493+ expect ( readManagedRelaySnapshotState ( result ) . error ) . toBe (
494+ "Relay returned status for environment environment-2 instead of environment-1." ,
354495 ) ;
355496 } ) ;
356497 } ) ;
0 commit comments