diff --git a/browser_tests/tests/settingsPlansSubscribe.spec.ts b/browser_tests/tests/settingsPlansSubscribe.spec.ts index 8fdc0bbd098..b3958710fa6 100644 --- a/browser_tests/tests/settingsPlansSubscribe.spec.ts +++ b/browser_tests/tests/settingsPlansSubscribe.spec.ts @@ -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, @@ -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 + } + 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 @@ -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 - } - 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' @@ -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) + await expect + .poll(() => + page.evaluate( + () => + document.documentElement.scrollWidth - + document.documentElement.clientWidth + ) + ) + .toBeLessThanOrEqual(1) + }) }) diff --git a/src/locales/en/main.json b/src/locales/en/main.json index 08ed194146c..1bc9cabae33 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -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": { diff --git a/src/platform/workspace/components/dialogs/settings/SettingsPlansSection.test.ts b/src/platform/workspace/components/dialogs/settings/SettingsPlansSection.test.ts index e8024aba80c..9882a71ccba 100644 --- a/src/platform/workspace/components/dialogs/settings/SettingsPlansSection.test.ts +++ b/src/platform/workspace/components/dialogs/settings/SettingsPlansSection.test.ts @@ -28,7 +28,9 @@ vi.mock('@/composables/billing/useBillingContext', () => ({ useBillingContext: () => ({ fetchPlans: mockFetchPlans, plans: mockCatalogPlans, - currentPlanSlug: mockCurrentPlanSlug + currentPlanSlug: mockCurrentPlanSlug, + isLoading: { value: false }, + error: { value: null } }) })) @@ -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() @@ -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() + }) }) diff --git a/src/platform/workspace/components/dialogs/settings/SettingsPlansSection.vue b/src/platform/workspace/components/dialogs/settings/SettingsPlansSection.vue index d98533ae705..3cee19730d6 100644 --- a/src/platform/workspace/components/dialogs/settings/SettingsPlansSection.vue +++ b/src/platform/workspace/components/dialogs/settings/SettingsPlansSection.vue @@ -1,5 +1,8 @@