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
40 changes: 40 additions & 0 deletions src/composables/billing/useBillingContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,46 @@ describe('useBillingContext', () => {
expect(useErrorHandling().toastErrorHandler).not.toHaveBeenCalled()
})

it.for([
{
name: 'cancelSubscription',
run: (context: ReturnType<typeof useBillingContext>) =>
context.cancelSubscription()
},
{
name: 'resubscribe',
run: (context: ReturnType<typeof useBillingContext>) =>
context.resubscribe()
}
])(
'rejects $name for its caller to show if the workspace changed during the wait',
async ({ run }) => {
const current = ref<{ id: string; type?: 'team' }>({ id: 'ws-a' })
const workspaceStore = useTeamWorkspaceStore()
Object.assign(workspaceStore, {
activeWorkspace: computed(() =>
fromPartial<NonNullable<typeof workspaceStore.activeWorkspace>>(
current.value
)
)
})
const context = useBillingContext()
vi.clearAllMocks()

const outcome = run(context).then(
() => 'resolved',
(error: unknown) => error
)
await nextTick()
current.value = { id: 'ws-b', type: 'team' }

expect(await outcome).toMatchObject({
message: 'Your active workspace changed. Switch back and try again.'
})
expect(useErrorHandling().toastErrorHandler).not.toHaveBeenCalled()
}
)

it('holds previewSubscribe and requireActiveSubscription until the workspace loads', async () => {
const loaded = holdWorkspaceUnloaded('team')
const context = useBillingContext()
Expand Down
15 changes: 13 additions & 2 deletions src/composables/billing/useBillingContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const ROUTING_WAIT_TIMEOUT_MS = 10_000

class BillingRoutingUnavailableError extends Error {}

class BillingWorkspaceChangedError extends Error {}

function isTeamPlanSlug(planSlug: string | null | undefined): boolean {
const normalizedSlug = planSlug?.toLowerCase()
return (
Expand Down Expand Up @@ -286,6 +288,15 @@ function useBillingContextInternal(): BillingContext {
return !workspaceId || currentId === workspaceId
}

// For actions whose caller shows the outcome: a workspace switch during the
// wait is a failure the caller reports, not a silent drop.
async function waitForRoutingInSameWorkspace(): Promise<void> {
if (await waitForRouting()) return
throw new BillingWorkspaceChangedError(
t('subscription.cancelDialog.workspaceChanged')
)
}

// For actions whose caller shows no outcome: reports a timeout once, as
// legacy actions did on failure, and resolves false so the caller stops.
async function whenRoutingKnown(): Promise<boolean> {
Expand Down Expand Up @@ -377,14 +388,14 @@ function useBillingContextInternal(): BillingContext {
}

async function cancelSubscription(isScopeCurrent?: () => boolean) {
if (!(await waitForRouting())) return
await waitForRoutingInSameWorkspace()
return activeContext.value.cancelSubscription(isScopeCurrent)
}

async function resubscribe(
options?: Parameters<BillingActions['resubscribe']>[0]
) {
if (!(await waitForRouting())) return
await waitForRoutingInSameWorkspace()
return activeContext.value.resubscribe(options)
}

Expand Down
Loading