diff --git a/src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts b/src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts index 65dce0c9c8a..accd895a849 100644 --- a/src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts +++ b/src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts @@ -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('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: '
' + props: ['paymentRecoveryLock'], + emits: ['paymentRecoveryClick'], + template: + '
' } })) @@ -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', () => { @@ -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')) + 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((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((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() + }) }) diff --git a/src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue b/src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue index e5e8922a94a..e0fba1aa9d0 100644 --- a/src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue +++ b/src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue @@ -1,19 +1,113 @@ diff --git a/src/components/actionbar/ComfyRunButton/ComfyQueueButton.test.ts b/src/components/actionbar/ComfyRunButton/ComfyQueueButton.test.ts index e4d940a850b..0aa8155658e 100644 --- a/src/components/actionbar/ComfyRunButton/ComfyQueueButton.test.ts +++ b/src/components/actionbar/ComfyRunButton/ComfyQueueButton.test.ts @@ -56,6 +56,15 @@ const i18n = createI18n({ runWorkflow: 'Run workflow', runWorkflowFront: 'Run workflow front', runWorkflowMissingResources: 'Workflow contains missing resources' + }, + subscription: { + paymentRecovery: { + ownerRunLabel: 'Update payment to run', + memberRunLabel: 'Run', + ownerRunTooltip: 'Update payment to restore this subscription', + memberRunTooltip: + 'Ask your workspace owner to restore this subscription' + } } } } @@ -136,7 +145,9 @@ const stubs = { DropdownMenuItem: { template: '
' } } -function renderQueueButton() { +function renderQueueButton( + props: { paymentRecoveryLock?: 'owner' | 'member' } = {} +) { const pinia = createTestingPinia({ createSpy: vi.fn, stubActions: (actionName) => actionName !== 'recordPromptError' @@ -144,6 +155,7 @@ function renderQueueButton() { const user = userEvent.setup() const result = render(ComfyQueueButton, { + props, global: { plugins: [pinia, i18n], directives: { @@ -165,6 +177,34 @@ describe('ComfyQueueButton', () => { expect(controls[1]).toHaveAttribute('data-testid', 'queue-button') }) + it.for([ + { + paymentRecoveryLock: 'owner', + label: 'Update payment to run', + variant: 'subscribe' + }, + { paymentRecoveryLock: 'member', label: 'Run', variant: 'secondary' } + ] as const)( + 'keeps the queue group mounted for a paused $paymentRecoveryLock and blocks execution', + async ({ paymentRecoveryLock, label, variant }) => { + useQueueSettingsStore().mode = 'change' + const { user, emitted } = renderQueueButton({ paymentRecoveryLock }) + const commandStore = useCommandStore() + + expect(screen.getByTestId('batch-count-edit')).toBeInTheDocument() + expect(screen.getByTestId('queue-mode-menu-trigger')).toBeDisabled() + expect(useQueueSettingsStore().mode).toBe('disabled') + const button = screen.getByTestId('queue-button') + expect(button).toHaveTextContent(label) + expect(button).toHaveAttribute('data-variant', variant) + + await user.click(button) + + expect(commandStore.execute).not.toHaveBeenCalled() + expect(emitted()).toHaveProperty('paymentRecoveryClick') + } + ) + it.for(missingResourceCases)( 'clears the warning icon when missing $label are resolved', async ({ setMissing, clearMissing }) => { diff --git a/src/components/actionbar/ComfyRunButton/ComfyQueueButton.vue b/src/components/actionbar/ComfyRunButton/ComfyQueueButton.vue index 2b156f70fe0..467673423ed 100644 --- a/src/components/actionbar/ComfyRunButton/ComfyQueueButton.vue +++ b/src/components/actionbar/ComfyRunButton/ComfyQueueButton.vue @@ -10,7 +10,12 @@ }" :variant="queueButtonVariant" size="unset" - :class="queueActionButtonClass" + :class=" + cn( + 'h-full gap-1.5 rounded-lg px-4', + paymentRecoveryLock ? 'font-medium' : 'font-light' + ) + " data-testid="queue-button" :data-variant="queueButtonVariant" @click="queuePrompt" @@ -24,6 +29,7 @@