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
Expand Up @@ -3,9 +3,15 @@ import { render, screen, waitFor } from '@testing-library/vue'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { nextTick, ref } from 'vue'

import { createI18n } from 'vue-i18n'

import TopbarSubscribeButton from '@/components/topbar/TopbarSubscribeButton.vue'
import enMessages from '@/locales/en/main.json' with { type: 'json' }

import CloudRunButtonWrapper from './CloudRunButtonWrapper.vue'

const mockCanRunWorkflows = ref(true)
const mockIsFreeTier = ref(true)
const mockIsInitialized = ref(true)
const mockBillingStatus = ref<string | null>('paid')
const mockSubscriptionTier = ref<string | null>(null)
Expand All @@ -30,6 +36,7 @@ vi.mock('@/composables/billing/useBillingContext', async () => {
() => mockIsInitialized.value && !mockCanRunWorkflows.value
),
billingStatus: mockBillingStatus,
isFreeTier: mockIsFreeTier,
subscription: computed(() =>
mockSubscriptionTier.value ? { tier: mockSubscriptionTier.value } : null
),
Expand All @@ -40,6 +47,17 @@ vi.mock('@/composables/billing/useBillingContext', async () => {
}
})

vi.mock('@/platform/distribution/types', () => ({
isCloud: true
}))

vi.mock(
'@/platform/cloud/subscription/composables/useSubscriptionDialog',
() => ({
useSubscriptionDialog: () => ({ showPricingTable: vi.fn() })
})
)

