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
5 changes: 5 additions & 0 deletions browser_tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ to a section below.

## Prerequisites

For real Cloud billing checkout recovery, sandbox setup, and remaining release
criteria, see [Cloud billing E2E coverage](../docs/testing/cloud-billing-e2e.md).
Its dedicated runner requires real authentication and a dedicated no-card account;
the live suite is excluded from the ordinary browser test configurations.

**CRITICAL**: Start the ComfyUI backend with `--multi-user`:

```bash
Expand Down
189 changes: 189 additions & 0 deletions browser_tests/fixtures/helpers/LiveCloudBilling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import {
zBillingOpStatusResponse,
zBillingStatusResponse,
zCurrentWorkspaceResponse,
zListSavedPaymentMethodsResponse,
zPreviewSubscribeResponse,
zSubscribeResponse
} from '@comfyorg/ingest-types/zod'
import type {
APIRequestContext,
Page,
Response,
TestInfo
} from '@playwright/test'
import { expect } from '@playwright/test'
import type { z } from 'zod'

import type { ComfyPage } from '@e2e/fixtures/ComfyPage'

export class LiveCloudBillingSession {
constructor(
private readonly request: APIRequestContext,
private readonly backend: string,
private readonly headers: Record<string, string>
) {}

async read<T>(path: string, schema: z.ZodType<T>): Promise<T> {
const response = await this.request
.get(new URL(path, this.backend).href, {
headers: this.headers,
maxRedirects: 0
})
.catch(() => {
throw new Error(`Cloud billing request failed: ${path}`)
})
expect(response.status(), `GET ${path}`).toBe(200)
return schema.parse(await response.json())
}

async assertNoCardAccount(testInfo: TestInfo) {
const workspace = await this.read(
'/api/workspaces/current',
zCurrentWorkspaceResponse
)
expect(workspace.type).toBe('personal')
expect(workspace.role).toBe('owner')
const status = await this.read(
'/api/billing/status',
zBillingStatusResponse
)
const methods = await this.read(
'/api/billing/payment-methods',
zListSavedPaymentMethodsResponse
)
await testInfo.attach('billing-preflight.json', {
body: JSON.stringify({
workspaceId: workspace.id,
isActive: status.is_active,
billingStatus: status.billing_status,
subscriptionTier: status.subscription_tier,
paymentMethodCount: methods.length,
backend: this.backend
}),
contentType: 'application/json'
})
if (status.is_active) expect(status.subscription_tier).toBe('FREE')
else
expect(['inactive', 'awaiting_payment_method']).toContain(
status.billing_status
)
expect(methods).toHaveLength(0)
}

async expectPending(operationId: string) {
await expect
.poll(() =>
this.read(
`/api/billing/ops/${encodeURIComponent(operationId)}`,
zBillingOpStatusResponse
)
)
.toMatchObject({
id: operationId,
status: 'pending',
phase: 'awaiting_payment_method'
})
}
}

export function matchesBillingResponse(
response: Response,
frontend: string,
path: string
) {
return response.url() === new URL(path, frontend).href
}

export class LiveCloudCheckout {
private readonly page: Page
readonly confirmation
readonly resumePayment
private readonly subscribe

constructor(
private readonly comfyPage: ComfyPage,
private readonly frontend: string
) {
this.page = comfyPage.page
this.confirmation = this.page.getByRole('dialog').filter({
has: this.page.getByRole('heading', {
name: 'Confirm your payment',
exact: true
})
})
this.resumePayment = this.confirmation.getByRole('button', {
name: 'Complete your payment',
exact: true
})
this.subscribe = this.confirmation.getByRole('button', {
name: 'Subscribe to Creator',
exact: true
})
}

async open() {
const [response] = await Promise.all([
this.page.waitForResponse((response) =>
matchesBillingResponse(
response,
this.frontend,
'/api/billing/preview-subscribe'
)
),
this.page.goto(`${this.frontend}/?pricing=creator&cycle=monthly`)
])
expect(response.status()).toBe(200)
const preview = zPreviewSubscribeResponse.parse(await response.json())
expect(preview.allowed).toBe(true)
expect(preview.new_plan.slug).toBe('creator-monthly')
await expect(
this.confirmation.getByText('Total due today', { exact: true })
).toBeVisible()
await expect(this.subscribe.or(this.resumePayment)).toBeEnabled()
}

async abandonCheckout(testInfo: TestInfo, resume = false) {
const action = resume
? this.resumePayment
: this.subscribe.or(this.resumePayment)
await expect(action).toBeEnabled()
const [response, checkout] = await Promise.all([
this.page.waitForResponse(
(response) =>
matchesBillingResponse(
response,
this.frontend,
'/api/billing/subscribe'
) && response.request().method() === 'POST'
),
this.page.waitForEvent('popup'),
action.click()
])
expect(response.status()).toBe(200)
const subscription = zSubscribeResponse.parse(await response.json())
expect(subscription.status).toBe('needs_payment_method')
expect(subscription.billing_op_id).not.toBe('')
const testCheckoutUrl =
/^https:\/\/checkout\.(?:stripe\.com|comfy\.org)\/c\/pay\/cs_test_/
expect(subscription.payment_method_url).toMatch(testCheckoutUrl)
await expect(checkout).toHaveURL(testCheckoutUrl)
await testInfo.attach('billing-operation.json', {
body: JSON.stringify({
operationId: subscription.billing_op_id,
status: subscription.status
}),
contentType: 'application/json'
})
await testInfo.attach('stripe-checkout.png', {
body: await checkout.screenshot(),
contentType: 'image/png'
})
await checkout.close()
return subscription.billing_op_id
}

async attachScreenshot(name: string) {
await this.comfyPage.attachScreenshot(name, { runInCI: true })
}
}
111 changes: 111 additions & 0 deletions browser_tests/fixtures/helpers/LiveCloudTopup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import {
zBillingBalanceResponse,
zBillingOpStatusResponse,
zBillingStatusResponse,
zCreateTopupResponse,
zListSavedPaymentMethodsResponse
} from '@comfyorg/ingest-types/zod'
import { expect } from '@playwright/test'
import type { TestInfo } from '@playwright/test'

import type { ComfyPage } from '@e2e/fixtures/ComfyPage'
import type { LiveCloudBillingSession } from '@e2e/fixtures/helpers/LiveCloudBilling'
import { matchesBillingResponse } from '@e2e/fixtures/helpers/LiveCloudBilling'

export class LiveCloudTopup {
private readonly dialog

constructor(
private readonly comfyPage: ComfyPage,
private readonly session: LiveCloudBillingSession,
private readonly frontend: string
) {
this.dialog = comfyPage.page.getByRole('dialog')
}

async open() {
const status = await this.session.read(
'/api/billing/status',
zBillingStatusResponse
)
expect(status.is_active).toBe(true)
expect(status.subscription_tier).toBe('CREATOR')
const methods = await this.session.read(
'/api/billing/payment-methods',
zListSavedPaymentMethodsResponse
)
expect(methods).not.toHaveLength(0)
await this.comfyPage.page.goto(`${this.frontend}/?topup=1`)
await this.dialog.getByRole('button', { name: '$10', exact: true }).click()
await this.dialog
.getByRole('button', { name: 'Add credits', exact: true })
.click()
await expect(
this.dialog.getByText(
'Your saved payment method is charged immediately.',
{ exact: true }
)
).toBeVisible()
}

async verifyPurchase(testInfo: TestInfo) {
const before = await this.session.read(
'/api/billing/balance',
zBillingBalanceResponse
)
const [response] = await Promise.all([
this.comfyPage.page.waitForResponse(
(response) =>
matchesBillingResponse(
response,
this.frontend,
'/api/billing/topup'
) && response.request().method() === 'POST'
),
this.dialog
.getByRole('button', { name: 'Pay $10.00', exact: true })
.click()
])
expect(response.status()).toBe(200)
const topup = zCreateTopupResponse.parse(await response.json())
expect(topup.amount_cents).toBe(1000n)
expect(topup.billing_op_id).not.toBe('')
await testInfo.attach('topup.json', {
body: JSON.stringify({
operationId: topup.billing_op_id,
topupId: topup.topup_id,
amountCents: Number(topup.amount_cents),
balanceBeforeCents: before.amount_micros
}),
contentType: 'application/json'
})
await expect(
this.comfyPage.page.getByText('Credits added successfully', {
exact: true
})
).toBeVisible()
await expect
.poll(async () => {
const operation = await this.session.read(
`/api/billing/ops/${encodeURIComponent(topup.billing_op_id)}`,
zBillingOpStatusResponse
)
return { id: operation.id, status: operation.status }
})
.toMatchObject({ id: topup.billing_op_id, status: 'succeeded' })
await expect
.poll(
async () =>
(
await this.session.read(
'/api/billing/balance',
zBillingBalanceResponse
)
).amount_micros
)
.toBe(before.amount_micros + Number(topup.amount_cents))
await this.comfyPage.attachScreenshot('saved-card-topup.png', {
runInCI: true
})
}
}
Loading
Loading