@checksum-ai/runtime 1.0.56 → 1.0.58
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli.js +6 -6
- package/index.js +30 -30
- package/package.json +1 -1
package/cli.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var w=Object.create;var h=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var k=Object.getPrototypeOf,P=Object.prototype.hasOwnProperty;var l=(
|
|
1
|
+
var w=Object.create;var h=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var k=Object.getPrototypeOf,P=Object.prototype.hasOwnProperty;var l=(i,t)=>h(i,"name",{value:t,configurable:!0});var R=(i,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of y(t))!P.call(i,o)&&o!==e&&h(i,o,{get:()=>t[o],enumerable:!(s=C(t,o))||s.enumerable});return i};var x=(i,t,e)=>(e=i!=null?w(k(i)):{},R(t||!i||!i.__esModule?h(e,"default",{value:i,enumerable:!0}):e,i));var r=require("fs"),u=x(require("child_process")),n=require("path");var d="checksum";function f({isInit:i,excludeLogin:t}){return[...i?["example.checksum.spec.ts","github-actions.example.yml"]:[],...t?[]:["login.ts"],"checksum.config.ts","playwright.config.ts","README.md",".gitignore.example"]}l(f,"getRuntimeFiles");var g=class{constructor(){this.TEST_RUN_MONITOR_PATH=(0,n.join)(__dirname,"test-run-monitor.js");this.CHECKSUM_API_URL="https://api.checksum.ai";this.CHECKSUM_APP_URL="https://app.checksum.ai";this.didFail=!1;this.isolationMode=!1;this.completeIndicators={upload:!1,tests:!1,report:!1};this.guardReturn=async(t,e=1e3,s="guardReturnTimedOut")=>{let o="guard-timed-out",a=l(async()=>(await this.awaitSleep(e+1e3),o),"guard"),c=await Promise.race([t,a()]);if(typeof c=="string"&&c===o)throw new Error(s);return c};this.awaitSleep=t=>new Promise(e=>setTimeout(e,t));this.printError=t=>{console.log("\x1B[31m%s\x1B[0m",t)}}async execute(){switch(process.argv.find(t=>t==="--help"||t==="-h")&&(await this.printHelp(process.argv[2]),process.exit(0)),process.argv[2]){case"init":this.install();break;case"test":await this.test(process.argv.slice(3));break;case"show-report":this.showReport(process.argv.slice(3));break;default:await this.printHelp()}process.exit(0)}async execCmd(t){let e=await u.spawn(t,{shell:!0,stdio:"inherit"});return new Promise((o,a)=>{e.on("exit",c=>{c===0?o(!0):a(new Error(`Checsum failed execution with code: ${c} `))})})}async getCmdOutput(t){return new Promise(function(e,s){u.exec(t,(o,a,c)=>{if(o){s(`Error executing command: ${o.message}`);return}e(a)})})}async printHelp(t){switch(t){default:console.log(`
|
|
2
2
|
Checksum CLI
|
|
3
3
|
Usage: checksum [command] [options]
|
|
4
4
|
|
|
@@ -7,13 +7,13 @@ init installs checksum files and folders
|
|
|
7
7
|
test [options] [test-filter...] runs checksum tests
|
|
8
8
|
help prints this help message
|
|
9
9
|
show-report [options] [report] show HTML report
|
|
10
|
-
`);break;case"test":try{let e="npx playwright test --help",
|
|
11
|
-
`);
|
|
12
|
-
`),console.log(
|
|
13
|
-
`))}catch(e){console.log("Error",e.message)}break;case"show-report":try{let e="npx playwright show-report --help",
|
|
10
|
+
`);break;case"test":try{let e="npx playwright test --help",s=(await this.getCmdOutput(e)).replace(/npx playwright/g,"yarn checksum").split(`
|
|
11
|
+
`);s.splice(5,0," --checksum-config=<config> Checksum configuration in JSON format").join(`
|
|
12
|
+
`),console.log(s.join(`
|
|
13
|
+
`))}catch(e){console.log("Error",e.message)}break;case"show-report":try{let e="npx playwright show-report --help",s=(await this.getCmdOutput(e)).replace(/npx playwright/g,"yarn checksum");console.log(s)}catch(e){console.log("Error",e.message)}break}}async showReport(t){let e=`npx playwright show-report ${t.join(" ")}`;try{await this.execCmd(e)}catch(s){console.log("Error showing report",s.message)}}async test(t){if(this.processChecksumArguments(t),this.setChecksumConfig(),!await this.getSession())return;let e;try{e=await this.guardReturn(this.startTestRunMonitor(this.testSession),1e4,"test run monitor timeout")}catch{console.log("Error starting test run monitor. Test results will not be available on checksum.")}this.buildVolatileConfig();let s=`${e?`CHECKSUM_UPLOAD_AGENT_PORT=${e} `:""}${this.config.options.hostReports?" PW_TEST_HTML_REPORT_OPEN=never":""} npx playwright test --config ${this.getPlaywrightConfigFile()} ${t.map(o=>`"${o}"`).join(" ")}`;await this.patchPlaywright();try{await this.execCmd(s),console.log("Tests execution finished")}catch(o){this.didFail=!0,console.log("Error during test",o.message)}finally{this.sendReportUploadRequest(),await this.patchPlaywright(!0),this.completeIndicators.tests=!0,await this.handleCompleteMessage()}}sendReportUploadRequest(){this.isolationMode||console.log("Waiting for test files to upload to Checksum..");let t=this.getPlaywrightReportPath();if(!(0,r.existsSync)(t)){console.log(`Could not find report file at ${t}`);return}this.testRunMonitorProcess.stdin.write(`cli:report=${t}`)}async patchPlaywright(t=!1){let e=`bash ${(0,n.join)(__dirname,"scripts/patch.sh")}${t?" off":""}`;try{await this.execCmd(e)}catch(s){console.log("Error patching playwright",s.message)}}getPlaywrightReportPath(){var o,a;let t=(0,n.join)(process.cwd(),"playwright-report"),e=require(this.getPlaywrightConfigFile()),{reporter:s}=e;return s instanceof Array&&s.length>1&&((o=s[1])!=null&&o.outputFolder)&&(t=(a=s[1])==null?void 0:a.outputFolder),process.env.PLAYWRIGHT_HTML_REPORT&&(t=process.env.PLAYWRIGHT_HTML_REPORT),(0,n.join)(t,"index.html")}getPlaywrightConfigFile(){return(0,n.join)(this.getRootDirPath(),"playwright.config.ts")}startTestRunMonitor(t){return new Promise(e=>{console.log("Starting test run monitor"),this.testRunMonitorProcess=u.spawn("node",[this.TEST_RUN_MONITOR_PATH,JSON.stringify({sessionId:t,checksumApiURL:this.CHECKSUM_API_URL,apiKey:this.config.apiKey}),...this.isolationMode?["isolated"]:[]]),this.testRunMonitorProcess.stdout.on("data",s=>{var p,m;let o=s.toString().trim();if(!o.startsWith("monitor")){(m=(p=this.config)==null?void 0:p.options)!=null&&m.printLogs&&console.log(`Message from test run monitor: ${o}`);return}let[a,c]=o.substring(o.indexOf(":")+1).split("=");a==="port"?e(c):this.handleTestRunMonitorMessage(a,c)}),this.testRunMonitorProcess.on("exit",(s,o)=>{console.log(`test run monitor process exited with code ${s} and signal ${o}`)}),this.testRunMonitorProcess.on("error",s=>{console.error(`Error starting test run monitor: ${s.message}`)})})}async handleTestRunMonitorMessage(t,e){switch(t){case"complete-with-errors":console.log("Error uploading test files to Checksum"),this.sendProcessingError().then(()=>{this.completeIndicators.upload=!0});break;case"complete":this.isolationMode||console.log("Test files uploaded successfully"),this.sendUploadsComplete().then(()=>{this.completeIndicators.upload=!0});break;case"report-uploaded":{if(this.isolationMode){this.completeIndicators.report=!0;break}console.log("Report generated and uploaded to Checksum");let s={};try{s=JSON.parse(e)}catch(o){console.log("Error parsing stats",o.message)}await this.sendTestRunEnd(s),this.completeIndicators.report=!0,console.log(`*******************
|
|
14
14
|
* Checksum report URL: ${this.CHECKSUM_APP_URL}/#/test-runs/${this.testSession}
|
|
15
15
|
*******************`);break}default:console.warn(`Unhandled test run monitor message: ${t}=${e}`)}}async handleCompleteMessage(){for(;;)Object.keys(this.completeIndicators).find(t=>!this.completeIndicators[t])?await this.awaitSleep(1e3):(console.log("Checksum test run complete"),this.shutdown(this.didFail?1:0))}shutdown(t=0){this.cleanup(),process.exit(t)}buildVolatileConfig(){if(!this.volatileChecksumConfig)return;let t=this.getVolatileConfigPath(),e=`
|
|
16
16
|
import { RunMode, getChecksumConfig } from "@checksum-ai/runtime";
|
|
17
17
|
|
|
18
18
|
export default getChecksumConfig(${JSON.stringify(this.config,null,2)});
|
|
19
|
-
`;(0,
|
|
19
|
+
`;(0,r.writeFileSync)(t,e)}cleanup(){var t,e;this.deleteVolatileConfig(),(t=this.testRunMonitorProcess)==null||t.stdin.write("cli:shutdown"),(e=this.testRunMonitorProcess)==null||e.kill()}async getSession(){try{if(!this.config.options.hostReports)return this.setIsolatedMode(),!0;let t=this.config.apiKey;if(!t||t==="<API key>")return this.printError("No API key found in checksum config - please set it in the config file - checksum.config.ts"),this.shutdown(1),!1;let e=JSON.stringify(await this.getEnvInfo()),s=await fetch(`${this.CHECKSUM_API_URL}/client-api/test-runs`,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json",ChecksumAppCode:t},body:e});return this.testSession=(await s.json()).uuid,!0}catch{return console.log("Error connecting to Checksum, the report will not be hosted"),this.setIsolatedMode(),!0}}setIsolatedMode(){this.isolationMode=!0,this.testSession="isolated-session"}async sendTestRunEnd(t){if(!this.isolationMode)try{let e=JSON.stringify({...t,endedAt:Date.now()});await this.updateTestRun(`${this.CHECKSUM_API_URL}/client-api/test-runs/${this.testSession}`,"PATCH",e)}catch(e){return console.log("Error sending test run end",e.message),null}}async sendUploadsComplete(){if(!this.isolationMode)try{await this.updateTestRun(`${this.CHECKSUM_API_URL}/client-api/test-runs/${this.testSession}/uploads-completed`,"PATCH")}catch(t){console.log("Error sending test run uploads complete",t.message)}}async sendProcessingError(){if(!this.isolationMode)try{await this.updateTestRun(`${this.CHECKSUM_API_URL}/client-api/test-runs/${this.testSession}/process-error`,"PATCH")}catch(t){console.log("Error sending test run processing error",t.message)}}async updateTestRun(t,e,s=void 0){return await fetch(t,{method:e,headers:{Accept:"application/json","Content-Type":"application/json",ChecksumAppCode:this.config.apiKey},body:s})}async getEnvInfo(){let t={commitHash:"",branch:"branch",environment:process.env.CI?"CI":"local",name:"name",startedAt:Date.now()};try{t.commitHash=(await this.getCmdOutput("git rev-parse HEAD")).toString().trim()}catch(e){console.log("Error getting git hash",e.message)}try{t.branch=(await this.getCmdOutput("git rev-parse --abbrev-ref HEAD")).toString().trim()}catch(e){console.log("Error getting branch",e.message)}try{t.name=(await this.getCmdOutput("git log -1 --pretty=%B")).toString().trim()}catch(e){console.log("Error getting name",e.message)}return t}getVolatileConfigPath(){return(0,n.join)(this.getRootDirPath(),"checksum.config.tmp.ts")}deleteVolatileConfig(){let t=this.getVolatileConfigPath();(0,r.existsSync)(t)&&(0,r.rmSync)(t)}setChecksumConfig(){this.config={...require((0,n.join)(this.getRootDirPath(),"checksum.config.ts")).default||{},...this.volatileChecksumConfig||{}},this.config.apiURL&&(this.CHECKSUM_API_URL=this.config.apiURL)}processChecksumArguments(t){this.deleteVolatileConfig();for(let e=0;e<t.length;e++){let s=t[e];if(s.startsWith("--checksum-config"))try{this.volatileChecksumConfig=JSON.parse(s.split("=")[1]),t.splice(e,1)}catch(o){console.log("Error parsing checksum config",o.message),this.volatileChecksumConfig=void 0}}}install(){console.log("Creating Checksum directory and necessary files to run your tests");let t=this.getRootDirPath();if((0,r.existsSync)(this.getRootDirPath())||(0,r.mkdirSync)(t),!(0,r.existsSync)(this.getChecksumRootOrigin()))throw new Error("Could not find checksum root directory, please install @checksum-ai/runtime package");let e=(0,r.existsSync)((0,n.join)(t,"checksum.config.ts"));f({isInit:!e}).forEach(s=>{let o=(0,n.join)(t,s);(0,r.existsSync)(o)||(0,r.copyFileSync)((0,n.join)(this.getChecksumRootOrigin(),s),o)}),(0,r.mkdirSync)((0,n.join)(t,"tests"),{recursive:!0}),["esra","har","trace","log"].forEach(s=>{(0,r.mkdirSync)((0,n.join)(t,"test-data",s),{recursive:!0})})}getRootDirPath(){return(0,n.join)(process.cwd(),d)}getChecksumRootOrigin(){return(0,n.join)(process.cwd(),"node_modules","@checksum-ai","runtime","checksum-root")}};l(g,"ChecksumCLI");(async()=>await new g().execute())();
|