Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, expectTypeOf, it } from 'vitest'

import type { BillingTelemetryEvent } from './billingTelemetryEvent.js'

type LateSuccessEvent = Extract<
BillingTelemetryEvent,
{ recovery_outcome?: 'late_success' }
>

describe('BillingTelemetryEvent', () => {
it('allows late success only on succeeded recovery terminals', () => {
expectTypeOf<LateSuccessEvent['stage']>().toEqualTypeOf<'succeeded'>()
expectTypeOf<LateSuccessEvent['operation']>().toEqualTypeOf<
'subscription_checkout' | 'resubscribe'
>()
})
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { describe, expect, it } from 'vitest'

import type { CheckoutJourneyTelemetryEvent } from './checkoutJourney.js'
import { getCheckoutJourneyTelemetryEventPayload } from './checkoutJourney.js'
import {
CHECKOUT_JOURNEY_EVENT_NAME_BY_PHASE,
CHECKOUT_JOURNEY_SCHEMA_VERSION,
getCheckoutJourneyTelemetryEventName,
getCheckoutJourneyTelemetryEventPayload
} from './checkoutJourney.js'

const CONTEXT = {
checkout_journey_id: 'journey-1',
Expand Down Expand Up @@ -152,3 +157,111 @@ describe('the checkout journey payload', () => {
)
})
})

const baseContext = {
checkout_journey_id: 'journey-1',
checkout_entered_at: '2026-09-09T00:00:00.000Z',
entry_flow: 'initial_subscription',
entry_source: 'pricing'
} as const

describe('getCheckoutJourneyTelemetryEventName', () => {
it('derives the wire name from the phase', () => {
const event: CheckoutJourneyTelemetryEvent = {
...baseContext,
phase: 'preview_ready',
assignment_status: 'resolved',
assigned_arm: 'treatment'
}
expect(getCheckoutJourneyTelemetryEventName(event)).toBe(
'billing.checkout.preview_ready'
)
})

// The table is a total Record, so a missing phase fails to compile — but a
// swapped value (entered -> 'billing.checkout.submitted') would not. Comparing
// each key against its own value restores what the template literal proved.
it.for(Object.entries(CHECKOUT_JOURNEY_EVENT_NAME_BY_PHASE))(
'names %s after its own phase',
([phase, name]) => {
expect(name).toBe(`billing.checkout.${phase}`)

const event = {
...baseContext,
phase,
assignment_status: 'unavailable'
} as CheckoutJourneyTelemetryEvent
expect(getCheckoutJourneyTelemetryEventName(event)).toBe(name)
}
)
})

describe('getCheckoutJourneyTelemetryEventPayload', () => {
it('stamps schema version and carries the resolved arm', () => {
const event: CheckoutJourneyTelemetryEvent = {
...baseContext,
phase: 'entered',
assignment_status: 'resolved',
assigned_arm: 'control'
}
const payload = getCheckoutJourneyTelemetryEventPayload(event)
expect(payload.schema_version).toBe(CHECKOUT_JOURNEY_SCHEMA_VERSION)
expect(payload.assigned_arm).toBe('control')
})

it('omits absent optional context rather than emitting undefined keys', () => {
const event: CheckoutJourneyTelemetryEvent = {
...baseContext,
phase: 'entered',
assignment_status: 'unavailable'
}
const payload = getCheckoutJourneyTelemetryEventPayload(event)
expect('billing_op_id' in payload).toBe(false)
expect('ui_mode' in payload).toBe(false)
})

it('carries the ui_mode the user actually saw', () => {
const event: CheckoutJourneyTelemetryEvent = {
...baseContext,
phase: 'entered',
assignment_status: 'resolved',
assigned_arm: 'treatment',
ui_mode: 'embedded'
}
const payload = getCheckoutJourneyTelemetryEventPayload(event)
expect(payload.ui_mode).toBe('embedded')
})

it('carries phase-specific fields for a failed preview', () => {
const event: CheckoutJourneyTelemetryEvent = {
...baseContext,
phase: 'preview_failed',
assignment_status: 'resolved',
assigned_arm: 'treatment',
failure_category: 'network',
preview_revision: 'rev-7'
}
const payload = getCheckoutJourneyTelemetryEventPayload(event)
expect(payload).toMatchObject({
phase: 'preview_failed',
failure_category: 'network',
preview_revision: 'rev-7'
})
})

it('distinguishes the failing element from the failure phase', () => {
const event: CheckoutJourneyTelemetryEvent = {
...baseContext,
phase: 'payment_element_failed',
assignment_status: 'resolved',
assigned_arm: 'treatment',
element: 'address',
element_phase: 'mount'
}
const payload = getCheckoutJourneyTelemetryEventPayload(event)
expect(payload).toMatchObject({
element: 'address',
element_phase: 'mount'
})
})
})
213 changes: 213 additions & 0 deletions packages/account-core/src/core/billing/telemetry/payload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { describe, expect, it } from 'vitest'
import type { BillingTelemetryEvent } from './billingTelemetryEvent.js'
import {
BILLING_PAYLOAD_FIELD_HANDLING,
getBillingTelemetryEventPayload,
getBillingWebTelemetryEventPayload,
getCloudAppBillingTelemetryEventPayload
} from './payload.js'
import type { BillingPayloadFieldHandling } from './payload.js'
import { toBillingTelemetryEvent } from './operationLifecycleEvent.js'

const SURFACES = [
{
Expand Down Expand Up @@ -143,3 +145,214 @@ describe('the billing payload allowlist', () => {
expect(refused).not.toHaveProperty('unlisted_field')
})
})

