diff --git a/browser_tests/tests/subscription.spec.ts b/browser_tests/tests/subscription.spec.ts index 07188830f0e..7d8c5ec27b1 100644 --- a/browser_tests/tests/subscription.spec.ts +++ b/browser_tests/tests/subscription.spec.ts @@ -125,6 +125,31 @@ unsubscribedTest.describe( } ) + unsubscribedTest( + 'App focus refreshes stale subscription state and restores Run', + async ({ comfyPage, subscriptionHelper }) => { + await expect( + comfyPage.page.getByTestId(TestIds.topbar.subscribeToRunButton) + ).toBeVisible() + + subscriptionHelper.setStatus({ + is_active: true, + subscription_tier: 'STANDARD', + subscription_duration: 'MONTHLY' + }) + await comfyPage.page.evaluate(() => { + window.dispatchEvent(new Event('focus')) + }) + + await expect( + comfyPage.page.getByTestId(TestIds.topbar.queueButton) + ).toBeVisible() + await expect( + comfyPage.page.getByTestId(TestIds.topbar.subscribeToRunButton) + ).toBeHidden() + } + ) + unsubscribedTest( 'Cleanup prevents stale subscription state after dialog close', async ({ comfyPage, subscriptionHelper }) => { diff --git a/src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts b/src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts index accd895a849..5747ae8e5be 100644 --- a/src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts +++ b/src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts @@ -112,6 +112,123 @@ describe('CloudRunButtonWrapper', () => { expect(screen.queryByTestId('queue-button')).not.toBeInTheDocument() }) + it('refreshes stale billing state on focus and restores Run', async () => { + mockCanRunWorkflows.value = false + mockBillingStatus.value = 'inactive' + state.fetchStatus.mockImplementationOnce(async () => { + mockBillingStatus.value = 'paid' + mockCanRunWorkflows.value = true + }) + renderWrapper() + + window.dispatchEvent(new Event('focus')) + + await waitFor(() => { + expect(state.fetchStatus).toHaveBeenCalledOnce() + expect(state.fetchBalance).toHaveBeenCalledOnce() + expect(screen.getByRole('button', { name: 'Run' })).toBeInTheDocument() + }) + }) + + it('refreshes stale billing state when the app becomes visible', async () => { + const visibilityState = vi + .spyOn(document, 'visibilityState', 'get') + .mockReturnValue('visible') + try { + mockCanRunWorkflows.value = false + mockBillingStatus.value = 'inactive' + state.fetchStatus.mockImplementationOnce(async () => { + mockBillingStatus.value = 'paid' + mockCanRunWorkflows.value = true + }) + renderWrapper() + + document.dispatchEvent(new Event('visibilitychange')) + + await waitFor(() => { + expect(state.fetchStatus).toHaveBeenCalledOnce() + expect(state.fetchBalance).toHaveBeenCalledOnce() + expect(screen.getByRole('button', { name: 'Run' })).toBeInTheDocument() + }) + } finally { + visibilityState.mockRestore() + } + }) + + it('deduplicates simultaneous focus and visibility refreshes', async () => { + let resolveRefresh!: () => void + const refresh = new Promise((resolve) => { + resolveRefresh = resolve + }) + const visibilityState = vi + .spyOn(document, 'visibilityState', 'get') + .mockReturnValue('visible') + try { + mockCanRunWorkflows.value = false + mockBillingStatus.value = 'inactive' + state.fetchStatus.mockReturnValueOnce(refresh) + state.fetchBalance.mockReturnValueOnce(refresh) + renderWrapper() + + window.dispatchEvent(new Event('focus')) + document.dispatchEvent(new Event('visibilitychange')) + + expect(state.fetchStatus).toHaveBeenCalledOnce() + expect(state.fetchBalance).toHaveBeenCalledOnce() + + resolveRefresh() + await refresh + } finally { + visibilityState.mockRestore() + } + }) + + it('does not refresh billing while Run is already available', () => { + renderWrapper() + + window.dispatchEvent(new Event('focus')) + + expect(state.fetchStatus).not.toHaveBeenCalled() + expect(state.fetchBalance).not.toHaveBeenCalled() + }) + + it('ignores visibility changes while the app remains hidden', () => { + const visibilityState = vi + .spyOn(document, 'visibilityState', 'get') + .mockReturnValue('hidden') + try { + mockCanRunWorkflows.value = false + mockBillingStatus.value = 'inactive' + renderWrapper() + + document.dispatchEvent(new Event('visibilitychange')) + + expect(state.fetchStatus).not.toHaveBeenCalled() + expect(state.fetchBalance).not.toHaveBeenCalled() + } finally { + visibilityState.mockRestore() + } + }) + + it('retries a failed stale billing refresh on the next focus', async () => { + mockCanRunWorkflows.value = false + mockBillingStatus.value = 'inactive' + state.fetchStatus.mockRejectedValueOnce(new Error('Status unavailable')) + state.fetchBalance.mockRejectedValueOnce(new Error('Balance unavailable')) + renderWrapper() + + window.dispatchEvent(new Event('focus')) + await waitFor(() => expect(state.fetchStatus).toHaveBeenCalledOnce()) + await new Promise((resolve) => setTimeout(resolve)) + + window.dispatchEvent(new Event('focus')) + + await waitFor(() => { + expect(state.fetchStatus).toHaveBeenCalledTimes(2) + expect(state.fetchBalance).toHaveBeenCalledTimes(2) + }) + }) + it('unlocks the run button once the subscription becomes active again', async () => { mockCanRunWorkflows.value = false renderWrapper() @@ -154,6 +271,17 @@ describe('CloudRunButtonWrapper', () => { expect(state.manageSubscription).toHaveBeenCalledOnce() }) + it('does not refresh paused billing before the recovery portal is opened', () => { + mockCanRunWorkflows.value = false + mockBillingStatus.value = 'paused' + renderWrapper() + + window.dispatchEvent(new Event('focus')) + + expect(state.fetchStatus).not.toHaveBeenCalled() + expect(state.fetchBalance).not.toHaveBeenCalled() + }) + it('keeps recovery open and surfaces portal failures', async () => { const error = new Error('Portal unavailable') state.manageSubscription.mockRejectedValueOnce(error) diff --git a/src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue b/src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue index e0fba1aa9d0..0aecf58377f 100644 --- a/src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue +++ b/src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue @@ -35,6 +35,7 @@ const dialogStore = useDialogStore() const { toastErrorHandler } = useErrorHandling() const isUpdatingPayment = ref(false) let paymentPortalRequest: Promise | null = null +let billingRefreshRequest: Promise | null = null let isUnmounted = false let refreshBillingOnFocus = false @@ -42,12 +43,6 @@ 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 @@ -56,6 +51,30 @@ const paymentRecoveryLock = computed<'owner' | 'member' | null>(() => : null ) +function refreshStaleBillingState() { + if ( + billingRefreshRequest || + (!refreshBillingOnFocus && + (!showsSubscribeToRunPrompt.value || paymentRecoveryLock.value)) + ) { + return + } + + refreshBillingOnFocus = false + billingRefreshRequest = Promise.allSettled([ + fetchStatus(), + fetchBalance() + ]).then(() => undefined) + void billingRefreshRequest.finally(() => { + billingRefreshRequest = null + }) +} + +useEventListener(window, 'focus', refreshStaleBillingState) +useEventListener(document, 'visibilitychange', () => { + if (document.visibilityState === 'visible') refreshStaleBillingState() +}) + function closePaymentRecoveryDialog() { dialogStore.closeDialog({ key: DIALOG_KEY }) }