@@ -571,7 +571,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
571571 const snapshot = excludedEnvironmentIds
572572 ? { ...remoteSnapshot , excludedEnvironmentIds }
573573 : remoteSnapshot ;
574- return ordinal === 2 && kind === blockedRead
574+ return ordinal === 1 && kind === blockedRead
575575 ? Deferred . succeed ( readStarted , undefined ) . pipe (
576576 Effect . andThen ( Deferred . await ( finishRead ) ) ,
577577 Effect . as ( snapshot ) ,
@@ -592,20 +592,9 @@ describe("makeRelayDeviceRegistrationRequest", () => {
592592 isReady : true ,
593593 entries : new Map ( ) ,
594594 } ) ;
595- expect ( backgroundRuntime . pending ) . toHaveLength ( 1 ) ;
596- const refresh = backgroundRuntime . pending . shift ( ) ;
597- if ( ! refresh ) throw new Error ( "Expected scope-only widget refresh" ) ;
598- const exit = yield * Effect . exit (
599- refresh . operation as Effect . Effect < unknown , unknown , ManagedRelay . ManagedRelayClient > ,
600- ) . pipe ( Effect . provide ( layer ) ) ;
601- refresh . resolve ( exit ) ;
602- expect ( Exit . isSuccess ( exit ) ) . toBe ( true ) ;
603- expect ( publishAgentActivityWidget ) . toHaveBeenLastCalledWith (
604- expect . objectContaining ( {
605- activeCount : 1 ,
606- activities : remoteSnapshot . aggregate ! . activities ,
607- } ) ,
608- ) ;
595+ // The in-flight read rechecks the scope before publishing; no second
596+ // foreground reconciliation is needed before that first response.
597+ expect ( backgroundRuntime . pending ) . toHaveLength ( 0 ) ;
609598 } else if ( transition === "background" ) {
610599 appStateMock . currentState = "background" ;
611600 for ( const listener of appStateMock . listeners ) listener ( "background" ) ;
@@ -622,8 +611,8 @@ describe("makeRelayDeviceRegistrationRequest", () => {
622611 yield * Fiber . join ( prime ) ;
623612 if ( transition === "scope change" ) {
624613 expect ( { globalReads, scopedReads } ) . toEqual ( {
625- globalReads : 3 ,
626- scopedReads : blockedRead === "global" ? 1 : 2 ,
614+ globalReads : 1 ,
615+ scopedReads : 1 ,
627616 } ) ;
628617 expect ( backgroundRuntime . pending ) . toHaveLength ( 0 ) ;
629618 expect ( widgetMocks . start ) . toHaveBeenCalledExactlyOnceWith (
@@ -660,7 +649,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
660649 expect ( activity . getPushToken ) . toHaveBeenCalledTimes ( 1 ) ;
661650 expect ( backgroundRuntime . pending ) . toHaveLength ( 0 ) ;
662651 } else {
663- expect ( globalReads ) . toBe ( 2 ) ;
652+ expect ( globalReads ) . toBe ( 1 ) ;
664653 expect ( widgetMocks . start ) . not . toHaveBeenCalled ( ) ;
665654 expect ( activity . getPushToken ) . not . toHaveBeenCalled ( ) ;
666655 expect ( widgetBeforeRelease ?. activities ) . toEqual ( [ ] ) ;
@@ -739,7 +728,9 @@ describe("makeRelayDeviceRegistrationRequest", () => {
739728 } ) ,
740729 expect . any ( Date ) ,
741730 ) ;
742- expect ( publishAgentActivityWidget ) . not . toHaveBeenCalled ( ) ;
731+ expect ( publishAgentActivityWidget ) . toHaveBeenCalledExactlyOnceWith (
732+ expect . objectContaining ( { activeCount : 0 , activities : [ ] } ) ,
733+ ) ;
743734 } ) ;
744735
745736 it ( "preserves disabled Live Activity preferences in relay registrations" , ( ) => {
@@ -1752,7 +1743,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
17521743 yield * runBackgroundOperations ( ) ;
17531744
17541745 expect ( widgetMocks . start ) . not . toHaveBeenCalled ( ) ;
1755- expect ( publishAgentActivityWidget ) . toHaveBeenCalledTimes ( 1 ) ;
1746+ expect ( publishAgentActivityWidget ) . toHaveBeenCalledTimes ( 2 ) ;
17561747 expect ( publishAgentActivityWidget ) . toHaveBeenLastCalledWith (
17571748 expect . objectContaining ( {
17581749 activeCount : 1 ,
@@ -1786,7 +1777,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
17861777 yield * runBackgroundOperations ( ) ;
17871778
17881779 expect ( widgetMocks . start ) . not . toHaveBeenCalled ( ) ;
1789- expect ( publishAgentActivityWidget ) . toHaveBeenCalledTimes ( 1 ) ;
1780+ expect ( publishAgentActivityWidget ) . toHaveBeenCalledTimes ( 2 ) ;
17901781 expect ( publishAgentActivityWidget ) . toHaveBeenLastCalledWith (
17911782 expect . objectContaining ( {
17921783 activeCount : 1 ,
@@ -1868,7 +1859,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
18681859 yield * Deferred . succeed ( finishFirstRead , undefined ) ;
18691860 yield * Fiber . join ( olderRefresh ) ;
18701861
1871- expect ( publishAgentActivityWidget ) . toHaveBeenCalledTimes ( 1 ) ;
1862+ expect ( publishAgentActivityWidget ) . toHaveBeenCalledTimes ( 2 ) ;
18721863 expect ( publishAgentActivityWidget ) . toHaveBeenLastCalledWith (
18731864 expect . objectContaining ( {
18741865 activities : [ expect . objectContaining ( { status : "Working" } ) ] ,
@@ -1929,7 +1920,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
19291920 } ) ,
19301921 expect . any ( Date ) ,
19311922 ) ;
1932- expect ( publishAgentActivityWidget ) . toHaveBeenCalledTimes ( 1 ) ;
1923+ expect ( publishAgentActivityWidget ) . toHaveBeenCalledTimes ( 2 ) ;
19331924 expect ( publishAgentActivityWidget ) . toHaveBeenLastCalledWith (
19341925 expect . objectContaining ( {
19351926 activeCount : 0 ,
@@ -1983,7 +1974,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
19831974 const snapshot = excludedEnvironmentIds
19841975 ? { aggregate : null , excludedEnvironmentIds }
19851976 : activeAgentActivitySnapshot ;
1986- return ordinal === 2 && kind === blockedRead
1977+ return ordinal === 1 && kind === blockedRead
19871978 ? Deferred . succeed ( readStarted , undefined ) . pipe (
19881979 Effect . andThen ( Deferred . await ( finishRead ) ) ,
19891980 Effect . as ( snapshot ) ,
@@ -2016,7 +2007,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
20162007 const readsBeforeTokenResolved = { global : globalReads , scoped : scopedReads } ;
20172008 finishToken ( "activity-token" ) ;
20182009 pending . resolve ( yield * Fiber . join ( localRegistration ) ) ;
2019- expect ( readsBeforeTokenResolved ) . toEqual ( { global : 2 , scoped : 2 } ) ;
2010+ expect ( readsBeforeTokenResolved ) . toEqual ( { global : 1 , scoped : 1 } ) ;
20202011 expect ( startsBeforeTokenResolved ) . toBe ( 1 ) ;
20212012 expect ( activities ) . toHaveLength ( 1 ) ;
20222013 } ) . pipe ( Effect . scoped ) ,
@@ -2073,11 +2064,9 @@ describe("makeRelayDeviceRegistrationRequest", () => {
20732064 preferencesStarted ( ) ;
20742065 return preferences ;
20752066 } ) ;
2076- let readCount = 0 ;
2077- const layer = snapshotRelayLayer ( ( ) => {
2078- readCount ++ ;
2079- return Effect . succeed ( readCount === 1 ? activeAgentActivitySnapshot : { aggregate : null } ) ;
2080- } ) ;
2067+ let snapshot : RelayAgentActivitySnapshotResponse = activeAgentActivitySnapshot ;
2068+ const readSnapshot = vi . fn ( ( ) => Effect . succeed ( snapshot ) ) ;
2069+ const layer = snapshotRelayLayer ( readSnapshot ) ;
20812070 setAgentAwarenessRelayTokenProvider ( ( ) => Promise . resolve ( "clerk-token-user-a" ) ) ;
20822071
20832072 const refresh = yield * refreshActiveLiveActivityRemoteRegistration ( ) . pipe (
@@ -2086,10 +2075,12 @@ describe("makeRelayDeviceRegistrationRequest", () => {
20862075 ) ;
20872076 yield * Effect . promise ( ( ) => started ) ;
20882077
2078+ expect ( readSnapshot ) . not . toHaveBeenCalled ( ) ;
2079+ snapshot = { aggregate : null } ;
20892080 finishPreferences ( { liveActivitiesEnabled : true } as Preferences ) ;
20902081 yield * Fiber . join ( refresh ) ;
20912082
2092- expect ( readCount ) . toBe ( 2 ) ;
2083+ expect ( readSnapshot ) . toHaveBeenCalledTimes ( 1 ) ;
20932084 expect ( widgetMocks . start ) . not . toHaveBeenCalled ( ) ;
20942085 expect ( publishAgentActivityWidget ) . toHaveBeenLastCalledWith (
20952086 expect . objectContaining ( {
@@ -2369,7 +2360,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
23692360 ) ;
23702361 } ) ;
23712362 yield * refreshActiveLiveActivityRemoteRegistration ( ) . pipe ( Effect . provide ( layer ) ) ;
2372- expect ( queries ) . toEqual ( [ undefined , [ liveEnvironmentId ] , undefined , [ liveEnvironmentId ] ] ) ;
2363+ expect ( queries ) . toEqual ( [ undefined , [ liveEnvironmentId ] ] ) ;
23732364 expect ( widgetMocks . start ) . toHaveBeenCalledWith (
23742365 expect . objectContaining ( {
23752366 activities : activeAgentActivitySnapshot . aggregate . activities ,
@@ -2547,6 +2538,43 @@ describe("makeRelayDeviceRegistrationRequest", () => {
25472538 } ) ,
25482539 ) ;
25492540
2541+ it . effect . each ( [ "account switch" , "provider remount" ] as const ) (
2542+ "clears the prior account's native widget on %s even when the relay is unavailable" ,
2543+ ( transition ) =>
2544+ Effect . gen ( function * ( ) {
2545+ setAgentAwarenessRelayTokenProvider ( ( ) => Promise . resolve ( "token-a" ) , "user-a" ) ;
2546+ backgroundRuntime . pending . length = 0 ;
2547+ yield * refreshActiveLiveActivityRemoteRegistration ( ) . pipe (
2548+ Effect . provide ( snapshotRelayLayer ( ) ) ,
2549+ ) ;
2550+ expect ( publishAgentActivityWidget ) . toHaveBeenLastCalledWith (
2551+ expect . objectContaining ( {
2552+ activeCount : 1 ,
2553+ activities : activeAgentActivitySnapshot . aggregate . activities ,
2554+ } ) ,
2555+ ) ;
2556+ if ( transition === "provider remount" ) releaseAgentAwarenessRelayTokenProvider ( ) ;
2557+ setAgentAwarenessRelayTokenProvider ( ( ) => Promise . resolve ( "token-b" ) , "user-b" ) ;
2558+ backgroundRuntime . pending . length = 0 ;
2559+ yield * refreshActiveLiveActivityRemoteRegistration ( ) . pipe (
2560+ Effect . provide (
2561+ snapshotRelayLayer ( ( ) =>
2562+ Effect . fail (
2563+ new ManagedRelay . ManagedRelayRequestFailedError ( {
2564+ action : "read relay agent activity snapshot" ,
2565+ transportFailed : true ,
2566+ cause : new Error ( "offline" ) ,
2567+ } ) ,
2568+ ) ,
2569+ ) ,
2570+ ) ,
2571+ ) ;
2572+ expect ( publishAgentActivityWidget ) . toHaveBeenLastCalledWith (
2573+ expect . objectContaining ( { activeCount : 0 , activities : [ ] } ) ,
2574+ ) ;
2575+ } ) ,
2576+ ) ;
2577+
25502578 it . effect ( "publishes freshness changes even when rows and count are unchanged" , ( ) =>
25512579 Effect . gen ( function * ( ) {
25522580 addLiveEnvironment ( ) ;
0 commit comments