diff --git a/src/locales/en/main.json b/src/locales/en/main.json index b58e3424435..086facbb2aa 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -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", diff --git a/src/platform/billing/components/TopUpAutoTopUpCard.test.ts b/src/platform/billing/components/TopUpAutoTopUpCard.test.ts new file mode 100644 index 00000000000..882a192c2b6 --- /dev/null +++ b/src/platform/billing/components/TopUpAutoTopUpCard.test.ts @@ -0,0 +1,127 @@ +import type { AutoTopUpSetup } from '@comfyorg/ingest-types' +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 | { kind: 'loading' | 'unavailable' } +} + +const shown: Served = { + enabled: true, + canManage: true, + setup: { state: 'ready' }, + card: loadedAutoTopUp('off') +} + +function renderCard(served: Partial = {}) { + 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>([ + { 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' + }) + ) + }) +}) diff --git a/src/platform/billing/components/TopUpAutoTopUpCard.vue b/src/platform/billing/components/TopUpAutoTopUpCard.vue new file mode 100644 index 00000000000..b31c83c10df --- /dev/null +++ b/src/platform/billing/components/TopUpAutoTopUpCard.vue @@ -0,0 +1,63 @@ + + + diff --git a/src/platform/billing/components/TopUpCreditsDialogContentLegacy.test.ts b/src/platform/billing/components/TopUpCreditsDialogContentLegacy.test.ts index 5dfa3e7e858..1c8b24912c1 100644 --- a/src/platform/billing/components/TopUpCreditsDialogContentLegacy.test.ts +++ b/src/platform/billing/components/TopUpCreditsDialogContentLegacy.test.ts @@ -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' @@ -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 @@ -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 } } } @@ -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() diff --git a/src/platform/billing/components/TopUpCreditsDialogContentLegacy.vue b/src/platform/billing/components/TopUpCreditsDialogContentLegacy.vue index 4d2922cbdc8..4d6ff0f699f 100644 --- a/src/platform/billing/components/TopUpCreditsDialogContentLegacy.vue +++ b/src/platform/billing/components/TopUpCreditsDialogContentLegacy.vue @@ -68,15 +68,18 @@

- +
+ + +
{ }) } + 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 diff --git a/src/platform/billing/components/TopUpCreditsDialogContentWorkspace.vue b/src/platform/billing/components/TopUpCreditsDialogContentWorkspace.vue index 4397693c7a0..ee6300fb8f3 100644 --- a/src/platform/billing/components/TopUpCreditsDialogContentWorkspace.vue +++ b/src/platform/billing/components/TopUpCreditsDialogContentWorkspace.vue @@ -295,6 +295,10 @@ {{ $t('credits.topUp.paymentProcessingBody') }}

+