-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(repl): refactor to use
processInteract
(#373)
- Loading branch information
1 parent
a8fb152
commit 005bcef
Showing
3 changed files
with
122 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { Readable } from 'node:stream'; | ||
import { on } from 'events'; | ||
import { setTimeout } from 'timers/promises'; | ||
|
||
type MaybePromise<T> = T | Promise<T>; | ||
|
||
export const processInteract = async ( | ||
stdout: Readable, | ||
actions: ((data: string) => MaybePromise<boolean | void>)[], | ||
timeout: number, | ||
) => { | ||
const startTime = Date.now(); | ||
const logs: [time: number, string][] = []; | ||
|
||
let currentAction = actions.shift(); | ||
|
||
const ac = new AbortController(); | ||
setTimeout(timeout, true, ac).then( | ||
() => { | ||
if (currentAction) { | ||
console.error(`Timeout ${timeout}ms exceeded:`); | ||
console.log(logs); | ||
} | ||
}, | ||
() => {}, | ||
); | ||
|
||
while (currentAction) { | ||
for await (const [chunk] of on(stdout, 'data')) { | ||
const chunkString = chunk.toString(); | ||
logs.push([ | ||
Date.now() - startTime, | ||
chunkString, | ||
]); | ||
|
||
const gotoNextAction = await currentAction(chunkString); | ||
if (gotoNextAction) { | ||
currentAction = actions.shift(); | ||
break; | ||
} | ||
} | ||
} | ||
ac.abort(); | ||
}; |