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
75 changes: 75 additions & 0 deletions apps/billing-web/e2e/full-page-endings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,50 @@ test('a reload after its own Pay went through renders Already completed on every
expect(subscribeRequests(cloud)).toHaveLength(1)
})

/** How the real preview route refuses a link: a 400 with its own code and sentence. */
function refuseQuote(cloud: MockCloud, message: string) {
cloud.reply('POST', '/billing/preview-subscribe', () => ({
status: 400,
body: { code: 'TRANSITION_NOT_ALLOWED', message }
}))
}

test('E2: reopening the checkout link after its own Pay went through is Already completed, even when the quote refuses it with a 400', async ({
page,
cloud,
signIn
}) => {
await signIn(CHECKOUT)
await payButton(page).click()
await expect(heading(page, "You're all set")).toBeVisible()
refuseQuote(cloud, 'the selected plan is already the current plan')

await page.goto(CHECKOUT)

await expect(heading(page, 'Already completed')).toBeVisible()
await expect(code(page)).toHaveText('op_subscribe')
await expect(heading(page, "Couldn't load your checkout")).toBeHidden()
await expect(payButton(page)).toBeHidden()
expect(subscribeRequests(cloud)).toHaveLength(1)
})

test("a coded quote refusal on a link nothing here paid is Checkout not available in the server's words", async ({
page,
cloud,
signIn
}) => {
refuseQuote(cloud, 'the selected plan is already the current plan')
await signIn(CHECKOUT)

await expect(heading(page, 'Checkout not available')).toBeVisible()
await expect(
page.getByText('the selected plan is already the current plan')
).toBeVisible()
await expect(code(page)).toHaveText('TRANSITION_NOT_ALLOWED')
await expect(page.getByRole('button', { name: 'Try again' })).toBeHidden()
await expect(contactSupport(page)).toBeVisible()
})

const RECEIPT_PLAN = { slug: 'pro_monthly', duration: 'MONTHLY' } as const

