@@ -3,39 +3,75 @@ import { afterEach, describe, expect, it, vi } from "vitest";
33import { consumeCommandResult } from "../utils/commandResult.js" ;
44
55const opened = vi . hoisted ( ( ) => [ ] as string [ ] ) ;
6+ const checkouts = vi . hoisted ( ( ) => [ ] as string [ ] ) ;
7+ const state = vi . hoisted ( ( ) => ( { plans : [ ] as string [ ] , unattended : true } ) ) ;
68
79vi . 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+ } ) ) ;
1016vi . 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
1929const { default : billing } = await import ( "./billing.js" ) ;
2030
2131afterEach ( ( ) => {
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+
2751describe ( "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} ) ;
0 commit comments