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
16 changes: 12 additions & 4 deletions browser_tests/tests/dialogs/cancelSubscriptionDialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ test.describe('CancelSubscription dialog', { tag: '@ui' }, () => {
})

// No billing workspace loads in this environment, so the cancellation waits
// for routing, times out and reports an error instead of cancelling. The
// success path is covered in cancelSubscriptionSdkRail.
test('"Cancel subscription" button reports an error when billing is unavailable', async ({
// for routing, times out and fails instead of cancelling. The success path
// is covered in cancelSubscriptionSdkRail.
test('"Cancel subscription" button fails without claiming success when billing is unavailable', async ({
comfyPage
}) => {
test.setTimeout(45_000)
Expand All @@ -44,8 +44,16 @@ test.describe('CancelSubscription dialog', { tag: '@ui' }, () => {

await dialog.confirmCancelButton.click()

await expect(comfyPage.toast.toastErrors.first()).toBeVisible({
await expect(comfyPage.toast.toastErrors).toHaveCount(1, {
timeout: 20_000
})
await expect(comfyPage.toast.toastErrors).toContainText(
'Failed to cancel subscription'
)
await expect(comfyPage.toast.toastErrors).toContainText(
"We couldn't reach your account. Try again in a moment."
)
await expect(comfyPage.toast.toastSuccesses).toHaveCount(0)
await expect(dialog.root).toBeVisible()
})
})
35 changes: 35 additions & 0 deletions src/composables/billing/useBillingContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,41 @@ describe('useBillingContext', () => {
expect(workspaceApi.createTopup).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 never loads',
async ({ run }) => {
vi.useFakeTimers()
onTestFinished(() => {
vi.useRealTimers()
})
holdWorkspaceUnloaded('team')
const context = useBillingContext()
vi.clearAllMocks()

const outcome = run(context).then(
() => 'resolved',
(error: unknown) => error
)
await vi.advanceTimersByTimeAsync(10_000)

expect(await outcome).toMatchObject({
message: "We couldn't reach your account. Try again in a moment."
})
expect(useErrorHandling().toastErrorHandler).not.toHaveBeenCalled()
}
)

it('drops an action silently when the workspace changed during the wait', async () => {
const current = ref<{ id: string; type?: 'team' }>({ id: 'ws-a' })
const workspaceStore = useTeamWorkspaceStore()
Expand Down
35 changes: 21 additions & 14 deletions src/composables/billing/useBillingContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import { useWorkspaceBilling } from '@/platform/workspace/composables/useWorkspa
const LEGACY_TEAM_PLAN_SLUG_PREFIX = 'team-'
const PER_CREDIT_TEAM_PLAN_SLUG_PREFIX = 'team_per_credit_'

const ROUTING_WAIT_TIMEOUT_MS = 10_000

class BillingRoutingUnavailableError extends Error {}

function isTeamPlanSlug(planSlug: string | null | undefined): boolean {
Expand Down Expand Up @@ -265,27 +267,32 @@ function useBillingContextInternal(): BillingContext {
{ immediate: true }
)

const ROUTING_WAIT_TIMEOUT_MS = 10_000

// Resolves false so the caller stops: after reporting once on timeout (as
// legacy actions did on failure), or silently when the user switched
// workspace during the wait.
async function whenRoutingKnown(): Promise<boolean> {
// Rejects on timeout. Resolves false when the user switched workspace during
// the wait, so the caller stops silently.
async function waitForRouting(): Promise<boolean> {
if (type.value !== 'unknown') return true
const workspaceId = store.activeWorkspace?.id
try {
await until(type).not.toBe('unknown', {
timeout: ROUTING_WAIT_TIMEOUT_MS,
throwOnTimeout: true
})
const currentId = store.activeWorkspace?.id
return !workspaceId || currentId === workspaceId
} catch {
useErrorHandling().toastErrorHandler(
new BillingRoutingUnavailableError(
t('auth.webSession.token.unavailable')
)
throw new BillingRoutingUnavailableError(
t('auth.webSession.token.unavailable')
)
}
const currentId = store.activeWorkspace?.id
return !workspaceId || currentId === workspaceId
}

// 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> {
try {
return await waitForRouting()
} catch (err) {
useErrorHandling().toastErrorHandler(err)
return false
}
}
Expand Down Expand Up @@ -370,14 +377,14 @@ function useBillingContextInternal(): BillingContext {
}

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

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

Expand Down
Loading