'use strict' ; import { LuaVm } from '../../app/lua-vm/lua-vm' ; import { readFileSync } from 'fs' ; import { join , basename } from 'path' ; export class LuaVmWrapper { luaVm : LuaVm ; onMap : Map < string , any > ; schedMap : Map < string , any > ; requestParams : any [ ] = [ ] ; constructor ( luaVm , onMap , schedMap ) { this . luaVm = luaVm ; this . onMap = onMap ; this . schedMap = schedMap ; } publish ( eventType , eventContent ) { const funcs = this . onMap . get ( eventType ) ; if ( funcs ) { funcs . map ( f => f ( eventContent ) ) ; } return this . requestParams ; } invoke ( schedName ) { const funcs = this . schedMap . get ( schedName ) ; if ( funcs ) { funcs . map ( f => f ( ) ) ; } return this . requestParams ; } clean ( ) { this . requestParams . length = 0 ; } } param path param injectMap export async function prepareLuaTest ( path , opt ? ) { const compName = basename ( path ) ; const luaVm = new LuaVm ( ) ; const cc = await import ( join ( '../../app/component/' , compName , '/defaultConfig' ) ) ; const compConfig = cc . default ; const hypertronsConfig = JSON . parse ( readFileSync ( join ( __dirname , '../../.github/hypertrons.json' ) ) . toString ( ) ) ; const luaContent = readFileSync ( join ( __dirname , '../../app/component/' , compName , '/index.lua' ) ) . toString ( ) ; const defaultInjectMap = new Map ( ) ; const onMap = new Map ( ) ; const schedMap = new Map ( ) ; const luaTestVm = new LuaVmWrapper ( luaVm , onMap , schedMap ) ; defaultMockLuaMethods . map ( m => { defaultInjectMap . set ( m , ( ... args ) => { luaTestVm . requestParams . push ( [ m , ... args ] ) ; } ) ; } ) ; defaultInjectMap . set ( 'on' , ( eventType , cb ) => { if ( ! onMap . get ( eventType ) ) { onMap . set ( eventType , [ ] ) ; } onMap . get ( eventType ) . push ( cb ) ; } ) ; defaultInjectMap . set ( 'sched' , ( schedName , _schedTime , cb ) => { if ( ! schedMap . get ( schedName ) ) { schedMap . set ( schedName , [ ] ) ; } schedMap . get ( schedName ) . push ( cb ) ; } ) ; defaultInjectMap . forEach ( ( v , k ) => { luaVm . set ( k , v ) ; } ) ; if ( opt ) { if ( opt . injectMap ) { opt . injectMap . forEach ( ( v , k ) => { luaVm . set ( k , v ) ; } ) ; } if ( opt . customConfig ) { Object . assign ( hypertronsConfig , opt . customConfig ) ; } } luaVm . set ( 'config' , hypertronsConfig ) ; luaVm . set ( 'compConfig' , compConfig ) ; luaVm . set ( 'compName' , compName ) ; luaVm . run ( luaContent ) ; return luaTestVm ; } const defaultMockLuaMethods = [ 'getData' , 'getRoles' , 'addIssue' , 'assign' , 'addIssueComment' , 'addLabels' , 'toNow' , 'log' , 'checkAuth' , 'merge' , 'runCI' , 'sendToSlack' , 'sendToMail' , 'sendToDingTalk' , 'labelSetup' , 'updateIssue' , 'updatePull' , 'removeLabel' , ] ;