From cecdf7d7984110cc1e1bca627ab02f2cff93fdc1 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Sat, 3 Oct 2026 23:39:22 -0400 Subject: [PATCH 1/2] test(billing): cancel and resubscribe reject when the workspace changes during the routing wait --- .../billing/useBillingContext.test.ts | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/composables/billing/useBillingContext.test.ts b/src/composables/billing/useBillingContext.test.ts index 3b03c9d4537..324abb958a0 100644 --- a/src/composables/billing/useBillingContext.test.ts +++ b/src/composables/billing/useBillingContext.test.ts @@ -751,6 +751,46 @@ describe('useBillingContext', () => { expect(useErrorHandling().toastErrorHandler).not.toHaveBeenCalled() }) + it.for([ + { + name: 'cancelSubscription', + run: (context: ReturnType) => + context.cancelSubscription() + }, + { + name: 'resubscribe', + run: (context: ReturnType) => + 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>( + 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() From 049f8a3a19dfeaeb70969fb6d856972e0c5e9615 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Sat, 3 Oct 2026 23:40:18 -0400 Subject: [PATCH 2/2] fix(billing): cancel and resubscribe fail when the workspace changes during the routing wait --- src/composables/billing/useBillingContext.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/composables/billing/useBillingContext.ts b/src/composables/billing/useBillingContext.ts index c69a45bfa72..9acc5b3853a 100644 --- a/src/composables/billing/useBillingContext.ts +++ b/src/composables/billing/useBillingContext.ts @@ -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 ( @@ -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 { + 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 { @@ -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[0] ) { - if (!(await waitForRouting())) return + await waitForRoutingInSameWorkspace() return activeContext.value.resubscribe(options) }