describe('getBillingTelemetryEventPayload', () => {
it.for<{
name: string
event: BillingTelemetryEvent
claim: { billing_client?: 'sdk' | 'legacy' }
}>([
{
name: 'the SDK lifecycle starts a subscription operation',
event: toBillingTelemetryEvent({
name: 'billing.operation.started',
billing_op_id: 'op-subscribe',
operation_type: 'subscription',
presentation: 'hosted',
resumed: false
}),
claim: { billing_client: 'sdk' }
},
{
name: 'the SDK lifecycle settles a top-up operation',
event: toBillingTelemetryEvent({
name: 'billing.operation.succeeded',
billing_op_id: 'op-topup',
operation_type: 'topup',
presentation: 'embedded',
resumed: true,
duration_ms: 1200
}),
claim: { billing_client: 'sdk' }
},
{
name: 'the SDK lifecycle times out a cancel operation',
event: toBillingTelemetryEvent({
name: 'billing.operation.timeout',
billing_op_id: 'op-cancel',
operation_type: 'cancel',
presentation: 'hosted',
resumed: false,
failure_category: 'poll_timeout',
duration_ms: 600_000
}),
claim: { billing_client: 'sdk' }
},
{
name: 'the legacy poller fails a subscription operation',
event: {
operation: 'operation',
stage: 'failed',
outcome: 'failure',
operation_type: 'subscription',
billing_op_id: 'op-polled',
failure_category: 'provider_decline',
billing_client: 'legacy'
},
claim: { billing_client: 'legacy' }
},
{
name: 'shared workspace code starts a cancel before choosing a rail',
event: {
operation: 'operation',
stage: 'started',
outcome: 'pending',
operation_type: 'cancel'
},
claim: {}
},
{
name: 'shared workspace code reports a subscription checkout intent',
event: {
operation: 'subscription_checkout',
stage: 'intent',
outcome: 'pending',
tier: 'pro',
cycle: 'monthly'
},
claim: {}
},
{
name: 'shared workspace code starts a top-up',
event: {
operation: 'topup',
stage: 'started',
outcome: 'pending'
},
claim: {}
},
{
name: 'shared workspace code fails a resubscribe',
event: {
operation: 'resubscribe',
stage: 'failed',
outcome: 'failure',
source: 'settings_billing_panel',
failure_category: 'network'
},
claim: {}
},
{
name: 'shared workspace code starts a downgrade to personal',
event: {
operation: 'downgrade_to_personal',
stage: 'started',
outcome: 'pending',
member_removal_count: 1,
member_removal_failures: 0
},
claim: {}
},
{
name: 'shared workspace code fails a capability read',
event: {
operation: 'capability_read',
stage: 'failed',
outcome: 'failure'
},
claim: {}
}
])(
'carries only the client the emitter knows when $name',
({ event, claim }) => {
const payload = getBillingTelemetryEventPayload(event)
expect(
Object.fromEntries(
Object.entries(payload).filter(([key]) => key === 'billing_client')
)
).toStrictEqual(claim)
}
)

it("keeps every field of the SDK lifecycle's event", () => {
const sdkFailure = toBillingTelemetryEvent({
name: 'billing.operation.failed',
billing_op_id: 'op-declined',
operation_type: 'topup',
presentation: 'embedded',
resumed: false,
failure_category: 'provider_decline',
decline_reason: 'card_declined',
duration_ms: 2300
})

expect(getBillingTelemetryEventPayload(sdkFailure)).toStrictEqual({
operation: 'operation',
stage: 'failed',
outcome: 'failure',
operation_type: 'topup',
billing_op_id: 'op-declined',
presentation: 'embedded',
resumed: false,
failure_category: 'provider_decline',
decline_reason: 'card_declined',
duration_ms: 2300,
billing_client: 'sdk'
})
})

it("keeps every field of the poller's event, which has no SDK fields", () => {
expect(
getBillingTelemetryEventPayload({
operation: 'operation',
stage: 'failed',
outcome: 'failure',
operation_type: 'subscription',
billing_op_id: 'op-polled',
tier: 'pro',
cycle: 'monthly',
checkout_type: 'new',
payment_intent_source: 'subscribe_now_button',
failure_category: 'provider_decline',
duration_ms: 4200,
billing_client: 'legacy'
})
).toStrictEqual({
operation: 'operation',
stage: 'failed',
outcome: 'failure',
operation_type: 'subscription',
billing_op_id: 'op-polled',
tier: 'pro',
cycle: 'monthly',
checkout_type: 'new',
payment_intent_source: 'subscribe_now_button',
failure_category: 'provider_decline',
duration_ms: 4200,
billing_client: 'legacy'
})
})

it('drops fields outside the contract and claims no surface', () => {
const event = {
operation: 'topup',
stage: 'failed',
outcome: 'failure',
billing_op_id: 'opaque-op-id',
failure_category: 'unknown',
email: 'user@example.com',
billing_surface: 'billing_web'
} satisfies BillingTelemetryEvent & {
email: string
billing_surface: string
}

expect(getBillingTelemetryEventPayload(event)).toStrictEqual({
operation: 'topup',
stage: 'failed',
outcome: 'failure',
billing_op_id: 'opaque-op-id',
failure_category: 'unknown'
})
})
})
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { describe, expect, it } from 'vitest'

import {
getBillingTelemetryEventPayload,
getCloudAppBillingTelemetryEventPayload
} from '@comfyorg/account-core/billing'
import { describe, expect, it } from 'vitest'
} from './payload.js'

describe('the web handoff event payload', () => {
it.for([
Expand Down
Loading
Loading