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,11 +1,12 @@
import type { AutoReloadSettings } from '@comfyorg/ingest-types'
import type { AutoReloadSettings, AutoTopUpSetup } from '@comfyorg/ingest-types'
import { describe, expect, it } from 'vitest'

import {
autoTopUpOffView,
canSetUpAutoTopUp,
deriveAutoTopUpCard,
deriveAutoTopUpSection
deriveAutoTopUpSection,
isAutoTopUpNotAvailable
} from './autoTopUpCard'
import type {
AutoTopUpSectionFacts,
Expand Down Expand Up @@ -170,8 +171,36 @@ describe('canSetUpAutoTopUp', () => {
name: 'a setup frozen for past due',
facts: { setup: { state: 'frozen', reason: 'payment_past_due' } },
can: false
},
{
name: 'a setup frozen for paused billing',
facts: { setup: { state: 'frozen', reason: 'billing_paused' } },
can: false
}
])('$name: $can', ({ facts, can }) => {
expect(canSetUpAutoTopUp({ ...manager, ...facts })).toBe(can)
})
})

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)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,22 @@ export interface AutoTopUpSetupFacts {
setup: AutoTopUpSetup | null
}

/** Platform Billing's Turn on block: served `auto_top_up_enabled` and `can_manage_auto_top_up`, and a setup neither unavailable nor frozen for past due. */
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)
}

/** Platform Billing's Turn on block: served `auto_top_up_enabled` and `can_manage_auto_top_up`, and a served setup that is not Not available. */
export function canSetUpAutoTopUp(facts: AutoTopUpSetupFacts): boolean {
if (!facts.autoTopUpEnabled || !facts.canManageAutoTopUp) return false
const { setup } = facts
if (setup === null || setup.state === 'unavailable') return false
return !(setup.state === 'frozen' && setup.reason === 'payment_past_due')
return facts.setup !== null && !isAutoTopUpNotAvailable(facts.setup)
}

export type AutoTopUpOffView =
Expand Down
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 @@ -425,6 +425,7 @@ export type {
export {
autoTopUpOffView,
canSetUpAutoTopUp,
isAutoTopUpNotAvailable,
deriveAutoTopUpCard,
deriveAutoTopUpSection
} from './billingTab/autoTopUpCard'
Expand Down
10 changes: 6 additions & 4 deletions packages/account-ui/src/billing/tab/BillingAutoTopUpCard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,12 @@ describe('BillingAutoTopUpCard', () => {
expect(onRetry).toHaveBeenCalledOnce()
})

it('renders nothing when the server marks setup unavailable', () => {
renderCard(loaded(), {
setup: { 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', (setup) => {
renderCard(loaded({ status: 'on' }), { setup })

expect(screen.queryByTestId('billing-auto-top-up-card')).toBeNull()
})
Expand Down
3 changes: 2 additions & 1 deletion packages/account-ui/src/billing/tab/BillingAutoTopUpCard.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<section
v-if="setup?.state !== 'unavailable'"
v-if="!isAutoTopUpNotAvailable(setup)"
:class="cn('flex flex-col', classes?.root)"
data-testid="billing-auto-top-up-card"
>
Expand Down Expand Up @@ -114,6 +114,7 @@ import type {
AutoTopUpOffView,
AutoTopUpOffViewFacts
} from '@comfyorg/account-core/billing'
import { isAutoTopUpNotAvailable } from '@comfyorg/account-core/billing'
import type { ButtonVariants } from '@comfyorg/design-system/button.variants'
import Skeleton from '@comfyorg/design-system/skeleton/Skeleton'
import { cn } from '@comfyorg/tailwind-utils'
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 @@ -101,11 +101,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
15 changes: 8 additions & 7 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 { canSetUpAutoTopUp } from '@comfyorg/account-core/billing'
import Button from '@comfyorg/design-system/button/Button'
import { computed, watch } from 'vue'

Expand All @@ -34,13 +35,13 @@ const { autoTopUpEnabled, canManageAutoTopUp, autoTopUpSetup } =
useBillingCapabilities()
const { state, ensureRead } = useBillingAutoTopUpCard()

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')
})
const canSetUp = computed(() =>
canSetUpAutoTopUp({
autoTopUpEnabled: autoTopUpEnabled.value,
canManageAutoTopUp: canManageAutoTopUp.value,
setup: autoTopUpSetup.value
})
)

watch(
canSetUp,
Expand Down
7 changes: 5 additions & 2 deletions src/platform/billing/composables/useAutoTopUpCardSection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { autoTopUpOffView } from '@comfyorg/account-core/billing'
import {
autoTopUpOffView,
isAutoTopUpNotAvailable
} from '@comfyorg/account-core/billing'
import { autoTopUpCardView } from '@comfyorg/account-ui/billing'
import { reportError } from '@comfyorg/telemetry/reportError'
import { computedAsync } from '@vueuse/core'
Expand Down Expand Up @@ -31,7 +34,7 @@ export function useAutoTopUpCardSection(shown: MaybeRefOrGetter<boolean>) {
useAutoTopUpCardViewed(() => {
const current = state.value
return toValue(shown) &&
autoTopUpSetup.value?.state !== 'unavailable' &&
!isAutoTopUpNotAvailable(autoTopUpSetup.value) &&
isReady.value &&
current.kind === 'loaded'
? current.settings.status
Expand Down
Loading