@@ -27,6 +27,7 @@ async function runCliCapture(
2727 options ?: {
2828 cwd ?: string ;
2929 env ?: Record < string , string | undefined > ;
30+ sendToDaemon ?: ( req : Omit < DaemonRequest , 'token' > ) => Promise < DaemonResponse > ;
3031 } ,
3132) : Promise < RunResult > {
3233 let stdout = '' ;
@@ -61,10 +62,12 @@ async function runCliCapture(
6162 return true ;
6263 } ) as typeof process . stderr . write ;
6364
64- const sendToDaemon = async ( req : Omit < DaemonRequest , 'token' > ) : Promise < DaemonResponse > => {
65- calls . push ( req ) ;
66- return { ok : true , data : { } } ;
67- } ;
65+ const sendToDaemon =
66+ options ?. sendToDaemon ??
67+ ( async ( req : Omit < DaemonRequest , 'token' > ) : Promise < DaemonResponse > => {
68+ calls . push ( req ) ;
69+ return { ok : true , data : { } } ;
70+ } ) ;
6871
6972 try {
7073 await runCli ( argv , { sendToDaemon } ) ;
@@ -280,74 +283,36 @@ test('install-from-source forwards remote-config run id with explicit lease bind
280283 'utf8' ,
281284 ) ;
282285
283- let stdout = '' ;
284- let stderr = '' ;
285- let code : number | null = null ;
286286 const calls : Array < Omit < DaemonRequest , 'token' > > = [ ] ;
287-
288- const originalExit = process . exit ;
289- const originalStdoutWrite = process . stdout . write . bind ( process . stdout ) ;
290- const originalStderrWrite = process . stderr . write . bind ( process . stderr ) ;
291- const originalCwd = process . cwd ( ) ;
292- const previousEnv = new Map < string , string | undefined > ( ) ;
293-
294- process . chdir ( project ) ;
295- previousEnv . set ( 'HOME' , process . env . HOME ) ;
296- process . env . HOME = home ;
297-
298- ( process as any ) . exit = ( ( nextCode ?: number ) => {
299- throw new ExitSignal ( nextCode ?? 0 ) ;
300- } ) as typeof process . exit ;
301- ( process . stdout as any ) . write = ( ( chunk : unknown ) => {
302- stdout += String ( chunk ) ;
303- return true ;
304- } ) as typeof process . stdout . write ;
305- ( process . stderr as any ) . write = ( ( chunk : unknown ) => {
306- stderr += String ( chunk ) ;
307- return true ;
308- } ) as typeof process . stderr . write ;
309-
310- const sendToDaemon = async ( req : Omit < DaemonRequest , 'token' > ) : Promise < DaemonResponse > => {
311- calls . push ( req ) ;
312- return {
313- ok : true ,
314- data : {
315- launchTarget : 'com.example.demo' ,
316- packageName : 'com.example.demo' ,
287+ const result = await runCliCapture (
288+ [
289+ 'install-from-source' ,
290+ 'https://example.com/app.apk' ,
291+ '--remote-config' ,
292+ remoteConfig ,
293+ '--lease-id' ,
294+ 'lease-demo-001' ,
295+ '--json' ,
296+ ] ,
297+ {
298+ cwd : project ,
299+ env : { HOME : home } ,
300+ sendToDaemon : async ( req ) => {
301+ calls . push ( req ) ;
302+ return {
303+ ok : true ,
304+ data : {
305+ launchTarget : 'com.example.demo' ,
306+ packageName : 'com.example.demo' ,
307+ } ,
308+ } ;
317309 } ,
318- } ;
319- } ;
320-
321- try {
322- await runCli (
323- [
324- 'install-from-source' ,
325- 'https://example.com/app.apk' ,
326- '--remote-config' ,
327- remoteConfig ,
328- '--lease-id' ,
329- 'lease-demo-001' ,
330- '--json' ,
331- ] ,
332- { sendToDaemon } ,
333- ) ;
334- } catch ( error ) {
335- if ( error instanceof ExitSignal ) code = error . code ;
336- else throw error ;
337- } finally {
338- process . exit = originalExit ;
339- process . stdout . write = originalStdoutWrite ;
340- process . stderr . write = originalStderrWrite ;
341- process . chdir ( originalCwd ) ;
342- for ( const [ key , value ] of previousEnv . entries ( ) ) {
343- if ( value === undefined ) delete process . env [ key ] ;
344- else process . env [ key ] = value ;
345- }
346- }
310+ } ,
311+ ) ;
347312
348- assert . equal ( code , null ) ;
349- assert . equal ( stderr , '' ) ;
350- const payload = JSON . parse ( stdout ) ;
313+ assert . equal ( result . code , null ) ;
314+ assert . equal ( result . stderr , '' ) ;
315+ const payload = JSON . parse ( result . stdout ) ;
351316 assert . equal ( payload . success , true ) ;
352317 assert . equal ( payload . data . launchTarget , 'com.example.demo' ) ;
353318 assert . equal ( calls . length , 1 ) ;
0 commit comments