import { ActionsInTestEnum } from "@shared/constants/recordedActions" ; import { iAction } from "@shared/types/action" ; import { resolveToBackendPath , resolveToFrontEndPath } from "@shared/utils/url" ; import axios from "axios" ; import { shell } from "electron" ; import { getBrowserActions , getMainActions } from "runner-utils/src" ; import * as url from "url" ; class CrusherTests { public static async getTest ( testId , customBackendPath = undefined ) { const testInfoResponse = await axios . get < { events : Array < iAction > ; } > ( resolveToBackendPath ( `/tests/${ testId }` , customBackendPath ) ) ; const testSteps = testInfoResponse . data . events ; return testSteps ; } public static async getReplayableTestActions ( actions , isMainTest , customBackendPath = undefined ) { const out = [ ] ; const browserActions = getBrowserActions ( actions ) ; if ( isMainTest ) { out . push ( ... browserActions ) ; } const runAfterTest = browserActions . find ( ( action ) => action . type === ActionsInTestEnum . RUN_AFTER_TEST ) ; if ( runAfterTest ) out . push ( ... ( await this . getReplayableTestActions ( await this . getTest ( runAfterTest . payload . meta . value , customBackendPath ) , false ) ) ) ; const mainActions = getMainActions ( actions ) ; if ( ! isMainTest ) { mainActions . map ( ( action ) => { return action ; } ) ; } out . push ( ... mainActions ) ; return out ; } public static async saveTestDirectly ( events , projectId , userToken , customBackendPath = undefined , customFrontEndPath = undefined ) { return axios . post ( resolveToBackendPath ( "tests/actions/save.temp" , customBackendPath ) , { events : events } , { headers : { Accept : "application/json, text/plain, */*" , "Content-Type" : "application/json" , Authorization : `Bearer ${ userToken }` } , } ) . then ( async ( result ) => { await axios . post ( resolveToBackendPath ( `/projects/${ projectId }/tests/actions/create` , customBackendPath ) , { tempTestId : result . data . insertId , name : new Date ( ) . toDateString ( ) . substr ( 4 , 6 ) + " " + new Date ( ) . toLocaleTimeString ( ) . substr ( 0 , 10 ) , } , { headers : { Cookie : `isLoggedIn=true; token=${ userToken }` , } , } ) ; } ) ; } public static async saveTest ( events , customBackendPath = undefined , customFrontEndPath = undefined ) { return axios . post ( resolveToBackendPath ( "tests/actions/save.temp" , customBackendPath ) , { events : events , } , { headers : { Accept : "application/json, text/plain, */*" , "Content-Type" : "application/json" } , } ) . then ( async ( result ) => { shell . openExternal ( resolveToFrontEndPath ( `/?temp_test_id=${ result . data . insertId }&temp_test_type=save` , customFrontEndPath ) ) ; } ) ; } public static async updateTest ( events , testId , customBackendPath = undefined , customFrontEndPath = undefined ) { return axios . post ( resolveToBackendPath ( "tests/actions/save.temp" , customBackendPath ) , { events : events } , { headers : { Accept : "application/json, text/plain, */*" , "Content-Type" : "application/json" } , } ) . then ( async ( result ) => { shell . openExternal ( resolveToFrontEndPath ( `/?temp_test_id=${ result . data . insertId }&temp_test_type=update&update_test_id=${ testId }` , customFrontEndPath ) ) ; } ) ; } } export { CrusherTests } ;