11import schema from "../../../generated/api-schema" ;
2- import { useLogger , type MilkioApp } from ".." ;
2+ import { ExecuteResultFail , type MilkioApp } from ".." ;
3+ import { cwd } from "node:process" ;
4+ import chalk from "chalk" ;
5+ import { handleCatchError } from "../utils/handle-catch-error.ts" ;
36
47export const executeApiTests = async < Path extends Array < keyof ( typeof schema ) [ "apiTestsSchema" ] > > ( app : MilkioApp , path : Path | string | true | 1 | undefined ) => {
5- console . log ( `🥛 Milkio Api Testing..\n ` ) ;
8+ console . log ( `${ chalk . hex ( "#81C7D4" ) ( `🧊 test running on` ) } ${ chalk . hex ( "#999A9E" ) . underline ( cwd ( ) ) } ` ) ;
69
710 let pathArr = [ ] as Array < string > ;
811 if ( ! path || path === "1" || path === 1 || path === true ) {
@@ -11,61 +14,63 @@ export const executeApiTests = async <Path extends Array<keyof (typeof schema)["
1114 pathArr = [ path ] as Path ;
1215 }
1316
14- const tests = [ ] ;
1517 const startedAt = new Date ( ) . getTime ( ) ;
16-
1718 const apiTestHooks = await import ( "../../../src/api-test.ts" ) ;
1819 await apiTestHooks . default . onBootstrap ( ) ;
20+ const results : Array < { path : string , case : number , fail : boolean , failMessage ?: string } > = [ ] ;
21+ console . log ( chalk . hex ( "#0B346E" ) ( `₋₋₋₋₋₋₋₋` ) ) ;
1922
2023 for ( const pathRaw of pathArr ) {
2124 let path = pathRaw . replaceAll ( "\\" , "/" ) ;
2225 if ( path . startsWith ( "/" ) ) path = path . slice ( 1 ) as Path [ number ] ;
2326
24- tests . push (
25- // @ts -ignore
26- ( async ( ) => {
27- // @ts -ignore
28- const module = await schema . apiTestsSchema [ path ] ( ) . module ;
29- const cases = module . test . getCases ( ) ;
30- let i = 0 ;
31- for ( const cs of cases ) {
32- ++ i ;
33- const csStartedAt = new Date ( ) . getTime ( ) ;
34- const clear = setTimeout ( ( ) => {
35- console . error ( `------` ) ;
36- console . error ( `❌ TIMEOUT -- More than ${ cs . timeout ?? 8192 } ms` ) ;
37- console . error ( ` ${ cs . name } | Path: src/apps/ ${ path as string } .ts | Case: ${ i } ` ) ;
38- console . error ( `------` ) ;
39- throw new Error ( "" ) ;
40- } , cs . timeout ?? 8192 ) ;
41- await cs . handler ( {
42- ... ( ( await apiTestHooks . default . onBefore ( ) ) ?? { } ) ,
43- log : ( ... args : Array < unknown > ) => console . log ( ... args ) ,
44- // @ts -ignore
45- execute : async ( options ?: any ) => app . execute ( ( path as any ) , options ) ,
46- executeOther : async ( path : any , options ?: any ) => app . execute ( ( path as any ) , options ) ,
47- executeStream : async ( options ?: any ) => app . executeStream ( ( path as any ) , options ) ,
48- randParams : ( ) => app . randParams ( path as any ) ,
49- randOtherParams : ( path : any ) => app . randParams ( path ) ,
50- reject : ( message ?: string ) => {
51- console . error ( `------` ) ;
52- console . error ( `❌ REJECT -- ${ message ?? "Test not satisfied" } ` ) ;
53- console . error ( ` ${ cs . name } | Path: src/apps/ ${ path as string } .ts | Case: ${ i } | Time: ${ new Date ( ) . getTime ( ) - csStartedAt } ms` ) ;
54- console . error ( `------ `) ;
55- throw new Error ( "" ) ;
56- } ,
57- } as any ) ;
58- clearTimeout ( clear ) ;
59- console . log ( `✅ DIRECT -- ${ cs . name } | Path: src/apps/ ${ path as string } .ts | Case: ${ i } | Time: ${ new Date ( ) . getTime ( ) - csStartedAt } ms` ) ;
60- }
61- } ) ( ) ,
62- ) ;
27+ // @ts -ignore
28+ const module = await schema . apiTestsSchema [ path ] ( ) . module ;
29+ const cases = module . test . getCases ( ) ;
30+ let i = 0 ;
31+ for ( const cs of cases ) {
32+ ++ i ;
33+ const csStartedAt = new Date ( ) . getTime ( ) ;
34+ let fail = false ;
35+ let failMessage : string | undefined = undefined ;
36+ try {
37+ await cs . handler ( {
38+ ... ( ( await apiTestHooks . default . onBefore ( ) ) ?? { } ) ,
39+ log : ( ... args : Array < unknown > ) => console . log ( ... args ) ,
40+ // @ts -ignore
41+ execute : async ( options ?: any ) => app . execute ( ( path as any ) , options ) ,
42+ executeOther : async ( path : any , options ?: any ) => app . execute ( ( path as any ) , options ) ,
43+ executeStream : async ( options ?: any ) => app . executeStream ( ( path as any ) , options ) ,
44+ randParams : ( ) => app . randParams ( path as any ) ,
45+ randOtherParams : ( path : any ) => app . randParams ( path ) ,
46+ reject : ( message ?: string ) => {
47+ fail = true ;
48+ failMessage = message ;
49+ } ,
50+ } ) ;
51+ } catch ( e : any ) {
52+ const response = handleCatchError ( e , 'no-execute-id' ) as ExecuteResultFail ;
53+ fail = true ;
54+ failMessage = response . fail . message ;
55+ }
56+ if ( fail ) {
57+ console . log ( ` ${ chalk . hex ( "#D75455" ) ( `\nrejected` ) } ${ chalk . hex ( "#999A9E" ) ( ` · ${ cs . name } | path: src/apps/ ${ path as string } .ts | case: ${ i } | time: ${ new Date ( ) . getTime ( ) - csStartedAt } ms` ) } `) ;
58+ console . log ( chalk . hex ( "#999A9E" ) ( failMessage ?? "Test not satisfied" ) ) ;
59+ } else {
60+ console . log ( ` ${ chalk . hex ( "#1B813E" ) ( `directed` ) } ${ chalk . hex ( "#999A9E" ) ( ` · ${ cs . name } | path: src/apps/ ${ path as string } .ts | case: ${ i } | time: ${ new Date ( ) . getTime ( ) - csStartedAt } ms` ) } ` ) ;
61+ }
62+ results . push ( { path, case : i , fail , failMessage } ) ;
63+ console . log ( chalk . hex ( "#0B346E" ) ( `₋₋₋₋₋₋₋₋` ) ) ;
64+ await new Promise ( resolve => setTimeout ( resolve , 64 ) ) ;
65+ }
6366 }
6467
65- await Promise . all ( tests ) ;
66-
6768 const endedAt = new Date ( ) . getTime ( ) ;
6869
69- console . log ( `\n✅ All tests passed.` ) ;
70- console . log ( `🥛 Milkio Api Testing took ${ ( ( endedAt - startedAt ) / 1000 ) . toFixed ( 2 ) } s\n` ) ;
70+ const failTotal = results . filter ( r => r . fail ) . length ;
71+ const passTotal = results . length - failTotal ;
72+
73+ console . log ( "" ) ;
74+ if ( failTotal === 0 ) console . log ( chalk . hex ( "#1B813E" ) ( `🥳 all tests ${ chalk . hex ( "#1B813E" ) ( `passed` ) } ${ chalk . hex ( "#999A9E" ) ( `🌟 milkio testing took ${ ( ( endedAt - startedAt ) / 1000 ) . toFixed ( 2 ) } s\n` ) } ` ) ) ;
75+ else console . log ( chalk . hex ( "#999A9E" ) ( `🤗️️ ${ failTotal } test${ failTotal > 1 ? "s" : "" } ${ chalk . hex ( "#D75455" ) ( `failed` ) } ${ passTotal > 0 ? `, and ${ results . length - failTotal } ${ chalk . hex ( "#1B813E" ) ( `passed` ) } ` : '' } ${ chalk . hex ( "#999A9E" ) ( `🌟 milkio testing took ${ ( ( endedAt - startedAt ) / 1000 ) . toFixed ( 2 ) } s` ) } ` ) ) ;
7176} ;
0 commit comments