Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/locales/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -2303,6 +2303,7 @@
"failedToCreateCustomer": "Failed to create customer: {error}",
"failedToInitiateCreditPurchase": "Failed to initiate credit purchase: {error}",
"failedToAccessBillingPortal": "Failed to access billing portal: {error}",
"invalidBillingPortalUrl": "Invalid portal URL",
"failedToPurchaseCredits": "Failed to purchase credits: {error}",
"failedToFetchSubscription": "Failed to fetch subscription status: {error}",
"failedToInitiateSubscription": "Failed to initiate subscription: {error}",
Expand Down
243 changes: 238 additions & 5 deletions src/platform/workspace/composables/useSubscriptionCheckout.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createTestingPinia } from '@pinia/testing'
import { setActivePinia } from 'pinia'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { computed, reactive } from 'vue'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { computed, effectScope, reactive } from 'vue'

import type { PaymentIntentSource } from '@/platform/telemetry/types'
import type {
Expand Down Expand Up @@ -49,6 +49,10 @@ function allPlans(): Plan[] {
return [makeStandardYearly(), makeCreatorMonthly()]
}

function errorWithCode(code: string, message = 'error') {
return Object.assign(new Error(message), { code })
}

interface ReactivationPreviewPlanInput {
slug: string
tier: PreviewSubscribeResponse['new_plan']['tier']
Expand Down Expand Up @@ -141,6 +145,9 @@ const {
mockFetchPlans,
mockFetchStatus,
mockFetchBalance,
mockOpen,
mockGetBillingStatus,
mockGetPaymentPortalUrl,
mockPlans,
mockResubscribe,
mockToastAdd,
Expand All @@ -162,6 +169,9 @@ const {
mockFetchPlans: vi.fn(),
mockFetchStatus: vi.fn(),
mockFetchBalance: vi.fn(),
mockOpen: vi.fn(),
mockGetBillingStatus: vi.fn(),
mockGetPaymentPortalUrl: vi.fn(),
mockPlans: { value: [] as Plan[] },
mockResubscribe: vi.fn(),
mockToastAdd: vi.fn(),
Expand Down Expand Up @@ -233,7 +243,11 @@ vi.mock('@/services/dialogService', () => ({

// Shields the test from the real workspaceApi → @/scripts/api → app.ts import chain
vi.mock('@/platform/workspace/api/workspaceApi', () => ({
workspaceApi: { resubscribe: mockResubscribe }
workspaceApi: {
resubscribe: mockResubscribe,
getBillingStatus: mockGetBillingStatus,
getPaymentPortalUrl: mockGetPaymentPortalUrl
}
}))

vi.mock('@/platform/workspace/stores/billingOperationStore', () => ({
Expand Down Expand Up @@ -295,16 +309,31 @@ vi.mock('vue-i18n', async (importOriginal) => {

describe('useSubscriptionCheckout', () => {
let emit: ReturnType<typeof vi.fn>
const scopes: ReturnType<typeof effectScope>[] = []

async function setup(
paymentIntentSource?: PaymentIntentSource,
tierPlanType: 'personal' | 'team' = 'personal'
) {
const { useSubscriptionCheckout } =
await import('./useSubscriptionCheckout')
return useSubscriptionCheckout(emit as never, paymentIntentSource, {
tierPlanType
const scope = effectScope()
scopes.push(scope)
return scope.run(() =>
useSubscriptionCheckout(emit as never, paymentIntentSource, {
tierPlanType
})
)!
}

async function submitRejectedPreview(code: string, message = 'error') {
const checkout = await setup()
mockPreviewSubscribe.mockRejectedValueOnce(errorWithCode(code, message))
await checkout.handleSubscribeClick({
tierKey: 'standard',
billingCycle: 'yearly'
})
return checkout
}

beforeEach(() => {
Expand All @@ -321,6 +350,17 @@ describe('useSubscriptionCheckout', () => {
mockShowDowngradeToPersonalDialog.mockResolvedValue(null)
mockUserId.value = 'user-1'
mockIsTeamPlan.value = false
mockOpen.mockReturnValue({} as Window)
mockGetBillingStatus.mockResolvedValue({ billing_status: 'paid' })
mockGetPaymentPortalUrl.mockResolvedValue({
url: 'https://billing.stripe.com/portal'
})
vi.stubGlobal('location', {
href: 'https://app.test/subscribe?invite=secret#token',
origin: 'https://app.test',
pathname: '/subscribe'
})
vi.stubGlobal('open', mockOpen)
mockShouldUseWorkspaceBilling.value = true
mockSetActiveWorkspaceId('workspace-1')
mockPermissions.value = {
Expand All @@ -332,6 +372,10 @@ describe('useSubscriptionCheckout', () => {
emit = vi.fn()
})

afterEach(() => {
for (const scope of scopes.splice(0)) scope.stop()
})

describe('handleSubscribeClick', () => {
it('transitions to preview on successful preview', async () => {
const checkout = await setup()
Expand Down Expand Up @@ -379,6 +423,96 @@ describe('useSubscriptionCheckout', () => {
)
})

it.for([
['SUBSCRIPTION_PAYMENT_REQUIRED', null],
['TRANSITION_NOT_ALLOWED', 'payment_failed']
] as const)(
'routes %s previews to the billing portal',
async ([code, status]) => {
if (status) {
mockGetBillingStatus.mockResolvedValueOnce({ billing_status: status })
}
await submitRejectedPreview(code)
expect(mockGetBillingStatus).toHaveBeenCalledTimes(status ? 1 : 0)
expect(mockGetPaymentPortalUrl).toHaveBeenCalledWith(
'https://app.test/subscribe'
)
expect(mockOpen).toHaveBeenCalledWith(
'https://billing.stripe.com/portal',
'_blank'
)
expect(globalThis.location.href).toBe(
'https://app.test/subscribe?invite=secret#token'
)
expect(mockFetchStatus).not.toHaveBeenCalled()

window.dispatchEvent(new Event('focus'))
await vi.waitFor(() => expect(mockFetchStatus).toHaveBeenCalledOnce())
window.dispatchEvent(new Event('focus'))
expect(mockFetchStatus).toHaveBeenCalledOnce()
}
)

it('preserves the checkout when the billing portal popup is blocked', async () => {
mockOpen.mockReturnValueOnce(null)
const checkout = await submitRejectedPreview(
'SUBSCRIPTION_PAYMENT_REQUIRED'
)

expect(checkout.selectedTierKey.value).toBe('standard')
expect(globalThis.location.href).toBe(
'https://app.test/subscribe?invite=secret#token'
)
expect(mockToastAdd).toHaveBeenCalledWith(
expect.objectContaining({
severity: 'warn',
detail: 'subscription.preview.paymentPopupBlocked'
})
)
})

it('keeps the original error path for non-payment transition failures', async () => {
await submitRejectedPreview(
'TRANSITION_NOT_ALLOWED',
'Plan change is unavailable'
)
expect(mockGetPaymentPortalUrl).not.toHaveBeenCalled()
expect(mockToastAdd).toHaveBeenCalledWith(
expect.objectContaining({ detail: 'Plan change is unavailable' })
)
})

it('shows the portal error when payment recovery cannot open', async () => {
mockGetPaymentPortalUrl.mockRejectedValueOnce(
new Error('Portal unavailable')
)
await submitRejectedPreview('SUBSCRIPTION_PAYMENT_REQUIRED')
expect(globalThis.location.href).toBe(
'https://app.test/subscribe?invite=secret#token'
)
expect(mockToastAdd).toHaveBeenCalledWith(
expect.objectContaining({ detail: 'Portal unavailable' })
)
})

it.for([
undefined,
'',
'javascript:alert(1)',
'https://billing.stripe.com.evil.test/portal'
])('rejects an unsafe billing portal URL: %s', async (url) => {
mockGetPaymentPortalUrl.mockResolvedValueOnce({ url })
await submitRejectedPreview('SUBSCRIPTION_PAYMENT_REQUIRED')
expect(globalThis.location.href).toBe(
'https://app.test/subscribe?invite=secret#token'
)
expect(mockToastAdd).toHaveBeenCalledWith(
expect.objectContaining({
detail: 'toastMessages.failedToAccessBillingPortal'
})
)
})

it('shows error toast when plan slug is not found', async () => {
const checkout = await setup()
mockPlans.value = []
Expand Down Expand Up @@ -617,6 +751,26 @@ describe('useSubscriptionCheckout', () => {
})

describe('handleSubscribeTeamClick', () => {
const teamStop = {
id: 'team_1400',
usd: 1400,
credits: 295_400,
discountedUsd: 1295
}

async function startTeamPaymentRecovery() {
mockPreviewSubscribe.mockRejectedValueOnce(
errorWithCode('SUBSCRIPTION_PAYMENT_REQUIRED')
)
const checkout = await setup()
const selection = checkout.handleSubscribeTeamClick({
stop: teamStop,
billingCycle: 'monthly',
isChange: true
})
return { checkout, selection }
}

it('transitions to preview with the selected team stop and cycle', async () => {
const checkout = await setup()

Expand Down Expand Up @@ -760,6 +914,45 @@ describe('useSubscriptionCheckout', () => {
expect(checkout.isLoadingPreview.value).toBe(false)
})

it('ignores a stale Team payment recovery after returning to pricing', async () => {
let resolvePortal!: (portal: { url: string }) => void
mockGetPaymentPortalUrl.mockImplementationOnce(
() =>
new Promise<{ url: string }>((resolve) => {
resolvePortal = resolve
})
)
const { checkout, selection } = await startTeamPaymentRecovery()
await vi.waitFor(() => expect(mockGetPaymentPortalUrl).toHaveBeenCalled())
expect(checkout.isLoadingPreview.value).toBe(true)

checkout.handleBackToPricing()
resolvePortal({ url: 'https://billing.stripe.com/portal' })
await selection

expect(globalThis.location.href).toBe(
'https://app.test/subscribe?invite=secret#token'
)
expect(checkout.checkoutStep.value).toBe('pricing')
expect(mockOpen).not.toHaveBeenCalled()
expect(mockToastAdd).not.toHaveBeenCalled()
})

it('returns a failed Team payment recovery to pricing', async () => {
mockGetPaymentPortalUrl.mockRejectedValueOnce(
new Error('Portal unavailable')
)
const { checkout, selection } = await startTeamPaymentRecovery()
await selection

expect(checkout.checkoutStep.value).toBe('pricing')
expect(checkout.selectedTeamStop.value).toBeNull()
expect(mockToastAdd).toHaveBeenCalledOnce()
expect(mockToastAdd).toHaveBeenCalledWith(
expect.objectContaining({ detail: 'Portal unavailable' })
)
})

it('falls back to the display-only confirm when the preview is a fresh subscription', async () => {
const checkout = await setup()
mockPreviewSubscribe.mockResolvedValueOnce({
Expand Down Expand Up @@ -2191,6 +2384,46 @@ describe('useSubscriptionCheckout', () => {
)
})

it('returns to pricing when reactivation payment recovery fails', async () => {
mockSubscription.value = { isCancelled: true }
mockPreviewSubscribe.mockRejectedValueOnce(
errorWithCode('SUBSCRIPTION_PAYMENT_REQUIRED')
)
mockGetPaymentPortalUrl.mockRejectedValueOnce(
new Error('Portal unavailable')
)
const checkout = await setup()
checkout.selectedTierKey.value = 'standard'
checkout.selectedBillingCycle.value = 'yearly'
checkout.previewData.value = {} as never

await checkout.handleConfirmTransition()

expect(checkout.checkoutStep.value).toBe('pricing')
expect(checkout.previewData.value).toBeNull()
expect(mockSubscribe).not.toHaveBeenCalled()
})

it('recovers payment failure while refreshing an expired quote', async () => {
mockSubscribe.mockRejectedValueOnce(
errorWithCode('PRORATION_QUOTE_EXPIRED', 'Quote expired')
)
mockPreviewSubscribe.mockRejectedValueOnce(
errorWithCode('SUBSCRIPTION_PAYMENT_REQUIRED')
)
const checkout = await setup()
checkout.selectedTierKey.value = 'standard'
checkout.selectedBillingCycle.value = 'yearly'

await checkout.handleConfirmTransition()

expect(mockOpen).toHaveBeenCalledWith(
'https://billing.stripe.com/portal',
'_blank'
)
expect(mockToastAdd).not.toHaveBeenCalled()
})

it('forwards confirmReactivation true when the disclosure banner reports consent', async () => {
const checkout = await setup()
checkout.selectedTierKey.value = 'standard'
Expand Down
Loading
Loading