@@ -21,7 +21,10 @@ import { executeSearchTool } from '@/lib/internal/search/execute-tool'
2121import { applyScheduleRunFailureUpdate } from '@/background/schedule-execution'
2222
2323const { stripeStub, mockSendEmail, mockNotifyScheduleAutoDisabled } = vi . hoisted ( ( ) => ( {
24- stripeStub : { subscriptions : { update : vi . fn ( ) } , invoices : { createPreview : vi . fn ( ) } } ,
24+ stripeStub : {
25+ subscriptions : { update : vi . fn ( ) , retrieve : vi . fn ( ) } ,
26+ invoices : { createPreview : vi . fn ( ) } ,
27+ } ,
2528 mockSendEmail : vi . fn ( ) ,
2629 mockNotifyScheduleAutoDisabled : vi . fn ( ) ,
2730} ) )
@@ -110,15 +113,21 @@ function search(userId: string, workspaceId: string) {
110113 } )
111114}
112115
116+ /** The subscriptions as Stripe holds them now, which the reminder reads back. */
117+ const liveSubscriptions = new Map < string , TrialEndingSubscription > ( )
118+
119+ /** A `trial_will_end` payload, which Stripe also holds as the live subscription. */
113120function trialWillEnd ( stripeSubscriptionId : string , status = 'trialing' ) : TrialEndingSubscription {
114- return {
121+ const subscription = {
115122 id : stripeSubscriptionId ,
116123 status,
117124 trial_end : Math . floor ( ( Date . now ( ) + 3 * DAY_MS ) / 1000 ) ,
118125 items : {
119126 data : [ { quantity : 1 , price : { unit_amount : 2500 , recurring : { interval : 'month' } } } ] ,
120127 } ,
121128 }
129+ liveSubscriptions . set ( stripeSubscriptionId , subscription )
130+ return subscription
122131}
123132
124133async function addClaimedSchedule ( ) {
@@ -152,6 +161,10 @@ beforeEach(() => {
152161 mockSendEmail . mockResolvedValue ( { success : true } )
153162 stripeStub . invoices . createPreview . mockReset ( )
154163 stripeStub . invoices . createPreview . mockResolvedValue ( { amount_due : 2500 } )
164+ stripeStub . subscriptions . retrieve . mockReset ( )
165+ stripeStub . subscriptions . retrieve . mockImplementation ( async ( id : string ) =>
166+ liveSubscriptions . get ( id )
167+ )
155168 mockNotifyScheduleAutoDisabled . mockReset ( )
156169} )
157170
@@ -280,6 +293,32 @@ describe('handleTrialWillEnd', () => {
280293
281294 expect ( mockSendEmail ) . not . toHaveBeenCalled ( )
282295 } )
296+
297+ it ( 'sends nothing when the trial ended before the reminder arrived' , async ( ) => {
298+ const userId = await createUser ( )
299+ const stripeSubscriptionId = await addSubscription ( userId , 'trialing' )
300+ const event = trialWillEnd ( stripeSubscriptionId )
301+ // A card that already had a trial ends the new one at once; the event still says trialing.
302+ liveSubscriptions . set ( stripeSubscriptionId , { ...event , status : 'past_due' , trial_end : null } )
303+
304+ await handleTrialWillEnd ( event )
305+
306+ expect ( mockSendEmail ) . not . toHaveBeenCalled ( )
307+ } )
308+ it ( 'sends nothing for a reminder about an end date the trial no longer has' , async ( ) => {
309+ const userId = await createUser ( )
310+ const stripeSubscriptionId = await addSubscription ( userId , 'trialing' )
311+ const event = trialWillEnd ( stripeSubscriptionId )
312+ // Still trialing, but its end moved after this event was sent.
313+ liveSubscriptions . set ( stripeSubscriptionId , {
314+ ...event ,
315+ trial_end : ( event . trial_end ?? 0 ) + 7 * 86_400 ,
316+ } )
317+
318+ await handleTrialWillEnd ( event )
319+
320+ expect ( mockSendEmail ) . not . toHaveBeenCalled ( )
321+ } )
283322} )
284323
285324describe ( 'endProTrial' , ( ) => {
0 commit comments