test('77-4068: a Pay that goes through counts the credits the server says it added', async ({
Expand Down Expand Up @@ -652,6 +696,37 @@ test('433-6840: a team link without its commit stop is an invalid link, and View
)
})

test('G7: a catalog team plan linked without its commit stop, which the quote refuses with a 400, is Plan not available and View plans opens the Team tab', async ({
page,
cloud,
signIn
}) => {
const [listed] = cloud.scenario.plans.plans
cloud.scenario.plans = {
...cloud.scenario.plans,
plans: [
...cloud.scenario.plans.plans,
{ ...listed, slug: 'team_per_credit_monthly', tier: 'TEAM' }
]
}
refuseQuote(
cloud,
'team_credit_stop_id is required for the per-credit Team plan'
)
await signIn(entryPath('checkout', { plan: 'team_per_credit_monthly' }))

await expect(heading(page, "This plan isn't available")).toBeVisible()
await expect(code(page)).toHaveText('CHECKOUT_LINK_INVALID')
await expect(heading(page, "Couldn't load your checkout")).toBeHidden()

await page.getByRole('button', { name: 'View plans' }).click()

await expect(heading(page, 'Host app')).toBeVisible()
await expect(page).toHaveURL(
'https://testcloud.comfy.org/?pricing=team&workspace=ws_e2e'
)
})

test('433-6840: a checkout link that names no plan, routed once the tab is signed in on the flag, is Plan not available, and View plans opens the pricing table', async ({
page,
signIn
Expand Down
31 changes: 31 additions & 0 deletions apps/billing-web/src/checkout/checkoutPage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,14 @@ const changed = (
})
const settledOnTheSpot: CheckoutPageEvent = { type: 'paySettled' }
const notAllowed: CheckoutPageEvent = { type: 'notAllowed' }
const ALREADY_CURRENT = {
code: 'TRANSITION_NOT_ALLOWED',
message: 'the selected plan is already the current plan'
}
const refusedByServer: CheckoutPageEvent = {
type: 'notAllowed',
server: ALREADY_CURRENT
}
const declinedElsewhere: OperationOutcome = {
kind: 'declined',
reason: 'card_declined',
Expand Down Expand Up @@ -1176,6 +1184,29 @@ describe("reduceCheckoutPage over this tab's own operation", () => {
events: [reconciled(undefined), notAllowed],
expected: { kind: 'refused', reason: 'unspecified' }
},
{
name: 'a quote the server refuses with its own code, with nothing of this tab settled, keeps that code and sentence',
events: [reconciled(undefined), refusedByServer],
expected: { kind: 'refused', server: ALREADY_CURRENT }
},
{
name: 'a success found on mount that the server refuses with its own code is Already completed',
events: [reconciled(succeededOperation()), refusedByServer],
expected: {
kind: 'terminal',
operation: succeededOperation(),
attribution: 'settled'
}
},
{
name: 'a coded refusal read after Already completed leaves it standing',
events: [reconciled(succeededOperation()), notAllowed, refusedByServer],
expected: {
kind: 'terminal',
operation: succeededOperation(),
attribution: 'settled'
}
},
{
name: 'its own payment found settled on a return is Success, whatever the quote says',
events: [reconciled(awaited(succeededOperation())), quoted(0)],
Expand Down
32 changes: 26 additions & 6 deletions apps/billing-web/src/checkout/checkoutPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export interface ScheduledChange {
readonly effectiveAt: string
}

/** A quote the server refused in its own words: its code, and the sentence it wrote for the customer. */
export interface ServerRefusal {
readonly code: string
readonly message?: string
}

/** `none` means the read succeeded and the workspace has no saved method. */
type SavedStatus = 'loading' | 'ready' | 'none' | 'failed'

Expand Down Expand Up @@ -159,6 +165,7 @@ export type CheckoutPage =
readonly reason: CapabilityDenialReason
readonly scheduled?: ScheduledChange
}
| { readonly kind: 'refused'; readonly server: ServerRefusal }
| {
readonly kind: 'unavailable'
readonly cause: LoadFailure
Expand Down Expand Up @@ -214,8 +221,11 @@ export type CheckoutPageEvent =
/** The lifecycle could not say what the workspace is waiting on. */
| { readonly type: 'recheckFailed'; readonly code: string }
| { readonly type: 'planUnavailable'; readonly reason: PlanUnavailableReason }
/** The quote answered `allowed: false`; its free-text reason is never read. */
| { readonly type: 'notAllowed' }
/**
* The server will not sell this link: a quote answering `allowed: false`,
* whose free-text reason is never read, or a coded 4xx refusal (`server`).
*/
| { readonly type: 'notAllowed'; readonly server?: ServerRefusal }
/** The server's catalog named the plan a settled payment bought. */
| { readonly type: 'settledPlanRead'; readonly plan: SettledPlan }
/** Try again on a checkout that could not load. */
Expand Down Expand Up @@ -394,17 +404,15 @@ function isStopEvent(event: CheckoutPageEvent): event is StopEvent {
/**
* The page a read that ends resolving leaves behind. A refused quote over a
* success this tab already saw is that success revisited; any other is a
* checkout the server will not sell, for a reason it gives no code for.
* checkout the server will not sell, in its own words when it coded them.
*/
function stoppedOn(
page: Extract<CheckoutPage, { kind: 'resolving' }>,
event: StopEvent
): CheckoutPage {
switch (event.type) {
case 'notAllowed':
return page.settled === undefined
? { kind: 'refused', reason: 'unspecified' }
: { kind: 'terminal', operation: page.settled, attribution: 'settled' }
return notSoldOn(page, event)
case 'refused':
return {
kind: 'refused',
Expand All @@ -422,6 +430,18 @@ function stoppedOn(
}
}

/** A link the server will not sell is the success this tab already saw, revisited, or a refusal. */
function notSoldOn(
page: Extract<CheckoutPage, { kind: 'resolving' }>,
event: Extract<CheckoutPageEvent, { type: 'notAllowed' }>
): CheckoutPage {
if (page.settled !== undefined)
return { kind: 'terminal', operation: page.settled, attribution: 'settled' }
return event.server === undefined
? { kind: 'refused', reason: 'unspecified' }
: { kind: 'refused', server: event.server }
}

/** An event that means nothing in the current state returns it untouched. */
export function reduceCheckoutPage(
page: CheckoutPage,
Expand Down
25 changes: 25 additions & 0 deletions apps/billing-web/src/checkout/endingScreen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,31 @@ describe('endingOf', () => {
page: { kind: 'terminal', attribution: 'settled' },
screen: { kind: 'already_completed' }
},
{
name: 'a quote the server refused with its own code and sentence',
page: {
kind: 'refused',
server: {
code: 'TRANSITION_NOT_ALLOWED',
message: 'the selected plan is already the current plan'
}
},
screen: {
kind: 'refused',
code: 'TRANSITION_NOT_ALLOWED',
copy: 'unknown',
serverMessage: 'the selected plan is already the current plan'
}
},
{
name: 'a quote the server refused with its own code and no sentence',
page: { kind: 'refused', server: { code: 'TRANSITION_NOT_ALLOWED' } },
screen: {
kind: 'refused',
code: 'TRANSITION_NOT_ALLOWED',
copy: 'unknown'
}
},
{
name: 'a checkout that could not load',
page: { kind: 'unavailable', cause: 'quote', code: 'REQUEST_FAILED' },
Expand Down
11 changes: 11 additions & 0 deletions apps/billing-web/src/checkout/endingScreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export type EndingScreen =
readonly kind: 'refused'
readonly code: string
readonly copy: RefusalCopy
/** The sentence the server wrote for a quote it refused, shown in place of the copy. */
readonly serverMessage?: string
}
| {
readonly kind: 'refused'
Expand Down Expand Up @@ -126,6 +128,15 @@ export function endingOf(page: CheckoutPage): EndingScreen | undefined {
function refusedEnding(
page: Extract<CheckoutPage, { kind: 'refused' }>
): EndingScreen {
if ('server' in page) {
const { code, message } = page.server
return {
kind: 'refused',
code,
copy: 'unknown',
...(message === undefined ? {} : { serverMessage: message })
}
}
const code = page.reason.toUpperCase()
return page.scheduled === undefined
? { kind: 'refused', code, copy: REFUSAL_COPY[page.reason] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ const bodyParams = computed(() =>
}
: { workspace }
)
/** A refusal the server worded reads in its words. */
const body = computed(
() =>
('serverMessage' in screen ? screen.serverMessage : undefined) ??
t(bodyKey.value, bodyParams.value)
)
const code = computed(() => ('code' in screen ? screen.code : undefined))
const receipt = computed(() => endingReceipt(screen))
/** A plan card stands in for the reference; without the plan's listing the code stays. */
Expand Down Expand Up @@ -188,7 +194,7 @@ function act() {
{{ title }}
</h1>
<p class="m-0 text-sm/5 text-muted-foreground">
{{ t(bodyKey, bodyParams) }}
{{ body }}
</p>
<i18n-t
v-if="screen.kind === 'in_progress'"
Expand Down
Loading
Loading