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
18 changes: 18 additions & 0 deletions browser_tests/fixtures/helpers/LiveCloudBilling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ export class LiveCloudBillingSession {
return schema.parse(await response.json())
}

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

async assertNoCardAccount(testInfo: TestInfo) {
const workspace = await this.read(
'/api/workspaces/current',
Expand Down
81 changes: 69 additions & 12 deletions browser_tests/fixtures/helpers/LiveCloudTopup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
zBillingBalanceResponse,
zBillingOpStatusResponse,
zBillingStatusResponse,
zCreateTopupRequest,
zCreateTopupResponse,
zListSavedPaymentMethodsResponse
} from '@comfyorg/ingest-types/zod'
Expand Down Expand Up @@ -53,6 +54,53 @@ export class LiveCloudTopup {
'/api/billing/balance',
zBillingBalanceResponse
)
const { topup } = await this.submitPurchase()
await this.attachPurchase(testInfo, topup, before.amount_micros)
await expect(
this.comfyPage.page.getByText('Credits added successfully', {
exact: true
})
).toBeVisible()
await this.expectPurchaseComplete(
topup.billing_op_id,
before.amount_micros + Number(topup.amount_cents)
)
await this.comfyPage.attachScreenshot('saved-card-topup.png', {
runInCI: true
})
}

async verifyIdempotentRetry(testInfo: TestInfo) {
const before = await this.session.read(
'/api/billing/balance',
zBillingBalanceResponse
)
const { request, topup } = await this.submitPurchase()
expect(request.idempotency_key).toBeTruthy()
if (!request.idempotency_key) {
throw new Error('Missing top-up idempotency key')
}
const retry = await this.session.post(
'/api/billing/topup',
{
amount_cents: Number(request.amount_cents),
idempotency_key: request.idempotency_key
},
zCreateTopupResponse
)
expect(retry.billing_op_id).toBe(topup.billing_op_id)
expect(retry.topup_id).toBe(topup.topup_id)
await this.attachPurchase(testInfo, topup, before.amount_micros, {
retryOperationId: retry.billing_op_id,
retryTopupId: retry.topup_id
})
await this.expectPurchaseComplete(
topup.billing_op_id,
before.amount_micros + Number(topup.amount_cents)
)
}

private async submitPurchase() {
const [response] = await Promise.all([
this.comfyPage.page.waitForResponse(
(response) =>
Expand All @@ -67,32 +115,44 @@ export class LiveCloudTopup {
.click()
])
expect(response.status()).toBe(200)
const request = zCreateTopupRequest.parse(response.request().postDataJSON())
const topup = zCreateTopupResponse.parse(await response.json())
expect(topup.amount_cents).toBe(1000n)
expect(topup.billing_op_id).not.toBe('')
return { request, topup }
}

private async attachPurchase(
testInfo: TestInfo,
topup: ReturnType<typeof zCreateTopupResponse.parse>,
balanceBeforeCents: number,
extra: Record<string, string> = {}
) {
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
balanceBeforeCents,
...extra
}),
contentType: 'application/json'
})
await expect(
this.comfyPage.page.getByText('Credits added successfully', {
exact: true
})
).toBeVisible()
}

private async expectPurchaseComplete(
operationId: string,
expectedBalanceCents: number
) {
await expect
.poll(async () => {
const operation = await this.session.read(
`/api/billing/ops/${encodeURIComponent(topup.billing_op_id)}`,
`/api/billing/ops/${encodeURIComponent(operationId)}`,
zBillingOpStatusResponse
)
return { id: operation.id, status: operation.status }
})
.toMatchObject({ id: topup.billing_op_id, status: 'succeeded' })
.toMatchObject({ id: operationId, status: 'succeeded' })
await expect
.poll(
async () =>
Expand All @@ -103,9 +163,6 @@ export class LiveCloudTopup {
)
).amount_micros
)
.toBe(before.amount_micros + Number(topup.amount_cents))
await this.comfyPage.attachScreenshot('saved-card-topup.png', {
runInCI: true
})
.toBe(expectedBalanceCents)
}
}
2 changes: 1 addition & 1 deletion browser_tests/playwrightSelection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Live billing opt-in', () => {
it.for([
{ enabled: '0', project: 'cloud-live', count: 0 },
{ enabled: '1', project: 'cloud-live', count: 3 },
{ enabled: '1', project: 'cloud-live-paid', count: 1 }
{ enabled: '1', project: 'cloud-live-paid', count: 2 }
])(
'collects $project only when explicitly enabled ($enabled)',
{ timeout: 90_000 },
Expand Down
9 changes: 9 additions & 0 deletions browser_tests/tests/liveCloud/paid/topupIdempotency.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { liveCloudPaidBillingFixture as test } from '@e2e/fixtures/liveCloudPaidBillingFixture'

test.describe('Real Cloud saved-card billing', { tag: ['@cloud-live'] }, () => {
test('retries one top-up without granting credits twice', async ({
topup
}, testInfo) => {
await topup.verifyIdempotentRetry(testInfo)
})
})
Loading