vi.mock('@/composables/useFeatureFlags', () => ({
useFeatureFlags: () => ({
flags: {
Expand Down Expand Up @@ -86,12 +104,20 @@ vi.mock('@/components/actionbar/ComfyRunButton/ComfyQueueButton.vue', () => ({
}
}))

vi.mock('@/platform/cloud/subscription/components/SubscribeToRun.vue', () => ({
default: {
name: 'SubscribeToRun',
template: '<div data-testid="subscribe-to-run-button" />'
vi.mock(
'@/platform/cloud/subscription/components/SubscribeToRun.vue',
async () => {
const { registerSubscribeToRunPrompt } =
await import('@/platform/cloud/subscription/composables/useSubscribeCtaPresence')
return {
default: {
name: 'SubscribeToRun',
setup: () => registerSubscribeToRunPrompt(),
template: '<div data-testid="subscribe-to-run-button" />'
}
}
}
}))
)

function renderWrapper() {
return render(CloudRunButtonWrapper)
Expand All @@ -105,6 +131,59 @@ describe('CloudRunButtonWrapper', () => {
mockSubscriptionTier.value = null
state.v1PaymentRecovery = true
state.canManageSubscription = true
mockIsFreeTier.value = true
})

describe('one subscribe CTA at a time', () => {
const CtaSurface = {
components: { CloudRunButtonWrapper, TopbarSubscribeButton },
template: '<div><TopbarSubscribeButton /><CloudRunButtonWrapper /></div>'
}

function renderCtaSurface() {
const i18n = createI18n({
legacy: false,
locale: 'en',
messages: { en: enMessages }
})
return render(CtaSurface, { global: { plugins: [i18n] } })
}

it('keeps the topbar CTA on a sales-managed plan, where the prompt never mounts', () => {
mockCanRunWorkflows.value = false
mockSubscriptionTier.value = 'ENTERPRISE'

renderCtaSurface()

expect(
screen.queryByTestId('subscribe-to-run-button')
).not.toBeInTheDocument()
expect(screen.getByTestId('topbar-subscribe-button')).toBeInTheDocument()
})

it('keeps the topbar CTA under payment recovery, where the prompt never mounts', () => {
mockCanRunWorkflows.value = false
mockBillingStatus.value = 'paused'

renderCtaSurface()

expect(
screen.queryByTestId('subscribe-to-run-button')
).not.toBeInTheDocument()
expect(screen.getByTestId('topbar-subscribe-button')).toBeInTheDocument()
})

it('yields the topbar CTA only while the prompt is actually mounted', async () => {
mockCanRunWorkflows.value = false

renderCtaSurface()
await nextTick()

expect(screen.getByTestId('subscribe-to-run-button')).toBeInTheDocument()
expect(
screen.queryByTestId('topbar-subscribe-button')
).not.toBeInTheDocument()
})
})

it('renders the runnable queue button when the subscription is active', () => {
Expand Down
49 changes: 44 additions & 5 deletions src/components/topbar/TopbarSubscribeButton.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it, vi } from 'vitest'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { nextTick } from 'vue'
import { createI18n } from 'vue-i18n'

import { render, screen } from '@testing-library/vue'
Expand Down Expand Up @@ -26,12 +27,32 @@ vi.mock(
})
)

vi.mock('@/composables/billing/useBillingContext', () => ({
useBillingContext: vi.fn(() => ({
isFreeTier: { value: true }
}))
const mockState = vi.hoisted(() => ({
holder: null as null | { isFreeTier: boolean; promptMounted: boolean }
}))

vi.mock('@/composables/billing/useBillingContext', async () => {
const { computed, reactive } = await import('vue')
mockState.holder ??= reactive({ isFreeTier: true, promptMounted: false })
return {
useBillingContext: vi.fn(() => ({
isFreeTier: computed(() => mockState.holder!.isFreeTier)
}))
}
})

vi.mock(
'@/platform/cloud/subscription/composables/useSubscribeCtaPresence',
async () => {
const { computed, reactive } = await import('vue')
mockState.holder ??= reactive({ isFreeTier: true, promptMounted: false })
return {
useSubscribeToRunPromptPresence: () =>
computed(() => mockState.holder!.promptMounted)
}
}
)

vi.mock('pinia')

vi.mock('firebase/app', () => ({
Expand Down Expand Up @@ -62,12 +83,30 @@ function renderComponent() {
}

describe('TopbarSubscribeButton', () => {
beforeEach(() => {
mockState.holder!.isFreeTier = true
mockState.holder!.promptMounted = false
})

it('renders on cloud when isFreeTier is true', () => {
mockIsCloud.value = true
renderComponent()
expect(screen.getByTestId('topbar-subscribe-button')).toBeInTheDocument()
})

it('yields while a Run-slot subscribe prompt is mounted, and returns when it unmounts', async () => {
mockIsCloud.value = true
mockState.holder!.promptMounted = true
renderComponent()
expect(
screen.queryByTestId('topbar-subscribe-button')
).not.toBeInTheDocument()

mockState.holder!.promptMounted = false
await nextTick()
expect(screen.getByTestId('topbar-subscribe-button')).toBeInTheDocument()
})

it('hides on non-cloud distribution', () => {
mockIsCloud.value = false
renderComponent()
Expand Down
4 changes: 3 additions & 1 deletion src/components/topbar/TopbarSubscribeButton.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<Button
v-if="isCloud && isFreeTier"
v-if="isCloud && isFreeTier && !subscribeToRunPromptVisible"
class="mr-2 shrink-0 whitespace-nowrap"
variant="subscribe"
size="sm"
Expand All @@ -14,10 +14,12 @@
<script setup lang="ts">
import Button from '@/components/ui/button/Button.vue'
import { useBillingContext } from '@/composables/billing/useBillingContext'
import { useSubscribeToRunPromptPresence } from '@/platform/cloud/subscription/composables/useSubscribeCtaPresence'
import { useSubscriptionDialog } from '@/platform/cloud/subscription/composables/useSubscriptionDialog'
import { isCloud } from '@/platform/distribution/types'

const { isFreeTier } = useBillingContext()
const subscribeToRunPromptVisible = useSubscribeToRunPromptPresence()
const subscriptionDialog = useSubscriptionDialog()

function handleClick() {
Expand Down
4 changes: 4 additions & 0 deletions src/platform/cloud/subscription/components/SubscribeToRun.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</template>

<script setup lang="ts">
import { registerSubscribeToRunPrompt } from '@/platform/cloud/subscription/composables/useSubscribeCtaPresence'
import { breakpointsTailwind, useBreakpoints } from '@vueuse/core'
import { computed } from 'vue'
import { useI18n } from 'vue-i18n'
Expand All @@ -27,6 +28,9 @@ import { isCloud } from '@/platform/distribution/types'
import { useWorkspaceUI } from '@/platform/workspace/composables/useWorkspaceUI'

const { t } = useI18n()

// Mounting suppresses TopbarSubscribeButton; see useSubscribeCtaPresence
registerSubscribeToRunPrompt()
const breakpoints = useBreakpoints(breakpointsTailwind)
const isMdOrLarger = breakpoints.greaterOrEqual('md')

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { computed, onMounted, onUnmounted, ref } from 'vue'

const mountedPrompts = ref(0)

/**
* Registers a mounted Run-slot subscribe prompt for the caller's lifetime.
* The billing flag alone is not evidence a prompt is on screen — builder
* mode unmounts the actionbar, Linear mode needs outputs, and the wrapper
* withholds the prompt for sales-managed and payment-recovery states — so
* anything yielding to the prompt must key on real presence.
*/
export function registerSubscribeToRunPrompt() {
onMounted(() => {
mountedPrompts.value += 1
})
onUnmounted(() => {
mountedPrompts.value = Math.max(0, mountedPrompts.value - 1)
})
}

/** Whether any Run-slot subscribe prompt is currently on screen. */
export function useSubscribeToRunPromptPresence() {
return computed(() => mountedPrompts.value > 0)
}
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ describe('CurrentUserPopoverWorkspace', () => {
}
)

it('shows Subscribe instead of Manage plan when payment_failed has no plan', () => {
it('shows the upgrade upsell instead of Manage plan when payment_failed has no plan', () => {
state.billingStatus = 'payment_failed'
state.canAccessSubscriptionFeatures = false
state.canManageSubscription = true
Expand All @@ -350,8 +350,11 @@ describe('CurrentUserPopoverWorkspace', () => {
screen.queryByTestId('manage-plan-menu-item')
).not.toBeInTheDocument()
expect(
screen.getByRole('button', { name: 'Subscribe' })
screen.getByTestId('upgrade-to-add-credits-button')
).toBeInTheDocument()
expect(
screen.queryByRole('button', { name: 'Subscribe' })
).not.toBeInTheDocument()
})

it('keeps Subscribe hidden on Local after switching to an unsubscribed workspace', async () => {
Expand Down Expand Up @@ -422,7 +425,8 @@ describe('CurrentUserPopoverWorkspace', () => {
expect(screen.getByTestId('add-credits-button')).toBeInTheDocument()
})

it('keeps the upgrade upsell for the Cloud free tier', () => {
it('shows one subscription CTA on the Cloud free tier', () => {
state.canAccessSubscriptionFeatures = false
state.canTopUp = false
state.canSubscribeSelfServe = true

Expand All @@ -432,6 +436,9 @@ describe('CurrentUserPopoverWorkspace', () => {
screen.getByTestId('upgrade-to-add-credits-button')
).toBeInTheDocument()
expect(screen.queryByTestId('add-credits-button')).not.toBeInTheDocument()
expect(
screen.queryByRole('button', { name: 'Subscribe' })
).not.toBeInTheDocument()
})

it('keeps Resubscribe hidden on Local for a cancelled plan', () => {
Expand All @@ -456,6 +463,7 @@ describe('CurrentUserPopoverWorkspace', () => {
canManageSubscriptionLifecycle: true,
canReactivate: true,
canSubscribeSelfServe: false,
canTopUp: false,
action: 'Resubscribe',
visible: true
},
Expand All @@ -467,6 +475,7 @@ describe('CurrentUserPopoverWorkspace', () => {
canManageSubscriptionLifecycle: false,
canReactivate: false,
canSubscribeSelfServe: false,
canTopUp: false,
action: 'Resubscribe',
visible: false
},
Expand All @@ -478,6 +487,7 @@ describe('CurrentUserPopoverWorkspace', () => {
canManageSubscriptionLifecycle: true,
canReactivate: false,
canSubscribeSelfServe: false,
canTopUp: false,
action: 'Resubscribe',
visible: false
},
Expand All @@ -489,6 +499,7 @@ describe('CurrentUserPopoverWorkspace', () => {
canManageSubscriptionLifecycle: false,
canReactivate: false,
canSubscribeSelfServe: true,
canTopUp: true,
action: 'Subscribe',
visible: true
},
Expand All @@ -500,6 +511,7 @@ describe('CurrentUserPopoverWorkspace', () => {
canManageSubscriptionLifecycle: true,
canReactivate: true,
canSubscribeSelfServe: false,
canTopUp: false,
action: 'Subscribe',
visible: false
}
Expand All @@ -512,6 +524,7 @@ describe('CurrentUserPopoverWorkspace', () => {
canManageSubscriptionLifecycle,
canReactivate,
canSubscribeSelfServe,
canTopUp,
action,
visible
}) => {
Expand All @@ -521,6 +534,7 @@ describe('CurrentUserPopoverWorkspace', () => {
state.canManageSubscriptionLifecycle = canManageSubscriptionLifecycle
state.canReactivatePlan = canReactivate
state.canSubscribeSelfServe = canSubscribeSelfServe
state.canTopUp = canTopUp

renderComponent('team')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ const showSubscribeAction = computed(
((isCancelled.value && canReactivatePlan.value) ||
(!canAccessSubscriptionFeatures.value &&
!hasDelinquentSubscription.value &&
canSubscribeSelfServe.value))
canSubscribeSelfServe.value &&
canTopUp.value))
)

const handleOpenUserSettings = () => {
Expand Down
Loading