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
4 changes: 4 additions & 0 deletions src/locales/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -3042,6 +3042,10 @@
"verifyTitle": "Verify your payment",
"startOver": "Start over",
"viewPricing": "View pricing details",
"autoTopUp": {
"prompt": "Running low on credits? Turn on auto top-up.",
"setUp": "Set up"
},
"youPay": "Amount (USD)",
"youGet": "Credits",
"buyCredits": "Continue to payment",
Expand Down
127 changes: 127 additions & 0 deletions src/platform/billing/components/TopUpAutoTopUpCard.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import type { AutoTopUpSetup } from '@comfyorg/ingest-types'

Check warning on line 1 in src/platform/billing/components/TopUpAutoTopUpCard.test.ts

View workflow job for this annotation

GitHub Actions / fallow / fallow

Boundary violation

Import from zone 'unclassified' to zone 'ingest-api/domain' is not allowed. src/platform/billing/components/TopUpAutoTopUpCard.test.ts -> packages/ingest-types/src/index.ts Route the import through an allowed zone or restructure the dependency.
import { render, screen } from '@testing-library/vue'
import userEvent from '@testing-library/user-event'
import { describe, expect, it, vi } from 'vitest'
import { computed } from 'vue'
import { createI18n } from 'vue-i18n'

import enMessages from '@/locales/en/main.json'
import { useTelemetry } from '@comfyorg/telemetry'
import {
autoTopUpCardState,
loadedAutoTopUp
} from '@/platform/billing/composables/__mocks__/useBillingAutoTopUpCard'
import {
autoTopUpEnabledFact,
canManageAutoTopUpFact
} from '@/platform/billing/composables/__mocks__/useBillingCapabilities'
import { useBillingAutoTopUpCard } from '@/platform/billing/composables/useBillingAutoTopUpCard'
import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'

import TopUpAutoTopUpCard from './TopUpAutoTopUpCard.vue'

vi.mock(import('@/platform/billing/composables/useBillingCapabilities'))
vi.mock(import('@/platform/billing/composables/useBillingAutoTopUpCard'))
vi.mock(import('@comfyorg/telemetry'))
vi.mock(import('@/platform/workspace/utils/platformLink'), () => ({
platformLink: (path: string, correlationId?: string) =>
`https://platform.test${path}?correlation_id=${correlationId}`
}))

const PROMPT = 'Running low on credits? Turn on auto top-up.'

type Served = {
enabled: boolean
canManage: boolean
setup: AutoTopUpSetup | null
card: ReturnType<typeof loadedAutoTopUp> | { kind: 'loading' | 'unavailable' }
}

const shown: Served = {
enabled: true,
canManage: true,
setup: { state: 'ready' },
card: loadedAutoTopUp('off')
}

function renderCard(served: Partial<Served> = {}) {
const { enabled, canManage, setup, card } = { ...shown, ...served }
const capabilities = useBillingCapabilities()
capabilities.autoTopUpEnabled = autoTopUpEnabledFact(enabled)
capabilities.canManageAutoTopUp = canManageAutoTopUpFact(canManage)
capabilities.autoTopUpSetup = computed(() => setup)
autoTopUpCardState.value = card
render(TopUpAutoTopUpCard, {
global: {
plugins: [
createI18n({
legacy: false,
locale: 'en',
messages: { en: enMessages }
})
]
}
})
}

