Skip to content

Commit 17dc9a3

Browse files
fix(cli): no second checkout after paying on the page, and billing tests stay offline
The pricing page opens before "Press Enter to pay", so a person who bought there and then pressed Enter got a second Stripe checkout. The plan is read again on Enter first. billing.test.ts mocks the offer and checkout, which otherwise went to the network.
1 parent bc055ef commit 17dc9a3

3 files changed

Lines changed: 60 additions & 15 deletions

File tree

‎docs/packages/cli.mdx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,8 +2260,11 @@ allowance or estimate what the next video will cost.
22602260

22612261
### `billing`
22622262

2263-
Opens the HyperFrames plans or credits page in the browser, then waits until the
2264-
account shows the change, so an agent's run carries on by itself. The page is on
2263+
Opens the HyperFrames plans or credits page in the browser and prints the price:
2264+
Pro's monthly price, or the credits that cover `--needed` and what they cost. At
2265+
a terminal, Enter opens Stripe's checkout; with no terminal (CI, a coding agent)
2266+
the checkout link is printed instead. Then it waits until the account shows the
2267+
change, so an agent's run carries on by itself. The page is on
22652268
the web app at hyperframes.studio. `HYPERFRAMES_PLANS_URL` and
22662269
`HYPERFRAMES_CREDITS_URL` point it at another page.
22672270

‎packages/cli/src/commands/billing.test.ts‎

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,75 @@ import { afterEach, describe, expect, it, vi } from "vitest";
33
import { consumeCommandResult } from "../utils/commandResult.js";
44

55
const opened = vi.hoisted(() => [] as string[]);
6+
const checkouts = vi.hoisted(() => [] as string[]);
7+
const state = vi.hoisted(() => ({ plans: [] as string[], unattended: true }));
68

79
vi.mock("../auth/browser.js", () => ({
810
openBrowser: async (url: string) => void opened.push(url),
911
}));
12+
vi.mock("../utils/attendedTerminal.js", () => ({ isUnattended: () => state.unattended }));
13+
vi.mock("node:readline/promises", () => ({
14+
createInterface: () => ({ question: async () => "", close: () => undefined }),
15+
}));
1016
vi.mock("../heygen/plan.js", async (importOriginal) => {
1117
const real = await importOriginal<typeof import("../heygen/plan.js")>();
12-
let calls = 0;
1318
return {
1419
...real,
15-
readPlan: async () => ({ plan: ++calls === 1 ? "none" : "pro", credits: 0 }),
20+
readPlan: async () => ({ plan: state.plans.shift() ?? "pro", credits: 0 }),
21+
readOffer: async () => ({ pro: { amount: 2500, currency: "usd" } }),
22+
createCheckout: async (kind: string) => {
23+
checkouts.push(kind);
24+
return "https://checkout.stripe.com/c/pay/cs_test_1";
25+
},
1626
};
1727
});
1828

1929
const { default: billing } = await import("./billing.js");
2030

2131
afterEach(() => {
2232
opened.length = 0;
33+
checkouts.length = 0;
2334
vi.restoreAllMocks();
2435
consumeCommandResult();
2536
});
2637

38+
async function subscribe(plans: string[], unattended: boolean, rawArgs: string[] = []) {
39+
state.plans = plans;
40+
state.unattended = unattended;
41+
const logged: string[] = [];
42+
vi.spyOn(console, "log").mockImplementation((line: string) => void logged.push(line));
43+
vi.useFakeTimers();
44+
const done = runCommand(billing, { rawArgs: ["subscribe", ...rawArgs] });
45+
await vi.advanceTimersByTimeAsync(2000);
46+
await done;
47+
vi.useRealTimers();
48+
return logged.join("\n");
49+
}
50+
2751
describe("billing subscribe", () => {
2852
it("opens the plans page credited to the feature and run that sent the person", async () => {
29-
vi.spyOn(console, "log").mockImplementation(() => undefined);
30-
vi.useFakeTimers();
31-
const done = runCommand(billing, {
32-
rawArgs: ["subscribe", "--feature", "reverse", "--session", "rvs_abc"],
33-
});
34-
await vi.advanceTimersByTimeAsync(2000);
35-
await done;
36-
vi.useRealTimers();
53+
await subscribe(["none", "pro"], true, ["--feature", "reverse", "--session", "rvs_abc"]);
3754
const url = new URL(opened[0] ?? "");
3855
expect(url.searchParams.get("feature")).toBe("reverse");
3956
expect(url.searchParams.get("rs")).toBe("rvs_abc");
4057
});
58+
59+
it("prints the price and the checkout link when nobody is at the terminal", async () => {
60+
const out = await subscribe(["none", "pro"], true);
61+
expect(out).toContain("HyperFrames Pro: $25.00 a month");
62+
expect(out).toContain("https://checkout.stripe.com/c/pay/cs_test_1");
63+
expect(checkouts).toEqual(["plans"]);
64+
});
65+
66+
it("opens Stripe's checkout on Enter", async () => {
67+
await subscribe(["none", "none", "pro"], false);
68+
expect(checkouts).toEqual(["plans"]);
69+
expect(opened.at(-1)).toBe("https://checkout.stripe.com/c/pay/cs_test_1");
70+
});
71+
72+
it("opens no second checkout when the person already paid on the page before Enter", async () => {
73+
await subscribe(["none", "pro"], false);
74+
expect(checkouts).toEqual([]);
75+
expect(opened).toHaveLength(1);
76+
});
4177
});

‎packages/cli/src/commands/billing.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ function priceLine(
3838
}
3939

4040
/** Prepares the checkout with the CLI's own token: opens it when a person presses Enter, prints it when nobody can.
41-
* A refusal is printed and the page stays the way to buy. */
42-
async function offerCheckout(kind: "plans" | "credits", before: Plan | null, needed?: number) {
41+
* A refusal is printed and the page stays the way to buy. Bought on the page before Enter: no second checkout. */
42+
async function offerCheckout(
43+
kind: "plans" | "credits",
44+
before: Plan | null,
45+
done: (p: Plan | null) => boolean,
46+
needed?: number,
47+
) {
4348
const offer = await readOffer();
4449
const missing = needed ? needed - (before?.credits ?? 0) : undefined;
4550
const line = priceLine(kind, offer, missing, needed);
@@ -52,6 +57,7 @@ async function offerCheckout(kind: "plans" | "credits", before: Plan | null, nee
5257
const rl = createInterface({ input: process.stdin, output: process.stdout });
5358
await rl.question("Press Enter to pay ");
5459
rl.close();
60+
if (done(await readPlan())) return;
5561
await openBrowser(await createCheckout(kind, offer, missing));
5662
} catch (error) {
5763
console.error(c.error(error instanceof Error ? error.message : String(error)));
@@ -104,7 +110,7 @@ export default defineCommand({
104110
});
105111
console.log(`${action === "subscribe" ? "Plans" : "Credits"} page: ${c.accent(url)}`);
106112
await openBrowser(url);
107-
await offerCheckout(action === "subscribe" ? "plans" : "credits", before, needed);
113+
await offerCheckout(action === "subscribe" ? "plans" : "credits", before, done, needed);
108114
console.log("Waiting for the person to finish ...");
109115
const deadline = Date.now() + (Number(args.timeout) || 600) * 1000;
110116
while (Date.now() < deadline) {

0 commit comments

Comments
 (0)