Skip to content
Closed
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
179 changes: 142 additions & 37 deletions browser_tests/tests/settingsPlansSubscribe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '@playwright/test'
import type { Route } from '@playwright/test'
import type { APIRequestContext, Page, Route } from '@playwright/test'

import type {
BillingPlansResponse,
Expand Down Expand Up @@ -83,7 +83,66 @@ function billingStatus(planSlug?: string): BillingStatusResponse {
}
}

test.describe('Local plans section subscribe (FE-1600 S2)', () => {
async function mockLegacyReads(page: Page) {
await page.route('**/customers/**', (r) =>
fulfillJson(
r,
r.request().url().includes('balance')
? { amount_micros: 0, currency: 'usd' }
: { is_active: false }
)
)
await page.route('**/api/billing/status', (r) =>
fulfillJson(r, billingStatus())
)
await page.route('**/api/billing/balance', (r) =>
fulfillJson(r, { amount_micros: 0, currency: 'usd' })
)
}

/**
* Boots the app with a mocked Firebase session and opens Plan & Credits.
* window.open is stubbed; the opened URL lands in dataset.openedUrl.
*/
async function bootToPlansSection(page: Page, request: APIRequestContext) {
// Multi-user servers show a user-select screen unless a user id is seeded,
// and a fresh CI server has no users at all — find or create one.
const usersResponse = await request.get(`${APP_URL}/api/users`)
const usersBody = (await usersResponse.json()) as {
users?: Record<string, string>
}
let userId = Object.keys(usersBody.users ?? {})[0]
if (!userId) {
const created = await request.post(`${APP_URL}/api/users`, {
data: { username: 'plans-subscribe-e2e' }
})
userId = (await created.json()) as string
}

await page.addInitScript((id) => {
if (id) localStorage.setItem('Comfy.userId', id)
window.open = (url) => {
document.documentElement.dataset.openedUrl = String(url)
return window
}
}, userId)

const auth = new CloudAuthHelper(page)
await auth.mockAuth()

await page.goto(APP_URL)
await page.waitForFunction(() => !!window.app?.extensionManager, null, {
timeout: 45_000
})

await new CommandHelper(page).executeCommand('Comfy.ShowSettingsDialog')
const dialog = page.getByTestId('settings-dialog')
await expect(dialog).toBeVisible()
await dialog.getByRole('button', { name: 'Plan & Credits' }).click()
return dialog
}

test.describe('Local plans section subscribe', () => {
test('subscribes via the workspace rail and flips the card to Current', async ({
page,
request
Expand Down Expand Up @@ -146,41 +205,7 @@ test.describe('Local plans section subscribe (FE-1600 S2)', () => {
fulfillJson(r, { status: 'succeeded' })
)

// A multi-user server boots to a user-selection screen unless a real user
// id is seeded, and a fresh CI server has no users at all — find or create
// one, same approach as the ComfyPage fixture.
const usersResponse = await request.get(`${APP_URL}/api/users`)
const usersBody = (await usersResponse.json()) as {
users?: Record<string, string>
}
let userId = Object.keys(usersBody.users ?? {})[0]
if (!userId) {
const created = await request.post(`${APP_URL}/api/users`, {
data: { username: 'plans-subscribe-e2e' }
})
userId = (await created.json()) as string
}

await page.addInitScript((id) => {
if (id) localStorage.setItem('Comfy.userId', id)
window.open = (url) => {
document.documentElement.dataset.openedUrl = String(url)
return window
}
}, userId)

const auth = new CloudAuthHelper(page)
await auth.mockAuth()

await page.goto(APP_URL)
await page.waitForFunction(() => !!window.app?.extensionManager, null, {
timeout: 45_000
})

await new CommandHelper(page).executeCommand('Comfy.ShowSettingsDialog')
const dialog = page.getByTestId('settings-dialog')
await expect(dialog).toBeVisible()
await dialog.getByRole('button', { name: 'Plan & Credits' }).click()
const dialog = await bootToPlansSection(page, request)

const chooseStandard = dialog.getByRole('button', {
name: 'Choose Standard'
Expand Down Expand Up @@ -223,4 +248,84 @@ test.describe('Local plans section subscribe (FE-1600 S2)', () => {

expect(legacyCheckoutRequests).toEqual([])
})

test('recovers from a failed plans fetch via retry', async ({
page,
request
}) => {
test.setTimeout(90_000)

let plansAvailable = false
await mockLegacyReads(page)
await page.route('**/api/billing/plans', async (r) => {
if (r.request().method() === 'OPTIONS') {
await r.fulfill({ status: 204, headers: CORS_HEADERS })
return
}
if (!plansAvailable) {
await r.fulfill({
status: 500,
contentType: 'application/json',
headers: CORS_HEADERS,
body: '{}'
})
return
}
await fulfillJson(r, plansResponse)
})

const dialog = await bootToPlansSection(page, request)

await expect(
dialog.getByText("We couldn't load your plan details.")
).toBeVisible()
await expect(
dialog.getByRole('button', { name: 'Choose Standard' })
).toHaveCount(0)

plansAvailable = true
await dialog.getByRole('button', { name: 'Try again' }).click()

await expect(
dialog.getByRole('button', { name: 'Choose Standard' })
).toBeEnabled()
await expect(
dialog.getByText("We couldn't load your plan details.")
).toHaveCount(0)
})

test('keeps the plans section within the viewport at mobile width', async ({
page,
request
}) => {
test.setTimeout(90_000)

await mockLegacyReads(page)
await page.route('**/api/billing/plans', (r) =>
fulfillJson(r, plansResponse)
)

const dialog = await bootToPlansSection(page, request)

const section = dialog.getByTestId('settings-plans-section')
await expect(
section.getByRole('button', { name: 'Choose Standard' })
).toBeVisible()

await page.setViewportSize({ width: 390, height: 844 })

// 1px tolerance for subpixel rounding.
await expect
.poll(() => section.evaluate((el) => el.scrollWidth - el.clientWidth))
.toBeLessThanOrEqual(1)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
await expect
.poll(() =>
page.evaluate(
() =>
document.documentElement.scrollWidth -
document.documentElement.clientWidth
)
)
.toBeLessThanOrEqual(1)
})
})
1 change: 1 addition & 0 deletions src/locales/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -2638,6 +2638,7 @@
"comingProjects": "Projects",
"enterpriseCopy": "Need more members? Looking for more flexibility or custom features?",
"contactUs": "Contact us",
"noPlansAvailable": "No plans are available right now. Check back soon.",
"checkoutCaption": "Checkout happens right here: payment details handled securely by Stripe. Prefer no plan? Keep topping up, anytime."
},
"credits": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ vi.mock('@/composables/billing/useBillingContext', () => ({
useBillingContext: () => ({
fetchPlans: mockFetchPlans,
plans: mockCatalogPlans,
currentPlanSlug: mockCurrentPlanSlug
currentPlanSlug: mockCurrentPlanSlug,
isLoading: { value: false },
error: { value: null }
})
}))

Expand Down Expand Up @@ -98,8 +100,11 @@ function renderSection() {

describe('SettingsPlansSection', () => {
beforeEach(() => {
useBillingPlans().teamCreditStops.value = null
mockCatalogPlans.value = []
const billingPlans = useBillingPlans()
billingPlans.teamCreditStops.value = null
billingPlans.isLoading.value = false
billingPlans.error.value = null
mockCatalogPlans.value = yearlyCatalog()
mockCurrentPlanSlug.value = null
mockFetchPlans.mockReset()
mockSubscribeToPersonal.mockReset()
Expand Down Expand Up @@ -317,4 +322,52 @@ describe('SettingsPlansSection', () => {
screen.getByRole('button', { name: 'Subscribe to Team Monthly' })
).toBeEnabled()
})

it('renders skeletons and no cards while the catalog is loading', () => {
mockCatalogPlans.value = []
useBillingPlans().isLoading.value = true
renderSection()

expect(screen.getByTestId('plans-skeleton')).toBeTruthy()
expect(screen.queryByRole('button', { name: 'Choose Standard' })).toBeNull()
})

it('shows the load error with a retry that re-dispatches the fetch', async () => {
mockCatalogPlans.value = []
useBillingPlans().error.value = 'network down'
renderSection()

expect(screen.getByText("We couldn't load your plan details.")).toBeTruthy()
expect(screen.queryByRole('button', { name: 'Choose Standard' })).toBeNull()

await userEvent.click(screen.getByRole('button', { name: 'Try again' }))
expect(mockFetchPlans).toHaveBeenCalledTimes(2)
})

it('shows the dedicated empty state when the catalog loads empty', () => {
mockCatalogPlans.value = []
renderSection()

expect(
screen.getByText('No plans are available right now. Check back soon.')
).toBeTruthy()
expect(screen.queryByText("We couldn't load your plan details.")).toBeNull()
expect(screen.queryByRole('button', { name: 'Choose Standard' })).toBeNull()
})

it('keeps cached cards rendered through a refetch and a refetch failure', () => {
useBillingPlans().isLoading.value = true
const { unmount } = renderSection()

expect(screen.getByRole('button', { name: 'Choose Standard' })).toBeTruthy()
expect(screen.queryByTestId('plans-skeleton')).toBeNull()
unmount()

useBillingPlans().isLoading.value = false
useBillingPlans().error.value = 'refetch failed'
renderSection()

expect(screen.getByRole('button', { name: 'Choose Standard' })).toBeTruthy()
expect(screen.queryByText("We couldn't load your plan details.")).toBeNull()
})
})
Loading
Loading