describe('TopUpAutoTopUpCard', () => {
it('offers a manager with auto top-up off to set it up on the billing site', () => {
renderCard()

expect(screen.getByText(PROMPT)).toBeInTheDocument()
expect(screen.getByRole('link', { name: 'Set up' })).toHaveAttribute(
'href',
expect.stringContaining('https://platform.test/profile/billing')
)
})

it.for<{ name: string } & Partial<Served>>([
{ name: 'auto top-up is not served', enabled: false },
{ name: 'the viewer cannot manage it', canManage: false },
{ name: 'no setup is served', setup: null },
{
name: 'setup is unavailable',
setup: { state: 'unavailable', reason: 'workspace_blocked' }
},
{
name: 'setup is frozen by a past-due payment',
setup: { state: 'frozen', reason: 'payment_past_due' }
},
{ name: 'auto top-up is already on', card: loadedAutoTopUp('on') },
{ name: 'auto top-up has failed', card: loadedAutoTopUp('failed') },
{ name: 'the status is still loading', card: { kind: 'loading' } },
{ name: 'the status read failed', card: { kind: 'unavailable' } }
])('shows nothing when $name', ({ name: _, ...served }) => {
renderCard(served)

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('reads the auto top-up status only when the served facts allow setup', () => {
renderCard({ canManage: false })
expect(useBillingAutoTopUpCard().ensureRead).not.toHaveBeenCalled()

renderCard()
expect(useBillingAutoTopUpCard().ensureRead).toHaveBeenCalledOnce()
})

it('reports the hand-off to the billing site', async () => {
renderCard()

await userEvent.click(screen.getByRole('link', { name: 'Set up' }))

expect(useTelemetry()?.trackBillingEvent).toHaveBeenCalledWith(
expect.objectContaining({
operation: 'platform_handoff',
target: 'auto_top_up',
action: 'turn_on'
})
)
})
})
63 changes: 63 additions & 0 deletions src/platform/billing/components/TopUpAutoTopUpCard.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<template>
<section
v-if="visible"
class="flex items-center justify-between gap-4 rounded-2xl border border-border/60 p-4"
>
<p class="m-0 text-sm text-foreground">
{{ $t('credits.topUp.autoTopUp.prompt') }}
</p>
<Button
as="a"
:href
target="_blank"
rel="noopener"
variant="secondary"
size="md"
class="shrink-0 no-underline"
@click="track"
>
{{ $t('credits.topUp.autoTopUp.setUp') }}
<i class="icon-[lucide--external-link]" aria-hidden="true" />
</Button>
</section>
</template>

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

import { useBillingAutoTopUpCard } from '@/platform/billing/composables/useBillingAutoTopUpCard'
import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
import { usePlatformHandoff } from '@/platform/billing/composables/usePlatformHandoff'

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')
})

watch(
canSetUp,
(allowed) => {
if (allowed) ensureRead()
},
{ immediate: true }
)

const visible = computed(() => {
if (!canSetUp.value) return false
const read = state.value
return read.kind === 'loaded' && read.settings.status === 'off'
})

const { href, track } = usePlatformHandoff('/profile/billing', {
target: 'auto_top_up',
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
action: 'turn_on'
})
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import { createI18n } from 'vue-i18n'
import enMessages from '@/locales/en/main.json'

import { useToast } from '@/components/ui/toast/toastStore'
import {
autoTopUpCardState,
loadedAutoTopUp
} from '@/platform/billing/composables/__mocks__/useBillingAutoTopUpCard'
import {
autoTopUpEnabledFact,
canManageAutoTopUpFact
} from '@/platform/billing/composables/__mocks__/useBillingCapabilities'
import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
import { useBillingRouting } from '@/platform/billing/composables/useBillingRouting'
import { useSubscription } from '@/platform/billing/subscription/composables/useSubscription'
Expand All @@ -26,6 +35,8 @@ vi.mock(import('@/platform/billing/composables/useBillingContext'))

vi.mock(import('@/platform/billing/composables/useBillingCapabilities'))

vi.mock(import('@/platform/billing/composables/useBillingAutoTopUpCard'))

