@@ -695,6 +695,103 @@ describe('MedplumNotificationBackend', () => {
695695 } ) ;
696696 } ) ;
697697
698+ describe ( 'applyReplicationSnapshotIfNewer' , ( ) => {
699+ it ( 'should skip apply when destination notification is newer' , async ( ) => {
700+ const snapshot = {
701+ id : 'notif-1' ,
702+ userId : 'user-123' ,
703+ notificationType : 'EMAIL' ,
704+ title : 'Incoming' ,
705+ bodyTemplate : 'body' ,
706+ contextName : 'testContext' ,
707+ contextParameters : { param1 : 'value1' } ,
708+ sendAfter : null ,
709+ subjectTemplate : 'subject' ,
710+ status : 'SENT' ,
711+ contextUsed : null ,
712+ extraParams : null ,
713+ adapterUsed : null ,
714+ sentAt : null ,
715+ readAt : null ,
716+ gitCommitSha : null ,
717+ createdAt : new Date ( '2026-02-28T10:00:00.000Z' ) ,
718+ updatedAt : new Date ( '2026-02-28T11:00:00.000Z' ) ,
719+ } ;
720+ const destinationNewer = {
721+ ...snapshot ,
722+ updatedAt : new Date ( '2026-02-28T12:00:00.000Z' ) ,
723+ } ;
724+
725+ jest . spyOn ( backend , 'getNotification' ) . mockResolvedValue (
726+ // biome-ignore lint/suspicious/noExplicitAny: test-only cast
727+ destinationNewer as any ,
728+ ) ;
729+ const persistNotificationUpdateSpy = jest
730+ . spyOn ( backend , 'persistNotificationUpdate' )
731+ . mockResolvedValue (
732+ // biome-ignore lint/suspicious/noExplicitAny: test-only cast
733+ destinationNewer as any ,
734+ ) ;
735+
736+ const result = await backend . applyReplicationSnapshotIfNewer (
737+ // biome-ignore lint/suspicious/noExplicitAny: test-only cast
738+ snapshot as any ,
739+ ) ;
740+
741+ expect ( result ) . toEqual ( { applied : false } ) ;
742+ expect ( persistNotificationUpdateSpy ) . not . toHaveBeenCalled ( ) ;
743+ } ) ;
744+
745+ it ( 'should apply update when destination notification is older' , async ( ) => {
746+ const snapshot = {
747+ id : 'notif-1' ,
748+ userId : 'user-123' ,
749+ notificationType : 'EMAIL' ,
750+ title : 'Incoming title' ,
751+ bodyTemplate : 'body' ,
752+ contextName : 'testContext' ,
753+ contextParameters : { param1 : 'value1' } ,
754+ sendAfter : null ,
755+ subjectTemplate : 'subject' ,
756+ status : 'SENT' ,
757+ contextUsed : null ,
758+ extraParams : null ,
759+ adapterUsed : null ,
760+ sentAt : null ,
761+ readAt : null ,
762+ gitCommitSha : null ,
763+ createdAt : new Date ( '2026-02-28T10:00:00.000Z' ) ,
764+ updatedAt : new Date ( '2026-02-28T11:00:00.000Z' ) ,
765+ } ;
766+ const destinationOlder = {
767+ ...snapshot ,
768+ updatedAt : new Date ( '2026-02-28T09:00:00.000Z' ) ,
769+ } ;
770+
771+ jest . spyOn ( backend , 'getNotification' ) . mockResolvedValue (
772+ // biome-ignore lint/suspicious/noExplicitAny: test-only cast
773+ destinationOlder as any ,
774+ ) ;
775+ const persistNotificationUpdateSpy = jest
776+ . spyOn ( backend , 'persistNotificationUpdate' )
777+ . mockResolvedValue (
778+ // biome-ignore lint/suspicious/noExplicitAny: test-only cast
779+ snapshot as any ,
780+ ) ;
781+
782+ const result = await backend . applyReplicationSnapshotIfNewer (
783+ // biome-ignore lint/suspicious/noExplicitAny: test-only cast
784+ snapshot as any ,
785+ ) ;
786+
787+ expect ( result ) . toEqual ( { applied : true } ) ;
788+ expect ( persistNotificationUpdateSpy ) . toHaveBeenCalledWith (
789+ 'notif-1' ,
790+ expect . objectContaining ( { title : 'Incoming title' } ) ,
791+ ) ;
792+ } ) ;
793+ } ) ;
794+
698795 describe ( 'filterNotifications' , ( ) => {
699796 it ( 'should map sendAfterRange to sent date comparators in FHIR search params' , async ( ) => {
700797 const from = new Date ( '2026-01-01T00:00:00.000Z' ) ;
0 commit comments