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
@@ -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<CreditsFlyoutFacts>) => ({ ...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'
})
})
})
Original file line number Diff line number Diff line change
@@ -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.
Comment thread
dante01yoon marked this conversation as resolved.
Resolved
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<CreditsCardFacts['periodCreditGrant']>['period']
): CreditsFlyoutPlan['cadence'] {
if (period === 'MONTHLY') return 'monthly'
if (period === 'ANNUAL') return 'yearly'
return null
}
6 changes: 6 additions & 0 deletions packages/account-core/src/core/billing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion packages/account-ui/src/billing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
113 changes: 113 additions & 0 deletions packages/account-ui/src/billing/tab/BillingCreditsBreakdown.test.ts
Original file line number Diff line number Diff line change
@@ -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: `<p>${note}</p>` } : {}
})
}

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()
})
})
Loading
Loading