Skip to content

Commit f905430

Browse files
fix(cli): billing's stop signal fires at --timeout, not only when the poll returns
The checkout path stopped once the polling task ended, but the poll's readPlan carried no signal. A poll read stalled across the deadline kept the signal live, so a held offer could still create and print a checkout after --timeout. A timer now aborts the controller at the deadline on its own, the initial and polling plan reads take the same signal, and the deadline counts from the start of the command. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01HebQXN7wsMK6GqT3MNtqXw
1 parent bd7f4db commit f905430

2 files changed

Lines changed: 44 additions & 10 deletions

File tree

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

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const held = vi.hoisted(() => ({
1919
realOffer: false,
2020
headerCalls: 0,
2121
holdHeadersAfter: undefined as number | undefined,
22+
stallPoll: false,
2223
}));
2324

2425
vi.mock("../telemetry/events.js", () => ({
@@ -63,10 +64,14 @@ vi.mock("../heygen/plan.js", async (importOriginal) => {
6364
terminal.args.push(args);
6465
return held.checkout ?? "https://checkout.stripe.com/c/pay/cs_test_1";
6566
},
66-
readPlan: async () => ({
67-
plan: ++plan.calls === 1 || !never.paid ? "none" : "pro",
68-
credits: plan.calls === 1 || !never.paid ? 0 : 500,
69-
}),
67+
readPlan: async (stop?: AbortSignal) => {
68+
if (++plan.calls > 1 && held.stallPoll)
69+
return new Promise((resolve) => stop?.addEventListener("abort", () => resolve(null)));
70+
return {
71+
plan: plan.calls === 1 || !never.paid ? "none" : "pro",
72+
credits: plan.calls === 1 || !never.paid ? 0 : 500,
73+
};
74+
},
7075
};
7176
});
7277

@@ -97,6 +102,7 @@ afterEach(() => {
97102
realOffer: false,
98103
headerCalls: 0,
99104
holdHeadersAfter: undefined,
105+
stallPoll: false,
100106
});
101107
vi.restoreAllMocks();
102108
consumeCommandResult();
@@ -326,6 +332,29 @@ describe("billing once the wait has ended", () => {
326332
expect(terminal.checkouts).toBe(0);
327333
});
328334

335+
it("creates no checkout after --timeout while a plan poll is still unanswered", async () => {
336+
const log = vi.spyOn(console, "log").mockImplementation(() => undefined);
337+
vi.spyOn(console, "error").mockImplementation(() => undefined);
338+
never.paid = false;
339+
held.stallPoll = true;
340+
const offer = release<object>();
341+
held.offer = offer.promise;
342+
vi.useFakeTimers();
343+
let settled = false;
344+
const done = runCommand(billing, { rawArgs: ["subscribe", "--timeout", "4"] }).then(
345+
() => (settled = true),
346+
() => (settled = true),
347+
);
348+
await vi.advanceTimersByTimeAsync(6000);
349+
offer.resolve({ pro: { amount: 2900, currency: "usd" } });
350+
await vi.advanceTimersByTimeAsync(0);
351+
vi.useRealTimers();
352+
expect(terminal.checkouts).toBe(0);
353+
expect(log.mock.calls.flat().join("\n")).not.toContain("Checkout link");
354+
expect(settled).toBe(true);
355+
await done;
356+
});
357+
329358
it("ends when the purchase lands while a sign-in refresh is still unanswered", async () => {
330359
vi.spyOn(console, "log").mockImplementation(() => undefined);
331360
held.realOffer = true;

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ export default defineCommand({
128128
}
129129
const needed = Number(args.needed) || undefined;
130130
const skill = checkoutSkill(args.skill);
131-
const before = await readPlan();
131+
const deadline = Date.now() + (Number(args.timeout) || 600) * 1000;
132+
const stop = new AbortController();
133+
const atDeadline = setTimeout(() => stop.abort(), deadline - Date.now());
134+
atDeadline.unref?.();
135+
const before = await readPlan(stop.signal);
132136
const done = (p: Plan | null) =>
133137
action === "subscribe"
134138
? !!p && p.plan !== "none"
@@ -146,17 +150,18 @@ export default defineCommand({
146150
});
147151
console.log(`${action === "subscribe" ? "Plans" : "Credits"} page: ${c.accent(url)}`);
148152
await openBrowser(url);
149-
const deadline = Date.now() + (Number(args.timeout) || 600) * 1000;
150-
const stop = new AbortController();
151153
const giveUp = new AbortController();
152154
const waiting = (async () => {
153-
while (Date.now() < deadline && !giveUp.signal.aborted) {
155+
while (!stop.signal.aborted && !giveUp.signal.aborted) {
154156
await new Promise((r) => setTimeout(r, 2000));
155-
const now = await readPlan();
157+
const now = await readPlan(stop.signal);
156158
if (done(now)) return now;
157159
}
158160
return null;
159-
})().finally(() => stop.abort());
161+
})().finally(() => {
162+
clearTimeout(atDeadline);
163+
stop.abort();
164+
});
160165
const checkoutFailure = await offerCheckout(
161166
action === "subscribe" ? "plans" : "credits",
162167
before,

0 commit comments

Comments
 (0)