@@ -5,34 +5,47 @@ import type { Plan } from "../heygen/plan.js";
55
66const opened = vi . hoisted ( ( ) => [ ] as string [ ] ) ;
77const checkouts = vi . hoisted ( ( ) => [ ] as { kind : string ; missing ?: number } [ ] ) ;
8- const state = vi . hoisted ( ( ) => ( { plans : [ ] as ( Plan | null ) [ ] , unattended : true , enters : 0 } ) ) ;
8+ const state = vi . hoisted ( ( ) => ( {
9+ plans : [ ] as ( Plan | null ) [ ] ,
10+ afterPlans : { plan : "pro" , credits : 1_000_000 } as Plan | null ,
11+ unattended : true ,
12+ enters : 0 ,
13+ enterHeld : false ,
14+ noPrice : false ,
15+ } ) ) ;
916
1017vi . mock ( "../auth/browser.js" , ( ) => ( {
1118 openBrowser : async ( url : string ) => void opened . push ( url ) ,
1219} ) ) ;
1320vi . mock ( "../utils/attendedTerminal.js" , ( ) => ( { isUnattended : ( ) => state . unattended } ) ) ;
1421vi . mock ( "node:readline/promises" , ( ) => ( {
1522 createInterface : ( ) => ( {
16- question : async ( ) => void state . enters ++ ,
23+ question : ( _prompt : string , opts ?: { signal ?: AbortSignal } ) =>
24+ state . enterHeld
25+ ? new Promise ( ( _ , reject ) => opts ?. signal ?. addEventListener ( "abort" , reject ) )
26+ : Promise . resolve ( void state . enters ++ ) ,
1727 close : ( ) => undefined ,
1828 } ) ,
1929} ) ) ;
2030vi . mock ( "../heygen/plan.js" , async ( importOriginal ) => {
2131 const real = await importOriginal < typeof import ( "../heygen/plan.js" ) > ( ) ;
2232 return {
2333 ...real ,
24- readPlan : async ( ) => ( state . plans . length ? state . plans . shift ( ) ! : pro ( 1_000_000 ) ) ,
25- readOffer : async ( ) => ( {
26- pro : { amount : 2500 , currency : "usd" } ,
27- addon : {
28- unitsPerBlock : 1000 ,
29- unitAmount : 100 ,
30- currency : "usd" ,
31- min : 1 ,
32- max : 100 ,
33- recommended : 25 ,
34- } ,
35- } ) ,
34+ readPlan : async ( ) => ( state . plans . length ? state . plans . shift ( ) ! : state . afterPlans ) ,
35+ readOffer : async ( ) =>
36+ state . noPrice
37+ ? { }
38+ : {
39+ pro : { amount : 2500 , currency : "usd" } ,
40+ addon : {
41+ unitsPerBlock : 1000 ,
42+ unitAmount : 100 ,
43+ currency : "usd" ,
44+ min : 1 ,
45+ max : 100 ,
46+ recommended : 25 ,
47+ } ,
48+ } ,
3649 createCheckout : async ( kind : string , _offer : unknown , missing ?: number ) => {
3750 checkouts . push ( { kind, missing } ) ;
3851 return "https://checkout.stripe.com/c/pay/cs_test_1" ;
@@ -48,8 +61,9 @@ const { default: billing } = await import("./billing.js");
4861afterEach ( ( ) => {
4962 opened . length = 0 ;
5063 checkouts . length = 0 ;
51- state . enters = 0 ;
64+ Object . assign ( state , { afterPlans : pro ( 1_000_000 ) , enters : 0 , enterHeld : false , noPrice : false } ) ;
5265 vi . restoreAllMocks ( ) ;
66+ vi . useRealTimers ( ) ;
5367 consumeCommandResult ( ) ;
5468} ) ;
5569
@@ -64,10 +78,14 @@ async function billingRun(
6478 vi . spyOn ( console , "log" ) . mockImplementation ( ( line : string ) => void logged . push ( line ) ) ;
6579 vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => undefined ) ;
6680 vi . useFakeTimers ( ) ;
67- const done = runCommand ( billing , { rawArgs } ) ;
81+ const failed = runCommand ( billing , { rawArgs } ) . then (
82+ ( ) => undefined ,
83+ ( error : unknown ) => error ,
84+ ) ;
6885 await vi . advanceTimersByTimeAsync ( 2000 ) ;
69- await done ;
86+ const error = await failed ;
7087 vi . useRealTimers ( ) ;
88+ if ( error ) throw error ;
7189 return logged . join ( "\n" ) ;
7290}
7391
@@ -105,6 +123,40 @@ describe("billing subscribe", () => {
105123 expect ( checkouts ) . toEqual ( [ ] ) ;
106124 expect ( out ) . toContain ( "Could not read your plan" ) ;
107125 } ) ;
126+
127+ it . each < [ string , Plan | null , boolean ] > ( [
128+ [ "an unreadable plan, unattended" , null , true ] ,
129+ [ "a stand-in plan, unattended" , { plan : "none" , credits : 0 , mock : true } , true ] ,
130+ [ "an unreadable plan, attended" , null , false ] ,
131+ ] ) ( "opens no checkout from %s at the start" , async ( _label , before , unattended ) => {
132+ const out = await billingRun ( [ before , pro ( 0 ) ] , unattended ) ;
133+ expect ( checkouts ) . toEqual ( [ ] ) ;
134+ expect ( state . enters ) . toBe ( 0 ) ;
135+ expect ( out ) . toContain ( "Could not read your plan" ) ;
136+ } ) ;
137+
138+ it . each ( [ true , false ] ) (
139+ "opens no checkout when the price cannot be read (unattended: %s)" ,
140+ async ( unattended ) => {
141+ state . noPrice = true ;
142+ const out = await billingRun ( [ free , pro ( 0 ) ] , unattended ) ;
143+ expect ( checkouts ) . toEqual ( [ ] ) ;
144+ expect ( out ) . toContain ( "Could not read the price" ) ;
145+ } ,
146+ ) ;
147+
148+ it ( "carries on when the person pays on the page and never presses Enter" , async ( ) => {
149+ state . enterHeld = true ;
150+ const out = await billingRun ( [ free , pro ( 0 ) ] , false ) ;
151+ expect ( out ) . toContain ( "✓ Done" ) ;
152+ expect ( checkouts ) . toEqual ( [ ] ) ;
153+ } ) ;
154+
155+ it ( "times out while the Enter prompt is still waiting" , async ( ) => {
156+ Object . assign ( state , { enterHeld : true , afterPlans : free } ) ;
157+ await expect ( billingRun ( [ free ] , false , [ "subscribe" , "--timeout" , "1" ] ) ) . rejects . toThrow ( ) ;
158+ expect ( checkouts ) . toEqual ( [ ] ) ;
159+ } ) ;
108160} ) ;
109161
110162describe ( "billing top-up" , ( ) => {
0 commit comments