@@ -8,7 +8,7 @@ const mockDbChain = {
88 where : jest . fn ( ( ) => mockDbChain ) ,
99 whereIn : jest . fn ( ( ) => mockDbChain ) ,
1010 whereNotNull : jest . fn ( ( ) => mockDbChain ) ,
11- select : jest . fn ( ( ) => mockDbChain ) ,
11+ select : jest . fn ( ) . mockImplementation ( async ( ) => mockVaultMilestones ) ,
1212}
1313
1414jest . unstable_mockModule ( '../db/index.js' , ( ) => ( {
@@ -29,7 +29,6 @@ describe('sendMilestoneReminders', () => {
2929 beforeEach ( ( ) => {
3030 jest . clearAllMocks ( )
3131 mockVaultMilestones = [ ]
32- ; ( mockDbChain . select as jest . Mock ) . mockResolvedValue ( mockVaultMilestones )
3332 } )
3433
3534 it ( 'sends a reminder for a milestone within lead time' , async ( ) => {
@@ -96,18 +95,21 @@ describe('sendMilestoneReminders', () => {
9695 ]
9796
9897 // First call
99- await sendMilestoneReminders ( {
98+ const result1 = await sendMilestoneReminders ( {
10099 now,
101100 leadTimesMs : [ 1 * 60 * 60 * 1000 ] ,
102101 } )
102+ expect ( result1 ) . toBe ( 1 )
103103 expect ( mockCreateNotification ) . toHaveBeenCalledTimes ( 1 )
104104
105- // Second call (should be deduplicated)
106- await sendMilestoneReminders ( {
105+ // Second call (simulating idempotency collision in DB)
106+ mockCreateNotification . mockRejectedValueOnce ( new Error ( 'Duplicate key value violates unique constraint' ) )
107+ const result2 = await sendMilestoneReminders ( {
107108 now,
108109 leadTimesMs : [ 1 * 60 * 60 * 1000 ] ,
109110 } )
110- expect ( mockCreateNotification ) . toHaveBeenCalledTimes ( 1 ) // Still 1
111+ expect ( result2 ) . toBe ( 0 )
112+ expect ( mockCreateNotification ) . toHaveBeenCalledTimes ( 2 )
111113 } )
112114
113115 it ( 'skips milestones that are not pending' , async ( ) => {
0 commit comments