@@ -62,13 +62,26 @@ afterAll(async () => {
6262 await fs . rm ( root , { recursive : true , force : true } )
6363} )
6464
65- const withTimeout = < T > ( promise : Promise < T > , ms : number , label : string ) =>
66- Promise . race ( [
67- promise ,
68- new Promise < never > ( ( _ , reject ) => setTimeout ( ( ) => reject ( new Error ( `${ label } after ${ ms } ms` ) ) , ms ) ) ,
69- ] )
65+ // Fails with `what()` evaluated at timeout, so the message carries the output captured so far.
66+ async function waitFor ( check : ( ) => boolean , ms : number , what : ( ) => string ) {
67+ const deadline = Date . now ( ) + ms
68+ while ( ! check ( ) ) {
69+ if ( Date . now ( ) > deadline ) throw new Error ( `${ what ( ) } (after ${ ms } ms)` )
70+ await Bun . sleep ( 50 )
71+ }
72+ }
73+
74+ async function exitWithin < T > ( exited : Promise < T > , ms : number , what : string ) {
75+ let timer : ReturnType < typeof setTimeout > | undefined
76+ const timeout = new Promise < never > ( ( _ , reject ) => {
77+ timer = setTimeout ( ( ) => reject ( new Error ( `${ what } after ${ ms } ms` ) ) , ms )
78+ } )
79+ return Promise . race ( [ exited , timeout ] ) . finally ( ( ) => clearTimeout ( timer ) )
80+ }
7081
71- describe ( "auth login cancel" , ( ) => {
82+ // Spawns the real CLI, so like cliIt tests it runs only in CI's bounded subprocess pass.
83+ // Windows has no POSIX signals or Ctrl+C-as-SIGINT through a PTY.
84+ describe . skipIf ( ! ! process . env . OPENCODE_SKIP_SUBPROCESS || process . platform === "win32" ) ( "auth login cancel" , ( ) => {
7285 test ( "Ctrl+C in a terminal exits 130" , async ( ) => {
7386 const child = ptySpawn ( process . execPath , argv , { name : "xterm-256color" , cols : 120 , rows : 40 , cwd : root , env } )
7487 let output = ""
@@ -77,15 +90,13 @@ describe("auth login cancel", () => {
7790 } )
7891 const exited = new Promise < { exitCode : number } > ( ( resolve ) => child . onExit ( resolve ) )
7992 try {
80- await withTimeout (
81- ( async ( ) => {
82- while ( ! output . includes ( WAITING ) ) await Bun . sleep ( 50 )
83- } ) ( ) ,
93+ await waitFor (
94+ ( ) => output . includes ( WAITING ) ,
8495 60_000 ,
85- `never printed "${ WAITING } ": ${ output } ` ,
96+ ( ) => `never printed "${ WAITING } ": ${ output } ` ,
8697 )
8798 child . write ( "\x03" )
88- const { exitCode } = await withTimeout ( exited , 10_000 , "still running after Ctrl+C" )
99+ const { exitCode } = await exitWithin ( exited , 10_000 , "still running after Ctrl+C" )
89100 expect ( exitCode ) . toBe ( 130 )
90101 expect ( output ) . toContain ( "Sign-in canceled." )
91102 } finally {
@@ -101,27 +112,24 @@ describe("auth login cancel", () => {
101112 stdout : "pipe" ,
102113 stderr : "pipe" ,
103114 } )
104- try {
115+ let output = ""
116+ const decoder = new TextDecoder ( )
117+ const drained = ( async ( ) => {
105118 const reader = proc . stdout . getReader ( )
106- const decoder = new TextDecoder ( )
107- let output = ""
108- await withTimeout (
109- ( async ( ) => {
110- while ( ! output . includes ( WAITING ) ) {
111- const { value, done } = await reader . read ( )
112- if ( done ) throw new Error ( `exited before waiting: ${ output } ` )
113- output += decoder . decode ( value )
114- }
115- } ) ( ) ,
119+ for ( let next = await reader . read ( ) ; ! next . done ; next = await reader . read ( ) ) output += decoder . decode ( next . value )
120+ } ) ( )
121+ try {
122+ await waitFor (
123+ ( ) => output . includes ( WAITING ) ,
116124 60_000 ,
117- `never printed "${ WAITING } ": ${ output } ` ,
125+ ( ) => `never printed "${ WAITING } ": ${ output } ` ,
118126 )
119- reader . releaseLock ( )
120127 proc . kill ( "SIGINT" )
121- expect ( await withTimeout ( proc . exited , 10_000 , "still running after SIGINT" ) ) . toBe ( 130 )
128+ expect ( await exitWithin ( proc . exited , 10_000 , "still running after SIGINT" ) ) . toBe ( 130 )
122129 expect ( await new Response ( proc . stderr ) . text ( ) ) . toContain ( "Sign-in canceled." )
123130 } finally {
124131 proc . kill ( "SIGKILL" )
132+ await drained
125133 }
126134 } , 90_000 )
127135} )
0 commit comments