Repository navigation
FRONT-4019 feat(billing): show the plan and top-up credits breakdown in the credits flyout #23337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dante01yoon
merged 3 commits into
main
from
1.61-billing-sdk/front-4019-credits-flyout-breakdown
Nov 13, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ea3234f
feat(billing): show the plan and top-up credits breakdown in the cred…
dante01yoon 26d2962
fix(account-ui): inline the credits breakdown skeleton so the package…
dante01yoon 94ee40b
fix(billing): show the served top-up balance and keep Add credits on …
dante01yoon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
packages/account-core/src/core/billing/billingTab/creditsFlyout.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' | ||
| }) | ||
| }) | ||
| }) |
49 changes: 49 additions & 0 deletions
49
packages/account-core/src/core/billing/billingTab/creditsFlyout.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
| 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
packages/account-ui/src/billing/tab/BillingCreditsBreakdown.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.