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
174 changes: 58 additions & 116 deletions src/platform/billing/composables/deriveBillingBanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const funded: BillingBannerInputs = {
hasFunds: true,
isCancelled: false,
endDate: null,
withinEnterpriseEndingNotice: false,
canManage: true,
isPlanEnded: false,
isPlanTerminal: false,
Expand All @@ -27,8 +28,8 @@ const funded: BillingBannerInputs = {
isActive: true
}

function derive(overrides: Partial<BillingBannerInputs>, now?: number) {
return deriveBillingBanner({ ...funded, ...overrides }, now)
function derive(overrides: Partial<BillingBannerInputs>) {
return deriveBillingBanner({ ...funded, ...overrides })
}

describe('deriveBillingBanner', () => {
Expand Down Expand Up @@ -357,132 +358,73 @@ describe('deriveBillingBanner', () => {
)
})

describe('enterprise ending notice window', () => {
const NOW = new Date('2026-09-03T12:00:00Z').getTime()

function daysFromNow(days: number): string {
return new Date(NOW + days * 24 * 60 * 60 * 1000).toISOString()
}

describe('enterprise ending notice', () => {
const enterprise: Partial<BillingBannerInputs> = {
isTeamPlan: false,
isEnterprise: true
isEnterprise: true,
isCancelled: true,
endDate: '2026-09-13T12:00:00Z'
}

it('stays quiet while the end date is beyond the notice window', () => {
expect(
derive(
{ ...enterprise, isCancelled: true, endDate: daysFromNow(30) },
NOW
)
).toBeNull()
})

it('surfaces the ending notice once the end date is near', () => {
expect(
derive(
{ ...enterprise, isCancelled: true, endDate: daysFromNow(10) },
NOW
)
).toBe('ending')
})

it('includes the window boundary itself', () => {
expect(
derive(
{ ...enterprise, isCancelled: true, endDate: daysFromNow(14) },
NOW
)
).toBe('ending')
})

it('shows the notice to members inside the window', () => {
expect(
derive(
{
...enterprise,
isCancelled: true,
endDate: daysFromNow(10),
canManage: false
},
NOW
)
).toBe('ending')
})

it('needs a populated end date, like the self-serve notice', () => {
expect(derive({ ...enterprise, isCancelled: true }, NOW)).toBeNull()
})

it('shows nothing for a passed end date once access is gone', () => {
// The window is unbounded below by design; this is the upstream gate
// that keeps a stale payload from pinning a perpetual past-date banner.
expect(
derive(
{
...enterprise,
isCancelled: true,
endDate: daysFromNow(-5),
canAccessSubscriptionFeatures: false
},
NOW
)
).toBeNull()
})

it('does not leak team-only banners into enterprise workspaces', () => {
expect(derive({ ...enterprise, hasFunds: false }, NOW)).toBeNull()
})

it('lets enterprise take precedence if a subscription reads as both', () => {
expect(
derive(
{
...enterprise,
isTeamPlan: true,
isCancelled: true,
endDate: daysFromNow(60)
},
NOW
)
).toBeNull()
expect(
derive({ ...enterprise, isTeamPlan: true, hasFunds: false }, NOW)
).toBeNull()
it.for([
{
case: 'outside the notice window',
inputs: { withinEnterpriseEndingNotice: false },
kind: null
},
{
case: 'inside the notice window',
inputs: { withinEnterpriseEndingNotice: true },
kind: 'ending'
},
{
case: 'inside the window for a member',
inputs: { withinEnterpriseEndingNotice: true, canManage: false },
kind: 'ending'
},
{
case: 'inside the window without a cancellation',
inputs: { withinEnterpriseEndingNotice: true, isCancelled: false },
kind: null
},
{
case: 'inside the window once access is gone',
inputs: {
withinEnterpriseEndingNotice: true,
canAccessSubscriptionFeatures: false
},
kind: null
},
{
case: 'out of credits',
inputs: { hasFunds: false },
kind: null
},
{
case: 'a plan that also reads as team',
inputs: { withinEnterpriseEndingNotice: false, isTeamPlan: true },
kind: null
}
] as const)('shows $kind $case', ({ inputs, kind }) => {
expect(derive({ ...enterprise, ...inputs })).toBe(kind)
})

it('gives an unrecognized tier no banner at all, as before this window', () => {
it('gives an unrecognized tier no banner at all', () => {
expect(
derive(
{
isTeamPlan: false,
isEnterprise: false,
isCancelled: true,
endDate: daysFromNow(10)
},
NOW
)
derive({
isTeamPlan: false,
isEnterprise: false,
isCancelled: true,
endDate: '2026-09-13T12:00:00Z',
withinEnterpriseEndingNotice: true
})
).toBeNull()
})

it('reverts cleanly when a webhook outage clears the end date', () => {
// cancel_at can disappear between polls; nothing latches, so the same
// inputs minus the cancellation must derive back to no banner.
it('never applies the window to a self-serve team cancellation', () => {
expect(
derive(
{ ...enterprise, isCancelled: true, endDate: daysFromNow(10) },
NOW
)
derive({ isCancelled: true, endDate: '2026-11-02T12:00:00Z' })
).toBe('ending')
expect(
derive({ ...enterprise, isCancelled: false, endDate: null }, NOW)
).toBeNull()
})

it('never applies the window to a self-serve team cancellation', () => {
expect(derive({ isCancelled: true, endDate: daysFromNow(60) }, NOW)).toBe(
'ending'
)
})
})
})
57 changes: 56 additions & 1 deletion src/platform/billing/composables/useBillingBanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ describe('useBillingBanner', () => {
isTeamPlan: ref(true),
billingStatus: ref<BillingStatus | null>('paid'),
tier: ref<SubscriptionInfo['tier']>(null),
subscription: ref<Pick<SubscriptionInfo, 'hasFunds'> | null>({
subscription: ref<
| (Pick<SubscriptionInfo, 'hasFunds'> &
Partial<Pick<SubscriptionInfo, 'isCancelled' | 'endDate'>>)
| null
>({
hasFunds: true
})
}
Expand Down Expand Up @@ -178,4 +182,55 @@ describe('useBillingBanner', () => {

expect(kind.value).toBe('outOfCredits')
})
it.for([
{ tier: 'PRO', isTeamPlan: false, audience: 'personal' },
{ tier: 'FOUNDERS_EDITION', isTeamPlan: false, audience: 'personal' },
{ tier: null, isTeamPlan: true, audience: 'team' },
{ tier: 'ENTERPRISE', isTeamPlan: false, audience: null },
{ tier: 'ENTERPRISE', isTeamPlan: true, audience: null },
{ tier: null, isTeamPlan: false, audience: null }
] as const)(
'reads the $tier tier with team plan $isTeamPlan as the $audience audience',
async ({ tier, isTeamPlan, audience }) => {
const billing = setupBilling()
const banner = useBillingBanner()

billing.tier.value = tier
billing.isTeamPlan.value = isTeamPlan
await nextTick()

expect(banner.audience.value).toBe(audience)
}
)

describe('the Enterprise ending notice', () => {
const NOW = new Date('2026-09-03T12:00:00Z').getTime()

beforeEach(() => {
vi.useFakeTimers({ now: NOW })
})

it.for([
{ days: 30, kind: null },
{ days: 14, kind: 'ending' },
{ days: 10, kind: 'ending' }
])(
'shows $kind for a cancellation $days days out',
async ({ days, kind: expected }) => {
const billing = setupBilling()
const { kind } = useBillingBanner()

billing.tier.value = 'ENTERPRISE'
billing.isTeamPlan.value = false
billing.subscription.value = {
hasFunds: true,
isCancelled: true,
endDate: new Date(NOW + days * 24 * 60 * 60 * 1000).toISOString()
}
await nextTick()

expect(kind.value).toBe(expected)
}
)
})
})
50 changes: 27 additions & 23 deletions src/platform/billing/composables/useBillingBanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useBillingContext } from '@/platform/billing/composables/useBillingCont
import { useFeatureFlags } from '@/composables/useFeatureFlags'
import { isWithinEnterpriseEndingNotice } from '@/platform/billing/subscription/constants/tierPricing'
import { isCloud } from '@/platform/distribution/types'
import { pendingServerFact } from '@/platform/serverFacts/pendingServerFact'
import type { SubscriptionInfo } from '@/platform/billing/composables/types'
import type { SubscriptionTier } from '@comfyorg/billing-core'
import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
Expand Down Expand Up @@ -33,6 +34,7 @@ export interface BillingBannerInputs {
hasFunds: boolean | null
isCancelled: boolean
endDate: string | null
withinEnterpriseEndingNotice: boolean
canManage: boolean
isPlanEnded: boolean
isPlanTerminal: boolean
Expand All @@ -54,14 +56,13 @@ export interface BillingBannerInputs {
// Decision recorded on FE-2035. Enterprise payment and credit lifecycles
// are handled by sales, so outOfCredits never applies.
function deriveEnterpriseBanner(
inputs: BillingBannerInputs,
now: number
inputs: BillingBannerInputs
): BillingBannerKind | null {
if (!inputs.canAccessSubscriptionFeatures) return null
if (!inputs.billingControlEnabled) return null
const withinEndingNotice =
inputs.isCancelled && isWithinEnterpriseEndingNotice(inputs.endDate, now)
return withinEndingNotice ? 'ending' : null
return inputs.isCancelled && inputs.withinEnterpriseEndingNotice
? 'ending'
: null
}

const PERSONAL_TIERS: ReadonlySet<SubscriptionTier> = new Set([
Expand Down Expand Up @@ -145,25 +146,27 @@ function hasPlanEnded(
}

export function deriveBillingBanner(
inputs: BillingBannerInputs,
now: number = Date.now()
inputs: BillingBannerInputs
): BillingBannerKind | null {
if (inputs.billingStatus === 'paused') return 'paused'
if (!inputs.isLoaded) return null
const audience = billingBannerAudience(inputs)
if (hasPlanEnded(inputs, audience)) {
return inputs.planEndedDismissed ? null : 'planEnded'
}
if (inputs.isEnterprise) return deriveEnterpriseBanner(inputs, now)
if (inputs.isEnterprise) return deriveEnterpriseBanner(inputs)
return audience ? deriveNoticeBanner(inputs, audience) : null
}

function classifyTier(
tier: SubscriptionTier | null | undefined
): Pick<BillingBannerInputs, 'isEnterprise' | 'isKnownPersonalTier'> {
return {
isEnterprise: tier === 'ENTERPRISE',
isKnownPersonalTier: tier != null && PERSONAL_TIERS.has(tier)
isEnterprise: pendingServerFact('BE-25244', tier === 'ENTERPRISE'),
isKnownPersonalTier: pendingServerFact(
'BE-25244',
tier != null && PERSONAL_TIERS.has(tier)
)
}
}

Expand Down Expand Up @@ -209,7 +212,7 @@ function useBillingBannerInternal() {

const bannerInputs = computed<BillingBannerInputs>(() => ({
billingControlEnabled: flags.billingControlEnabled,
isTeamPlan: isTeamPlan.value,
isTeamPlan: pendingServerFact('BE-25244', isTeamPlan.value),
billingStatus: billingStatus.value,
canAccessSubscriptionFeatures: canAccessSubscriptionFeatures.value,
canManage: permissions.value.canManageSubscription,
Expand All @@ -222,25 +225,26 @@ function useBillingBannerInternal() {
runDeniedPastDue: runDeniedPaymentPastDue.value,
unifiedBillingSettingsEnabled: flags.unifiedBillingSettingsEnabled,
isActive: subscription.value?.isActive === true,
withinEnterpriseEndingNotice: pendingServerFact(
'BE-25244',
isWithinEnterpriseEndingNotice(subscription.value?.endDate, now.value)
),
...readSubscriptionInputs(subscription.value)
}))

const kind = computed<BillingBannerKind | null>(() =>
isCloud ? deriveBillingBanner(bannerInputs.value, now.value) : null
isCloud ? deriveBillingBanner(bannerInputs.value) : null
)
const lifecycleKind = computed<BillingBannerKind | null>(() =>
isCloud
? deriveBillingBanner(
{
...bannerInputs.value,
hasFunds: true,
topUpDeniedPastDue: false,
runDeniedPastDue: false,
planEndedDismissed: false,
planChangeDismissed: false
},
now.value
)
? deriveBillingBanner({
...bannerInputs.value,
hasFunds: true,
topUpDeniedPastDue: false,
runDeniedPastDue: false,
planEndedDismissed: false,
planChangeDismissed: false
})
: null
)
const audience = computed(() => billingBannerAudience(bannerInputs.value))
Expand Down
Loading