diff --git a/packages/account-core/src/core/billing/billingTab/creditsFlyout.test.ts b/packages/account-core/src/core/billing/billingTab/creditsFlyout.test.ts new file mode 100644 index 00000000000..3e4b5d65908 --- /dev/null +++ b/packages/account-core/src/core/billing/billingTab/creditsFlyout.test.ts @@ -0,0 +1,135 @@ +import { describe, expect, it } from 'vitest' + +import { deriveCreditsFlyout } from './creditsFlyout' +import type { CreditsFlyoutFacts } from './creditsFlyout' + +const active: CreditsFlyoutFacts = { + loading: false, + balance: { + amountMicros: 3600, + effectiveBalanceMicros: 3600, + cloudCreditBalanceMicros: 2630, + prepaidBalanceMicros: 970 + }, + planKind: 'active', + billingStatus: 'paid', + periodCreditGrant: { state: 'amount', credits: 7400, period: 'MONTHLY' }, + nextCreditRefillAt: '2026-10-28T00:00:00Z', + endDate: null +} + +const facts = (over: Partial) => ({ ...active, ...over }) + +describe('deriveCreditsFlyout', () => { + it('shows the plan pool from period_credit_grant with its refill date', () => { + expect(deriveCreditsFlyout(active)).toEqual({ + kind: 'plan', + availableCents: 3600, + planCents: 2630, + planPoolCredits: 7400, + topUpCents: 970, + topUpInUse: false, + showUsage: true, + planDate: { kind: 'refills', date: '2026-10-28T00:00:00Z' }, + cadence: 'monthly' + }) + }) + + it('keeps the year for a yearly grant', () => { + const state = deriveCreditsFlyout( + facts({ + periodCreditGrant: { state: 'amount', credits: 88800, period: 'ANNUAL' } + }) + ) + expect(state).toMatchObject({ kind: 'plan', cadence: 'yearly' }) + }) + + it.for([ + { case: 'not served', grant: null }, + { case: 'unknown', grant: { state: 'unknown' as const } } + ])('has no plan total when the grant is $case', ({ grant }) => { + expect( + deriveCreditsFlyout(facts({ periodCreditGrant: grant })) + ).toMatchObject({ kind: 'plan', planPoolCredits: null, cadence: null }) + }) + + it.for([ + { case: 'no plan', over: { planKind: 'noPlan' as const } }, + { case: 'an ended plan', over: { planKind: 'ended' as const } }, + { + case: 'a proven zero grant', + over: { periodCreditGrant: { state: 'none' as const } } + } + ])('shows the served top-up balance only for $case', ({ over }) => { + expect(deriveCreditsFlyout(facts(over))).toEqual({ + kind: 'noPlan', + topUpCents: 970 + }) + }) + + it('stops top-up at 0 with no plan and an overdrawn balance', () => { + expect( + deriveCreditsFlyout( + facts({ + planKind: 'noPlan', + balance: { + amountMicros: -120, + effectiveBalanceMicros: -120, + prepaidBalanceMicros: 0 + } + }) + ) + ).toEqual({ kind: 'noPlan', topUpCents: 0 }) + }) + + it('stops both pools at 0 when overdrawn', () => { + const state = deriveCreditsFlyout( + facts({ balance: { ...active.balance!, effectiveBalanceMicros: -50 } }) + ) + expect(state).toMatchObject({ kind: 'plan', planCents: 0, topUpCents: 0 }) + }) + + it('keeps the bar and refills after payment while paused', () => { + expect( + deriveCreditsFlyout(facts({ billingStatus: 'paused' })) + ).toMatchObject({ + planCents: 0, + showUsage: true, + planDate: { kind: 'afterPayment' } + }) + }) + + it('shows the end date for an ending plan', () => { + expect( + deriveCreditsFlyout( + facts({ planKind: 'ending', endDate: '2026-12-01T00:00:00Z' }) + ) + ).toMatchObject({ + planDate: { kind: 'availableUntil', date: '2026-12-01T00:00:00Z' } + }) + }) + + it('loads until the first balance arrives', () => { + expect( + deriveCreditsFlyout(facts({ loading: true, balance: null })) + ).toEqual({ kind: 'loading' }) + }) + + it('keeps the last balance while a refresh loads', () => { + expect(deriveCreditsFlyout(facts({ loading: true }))).toMatchObject({ + kind: 'plan' + }) + }) + + it.for([ + { case: 'missing', balance: null }, + { + case: 'not authoritative', + balance: { amountMicros: 10, effectiveBalanceAuthoritative: false } + } + ])('is unavailable when the balance is $case', ({ balance }) => { + expect(deriveCreditsFlyout(facts({ balance }))).toEqual({ + kind: 'unavailable' + }) + }) +}) diff --git a/packages/account-core/src/core/billing/billingTab/creditsFlyout.ts b/packages/account-core/src/core/billing/billingTab/creditsFlyout.ts new file mode 100644 index 00000000000..a0e469d0877 --- /dev/null +++ b/packages/account-core/src/core/billing/billingTab/creditsFlyout.ts @@ -0,0 +1,49 @@ +import { deriveCreditsCard } from './creditsCard' +import type { CreditsCardFacts, PlanCreditsState } from './creditsCard' + +export interface CreditsFlyoutFacts extends Omit< + CreditsCardFacts, + 'planPoolCredits' +> { + loading: boolean +} + +export type CreditsFlyoutPlan = PlanCreditsState & { + cadence: 'monthly' | 'yearly' | null +} + +export type CreditsFlyoutState = + | { kind: 'loading' } + | { kind: 'unavailable' } + | { kind: 'noPlan'; topUpCents: number } + | CreditsFlyoutPlan + +/** Maps the served balance and plan state onto the credits flyout breakdown. */ +export function deriveCreditsFlyout( + facts: CreditsFlyoutFacts +): CreditsFlyoutState { + if (facts.loading && !facts.balance) return { kind: 'loading' } + // The plan total is only ever the served period_credit_grant. + const card = deriveCreditsCard({ ...facts, planPoolCredits: null }) + if (card.kind === 'unavailable') return card + // Pools stop at 0; the Credits row carries the signed total. + if (card.kind === 'noPlan') { + const overdrawn = card.availableCents < 0 + const prepaid = facts.balance?.prepaidBalanceMicros ?? 0 + return { kind: 'noPlan', topUpCents: overdrawn ? 0 : prepaid } + } + return { + ...card, + // The flyout keeps the bar while paused (Figma Paused row). + showUsage: true, + cadence: cadenceOf(facts.periodCreditGrant?.period) + } +} + +function cadenceOf( + period: NonNullable['period'] +): CreditsFlyoutPlan['cadence'] { + if (period === 'MONTHLY') return 'monthly' + if (period === 'ANNUAL') return 'yearly' + return null +} diff --git a/packages/account-core/src/core/billing/index.ts b/packages/account-core/src/core/billing/index.ts index be7a5b4b4b8..cf8581e8e85 100644 --- a/packages/account-core/src/core/billing/index.ts +++ b/packages/account-core/src/core/billing/index.ts @@ -403,6 +403,12 @@ export type { PlanCreditsState } from './billingTab/creditsCard' export { addCreditsEmphasis, deriveCreditsCard } from './billingTab/creditsCard' +export type { + CreditsFlyoutFacts, + CreditsFlyoutPlan, + CreditsFlyoutState +} from './billingTab/creditsFlyout' +export { deriveCreditsFlyout } from './billingTab/creditsFlyout' export type { PlanCheckoutBanner } from './billingTab/planCheckoutBanner' export { planCheckoutBannerFor } from './billingTab/planCheckoutBanner' export type { diff --git a/packages/account-ui/src/billing/index.ts b/packages/account-ui/src/billing/index.ts index 587452af656..3d50e183bb2 100644 --- a/packages/account-ui/src/billing/index.ts +++ b/packages/account-ui/src/billing/index.ts @@ -53,10 +53,14 @@ export type { AddCreditsUi } from './addCredits/addCreditsPresentation' export { default as AddCreditsDialog } from './addCredits/AddCreditsDialog.vue' export type { PlanRowCopy } from './tab/planRowCopy' export { default as BillingPlanRow } from './tab/BillingPlanRow.vue' -export type { CreditsCardCopy } from './tab/creditsCardCopy' +export type { + CreditsBreakdownCopy, + CreditsCardCopy +} from './tab/creditsCardCopy' export { CREDITS_PER_USD } from './tab/creditsCardCopy' export { default as AddCreditsAction } from './tab/AddCreditsAction.vue' export { default as BillingCreditsCard } from './tab/BillingCreditsCard.vue' +export { default as BillingCreditsBreakdown } from './tab/BillingCreditsBreakdown.vue' export type { AutoTopUpActionLabel, AutoTopUpCardCopy, diff --git a/packages/account-ui/src/billing/tab/BillingCreditsBreakdown.test.ts b/packages/account-ui/src/billing/tab/BillingCreditsBreakdown.test.ts new file mode 100644 index 00000000000..b38b74bec1c --- /dev/null +++ b/packages/account-ui/src/billing/tab/BillingCreditsBreakdown.test.ts @@ -0,0 +1,113 @@ +import { render, screen } from '@testing-library/vue' +import { describe, expect, it } from 'vitest' + +import type { + CreditsFlyoutPlan, + CreditsFlyoutState +} from '@comfyorg/account-core/billing' + +import BillingCreditsBreakdown from './BillingCreditsBreakdown.vue' +import { creditsBreakdownCopy } from './__fixtures__/creditsCardCopy' + +const PLAN: CreditsFlyoutPlan = { + kind: 'plan', + availableCents: 1100, + planCents: 1000, + planPoolCredits: 8440, + topUpCents: 100, + topUpInUse: false, + showUsage: true, + planDate: { kind: 'refills', date: '2026-10-28T00:00:00Z' }, + cadence: 'monthly' +} + +function renderBreakdown(state: CreditsFlyoutState, note?: string) { + render(BillingCreditsBreakdown, { + props: { state, copy: creditsBreakdownCopy, locale: 'en' }, + slots: note ? { topUpNote: `

${note}

` } : {} + }) +} + +const textOf = (testId: string) => + screen.queryByTestId(testId)?.textContent.trim() ?? null +const plan = () => textOf('credits-breakdown-plan') +const topUp = () => textOf('credits-breakdown-top-up') +const meter = () => screen.queryByRole('meter') +const shows = (text: string) => screen.queryByText(text) !== null + +describe('BillingCreditsBreakdown', () => { + it('shows plan credits of the plan total, the used bar, the refill date and top-up', () => { + renderBreakdown(PLAN) + + expect(shows('Plan credits')).toBe(true) + expect(plan()).toBe('2,110 of 8,440') + expect(meter()?.getAttribute('aria-valuenow')).toBe('75') + expect(shows('Refills October 28')).toBe(true) + expect(shows('Top-up credits')).toBe(true) + expect(topUp()).toBe('211') + }) + + it('keeps the year on a yearly plan', () => { + renderBreakdown({ ...PLAN, cadence: 'yearly' }) + + expect(shows('Refills October 28, 2026')).toBe(true) + }) + + it('fills the bar when the plan pool is used up', () => { + renderBreakdown({ ...PLAN, planCents: 0 }) + + expect(plan()).toBe('0 of 8,440') + expect(meter()?.getAttribute('aria-valuenow')).toBe('100') + }) + + it('refills after the payment goes through', () => { + renderBreakdown({ ...PLAN, planDate: { kind: 'afterPayment' } }) + + expect(shows('Refills when the payment goes through')).toBe(true) + }) + + it.for([ + { case: 'not served', planPoolCredits: null }, + { case: 'zero', planPoolCredits: 0 } + ])( + 'shows remaining plan credits without a total or bar when the total is $case', + ({ planPoolCredits }) => { + renderBreakdown({ ...PLAN, planPoolCredits }) + + expect(plan()).toBe('2,110') + expect(meter()).toBeNull() + } + ) + + it('shows top-up credits only, with the host note, when there is no plan', () => { + renderBreakdown( + { kind: 'noPlan', topUpCents: 100 }, + "Buying credits doesn't start a Cloud plan." + ) + + expect(shows('Plan credits')).toBe(false) + expect(meter()).toBeNull() + expect(topUp()).toBe('211') + expect(shows("Buying credits doesn't start a Cloud plan.")).toBe(true) + }) + + it('shows dashes and no bar when the balance is unavailable', () => { + renderBreakdown({ kind: 'unavailable' }) + + expect(plan()).toBe('—') + expect(topUp()).toBe('—') + expect(meter()).toBeNull() + expect(screen.getByRole('alert').textContent.trim()).toBe( + "Couldn't load your balance." + ) + }) + + it('shows skeletons while loading', () => { + renderBreakdown({ kind: 'loading' }) + + expect(shows('Plan credits')).toBe(true) + expect(plan()).toBeNull() + expect(topUp()).toBeNull() + expect(meter()).toBeNull() + }) +}) diff --git a/packages/account-ui/src/billing/tab/BillingCreditsBreakdown.vue b/packages/account-ui/src/billing/tab/BillingCreditsBreakdown.vue new file mode 100644 index 00000000000..ba856ac5943 --- /dev/null +++ b/packages/account-ui/src/billing/tab/BillingCreditsBreakdown.vue @@ -0,0 +1,135 @@ + + + diff --git a/packages/account-ui/src/billing/tab/__fixtures__/creditsCardCopy.ts b/packages/account-ui/src/billing/tab/__fixtures__/creditsCardCopy.ts index 0db827864fb..404884fe9dd 100644 --- a/packages/account-ui/src/billing/tab/__fixtures__/creditsCardCopy.ts +++ b/packages/account-ui/src/billing/tab/__fixtures__/creditsCardCopy.ts @@ -1,4 +1,7 @@ -import type { CreditsCardCopy } from '#/billing/tab/creditsCardCopy' +import type { + CreditsBreakdownCopy, + CreditsCardCopy +} from '#/billing/tab/creditsCardCopy' const ADD_CREDITS_REASONS: Record = { 'credits.addCreditsRefusal.topupPurchasesPaused': @@ -34,3 +37,9 @@ export const creditsCardCopy: CreditsCardCopy = { addCredits: 'Add credits', addCreditsReason: (reasonKey) => ADD_CREDITS_REASONS[reasonKey] ?? reasonKey } + +export const creditsBreakdownCopy: CreditsBreakdownCopy = { + ...creditsCardCopy, + planCredits: 'Plan credits', + topUpCredits: 'Top-up credits' +} diff --git a/packages/account-ui/src/billing/tab/creditsCardCopy.ts b/packages/account-ui/src/billing/tab/creditsCardCopy.ts index 66f1609b0e3..f248bb608c7 100644 --- a/packages/account-ui/src/billing/tab/creditsCardCopy.ts +++ b/packages/account-ui/src/billing/tab/creditsCardCopy.ts @@ -49,15 +49,34 @@ export function planUsedPercent(plan: PlanCreditsState): number | null { return Math.round(Math.min(Math.max(1 - remaining, 0), 1) * 100) } -/** The plan pool's refill or availability line. */ +/** Host-translated strings for the credits flyout breakdown. */ +export interface CreditsBreakdownCopy extends Pick< + CreditsCardCopy, + | 'planOfPool' + | 'refills' + | 'refillsAfterPayment' + | 'availableUntil' + | 'unavailable' +> { + readonly planCredits: string + readonly topUpCredits: string +} + +type PlanDateCopy = Pick< + CreditsCardCopy, + 'refills' | 'refillsAfterPayment' | 'availableUntil' +> + +/** The plan pool's refill or availability line; a null cadence keeps the year. */ export function planDateLine( plan: PlanCreditsState, - copy: CreditsCardCopy, - locale: string + copy: PlanDateCopy, + locale: string, + cadence: 'monthly' | 'yearly' | null = null ): string { if (plan.planDate.kind === 'afterPayment') return copy.refillsAfterPayment const date = plan.planDate.date - ? formatPlanDate(plan.planDate.date, locale, null) + ? formatPlanDate(plan.planDate.date, locale, cadence) : '' if (!date) return '' return plan.planDate.kind === 'refills' diff --git a/src/locales/en/main.json b/src/locales/en/main.json index 5f91e84fade..23de588cb3d 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -4176,6 +4176,9 @@ "credits": "Credits", "balanceUnavailable": "Couldn't load your balance.", "billingSettings": "Billing settings", + "planCredits": "Plan credits", + "topUpCredits": "Top-up credits", + "paymentProcessing": "Payment processing…", "planStatus": { "noPlan": "No plan", "paused": "Paused", diff --git a/src/platform/account/components/ProfileMenuCreditsFlyout.test.ts b/src/platform/account/components/ProfileMenuCreditsFlyout.test.ts index 3282607c9d5..62f529bebec 100644 --- a/src/platform/account/components/ProfileMenuCreditsFlyout.test.ts +++ b/src/platform/account/components/ProfileMenuCreditsFlyout.test.ts @@ -1,3 +1,4 @@ +import type { PlanRowState } from '@comfyorg/account-core/billing' import type { BillingTopUpDenial } from '@comfyorg/billing-core' import { render, screen } from '@testing-library/vue' import userEvent from '@testing-library/user-event' @@ -6,14 +7,21 @@ import { computed } from 'vue' import { createI18n } from 'vue-i18n' import enMessages from '@/locales/en/main.json' +import { useBillingPlanRow } from '@/platform/billing/components/billingTab/useBillingPlanRow' +import type { BillingContext } from '@/platform/billing/composables/types' import { paymentRecoveryAvailableFact } from '@/platform/billing/composables/__mocks__/useBillingCapabilities' import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities' +import { useBillingContext } from '@/platform/billing/composables/useBillingContext' import { useBillingDialogs } from '@/platform/billing/composables/useBillingDialogs' import { usePaymentRecoveryDialog } from '@/platform/billing/composables/usePaymentRecoveryDialog' +import { useTopUpPlanCreditCopy } from '@/platform/billing/composables/useTopUpPlanCreditCopy' import ProfileMenuCreditsFlyout from '@/platform/account/components/ProfileMenuCreditsFlyout.vue' vi.mock(import('@/platform/billing/composables/useBillingCapabilities')) +vi.mock(import('@/platform/billing/composables/useBillingContext')) +vi.mock(import('@/platform/billing/components/billingTab/useBillingPlanRow')) +vi.mock(import('@/platform/billing/composables/useTopUpPlanCreditCopy')) vi.mock(import('@/platform/billing/composables/useBillingDialogs')) vi.mock(import('@/platform/billing/composables/usePaymentRecoveryDialog')) vi.mock( @@ -34,14 +42,55 @@ type Served = { canTopUp: boolean denial?: BillingTopUpDenial paymentRecovery: boolean + balance: BillingContext['balance']['value'] + planKind: PlanRowState['kind'] + grant: BillingContext['periodCreditGrant']['value'] + planNote: string | null +} + +const BALANCE: NonNullable = { + amountMicros: 1100, + currency: 'usd', + effectiveBalanceMicros: 1100, + cloudCreditBalanceMicros: 1000, + prepaidBalanceMicros: 100 +} + +function serveBilling({ + balance, + planKind, + grant, + planNote +}: Pick) { + const context = useBillingContext() + vi.mocked(useBillingContext).mockReturnValue({ + ...context, + balance: computed(() => balance), + billingStatus: computed(() => 'paid'), + periodCreditGrant: computed(() => grant), + nextCreditRefillAt: computed(() => '2026-10-28T00:00:00Z') + }) + vi.mocked(useBillingPlanRow).mockReturnValue({ + state: computed(() => ({ kind: planKind }) as PlanRowState) + }) + vi.mocked(useTopUpPlanCreditCopy).mockReturnValue(computed(() => planNote)) +} + +const DEFAULTS: Served = { + canTopUp: true, + paymentRecovery: false, + balance: BALANCE, + planKind: 'active', + grant: { state: 'amount', credits: 8440, period: 'MONTHLY' }, + planNote: null } function renderFlyout(served: Partial = {}) { - const { canTopUp, denial, paymentRecovery } = { - canTopUp: true, - paymentRecovery: false, + const { canTopUp, denial, paymentRecovery, ...billing } = { + ...DEFAULTS, ...served } + serveBilling(billing) const capabilities = useBillingCapabilities() capabilities.canTopUp = computed(() => canTopUp) capabilities.topUpDenial = computed(() => denial) @@ -64,6 +113,8 @@ function renderFlyout(served: Partial = {}) { } const addCredits = () => screen.queryByRole('button', { name: 'Add credits' }) +const textOf = (testId: string) => + screen.queryByTestId(testId)?.textContent.trim() ?? null describe('ProfileMenuCreditsFlyout', () => { it('lets a workspace that can top up add credits, then closes the menu', async () => { @@ -121,6 +172,56 @@ describe('ProfileMenuCreditsFlyout', () => { expect(screen.queryByTestId('update-payment-button') !== null).toBe(served) }) + it('shows the plan and top-up breakdown above the actions', () => { + renderFlyout() + + expect(textOf('credits-breakdown-plan')).toBe('2,110 of 8,440') + expect(screen.getByRole('meter').getAttribute('aria-valuenow')).toBe('75') + expect(screen.getByText('Refills October 28')).toBeInTheDocument() + expect(textOf('credits-breakdown-top-up')).toBe('211') + expect( + screen + .getByTestId('credits-breakdown') + .compareDocumentPosition(addCredits()!) & + Node.DOCUMENT_POSITION_FOLLOWING + ).toBeTruthy() + }) + + it('keeps the year on a yearly plan', () => { + renderFlyout({ + grant: { state: 'amount', credits: 101280, period: 'ANNUAL' } + }) + + expect(screen.getByText('Refills October 28, 2026')).toBeInTheDocument() + }) + + it('shows no plan total or bar when the grant is unknown', () => { + renderFlyout({ grant: { state: 'unknown' } }) + + expect(textOf('credits-breakdown-plan')).toBe('2,110') + expect(screen.queryByRole('meter')).not.toBeInTheDocument() + }) + + it('shows top-up credits only, with the plan note, when there is no plan', () => { + const note = enMessages.credits.topUp.planRequiredSubscribe + renderFlyout({ planKind: 'noPlan', planNote: note }) + + expect(screen.queryByText('Plan credits')).not.toBeInTheDocument() + expect(textOf('credits-breakdown-top-up')).toBe('211') + expect(screen.getByText(note)).toBeInTheDocument() + }) + + it('shows dashes, and keeps Add credits on can_top_up, when the balance is unavailable', () => { + renderFlyout({ balance: null }) + + expect(textOf('credits-breakdown-plan')).toBe('—') + expect(textOf('credits-breakdown-top-up')).toBe('—') + expect(screen.getByRole('alert')).toHaveTextContent( + "Couldn't load your balance." + ) + expect(addCredits()).toBeEnabled() + }) + it('opens Billing settings', async () => { const user = userEvent.setup() const { onOpenBilling } = renderFlyout() diff --git a/src/platform/account/components/ProfileMenuCreditsFlyout.vue b/src/platform/account/components/ProfileMenuCreditsFlyout.vue index 5b99549323c..97cd719d876 100644 --- a/src/platform/account/components/ProfileMenuCreditsFlyout.vue +++ b/src/platform/account/components/ProfileMenuCreditsFlyout.vue @@ -1,61 +1,84 @@