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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/billing-web/e2e/fixtures/scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function capabilitiesWith(
workspace_id: E2E_USER.workspaceId
},
revision: 1,
auto_top_up_enabled: false,
rollout_defaults_applied: {
can_downgrade_to_personal: false,
can_subscribe_self_serve: false,
Expand Down
1 change: 1 addition & 0 deletions apps/billing-web/src/test/fakeBillingClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ export function createFakeBillingClient(
...granted
},
denials,
autoTopUpEnabled: false,
rolloutDefaultsApplied: {
can_downgrade_to_personal: false,
can_subscribe_self_serve: false,
Expand Down
14 changes: 12 additions & 2 deletions browser_tests/fixtures/agentTurnLockFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,18 @@ class TurnLockServer {
}

recordAsk(event: AgentWsEvent): void {
if (event.type === 'agent_ask')
this.pendingAsk = zAgentPendingAsk.parse(event.data)
if (event.type !== 'agent_ask') return
const ask = zAgentPendingAsk.parse(event.data)
if (ask.kind === 'run_approval') {
const docSeq =
ask.context.doc_seq === undefined
? undefined
: Number(ask.context.doc_seq)
if (docSeq !== undefined) expect(Number.isSafeInteger(docSeq)).toBe(true)
this.pendingAsk = { ...ask, context: { ...ask.context, doc_seq: docSeq } }
} else {
this.pendingAsk = ask
}
}

completeTurn(): void {
Expand Down
1 change: 1 addition & 0 deletions browser_tests/fixtures/data/billingCapabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function createBillingCapabilities(
can_top_up: true,
...overrides
},
auto_top_up_enabled: false,
rollout_defaults_applied: {
can_downgrade_to_personal: false,
can_subscribe_self_serve: false,
Expand Down
18 changes: 18 additions & 0 deletions packages/account-core/src/core/billing/capabilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,30 @@ function capabilitiesBody(overrides: Record<string, unknown> = {}) {
expires_at: new Date(Date.now() + 10 * 60 * 1000).toISOString(),
resolved_for: { user_id: 'uid-1', workspace_id: 'ws-1' },
revision: 42,
auto_top_up_enabled: false,
rollout_defaults_applied: ROLLOUT_DEFAULTS,
...overrides
}
}

describe('createCapabilitiesReader', () => {
it.for([true, false])(
'preserves served auto top-up rollout %s',
async (served) => {
const { scopeSource } = fakeSession()
const { transport } = fakeTransport([
httpOk(capabilitiesBody({ auto_top_up_enabled: served }))
])
const result = await createCapabilitiesReader({
transport,
scopeSource
}).read()
expect(result.status).toBe('ok')
if (result.status !== 'ok') throw new Error('Expected successful read')
expect(result.value.autoTopUpEnabled).toBe(served)
}
)

it('decodes the server answer and reports the resolved scope', async () => {
const { scopeSource } = fakeSession()
const { transport } = fakeTransport([httpOk(capabilitiesBody())])
Expand Down
3 changes: 3 additions & 0 deletions packages/account-core/src/core/billing/capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export type CapabilityRolloutDefaults = z.infer<
* clients can compare it as one.
*/
const CapabilitiesBodySchema = z.object({
auto_top_up_enabled: zBillingCapabilitiesResponse.shape.auto_top_up_enabled,
capabilities: zBillingCapabilities,
expires_at: z.string(),
has_parked_billing_operation:
Expand All @@ -75,6 +76,7 @@ type CapabilitiesBody = z.infer<typeof CapabilitiesBodySchema>
export type CapabilityScope = BillingScope

export interface CapabilitiesSnapshot {
readonly autoTopUpEnabled: boolean
readonly capabilities: BillingCapabilities
/**
* Present only for a capability the server refused and explained. An absent
Expand Down Expand Up @@ -191,6 +193,7 @@ export function createCapabilitiesReader(
return {
status: 'ok',
value: {
autoTopUpEnabled: data.auto_top_up_enabled,
capabilities: data.capabilities,
denials,
hasParkedBillingOperation: data.has_parked_billing_operation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function capabilitiesBody(
expires_at: new Date(Date.now() + 10 * 60 * 1000).toISOString(),
resolved_for: { user_id: canonicalUserId, workspace_id: TEAM_WORKSPACE.id },
revision: 7,
auto_top_up_enabled: false,
rollout_defaults_applied: {
can_downgrade_to_personal: false,
can_subscribe_self_serve: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function capabilitiesBody(
expires_at: '2024-06-15T12:01:00.000Z',
resolved_for: { user_id: 'uid-1', workspace_id: 'ws-1' },
revision: 42n,
auto_top_up_enabled: false,
rollout_defaults_applied: {
can_downgrade_to_personal: false,
can_subscribe_self_serve: false,
Expand All @@ -61,6 +62,7 @@ function capabilitiesBody(
}

expectTypeOf<keyof CapabilitiesResponse>().toEqualTypeOf<
| 'auto_top_up_enabled'
| 'auto_top_up_setup'
| 'capabilities'
| 'denied_reason_params'
Expand Down
40 changes: 40 additions & 0 deletions packages/account-core/src/core/billing/plans.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,44 @@ describe('createPlansReader', () => {

expect(calls[0].timeoutMs).toBe(5_000)
})

it('drops unknown display features without rejecting the catalog', async () => {
const { scopeSource } = fakeSession()
const { transport } = fakeTransport([
httpOk({
...CATALOG,
plans: [
{
...PLAN,
features: ['Fallback copy'],
feature_items: [
{
key: 'shortRuntime',
status: 'available',
text: 'Short runtime'
},
{
key: 'futureFeature',
status: 'available',
text: 'Future feature'
}
]
}
]
})
])

const result = await createPlansReader({ transport, scopeSource }).read()

expect(result.status).toBe('ok')
if (result.status !== 'ok') return
expect(result.value.data.plans[0].features).toEqual(['Fallback copy'])
expect(result.value.data.plans[0].feature_items).toEqual([
{
key: 'shortRuntime',
status: 'available',
text: 'Short runtime'
}
])
})
})
27 changes: 25 additions & 2 deletions packages/account-core/src/core/billing/plans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* server for the actor that asked, so the response is cached under the scope it
* was resolved for and no plan is filtered, ranked, or priced here.
*/
import { zGetBillingPlansResponse } from '@comfyorg/ingest-types/zod'
import {
zGetBillingPlansResponse,
zPlanFeatureItem
} from '@comfyorg/ingest-types/zod'
import type { z } from 'zod'

import type { BillingResult, BillingTransport } from './billingContracts'
Expand Down Expand Up @@ -51,6 +54,25 @@ export interface PlansReaderOptions {
readonly now?: () => number
}

function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}

function filterUnreadableFeatureItems(plan: unknown): unknown {
if (!isRecord(plan) || !Array.isArray(plan.feature_items)) return plan
return {
...plan,
feature_items: plan.feature_items.filter(
(item) => zPlanFeatureItem.safeParse(item).success
)
}
}

function filterUnreadablePlanFeatures(body: unknown): unknown {
if (!isRecord(body) || !Array.isArray(body.plans)) return body
return { ...body, plans: body.plans.map(filterUnreadableFeatureItems) }
}

export function createPlansReader(options: PlansReaderOptions): PlansReader {
const { transport, scopeSource, now = Date.now } = options

Expand All @@ -62,7 +84,8 @@ export function createPlansReader(options: PlansReaderOptions): PlansReader {
transport,
scopeSource,
route: PLANS_ROUTE,
parse: (body) => zGetBillingPlansResponse.safeParse(body),
parse: (body) =>
zGetBillingPlansResponse.safeParse(filterUnreadablePlanFeatures(body)),
timeoutMs: (readOptions) => readOptions?.timeoutMs,
project: ({ data }, scope) => ({
status: 'ok',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ function capabilitiesBody(overrides: Record<string, unknown> = {}) {
expires_at: new Date(Date.now() + 10 * 60 * 1000).toISOString(),
resolved_for: { user_id: 'uid-1', workspace_id: 'ws-1' },
revision: 42,
auto_top_up_enabled: false,
rollout_defaults_applied: {
can_downgrade_to_personal: false,
can_subscribe_self_serve: false,
Expand Down
1 change: 1 addition & 0 deletions packages/account-core/src/core/billing/topup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ function capabilitiesBody(overrides: Record<string, unknown> = {}) {
expires_at: new Date(NOW + 10 * 60 * 1000).toISOString(),
resolved_for: { user_id: 'uid-1', workspace_id: 'ws-1' },
revision: 42,
auto_top_up_enabled: false,
rollout_defaults_applied: {
can_downgrade_to_personal: false,
can_subscribe_self_serve: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ function capabilitiesBody(overrides: Record<string, unknown> = {}) {
expires_at: new Date(NOW + 10 * 60 * 1000).toISOString(),
resolved_for: { user_id: 'uid-1', workspace_id: 'ws-1' },
revision: 42,
auto_top_up_enabled: false,
rollout_defaults_applied: {
can_downgrade_to_personal: false,
can_subscribe_self_serve: false,
Expand Down
34 changes: 34 additions & 0 deletions packages/ingest-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ export type {
AuthenticateResponse2,
AuthenticateResponses,
AuthTypeNotAllowedError,
AutoReloadCardExpiry,
AutoReloadDeclinedPaymentMethod,
AutoReloadSettings,
AutoReloadSettingsRequest,
AutoTopUpSetup,
Expand Down Expand Up @@ -241,10 +243,16 @@ export type {
CccCreatorsPage,
CccCreatorSummary,
Cccjson,
CccLegacyTarget,
CccPublicItemsPage,
CccWebsiteCardFacet,
CccWebsiteCardFacets,
CccWebsiteCardsPage,
CccWebsiteModelDirectory,
CccWebsiteRootPageInfo,
CccWebsiteRootPresentation,
CccWebsiteRootTab,
CccWebsiteRootTabs,
CheckAssetByHashData,
CheckAssetByHashError,
CheckAssetByHashErrors,
Expand Down Expand Up @@ -641,11 +649,19 @@ export type {
GetCccItemHistoryErrors,
GetCccItemHistoryResponse,
GetCccItemHistoryResponses,
GetCccLegacyTargetData,
GetCccLegacyTargetErrors,
GetCccLegacyTargetResponse,
GetCccLegacyTargetResponses,
GetCccPreviewMediaBytesData,
GetCccPreviewMediaBytesError,
GetCccPreviewMediaBytesErrors,
GetCccPreviewMediaBytesResponse,
GetCccPreviewMediaBytesResponses,
GetCccPublicCacheAuthorityData,
GetCccPublicCacheAuthorityErrors,
GetCccPublicCacheAuthorityResponse,
GetCccPublicCacheAuthorityResponses,
GetCccPublicMediaBytesData,
GetCccPublicMediaBytesErrors,
GetCccPublicMediaBytesResponse,
Expand All @@ -669,6 +685,10 @@ export type {
GetCccTypeSchemaErrors,
GetCccTypeSchemaResponse,
GetCccTypeSchemaResponses,
GetCccWebsiteRootPresentationData,
GetCccWebsiteRootPresentationErrors,
GetCccWebsiteRootPresentationResponse,
GetCccWebsiteRootPresentationResponses,
GetChurnkeyAuthData,
GetChurnkeyAuthError,
GetChurnkeyAuthErrors,
Expand Down Expand Up @@ -1019,8 +1039,10 @@ export type {
GlobalSubgraphInfo,
HeadCccPreviewMediaBytesData,
HeadCccPreviewMediaBytesErrors,
HeadCccPreviewMediaBytesResponses,
HeadCccPublicMediaBytesData,
HeadCccPublicMediaBytesErrors,
HeadCccPublicMediaBytesResponses,
HistoryDetailEntry,
HistoryDetailResponse,
HistoryEntry,
Expand Down Expand Up @@ -1088,6 +1110,10 @@ export type {
ListCccWebsiteCardsErrors,
ListCccWebsiteCardsResponse,
ListCccWebsiteCardsResponses,
ListCccWebsiteModelDirectoryData,
ListCccWebsiteModelDirectoryErrors,
ListCccWebsiteModelDirectoryResponse,
ListCccWebsiteModelDirectoryResponses,
ListContentCatalogItemsData,
ListContentCatalogItemsError,
ListContentCatalogItemsErrors,
Expand Down Expand Up @@ -1226,6 +1252,7 @@ export type {
PlanActionDeniedReasonParams,
PlanAvailability,
PlanAvailabilityReason,
PlanFeatureItem,
PlanSeatSummary,
PostAssetsFromWorkflowData,
PostAssetsFromWorkflowError,
Expand Down Expand Up @@ -1274,6 +1301,7 @@ export type {
PreviewSubscribeResponse,
PreviewSubscribeResponse2,
PreviewSubscribeResponses,
PreviousPlan,
PromptErrorResponse,
PromptInfo,
PromptRequest,
Expand Down Expand Up @@ -1445,6 +1473,12 @@ export type {
StartSsoError,
StartSsoErrors,
StartSsoResponses,
StartWorkspaceDeploymentData,
StartWorkspaceDeploymentError,
StartWorkspaceDeploymentErrors,
StartWorkspaceDeploymentResponse,
StartWorkspaceDeploymentResponses,
StartWorkspaceDeploymentResult,
StoredAgentConsentSetting,
SubmitFeedbackData,
SubmitFeedbackError,
Expand Down
Loading
Loading