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
25 changes: 25 additions & 0 deletions packages/account-core/src/core/billing/capabilities.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AutoTopUpSetup } from '@comfyorg/ingest-types'
import { describe, expect, it, vi } from 'vitest'

import type { SessionSnapshot } from '#/core/session'
Expand All @@ -7,6 +8,7 @@ import type { BillingSession, BillingTransport } from './billingContracts'
import {
CAPABILITY_REVISION_HEADER,
createCapabilitiesReader,
isAutoTopUpNotAvailable,
readCapabilityRevision
} from './capabilities'
import { createSessionBillingTransport } from './transport'
Expand Down Expand Up @@ -763,3 +765,26 @@ describe('readCapabilityRevision', () => {
expect(readCapabilityRevision(() => value)).toBeUndefined()
})
})

describe('isAutoTopUpNotAvailable', () => {
it.for<{ setup: AutoTopUpSetup | null; hidden: boolean }>([
{ setup: null, hidden: false },
{ setup: { state: 'ready' }, hidden: false },
{
setup: { state: 'frozen', reason: 'default_payment_method_missing' },
hidden: false
},
{
setup: { state: 'frozen', reason: 'billing_payment_pending' },
hidden: false
},
{ setup: { state: 'frozen', reason: 'billing_paused' }, hidden: true },
{ setup: { state: 'frozen', reason: 'payment_past_due' }, hidden: true },
{
setup: { state: 'unavailable', reason: 'billing_rail_not_supported' },
hidden: true
}
])('reads setup $setup as hidden: $hidden', ({ setup, hidden }) => {
expect(isAutoTopUpNotAvailable(setup)).toBe(hidden)
})
})
13 changes: 13 additions & 0 deletions packages/account-core/src/core/billing/capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* push concerns that need a document and a scheduler, so they belong to the
* view package that has both — not to a framework-free core.
*/
import type { AutoTopUpSetup } from '@comfyorg/ingest-types'
import {
zBillingCapabilities,
zBillingCapabilitiesResponse,
Expand Down Expand Up @@ -247,3 +248,15 @@ export function createCapabilitiesReader(
dispose: reader.dispose
}
}

const PAUSED_PLAN_REASONS: ReadonlySet<AutoTopUpSetup['reason']> = new Set([
'payment_past_due',
'billing_paused'
])

/** Auto top-up is hidden on every surface while setup is unavailable or frozen for a paused plan. */
export function isAutoTopUpNotAvailable(setup: AutoTopUpSetup | null): boolean {
if (setup === null) return false
if (setup.state === 'unavailable') return true
return setup.state === 'frozen' && PAUSED_PLAN_REASONS.has(setup.reason)
}
1 change: 1 addition & 0 deletions packages/account-core/src/core/billing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export {
CAPABILITIES_ROUTE,
CAPABILITY_REVISION_HEADER,
createCapabilitiesReader,
isAutoTopUpNotAvailable,
readCapabilityRevision
} from './capabilities'
export type {
Expand Down
16 changes: 11 additions & 5 deletions src/platform/billing/components/TopUpAutoTopUpCard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,17 @@ describe('TopUpAutoTopUpCard', () => {
expect(screen.queryByText(PROMPT)).toBeNull()
})

it('stays shown while setup is frozen for another reason', () => {
renderCard({ setup: { state: 'frozen', reason: 'billing_paused' } })

expect(screen.getByText(PROMPT)).toBeInTheDocument()
})
it.for([
{ reason: 'billing_paused', shown: false },
{ reason: 'default_payment_method_missing', shown: true }
] as const)(
'shows the prompt: $shown while setup is frozen for $reason',
({ reason, shown }) => {
renderCard({ setup: { state: 'frozen', reason } })

expect(screen.queryByText(PROMPT) !== null).toBe(shown)
}
)

it('reads the auto top-up status only when the served facts allow setup', () => {
renderCard({ canManage: false })
Expand Down
4 changes: 2 additions & 2 deletions src/platform/billing/components/TopUpAutoTopUpCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</template>

<script setup lang="ts">
import { isAutoTopUpNotAvailable } from '@comfyorg/account-core/billing'
import Button from '@comfyorg/design-system/button/Button'
import { computed, watch } from 'vue'

Expand All @@ -38,8 +39,7 @@ const canSetUp = computed(() => {
if (!autoTopUpEnabled.value) return false
if (!canManageAutoTopUp.value) return false
const setup = autoTopUpSetup.value
if (setup === null || setup.state === 'unavailable') return false
return !(setup.state === 'frozen' && setup.reason === 'payment_past_due')
return setup !== null && !isAutoTopUpNotAvailable(setup)
})

watch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,19 +435,23 @@ describe('BillingAutoTopUpCard', () => {
}
)

it('renders nothing when the server marks setup unavailable', async () => {
renderCard(configured, {
state: 'unavailable',
reason: 'billing_rail_not_supported'
})
it.for([
{ state: 'unavailable', reason: 'billing_rail_not_supported' },
{ state: 'frozen', reason: 'billing_paused' },
{ state: 'frozen', reason: 'payment_past_due' }
] as const)(
'renders nothing when setup is $state for $reason',
async (setup) => {
renderCard(configured, setup)

await vi.waitFor(() =>
expect(billingApi.getBillingAutoReload).toHaveBeenCalled()
)
expect(
screen.queryByTestId('billing-auto-top-up-card')
).not.toBeInTheDocument()
})
await vi.waitFor(() =>
expect(billingApi.getBillingAutoReload).toHaveBeenCalled()
)
expect(
screen.queryByTestId('billing-auto-top-up-card')
).not.toBeInTheDocument()
}
)

it('reports no view while the server hides the card, then one once it shows', async () => {
const { setupRef } = renderCard(configured, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<section
v-if="autoTopUpSetup?.state !== 'unavailable'"
v-if="!isAutoTopUpNotAvailable(autoTopUpSetup)"
class="flex flex-col"
data-testid="billing-auto-top-up-card"
>
Expand Down Expand Up @@ -82,6 +82,7 @@

<script setup lang="ts">
import type { AutoTopUpCardAction } from '@comfyorg/account-core/billing'
import { isAutoTopUpNotAvailable } from '@comfyorg/account-core/billing'
import { cn } from '@comfyorg/tailwind-utils'
import { computedAsync } from '@vueuse/core'
import { computed } from 'vue'
Expand Down Expand Up @@ -228,7 +229,7 @@ ensureRead()

useAutoTopUpCardViewed(() => {
const current = state.value
return autoTopUpSetup.value?.state !== 'unavailable' &&
return !isAutoTopUpNotAvailable(autoTopUpSetup.value) &&
isReady.value &&
current.kind === 'loaded'
? current.settings.status
Expand Down
Loading