1+ import { describe , it } from 'mocha' ;
2+ import { expect } from 'chai' ;
3+ //NOTE: no extensions in tests because it's excluded in tsconfig.json and
4+ //we are testing in a typescript environment via `ts-mocha -r tsx` (esm)
5+ import Terminal from '../src/router/Terminal' ;
6+
7+ describe ( 'Terminal Tests' , ( ) => {
8+ it ( 'Should get args' , async ( ) => {
9+ const terminal = new Terminal ( [ 'command' , '--i' , 'value' ] ) ;
10+ expect ( terminal . args [ 0 ] ) . to . equal ( '--i' ) ;
11+ expect ( terminal . args [ 1 ] ) . to . equal ( 'value' ) ;
12+ } )
13+ it ( 'Should get brand' , async ( ) => {
14+ const terminal = new Terminal ( [ ] , '[test]' ) ;
15+ expect ( terminal . brand ) . to . equal ( '[test]' ) ;
16+ } )
17+ it ( 'Should configure controls' , async ( ) => {
18+ const terminal = new Terminal ( [ 'command' , '--i' , 'value' ] , '[test]' ) ;
19+ const control = terminal . control ;
20+ expect ( control . brand ) . to . equal ( '[test]' ) ;
21+ expect ( typeof control . error ) . to . equal ( 'function' ) ;
22+ expect ( typeof control . success ) . to . equal ( 'function' ) ;
23+ expect ( typeof control . warning ) . to . equal ( 'function' ) ;
24+ expect ( typeof control . info ) . to . equal ( 'function' ) ;
25+ expect ( typeof control . output ) . to . equal ( 'function' ) ;
26+ expect ( typeof control . input ) . to . equal ( 'function' ) ;
27+ } )
28+ it ( 'Should parse data' , async ( ) => {
29+ const terminal = new Terminal ( [ 'command' , '--i' , 'value' ] ) ;
30+ expect ( terminal . data . i ) . to . equal ( 'value' ) ;
31+ } )
32+ it ( 'Should expect arguments' , async ( ) => {
33+ const terminal = new Terminal ( [ 'command' , '--i' , 'value' ] ) ;
34+ const input = terminal . expect ( [ 'i' , 'input' ] , 'default' ) ;
35+ expect ( input ) . to . equal ( 'value' ) ;
36+ } )
37+ it ( 'Should run command successfully' , async ( ) => {
38+ const terminal = new Terminal ( [ 'command' , '--i' , 'value' ] ) ;
39+ terminal . on ( 'command' , ( req , res ) => {
40+ res . setResults ( req . data ( ) ) ;
41+ } )
42+ const response = await terminal . run < { i : string } > ( ) ;
43+ expect ( response . code ) . to . equal ( 200 ) ;
44+ expect ( response . status ) . to . equal ( 'OK' ) ;
45+ expect ( response . total ) . to . equal ( 1 ) ;
46+ expect ( response . results ?. i ) . to . equal ( 'value' ) ;
47+ } )
48+ it ( 'Should run command then error' , async ( ) => {
49+ const terminal = new Terminal ( [ 'command' , '--i' , 'value' ] ) ;
50+ terminal . on ( 'command' , ( req , res ) => {
51+ res . setError ( 'error message' ) ;
52+ } )
53+ const response = await terminal . run < { i : string } > ( ) ;
54+ expect ( response . code ) . to . equal ( 400 ) ;
55+ expect ( response . status ) . to . equal ( 'Bad Request' ) ;
56+ expect ( response . error ) . to . equal ( 'error message' ) ;
57+ } )
58+ } )
0 commit comments