@@ -2,14 +2,26 @@ import { authMockFns, createMockRequest, resetEnvFlagsMock, setEnvFlags } from '
22import { billingAccessMock , billingAccessMockFns } from '@sim/testing/mocks/billing-access.mock'
33import { billingCoreMock , billingCoreMockFns } from '@sim/testing/mocks/billing-core.mock'
44import { billingOrganizationMock } from '@sim/testing/mocks/billing-organization.mock'
5- import { billingPlanMock } from '@sim/testing/mocks/billing-plan.mock'
5+ import { billingPlanMock , billingPlanMockFns } from '@sim/testing/mocks/billing-plan.mock'
66import { billingSubscriptionMock } from '@sim/testing/mocks/billing-subscription.mock'
77import { posthogServerMock } from '@sim/testing/mocks/posthog-server.mock'
8+ import { stripeClientMock } from '@sim/testing/mocks/stripe.mock'
89import { afterAll , beforeAll , beforeEach , describe , expect , it , vi } from 'vitest'
910
10- const { mockCanManageWorkspaceBilling, mockGetWorkspaceHostContextForViewer } = vi . hoisted ( ( ) => ( {
11+ const {
12+ mockCanManageWorkspaceBilling,
13+ mockGetWorkspaceHostContextForViewer,
14+ mockClaimCheckoutAdmission,
15+ mockReleaseCheckoutAdmission,
16+ mockReadSwitchBasis,
17+ mockRecordPlanSwitch,
18+ } = vi . hoisted ( ( ) => ( {
1119 mockCanManageWorkspaceBilling : vi . fn ( ) ,
1220 mockGetWorkspaceHostContextForViewer : vi . fn ( ) ,
21+ mockClaimCheckoutAdmission : vi . fn ( ) ,
22+ mockReleaseCheckoutAdmission : vi . fn ( ) ,
23+ mockReadSwitchBasis : vi . fn ( ) ,
24+ mockRecordPlanSwitch : vi . fn ( ) ,
1325} ) )
1426
1527vi . mock ( '@/lib/billing/core/access' , ( ) => billingAccessMock )
@@ -22,6 +34,26 @@ vi.mock('@/lib/billing/core/plan', () => billingPlanMock)
2234
2335vi . mock ( '@/lib/billing/core/subscription' , ( ) => billingSubscriptionMock )
2436
37+ vi . mock ( '@/lib/billing/stripe-client' , ( ) => stripeClientMock )
38+
39+ vi . mock ( '@/lib/billing/checkout-admission' , ( ) => ( {
40+ claimCheckoutAdmission : mockClaimCheckoutAdmission ,
41+ releaseCheckoutAdmission : mockReleaseCheckoutAdmission ,
42+ } ) )
43+
44+ vi . mock ( '@/lib/billing/plans' , ( ) => ( {
45+ getPlanByName : ( name : string ) => ( {
46+ name,
47+ priceId : 'price-month' ,
48+ annualDiscountPriceId : 'price-year' ,
49+ } ) ,
50+ } ) )
51+
52+ vi . mock ( '@/lib/billing/core/plan-switch' , ( ) => ( {
53+ readSwitchBasis : mockReadSwitchBasis ,
54+ recordPlanSwitch : mockRecordPlanSwitch ,
55+ } ) )
56+
2557vi . mock ( '@/lib/billing/workspace-permissions' , ( ) => ( {
2658 canManageWorkspaceBilling : mockCanManageWorkspaceBilling ,
2759} ) )
@@ -34,7 +66,9 @@ vi.mock('@/lib/workspaces/host-context', () => ({
3466
3567import { POST } from '@/app/api/billing/switch-plan/route'
3668
37- const { mockGetOrganizationSubscription } = billingCoreMockFns
69+ const { mockGetOrganizationSubscription, mockIsSubscriptionOrgScoped } = billingCoreMockFns
70+
71+ const routeContext = { params : Promise . resolve ( { } ) }
3872
3973const { mockGetEffectiveBillingStatus } = billingAccessMockFns
4074
@@ -96,7 +130,8 @@ describe('POST /api/billing/switch-plan', () => {
96130 createMockRequest ( 'POST' , {
97131 targetPlanName : 'enterprise' ,
98132 workspaceId : 'workspace-1' ,
99- } )
133+ } ) ,
134+ routeContext
100135 )
101136
102137 expect ( response . status ) . toBe ( 400 )
@@ -105,4 +140,75 @@ describe('POST /api/billing/switch-plan', () => {
105140 } )
106141 expect ( mockGetEffectiveBillingStatus ) . not . toHaveBeenCalled ( )
107142 } )
143+
144+ describe ( 'during a Pro free trial' , ( ) => {
145+ const personalPro = ( status : string ) => ( {
146+ id : 'subscription-1' ,
147+ referenceId : 'viewer-1' ,
148+ plan : 'pro_6000' ,
149+ status,
150+ stripeSubscriptionId : 'stripe-subscription-1' ,
151+ } )
152+ const mockStripeUpdate = vi . fn ( )
153+
154+ beforeEach ( ( ) => {
155+ mockGetEffectiveBillingStatus . mockResolvedValue ( {
156+ billingBlocked : false ,
157+ billingBlockedReason : null ,
158+ blockedByOrgOwner : false ,
159+ } )
160+ mockIsSubscriptionOrgScoped . mockResolvedValue ( false )
161+ mockClaimCheckoutAdmission . mockResolvedValue ( { claimToken : 'claim-1' } )
162+ stripeClientMock . requireStripeClient . mockReturnValue ( {
163+ subscriptions : {
164+ retrieve : vi . fn ( ) . mockResolvedValue ( {
165+ status : 'trialing' ,
166+ items : {
167+ data : [ { id : 'item-1' , price : { id : 'price-1' , recurring : { interval : 'month' } } } ] ,
168+ } ,
169+ } ) ,
170+ update : mockStripeUpdate ,
171+ } ,
172+ } )
173+ } )
174+
175+ it . each ( [
176+ { label : 'a tier change' , targetPlanName : 'pro_25000' , interval : undefined } ,
177+ { label : 'an interval change' , targetPlanName : 'pro_6000' , interval : 'year' } ,
178+ ] ) ( 'refuses $label before touching Stripe' , async ( switchCase ) => {
179+ billingPlanMockFns . mockGetHighestPrioritySubscription . mockResolvedValue (
180+ personalPro ( 'trialing' )
181+ )
182+
183+ const response = await POST (
184+ createMockRequest ( 'POST' , {
185+ targetPlanName : switchCase . targetPlanName ,
186+ interval : switchCase . interval ,
187+ } ) ,
188+ routeContext
189+ )
190+
191+ expect ( response . status ) . toBe ( 409 )
192+ await expect ( response . json ( ) ) . resolves . toMatchObject ( {
193+ error : expect . stringContaining ( 'free trial ends' ) ,
194+ } )
195+ expect ( stripeClientMock . requireStripeClient ) . not . toHaveBeenCalled ( )
196+ } )
197+
198+ it ( 'refuses a switch Stripe still holds in trial while the row has moved on' , async ( ) => {
199+ const subscription = personalPro ( 'active' )
200+ billingPlanMockFns . mockGetHighestPrioritySubscription . mockResolvedValue ( subscription )
201+ mockReadSwitchBasis . mockResolvedValue ( { subscription, pool : undefined } )
202+
203+ const response = await POST (
204+ createMockRequest ( 'POST' , { targetPlanName : 'pro_6000' , interval : 'year' } ) ,
205+ routeContext
206+ )
207+
208+ expect ( response . status ) . toBe ( 409 )
209+ expect ( mockStripeUpdate ) . not . toHaveBeenCalled ( )
210+ expect ( mockRecordPlanSwitch ) . not . toHaveBeenCalled ( )
211+ expect ( mockReleaseCheckoutAdmission ) . toHaveBeenCalledWith ( { claimToken : 'claim-1' } )
212+ } )
213+ } )
108214} )
0 commit comments