From 3ff6dacb51e2424b33521eefff0dc37229b06eaa Mon Sep 17 00:00:00 2001 From: huang47 <157390+huang47@users.noreply.github.com> Date: Wed, 16 Sep 2026 16:48:46 -0700 Subject: [PATCH 1/2] test: isolate subscription lifecycle scenarios --- .../utils/liveCloudBillingPolicy.test.ts | 28 +++ .../fixtures/utils/liveCloudBillingPolicy.ts | 9 +- .../disposable/cancelReactivate.spec.ts | 27 +++ .../disposable/planTransitions.spec.ts | 23 ++ .../liveCloud/helpers/lifecycleScenarios.ts | 207 ++++++++++++++++++ docs/testing/cloud-billing-e2e.md | 9 + 6 files changed, 302 insertions(+), 1 deletion(-) create mode 100644 browser_tests/tests/liveCloud/disposable/cancelReactivate.spec.ts create mode 100644 browser_tests/tests/liveCloud/disposable/planTransitions.spec.ts create mode 100644 browser_tests/tests/liveCloud/helpers/lifecycleScenarios.ts diff --git a/browser_tests/fixtures/utils/liveCloudBillingPolicy.test.ts b/browser_tests/fixtures/utils/liveCloudBillingPolicy.test.ts index fbdea395459..dfec1572f84 100644 --- a/browser_tests/fixtures/utils/liveCloudBillingPolicy.test.ts +++ b/browser_tests/fixtures/utils/liveCloudBillingPolicy.test.ts @@ -383,3 +383,31 @@ describe('Disposable payment permissions', () => { ).toBe(false) }) }) + +describe('Subscription lifecycle permissions', () => { + it.for([ + '/api/billing/subscription/cancel', + '/api/billing/subscription/resubscribe' + ])('limits %s to opted-in sandbox tests', (path) => { + const url = new URL(path, config.PLAYWRIGHT_SETUP_API_URL) + expect(isLiveCloudMutationAllowed(url, 'POST', config)).toBe(false) + expect( + isLiveCloudMutationAllowed(url, 'POST', { + ...config, + allowPayments: true + }) + ).toBe(true) + const production = { + ...config, + PLAYWRIGHT_SETUP_API_URL: 'https://cloud.comfy.org', + allowPayments: true + } + expect( + isLiveCloudMutationAllowed( + new URL(path, production.PLAYWRIGHT_SETUP_API_URL), + 'POST', + production + ) + ).toBe(false) + }) +}) diff --git a/browser_tests/fixtures/utils/liveCloudBillingPolicy.ts b/browser_tests/fixtures/utils/liveCloudBillingPolicy.ts index 1ddcee00a41..ea21ced66a7 100644 --- a/browser_tests/fixtures/utils/liveCloudBillingPolicy.ts +++ b/browser_tests/fixtures/utils/liveCloudBillingPolicy.ts @@ -123,7 +123,14 @@ function isPaymentPostAllowed(url: URL, backend: string): boolean { url.pathname === '/ajax/metrics_batch' ) return true - if (url.origin === backend && url.pathname === '/api/billing/payment-portal') + if ( + url.origin === backend && + [ + '/api/billing/payment-portal', + '/api/billing/subscription/cancel', + '/api/billing/subscription/resubscribe' + ].includes(url.pathname) + ) return true return ( url.origin === 'https://api.stripe.com' && diff --git a/browser_tests/tests/liveCloud/disposable/cancelReactivate.spec.ts b/browser_tests/tests/liveCloud/disposable/cancelReactivate.spec.ts new file mode 100644 index 00000000000..96e2db59da4 --- /dev/null +++ b/browser_tests/tests/liveCloud/disposable/cancelReactivate.spec.ts @@ -0,0 +1,27 @@ +import { verifyCancellationRecovery } from '@e2e/tests/liveCloud/helpers/lifecycleScenarios' +import { + expect, + liveCloudDisposableBillingFixture as test +} from '@e2e/fixtures/liveCloudDisposableBillingFixture' + +test.describe( + 'Real Cloud subscription lifecycle', + { tag: ['@cloud-live'] }, + () => { + test.setTimeout(240_000) + + test('cancels and reactivates without changing the balance', async ({ + disposableCheckout + }, testInfo) => { + await disposableCheckout.checkout.completeCheckout( + disposableCheckout.billingSession, + testInfo + ) + const recovery = await verifyCancellationRecovery( + disposableCheckout.billingSession, + testInfo + ) + expect(recovery.balanceAfterCents).toBe(recovery.balanceBeforeCents) + }) + } +) diff --git a/browser_tests/tests/liveCloud/disposable/planTransitions.spec.ts b/browser_tests/tests/liveCloud/disposable/planTransitions.spec.ts new file mode 100644 index 00000000000..9fc99d409d5 --- /dev/null +++ b/browser_tests/tests/liveCloud/disposable/planTransitions.spec.ts @@ -0,0 +1,23 @@ +import { verifyPlanTransitions } from '@e2e/tests/liveCloud/helpers/lifecycleScenarios' +import { + expect, + liveCloudDisposableBillingFixture as test +} from '@e2e/fixtures/liveCloudDisposableBillingFixture' + +test.describe('Real Cloud plan transitions', { tag: ['@cloud-live'] }, () => { + test.setTimeout(240_000) + + test('upgrades now and schedules a downgrade', async ({ + disposableCheckout + }, testInfo) => { + await disposableCheckout.checkout.completeCheckout( + disposableCheckout.billingSession, + testInfo + ) + const transitions = await verifyPlanTransitions( + disposableCheckout.billingSession, + testInfo + ) + expect(transitions.scheduledPlan).toBe('standard-monthly') + }) +}) diff --git a/browser_tests/tests/liveCloud/helpers/lifecycleScenarios.ts b/browser_tests/tests/liveCloud/helpers/lifecycleScenarios.ts new file mode 100644 index 00000000000..bd39f9cf5ba --- /dev/null +++ b/browser_tests/tests/liveCloud/helpers/lifecycleScenarios.ts @@ -0,0 +1,207 @@ +import { + zBillingBalanceResponse, + zBillingStatusResponse, + zBillingOpStatusResponse, + zCancelSubscriptionAcceptedResponse, + zCancelSubscriptionResponse, + zResubscribeResponse, + zListSavedPaymentMethodsResponse, + zPreviewSubscribeResponse, + zSubscribeResponse +} from '@comfyorg/ingest-types/zod' +import { z } from 'zod' +import { expect } from '@playwright/test' +import type { TestInfo } from '@playwright/test' +import type { LiveCloudBillingSession } from '@e2e/fixtures/helpers/LiveCloudBillingSession' +async function expectSucceeded( + session: LiveCloudBillingSession, + operationId: string +) { + await expect + .poll( + async () => { + const operation = await session.read( + `/api/billing/ops/${encodeURIComponent(operationId)}`, + zBillingOpStatusResponse + ) + return operation.status + }, + { timeout: 60_000 } + ) + .toBe('succeeded') +} +export async function verifyCancellationRecovery( + session: LiveCloudBillingSession, + testInfo: TestInfo +) { + const balanceBefore = await session.read( + '/api/billing/balance', + zBillingBalanceResponse + ) + const cancel = await session.post( + '/api/billing/subscription/cancel', + { idempotency_key: crypto.randomUUID() }, + z.union([zCancelSubscriptionResponse, zCancelSubscriptionAcceptedResponse]), + [200, 202] + ) + await expectSucceeded(session, cancel.billing_op_id) + await expect + .poll(() => session.read('/api/billing/status', zBillingStatusResponse)) + .toMatchObject({ + plan_slug: 'creator-monthly', + subscription_tier: 'CREATOR' + }) + const canceled = await session.read( + '/api/billing/status', + zBillingStatusResponse + ) + expect(canceled.cancel_at).toBeTruthy() + const resubscribe = await session.post( + '/api/billing/subscription/resubscribe', + { idempotency_key: crypto.randomUUID() }, + zResubscribeResponse + ) + if (resubscribe.status === 'pending') { + await expectSucceeded(session, resubscribe.billing_op_id) + } + await expect + .poll(async () => { + const status = await session.read( + '/api/billing/status', + zBillingStatusResponse + ) + return { + cancelAt: status.cancel_at, + planSlug: status.plan_slug, + subscriptionStatus: status.subscription_status + } + }) + .toMatchObject({ + cancelAt: undefined, + planSlug: 'creator-monthly', + subscriptionStatus: 'active' + }) + const balanceAfter = await session.read( + '/api/billing/balance', + zBillingBalanceResponse + ) + expect(balanceAfter.amount_micros).toBe(balanceBefore.amount_micros) + const result = { + cancelOperationId: cancel.billing_op_id, + reactivateOperationId: resubscribe.billing_op_id, + balanceBeforeCents: balanceBefore.amount_micros, + balanceAfterCents: balanceAfter.amount_micros + } + await testInfo.attach('cancellation-recovery.json', { + body: JSON.stringify(result), + contentType: 'application/json' + }) + return result +} +export async function verifyPlanTransitions( + session: LiveCloudBillingSession, + testInfo: TestInfo +) { + const methods = await session.read( + '/api/billing/payment-methods', + zListSavedPaymentMethodsResponse + ) + expect(methods).toHaveLength(1) + const balanceBeforeUpgrade = await session.read( + '/api/billing/balance', + zBillingBalanceResponse + ) + const upgradePreview = await session.post( + '/api/billing/preview-subscribe', + { plan_slug: 'pro-monthly' }, + zPreviewSubscribeResponse + ) + expect(upgradePreview).toMatchObject({ + allowed: true, + transition_type: 'upgrade' + }) + const upgradeRequest: Record = { + plan_slug: 'pro-monthly' + } + if (upgradePreview.quote_id) { + upgradeRequest.quote_id = upgradePreview.quote_id + } + if (upgradePreview.quote_version !== undefined) { + upgradeRequest.quote_version = upgradePreview.quote_version + } + if (upgradePreview.proration_at) { + upgradeRequest.proration_at = upgradePreview.proration_at + } + const upgrade = await session.post( + '/api/billing/subscribe', + upgradeRequest, + zSubscribeResponse + ) + await expectSucceeded(session, upgrade.billing_op_id) + await expect + .poll(() => session.read('/api/billing/status', zBillingStatusResponse)) + .toMatchObject({ + plan_slug: 'pro-monthly', + subscription_tier: 'PRO' + }) + const expectedUpgradeBalance = + balanceBeforeUpgrade.amount_micros + + Number(upgradePreview.credits_today_cents) + await expect + .poll( + async () => + (await session.read('/api/billing/balance', zBillingBalanceResponse)) + .amount_micros + ) + .toBe(expectedUpgradeBalance) + const downgradePreview = await session.post( + '/api/billing/preview-subscribe', + { plan_slug: 'standard-monthly' }, + zPreviewSubscribeResponse + ) + expect(downgradePreview).toMatchObject({ + allowed: true, + transition_type: 'downgrade' + }) + const downgradeRequest: Record = { + plan_slug: 'standard-monthly' + } + if (downgradePreview.quote_id) { + downgradeRequest.quote_id = downgradePreview.quote_id + } + if (downgradePreview.quote_version !== undefined) { + downgradeRequest.quote_version = downgradePreview.quote_version + } + if (downgradePreview.proration_at) { + downgradeRequest.proration_at = downgradePreview.proration_at + } + const downgrade = await session.post( + '/api/billing/subscribe', + downgradeRequest, + zSubscribeResponse + ) + await expectSucceeded(session, downgrade.billing_op_id) + await expect + .poll(() => session.read('/api/billing/status', zBillingStatusResponse)) + .toMatchObject({ + plan_slug: 'pro-monthly', + scheduled_change: { plan_slug: 'standard-monthly' } + }) + const balanceAfterDowngrade = await session.read( + '/api/billing/balance', + zBillingBalanceResponse + ) + expect(balanceAfterDowngrade.amount_micros).toBe(expectedUpgradeBalance) + const result = { + upgradeOperationId: upgrade.billing_op_id, + downgradeOperationId: downgrade.billing_op_id, + balanceBeforeUpgradeCents: balanceBeforeUpgrade.amount_micros, + balanceAfterDowngradeCents: balanceAfterDowngrade.amount_micros, + scheduledPlan: 'standard-monthly' + } + await testInfo.attach('plan-transitions.json', { + body: JSON.stringify(result), + contentType: 'application/json' + }) + return result +} diff --git a/docs/testing/cloud-billing-e2e.md b/docs/testing/cloud-billing-e2e.md index 96bdcab10e3..5dfd15208b2 100644 --- a/docs/testing/cloud-billing-e2e.md +++ b/docs/testing/cloud-billing-e2e.md @@ -82,3 +82,12 @@ Traces start after permanent-account sign-in and contain authenticated network traffic. Keep reports private. Disposable accounts require no reusable credentials. Completed-payment assertions verify saved cards through the real hosted billing portal, because Staging disables the embedded-checkout saved-card API. + +## Subscription lifecycle + +PR #17548 depends only on #17481. Run `--project=cloud-live-disposable` for +cancellation/reactivation and plan transitions. The shared setup provisions a +paid sandbox subscription; all lifecycle assertions and endpoint permissions live +in this PR. Cancellation/reactivation preserves balance, upgrade grants the quote, +and scheduled downgrade grants nothing. Live execution still requires the saved-card +endpoint needed by paid setup. From d6d8f61dbfda2c1ecf36419968ba495633d20a0e Mon Sep 17 00:00:00 2001 From: huang47 <157390+huang47@users.noreply.github.com> Date: Wed, 16 Sep 2026 17:39:24 -0700 Subject: [PATCH 2/2] test: use hosted card verification for plan transitions --- .../tests/liveCloud/disposable/planTransitions.spec.ts | 3 ++- browser_tests/tests/liveCloud/helpers/lifecycleScenarios.ts | 6 ------ docs/testing/cloud-billing-e2e.md | 4 ++-- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/browser_tests/tests/liveCloud/disposable/planTransitions.spec.ts b/browser_tests/tests/liveCloud/disposable/planTransitions.spec.ts index 9fc99d409d5..62ee6de261c 100644 --- a/browser_tests/tests/liveCloud/disposable/planTransitions.spec.ts +++ b/browser_tests/tests/liveCloud/disposable/planTransitions.spec.ts @@ -10,10 +10,11 @@ test.describe('Real Cloud plan transitions', { tag: ['@cloud-live'] }, () => { test('upgrades now and schedules a downgrade', async ({ disposableCheckout }, testInfo) => { - await disposableCheckout.checkout.completeCheckout( + const completion = await disposableCheckout.checkout.completeCheckout( disposableCheckout.billingSession, testInfo ) + expect(completion.paymentMethodCount).toBe(1) const transitions = await verifyPlanTransitions( disposableCheckout.billingSession, testInfo diff --git a/browser_tests/tests/liveCloud/helpers/lifecycleScenarios.ts b/browser_tests/tests/liveCloud/helpers/lifecycleScenarios.ts index bd39f9cf5ba..85f6d5f1259 100644 --- a/browser_tests/tests/liveCloud/helpers/lifecycleScenarios.ts +++ b/browser_tests/tests/liveCloud/helpers/lifecycleScenarios.ts @@ -5,7 +5,6 @@ import { zCancelSubscriptionAcceptedResponse, zCancelSubscriptionResponse, zResubscribeResponse, - zListSavedPaymentMethodsResponse, zPreviewSubscribeResponse, zSubscribeResponse } from '@comfyorg/ingest-types/zod' @@ -102,11 +101,6 @@ export async function verifyPlanTransitions( session: LiveCloudBillingSession, testInfo: TestInfo ) { - const methods = await session.read( - '/api/billing/payment-methods', - zListSavedPaymentMethodsResponse - ) - expect(methods).toHaveLength(1) const balanceBeforeUpgrade = await session.read( '/api/billing/balance', zBillingBalanceResponse diff --git a/docs/testing/cloud-billing-e2e.md b/docs/testing/cloud-billing-e2e.md index 5dfd15208b2..3b4a3b4ca70 100644 --- a/docs/testing/cloud-billing-e2e.md +++ b/docs/testing/cloud-billing-e2e.md @@ -89,5 +89,5 @@ PR #17548 depends only on #17481. Run `--project=cloud-live-disposable` for cancellation/reactivation and plan transitions. The shared setup provisions a paid sandbox subscription; all lifecycle assertions and endpoint permissions live in this PR. Cancellation/reactivation preserves balance, upgrade grants the quote, -and scheduled downgrade grants nothing. Live execution still requires the saved-card -endpoint needed by paid setup. +and scheduled downgrade grants nothing. Paid setup verifies the saved card through +the hosted billing portal.