@@ -391,26 +391,75 @@ describe("makeRelayDeviceRegistrationRequest", () => {
391391 ) . toEqual ( { liveActivitiesEnabled : true , baseFontSize : 18 } ) ;
392392 } ) ;
393393
394- it . effect ( "registers at most one listener while a Live Activity push token is pending " , ( ) => {
394+ it . effect ( "registers one push-token listener per Live Activity across wrappers " , ( ) => {
395395 registerAgentAwarenessConnection ( savedConnection ( ) ) ;
396396 const addPushTokenListener = vi . fn ( ) ;
397- const activity = {
397+ // getInstances() returns a new wrapper for the same native activity each call.
398+ const wrapper = ( ) => ( {
399+ getId : ( ) => "activity-1" ,
398400 getPushToken : vi . fn ( ( ) => Promise . resolve ( null ) ) ,
399401 addPushTokenListener,
400- } ;
402+ } ) ;
401403
402404 return Effect . gen ( function * ( ) {
403- expect ( yield * registerLiveActivityPushToken ( { activity : activity as never } ) ) . toBe ( false ) ;
404- expect ( yield * registerLiveActivityPushToken ( { activity : activity as never } ) ) . toBe ( false ) ;
405+ expect ( yield * registerLiveActivityPushToken ( { activity : wrapper ( ) as never } ) ) . toBe ( false ) ;
406+ expect ( yield * registerLiveActivityPushToken ( { activity : wrapper ( ) as never } ) ) . toBe ( false ) ;
405407
406- expect ( activity . getPushToken ) . toHaveBeenCalledTimes ( 2 ) ;
407408 expect ( addPushTokenListener ) . toHaveBeenCalledTimes ( 1 ) ;
408409 } ) . pipe ( Effect . provide ( layerRelayTest ) ) ;
409410 } ) ;
410411
412+ it . effect ( "registers a Live Activity push token that rotates after registration" , ( ) => {
413+ const registeredTokens : Array < string > = [ ] ;
414+ vi . stubGlobal ( "fetch" , async ( request : RequestInfo | URL , init ?: RequestInit ) => {
415+ const url = request instanceof Request ? request . url : String ( request ) ;
416+ if ( url . endsWith ( "/v1/client/dpop-token" ) ) {
417+ return Response . json ( {
418+ access_token : "relay-dpop-token" ,
419+ issued_token_type : "urn:ietf:params:oauth:token-type:access_token" ,
420+ token_type : "DPoP" ,
421+ expires_in : 300 ,
422+ scope : "mobile:registration" ,
423+ } ) ;
424+ }
425+ if ( url . endsWith ( "/v1/mobile/live-activities" ) ) {
426+ const body = request instanceof Request ? await request . text ( ) : String ( init ?. body ) ;
427+ registeredTokens . push (
428+ ( JSON . parse ( body ) as { readonly activityPushToken : string } ) . activityPushToken ,
429+ ) ;
430+ }
431+ return Response . json ( { ok : true } ) ;
432+ } ) ;
433+ Constants . expoConfig ! . extra = { relay : { url : "https://relay.example.test/" } } ;
434+ setAgentAwarenessRelayTokenProvider ( ( ) => Promise . resolve ( "clerk-token-user-a" ) ) ;
435+ let tokenListener : ( ( event : { readonly pushToken : string } ) => void ) | undefined ;
436+ const activity = {
437+ getId : ( ) => "activity-1" ,
438+ getPushToken : vi . fn ( ( ) => Promise . resolve ( "activity-token" ) ) ,
439+ addPushTokenListener : vi . fn ( ( listener : typeof tokenListener ) => {
440+ tokenListener = listener ;
441+ return { remove : vi . fn ( ) } ;
442+ } ) ,
443+ } ;
444+
445+ return Effect . gen ( function * ( ) {
446+ yield * runBackgroundOperations ( ) ;
447+ expect ( yield * registerLiveActivityPushToken ( { activity : activity as never } ) ) . toBe ( true ) ;
448+
449+ tokenListener ?.( { pushToken : "rotated-activity-token" } ) ;
450+ yield * runBackgroundOperations ( ) ;
451+
452+ expect ( registeredTokens ) . toEqual ( [ "activity-token" , "rotated-activity-token" ] ) ;
453+ } ) . pipe (
454+ Effect . provideService ( FetchHttpClient . Fetch , globalThis . fetch ) ,
455+ Effect . provide ( layerRelayTest ) ,
456+ ) ;
457+ } ) ;
458+
411459 it . effect ( "preserves Live Activity push-token lookup failures" , ( ) => {
412460 const cause = new Error ( "native token lookup failed" ) ;
413461 const activity = {
462+ getId : ( ) => "activity-1" ,
414463 getPushToken : vi . fn ( ( ) => Promise . reject ( cause ) ) ,
415464 addPushTokenListener : vi . fn ( ) ,
416465 } ;
@@ -435,6 +484,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
435484 ( ) => {
436485 registerAgentAwarenessConnection ( savedConnection ( ) ) ;
437486 const activity = {
487+ getId : ( ) => "activity-1" ,
438488 getPushToken : vi . fn ( ( ) => Promise . resolve ( "activity-token" ) ) ,
439489 addPushTokenListener : vi . fn ( ) ,
440490 } ;
@@ -449,6 +499,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
449499 "registers APNS-started Live Activities for relay updates without mutating them locally" ,
450500 ( ) => {
451501 const activity = {
502+ getId : ( ) => "activity-1" ,
452503 getPushToken : vi . fn ( ( ) => Promise . resolve ( "activity-token" ) ) ,
453504 addPushTokenListener : vi . fn ( ) ,
454505 start : vi . fn ( ) ,
@@ -473,6 +524,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
473524 "re-registers active Live Activity tokens when the app returns to the foreground" ,
474525 ( ) => {
475526 const activity = {
527+ getId : ( ) => "activity-1" ,
476528 getPushToken : vi . fn ( ( ) => Promise . resolve ( "activity-token" ) ) ,
477529 addPushTokenListener : vi . fn ( ) ,
478530 } ;
@@ -502,6 +554,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
502554 it ( "ends local Live Activities and stops foreground reconciliation on cloud sign-out" , ( ) => {
503555 const end = vi . fn ( ( ) => Promise . resolve ( ) ) ;
504556 const activity = {
557+ getId : ( ) => "activity-1" ,
505558 getPushToken : vi . fn ( ( ) => Promise . resolve ( "activity-token" ) ) ,
506559 addPushTokenListener : vi . fn ( ) ,
507560 end,
@@ -613,6 +666,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
613666 it ( "releases the provider without ending activities or clearing the registration" , ( ) => {
614667 const end = vi . fn ( ( ) => Promise . resolve ( ) ) ;
615668 const activity = {
669+ getId : ( ) => "activity-1" ,
616670 getPushToken : vi . fn ( ( ) => Promise . resolve ( "activity-token" ) ) ,
617671 addPushTokenListener : vi . fn ( ) ,
618672 end,
@@ -708,6 +762,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
708762 } ,
709763 } ;
710764 const activity = {
765+ getId : ( ) => "activity-1" ,
711766 getPushToken : vi . fn ( ( ) => Promise . resolve ( "activity-token" ) ) ,
712767 addPushTokenListener : vi . fn ( ) ,
713768 } ;
0 commit comments