Skip to content
Closed
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
25 changes: 25 additions & 0 deletions browser_tests/tests/subscription.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down
128 changes: 128 additions & 0 deletions src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>((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()
Expand Down Expand Up @@ -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)
Expand Down
31 changes: 25 additions & 6 deletions src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,14 @@
const { toastErrorHandler } = useErrorHandling()
const isUpdatingPayment = ref(false)
let paymentPortalRequest: Promise<void> | null = null
let billingRefreshRequest: Promise<void> | null = null
let isUnmounted = false
let refreshBillingOnFocus = false

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
Expand All @@ -56,6 +51,30 @@
: null
)

function refreshStaleBillingState() {
if (
billingRefreshRequest ||
(!refreshBillingOnFocus &&
(!showsSubscribeToRunPrompt.value || paymentRecoveryLock.value))

Check failure on line 58 in src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue

View workflow job for this annotation

GitHub Actions / setup

Cannot find name 'showsSubscribeToRunPrompt'.

Check failure on line 58 in src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue

View workflow job for this annotation

GitHub Actions / scan

Cannot find name 'showsSubscribeToRunPrompt'.

Check failure on line 58 in src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue

View workflow job for this annotation

GitHub Actions / validate-fonts

Cannot find name 'showsSubscribeToRunPrompt'.

Check failure on line 58 in src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue

View workflow job for this annotation

GitHub Actions / perf-tests

Cannot find name 'showsSubscribeToRunPrompt'.

Check failure on line 58 in src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue

View workflow job for this annotation

GitHub Actions / lint-and-format

'showsSubscribeToRunPrompt' is not defined

Check failure on line 58 in src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue

View workflow job for this annotation

GitHub Actions / test

src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts > CloudRunButtonWrapper > does not refresh billing while Run is already available

ReferenceError: showsSubscribeToRunPrompt is not defined ❯ GlobalWindow.refreshStaleBillingState src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue:58:9 ❯ GlobalWindow.#callDispatchEventListeners node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:305:41 ❯ GlobalWindow.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:142:35 ❯ GlobalWindow.#goThroughDispatchEventPhases node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:214:32 ❯ GlobalWindow.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:135:38 ❯ src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts:189:12

Check failure on line 58 in src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue

View workflow job for this annotation

GitHub Actions / test

src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts > CloudRunButtonWrapper > deduplicates simultaneous focus and visibility refreshes

ReferenceError: showsSubscribeToRunPrompt is not defined ❯ GlobalWindow.refreshStaleBillingState src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue:58:9 ❯ GlobalWindow.#callDispatchEventListeners node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:305:41 ❯ GlobalWindow.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:142:35 ❯ GlobalWindow.#goThroughDispatchEventPhases node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:214:32 ❯ GlobalWindow.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:135:38 ❯ src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts:173:14

Check failure on line 58 in src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue

View workflow job for this annotation

GitHub Actions / test

src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts > CloudRunButtonWrapper > deduplicates simultaneous focus and visibility refreshes

ReferenceError: showsSubscribeToRunPrompt is not defined ❯ GlobalWindow.refreshStaleBillingState src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue:58:9 ❯ GlobalWindow.#callDispatchEventListeners node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:305:41 ❯ GlobalWindow.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:142:35 ❯ GlobalWindow.#goThroughDispatchEventPhases node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:214:32 ❯ GlobalWindow.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:135:38 ❯ src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts:173:14

Check failure on line 58 in src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue

View workflow job for this annotation

GitHub Actions / test

src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts > CloudRunButtonWrapper > deduplicates simultaneous focus and visibility refreshes

ReferenceError: showsSubscribeToRunPrompt is not defined ❯ GlobalWindow.refreshStaleBillingState src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue:58:9 ❯ GlobalWindow.#callDispatchEventListeners node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:305:41 ❯ GlobalWindow.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:142:35 ❯ GlobalWindow.#goThroughDispatchEventPhases node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:214:32 ❯ GlobalWindow.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:135:38 ❯ src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts:173:14

Check failure on line 58 in src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue

View workflow job for this annotation

GitHub Actions / test

src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts > CloudRunButtonWrapper > refreshes stale billing state when the app becomes visible

ReferenceError: showsSubscribeToRunPrompt is not defined ❯ refreshStaleBillingState src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue:58:9 ❯ HTMLDocument.<anonymous> src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue:75:47 ❯ HTMLDocument.#callDispatchEventListeners node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:305:41 ❯ HTMLDocument.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:142:35 ❯ HTMLDocument.#goThroughDispatchEventPhases node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:214:32 ❯ HTMLDocument.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:135:38 ❯ src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts:146:16

Check failure on line 58 in src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue

View workflow job for this annotation

GitHub Actions / test

src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts > CloudRunButtonWrapper > refreshes stale billing state when the app becomes visible

ReferenceError: showsSubscribeToRunPrompt is not defined ❯ refreshStaleBillingState src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue:58:9 ❯ HTMLDocument.<anonymous> src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue:75:47 ❯ HTMLDocument.#callDispatchEventListeners node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:305:41 ❯ HTMLDocument.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:142:35 ❯ HTMLDocument.#goThroughDispatchEventPhases node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:214:32 ❯ HTMLDocument.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:135:38 ❯ src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts:146:16

Check failure on line 58 in src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue

View workflow job for this annotation

GitHub Actions / test

src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts > CloudRunButtonWrapper > refreshes stale billing state when the app becomes visible

ReferenceError: showsSubscribeToRunPrompt is not defined ❯ refreshStaleBillingState src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue:58:9 ❯ HTMLDocument.<anonymous> src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue:75:47 ❯ HTMLDocument.#callDispatchEventListeners node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:305:41 ❯ HTMLDocument.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:142:35 ❯ HTMLDocument.#goThroughDispatchEventPhases node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:214:32 ❯ HTMLDocument.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:135:38 ❯ src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts:146:16

Check failure on line 58 in src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue

View workflow job for this annotation

GitHub Actions / test

src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts > CloudRunButtonWrapper > refreshes stale billing state on focus and restores Run

ReferenceError: showsSubscribeToRunPrompt is not defined ❯ GlobalWindow.refreshStaleBillingState src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue:58:9 ❯ GlobalWindow.#callDispatchEventListeners node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:305:41 ❯ GlobalWindow.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:142:35 ❯ GlobalWindow.#goThroughDispatchEventPhases node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:214:32 ❯ GlobalWindow.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:135:38 ❯ src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts:124:12

Check failure on line 58 in src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue

View workflow job for this annotation

GitHub Actions / test

src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts > CloudRunButtonWrapper > refreshes stale billing state on focus and restores Run

ReferenceError: showsSubscribeToRunPrompt is not defined ❯ GlobalWindow.refreshStaleBillingState src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue:58:9 ❯ GlobalWindow.#callDispatchEventListeners node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:305:41 ❯ GlobalWindow.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:142:35 ❯ GlobalWindow.#goThroughDispatchEventPhases node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:214:32 ❯ GlobalWindow.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:135:38 ❯ src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts:124:12

Check failure on line 58 in src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue

View workflow job for this annotation

GitHub Actions / test

src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts > CloudRunButtonWrapper > refreshes stale billing state on focus and restores Run

ReferenceError: showsSubscribeToRunPrompt is not defined ❯ GlobalWindow.refreshStaleBillingState src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue:58:9 ❯ GlobalWindow.#callDispatchEventListeners node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:305:41 ❯ GlobalWindow.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:142:35 ❯ GlobalWindow.#goThroughDispatchEventPhases node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:214:32 ❯ GlobalWindow.dispatchEvent node_modules/.pnpm/happy-dom@20.9.0/node_modules/happy-dom/src/event/EventTarget.ts:135:38 ❯ src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts:124:12
) {
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 })
}
Expand Down
Loading