vi.mock(import('@/platform/distribution/types'), () => ({
isCloud: true,
isDesktop: false
Expand Down Expand Up @@ -68,7 +79,8 @@ const i18n = createI18n({
contactUs: 'Contact us',
viewPricing: 'View pricing',
insufficientWorkflowMessage: 'Insufficient credits',
buyCredits: 'Continue to payment'
buyCredits: 'Continue to payment',
autoTopUp: enMessages.credits.topUp.autoTopUp
}
}
}
Expand Down Expand Up @@ -237,6 +249,26 @@ describe('TopUpCreditsDialogContentLegacy', () => {
}
)

it('offers auto top-up set-up above Continue to payment when every served fact allows it', () => {
const capabilities = useBillingCapabilities()
capabilities.autoTopUpEnabled = autoTopUpEnabledFact(true)
capabilities.canManageAutoTopUp = canManageAutoTopUpFact(true)
capabilities.autoTopUpSetup = computed(() => ({ state: 'ready' as const }))
autoTopUpCardState.value = loadedAutoTopUp('off')

renderDialog()

const setUp = screen.getByRole('link', { name: 'Set up' })
expect(
screen.getByText('Running low on credits? Turn on auto top-up.')
).toBeInTheDocument()
expect(
setUp.compareDocumentPosition(
screen.getByRole('button', { name: 'Continue to payment' })
) & Node.DOCUMENT_POSITION_FOLLOWING
).toBeTruthy()
})

it('shows Plan & Credits after a successful Cloud purchase', async () => {
renderDialog()
await clickBuyCredits()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,18 @@
</p>

<div class="flex flex-col gap-8 p-8">
<Button
:disabled="!isValidAmount || loading"
:loading
variant="primary"
size="lg"
@click="handleBuy"
>
{{ $t('credits.topUp.buyCredits') }}
</Button>
<div class="flex flex-col gap-4">
<TopUpAutoTopUpCard />
<Button
:disabled="!isValidAmount || loading"
:loading
variant="primary"
size="lg"
@click="handleBuy"
>
{{ $t('credits.topUp.buyCredits') }}
</Button>
</div>
<div class="flex items-center justify-center gap-1">
<a
:href="pricingUrl"
Expand All @@ -101,6 +104,7 @@ import { creditsToUsd, usdToCredits } from '@/base/credits/comfyCredits'
import Button from '@comfyorg/design-system/button/Button'
import DialogTitle from '@comfyorg/design-system/dialog/DialogTitle'
import TopUpAmountFields from './TopUpAmountFields.vue'
import TopUpAutoTopUpCard from './TopUpAutoTopUpCard.vue'
import { useToast } from '@/components/ui/toast/toastStore'
import { useAuthActions } from '@/composables/auth/useAuthActions'
import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ import { WorkspaceApiError } from '@/platform/remote/ingest/ingestApiError'
import type { CreateTopupResponse } from '@comfyorg/billing-core'
import { billingOperation } from '@/platform/billing/composables/billingOperationTestUtils'
import type { BillingOperation } from '@/platform/billing/composables/billingOperationTestUtils'
import {
autoTopUpCardState,
loadedAutoTopUp
} from '@/platform/billing/composables/__mocks__/useBillingAutoTopUpCard'
import {
autoTopUpEnabledFact,
canManageAutoTopUpFact
} from '@/platform/billing/composables/__mocks__/useBillingCapabilities'
import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
import { mockBillingContext } from '@/utils/__tests__/mockBillingContext'

Expand Down Expand Up @@ -68,6 +76,8 @@ vi.mock(import('@/platform/billing/composables/useBillingContext'))

vi.mock(import('@/platform/billing/composables/useBillingCapabilities'))

vi.mock(import('@/platform/billing/composables/useBillingAutoTopUpCard'))

vi.mock(import('@/platform/settings/composables/useSettingsDialog'))

vi.mock(import('@comfyorg/telemetry'))
Expand Down Expand Up @@ -2510,6 +2520,52 @@ describe('TopUpCreditsDialogContentWorkspace', () => {
})
}

describe('the auto top-up card', () => {
const AUTO_TOP_UP_PROMPT = 'Running low on credits? Turn on auto top-up.'

beforeEach(() => {
const capabilities = useBillingCapabilities()
capabilities.autoTopUpEnabled = autoTopUpEnabledFact(true)
capabilities.canManageAutoTopUp = canManageAutoTopUpFact(true)
capabilities.autoTopUpSetup = computed(() => ({
state: 'ready' as const
}))
autoTopUpCardState.value = loadedAutoTopUp('off')
})

it('sits above Add credits on the amount step', () => {
renderDialog()

const setUp = screen.getByRole('link', { name: 'Set up' })
expect(screen.getByText(AUTO_TOP_UP_PROMPT)).toBeInTheDocument()
expect(
setUp.compareDocumentPosition(
screen.getByRole('button', { name: 'Add credits' })
) & Node.DOCUMENT_POSITION_FOLLOWING
).toBeTruthy()
})

it('is gone on the payment step', async () => {
renderDialog()

await clickAddCredits()

expect(
screen.getByRole('button', { name: 'Pay $50.00' })
).toBeInTheDocument()
expect(screen.queryByText(AUTO_TOP_UP_PROMPT)).not.toBeInTheDocument()
})

it('gives way to the payment processing notice', async () => {
vi.mocked(useFeatureFlags().flags).unifiedBillingSettingsEnabled = true
serveProcessingCheckout(2500)
renderDialog()

await screen.findByRole('status')
expect(screen.queryByText(AUTO_TOP_UP_PROMPT)).not.toBeInTheDocument()
})
})

describe('a top-up payment still processing', () => {
it('keeps add credits available and names the served amount', async () => {
vi.mocked(useFeatureFlags().flags).unifiedBillingSettingsEnabled = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@
{{ $t('credits.topUp.paymentProcessingBody') }}
</p>
</div>
<TopUpAutoTopUpCard
v-if="step === 'amount' && !processingPayment"
class="mb-2"
/>
<Button
v-if="step === 'confirm' && topupActionUrl && canTopUp"
variant="primary"
Expand Down Expand Up @@ -381,6 +385,7 @@ import {
import Button from '@comfyorg/design-system/button/Button'
import DialogTitle from '@comfyorg/design-system/dialog/DialogTitle'
import TopUpAmountFields from './TopUpAmountFields.vue'
import TopUpAutoTopUpCard from './TopUpAutoTopUpCard.vue'
import { watchForTopupBalanceUpdate } from '@/platform/billing/composables/topupBalanceRefresh'
import { useTopUpPlanCreditCopy } from '@/platform/billing/composables/useTopUpPlanCreditCopy'
import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
Expand Down
Loading
Loading