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
Original file line number Diff line number Diff line change
@@ -1,21 +1,77 @@
import { render, screen } from '@testing-library/vue'
import userEvent from '@testing-library/user-event'
import { render, screen, waitFor } from '@testing-library/vue'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { nextTick, ref } from 'vue'

import CloudRunButtonWrapper from './CloudRunButtonWrapper.vue'

const mockCanRunWorkflows = ref(true)
const mockBillingStatus = ref<string | null>('paid')
const state = vi.hoisted(() => ({
v1PaymentRecovery: true,
canManageSubscription: true,
manageSubscription: vi.fn(),
fetchStatus: vi.fn(),
fetchBalance: vi.fn(),
toastErrorHandler: vi.fn(),
showLayoutDialog: vi.fn(),
closeDialog: vi.fn(),
updateDialog: vi.fn()
}))

vi.mock('@/composables/billing/useBillingContext', () => ({
useBillingContext: () => ({
canRunWorkflows: mockCanRunWorkflows
canRunWorkflows: mockCanRunWorkflows,
billingStatus: mockBillingStatus,
manageSubscription: state.manageSubscription,
fetchStatus: state.fetchStatus,
fetchBalance: state.fetchBalance
})
}))

vi.mock('@/composables/useFeatureFlags', () => ({
useFeatureFlags: () => ({
flags: {
get v1PaymentRecovery() {
return state.v1PaymentRecovery
}
}
})
}))

vi.mock('@/composables/useErrorHandling', () => ({
useErrorHandling: () => ({ toastErrorHandler: state.toastErrorHandler })
}))

vi.mock('@/platform/workspace/composables/useWorkspaceUI', async () => {
const { computed } = await import('vue')
return {
useWorkspaceUI: () => ({
permissions: computed(() => ({
canManageSubscription: state.canManageSubscription
}))
})
}
})

vi.mock('@/services/dialogService', () => ({
useDialogService: () => ({ showLayoutDialog: state.showLayoutDialog })
}))

vi.mock('@/stores/dialogStore', () => ({
useDialogStore: () => ({
closeDialog: state.closeDialog,
updateDialog: state.updateDialog
})
}))

vi.mock('@/components/actionbar/ComfyRunButton/ComfyQueueButton.vue', () => ({
default: {
name: 'ComfyQueueButton',
template: '<div data-testid="queue-button" />'
props: ['paymentRecoveryLock'],
emits: ['paymentRecoveryClick'],
template:
'<div data-testid="queue-group"><div data-testid="batch-count"/><button data-testid="queue-button" @click="$emit(\'paymentRecoveryClick\')">{{ paymentRecoveryLock === \'owner\' ? \'Update payment to run\' : \'Run\' }}</button><div data-testid="queue-dropdown"/></div>'
}
}))

