@@ -12,12 +12,16 @@ import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
1212import { PRO_TRIAL_DAYS , PRO_TRIAL_USAGE_LIMIT_DOLLARS } from '@/lib/billing/constants'
1313import { syncUsageLimitsFromSubscription , updateUserUsageLimit } from '@/lib/billing/core/usage'
1414import { setUsageLimitForCredits } from '@/lib/billing/credits/purchase'
15- import { enforceOneTrialPerCard , getProTrialCheckoutParams } from '@/lib/billing/trial'
15+ import {
16+ enforceOneTrialPerCard ,
17+ enforceOneTrialPerCardForCheckout ,
18+ getProTrialCheckoutParams ,
19+ } from '@/lib/billing/trial'
1620
1721const { stripeStub } = vi . hoisted ( ( ) => ( {
1822 stripeStub : {
1923 paymentMethods : { retrieve : vi . fn ( ) } ,
20- subscriptions : { update : vi . fn ( ) } ,
24+ subscriptions : { retrieve : vi . fn ( ) , update : vi . fn ( ) } ,
2125 } ,
2226} ) )
2327
@@ -83,6 +87,10 @@ function trialingStripeSubscription(
8387 } as Stripe . Subscription
8488}
8589
90+ function payer ( userId : string ) : { plan : string ; referenceId : string } {
91+ return { plan : PRO_PLAN , referenceId : userId }
92+ }
93+
8694function useCard ( fingerprint : string | null ) : void {
8795 if ( fingerprint ) fingerprints . push ( fingerprint )
8896 stripeStub . paymentMethods . retrieve . mockResolvedValue ( {
@@ -101,6 +109,7 @@ async function readLimit(userId: string): Promise<number> {
101109
102110beforeEach ( ( ) => {
103111 stripeStub . paymentMethods . retrieve . mockReset ( )
112+ stripeStub . subscriptions . retrieve . mockReset ( )
104113 stripeStub . subscriptions . update . mockReset ( )
105114 stripeStub . subscriptions . update . mockResolvedValue ( { } )
106115} )
@@ -202,7 +211,10 @@ describe('enforceOneTrialPerCard', () => {
202211 useCard ( fingerprint )
203212
204213 expect (
205- await enforceOneTrialPerCard ( trialingStripeSubscription ( stripeSubscriptionId , 'pm_1' ) , userId )
214+ await enforceOneTrialPerCard (
215+ trialingStripeSubscription ( stripeSubscriptionId , 'pm_1' ) ,
216+ payer ( userId )
217+ )
206218 ) . toBe ( true )
207219
208220 expect ( stripeStub . subscriptions . update ) . not . toHaveBeenCalled ( )
@@ -221,8 +233,8 @@ describe('enforceOneTrialPerCard', () => {
221233 useCard ( `fp_${ generateId ( ) } ` )
222234 const trial = trialingStripeSubscription ( stripeSubscriptionId , 'pm_1' )
223235
224- expect ( await enforceOneTrialPerCard ( trial , userId ) ) . toBe ( true )
225- expect ( await enforceOneTrialPerCard ( trial , userId ) ) . toBe ( true )
236+ expect ( await enforceOneTrialPerCard ( trial , payer ( userId ) ) ) . toBe ( true )
237+ expect ( await enforceOneTrialPerCard ( trial , payer ( userId ) ) ) . toBe ( true )
226238 expect ( stripeStub . subscriptions . update ) . not . toHaveBeenCalled ( )
227239 } )
228240
@@ -239,7 +251,7 @@ describe('enforceOneTrialPerCard', () => {
239251 expect (
240252 await enforceOneTrialPerCard (
241253 trialingStripeSubscription ( firstSubscriptionId , 'pm_1' ) ,
242- firstUserId
254+ payer ( firstUserId )
243255 )
244256 ) . toBe ( true )
245257 await db . delete ( subscription ) . where ( eq ( subscription . referenceId , firstUserId ) )
@@ -256,7 +268,7 @@ describe('enforceOneTrialPerCard', () => {
256268 expect (
257269 await enforceOneTrialPerCard (
258270 trialingStripeSubscription ( secondSubscriptionId , 'pm_2' ) ,
259- secondUserId
271+ payer ( secondUserId )
260272 )
261273 ) . toBe ( false )
262274 expect ( stripeStub . subscriptions . update ) . toHaveBeenCalledExactlyOnceWith (
@@ -278,7 +290,10 @@ describe('enforceOneTrialPerCard', () => {
278290 } )
279291
280292 expect (
281- await enforceOneTrialPerCard ( trialingStripeSubscription ( stripeSubscriptionId , null ) , userId )
293+ await enforceOneTrialPerCard (
294+ trialingStripeSubscription ( stripeSubscriptionId , null ) ,
295+ payer ( userId )
296+ )
282297 ) . toBe ( false )
283298 expect ( stripeStub . subscriptions . update ) . toHaveBeenCalledOnce ( )
284299 } )
@@ -298,12 +313,18 @@ describe('enforceOneTrialPerCard', () => {
298313
299314 useCard ( firstCard )
300315 expect (
301- await enforceOneTrialPerCard ( trialingStripeSubscription ( firstSubscriptionId , 'pm_1' ) , userId )
316+ await enforceOneTrialPerCard (
317+ trialingStripeSubscription ( firstSubscriptionId , 'pm_1' ) ,
318+ payer ( userId )
319+ )
302320 ) . toBe ( true )
303321
304322 useCard ( `fp_${ generateId ( ) } ` )
305323 expect (
306- await enforceOneTrialPerCard ( trialingStripeSubscription ( racingSubscriptionId , 'pm_2' ) , userId )
324+ await enforceOneTrialPerCard (
325+ trialingStripeSubscription ( racingSubscriptionId , 'pm_2' ) ,
326+ payer ( userId )
327+ )
307328 ) . toBe ( false )
308329 expect ( stripeStub . subscriptions . update ) . toHaveBeenCalledExactlyOnceWith (
309330 racingSubscriptionId ,
@@ -313,25 +334,126 @@ describe('enforceOneTrialPerCard', () => {
313334
314335 useCard ( firstCard )
315336 expect (
316- await enforceOneTrialPerCard ( trialingStripeSubscription ( firstSubscriptionId , 'pm_1' ) , userId )
337+ await enforceOneTrialPerCard (
338+ trialingStripeSubscription ( firstSubscriptionId , 'pm_1' ) ,
339+ payer ( userId )
340+ )
317341 ) . toBe ( true )
318342 expect ( stripeStub . subscriptions . update ) . toHaveBeenCalledOnce ( )
319343 } )
320344
345+ it ( 'keeps exactly one of two checkouts racing on one account' , async ( ) => {
346+ const userId = await createUser ( )
347+ const racing = [ `sub_${ generateId ( ) } ` , `sub_${ generateId ( ) } ` ]
348+ for ( const stripeSubscriptionId of racing ) {
349+ await addSubscription ( userId , {
350+ status : 'trialing' ,
351+ trialStart : new Date ( ) ,
352+ stripeSubscriptionId,
353+ } )
354+ }
355+ const cards = racing . map ( ( ) => `fp_${ generateId ( ) } ` )
356+ fingerprints . push ( ...cards )
357+ stripeStub . paymentMethods . retrieve . mockImplementation ( async ( id : string ) => ( {
358+ id,
359+ card : { fingerprint : cards [ Number ( id . slice ( 3 ) ) ] } ,
360+ } ) )
361+
362+ const kept = await Promise . all (
363+ racing . map ( ( stripeSubscriptionId , index ) =>
364+ enforceOneTrialPerCard (
365+ trialingStripeSubscription ( stripeSubscriptionId , `pm_${ index } ` ) ,
366+ payer ( userId )
367+ )
368+ )
369+ )
370+
371+ expect ( kept . filter ( Boolean ) ) . toHaveLength ( 1 )
372+ expect ( stripeStub . subscriptions . update ) . toHaveBeenCalledOnce ( )
373+ } )
374+
375+ it ( 'leaves a trial on any plan other than Pro alone' , async ( ) => {
376+ const userId = await createUser ( )
377+
378+ expect (
379+ await enforceOneTrialPerCard ( trialingStripeSubscription ( `sub_${ generateId ( ) } ` , 'pm_1' ) , {
380+ plan : 'team_6000' ,
381+ referenceId : userId ,
382+ } )
383+ ) . toBe ( false )
384+ expect ( stripeStub . paymentMethods . retrieve ) . not . toHaveBeenCalled ( )
385+ expect ( stripeStub . subscriptions . update ) . not . toHaveBeenCalled ( )
386+ } )
387+
321388 it ( 'leaves a checkout that did not start a trial alone' , async ( ) => {
322389 const userId = await createUser ( )
323390
324391 expect (
325392 await enforceOneTrialPerCard (
326393 { id : `sub_${ generateId ( ) } ` , status : 'active' } as Stripe . Subscription ,
327- userId
394+ payer ( userId )
328395 )
329396 ) . toBe ( false )
330397 expect ( stripeStub . paymentMethods . retrieve ) . not . toHaveBeenCalled ( )
331398 expect ( stripeStub . subscriptions . update ) . not . toHaveBeenCalled ( )
332399 } )
333400} )
334401
402+ describe ( 'enforceOneTrialPerCardForCheckout' , ( ) => {
403+ it ( "judges a completed checkout's trial by the subscription Sim recorded for it" , async ( ) => {
404+ const fingerprint = `fp_${ generateId ( ) } `
405+ useCard ( fingerprint )
406+ const firstUserId = await createUser ( )
407+ const firstSubscriptionId = `sub_${ generateId ( ) } `
408+ await addSubscription ( firstUserId , {
409+ status : 'trialing' ,
410+ trialStart : new Date ( ) ,
411+ stripeSubscriptionId : firstSubscriptionId ,
412+ } )
413+ await enforceOneTrialPerCard (
414+ trialingStripeSubscription ( firstSubscriptionId , 'pm_1' ) ,
415+ payer ( firstUserId )
416+ )
417+
418+ const userId = await createUser ( )
419+ const stripeSubscriptionId = `sub_${ generateId ( ) } `
420+ await addSubscription ( userId , {
421+ status : 'trialing' ,
422+ trialStart : new Date ( ) ,
423+ stripeSubscriptionId,
424+ } )
425+ stripeStub . subscriptions . retrieve . mockResolvedValue (
426+ trialingStripeSubscription ( stripeSubscriptionId , 'pm_2' )
427+ )
428+
429+ await enforceOneTrialPerCardForCheckout ( {
430+ mode : 'subscription' ,
431+ subscription : stripeSubscriptionId ,
432+ } as Stripe . Checkout . Session )
433+
434+ expect ( stripeStub . subscriptions . update ) . toHaveBeenCalledExactlyOnceWith (
435+ stripeSubscriptionId ,
436+ { trial_end : 'now' } ,
437+ { idempotencyKey : `end-ineligible-trial:${ stripeSubscriptionId } ` }
438+ )
439+ } )
440+
441+ it ( 'fails, so the webhook retries, for a trial Sim has not recorded yet' , async ( ) => {
442+ const stripeSubscriptionId = `sub_${ generateId ( ) } `
443+ stripeStub . subscriptions . retrieve . mockResolvedValue (
444+ trialingStripeSubscription ( stripeSubscriptionId , 'pm_1' )
445+ )
446+
447+ await expect (
448+ enforceOneTrialPerCardForCheckout ( {
449+ mode : 'subscription' ,
450+ subscription : stripeSubscriptionId ,
451+ } as Stripe . Checkout . Session )
452+ ) . rejects . toThrow ( 'is not recorded yet' )
453+ expect ( stripeStub . subscriptions . update ) . not . toHaveBeenCalled ( )
454+ } )
455+ } )
456+
335457describe ( 'the trial usage limit' , ( ) => {
336458 it ( "holds a trial to its allowance, lowering a limit an earlier plan left higher, until it converts to the plan's base" , async ( ) => {
337459 const userId = await createUser ( )
0 commit comments