Skip to content

Commit 20eaced

Browse files
fix(cli): billing re-reads the price on Enter, and asks again when it changed
1 parent fbb6487 commit 20eaced

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const state = vi.hoisted(() => ({
1212
enters: 0,
1313
enterHeld: false,
1414
noPrice: false,
15+
proPrices: [] as number[],
1516
}));
1617

1718
vi.mock("../auth/browser.js", () => ({
@@ -36,7 +37,7 @@ vi.mock("../heygen/plan.js", async (importOriginal) => {
3637
state.noPrice
3738
? {}
3839
: {
39-
pro: { amount: 2500, currency: "usd" },
40+
pro: { amount: state.proPrices.shift() ?? 2500, currency: "usd" },
4041
addon: {
4142
unitsPerBlock: 1000,
4243
unitAmount: 100,
@@ -61,7 +62,9 @@ const { default: billing } = await import("./billing.js");
6162
afterEach(() => {
6263
opened.length = 0;
6364
checkouts.length = 0;
64-
Object.assign(state, { afterPlans: pro(1_000_000), enters: 0, enterHeld: false, noPrice: false });
65+
Object.assign(state, {
66+
...{ afterPlans: pro(1_000_000), enters: 0, enterHeld: false, noPrice: false, proPrices: [] },
67+
});
6568
vi.restoreAllMocks();
6669
vi.useRealTimers();
6770
consumeCommandResult();
@@ -145,6 +148,14 @@ describe("billing subscribe", () => {
145148
},
146149
);
147150

151+
it("shows a Pro price that changed before Enter and asks again", async () => {
152+
state.proPrices = [2500, 3000, 3000];
153+
const out = await billingRun([free, free, free, pro(0)], false);
154+
expect(out).toContain("HyperFrames Pro: $30.00 a month");
155+
expect(state.enters).toBe(2);
156+
expect(checkouts).toEqual([{ kind: "plans" }]);
157+
});
158+
148159
it("carries on when the person pays on the page and never presses Enter", async () => {
149160
state.enterHeld = true;
150161
const out = await billingRun([free, pro(0)], false);

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ type Done = (p: Plan | null) => boolean;
4040

4141
const readFromTheServer = (p: Plan | null): p is Plan => !!p && !p.mock;
4242
const NO_PLAN = "Could not read your plan, so no checkout opened. Finish on the page.";
43+
const NO_PRICE = "Could not read the price, so no checkout opened. Finish on the page.";
4344

4445
async function payOrShowTheNewPrice(
4546
kind: "plans" | "credits",
46-
offer: Offer,
4747
done: Done,
4848
shown: string,
4949
stop: AbortSignal,
@@ -53,8 +53,10 @@ async function payOrShowTheNewPrice(
5353
if (stop.aborted) return undefined;
5454
if (!readFromTheServer(now)) return void console.log(NO_PLAN);
5555
if (done(now)) return undefined;
56+
const offer = await readOffer();
5657
const missing = needed ? needed - now.credits : undefined;
57-
const line = priceLine(kind, offer, missing, needed) ?? shown;
58+
const line = priceLine(kind, offer, missing, needed);
59+
if (!line) return void console.log(NO_PRICE);
5860
if (line !== shown) {
5961
console.log(line);
6062
return line;
@@ -66,7 +68,6 @@ async function payOrShowTheNewPrice(
6668

6769
async function checkoutOnEnterAtTheCurrentPrice(
6870
kind: "plans" | "credits",
69-
offer: Offer,
7071
done: Done,
7172
shown: string,
7273
stop: AbortSignal,
@@ -76,7 +77,7 @@ async function checkoutOnEnterAtTheCurrentPrice(
7677
try {
7778
for (let price: string | undefined = shown; price; ) {
7879
await rl.question("Press Enter to pay, or finish on the page ", { signal: stop });
79-
price = await payOrShowTheNewPrice(kind, offer, done, price, stop, needed);
80+
price = await payOrShowTheNewPrice(kind, done, price, stop, needed);
8081
}
8182
} catch (error) {
8283
if (!stop.aborted) throw error;
@@ -97,13 +98,12 @@ async function offerCheckoutOrPrintTheLink(
9798
const offer = await readOffer();
9899
const missing = needed ? needed - before.credits : undefined;
99100
const line = priceLine(kind, offer, missing, needed);
100-
if (!line)
101-
return void console.log("Could not read the price, so no checkout opened. Finish on the page.");
101+
if (!line) return void console.log(NO_PRICE);
102102
console.log(line);
103103
try {
104104
if (isUnattended())
105105
console.log(`Checkout link: ${c.accent(await createCheckout(kind, offer, missing))}`);
106-
else await checkoutOnEnterAtTheCurrentPrice(kind, offer, done, line, stop, needed);
106+
else await checkoutOnEnterAtTheCurrentPrice(kind, done, line, stop, needed);
107107
} catch (error) {
108108
console.error(c.error(error instanceof Error ? error.message : String(error)));
109109
}

0 commit comments

Comments
 (0)