Expand All @@ -33,6 +89,10 @@ function renderWrapper() {
describe('CloudRunButtonWrapper', () => {
beforeEach(() => {
mockCanRunWorkflows.value = true
mockBillingStatus.value = 'paid'
state.v1PaymentRecovery = true
state.canManageSubscription = true
vi.clearAllMocks()
})

it('renders the runnable queue button when the subscription is active', () => {
Expand Down Expand Up @@ -66,4 +126,177 @@ describe('CloudRunButtonWrapper', () => {
screen.queryByTestId('subscribe-to-run-button')
).not.toBeInTheDocument()
})

it('preserves queue controls and opens the owner recovery flow when paused', async () => {
mockCanRunWorkflows.value = false
mockBillingStatus.value = 'paused'
renderWrapper()

expect(screen.getByTestId('batch-count')).toBeInTheDocument()
expect(screen.getByTestId('queue-dropdown')).toBeInTheDocument()
expect(screen.getByTestId('queue-button')).toHaveTextContent(
'Update payment to run'
)
expect(
screen.queryByTestId('subscribe-to-run-button')
).not.toBeInTheDocument()

await userEvent.click(
screen.getByRole('button', { name: 'Update payment to run' })
)
const dialogOptions = state.showLayoutDialog.mock.calls[0][0]
expect(dialogOptions.props.canManage).toBe(true)

await dialogOptions.props.onUpdatePayment()
expect(state.closeDialog).toHaveBeenCalledWith({
key: 'subscription-paused'
})
expect(state.manageSubscription).toHaveBeenCalledOnce()
})

it('keeps recovery open and surfaces portal failures', async () => {
const error = new Error('Portal unavailable')
state.manageSubscription.mockRejectedValueOnce(error)
mockCanRunWorkflows.value = false
mockBillingStatus.value = 'paused'
renderWrapper()

await userEvent.click(screen.getByTestId('queue-button'))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
const dialogOptions = state.showLayoutDialog.mock.calls[0][0]
await dialogOptions.props.onUpdatePayment()

expect(state.toastErrorHandler).toHaveBeenCalledWith(error)
expect(state.closeDialog).not.toHaveBeenCalled()
})

it('refreshes billing once on focus after returning from the portal', async () => {
mockCanRunWorkflows.value = false
mockBillingStatus.value = 'paused'
state.fetchStatus.mockImplementationOnce(() => {
mockBillingStatus.value = 'paid'
mockCanRunWorkflows.value = true
})
renderWrapper()

await userEvent.click(
screen.getByRole('button', { name: 'Update payment to run' })
)
const dialog = state.showLayoutDialog.mock.calls[0][0]
await dialog.props.onUpdatePayment()

window.dispatchEvent(new Event('focus'))
await waitFor(() => {
expect(state.fetchStatus).toHaveBeenCalledOnce()
expect(state.fetchBalance).toHaveBeenCalledOnce()
expect(screen.getByRole('button', { name: 'Run' })).toBeInTheDocument()
})

window.dispatchEvent(new Event('focus'))
expect(state.fetchStatus).toHaveBeenCalledOnce()
expect(state.fetchBalance).toHaveBeenCalledOnce()
})

it('reuses a pending portal request across rapid clicks and reopen', async () => {
let resolvePortal!: () => void
state.manageSubscription.mockImplementationOnce(
() =>
new Promise<void>((resolve) => {
resolvePortal = resolve
})
)
mockCanRunWorkflows.value = false
mockBillingStatus.value = 'paused'
renderWrapper()

await userEvent.click(
screen.getByRole('button', { name: 'Update payment to run' })
)
const firstDialog = state.showLayoutDialog.mock.calls[0][0]
const firstRequest = firstDialog.props.onUpdatePayment()
const repeatedRequest = firstDialog.props.onUpdatePayment()
expect(state.manageSubscription).toHaveBeenCalledOnce()

firstDialog.props.onClose()
await userEvent.click(
screen.getByRole('button', { name: 'Update payment to run' })
)
const reopenedDialog = state.showLayoutDialog.mock.calls.at(-1)?.[0]
expect(reopenedDialog.props.isUpdatingPayment).toBe(true)
expect(reopenedDialog.props.onUpdatePayment()).toBe(firstRequest)
expect(repeatedRequest).toBe(firstRequest)
expect(state.manageSubscription).toHaveBeenCalledOnce()

resolvePortal()
await firstRequest
expect(state.updateDialog).toHaveBeenLastCalledWith({
key: 'subscription-paused',
contentProps: { isUpdatingPayment: false }
})
})

it('does not update a replacement dialog after unmount', async () => {
let resolvePortal!: () => void
state.manageSubscription.mockImplementationOnce(
() =>
new Promise<void>((resolve) => {
resolvePortal = resolve
})
)
mockCanRunWorkflows.value = false
mockBillingStatus.value = 'paused'
const { unmount } = renderWrapper()

await userEvent.click(
screen.getByRole('button', { name: 'Update payment to run' })
)
const dialog = state.showLayoutDialog.mock.calls[0][0]
const portalRequest = dialog.props.onUpdatePayment()
unmount()

resolvePortal()
await portalRequest
expect(state.closeDialog).not.toHaveBeenCalled()
expect(state.updateDialog).toHaveBeenCalledTimes(1)
expect(state.updateDialog).toHaveBeenCalledWith({
key: 'subscription-paused',
contentProps: { isUpdatingPayment: true }
})
})

it('opens member-safe recovery copy without a payment action', async () => {
mockCanRunWorkflows.value = false
mockBillingStatus.value = 'paused'
state.canManageSubscription = false
renderWrapper()

expect(screen.getByTestId('queue-button')).toHaveTextContent('Run')
await userEvent.click(screen.getByTestId('queue-button'))

const dialogOptions = state.showLayoutDialog.mock.calls[0][0]
expect(dialogOptions.props.canManage).toBe(false)
dialogOptions.props.onClose()
expect(state.closeDialog).toHaveBeenCalledWith({
key: 'subscription-paused'
})
expect(state.manageSubscription).not.toHaveBeenCalled()
})

it('keeps generic inactive behavior when payment recovery is disabled', () => {
mockCanRunWorkflows.value = false
mockBillingStatus.value = 'paused'
state.v1PaymentRecovery = false
renderWrapper()

expect(screen.getByTestId('subscribe-to-run-button')).toBeInTheDocument()
expect(screen.queryByTestId('queue-group')).not.toBeInTheDocument()
})

it('keeps generic inactive behavior for non-paused statuses', () => {
mockCanRunWorkflows.value = false
mockBillingStatus.value = 'inactive'
renderWrapper()

expect(screen.getByTestId('subscribe-to-run-button')).toBeInTheDocument()
expect(screen.queryByTestId('queue-group')).not.toBeInTheDocument()
})
})
108 changes: 101 additions & 7 deletions src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,113 @@
<template>
<component
:is="currentButton"
:key="canRunWorkflows ? 'queue' : 'subscribe'"
<ComfyQueueButton
v-if="canRunWorkflows || paymentRecoveryLock"
:payment-recovery-lock="paymentRecoveryLock"
@payment-recovery-click="showPaymentRecoveryDialog"
/>
<SubscribeToRunButton v-else />
</template>
<script setup lang="ts">
import { computed } from 'vue'
import { useEventListener } from '@vueuse/core'
import { computed, onUnmounted, ref } from 'vue'

import ComfyQueueButton from '@/components/actionbar/ComfyRunButton/ComfyQueueButton.vue'
import { useBillingContext } from '@/composables/billing/useBillingContext'
import { useErrorHandling } from '@/composables/useErrorHandling'
import { useFeatureFlags } from '@/composables/useFeatureFlags'
import SubscribeToRunButton from '@/platform/cloud/subscription/components/SubscribeToRun.vue'
import SubscriptionPausedDialog from '@/platform/workspace/components/SubscriptionPausedDialog.vue'
import { useWorkspaceUI } from '@/platform/workspace/composables/useWorkspaceUI'
import { useDialogService } from '@/services/dialogService'
import { useDialogStore } from '@/stores/dialogStore'

const { canRunWorkflows } = useBillingContext()
const DIALOG_KEY = 'subscription-paused'
const {
canRunWorkflows,
billingStatus,
manageSubscription,
fetchStatus,
fetchBalance
} = useBillingContext()
const { flags } = useFeatureFlags()
const { permissions } = useWorkspaceUI()
const dialogService = useDialogService()
const dialogStore = useDialogStore()
const { toastErrorHandler } = useErrorHandling()
const isUpdatingPayment = ref(false)
let paymentPortalRequest: Promise<void> | null = null
let isUnmounted = false
let refreshBillingOnFocus = false

const currentButton = computed(() =>
canRunWorkflows.value ? ComfyQueueButton : SubscribeToRunButton
onUnmounted(() => {
isUnmounted = true
})

useEventListener(window, 'focus', () => {
if (!refreshBillingOnFocus) return
refreshBillingOnFocus = false
void Promise.allSettled([fetchStatus(), fetchBalance()])
})

const paymentRecoveryLock = computed<'owner' | 'member' | null>(() =>
flags.v1PaymentRecovery && billingStatus.value === 'paused'
? permissions.value.canManageSubscription
? 'owner'
: 'member'
: null
)

function closePaymentRecoveryDialog() {
dialogStore.closeDialog({ key: DIALOG_KEY })
}

function updatePayment(): Promise<void> {
if (paymentPortalRequest) return paymentPortalRequest

paymentPortalRequest = (async () => {
isUpdatingPayment.value = true
dialogStore.updateDialog({
key: DIALOG_KEY,
contentProps: { isUpdatingPayment: true }
})
try {
await manageSubscription()
Comment thread
dante01yoon marked this conversation as resolved.
Resolved
if (!isUnmounted) {
refreshBillingOnFocus = true
closePaymentRecoveryDialog()
}
} catch (error) {
if (!isUnmounted) toastErrorHandler(error)
} finally {
isUpdatingPayment.value = false
paymentPortalRequest = null
if (!isUnmounted) {
dialogStore.updateDialog({
key: DIALOG_KEY,
contentProps: { isUpdatingPayment: false }
})
}
}
})()

return paymentPortalRequest
}

function showPaymentRecoveryDialog() {
dialogService.showLayoutDialog({
key: DIALOG_KEY,
component: SubscriptionPausedDialog,
props: {
canManage: paymentRecoveryLock.value === 'owner',
isUpdatingPayment: isUpdatingPayment.value,
onClose: closePaymentRecoveryDialog,
onUpdatePayment: updatePayment
},
dialogComponentProps: {
renderer: 'reka',
headless: true,
contentClass:
'w-[min(360px,95vw)] max-w-[min(360px,95vw)] sm:max-w-[min(360px,95vw)] border-0 bg-transparent shadow-none'
}
})
}
</script>
Loading
Loading