From 3f5f3cff31546c9371530c8b27f15b39b439481f Mon Sep 17 00:00:00 2001 From: huang47 <157390+huang47@users.noreply.github.com> Date: Fri, 11 Sep 2026 23:34:34 -0700 Subject: [PATCH] test: cover idempotent top-up retries --- .../fixtures/helpers/LiveCloudBilling.ts | 18 +++++ .../fixtures/helpers/LiveCloudTopup.ts | 81 ++++++++++++++++--- browser_tests/playwrightSelection.test.ts | 2 +- .../liveCloud/paid/topupIdempotency.spec.ts | 9 +++ 4 files changed, 97 insertions(+), 13 deletions(-) create mode 100644 browser_tests/tests/liveCloud/paid/topupIdempotency.spec.ts diff --git a/browser_tests/fixtures/helpers/LiveCloudBilling.ts b/browser_tests/fixtures/helpers/LiveCloudBilling.ts index 3421a27d596..c41f508f1eb 100644 --- a/browser_tests/fixtures/helpers/LiveCloudBilling.ts +++ b/browser_tests/fixtures/helpers/LiveCloudBilling.ts @@ -37,6 +37,24 @@ export class LiveCloudBillingSession { return schema.parse(await response.json()) } + async post( + path: string, + data: Record, + schema: z.ZodType + ): Promise { + 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', diff --git a/browser_tests/fixtures/helpers/LiveCloudTopup.ts b/browser_tests/fixtures/helpers/LiveCloudTopup.ts index 156e1a35199..fbc0164eb4b 100644 --- a/browser_tests/fixtures/helpers/LiveCloudTopup.ts +++ b/browser_tests/fixtures/helpers/LiveCloudTopup.ts @@ -2,6 +2,7 @@ import { zBillingBalanceResponse, zBillingOpStatusResponse, zBillingStatusResponse, + zCreateTopupRequest, zCreateTopupResponse, zListSavedPaymentMethodsResponse } from '@comfyorg/ingest-types/zod' @@ -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) => @@ -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, + balanceBeforeCents: number, + extra: Record = {} + ) { 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 () => @@ -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) } } diff --git a/browser_tests/playwrightSelection.test.ts b/browser_tests/playwrightSelection.test.ts index a85eb5879ea..92e255e04f3 100644 --- a/browser_tests/playwrightSelection.test.ts +++ b/browser_tests/playwrightSelection.test.ts @@ -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 }, diff --git a/browser_tests/tests/liveCloud/paid/topupIdempotency.spec.ts b/browser_tests/tests/liveCloud/paid/topupIdempotency.spec.ts new file mode 100644 index 00000000000..ae09f759943 --- /dev/null +++ b/browser_tests/tests/liveCloud/paid/topupIdempotency.spec.ts @@ -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) + }) +})