From ef181b80db4745c1e17e124097426fc8254192aa Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Wed, 16 Sep 2026 09:58:30 -0400 Subject: [PATCH 1/8] test(account): pin the generated billing contracts the SDK decodes --- .../src/core/billing/contracts/README.md | 8 + .../contracts/billingBalance.contract.test.ts | 53 +++++++ .../billingCapabilities.contract.test.ts | 122 ++++++++++++++++ .../billingOpStatus.contract.test.ts | 125 ++++++++++++++++ .../contracts/billingStatus.contract.test.ts | 127 ++++++++++++++++ .../cancelSubscription.contract.test.ts | 32 ++++ .../contracts/errorResponse.contract.test.ts | 32 ++++ .../contracts/paymentPortal.contract.test.ts | 27 ++++ .../previewSubscribe.contract.test.ts | 137 ++++++++++++++++++ .../contracts/resubscribe.contract.test.ts | 42 ++++++ .../contracts/subscribe.contract.test.ts | 98 +++++++++++++ .../billing/contracts/topup.contract.test.ts | 85 +++++++++++ .../contracts/topupCheckout.contract.test.ts | 110 ++++++++++++++ 13 files changed, 998 insertions(+) create mode 100644 packages/account/src/core/billing/contracts/README.md create mode 100644 packages/account/src/core/billing/contracts/billingBalance.contract.test.ts create mode 100644 packages/account/src/core/billing/contracts/billingCapabilities.contract.test.ts create mode 100644 packages/account/src/core/billing/contracts/billingOpStatus.contract.test.ts create mode 100644 packages/account/src/core/billing/contracts/billingStatus.contract.test.ts create mode 100644 packages/account/src/core/billing/contracts/cancelSubscription.contract.test.ts create mode 100644 packages/account/src/core/billing/contracts/errorResponse.contract.test.ts create mode 100644 packages/account/src/core/billing/contracts/paymentPortal.contract.test.ts create mode 100644 packages/account/src/core/billing/contracts/previewSubscribe.contract.test.ts create mode 100644 packages/account/src/core/billing/contracts/resubscribe.contract.test.ts create mode 100644 packages/account/src/core/billing/contracts/subscribe.contract.test.ts create mode 100644 packages/account/src/core/billing/contracts/topup.contract.test.ts create mode 100644 packages/account/src/core/billing/contracts/topupCheckout.contract.test.ts diff --git a/packages/account/src/core/billing/contracts/README.md b/packages/account/src/core/billing/contracts/README.md new file mode 100644 index 00000000000..9f504988ce8 --- /dev/null +++ b/packages/account/src/core/billing/contracts/README.md @@ -0,0 +1,8 @@ +# Generated billing contracts + +One file per generated schema the billing SDK core decodes. Each pins the +fields the core actually reads and the enum members it branches on, so a +regenerated `@comfyorg/ingest-types` that renames, drops, or widens one of them +fails a named test here instead of surfacing as a runtime `MALFORMED_RESPONSE`. + +A failure means the backend contract moved: fix the SDK, not the test. diff --git a/packages/account/src/core/billing/contracts/billingBalance.contract.test.ts b/packages/account/src/core/billing/contracts/billingBalance.contract.test.ts new file mode 100644 index 00000000000..0429f6dd498 --- /dev/null +++ b/packages/account/src/core/billing/contracts/billingBalance.contract.test.ts @@ -0,0 +1,53 @@ +import { zBillingBalanceResponse } from '@comfyorg/ingest-types/zod' +import { describe, expect, expectTypeOf, it } from 'vitest' +import type { z } from 'zod' + +type BalanceBody = z.input +type Balance = z.infer + +function balanceBody(overrides: Partial = {}): BalanceBody { + return { + amount_micros: 12_500_000, + currency: 'usd', + ...overrides + } +} + +describe('billing balance contract', () => { + it('accepts a balance carrying only the required fields', () => { + expect(zBillingBalanceResponse.safeParse(balanceBody())).toMatchObject({ + success: true + }) + }) + + it('reports micros as a number the reader compares against a baseline', () => { + expectTypeOf().toEqualTypeOf() + }) + + it('keeps every balance breakdown optional beside the headline amount', () => { + const breakdown = balanceBody({ + cloud_credit_balance_micros: 1, + effective_balance_micros: 2, + pending_charges_micros: 3, + prepaid_balance_micros: 4 + }) + + expect(zBillingBalanceResponse.safeParse(breakdown)).toMatchObject({ + success: true + }) + expectTypeOf< + Pick< + Balance, + | 'cloud_credit_balance_micros' + | 'effective_balance_micros' + | 'pending_charges_micros' + | 'prepaid_balance_micros' + > + >().toEqualTypeOf<{ + cloud_credit_balance_micros?: number + effective_balance_micros?: number + pending_charges_micros?: number + prepaid_balance_micros?: number + }>() + }) +}) diff --git a/packages/account/src/core/billing/contracts/billingCapabilities.contract.test.ts b/packages/account/src/core/billing/contracts/billingCapabilities.contract.test.ts new file mode 100644 index 00000000000..fecf5d192ec --- /dev/null +++ b/packages/account/src/core/billing/contracts/billingCapabilities.contract.test.ts @@ -0,0 +1,122 @@ +import { zBillingCapabilitiesResponse } from '@comfyorg/ingest-types/zod' +import type { + zBillingCapabilities, + zBillingCapabilityRolloutDefaults, + zBillingCapabilityScope +} from '@comfyorg/ingest-types/zod' +import { describe, expect, expectTypeOf, it } from 'vitest' +import type { z } from 'zod' + +type CapabilitiesResponseBody = z.input +type CapabilitiesResponse = z.infer +type Capabilities = z.infer +type CapabilityScope = z.infer +type RolloutDefaults = z.infer + +function capabilities(overrides: Partial = {}): Capabilities { + return { + can_cancel: true, + can_change_seats: true, + can_downgrade_to_personal: false, + can_invite_members: true, + can_reactivate: false, + can_subscribe_self_serve: true, + can_top_up: true, + ...overrides + } +} + +function capabilitiesBody( + overrides: Partial = {} +): CapabilitiesResponseBody { + return { + capabilities: capabilities(), + expires_at: '2024-06-15T12:01:00.000Z', + resolved_for: { user_id: 'uid-1', workspace_id: 'ws-1' }, + revision: 42n, + rollout_defaults_applied: { + can_downgrade_to_personal: false, + can_subscribe_self_serve: false, + can_top_up: false + }, + ...overrides + } +} + +describe('billing capabilities contract', () => { + it('accepts the body the capabilities reader decodes', () => { + expect( + zBillingCapabilitiesResponse.safeParse(capabilitiesBody()) + ).toMatchObject({ success: true }) + }) + + it('carries exactly the five fields the reader composes its own schema from', () => { + expectTypeOf().toEqualTypeOf< + | 'capabilities' + | 'expires_at' + | 'resolved_for' + | 'revision' + | 'rollout_defaults_applied' + >() + }) + + it('grants every capability as a boolean, including the top-up gate', () => { + expectTypeOf().toEqualTypeOf<{ + can_cancel: boolean + can_change_seats: boolean + can_downgrade_to_personal: boolean + can_invite_members: boolean + can_reactivate: boolean + can_subscribe_self_serve: boolean + can_top_up: boolean + }>() + }) + + it('resolves for a user and a workspace, both of which the reader matches against its scope', () => { + expectTypeOf().toEqualTypeOf<{ + user_id: string + workspace_id: string + }>() + }) + + it('reports rollout defaults for the three capabilities that have them', () => { + expectTypeOf().toEqualTypeOf<{ + can_downgrade_to_personal: boolean + can_subscribe_self_serve: boolean + can_top_up: boolean + }>() + }) + + it('dates the snapshot with a string the reader paces freshness from', () => { + expectTypeOf().toEqualTypeOf() + + const parsed = zBillingCapabilitiesResponse.safeParse(capabilitiesBody()) + + expect( + parsed.success && Number.isNaN(Date.parse(parsed.data.expires_at)) + ).toBe(false) + }) + + it('bounds the revision to the safe range the reader reads it back as a number over', () => { + expectTypeOf().toEqualTypeOf() + + const safe = { ...capabilitiesBody(), revision: Number.MAX_SAFE_INTEGER } + const unsafe = capabilitiesBody({ + revision: BigInt(Number.MAX_SAFE_INTEGER) + 1n + }) + + expect(zBillingCapabilitiesResponse.safeParse(safe)).toMatchObject({ + success: true + }) + expect(zBillingCapabilitiesResponse.safeParse(unsafe).success).toBe(false) + }) + + it('omits denied reasons, which the reader therefore decodes off the raw body', () => { + const parsed = zBillingCapabilitiesResponse.safeParse({ + ...capabilitiesBody(), + denied_reasons: { can_subscribe_self_serve: 'not_workspace_owner' } + }) + + expect(parsed.success && 'denied_reasons' in parsed.data).toBe(false) + }) +}) diff --git a/packages/account/src/core/billing/contracts/billingOpStatus.contract.test.ts b/packages/account/src/core/billing/contracts/billingOpStatus.contract.test.ts new file mode 100644 index 00000000000..a0a6153bb62 --- /dev/null +++ b/packages/account/src/core/billing/contracts/billingOpStatus.contract.test.ts @@ -0,0 +1,125 @@ +import { zBillingOpStatusResponse } from '@comfyorg/ingest-types/zod' +import { describe, expect, expectTypeOf, it } from 'vitest' +import type { z } from 'zod' + +type OpStatusBody = z.input +type OpStatus = z.infer + +function opStatusBody(overrides: Partial = {}): OpStatusBody { + return { + id: 'op-1', + started_at: '2024-06-15T12:00:00.000Z', + status: 'pending', + ...overrides + } +} + +/** Every value the reducer distinguishes; 'pending' is its stay-put branch. */ +const REDUCED_STATUSES = [ + 'pending', + 'succeeded', + 'failed', + 'reconciliation_needed' +] as const satisfies readonly OpStatus['status'][] + +/** Every value the pending reducer reads or `settleChallenge` assigns. */ +const AUTHENTICATION_STATES = [ + 'requires_action', + 'processing', + 'failed_retryable', + 'succeeded', + 'reconciliation_needed' +] as const satisfies readonly NonNullable[] + +describe('billing operation status contract', () => { + it('accepts a status carrying only the required fields', () => { + expect(zBillingOpStatusResponse.safeParse(opStatusBody())).toMatchObject({ + success: true + }) + }) + + it('carries exactly the four statuses the reducer handles', () => { + expectTypeOf().toEqualTypeOf< + 'pending' | 'succeeded' | 'failed' | 'reconciliation_needed' + >() + }) + + it.for(REDUCED_STATUSES)('accepts the %s status', (status) => { + expect( + zBillingOpStatusResponse.safeParse(opStatusBody({ status })) + ).toMatchObject({ success: true }) + }) + + it('carries exactly the five authentication states the reducer reads', () => { + expectTypeOf().toEqualTypeOf< + | 'requires_action' + | 'processing' + | 'failed_retryable' + | 'succeeded' + | 'reconciliation_needed' + | undefined + >() + }) + + it.for(AUTHENTICATION_STATES)( + 'accepts the %s authentication state', + (authentication_state) => { + expect( + zBillingOpStatusResponse.safeParse( + opStatusBody({ authentication_state }) + ) + ).toMatchObject({ success: true }) + } + ) + + it('carries the decline reasons a failed operation reports, including the generic fallback', () => { + expectTypeOf().toEqualTypeOf< + | 'card_declined' + | 'insufficient_funds' + | 'expired_card' + | 'incorrect_cvc' + | 'authentication_required' + | 'authentication_failed' + | 'processing_error' + | 'generic' + | undefined + >() + }) + + it('carries the recovery actions a host offers after a decline', () => { + expectTypeOf().toEqualTypeOf< + | 'retry' + | 'replace_payment_method' + | 'authenticate_payment' + | 'contact_support' + | undefined + >() + }) + + it('carries the server phases the pending state mirrors', () => { + expectTypeOf().toEqualTypeOf< + | 'awaiting_payment_method' + | 'awaiting_invoice_payment' + | 'in_progress' + | undefined + >() + }) + + it('identifies the operation and its continuations as plain strings', () => { + expectTypeOf().toEqualTypeOf() + expectTypeOf().toEqualTypeOf() + expectTypeOf().toEqualTypeOf< + string | undefined + >() + }) + + it('leaves retryable absent by default, so a decline is not retryable unless the server says so', () => { + expectTypeOf().toEqualTypeOf() + + const parsed = zBillingOpStatusResponse.safeParse( + opStatusBody({ status: 'failed' }) + ) + + expect(parsed.success && parsed.data.retryable).toBeUndefined() + }) +}) diff --git a/packages/account/src/core/billing/contracts/billingStatus.contract.test.ts b/packages/account/src/core/billing/contracts/billingStatus.contract.test.ts new file mode 100644 index 00000000000..028e45d85ad --- /dev/null +++ b/packages/account/src/core/billing/contracts/billingStatus.contract.test.ts @@ -0,0 +1,127 @@ +import { zBillingStatusResponse } from '@comfyorg/ingest-types/zod' +import { describe, expect, expectTypeOf, it } from 'vitest' +import type { z } from 'zod' + +type StatusBody = z.input +type Status = z.infer + +function statusBody(overrides: Partial = {}): StatusBody { + return { + has_funds: true, + is_active: true, + max_seats: 5, + occupied_seats: 1, + scheduled_change: null, + team_credit_stop: null, + ...overrides + } +} + +/** Every rail presentation routing distinguishes; only 'stripe' reaches the embedded challenge. */ +const BILLING_RAILS = [ + 'legacy_stripe', + 'metronome', + 'stripe' +] as const satisfies readonly NonNullable[] + +/** Every kind the lifecycle adopts from a server-reported pending operation. */ +const PENDING_OP_TYPES = [ + 'subscription', + 'topup' +] as const satisfies readonly NonNullable[] + +/** Every value subscribe eligibility distinguishes; only 'canceled' reroutes to resubscribe. */ +const SUBSCRIPTION_STATUSES = [ + 'active', + 'ended', + 'canceled' +] as const satisfies readonly NonNullable[] + +describe('billing status contract', () => { + it('accepts a status carrying only the required fields', () => { + expect(zBillingStatusResponse.safeParse(statusBody())).toMatchObject({ + success: true + }) + }) + + it('carries the three rails presentation routing distinguishes', () => { + expectTypeOf().toEqualTypeOf< + 'legacy_stripe' | 'metronome' | 'stripe' | undefined + >() + }) + + it.for(BILLING_RAILS)('accepts the %s rail', (billing_rail) => { + expect( + zBillingStatusResponse.safeParse(statusBody({ billing_rail })) + ).toMatchObject({ success: true }) + }) + + it('names the pending operation the lifecycle reattaches to', () => { + expectTypeOf().toEqualTypeOf< + string | undefined + >() + expectTypeOf().toEqualTypeOf< + 'subscription' | 'topup' | undefined + >() + }) + + it.for(PENDING_OP_TYPES)( + 'accepts a pending %s operation', + (pending_billing_op_type) => { + const body = statusBody({ + pending_billing_op_id: 'op-1', + pending_billing_op_type + }) + + expect(zBillingStatusResponse.safeParse(body)).toMatchObject({ + success: true + }) + } + ) + + it('carries the continuations the lifecycle adopts alongside a pending operation', () => { + expectTypeOf().toEqualTypeOf() + expectTypeOf().toEqualTypeOf< + string | undefined + >() + }) + + it('reports subscription eligibility as an active flag, a tier, and a status', () => { + expectTypeOf().toEqualTypeOf() + expectTypeOf().toEqualTypeOf< + 'active' | 'ended' | 'canceled' | undefined + >() + expectTypeOf().toEqualTypeOf< + | 'FREE' + | 'STANDARD' + | 'CREATOR' + | 'PRO' + | 'FOUNDERS_EDITION' + | 'TEAM' + | 'ENTERPRISE' + | undefined + >() + }) + + it.for(SUBSCRIPTION_STATUSES)( + 'accepts the %s subscription status', + (subscription_status) => { + const body = statusBody({ + subscription_status, + subscription_tier: 'PRO' + }) + + expect(zBillingStatusResponse.safeParse(body)).toMatchObject({ + success: true + }) + } + ) + + it('keeps FREE in the tier set eligibility reads as unpaid', () => { + const body = statusBody({ subscription_tier: 'FREE' }) + + expect(zBillingStatusResponse.safeParse(body)).toMatchObject({ + success: true + }) + }) +}) diff --git a/packages/account/src/core/billing/contracts/cancelSubscription.contract.test.ts b/packages/account/src/core/billing/contracts/cancelSubscription.contract.test.ts new file mode 100644 index 00000000000..9d3dd59343a --- /dev/null +++ b/packages/account/src/core/billing/contracts/cancelSubscription.contract.test.ts @@ -0,0 +1,32 @@ +import { zCancelSubscriptionResponse2 } from '@comfyorg/ingest-types/zod' +import { describe, expect, expectTypeOf, it } from 'vitest' +import type { z } from 'zod' + +type CancelBody = z.input +type Cancel = z.infer + +const CANCEL_VARIANTS = [ + { + variant: 'scheduled', + body: { billing_op_id: 'op-1', cancel_at: '2024-07-15T12:00:00.000Z' } + }, + { + variant: 'accepted', + body: { billing_op_id: 'op-1', status: 'pending' } + } +] as const satisfies readonly { variant: string; body: CancelBody }[] + +describe('cancel subscription contract', () => { + it.for(CANCEL_VARIANTS)( + 'reads the operation id off the $variant response', + ({ body }) => { + const parsed = zCancelSubscriptionResponse2.safeParse(body) + + expect(parsed.success && parsed.data.billing_op_id).toBe('op-1') + } + ) + + it('carries the operation id on every member of the response union', () => { + expectTypeOf().toEqualTypeOf() + }) +}) diff --git a/packages/account/src/core/billing/contracts/errorResponse.contract.test.ts b/packages/account/src/core/billing/contracts/errorResponse.contract.test.ts new file mode 100644 index 00000000000..95fecfb33bd --- /dev/null +++ b/packages/account/src/core/billing/contracts/errorResponse.contract.test.ts @@ -0,0 +1,32 @@ +import { zErrorResponse } from '@comfyorg/ingest-types/zod' +import { describe, expect, expectTypeOf, it } from 'vitest' +import type { z } from 'zod' + +type ErrorBody = z.input +type ErrorResponse = z.infer + +function errorBody(overrides: Partial = {}): ErrorBody { + return { + code: 'NO_PAYMENT_METHOD', + message: 'The workspace has no payment method on file.', + ...overrides + } +} + +describe('billing error body contract', () => { + it('accepts an error carrying only a code and a message', () => { + expect(zErrorResponse.safeParse(errorBody())).toMatchObject({ + success: true + }) + }) + + it('reports the code as an open string the commands match against their own closed set', () => { + expectTypeOf().toEqualTypeOf() + }) + + it('withholds the code from a body missing its message', () => { + expect( + zErrorResponse.safeParse({ code: 'NO_PAYMENT_METHOD' }).success + ).toBe(false) + }) +}) diff --git a/packages/account/src/core/billing/contracts/paymentPortal.contract.test.ts b/packages/account/src/core/billing/contracts/paymentPortal.contract.test.ts new file mode 100644 index 00000000000..6446e5af3f8 --- /dev/null +++ b/packages/account/src/core/billing/contracts/paymentPortal.contract.test.ts @@ -0,0 +1,27 @@ +import { zPaymentPortalResponse } from '@comfyorg/ingest-types/zod' +import { describe, expect, expectTypeOf, it } from 'vitest' +import type { z } from 'zod' + +type PortalBody = z.input +type Portal = z.infer + +function portalResponse(overrides: Partial = {}): PortalBody { + return { url: 'https://billing.comfy.org/portal/session-1', ...overrides } +} + +describe('payment portal contract', () => { + it('accepts a response carrying only the portal url', () => { + expect(zPaymentPortalResponse.safeParse(portalResponse())).toMatchObject({ + success: true + }) + }) + + it('requires the url, which the command still re-checks for https itself', () => { + expectTypeOf().toEqualTypeOf() + expect(zPaymentPortalResponse.safeParse({}).success).toBe(false) + expect( + zPaymentPortalResponse.safeParse(portalResponse({ url: 'http://host' })) + .success + ).toBe(true) + }) +}) diff --git a/packages/account/src/core/billing/contracts/previewSubscribe.contract.test.ts b/packages/account/src/core/billing/contracts/previewSubscribe.contract.test.ts new file mode 100644 index 00000000000..9303c0b88ae --- /dev/null +++ b/packages/account/src/core/billing/contracts/previewSubscribe.contract.test.ts @@ -0,0 +1,137 @@ +import { + zPreviewSubscribeRequest, + zPreviewSubscribeResponse +} from '@comfyorg/ingest-types/zod' +import { describe, expect, expectTypeOf, it } from 'vitest' +import type { z } from 'zod' + +type PreviewRequestBody = z.input +type PreviewRequest = z.infer +type PreviewResponseBody = z.input +type PreviewResponse = z.infer + +function previewRequest( + overrides: Partial = {} +): PreviewRequestBody { + return { plan_slug: 'pro_monthly', ...overrides } +} + +const planInfo: PreviewResponseBody['new_plan'] = { + credits_cents: 2000n, + duration: 'MONTHLY', + price_cents: 3000n, + seat_summary: { + seat_count: 1, + total_cost_cents: 3000n, + total_credits_cents: 2000n + }, + slug: 'pro_monthly', + tier: 'PRO' +} + +function previewResponse( + overrides: Partial = {} +): PreviewResponseBody { + return { + allowed: true, + cost_next_period_cents: 3000n, + cost_today_cents: 3000n, + credits_next_period_cents: 2000n, + credits_today_cents: 2000n, + effective_at: '2024-06-15T12:00:00.000Z', + is_immediate: true, + new_plan: planInfo, + transition_type: 'new_subscription', + ...overrides + } +} + +/** Every transition the quote can describe; a host renders all four. */ +const TRANSITION_TYPES = [ + 'new_subscription', + 'upgrade', + 'downgrade', + 'duration_change' +] as const satisfies readonly PreviewResponse['transition_type'][] + +describe('preview subscribe contract', () => { + it('accepts the request the command builds from its camel-cased input', () => { + const body = previewRequest({ + checkout_attempt_id: 'attempt-1', + promotion_code: 'LAUNCH', + team_credit_stop_id: 'stop-1' + }) + + expect(zPreviewSubscribeRequest.safeParse(body)).toMatchObject({ + success: true + }) + }) + + it('requires the plan slug and leaves every other request field optional', () => { + expectTypeOf().toEqualTypeOf<{ + plan_slug: string + checkout_attempt_id?: string + promotion_code?: string + team_credit_stop_id?: string + }>() + expect(zPreviewSubscribeRequest.safeParse({}).success).toBe(false) + }) + + it('accepts a quote carrying only the required fields', () => { + expect( + zPreviewSubscribeResponse.safeParse(previewResponse()) + ).toMatchObject({ success: true }) + }) + + it.for(TRANSITION_TYPES)('quotes the %s transition', (transition_type) => { + const parsed = zPreviewSubscribeResponse.safeParse( + previewResponse({ transition_type }) + ) + + expect(parsed.success && parsed.data.transition_type).toBe(transition_type) + }) + + it('states whether the change is allowed and when it takes effect', () => { + expectTypeOf().toEqualTypeOf() + expectTypeOf().toEqualTypeOf() + expectTypeOf().toEqualTypeOf() + }) + + it('reports money as an int64, so a host formats a bigint rather than a number', () => { + expectTypeOf().toEqualTypeOf() + expectTypeOf< + PreviewResponse['cost_next_period_cents'] + >().toEqualTypeOf() + expectTypeOf< + PreviewResponse['credits_today_cents'] + >().toEqualTypeOf() + expectTypeOf< + PreviewResponse['credits_next_period_cents'] + >().toEqualTypeOf() + }) + + it('coerces the JSON numbers the wire actually carries for those amounts', () => { + const wireBody = { ...previewResponse(), cost_today_cents: 3000 } + const parsed = zPreviewSubscribeResponse.safeParse(wireBody) + + expect(parsed.success && parsed.data.cost_today_cents).toBe(3000n) + }) + + it('describes discounts as product copy the host renders rather than matches', () => { + const body = previewResponse({ + discounts: [{ code: 'LAUNCH', kind: 'promotion', name: 'Launch offer' }] + }) + const parsed = zPreviewSubscribeResponse.safeParse(body) + + expect(parsed.success && parsed.data.discounts?.[0]).toMatchObject({ + code: 'LAUNCH', + kind: 'promotion' + }) + }) + + it('flags the reactivation confirmation the subscribe command is asked to resend', () => { + expectTypeOf< + PreviewResponse['requires_reactivation_confirmation'] + >().toEqualTypeOf() + }) +}) diff --git a/packages/account/src/core/billing/contracts/resubscribe.contract.test.ts b/packages/account/src/core/billing/contracts/resubscribe.contract.test.ts new file mode 100644 index 00000000000..d707d2cffdb --- /dev/null +++ b/packages/account/src/core/billing/contracts/resubscribe.contract.test.ts @@ -0,0 +1,42 @@ +import { zResubscribeResponse } from '@comfyorg/ingest-types/zod' +import { describe, expect, expectTypeOf, it } from 'vitest' +import type { z } from 'zod' + +type ResubscribeBody = z.input +type Resubscribe = z.infer + +function resubscribeResponse( + overrides: Partial = {} +): ResubscribeBody { + return { billing_op_id: 'op-1', status: 'active', ...overrides } +} + +const RESUBSCRIBE_STATUSES = [ + 'active', + 'pending' +] as const satisfies readonly Resubscribe['status'][] + +describe('resubscribe contract', () => { + it('accepts a response carrying only the operation id and its status', () => { + expect(zResubscribeResponse.safeParse(resubscribeResponse())).toMatchObject( + { + success: true + } + ) + }) + + it('returns the operation id the lifecycle adopts', () => { + expectTypeOf().toEqualTypeOf() + }) + + it.for(RESUBSCRIBE_STATUSES)( + 'adopts the operation regardless of the %s status', + (status) => { + const parsed = zResubscribeResponse.safeParse( + resubscribeResponse({ status }) + ) + + expect(parsed.success && parsed.data.billing_op_id).toBe('op-1') + } + ) +}) diff --git a/packages/account/src/core/billing/contracts/subscribe.contract.test.ts b/packages/account/src/core/billing/contracts/subscribe.contract.test.ts new file mode 100644 index 00000000000..e89a93e3dab --- /dev/null +++ b/packages/account/src/core/billing/contracts/subscribe.contract.test.ts @@ -0,0 +1,98 @@ +import { + zSubscribeRequest, + zSubscribeResponse +} from '@comfyorg/ingest-types/zod' +import { describe, expect, expectTypeOf, it } from 'vitest' +import type { z } from 'zod' + +type SubscribeRequestBody = z.input +type SubscribeRequest = z.infer +type SubscribeResponseBody = z.input +type SubscribeResponse = z.infer + +function subscribeRequest( + overrides: Partial = {} +): SubscribeRequestBody { + return { plan_slug: 'pro_monthly', ...overrides } +} + +function subscribeResponse( + overrides: Partial = {} +): SubscribeResponseBody { + return { billing_op_id: 'op-1', status: 'subscribed', ...overrides } +} + +/** Every outcome the command distinguishes; only 'needs_payment_method' demands a hosted page. */ +const SUBSCRIBE_STATUSES = [ + 'subscribed', + 'needs_payment_method', + 'pending_payment' +] as const satisfies readonly SubscribeResponse['status'][] + +describe('subscribe contract', () => { + it('accepts a request carrying only the plan slug', () => { + expect(zSubscribeRequest.safeParse(subscribeRequest())).toMatchObject({ + success: true + }) + }) + + it('carries the payment credentials the command deconflicts before sending', () => { + expectTypeOf().toEqualTypeOf< + string | undefined + >() + expectTypeOf().toEqualTypeOf< + string | undefined + >() + expectTypeOf().toEqualTypeOf< + string | undefined + >() + }) + + it('rejects a saved payment method that is not a Stripe payment-method id', () => { + const rejected = subscribeRequest({ saved_payment_method_id: 'card_123' }) + const accepted = subscribeRequest({ saved_payment_method_id: 'pm_123' }) + + expect(zSubscribeRequest.safeParse(rejected).success).toBe(false) + expect(zSubscribeRequest.safeParse(accepted)).toMatchObject({ + success: true + }) + }) + + it('accepts the reactivation confirmation the server asks callers to resend', () => { + expectTypeOf().toEqualTypeOf< + boolean | undefined + >() + + const body = subscribeRequest({ confirm_reactivation: true }) + + expect(zSubscribeRequest.safeParse(body)).toMatchObject({ success: true }) + }) + + it('returns the operation id the lifecycle adopts', () => { + expectTypeOf().toEqualTypeOf() + }) + + it('carries the three subscribe outcomes the command distinguishes', () => { + expectTypeOf().toEqualTypeOf< + 'subscribed' | 'needs_payment_method' | 'pending_payment' + >() + }) + + it.for(SUBSCRIBE_STATUSES)('accepts the %s outcome', (status) => { + expect( + zSubscribeResponse.safeParse(subscribeResponse({ status })) + ).toMatchObject({ success: true }) + }) + + it('leaves the hosted payment page optional, which the command reads as a missing url', () => { + expectTypeOf().toEqualTypeOf< + string | undefined + >() + + const parsed = zSubscribeResponse.safeParse( + subscribeResponse({ status: 'needs_payment_method' }) + ) + + expect(parsed.success && parsed.data.payment_method_url).toBeUndefined() + }) +}) diff --git a/packages/account/src/core/billing/contracts/topup.contract.test.ts b/packages/account/src/core/billing/contracts/topup.contract.test.ts new file mode 100644 index 00000000000..6f77ae44ffd --- /dev/null +++ b/packages/account/src/core/billing/contracts/topup.contract.test.ts @@ -0,0 +1,85 @@ +import { + zCreateTopupRequest, + zCreateTopupResponse +} from '@comfyorg/ingest-types/zod' +import { describe, expect, expectTypeOf, it } from 'vitest' +import type { z } from 'zod' + +type TopupRequestBody = z.input +type TopupRequest = z.infer +type TopupResponseBody = z.input +type TopupResponse = z.infer + +const MINIMUM_AMOUNT_CENTS = 500 + +function topupRequest( + overrides: Partial = {} +): TopupRequestBody { + return { amount_cents: BigInt(MINIMUM_AMOUNT_CENTS), ...overrides } +} + +function topupResponse( + overrides: Partial = {} +): TopupResponseBody { + return { + amount_cents: BigInt(MINIMUM_AMOUNT_CENTS), + billing_op_id: 'op-1', + status: 'pending', + topup_id: 'topup-1', + ...overrides + } +} + +const TOPUP_STATUSES = [ + 'pending', + 'completed', + 'failed' +] as const satisfies readonly TopupResponse['status'][] + +describe('topup contract', () => { + it('accepts a request carrying only the amount', () => { + expect(zCreateTopupRequest.safeParse(topupRequest())).toMatchObject({ + success: true + }) + }) + + it('validates the JSON number the command sends and coerces it to an int64 the transport never serializes', () => { + expectTypeOf().toEqualTypeOf() + + const parsed = zCreateTopupRequest.safeParse({ + amount_cents: MINIMUM_AMOUNT_CENTS + }) + + expect(parsed.success && parsed.data.amount_cents).toBe( + BigInt(MINIMUM_AMOUNT_CENTS) + ) + }) + + it('rejects an amount below the minimum the command reports as invalid', () => { + const below = { amount_cents: MINIMUM_AMOUNT_CENTS - 1 } + + expect(zCreateTopupRequest.safeParse(below).success).toBe(false) + expect( + zCreateTopupRequest.safeParse({ amount_cents: MINIMUM_AMOUNT_CENTS }) + ).toMatchObject({ success: true }) + }) + + it('carries the idempotency key the command mints per attempt', () => { + expectTypeOf().toEqualTypeOf< + string | undefined + >() + }) + + it('returns the operation id the lifecycle adopts', () => { + expectTypeOf().toEqualTypeOf() + }) + + it.for(TOPUP_STATUSES)( + 'adopts the operation regardless of the %s status', + (status) => { + const parsed = zCreateTopupResponse.safeParse(topupResponse({ status })) + + expect(parsed.success && parsed.data.billing_op_id).toBe('op-1') + } + ) +}) diff --git a/packages/account/src/core/billing/contracts/topupCheckout.contract.test.ts b/packages/account/src/core/billing/contracts/topupCheckout.contract.test.ts new file mode 100644 index 00000000000..37171ebae05 --- /dev/null +++ b/packages/account/src/core/billing/contracts/topupCheckout.contract.test.ts @@ -0,0 +1,110 @@ +import { + zCreateTopupCheckoutRequest, + zCreateTopupCheckoutResponse +} from '@comfyorg/ingest-types/zod' +import { describe, expect, expectTypeOf, it } from 'vitest' +import type { z } from 'zod' + +type CheckoutRequestBody = z.input +type CheckoutRequest = z.infer +type CheckoutResponseBody = z.input +type CheckoutResponse = z.infer + +const MINIMUM_AMOUNT_CENTS = 500 +const MAXIMUM_AMOUNT_CENTS = 473_900 +const RETURN_URL = 'https://app.comfy.org/billing/return' + +function checkoutRequest( + overrides: Partial = {} +): CheckoutRequestBody { + return { + amount_cents: BigInt(MINIMUM_AMOUNT_CENTS), + return_url: RETURN_URL, + ...overrides + } +} + +function checkoutResponse( + overrides: Partial = {} +): CheckoutResponseBody { + return { + checkout_url: 'https://checkout.stripe.com/c/session-1', + ...overrides + } +} + +/** + * The command tells an invalid amount from an invalid return url by the + * issue's own path, so the field names are part of the contract. + */ +const INVALID_REQUESTS = [ + { + rejected: 'an amount below the minimum', + field: 'amount_cents', + body: { amount_cents: MINIMUM_AMOUNT_CENTS - 1, return_url: RETURN_URL } + }, + { + rejected: 'an amount above the maximum', + field: 'amount_cents', + body: { amount_cents: MAXIMUM_AMOUNT_CENTS + 1, return_url: RETURN_URL } + }, + { + rejected: 'a return url that is not a url', + field: 'return_url', + body: { amount_cents: MINIMUM_AMOUNT_CENTS, return_url: 'not-a-url' } + } +] as const + +describe('hosted topup checkout contract', () => { + it('accepts a request carrying an in-range amount and a return url', () => { + expect( + zCreateTopupCheckoutRequest.safeParse(checkoutRequest()) + ).toMatchObject({ success: true }) + expect( + zCreateTopupCheckoutRequest.safeParse({ + amount_cents: MAXIMUM_AMOUNT_CENTS, + return_url: RETURN_URL + }) + ).toMatchObject({ success: true }) + }) + + it('coerces the JSON amount to an int64 the transport never serializes', () => { + expectTypeOf().toEqualTypeOf() + + const parsed = zCreateTopupCheckoutRequest.safeParse({ + amount_cents: MINIMUM_AMOUNT_CENTS, + return_url: RETURN_URL + }) + + expect(parsed.success && parsed.data.amount_cents).toBe( + BigInt(MINIMUM_AMOUNT_CENTS) + ) + }) + + it.for(INVALID_REQUESTS)('blames $field for $rejected', ({ field, body }) => { + const parsed = zCreateTopupCheckoutRequest.safeParse(body) + const paths = parsed.success + ? [] + : parsed.error.issues.map((issue) => issue.path[0]) + + expect(paths).toContain(field) + }) + + it('returns the hosted page the command still re-checks for https itself', () => { + expectTypeOf().toEqualTypeOf() + expect( + zCreateTopupCheckoutResponse.safeParse(checkoutResponse()) + ).toMatchObject({ success: true }) + expect(zCreateTopupCheckoutResponse.safeParse({}).success).toBe(false) + }) + + it('leaves the provider session id optional, so the host correlates only when it is sent', () => { + expectTypeOf().toEqualTypeOf< + string | undefined + >() + + const parsed = zCreateTopupCheckoutResponse.safeParse(checkoutResponse()) + + expect(parsed.success && parsed.data.session_id).toBeUndefined() + }) +}) From 0bdd9b57635182090170c59a13c24431aad93e65 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Wed, 16 Sep 2026 10:29:31 -0400 Subject: [PATCH 2/8] test(account): reject malformed timestamps and pin enum exhaustiveness in the billing contracts --- .../billingCapabilities.contract.test.ts | 9 +++++ .../billingOpStatus.contract.test.ts | 17 +++++---- .../contracts/billingStatus.contract.test.ts | 6 +-- .../cancelSubscription.contract.test.ts | 9 +++++ .../previewSubscribe.contract.test.ts | 38 +++++++++++++++++++ .../contracts/resubscribe.contract.test.ts | 6 +++ .../contracts/subscribe.contract.test.ts | 2 +- .../billing/contracts/topup.contract.test.ts | 6 +++ 8 files changed, 82 insertions(+), 11 deletions(-) diff --git a/packages/account/src/core/billing/contracts/billingCapabilities.contract.test.ts b/packages/account/src/core/billing/contracts/billingCapabilities.contract.test.ts index fecf5d192ec..dad4fa70090 100644 --- a/packages/account/src/core/billing/contracts/billingCapabilities.contract.test.ts +++ b/packages/account/src/core/billing/contracts/billingCapabilities.contract.test.ts @@ -97,6 +97,14 @@ describe('billing capabilities contract', () => { ).toBe(false) }) + it('rejects an expiry the reader could not pace freshness from', () => { + expect( + zBillingCapabilitiesResponse.safeParse( + capabilitiesBody({ expires_at: 'not-a-date' }) + ).success + ).toBe(false) + }) + it('bounds the revision to the safe range the reader reads it back as a number over', () => { expectTypeOf().toEqualTypeOf() @@ -117,6 +125,7 @@ describe('billing capabilities contract', () => { denied_reasons: { can_subscribe_self_serve: 'not_workspace_owner' } }) + expect(parsed).toMatchObject({ success: true }) expect(parsed.success && 'denied_reasons' in parsed.data).toBe(false) }) }) diff --git a/packages/account/src/core/billing/contracts/billingOpStatus.contract.test.ts b/packages/account/src/core/billing/contracts/billingOpStatus.contract.test.ts index a0a6153bb62..fbc55b5307c 100644 --- a/packages/account/src/core/billing/contracts/billingOpStatus.contract.test.ts +++ b/packages/account/src/core/billing/contracts/billingOpStatus.contract.test.ts @@ -38,9 +38,17 @@ describe('billing operation status contract', () => { }) }) + it('rejects a start time the reducer could not order the operation by', () => { + expect( + zBillingOpStatusResponse.safeParse( + opStatusBody({ started_at: 'not-a-date' }) + ).success + ).toBe(false) + }) + it('carries exactly the four statuses the reducer handles', () => { expectTypeOf().toEqualTypeOf< - 'pending' | 'succeeded' | 'failed' | 'reconciliation_needed' + (typeof REDUCED_STATUSES)[number] >() }) @@ -52,12 +60,7 @@ describe('billing operation status contract', () => { it('carries exactly the five authentication states the reducer reads', () => { expectTypeOf().toEqualTypeOf< - | 'requires_action' - | 'processing' - | 'failed_retryable' - | 'succeeded' - | 'reconciliation_needed' - | undefined + (typeof AUTHENTICATION_STATES)[number] | undefined >() }) diff --git a/packages/account/src/core/billing/contracts/billingStatus.contract.test.ts b/packages/account/src/core/billing/contracts/billingStatus.contract.test.ts index 028e45d85ad..b094035f736 100644 --- a/packages/account/src/core/billing/contracts/billingStatus.contract.test.ts +++ b/packages/account/src/core/billing/contracts/billingStatus.contract.test.ts @@ -46,7 +46,7 @@ describe('billing status contract', () => { it('carries the three rails presentation routing distinguishes', () => { expectTypeOf().toEqualTypeOf< - 'legacy_stripe' | 'metronome' | 'stripe' | undefined + (typeof BILLING_RAILS)[number] | undefined >() }) @@ -61,7 +61,7 @@ describe('billing status contract', () => { string | undefined >() expectTypeOf().toEqualTypeOf< - 'subscription' | 'topup' | undefined + (typeof PENDING_OP_TYPES)[number] | undefined >() }) @@ -89,7 +89,7 @@ describe('billing status contract', () => { it('reports subscription eligibility as an active flag, a tier, and a status', () => { expectTypeOf().toEqualTypeOf() expectTypeOf().toEqualTypeOf< - 'active' | 'ended' | 'canceled' | undefined + (typeof SUBSCRIPTION_STATUSES)[number] | undefined >() expectTypeOf().toEqualTypeOf< | 'FREE' diff --git a/packages/account/src/core/billing/contracts/cancelSubscription.contract.test.ts b/packages/account/src/core/billing/contracts/cancelSubscription.contract.test.ts index 9d3dd59343a..122d839e9f2 100644 --- a/packages/account/src/core/billing/contracts/cancelSubscription.contract.test.ts +++ b/packages/account/src/core/billing/contracts/cancelSubscription.contract.test.ts @@ -29,4 +29,13 @@ describe('cancel subscription contract', () => { it('carries the operation id on every member of the response union', () => { expectTypeOf().toEqualTypeOf() }) + + it('rejects a scheduled cancellation dated by an unreadable timestamp', () => { + const parsed = zCancelSubscriptionResponse2.safeParse({ + billing_op_id: 'op-1', + cancel_at: 'not-a-date' + }) + + expect(parsed.success).toBe(false) + }) }) diff --git a/packages/account/src/core/billing/contracts/previewSubscribe.contract.test.ts b/packages/account/src/core/billing/contracts/previewSubscribe.contract.test.ts index 9303c0b88ae..63715cc42a4 100644 --- a/packages/account/src/core/billing/contracts/previewSubscribe.contract.test.ts +++ b/packages/account/src/core/billing/contracts/previewSubscribe.contract.test.ts @@ -54,6 +54,17 @@ const TRANSITION_TYPES = [ 'duration_change' ] as const satisfies readonly PreviewResponse['transition_type'][] +/** Every amount the quote always carries, each one the host formats as money. */ +const MONEY_FIELDS = [ + 'cost_next_period_cents', + 'cost_today_cents', + 'credits_next_period_cents', + 'credits_today_cents' +] as const satisfies readonly (keyof PreviewResponseBody)[] + +const INT64_MIN = -9223372036854775808n +const INT64_MAX = 9223372036854775807n + describe('preview subscribe contract', () => { it('accepts the request the command builds from its camel-cased input', () => { const body = previewRequest({ @@ -83,6 +94,12 @@ describe('preview subscribe contract', () => { ).toMatchObject({ success: true }) }) + it('carries exactly the four transitions a host renders', () => { + expectTypeOf().toEqualTypeOf< + (typeof TRANSITION_TYPES)[number] + >() + }) + it.for(TRANSITION_TYPES)('quotes the %s transition', (transition_type) => { const parsed = zPreviewSubscribeResponse.safeParse( previewResponse({ transition_type }) @@ -97,6 +114,14 @@ describe('preview subscribe contract', () => { expectTypeOf().toEqualTypeOf() }) + it('rejects an effective date a host could not read the change from', () => { + expect( + zPreviewSubscribeResponse.safeParse( + previewResponse({ effective_at: 'not-a-date' }) + ).success + ).toBe(false) + }) + it('reports money as an int64, so a host formats a bigint rather than a number', () => { expectTypeOf().toEqualTypeOf() expectTypeOf< @@ -117,6 +142,19 @@ describe('preview subscribe contract', () => { expect(parsed.success && parsed.data.cost_today_cents).toBe(3000n) }) + it.for(MONEY_FIELDS)('bounds %s to the int64 range', (field) => { + const parseAmount = (amount: bigint) => + zPreviewSubscribeResponse.safeParse({ + ...previewResponse(), + [field]: amount + }) + + expect(parseAmount(INT64_MIN)).toMatchObject({ success: true }) + expect(parseAmount(INT64_MAX)).toMatchObject({ success: true }) + expect(parseAmount(INT64_MIN - 1n).success).toBe(false) + expect(parseAmount(INT64_MAX + 1n).success).toBe(false) + }) + it('describes discounts as product copy the host renders rather than matches', () => { const body = previewResponse({ discounts: [{ code: 'LAUNCH', kind: 'promotion', name: 'Launch offer' }] diff --git a/packages/account/src/core/billing/contracts/resubscribe.contract.test.ts b/packages/account/src/core/billing/contracts/resubscribe.contract.test.ts index d707d2cffdb..4d330f7aaff 100644 --- a/packages/account/src/core/billing/contracts/resubscribe.contract.test.ts +++ b/packages/account/src/core/billing/contracts/resubscribe.contract.test.ts @@ -29,6 +29,12 @@ describe('resubscribe contract', () => { expectTypeOf().toEqualTypeOf() }) + it('carries exactly the two statuses a resubscribe settles into', () => { + expectTypeOf().toEqualTypeOf< + (typeof RESUBSCRIBE_STATUSES)[number] + >() + }) + it.for(RESUBSCRIBE_STATUSES)( 'adopts the operation regardless of the %s status', (status) => { diff --git a/packages/account/src/core/billing/contracts/subscribe.contract.test.ts b/packages/account/src/core/billing/contracts/subscribe.contract.test.ts index e89a93e3dab..b989758bac2 100644 --- a/packages/account/src/core/billing/contracts/subscribe.contract.test.ts +++ b/packages/account/src/core/billing/contracts/subscribe.contract.test.ts @@ -74,7 +74,7 @@ describe('subscribe contract', () => { it('carries the three subscribe outcomes the command distinguishes', () => { expectTypeOf().toEqualTypeOf< - 'subscribed' | 'needs_payment_method' | 'pending_payment' + (typeof SUBSCRIBE_STATUSES)[number] >() }) diff --git a/packages/account/src/core/billing/contracts/topup.contract.test.ts b/packages/account/src/core/billing/contracts/topup.contract.test.ts index 6f77ae44ffd..8aef6f7e607 100644 --- a/packages/account/src/core/billing/contracts/topup.contract.test.ts +++ b/packages/account/src/core/billing/contracts/topup.contract.test.ts @@ -74,6 +74,12 @@ describe('topup contract', () => { expectTypeOf().toEqualTypeOf() }) + it('carries exactly the three statuses a top-up settles into', () => { + expectTypeOf().toEqualTypeOf< + (typeof TOPUP_STATUSES)[number] + >() + }) + it.for(TOPUP_STATUSES)( 'adopts the operation regardless of the %s status', (status) => { From 967e59446e8b916a76c17f1267bfe80b984c1366 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Wed, 16 Sep 2026 10:47:08 -0400 Subject: [PATCH 3/8] test(account): pin the optional preview amounts too --- .../previewSubscribe.contract.test.ts | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/account/src/core/billing/contracts/previewSubscribe.contract.test.ts b/packages/account/src/core/billing/contracts/previewSubscribe.contract.test.ts index 63715cc42a4..77440059d20 100644 --- a/packages/account/src/core/billing/contracts/previewSubscribe.contract.test.ts +++ b/packages/account/src/core/billing/contracts/previewSubscribe.contract.test.ts @@ -54,12 +54,14 @@ const TRANSITION_TYPES = [ 'duration_change' ] as const satisfies readonly PreviewResponse['transition_type'][] -/** Every amount the quote always carries, each one the host formats as money. */ +/** Every amount the quote can carry, each one the host formats as money. */ const MONEY_FIELDS = [ + 'amount_due_cents', 'cost_next_period_cents', 'cost_today_cents', 'credits_next_period_cents', - 'credits_today_cents' + 'credits_today_cents', + 'renewal_amount_cents' ] as const satisfies readonly (keyof PreviewResponseBody)[] const INT64_MIN = -9223372036854775808n @@ -133,14 +135,25 @@ describe('preview subscribe contract', () => { expectTypeOf< PreviewResponse['credits_next_period_cents'] >().toEqualTypeOf() + expectTypeOf().toEqualTypeOf< + bigint | undefined + >() + expectTypeOf().toEqualTypeOf< + bigint | undefined + >() }) - it('coerces the JSON numbers the wire actually carries for those amounts', () => { - const wireBody = { ...previewResponse(), cost_today_cents: 3000 } - const parsed = zPreviewSubscribeResponse.safeParse(wireBody) + it.for(MONEY_FIELDS)( + 'coerces the JSON number the wire actually carries for %s', + (field) => { + const parsed = zPreviewSubscribeResponse.safeParse({ + ...previewResponse(), + [field]: 3000 + }) - expect(parsed.success && parsed.data.cost_today_cents).toBe(3000n) - }) + expect(parsed.success && parsed.data[field]).toBe(3000n) + } + ) it.for(MONEY_FIELDS)('bounds %s to the int64 range', (field) => { const parseAmount = (amount: bigint) => From 0286f509f8a40df3a6c16673e7cb7ee77ff112fa Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Thu, 17 Sep 2026 01:39:51 -0400 Subject: [PATCH 4/8] test(account-core): land the generated billing contract pins in the live package (FE-2217) The rename of packages/account to packages/account-core landed on main while this branch was open, so all thirteen files were about to merge into a directory main no longer has a package.json for: pnpm's packages/** glob skips it, the account-core vitest project includes src/core/** relative to its own package, and the root vitest include is src/**. The merge stays clean and CI stays green, and the regen tripwire the PR exists for never runs again. Pure path move, no import edits: every file imports only @comfyorg/ingest-types/zod, vitest and type { z } from 'zod'. --- .../src/core/billing/contracts/README.md | 0 .../src/core/billing/contracts/billingBalance.contract.test.ts | 0 .../core/billing/contracts/billingCapabilities.contract.test.ts | 0 .../src/core/billing/contracts/billingOpStatus.contract.test.ts | 0 .../src/core/billing/contracts/billingStatus.contract.test.ts | 0 .../core/billing/contracts/cancelSubscription.contract.test.ts | 0 .../src/core/billing/contracts/errorResponse.contract.test.ts | 0 .../src/core/billing/contracts/paymentPortal.contract.test.ts | 0 .../src/core/billing/contracts/previewSubscribe.contract.test.ts | 0 .../src/core/billing/contracts/resubscribe.contract.test.ts | 0 .../src/core/billing/contracts/subscribe.contract.test.ts | 0 .../src/core/billing/contracts/topup.contract.test.ts | 0 .../src/core/billing/contracts/topupCheckout.contract.test.ts | 0 13 files changed, 0 insertions(+), 0 deletions(-) rename packages/{account => account-core}/src/core/billing/contracts/README.md (100%) rename packages/{account => account-core}/src/core/billing/contracts/billingBalance.contract.test.ts (100%) rename packages/{account => account-core}/src/core/billing/contracts/billingCapabilities.contract.test.ts (100%) rename packages/{account => account-core}/src/core/billing/contracts/billingOpStatus.contract.test.ts (100%) rename packages/{account => account-core}/src/core/billing/contracts/billingStatus.contract.test.ts (100%) rename packages/{account => account-core}/src/core/billing/contracts/cancelSubscription.contract.test.ts (100%) rename packages/{account => account-core}/src/core/billing/contracts/errorResponse.contract.test.ts (100%) rename packages/{account => account-core}/src/core/billing/contracts/paymentPortal.contract.test.ts (100%) rename packages/{account => account-core}/src/core/billing/contracts/previewSubscribe.contract.test.ts (100%) rename packages/{account => account-core}/src/core/billing/contracts/resubscribe.contract.test.ts (100%) rename packages/{account => account-core}/src/core/billing/contracts/subscribe.contract.test.ts (100%) rename packages/{account => account-core}/src/core/billing/contracts/topup.contract.test.ts (100%) rename packages/{account => account-core}/src/core/billing/contracts/topupCheckout.contract.test.ts (100%) diff --git a/packages/account/src/core/billing/contracts/README.md b/packages/account-core/src/core/billing/contracts/README.md similarity index 100% rename from packages/account/src/core/billing/contracts/README.md rename to packages/account-core/src/core/billing/contracts/README.md diff --git a/packages/account/src/core/billing/contracts/billingBalance.contract.test.ts b/packages/account-core/src/core/billing/contracts/billingBalance.contract.test.ts similarity index 100% rename from packages/account/src/core/billing/contracts/billingBalance.contract.test.ts rename to packages/account-core/src/core/billing/contracts/billingBalance.contract.test.ts diff --git a/packages/account/src/core/billing/contracts/billingCapabilities.contract.test.ts b/packages/account-core/src/core/billing/contracts/billingCapabilities.contract.test.ts similarity index 100% rename from packages/account/src/core/billing/contracts/billingCapabilities.contract.test.ts rename to packages/account-core/src/core/billing/contracts/billingCapabilities.contract.test.ts diff --git a/packages/account/src/core/billing/contracts/billingOpStatus.contract.test.ts b/packages/account-core/src/core/billing/contracts/billingOpStatus.contract.test.ts similarity index 100% rename from packages/account/src/core/billing/contracts/billingOpStatus.contract.test.ts rename to packages/account-core/src/core/billing/contracts/billingOpStatus.contract.test.ts diff --git a/packages/account/src/core/billing/contracts/billingStatus.contract.test.ts b/packages/account-core/src/core/billing/contracts/billingStatus.contract.test.ts similarity index 100% rename from packages/account/src/core/billing/contracts/billingStatus.contract.test.ts rename to packages/account-core/src/core/billing/contracts/billingStatus.contract.test.ts diff --git a/packages/account/src/core/billing/contracts/cancelSubscription.contract.test.ts b/packages/account-core/src/core/billing/contracts/cancelSubscription.contract.test.ts similarity index 100% rename from packages/account/src/core/billing/contracts/cancelSubscription.contract.test.ts rename to packages/account-core/src/core/billing/contracts/cancelSubscription.contract.test.ts diff --git a/packages/account/src/core/billing/contracts/errorResponse.contract.test.ts b/packages/account-core/src/core/billing/contracts/errorResponse.contract.test.ts similarity index 100% rename from packages/account/src/core/billing/contracts/errorResponse.contract.test.ts rename to packages/account-core/src/core/billing/contracts/errorResponse.contract.test.ts diff --git a/packages/account/src/core/billing/contracts/paymentPortal.contract.test.ts b/packages/account-core/src/core/billing/contracts/paymentPortal.contract.test.ts similarity index 100% rename from packages/account/src/core/billing/contracts/paymentPortal.contract.test.ts rename to packages/account-core/src/core/billing/contracts/paymentPortal.contract.test.ts diff --git a/packages/account/src/core/billing/contracts/previewSubscribe.contract.test.ts b/packages/account-core/src/core/billing/contracts/previewSubscribe.contract.test.ts similarity index 100% rename from packages/account/src/core/billing/contracts/previewSubscribe.contract.test.ts rename to packages/account-core/src/core/billing/contracts/previewSubscribe.contract.test.ts diff --git a/packages/account/src/core/billing/contracts/resubscribe.contract.test.ts b/packages/account-core/src/core/billing/contracts/resubscribe.contract.test.ts similarity index 100% rename from packages/account/src/core/billing/contracts/resubscribe.contract.test.ts rename to packages/account-core/src/core/billing/contracts/resubscribe.contract.test.ts diff --git a/packages/account/src/core/billing/contracts/subscribe.contract.test.ts b/packages/account-core/src/core/billing/contracts/subscribe.contract.test.ts similarity index 100% rename from packages/account/src/core/billing/contracts/subscribe.contract.test.ts rename to packages/account-core/src/core/billing/contracts/subscribe.contract.test.ts diff --git a/packages/account/src/core/billing/contracts/topup.contract.test.ts b/packages/account-core/src/core/billing/contracts/topup.contract.test.ts similarity index 100% rename from packages/account/src/core/billing/contracts/topup.contract.test.ts rename to packages/account-core/src/core/billing/contracts/topup.contract.test.ts diff --git a/packages/account/src/core/billing/contracts/topupCheckout.contract.test.ts b/packages/account-core/src/core/billing/contracts/topupCheckout.contract.test.ts similarity index 100% rename from packages/account/src/core/billing/contracts/topupCheckout.contract.test.ts rename to packages/account-core/src/core/billing/contracts/topupCheckout.contract.test.ts From cf6681474885aa5955c4e8a7f446fec21e4d58b5 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Thu, 17 Sep 2026 01:44:50 -0400 Subject: [PATCH 5/8] test(account-core): name the capabilities pins after the schema they parse (FE-2217) zBillingCapabilitiesResponse has no non-generated reference: the reader composes its own CapabilitiesBodySchema from three generated pieces with expires_at as a plain string and revision as a number, and capabilities.test.ts pins that an unparseable expiry falls back to the fixed lifetime rather than failing the read. Two rows here claimed the opposite, so a regen that widened the date-time format would have sent a maintainer to tighten a deliberate leniency, with README pointing at the SDK. Both rows keep their assertions; only the names and the README caveat change. --- packages/account-core/src/core/billing/contracts/README.md | 4 +++- .../billing/contracts/billingCapabilities.contract.test.ts | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/account-core/src/core/billing/contracts/README.md b/packages/account-core/src/core/billing/contracts/README.md index 9f504988ce8..4079aaa42a1 100644 --- a/packages/account-core/src/core/billing/contracts/README.md +++ b/packages/account-core/src/core/billing/contracts/README.md @@ -5,4 +5,6 @@ fields the core actually reads and the enum members it branches on, so a regenerated `@comfyorg/ingest-types` that renames, drops, or widens one of them fails a named test here instead of surfacing as a runtime `MALFORMED_RESPONSE`. -A failure means the backend contract moved: fix the SDK, not the test. +A failure means the backend contract moved: fix the SDK, not the test. Where +the SDK reads a field more leniently than the generated schema does, the test +name says so — that row pins the generated shape, not the SDK's. diff --git a/packages/account-core/src/core/billing/contracts/billingCapabilities.contract.test.ts b/packages/account-core/src/core/billing/contracts/billingCapabilities.contract.test.ts index dad4fa70090..c7086eb43b0 100644 --- a/packages/account-core/src/core/billing/contracts/billingCapabilities.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/billingCapabilities.contract.test.ts @@ -44,7 +44,7 @@ function capabilitiesBody( } describe('billing capabilities contract', () => { - it('accepts the body the capabilities reader decodes', () => { + it('accepts the body the capabilities endpoint returns', () => { expect( zBillingCapabilitiesResponse.safeParse(capabilitiesBody()) ).toMatchObject({ success: true }) @@ -97,7 +97,7 @@ describe('billing capabilities contract', () => { ).toBe(false) }) - it('rejects an expiry the reader could not pace freshness from', () => { + it('requires a datetime expiry, which the reader re-decodes leniently as a plain string', () => { expect( zBillingCapabilitiesResponse.safeParse( capabilitiesBody({ expires_at: 'not-a-date' }) From 1dfe2536f49295137bcee749059b36fc3b207b37 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Thu, 17 Sep 2026 02:15:45 -0400 Subject: [PATCH 6/8] FE-2217 test(account-core): make the contract titles match what they pin Retitle the rows that named SDK behaviour their assertion never exercised, split the url rows so the acceptance each one proves is the one its title claims, and drop a freshness assertion that could not fail. --- .../billingCapabilities.contract.test.ts | 6 ------ .../contracts/billingOpStatus.contract.test.ts | 2 +- .../contracts/errorResponse.contract.test.ts | 2 +- .../contracts/paymentPortal.contract.test.ts | 11 ++++++++--- .../contracts/resubscribe.contract.test.ts | 2 +- .../core/billing/contracts/topup.contract.test.ts | 2 +- .../contracts/topupCheckout.contract.test.ts | 15 ++++++++++++++- 7 files changed, 26 insertions(+), 14 deletions(-) diff --git a/packages/account-core/src/core/billing/contracts/billingCapabilities.contract.test.ts b/packages/account-core/src/core/billing/contracts/billingCapabilities.contract.test.ts index c7086eb43b0..007fc2d4a45 100644 --- a/packages/account-core/src/core/billing/contracts/billingCapabilities.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/billingCapabilities.contract.test.ts @@ -89,12 +89,6 @@ describe('billing capabilities contract', () => { it('dates the snapshot with a string the reader paces freshness from', () => { expectTypeOf().toEqualTypeOf() - - const parsed = zBillingCapabilitiesResponse.safeParse(capabilitiesBody()) - - expect( - parsed.success && Number.isNaN(Date.parse(parsed.data.expires_at)) - ).toBe(false) }) it('requires a datetime expiry, which the reader re-decodes leniently as a plain string', () => { diff --git a/packages/account-core/src/core/billing/contracts/billingOpStatus.contract.test.ts b/packages/account-core/src/core/billing/contracts/billingOpStatus.contract.test.ts index fbc55b5307c..c0f9d842f33 100644 --- a/packages/account-core/src/core/billing/contracts/billingOpStatus.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/billingOpStatus.contract.test.ts @@ -38,7 +38,7 @@ describe('billing operation status contract', () => { }) }) - it('rejects a start time the reducer could not order the operation by', () => { + it('requires a datetime start time, so a malformed one fails the whole poll', () => { expect( zBillingOpStatusResponse.safeParse( opStatusBody({ started_at: 'not-a-date' }) diff --git a/packages/account-core/src/core/billing/contracts/errorResponse.contract.test.ts b/packages/account-core/src/core/billing/contracts/errorResponse.contract.test.ts index 95fecfb33bd..a276b389b92 100644 --- a/packages/account-core/src/core/billing/contracts/errorResponse.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/errorResponse.contract.test.ts @@ -24,7 +24,7 @@ describe('billing error body contract', () => { expectTypeOf().toEqualTypeOf() }) - it('withholds the code from a body missing its message', () => { + it('rejects a body without a message', () => { expect( zErrorResponse.safeParse({ code: 'NO_PAYMENT_METHOD' }).success ).toBe(false) diff --git a/packages/account-core/src/core/billing/contracts/paymentPortal.contract.test.ts b/packages/account-core/src/core/billing/contracts/paymentPortal.contract.test.ts index 6446e5af3f8..13e24bcdd4e 100644 --- a/packages/account-core/src/core/billing/contracts/paymentPortal.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/paymentPortal.contract.test.ts @@ -16,12 +16,17 @@ describe('payment portal contract', () => { }) }) - it('requires the url, which the command still re-checks for https itself', () => { + it('requires the url', () => { expectTypeOf().toEqualTypeOf() expect(zPaymentPortalResponse.safeParse({}).success).toBe(false) + }) + + it('validates the url no further than a string, so the command checks the scheme itself', () => { expect( zPaymentPortalResponse.safeParse(portalResponse({ url: 'http://host' })) - .success - ).toBe(true) + ).toMatchObject({ success: true }) + expect( + zPaymentPortalResponse.safeParse(portalResponse({ url: 'not-a-url' })) + ).toMatchObject({ success: true }) }) }) diff --git a/packages/account-core/src/core/billing/contracts/resubscribe.contract.test.ts b/packages/account-core/src/core/billing/contracts/resubscribe.contract.test.ts index 4d330f7aaff..37906a96e8e 100644 --- a/packages/account-core/src/core/billing/contracts/resubscribe.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/resubscribe.contract.test.ts @@ -36,7 +36,7 @@ describe('resubscribe contract', () => { }) it.for(RESUBSCRIBE_STATUSES)( - 'adopts the operation regardless of the %s status', + 'carries the operation id alongside the %s status', (status) => { const parsed = zResubscribeResponse.safeParse( resubscribeResponse({ status }) diff --git a/packages/account-core/src/core/billing/contracts/topup.contract.test.ts b/packages/account-core/src/core/billing/contracts/topup.contract.test.ts index 8aef6f7e607..c86050564d5 100644 --- a/packages/account-core/src/core/billing/contracts/topup.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/topup.contract.test.ts @@ -81,7 +81,7 @@ describe('topup contract', () => { }) it.for(TOPUP_STATUSES)( - 'adopts the operation regardless of the %s status', + 'carries the operation id alongside the %s status', (status) => { const parsed = zCreateTopupResponse.safeParse(topupResponse({ status })) diff --git a/packages/account-core/src/core/billing/contracts/topupCheckout.contract.test.ts b/packages/account-core/src/core/billing/contracts/topupCheckout.contract.test.ts index 37171ebae05..1c7d0f65b00 100644 --- a/packages/account-core/src/core/billing/contracts/topupCheckout.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/topupCheckout.contract.test.ts @@ -90,7 +90,7 @@ describe('hosted topup checkout contract', () => { expect(paths).toContain(field) }) - it('returns the hosted page the command still re-checks for https itself', () => { + it('requires the hosted page url', () => { expectTypeOf().toEqualTypeOf() expect( zCreateTopupCheckoutResponse.safeParse(checkoutResponse()) @@ -98,6 +98,19 @@ describe('hosted topup checkout contract', () => { expect(zCreateTopupCheckoutResponse.safeParse({}).success).toBe(false) }) + it('validates the hosted page as a url but not as https, which the command re-checks itself', () => { + expect( + zCreateTopupCheckoutResponse.safeParse( + checkoutResponse({ checkout_url: 'http://host' }) + ) + ).toMatchObject({ success: true }) + expect( + zCreateTopupCheckoutResponse.safeParse( + checkoutResponse({ checkout_url: 'not-a-url' }) + ).success + ).toBe(false) + }) + it('leaves the provider session id optional, so the host correlates only when it is sent', () => { expectTypeOf().toEqualTypeOf< string | undefined From 278fcc003dd9368d089b12bdb033adab965815e9 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Thu, 17 Sep 2026 02:20:21 -0400 Subject: [PATCH 7/8] FE-2217 test(account-core): stop reporting the type pins as vitest tests The type pins were wrapped in it() blocks vitest could only pass: nothing here runs vitest --typecheck, so 30 of the folder's reported tests asserted nothing at runtime. Hoist them to module scope, where the package typecheck is what fails, and say so in the README. --- .../src/core/billing/contracts/README.md | 13 ++- .../contracts/billingBalance.contract.test.ts | 35 ++++--- .../billingCapabilities.contract.test.ts | 80 +++++++-------- .../billingOpStatus.contract.test.ts | 98 ++++++++----------- .../contracts/billingStatus.contract.test.ts | 72 +++++++------- .../cancelSubscription.contract.test.ts | 9 +- .../contracts/errorResponse.contract.test.ts | 9 +- .../contracts/paymentPortal.contract.test.ts | 4 +- .../previewSubscribe.contract.test.ts | 81 +++++++-------- .../contracts/resubscribe.contract.test.ts | 18 ++-- .../contracts/subscribe.contract.test.ts | 54 +++++----- .../billing/contracts/topup.contract.test.ts | 32 +++--- .../contracts/topupCheckout.contract.test.ts | 16 +-- 13 files changed, 243 insertions(+), 278 deletions(-) diff --git a/packages/account-core/src/core/billing/contracts/README.md b/packages/account-core/src/core/billing/contracts/README.md index 4079aaa42a1..5afe821904c 100644 --- a/packages/account-core/src/core/billing/contracts/README.md +++ b/packages/account-core/src/core/billing/contracts/README.md @@ -1,9 +1,18 @@ # Generated billing contracts -One file per generated schema the billing SDK core decodes. Each pins the +One file per endpoint contract the billing SDK core decodes. Each pins the fields the core actually reads and the enum members it branches on, so a regenerated `@comfyorg/ingest-types` that renames, drops, or widens one of them -fails a named test here instead of surfacing as a runtime `MALFORMED_RESPONSE`. +fails here instead of surfacing as a runtime `MALFORMED_RESPONSE`. + +Each file pins its contract twice, and the two halves fail in different jobs: + +- `describe` blocks hold the runtime pins — what the generated schema accepts + and rejects. `pnpm --filter @comfyorg/account-core test` fails on those. +- Module-scope `expectTypeOf` calls hold the type pins — the shapes the SDK + reads off a parsed body. Vitest never evaluates them; the package + `typecheck` script is what fails, as a `TS2344`. They sit outside `it` so + the vitest report never counts a pass vitest did not check. A failure means the backend contract moved: fix the SDK, not the test. Where the SDK reads a field more leniently than the generated schema does, the test diff --git a/packages/account-core/src/core/billing/contracts/billingBalance.contract.test.ts b/packages/account-core/src/core/billing/contracts/billingBalance.contract.test.ts index 0429f6dd498..c36126b8013 100644 --- a/packages/account-core/src/core/billing/contracts/billingBalance.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/billingBalance.contract.test.ts @@ -13,6 +13,23 @@ function balanceBody(overrides: Partial = {}): BalanceBody { } } +// Compile-time pins: a regen that moves these fails the package typecheck. +expectTypeOf().toEqualTypeOf() +expectTypeOf< + Pick< + Balance, + | 'cloud_credit_balance_micros' + | 'effective_balance_micros' + | 'pending_charges_micros' + | 'prepaid_balance_micros' + > +>().toEqualTypeOf<{ + cloud_credit_balance_micros?: number + effective_balance_micros?: number + pending_charges_micros?: number + prepaid_balance_micros?: number +}>() + describe('billing balance contract', () => { it('accepts a balance carrying only the required fields', () => { expect(zBillingBalanceResponse.safeParse(balanceBody())).toMatchObject({ @@ -20,10 +37,6 @@ describe('billing balance contract', () => { }) }) - it('reports micros as a number the reader compares against a baseline', () => { - expectTypeOf().toEqualTypeOf() - }) - it('keeps every balance breakdown optional beside the headline amount', () => { const breakdown = balanceBody({ cloud_credit_balance_micros: 1, @@ -35,19 +48,5 @@ describe('billing balance contract', () => { expect(zBillingBalanceResponse.safeParse(breakdown)).toMatchObject({ success: true }) - expectTypeOf< - Pick< - Balance, - | 'cloud_credit_balance_micros' - | 'effective_balance_micros' - | 'pending_charges_micros' - | 'prepaid_balance_micros' - > - >().toEqualTypeOf<{ - cloud_credit_balance_micros?: number - effective_balance_micros?: number - pending_charges_micros?: number - prepaid_balance_micros?: number - }>() }) }) diff --git a/packages/account-core/src/core/billing/contracts/billingCapabilities.contract.test.ts b/packages/account-core/src/core/billing/contracts/billingCapabilities.contract.test.ts index 007fc2d4a45..27c05058a90 100644 --- a/packages/account-core/src/core/billing/contracts/billingCapabilities.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/billingCapabilities.contract.test.ts @@ -13,7 +13,7 @@ type Capabilities = z.infer type CapabilityScope = z.infer type RolloutDefaults = z.infer -function capabilities(overrides: Partial = {}): Capabilities { +function capabilities(): Capabilities { return { can_cancel: true, can_change_seats: true, @@ -21,8 +21,7 @@ function capabilities(overrides: Partial = {}): Capabilities { can_invite_members: true, can_reactivate: false, can_subscribe_self_serve: true, - can_top_up: true, - ...overrides + can_top_up: true } } @@ -43,6 +42,38 @@ function capabilitiesBody( } } +// Compile-time pins: a regen that moves these fails the package typecheck. + +// The five fields the reader composes its own schema from. +expectTypeOf().toEqualTypeOf< + | 'capabilities' + | 'expires_at' + | 'resolved_for' + | 'revision' + | 'rollout_defaults_applied' +>() +expectTypeOf().toEqualTypeOf<{ + can_cancel: boolean + can_change_seats: boolean + can_downgrade_to_personal: boolean + can_invite_members: boolean + can_reactivate: boolean + can_subscribe_self_serve: boolean + can_top_up: boolean +}>() +expectTypeOf().toEqualTypeOf<{ + user_id: string + workspace_id: string +}>() +expectTypeOf().toEqualTypeOf<{ + can_downgrade_to_personal: boolean + can_subscribe_self_serve: boolean + can_top_up: boolean +}>() +// The expiry the reader paces freshness from; the revision it reads as a number. +expectTypeOf().toEqualTypeOf() +expectTypeOf().toEqualTypeOf() + describe('billing capabilities contract', () => { it('accepts the body the capabilities endpoint returns', () => { expect( @@ -50,47 +81,6 @@ describe('billing capabilities contract', () => { ).toMatchObject({ success: true }) }) - it('carries exactly the five fields the reader composes its own schema from', () => { - expectTypeOf().toEqualTypeOf< - | 'capabilities' - | 'expires_at' - | 'resolved_for' - | 'revision' - | 'rollout_defaults_applied' - >() - }) - - it('grants every capability as a boolean, including the top-up gate', () => { - expectTypeOf().toEqualTypeOf<{ - can_cancel: boolean - can_change_seats: boolean - can_downgrade_to_personal: boolean - can_invite_members: boolean - can_reactivate: boolean - can_subscribe_self_serve: boolean - can_top_up: boolean - }>() - }) - - it('resolves for a user and a workspace, both of which the reader matches against its scope', () => { - expectTypeOf().toEqualTypeOf<{ - user_id: string - workspace_id: string - }>() - }) - - it('reports rollout defaults for the three capabilities that have them', () => { - expectTypeOf().toEqualTypeOf<{ - can_downgrade_to_personal: boolean - can_subscribe_self_serve: boolean - can_top_up: boolean - }>() - }) - - it('dates the snapshot with a string the reader paces freshness from', () => { - expectTypeOf().toEqualTypeOf() - }) - it('requires a datetime expiry, which the reader re-decodes leniently as a plain string', () => { expect( zBillingCapabilitiesResponse.safeParse( @@ -100,8 +90,6 @@ describe('billing capabilities contract', () => { }) it('bounds the revision to the safe range the reader reads it back as a number over', () => { - expectTypeOf().toEqualTypeOf() - const safe = { ...capabilitiesBody(), revision: Number.MAX_SAFE_INTEGER } const unsafe = capabilitiesBody({ revision: BigInt(Number.MAX_SAFE_INTEGER) + 1n diff --git a/packages/account-core/src/core/billing/contracts/billingOpStatus.contract.test.ts b/packages/account-core/src/core/billing/contracts/billingOpStatus.contract.test.ts index c0f9d842f33..97875f75984 100644 --- a/packages/account-core/src/core/billing/contracts/billingOpStatus.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/billingOpStatus.contract.test.ts @@ -31,6 +31,49 @@ const AUTHENTICATION_STATES = [ 'reconciliation_needed' ] as const satisfies readonly NonNullable[] +// Compile-time pins: a regen that moves these fails the package typecheck. + +// The tables above are exhaustive, so a new member reaches an `it.for` row. +expectTypeOf().toEqualTypeOf< + (typeof REDUCED_STATUSES)[number] +>() +expectTypeOf().toEqualTypeOf< + (typeof AUTHENTICATION_STATES)[number] | undefined +>() +// The decline reasons a failed operation reports, including the fallback. +expectTypeOf().toEqualTypeOf< + | 'card_declined' + | 'insufficient_funds' + | 'expired_card' + | 'incorrect_cvc' + | 'authentication_required' + | 'authentication_failed' + | 'processing_error' + | 'generic' + | undefined +>() +// The recovery actions a host offers after a decline. +expectTypeOf().toEqualTypeOf< + | 'retry' + | 'replace_payment_method' + | 'authenticate_payment' + | 'contact_support' + | undefined +>() +// The server phases the pending state mirrors. +expectTypeOf().toEqualTypeOf< + | 'awaiting_payment_method' + | 'awaiting_invoice_payment' + | 'in_progress' + | undefined +>() +expectTypeOf().toEqualTypeOf() +expectTypeOf().toEqualTypeOf() +expectTypeOf().toEqualTypeOf< + string | undefined +>() +expectTypeOf().toEqualTypeOf() + describe('billing operation status contract', () => { it('accepts a status carrying only the required fields', () => { expect(zBillingOpStatusResponse.safeParse(opStatusBody())).toMatchObject({ @@ -46,24 +89,12 @@ describe('billing operation status contract', () => { ).toBe(false) }) - it('carries exactly the four statuses the reducer handles', () => { - expectTypeOf().toEqualTypeOf< - (typeof REDUCED_STATUSES)[number] - >() - }) - it.for(REDUCED_STATUSES)('accepts the %s status', (status) => { expect( zBillingOpStatusResponse.safeParse(opStatusBody({ status })) ).toMatchObject({ success: true }) }) - it('carries exactly the five authentication states the reducer reads', () => { - expectTypeOf().toEqualTypeOf< - (typeof AUTHENTICATION_STATES)[number] | undefined - >() - }) - it.for(AUTHENTICATION_STATES)( 'accepts the %s authentication state', (authentication_state) => { @@ -75,50 +106,7 @@ describe('billing operation status contract', () => { } ) - it('carries the decline reasons a failed operation reports, including the generic fallback', () => { - expectTypeOf().toEqualTypeOf< - | 'card_declined' - | 'insufficient_funds' - | 'expired_card' - | 'incorrect_cvc' - | 'authentication_required' - | 'authentication_failed' - | 'processing_error' - | 'generic' - | undefined - >() - }) - - it('carries the recovery actions a host offers after a decline', () => { - expectTypeOf().toEqualTypeOf< - | 'retry' - | 'replace_payment_method' - | 'authenticate_payment' - | 'contact_support' - | undefined - >() - }) - - it('carries the server phases the pending state mirrors', () => { - expectTypeOf().toEqualTypeOf< - | 'awaiting_payment_method' - | 'awaiting_invoice_payment' - | 'in_progress' - | undefined - >() - }) - - it('identifies the operation and its continuations as plain strings', () => { - expectTypeOf().toEqualTypeOf() - expectTypeOf().toEqualTypeOf() - expectTypeOf().toEqualTypeOf< - string | undefined - >() - }) - it('leaves retryable absent by default, so a decline is not retryable unless the server says so', () => { - expectTypeOf().toEqualTypeOf() - const parsed = zBillingOpStatusResponse.safeParse( opStatusBody({ status: 'failed' }) ) diff --git a/packages/account-core/src/core/billing/contracts/billingStatus.contract.test.ts b/packages/account-core/src/core/billing/contracts/billingStatus.contract.test.ts index b094035f736..b494df9ed39 100644 --- a/packages/account-core/src/core/billing/contracts/billingStatus.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/billingStatus.contract.test.ts @@ -37,6 +37,39 @@ const SUBSCRIPTION_STATUSES = [ 'canceled' ] as const satisfies readonly NonNullable[] +// Compile-time pins: a regen that moves these fails the package typecheck. + +// The tables above are exhaustive, so a new member reaches an `it.for` row. +expectTypeOf().toEqualTypeOf< + (typeof BILLING_RAILS)[number] | undefined +>() +expectTypeOf().toEqualTypeOf< + (typeof PENDING_OP_TYPES)[number] | undefined +>() +expectTypeOf().toEqualTypeOf< + (typeof SUBSCRIPTION_STATUSES)[number] | undefined +>() +expectTypeOf().toEqualTypeOf< + | 'FREE' + | 'STANDARD' + | 'CREATOR' + | 'PRO' + | 'FOUNDERS_EDITION' + | 'TEAM' + | 'ENTERPRISE' + | undefined +>() +expectTypeOf().toEqualTypeOf() +// The pending operation the lifecycle reattaches to, and the continuations it +// adopts alongside it. +expectTypeOf().toEqualTypeOf< + string | undefined +>() +expectTypeOf().toEqualTypeOf() +expectTypeOf().toEqualTypeOf< + string | undefined +>() + describe('billing status contract', () => { it('accepts a status carrying only the required fields', () => { expect(zBillingStatusResponse.safeParse(statusBody())).toMatchObject({ @@ -44,27 +77,12 @@ describe('billing status contract', () => { }) }) - it('carries the three rails presentation routing distinguishes', () => { - expectTypeOf().toEqualTypeOf< - (typeof BILLING_RAILS)[number] | undefined - >() - }) - it.for(BILLING_RAILS)('accepts the %s rail', (billing_rail) => { expect( zBillingStatusResponse.safeParse(statusBody({ billing_rail })) ).toMatchObject({ success: true }) }) - it('names the pending operation the lifecycle reattaches to', () => { - expectTypeOf().toEqualTypeOf< - string | undefined - >() - expectTypeOf().toEqualTypeOf< - (typeof PENDING_OP_TYPES)[number] | undefined - >() - }) - it.for(PENDING_OP_TYPES)( 'accepts a pending %s operation', (pending_billing_op_type) => { @@ -79,30 +97,6 @@ describe('billing status contract', () => { } ) - it('carries the continuations the lifecycle adopts alongside a pending operation', () => { - expectTypeOf().toEqualTypeOf() - expectTypeOf().toEqualTypeOf< - string | undefined - >() - }) - - it('reports subscription eligibility as an active flag, a tier, and a status', () => { - expectTypeOf().toEqualTypeOf() - expectTypeOf().toEqualTypeOf< - (typeof SUBSCRIPTION_STATUSES)[number] | undefined - >() - expectTypeOf().toEqualTypeOf< - | 'FREE' - | 'STANDARD' - | 'CREATOR' - | 'PRO' - | 'FOUNDERS_EDITION' - | 'TEAM' - | 'ENTERPRISE' - | undefined - >() - }) - it.for(SUBSCRIPTION_STATUSES)( 'accepts the %s subscription status', (subscription_status) => { diff --git a/packages/account-core/src/core/billing/contracts/cancelSubscription.contract.test.ts b/packages/account-core/src/core/billing/contracts/cancelSubscription.contract.test.ts index 122d839e9f2..fb7ec095761 100644 --- a/packages/account-core/src/core/billing/contracts/cancelSubscription.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/cancelSubscription.contract.test.ts @@ -16,6 +16,11 @@ const CANCEL_VARIANTS = [ } ] as const satisfies readonly { variant: string; body: CancelBody }[] +// Compile-time pins: a regen that moves these fails the package typecheck. + +// Every member of the response union carries the operation id. +expectTypeOf().toEqualTypeOf() + describe('cancel subscription contract', () => { it.for(CANCEL_VARIANTS)( 'reads the operation id off the $variant response', @@ -26,10 +31,6 @@ describe('cancel subscription contract', () => { } ) - it('carries the operation id on every member of the response union', () => { - expectTypeOf().toEqualTypeOf() - }) - it('rejects a scheduled cancellation dated by an unreadable timestamp', () => { const parsed = zCancelSubscriptionResponse2.safeParse({ billing_op_id: 'op-1', diff --git a/packages/account-core/src/core/billing/contracts/errorResponse.contract.test.ts b/packages/account-core/src/core/billing/contracts/errorResponse.contract.test.ts index a276b389b92..08ddce53417 100644 --- a/packages/account-core/src/core/billing/contracts/errorResponse.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/errorResponse.contract.test.ts @@ -13,6 +13,11 @@ function errorBody(overrides: Partial = {}): ErrorBody { } } +// Compile-time pins: a regen that moves these fails the package typecheck. + +// An open string the commands match against their own closed set of codes. +expectTypeOf().toEqualTypeOf() + describe('billing error body contract', () => { it('accepts an error carrying only a code and a message', () => { expect(zErrorResponse.safeParse(errorBody())).toMatchObject({ @@ -20,10 +25,6 @@ describe('billing error body contract', () => { }) }) - it('reports the code as an open string the commands match against their own closed set', () => { - expectTypeOf().toEqualTypeOf() - }) - it('rejects a body without a message', () => { expect( zErrorResponse.safeParse({ code: 'NO_PAYMENT_METHOD' }).success diff --git a/packages/account-core/src/core/billing/contracts/paymentPortal.contract.test.ts b/packages/account-core/src/core/billing/contracts/paymentPortal.contract.test.ts index 13e24bcdd4e..7d7bc81d97b 100644 --- a/packages/account-core/src/core/billing/contracts/paymentPortal.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/paymentPortal.contract.test.ts @@ -9,6 +9,9 @@ function portalResponse(overrides: Partial = {}): PortalBody { return { url: 'https://billing.comfy.org/portal/session-1', ...overrides } } +// Compile-time pins: a regen that moves these fails the package typecheck. +expectTypeOf().toEqualTypeOf() + describe('payment portal contract', () => { it('accepts a response carrying only the portal url', () => { expect(zPaymentPortalResponse.safeParse(portalResponse())).toMatchObject({ @@ -17,7 +20,6 @@ describe('payment portal contract', () => { }) it('requires the url', () => { - expectTypeOf().toEqualTypeOf() expect(zPaymentPortalResponse.safeParse({}).success).toBe(false) }) diff --git a/packages/account-core/src/core/billing/contracts/previewSubscribe.contract.test.ts b/packages/account-core/src/core/billing/contracts/previewSubscribe.contract.test.ts index 77440059d20..d42b0b666cc 100644 --- a/packages/account-core/src/core/billing/contracts/previewSubscribe.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/previewSubscribe.contract.test.ts @@ -67,6 +67,42 @@ const MONEY_FIELDS = [ const INT64_MIN = -9223372036854775808n const INT64_MAX = 9223372036854775807n +// Compile-time pins: a regen that moves these fails the package typecheck. + +// The plan slug is the only required request field. +expectTypeOf().toEqualTypeOf<{ + plan_slug: string + checkout_attempt_id?: string + promotion_code?: string + team_credit_stop_id?: string +}>() +// The transition table above is exhaustive, so a new member reaches a row. +expectTypeOf().toEqualTypeOf< + (typeof TRANSITION_TYPES)[number] +>() +expectTypeOf().toEqualTypeOf() +expectTypeOf().toEqualTypeOf() +expectTypeOf().toEqualTypeOf() +// Money is an int64, so a host formats a bigint rather than a number. +expectTypeOf().toEqualTypeOf() +expectTypeOf< + PreviewResponse['cost_next_period_cents'] +>().toEqualTypeOf() +expectTypeOf().toEqualTypeOf() +expectTypeOf< + PreviewResponse['credits_next_period_cents'] +>().toEqualTypeOf() +expectTypeOf().toEqualTypeOf< + bigint | undefined +>() +expectTypeOf().toEqualTypeOf< + bigint | undefined +>() +// The reactivation confirmation the subscribe command is asked to resend. +expectTypeOf< + PreviewResponse['requires_reactivation_confirmation'] +>().toEqualTypeOf() + describe('preview subscribe contract', () => { it('accepts the request the command builds from its camel-cased input', () => { const body = previewRequest({ @@ -80,13 +116,7 @@ describe('preview subscribe contract', () => { }) }) - it('requires the plan slug and leaves every other request field optional', () => { - expectTypeOf().toEqualTypeOf<{ - plan_slug: string - checkout_attempt_id?: string - promotion_code?: string - team_credit_stop_id?: string - }>() + it('requires the plan slug', () => { expect(zPreviewSubscribeRequest.safeParse({}).success).toBe(false) }) @@ -96,12 +126,6 @@ describe('preview subscribe contract', () => { ).toMatchObject({ success: true }) }) - it('carries exactly the four transitions a host renders', () => { - expectTypeOf().toEqualTypeOf< - (typeof TRANSITION_TYPES)[number] - >() - }) - it.for(TRANSITION_TYPES)('quotes the %s transition', (transition_type) => { const parsed = zPreviewSubscribeResponse.safeParse( previewResponse({ transition_type }) @@ -110,12 +134,6 @@ describe('preview subscribe contract', () => { expect(parsed.success && parsed.data.transition_type).toBe(transition_type) }) - it('states whether the change is allowed and when it takes effect', () => { - expectTypeOf().toEqualTypeOf() - expectTypeOf().toEqualTypeOf() - expectTypeOf().toEqualTypeOf() - }) - it('rejects an effective date a host could not read the change from', () => { expect( zPreviewSubscribeResponse.safeParse( @@ -124,25 +142,6 @@ describe('preview subscribe contract', () => { ).toBe(false) }) - it('reports money as an int64, so a host formats a bigint rather than a number', () => { - expectTypeOf().toEqualTypeOf() - expectTypeOf< - PreviewResponse['cost_next_period_cents'] - >().toEqualTypeOf() - expectTypeOf< - PreviewResponse['credits_today_cents'] - >().toEqualTypeOf() - expectTypeOf< - PreviewResponse['credits_next_period_cents'] - >().toEqualTypeOf() - expectTypeOf().toEqualTypeOf< - bigint | undefined - >() - expectTypeOf().toEqualTypeOf< - bigint | undefined - >() - }) - it.for(MONEY_FIELDS)( 'coerces the JSON number the wire actually carries for %s', (field) => { @@ -179,10 +178,4 @@ describe('preview subscribe contract', () => { kind: 'promotion' }) }) - - it('flags the reactivation confirmation the subscribe command is asked to resend', () => { - expectTypeOf< - PreviewResponse['requires_reactivation_confirmation'] - >().toEqualTypeOf() - }) }) diff --git a/packages/account-core/src/core/billing/contracts/resubscribe.contract.test.ts b/packages/account-core/src/core/billing/contracts/resubscribe.contract.test.ts index 37906a96e8e..388deb60925 100644 --- a/packages/account-core/src/core/billing/contracts/resubscribe.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/resubscribe.contract.test.ts @@ -16,6 +16,14 @@ const RESUBSCRIBE_STATUSES = [ 'pending' ] as const satisfies readonly Resubscribe['status'][] +// Compile-time pins: a regen that moves these fails the package typecheck. + +// The table above is exhaustive, so a new status reaches an `it.for` row. +expectTypeOf().toEqualTypeOf< + (typeof RESUBSCRIBE_STATUSES)[number] +>() +expectTypeOf().toEqualTypeOf() + describe('resubscribe contract', () => { it('accepts a response carrying only the operation id and its status', () => { expect(zResubscribeResponse.safeParse(resubscribeResponse())).toMatchObject( @@ -25,16 +33,6 @@ describe('resubscribe contract', () => { ) }) - it('returns the operation id the lifecycle adopts', () => { - expectTypeOf().toEqualTypeOf() - }) - - it('carries exactly the two statuses a resubscribe settles into', () => { - expectTypeOf().toEqualTypeOf< - (typeof RESUBSCRIBE_STATUSES)[number] - >() - }) - it.for(RESUBSCRIBE_STATUSES)( 'carries the operation id alongside the %s status', (status) => { diff --git a/packages/account-core/src/core/billing/contracts/subscribe.contract.test.ts b/packages/account-core/src/core/billing/contracts/subscribe.contract.test.ts index b989758bac2..d9b30e50240 100644 --- a/packages/account-core/src/core/billing/contracts/subscribe.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/subscribe.contract.test.ts @@ -29,6 +29,30 @@ const SUBSCRIBE_STATUSES = [ 'pending_payment' ] as const satisfies readonly SubscribeResponse['status'][] +// Compile-time pins: a regen that moves these fails the package typecheck. + +// The payment credentials the command deconflicts before sending. +expectTypeOf().toEqualTypeOf< + string | undefined +>() +expectTypeOf().toEqualTypeOf< + string | undefined +>() +expectTypeOf().toEqualTypeOf< + string | undefined +>() +expectTypeOf().toEqualTypeOf< + boolean | undefined +>() +// The table above is exhaustive, so a new outcome reaches an `it.for` row. +expectTypeOf().toEqualTypeOf< + (typeof SUBSCRIBE_STATUSES)[number] +>() +expectTypeOf().toEqualTypeOf() +expectTypeOf().toEqualTypeOf< + string | undefined +>() + describe('subscribe contract', () => { it('accepts a request carrying only the plan slug', () => { expect(zSubscribeRequest.safeParse(subscribeRequest())).toMatchObject({ @@ -36,18 +60,6 @@ describe('subscribe contract', () => { }) }) - it('carries the payment credentials the command deconflicts before sending', () => { - expectTypeOf().toEqualTypeOf< - string | undefined - >() - expectTypeOf().toEqualTypeOf< - string | undefined - >() - expectTypeOf().toEqualTypeOf< - string | undefined - >() - }) - it('rejects a saved payment method that is not a Stripe payment-method id', () => { const rejected = subscribeRequest({ saved_payment_method_id: 'card_123' }) const accepted = subscribeRequest({ saved_payment_method_id: 'pm_123' }) @@ -59,25 +71,11 @@ describe('subscribe contract', () => { }) it('accepts the reactivation confirmation the server asks callers to resend', () => { - expectTypeOf().toEqualTypeOf< - boolean | undefined - >() - const body = subscribeRequest({ confirm_reactivation: true }) expect(zSubscribeRequest.safeParse(body)).toMatchObject({ success: true }) }) - it('returns the operation id the lifecycle adopts', () => { - expectTypeOf().toEqualTypeOf() - }) - - it('carries the three subscribe outcomes the command distinguishes', () => { - expectTypeOf().toEqualTypeOf< - (typeof SUBSCRIBE_STATUSES)[number] - >() - }) - it.for(SUBSCRIBE_STATUSES)('accepts the %s outcome', (status) => { expect( zSubscribeResponse.safeParse(subscribeResponse({ status })) @@ -85,10 +83,6 @@ describe('subscribe contract', () => { }) it('leaves the hosted payment page optional, which the command reads as a missing url', () => { - expectTypeOf().toEqualTypeOf< - string | undefined - >() - const parsed = zSubscribeResponse.safeParse( subscribeResponse({ status: 'needs_payment_method' }) ) diff --git a/packages/account-core/src/core/billing/contracts/topup.contract.test.ts b/packages/account-core/src/core/billing/contracts/topup.contract.test.ts index c86050564d5..00e4a5cc502 100644 --- a/packages/account-core/src/core/billing/contracts/topup.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/topup.contract.test.ts @@ -36,6 +36,20 @@ const TOPUP_STATUSES = [ 'failed' ] as const satisfies readonly TopupResponse['status'][] +// Compile-time pins: a regen that moves these fails the package typecheck. + +// The amount the command validates, coerced to an int64 the transport never +// serializes, and the idempotency key it mints per attempt. +expectTypeOf().toEqualTypeOf() +expectTypeOf().toEqualTypeOf< + string | undefined +>() +// The table above is exhaustive, so a new status reaches an `it.for` row. +expectTypeOf().toEqualTypeOf< + (typeof TOPUP_STATUSES)[number] +>() +expectTypeOf().toEqualTypeOf() + describe('topup contract', () => { it('accepts a request carrying only the amount', () => { expect(zCreateTopupRequest.safeParse(topupRequest())).toMatchObject({ @@ -44,8 +58,6 @@ describe('topup contract', () => { }) it('validates the JSON number the command sends and coerces it to an int64 the transport never serializes', () => { - expectTypeOf().toEqualTypeOf() - const parsed = zCreateTopupRequest.safeParse({ amount_cents: MINIMUM_AMOUNT_CENTS }) @@ -64,22 +76,6 @@ describe('topup contract', () => { ).toMatchObject({ success: true }) }) - it('carries the idempotency key the command mints per attempt', () => { - expectTypeOf().toEqualTypeOf< - string | undefined - >() - }) - - it('returns the operation id the lifecycle adopts', () => { - expectTypeOf().toEqualTypeOf() - }) - - it('carries exactly the three statuses a top-up settles into', () => { - expectTypeOf().toEqualTypeOf< - (typeof TOPUP_STATUSES)[number] - >() - }) - it.for(TOPUP_STATUSES)( 'carries the operation id alongside the %s status', (status) => { diff --git a/packages/account-core/src/core/billing/contracts/topupCheckout.contract.test.ts b/packages/account-core/src/core/billing/contracts/topupCheckout.contract.test.ts index 1c7d0f65b00..87080ec67ed 100644 --- a/packages/account-core/src/core/billing/contracts/topupCheckout.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/topupCheckout.contract.test.ts @@ -55,6 +55,15 @@ const INVALID_REQUESTS = [ } ] as const +// Compile-time pins: a regen that moves these fails the package typecheck. + +// The amount coerced to an int64 the transport never serializes. +expectTypeOf().toEqualTypeOf() +expectTypeOf().toEqualTypeOf() +expectTypeOf().toEqualTypeOf< + string | undefined +>() + describe('hosted topup checkout contract', () => { it('accepts a request carrying an in-range amount and a return url', () => { expect( @@ -69,8 +78,6 @@ describe('hosted topup checkout contract', () => { }) it('coerces the JSON amount to an int64 the transport never serializes', () => { - expectTypeOf().toEqualTypeOf() - const parsed = zCreateTopupCheckoutRequest.safeParse({ amount_cents: MINIMUM_AMOUNT_CENTS, return_url: RETURN_URL @@ -91,7 +98,6 @@ describe('hosted topup checkout contract', () => { }) it('requires the hosted page url', () => { - expectTypeOf().toEqualTypeOf() expect( zCreateTopupCheckoutResponse.safeParse(checkoutResponse()) ).toMatchObject({ success: true }) @@ -112,10 +118,6 @@ describe('hosted topup checkout contract', () => { }) it('leaves the provider session id optional, so the host correlates only when it is sent', () => { - expectTypeOf().toEqualTypeOf< - string | undefined - >() - const parsed = zCreateTopupCheckoutResponse.safeParse(checkoutResponse()) expect(parsed.success && parsed.data.session_id).toBeUndefined() From b3f14e922414dd0e998f230d93decd24da219133 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Thu, 17 Sep 2026 02:26:57 -0400 Subject: [PATCH 8/8] FE-2217 test(account-core): give the unpinned contract rows teeth Pin the topup response amount's coercion and int64 bounds and the request's ceiling, make the money-field and tier tables exhaustive in both directions, type the hosted-checkout rejection table, and drop the builder parameters no caller varied. --- .../contracts/billingStatus.contract.test.ts | 24 +++++++----- .../contracts/errorResponse.contract.test.ts | 5 +-- .../previewSubscribe.contract.test.ts | 5 +++ .../billing/contracts/topup.contract.test.ts | 37 +++++++++++++++++-- .../contracts/topupCheckout.contract.test.ts | 13 ++++--- 5 files changed, 61 insertions(+), 23 deletions(-) diff --git a/packages/account-core/src/core/billing/contracts/billingStatus.contract.test.ts b/packages/account-core/src/core/billing/contracts/billingStatus.contract.test.ts index b494df9ed39..39101c12804 100644 --- a/packages/account-core/src/core/billing/contracts/billingStatus.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/billingStatus.contract.test.ts @@ -37,6 +37,17 @@ const SUBSCRIPTION_STATUSES = [ 'canceled' ] as const satisfies readonly NonNullable[] +/** Every tier the status can report; eligibility reads only 'FREE' as unpaid. */ +const SUBSCRIPTION_TIERS = [ + 'FREE', + 'STANDARD', + 'CREATOR', + 'PRO', + 'FOUNDERS_EDITION', + 'TEAM', + 'ENTERPRISE' +] as const satisfies readonly NonNullable[] + // Compile-time pins: a regen that moves these fails the package typecheck. // The tables above are exhaustive, so a new member reaches an `it.for` row. @@ -50,14 +61,7 @@ expectTypeOf().toEqualTypeOf< (typeof SUBSCRIPTION_STATUSES)[number] | undefined >() expectTypeOf().toEqualTypeOf< - | 'FREE' - | 'STANDARD' - | 'CREATOR' - | 'PRO' - | 'FOUNDERS_EDITION' - | 'TEAM' - | 'ENTERPRISE' - | undefined + (typeof SUBSCRIPTION_TIERS)[number] | undefined >() expectTypeOf().toEqualTypeOf() // The pending operation the lifecycle reattaches to, and the continuations it @@ -111,8 +115,8 @@ describe('billing status contract', () => { } ) - it('keeps FREE in the tier set eligibility reads as unpaid', () => { - const body = statusBody({ subscription_tier: 'FREE' }) + it.for(SUBSCRIPTION_TIERS)('accepts the %s tier', (subscription_tier) => { + const body = statusBody({ subscription_tier }) expect(zBillingStatusResponse.safeParse(body)).toMatchObject({ success: true diff --git a/packages/account-core/src/core/billing/contracts/errorResponse.contract.test.ts b/packages/account-core/src/core/billing/contracts/errorResponse.contract.test.ts index 08ddce53417..848b95e3d45 100644 --- a/packages/account-core/src/core/billing/contracts/errorResponse.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/errorResponse.contract.test.ts @@ -5,11 +5,10 @@ import type { z } from 'zod' type ErrorBody = z.input type ErrorResponse = z.infer -function errorBody(overrides: Partial = {}): ErrorBody { +function errorBody(): ErrorBody { return { code: 'NO_PAYMENT_METHOD', - message: 'The workspace has no payment method on file.', - ...overrides + message: 'The workspace has no payment method on file.' } } diff --git a/packages/account-core/src/core/billing/contracts/previewSubscribe.contract.test.ts b/packages/account-core/src/core/billing/contracts/previewSubscribe.contract.test.ts index d42b0b666cc..f09bac0f6b1 100644 --- a/packages/account-core/src/core/billing/contracts/previewSubscribe.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/previewSubscribe.contract.test.ts @@ -69,6 +69,11 @@ const INT64_MAX = 9223372036854775807n // Compile-time pins: a regen that moves these fails the package typecheck. +// The money table above is exhaustive, so a seventh amount reaches both the +// coercion rows and the int64 rows. +expectTypeOf<(typeof MONEY_FIELDS)[number]>().toEqualTypeOf< + Extract +>() // The plan slug is the only required request field. expectTypeOf().toEqualTypeOf<{ plan_slug: string diff --git a/packages/account-core/src/core/billing/contracts/topup.contract.test.ts b/packages/account-core/src/core/billing/contracts/topup.contract.test.ts index 00e4a5cc502..3b632210e13 100644 --- a/packages/account-core/src/core/billing/contracts/topup.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/topup.contract.test.ts @@ -11,11 +11,10 @@ type TopupResponseBody = z.input type TopupResponse = z.infer const MINIMUM_AMOUNT_CENTS = 500 +const INT64_MAX = 9223372036854775807n -function topupRequest( - overrides: Partial = {} -): TopupRequestBody { - return { amount_cents: BigInt(MINIMUM_AMOUNT_CENTS), ...overrides } +function topupRequest(): TopupRequestBody { + return { amount_cents: BigInt(MINIMUM_AMOUNT_CENTS) } } function topupResponse( @@ -76,6 +75,36 @@ describe('topup contract', () => { ).toMatchObject({ success: true }) }) + it('rejects an amount past the int64 ceiling the command reports as invalid', () => { + expect( + zCreateTopupRequest.safeParse({ amount_cents: INT64_MAX }) + ).toMatchObject({ success: true }) + expect( + zCreateTopupRequest.safeParse({ amount_cents: INT64_MAX + 1n }).success + ).toBe(false) + }) + + it('coerces the JSON number the wire carries for the response amount', () => { + const parsed = zCreateTopupResponse.safeParse({ + ...topupResponse(), + amount_cents: MINIMUM_AMOUNT_CENTS + }) + + expect(parsed.success && parsed.data.amount_cents).toBe( + BigInt(MINIMUM_AMOUNT_CENTS) + ) + }) + + it('bounds the response amount to the int64 range', () => { + const parseAmount = (amount_cents: bigint) => + zCreateTopupResponse.safeParse({ ...topupResponse(), amount_cents }) + + expect(parseAmount(INT64_MAX)).toMatchObject({ success: true }) + expect(parseAmount(-INT64_MAX - 1n)).toMatchObject({ success: true }) + expect(parseAmount(INT64_MAX + 1n).success).toBe(false) + expect(parseAmount(-INT64_MAX - 2n).success).toBe(false) + }) + it.for(TOPUP_STATUSES)( 'carries the operation id alongside the %s status', (status) => { diff --git a/packages/account-core/src/core/billing/contracts/topupCheckout.contract.test.ts b/packages/account-core/src/core/billing/contracts/topupCheckout.contract.test.ts index 87080ec67ed..d783713089d 100644 --- a/packages/account-core/src/core/billing/contracts/topupCheckout.contract.test.ts +++ b/packages/account-core/src/core/billing/contracts/topupCheckout.contract.test.ts @@ -14,13 +14,10 @@ const MINIMUM_AMOUNT_CENTS = 500 const MAXIMUM_AMOUNT_CENTS = 473_900 const RETURN_URL = 'https://app.comfy.org/billing/return' -function checkoutRequest( - overrides: Partial = {} -): CheckoutRequestBody { +function checkoutRequest(): CheckoutRequestBody { return { amount_cents: BigInt(MINIMUM_AMOUNT_CENTS), - return_url: RETURN_URL, - ...overrides + return_url: RETURN_URL } } @@ -53,7 +50,11 @@ const INVALID_REQUESTS = [ field: 'return_url', body: { amount_cents: MINIMUM_AMOUNT_CENTS, return_url: 'not-a-url' } } -] as const +] as const satisfies readonly { + rejected: string + field: keyof CheckoutRequestBody + body: Partial> +}[] // Compile-time pins: a regen that moves these fails the package typecheck.