@@ -5,47 +5,60 @@ import { TaskList } from '../src/task_list';
5
5
class TestContext {
6
6
input = new PassThrough ( ) ;
7
7
output = new PassThrough ( ) ;
8
- expectations : ( ( ) => boolean ) [ ] = [ ] ;
9
- tl = new TaskList ( this . input , this . output ) ;
10
-
11
- async run ( ) {
12
- this . tl . run ( ) ;
8
+ taskList = new TaskList ( this . input , this . output ) ;
9
+ run ( ) {
10
+ this . taskList . run ( ) ;
11
+ }
13
12
14
- for ( const expectation of this . expectations ) {
15
- await new Promise < void > ( ( resolve ) =>
16
- this . output . once ( 'readable' , ( ) => {
17
- if ( expectation ( ) ) resolve ( ) ;
18
- } ) ,
19
- ) ;
13
+ emptyOutput ( ) {
14
+ while ( this . output . read ( ) ) {
15
+ // Clear the output stream
20
16
}
17
+ }
21
18
22
- this . input . end ( ) ;
23
- this . output . end ( ) ;
19
+ sendCommand ( command : string ) {
20
+ this . emptyOutput ( ) ;
21
+ this . input . write ( `${ command } \n` ) ;
24
22
}
25
23
26
- expectOutput ( lines : string [ ] ) {
27
- let text = lines . join ( '\n' ) + '\n' ;
28
- this . expectations . push ( ( ) => {
29
- const data = this . output . read ( text . length ) ?. toString ( ) ;
30
- expect ( data ) . toBe ( text ) ;
31
- return ! ! data ;
32
- } ) ;
24
+ getOutput ( ) {
25
+ let output = '' ;
26
+ let chunk = this . output . read ( ) ;
27
+ if ( chunk === null ) {
28
+ return null ;
29
+ }
30
+ while ( chunk !== null ) {
31
+ output += chunk . toString ( ) ;
32
+ chunk = this . output . read ( ) ;
33
+ }
34
+ return output ;
35
+ }
36
+ getOutputWithoutPrompt ( ) {
37
+ const output = this . getOutput ( ) ;
38
+ if ( output === null ) {
39
+ return null ;
40
+ }
41
+ // expect ends with '> ' and remove it
42
+ expect ( output . endsWith ( '\n> ' ) ) . toBe ( true ) ;
43
+ return output . slice ( 0 , - 3 ) ; // Remove the trailing '> '
33
44
}
34
45
35
- sendCommand ( command : string ) {
36
- this . expectations . push ( ( ) => {
37
- const prompt = this . output . read ( 2 ) ?. toString ( ) ;
38
- expect ( prompt ) . toBe ( '> ') ;
39
- this . input . write ( ` ${ command } \n` ) ;
40
- return ! ! prompt ;
41
- } ) ;
46
+ expectOutput ( lines : string [ ] ) {
47
+ const output = this . getOutputWithoutPrompt ( ) ;
48
+ if ( output === null ) {
49
+ throw new Error ( 'Output is null ') ;
50
+ }
51
+ const expectedOutput = lines . join ( '\n' ) ;
52
+ expect ( output ) . toBe ( expectedOutput ) ;
42
53
}
43
54
}
44
55
45
56
describe ( 'TaskList Application' , ( ) => {
46
57
test ( 'full interaction test' , async ( ) => {
47
58
const ctx = new TestContext ( ) ;
48
59
60
+ ctx . run ( ) ;
61
+
49
62
ctx . sendCommand ( 'show' ) ;
50
63
51
64
ctx . sendCommand ( 'add project secrets' ) ;
@@ -84,7 +97,5 @@ describe('TaskList Application', () => {
84
97
'' ,
85
98
] ) ;
86
99
ctx . sendCommand ( 'quit' ) ;
87
-
88
- await ctx . run ( ) ;
89
100
} ) ;
90
101
} ) ;
0 commit comments