diff --git a/packages/cli/src/commands/billing.test.ts b/packages/cli/src/commands/billing.test.ts index a35eb69268..7e51919e20 100644 --- a/packages/cli/src/commands/billing.test.ts +++ b/packages/cli/src/commands/billing.test.ts @@ -4,7 +4,18 @@ import { consumeCommandResult } from "../utils/commandResult.js"; const opened = vi.hoisted(() => [] as string[]); const plan = vi.hoisted(() => ({ calls: 0 })); -const terminal = vi.hoisted(() => ({ attended: false, checkouts: 0 })); +const terminal = vi.hoisted(() => ({ + attended: false, + checkouts: 0, + checkoutError: null as unknown, +})); +const events = vi.hoisted(() => ({ failed: [] as unknown[], timeout: [] as unknown[] })); +const never = vi.hoisted(() => ({ paid: true })); + +vi.mock("../telemetry/events.js", () => ({ + trackBillingCheckoutFailed: (p: unknown) => void events.failed.push(p), + trackBillingWaitTimeout: (p: unknown) => void events.timeout.push(p), +})); vi.mock("../utils/attendedTerminal.js", () => ({ isUnattended: () => !terminal.attended })); vi.mock("node:readline/promises", () => ({ @@ -32,12 +43,13 @@ vi.mock("../heygen/plan.js", async (importOriginal) => { ...real, readOffer: async () => ({}), createCheckout: async () => { + if (terminal.checkoutError) throw terminal.checkoutError; terminal.checkouts++; return "https://checkout.stripe.com/c/pay/cs_test_1"; }, readPlan: async () => ({ - plan: ++plan.calls === 1 ? "none" : "pro", - credits: plan.calls === 1 ? 0 : 500, + plan: ++plan.calls === 1 || !never.paid ? "none" : "pro", + credits: plan.calls === 1 || !never.paid ? 0 : 500, }), }; }); @@ -57,6 +69,10 @@ afterEach(() => { plan.calls = 0; terminal.attended = false; terminal.checkouts = 0; + terminal.checkoutError = null; + events.failed.length = 0; + events.timeout.length = 0; + never.paid = true; vi.restoreAllMocks(); consumeCommandResult(); }); @@ -133,3 +149,71 @@ describe("billing at a terminal", () => { expect(opened.map((u) => new URL(u).hostname)).toEqual(["hyperframes.studio"]); }); }); + +describe("billing outcomes", () => { + it("keeps waiting after a failed checkout, because the open pricing page can still take the payment", async () => { + const { CheckoutError } = await import("../heygen/plan.js"); + terminal.checkoutError = new CheckoutError("checkout_network", "https://x.test/secret failed"); + const log = vi.spyOn(console, "log").mockImplementation(() => undefined); + vi.spyOn(console, "error").mockImplementation(() => undefined); + vi.useFakeTimers(); + const done = runCommand(billing, { rawArgs: ["subscribe"] }); + await vi.advanceTimersByTimeAsync(2000); + await done; + vi.useRealTimers(); + expect(events.failed).toEqual([{ reason: "checkout_network" }]); + expect(log.mock.calls.flat().join("\n")).toContain("Done"); + }); + + it("fails with the checkout error, not as a timeout, when the checkout failed and nobody paid", async () => { + const { CheckoutError } = await import("../heygen/plan.js"); + terminal.checkoutError = new CheckoutError("checkout_rejected", "HeyGen would not open it"); + never.paid = false; + vi.spyOn(console, "log").mockImplementation(() => undefined); + vi.spyOn(console, "error").mockImplementation(() => undefined); + vi.useFakeTimers(); + const done = runCommand(billing, { rawArgs: ["subscribe", "--timeout", "4"] }); + const settled = expect(done).rejects.toThrow(); + await vi.advanceTimersByTimeAsync(6000); + await settled; + vi.useRealTimers(); + expect(events.failed).toEqual([{ reason: "checkout_rejected" }]); + expect(events.timeout).toEqual([]); + }); + + it("stops at once when credits are not on sale, since the credits page cannot sell them either", async () => { + const { CheckoutError } = await import("../heygen/plan.js"); + terminal.checkoutError = new CheckoutError("checkout_unavailable", "Credits are not on sale"); + never.paid = false; + vi.spyOn(console, "log").mockImplementation(() => undefined); + vi.spyOn(console, "error").mockImplementation(() => undefined); + await expect(runCommand(billing, { rawArgs: ["subscribe"] })).rejects.toThrow(); + expect(events.failed).toEqual([{ reason: "checkout_unavailable" }]); + }); + + it("ends a wait with no payment as wait_timeout, not a checkout failure or a failed command", async () => { + never.paid = false; + vi.spyOn(console, "log").mockImplementation(() => undefined); + const error = vi.spyOn(console, "error").mockImplementation(() => undefined); + vi.useFakeTimers(); + const done = runCommand(billing, { rawArgs: ["subscribe", "--timeout", "4"] }); + await vi.advanceTimersByTimeAsync(6000); + await done; + vi.useRealTimers(); + expect(events.timeout).toEqual([{ action: "subscribe" }]); + expect(events.failed).toEqual([]); + expect(error.mock.calls.flat().join("\n")).toContain("Still waiting"); + expect(consumeCommandResult().exitCode).toBe(124); + }); + + it("emits no event when the payment lands", async () => { + vi.spyOn(console, "log").mockImplementation(() => undefined); + vi.useFakeTimers(); + const done = runCommand(billing, { rawArgs: ["subscribe"] }); + await vi.advanceTimersByTimeAsync(2000); + await done; + vi.useRealTimers(); + expect(events.failed).toEqual([]); + expect(events.timeout).toEqual([]); + }); +}); diff --git a/packages/cli/src/commands/billing.ts b/packages/cli/src/commands/billing.ts index fee7968cb7..325e6f5772 100644 --- a/packages/cli/src/commands/billing.ts +++ b/packages/cli/src/commands/billing.ts @@ -7,11 +7,13 @@ import { pageUrl, readOffer, readPlan, + CheckoutError, type Offer, type Plan, } from "../heygen/plan.js"; import { c } from "../ui/colors.js"; -import { failCommand } from "../utils/commandResult.js"; +import { trackBillingCheckoutFailed, trackBillingWaitTimeout } from "../telemetry/events.js"; +import { failCommand, setCommandExitCode } from "../utils/commandResult.js"; import { isUnattended } from "../utils/attendedTerminal.js"; import { defineCommand } from "../utils/defineCommand.js"; @@ -36,31 +38,46 @@ function priceLine( return `${needed ? `This run needs ${needed.toLocaleString("en-US")} credits. ` : ""}${credits} add-on credits: ${price}`; } +const WAIT_TIMEOUT_EXIT = 124; + +async function openCheckout( + kind: "plans" | "credits", + offer: Offer, + missing: number | undefined, + signal: AbortSignal, +): Promise { + if (isUnattended()) { + console.log(`Checkout link: ${c.accent(await createCheckout(kind, offer, missing))}`); + return; + } + const rl = createInterface({ input: process.stdin, output: process.stdout }); + try { + await rl.question("Press Enter to pay ", { signal }); + } finally { + rl.close(); + } + await openBrowser(await createCheckout(kind, offer, missing)); +} + async function offerCheckout( kind: "plans" | "credits", before: Plan | null, signal: AbortSignal, needed?: number, -) { +): Promise { const offer = await readOffer(); const missing = needed ? needed - (before?.credits ?? 0) : undefined; const line = priceLine(kind, offer, missing, needed); if (line) console.log(line); try { - if (isUnattended()) { - console.log(`Checkout link: ${c.accent(await createCheckout(kind, offer, missing))}`); - return; - } - const rl = createInterface({ input: process.stdin, output: process.stdout }); - try { - await rl.question("Press Enter to pay ", { signal }); - } finally { - rl.close(); - } - await openBrowser(await createCheckout(kind, offer, missing)); + await openCheckout(kind, offer, missing, signal); + return null; } catch (error) { - if (signal.aborted) return; + if (signal.aborted) return null; console.error(c.error(error instanceof Error ? error.message : String(error))); + if (!(error instanceof CheckoutError)) return null; + trackBillingCheckoutFailed({ reason: error.reason }); + return error; } } @@ -112,15 +129,25 @@ export default defineCommand({ await openBrowser(url); const deadline = Date.now() + (Number(args.timeout) || 600) * 1000; const stop = new AbortController(); + const giveUp = new AbortController(); const waiting = (async () => { - while (Date.now() < deadline) { + while (Date.now() < deadline && !giveUp.signal.aborted) { await new Promise((r) => setTimeout(r, 2000)); const now = await readPlan(); if (done(now)) return now; } return null; })().finally(() => stop.abort()); - await offerCheckout(action === "subscribe" ? "plans" : "credits", before, stop.signal, needed); + const checkoutFailure = await offerCheckout( + action === "subscribe" ? "plans" : "credits", + before, + stop.signal, + needed, + ); + if (checkoutFailure?.reason === "checkout_unavailable") { + giveUp.abort(); + failCommand(); + } console.log("Waiting for the person to finish ..."); const now = await waiting; if (now) { @@ -132,6 +159,8 @@ export default defineCommand({ `Still waiting on the ${action === "subscribe" ? "plans" : "credits"} page; run this again once it is done.`, ), ); - failCommand(); + if (checkoutFailure) failCommand(); + trackBillingWaitTimeout({ action }); + setCommandExitCode(WAIT_TIMEOUT_EXIT); }, }); diff --git a/packages/cli/src/heygen/plan.ts b/packages/cli/src/heygen/plan.ts index 0fba0b2af7..43bc8bad6e 100644 --- a/packages/cli/src/heygen/plan.ts +++ b/packages/cli/src/heygen/plan.ts @@ -156,6 +156,19 @@ export function stripeCheckoutUrl(value: unknown): string | undefined { : undefined; } +export type CheckoutFailure = "checkout_unavailable" | "checkout_network" | "checkout_rejected"; + +/** A checkout that could not be opened, tagged with a closed reason for telemetry. */ +export class CheckoutError extends Error { + constructor( + readonly reason: CheckoutFailure, + message: string, + ) { + super(message); + this.name = "CheckoutError"; + } +} + /** Creates the checkout with the CLI's own token and returns Stripe's page; throws when HeyGen refuses. */ export async function createCheckout( kind: "plans" | "credits", @@ -164,7 +177,10 @@ export async function createCheckout( ): Promise { const addon = kind === "credits" ? offer.addon : undefined; if (kind === "credits" && !addon) - throw new Error("Credits are not on sale for this account right now."); + throw new CheckoutError( + "checkout_unavailable", + "Credits are not on sale for this account right now.", + ); const body = addon ? { quantity: addonBlocks(addon, missing), @@ -184,7 +200,12 @@ export async function createCheckout( body: JSON.stringify(body), signal: AbortSignal.timeout(20_000), }, - ); + ).catch((error: unknown) => { + throw new CheckoutError( + "checkout_network", + `Could not reach HeyGen to open a checkout: ${error instanceof Error ? error.message : String(error)}`, + ); + }); const reply: unknown = await res.json().catch(() => null); const data = isRecord(reply) && isRecord(reply.data) ? reply.data : reply; const url = res.ok && isRecord(data) ? stripeCheckoutUrl(data.url) : undefined; @@ -193,5 +214,5 @@ export async function createCheckout( isRecord(reply) && typeof reply.message === "string" ? reply.message.slice(0, 200) : `HTTP ${res.status}`; - throw new Error(`HeyGen would not open a checkout: ${why}`); + throw new CheckoutError("checkout_rejected", `HeyGen would not open a checkout: ${why}`); } diff --git a/packages/cli/src/telemetry/events.ts b/packages/cli/src/telemetry/events.ts index ea7d8a8324..174326b116 100644 --- a/packages/cli/src/telemetry/events.ts +++ b/packages/cli/src/telemetry/events.ts @@ -1010,6 +1010,18 @@ export function trackReverseStartUnrecorded(props: { trackEvent("cli_reverse_start_unrecorded", { cause: props.cause, status: props.status }); } +// `billing` could not open a checkout. `reason` is the closed CheckoutFailure class, never the message. +export function trackBillingCheckoutFailed(props: { + reason: "checkout_unavailable" | "checkout_network" | "checkout_rejected"; +}): void { + trackEvent("cli_billing_checkout_failed", { reason: props.reason }); +} + +// `billing` waited out its timeout with no payment: the person did not finish, nothing is broken. +export function trackBillingWaitTimeout(props: { action: "subscribe" | "add-credits" }): void { + trackEvent("cli_billing_wait_timeout", { action: props.action }); +} + // grade-compare / compare stand up headless Chrome and render up to 16 cells. // Cell count, truncation-cap hits, and whether the render-ready timeout fired // are the signals needed before safely lifting the cap. Low-cardinality only.