@checksum-ai/runtime 3.0.3-beta → 3.0.3
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/.env +1 -1
- package/cli.js +2 -2
- package/package.json +1 -1
package/.env
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
CHECKSUM_RUNTIME_BUILD_TIME=2026-05-
|
|
1
|
+
CHECKSUM_RUNTIME_BUILD_TIME=2026-05-07T08:40:25.654Z
|
package/cli.js
CHANGED
|
@@ -749,9 +749,9 @@ ${s.description}`})}async onTestComplete(s){this.testRunAnalytics.testRunEnd({su
|
|
|
749
749
|
|
|
750
750
|
export default getChecksumConfig(${JSON.stringify(this.config,null,2)});
|
|
751
751
|
`;(0,Ug.writeFileSync)(n,s)}async cleanup(){this.runtimeAnalytics&&(Oc("Flushing events..."),await Zd(this.runtimeAnalytics.close(),2e4)),this.deleteVolatileConfig(),this.childProcesses.forEach(n=>{n.kill("SIGTERM")})}async getEnvInfo(){let n={commitHash:"",branch:"branch",environment:process.env.CI?"CI":"local",name:"name",startedAt:Date.now(),runtimeVersion:this.checksumRuntimeVersion,request:kc.testRun.request,platform:kc.testRun.platform,runType:kc.testRun.runType};try{n.commitHash=await this.getCmdOutput("git rev-parse HEAD")}catch(s){console.log("Error getting git hash",s.message)}try{n.branch=await this.getCmdOutput("git rev-parse --abbrev-ref HEAD")}catch(s){console.log("Error getting branch",s.message)}return n.name=await this.getEnvName(),n}async getEnvName(){let n=this.getChecksumArg("title");if(typeof n=="string"&&(n==null?void 0:n.length)>0)return n;try{return await this.getCmdOutput("git log -1 --pretty=%B")}catch(s){console.log("Error getting name",s.message);return}}getVolatileConfigPath(){return(0,b0.join)(this.checksumRoot??"","checksum.config.tmp.ts")}deleteVolatileConfig(){let n=this.getVolatileConfigPath();(0,Ug.existsSync)(n)&&(0,Ug.rmSync)(n)}loadChecksumConfig(n={}){this.config={...this.readChecksumConfigFile(n)||{},...this.volatileChecksumConfig||{}},this.debugMode&&(this.config.options.printLogs=!0),this.traceChecksumConfig()}readChecksumConfigFile(n={}){let s=(0,b0.join)(this.checksumRoot,"checksum.config.ts");if(!(0,Ug.existsSync)(s))return n.muteChecksumConfigMissingError||console.warn(`No checksum.config.ts found at ${s}. Continuing with default configuration.`),{};try{return require(s).default}catch(c){console.log(`Failed to load checksum.config.ts at ${s}:
|
|
752
|
-
${c.message||c}`),process.exit(1)}}traceChecksumConfig(){if(!this.runtimeAnalytics)return;let n=Object.keys(this.config).reduce((s,c)=>({...s,[c]:typeof this.config[c]=="string"?this.config[c]:JSON.stringify(this.config[c])}),{});this.runtimeAnalytics.checksumConfig(this.config)}processConfigArg(){this.deleteVolatileConfig();let n=this.getChecksumArg("config");if(n)try{this.volatileChecksumConfig=JSON.parse(n)}catch(s){this.volatileChecksumConfig=void 0,console.log("Error parsing cksm config",s.message)}}getNonChecksumArgs(){return this.args.filter(n=>!this.isChecksumArg(n))}getArg(n){var u;let s=this.args.find(_=>_.startsWith(`${n}`)),c=(u=s==null?void 0:s.split("=")[1])==null?void 0:u.trim();return c||!!s}getChecksumArg(n){return this.getArg(`--cksm-${n}`)}getChecksumRootOrigin(){return(0,b0.join)(this.projectRootDirectory,"node_modules","@checksum-ai","runtime","checksum-root")}locateChecksumLibs(){try{this.findProjectRoot(),this.findChecksumRoot()}catch(n){console.log(n.message),process.exit(1)}}findProjectRoot(){let n=process.cwd(),s=n;for(let c=0;c<10;c++){if((0,Ug.existsSync)((0,b0.join)(s,".git"))){if(this.absoluteProjectRootDirectory)break;this.absoluteProjectRootDirectory=s,Oc("Project root directory found at (git root)",this.absoluteProjectRootDirectory)}if((0,b0.parse)(s).root===s)break;s=(0,b0.join)(s,"..")}n=process.cwd();for(let c=0;c<6;c++){let u=(0,b0.join)(n,"node_modules","@checksum-ai","runtime");if((0,Ug.existsSync)(u)){this.projectRootDirectory=n,this.absoluteProjectRootDirectory||(this.absoluteProjectRootDirectory=n),Oc("Project root directory found at (node_modules fallback)",this.projectRootDirectory);return}if((0,b0.parse)(n).root===n)break;n=(0,b0.join)(n,"..")}if(this.absoluteProjectRootDirectory){this.projectRootDirectory=this.absoluteProjectRootDirectory;return}throw new Error(process.env.CHECKSUM_RUNTIME_DEV_MODE?"Could not resolve checksum root origins, tried to look in .git and node modules. please build the runtime":"Could not resolve checksum root origins")}setChecksumRoot(n){this.checksumRoot=n,Oc("Checksum root directory found at",this.checksumRoot),this.createInternalFilesDir()}findChecksumRoot(){if(kc.checksumRootFolderBypass){let c=(0,b0.isAbsolute)(kc.checksumRootFolderBypass)?kc.checksumRootFolderBypass:(0,b0.join)(this.projectRootDirectory,kc.checksumRootFolderBypass);this.setChecksumRoot(c);return}let n=t(c=>{try{return(0,Ug.readdirSync)(c).includes("checksum.config.ts")}catch{return!1}},"containsChecksumConfig"),s=[this.projectRootDirectory];for(;s.length;){let c=s.pop(),u=(0,Ug.readdirSync)(c,{withFileTypes:!0});for(let _ of u){let g=(0,b0.join)(c,_.name),b=g;if(_.isSymbolicLink())try{let w=(0,Ug.readlinkSync)(g);if(!(0,Ug.lstatSync)(w).isDirectory())continue;b=w}catch{continue}else if(!_.isDirectory())continue;if(_.name===Bxe){if(n(b)){this.setChecksumRoot(b);return}}else s.push(g)}}throw new Error("Could not find checksum root folder. Run `npx checksumai init` to create one.")}log(n,s=!1){s?this.printError(n):console.log(n)}runtimeTrace(n,s){this.runtimeAnalytics.eventWithMessage(s,n)}isChecksumArg(n){return n.startsWith("--cksm-")}};t(Kdt,"CLICommandBase");var mv=Kdt;var Ume="--cksm-auto-heal",O5={enable:`${Ume}`,createPr:`${Ume}-create-pr`,repoName:`${Ume}-repo-name`,prNumber:`${Ume}-pr-number`,branch:`${Ume}-branch`};function Xor(i){if(!fCn(i.args,O5.enable))return{kind:"disabled"};let n=jme(i.args,O5.createPr),s=jme(i.args,O5.repoName),c=jme(i.args,O5.prNumber),u=jme(i.args,O5.branch),_=pCn(n,!0);if(_===void 0)return{kind:"error",message:`${O5.createPr}: expected "true" or "false" (or omit a value), got "${n}"`};let g;if(c!==void 0&&c!==""){let J=Number(c);if(!Number.isInteger(J)||J<=0)return{kind:"error",message:`${O5.prNumber}: expected a positive integer, got "${c}"`};g=J}let b=uCn(i.env),w=typeof s=="string"&&s!==""?s:b.repoName,k=typeof u=="string"&&u!==""?u:b.branch,O=g??b.prNumber;if(_&&!w)return{kind:"error",message:`${O5.repoName} is required when auto-create-pr is enabled \u2014 pass --cksm-auto-heal-repo-name=owner/repo or set GITHUB_REPOSITORY / CI_PROJECT_PATH`};if(O!==void 0&&!w)return{kind:"error",message:`${O5.repoName} is required when ${O5.prNumber} is set`};let G={autoCreatePR:_};return k&&(G.branch=k),O!==void 0&&(G.prNumber=O),w&&(G.repoName=w),{kind:"enabled",options:G}}t(Xor,"resolveAutoHealOptions");function uCn(i){var n;if(i.GITHUB_ACTIONS==="true"){let s=i.GITHUB_REPOSITORY||void 0,c=i.GITHUB_HEAD_REF||i.GITHUB_REF_NAME||void 0,u;if(i.GITHUB_EVENT_NAME==="pull_request"){let _=(n=i.GITHUB_REF)==null?void 0:n.match(/^refs\/pull\/(\d+)\//);if(_){let g=Number(_[1]);Number.isInteger(g)&&g>0&&(u=g)}}return{repoName:s,branch:c,prNumber:u}}if(i.GITLAB_CI==="true"){let s=i.CI_PROJECT_PATH||void 0,c=i.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME||i.CI_COMMIT_REF_NAME||void 0,u;if(i.CI_MERGE_REQUEST_IID){let _=Number(i.CI_MERGE_REQUEST_IID);Number.isInteger(_)&&_>0&&(u=_)}return{repoName:s,branch:c,prNumber:u}}return{}}t(uCn,"detectCiContext");function jme(i,n){for(let s of i){if(s===n)return"";if(s.startsWith(`${n}=`))return s.slice(n.length+1).trim()}}t(jme,"findFlagValue");function fCn(i,n){return jme(i,n)!==void 0}t(fCn,"findFlag");function pCn(i,n){if(i===void 0)return n;if(i===""||i==="true")return!0;if(i==="false")return!1}t(pCn,"parseBoolFlag");var Xdt=require("crypto");function ecr(i,n){return{testSuiteRunId:n,appName:"generic-app",commitHash:"generic-commit-hash",envName:"generic-env-name",branch:"generic-branch",checksumRuntimeVersion:i}}t(ecr,"createDefaultTestSuiteRunInfo");function tcr(i,n,s={}){let c=["mock-app","mock-app-2","mock-app-3"];return{...{appName:c[Math.floor(Math.random()*c.length)],commitHash:(0,Xdt.randomUUID)(),envName:"test-environment",branch:(0,Xdt.randomUUID)(),testSuiteRunId:n,checksumRuntimeVersion:i},...s}}t(tcr,"createMockTestSuiteRunInfo");var Qte=require("fs"),Ute=require("path");var e5e=require("fs"),$1=Pa(kKe());var dCn=/^[0-9a-zA-Z]+$/,_Cn=5;function rcr(i){return i?i.length===_Cn&&dCn.test(i):!1}t(rcr,"isValidChecksumTestId");var n_t=class n_t{extract(n,s){if(!n)return;let c=n.split("/"),u=c[c.length-1];if(!u.endsWith(".checksum.spec.ts"))return;let g=u.replace(".checksum.spec.ts","").split(" - ").pop();return rcr(g)?g:void 0}};t(n_t,"FileNameExtractionStrategy");var e_t=n_t,i_t=class i_t{extract(n,s){if(!(!n||!(0,e5e.existsSync)(n)))try{let c=(0,e5e.readFileSync)(n,"utf-8"),u=$1.createSourceFile(n,c,$1.ScriptTarget.Latest,!0),_=new Map,g=t(b=>{if($1.isCallExpression(b)){let w=b;if(this.isTestCall(w.expression)){let k=w.arguments[0];if(k&&$1.isCallExpression(k)&&$1.isIdentifier(k.expression)&&k.expression.text==="defineChecksumTest"){let O=this.extractDefineChecksumTestArgs(k);O&&rcr(O.checksumTestId)&&_.set(O.testTitle,O.checksumTestId)}}}$1.forEachChild(b,g)},"visit");return g(u),s&&_.has(s)?_.get(s):_.size>0?Array.from(_.values())[0]:void 0}catch{return}}isTestCall(n){return $1.isIdentifier(n)&&n.text==="test"?!0:$1.isPropertyAccessExpression(n)?$1.isIdentifier(n.expression)&&n.expression.text==="test":!1}extractDefineChecksumTestArgs(n){let s=n.arguments;if(s.length<2)return;let c=s[0],u=s[1],_=this.extractStringLiteral(c),g=this.extractStringLiteral(u);if(_&&g)return{testTitle:_,checksumTestId:g}}extractStringLiteral(n){if($1.isStringLiteral(n)||$1.isNoSubstitutionTemplateLiteral(n))return n.text}};t(i_t,"FileContentExtractionStrategy");var t_t=i_t,s_t=class s_t{constructor(n){this.strategies=n||[new t_t,new e_t]}extract(n,s){for(let c of this.strategies){let u=c.extract(n,s);if(u)return u}}};t(s_t,"ChecksumTestIdExtractor");var r_t=s_t;var ncr=new r_t;var a_t=t(i=>isNaN(i)?0:i,"getStat"),mCn=t((i,n,s)=>{if(!i)return;let c=s||n,u;if((0,Ute.isAbsolute)(i))u=i;else{let g=(0,Ute.dirname)(n);u=(0,Ute.resolve)(g,i)}return u.replace(c+"/","")},"toRelativeFilePath"),icr=t(i=>{let n=[];if(i.specs&&n.push(...i.specs),i.suites)for(let s of i.suites)n.push(...icr(s));return n},"extractSpecsFromSuite"),hCn=t(i=>{let n=[];for(let s of i.suites)n.push(...icr(s));return n},"extractFlatSpecsMap"),gCn=t((i,n,s,c,u)=>{let{expected:_,unexpected:g,flaky:b}=i.stats,w=[];hCn(i).forEach(G=>{let J=yCn(G.id,n);J&&(G.recovered=!0);let ae=G.tests[0],ce=G.tags.includes("bug")||ae.annotations.some(vt=>vt.type==="bug"),be=ae.annotations&&ae.annotations.length>0?ae.annotations.map(vt=>({type:vt.type,description:vt.description||""})):[],yt=G.tags||[],_t=s[G.id]||ncr.extract(G.file,G.title);w.push({checksumTestId:_t,playwrightTestId:G.id,success:!!G.ok,recovered:J,bug:ce,annotations:be,tags:yt,outcome:ae.status,filePath:G.file,relativeFilePath:c?mCn(G.file,c,u):G.file})});let O={passed:a_t(b)+a_t(_),failed:a_t(g),bug:w.filter(G=>G.bug).length,recovered:w.filter(G=>G.recovered).length,tests:w};return w.forEach(G=>{G.bug&&(G.outcome==="expected"||G.outcome==="flaky"?O.passed--:G.outcome==="unexpected"&&O.failed--)}),O},"modifyStatsPerTestFile"),yCn=t((i,n)=>Object.values(n[i]||{}).some(s=>{var c,u;return(u=(c=s.autoRecoveryMetadata)==null?void 0:c.data)==null?void 0:u.autoRecovered}),"isAutoRecovered"),scr=t((i,n,s,c,u)=>{(0,Qte.cpSync)(i,i+".backup");try{let _=JSON.parse((0,Qte.readFileSync)(i,"utf-8")),g=gCn(_,n,s,c,u);return _.checksumMetadata=g,(0,Qte.writeFileSync)(i,JSON.stringify(_,null,2)),_}catch(_){return console.log("Error processing JSON report",_.message),null}},"processJSONReport");var c_t=class c_t extends mv{constructor(s,c=!1){super(s,c);this.MAX_COMPLETION_WAIT=2*3600*1e3;this.didFail=!1;this.isolationMode=!1;this.trmMessagesBuffer="";this.completeIndicators={upload:!1,tests:!1,report:!1};this.uploadProgress=0;this.playwrightIdToReportMetadata={};this.pwTestIdToChecksumTestId={};this.logFilter={"upload-progress":{logEveryMS:1e3*30,lastLogTimestamp:0,logOnValueChange:!0,value:""}}}get replMode(){return this.getChecksumArg("repl")??!1}async run(){if(this.loadTracer(),this.validateAuthExists(),this.buildVolatileChecksumConfig(),!await this.getSession())return;this.testSuiteAnalytics=Qme.getInstance(this.testSuiteRunInfo,this.isolationMode),this.testSuiteAnalytics.genericEvent(`Project root found at ${this.projectRootDirectory}`,{message:`Checksum root found at ${this.checksumRoot}`});let s={CHECKSUM_ROOT_FOLDER:this.checksumRoot,CHECKSUM_TEST_SUITE_INFO:JSON.stringify(this.testSuiteRunInfo)},c;try{c=await Zd(this.startTestRunMonitor(this.testSuiteRunId),1e4,"test run monitor timeout"),s.CHECKSUM_UPLOAD_AGENT_PORT=c}catch{let w="Error starting test run monitor. Test results will not be available on checksum.";this.testSuiteAnalytics.testRunMonitorError({error:w}),this.log(w)}s.PW_TEST_HTML_REPORT_OPEN="never",this.isolationMode&&(s.CHECKSUM_ISOLATED_MODE="true"),this.testSuiteRunId&&(s.CHECKSUM_TEST_SUITE_ID=this.testSuiteRunId),this.replMode&&(s.CHECKSUM_REPL="true"),this.getChecksumArg("tm")&&(s.CHECKSUM_SHOW_TIME_MACHINE="true");let u=this.getNonChecksumArgs(),_=this.getChecksumArg("rerun-failed");if(_)try{let b=await this.fetchFailedTestPaths(typeof _=="string"?_:void 0);if(b.length===0){console.log("No non-passing tests found \u2014 nothing to rerun."),await this.shutdown(0);return}console.log(`Re-running ${b.length} non-passing test file(s)`),u.unshift(...b)}catch(b){this.printError(`Failed to fetch failed tests: ${b.message}`),await this.shutdown(1);return}this.replMode&&u.push("--debug");let g=`npx playwright test --config "${this.getPlaywrightConfigFilePath()}" ${u.map(b=>`"${b}"`).join(" ")}`;await this.patchPlaywright();try{console.log(`Tests running with @checksum-ai/runtime version ${this.checksumRuntimeVersion}`),this.replMode?(console.log("Running in REPL mode"),this.execCmd(g,s),await this.execCmd(`node ${__dirname}/repl.js`,s)):(this.testSuiteAnalytics.testSuiteStart(),await this.execCmd(g,s)),console.log(`Tests execution finished. Checksum is finalizing${this.isolationMode?"":" and uploading"} the report, please wait...`)}catch{this.didFail=!0,console.log("Error during test execution: Failed passing test"),this.testSuiteAnalytics.testsFailed()}finally{this.isolationMode||console.log("Waiting for test files to upload to Checksum..."),this.sendReportUploadRequest(),await this.patchPlaywright(!0),this.completeIndicators.tests=!0,await this.handleCompleteMessage()}}async uploadReport(){var u;this.loadTracer(),this.validateAuthExists(),this.buildVolatileChecksumConfig(),this.config.options.hostReports=!0,this.tryLoadPlaywrightConfigFromFile(),this.loadPlaywrightConfigOptions();let s=this.resolveUserReportPathArg();s&&this.playwrightConfigOptions&&(this.playwrightConfigOptions.reportPath=s);let c=(u=this.playwrightConfigOptions)==null?void 0:u.reportPath;if(!c||!(0,hv.existsSync)(c)){this.printError(`Could not find report at ${c??"(unresolved)"}. Pass an explicit path: npx checksumai upload-report <path>`),await this.shutdown(1);return}if(await this.getSession()){this.testSuiteAnalytics=Qme.getInstance(this.testSuiteRunInfo,this.isolationMode);try{await Zd(this.startTestRunMonitor(this.testSuiteRunId),1e4,"test run monitor timeout")}catch(_){this.printError(`Failed to start test run monitor: ${_.message}. Report will not be uploaded.`),await this.shutdown(1);return}console.log(`Uploading report from ${c} to Checksum...`),this.sendReportUploadRequest(),this.completeIndicators.tests=!0,await this.handleCompleteMessage()}}tryLoadPlaywrightConfigFromFile(){if(!this.playwrightConfig)try{let s=this.getPlaywrightConfigFilePath();if((0,hv.existsSync)(s)){let c=require(s),u=(c==null?void 0:c.default)??c;u&&(this.playwrightConfig=u)}}catch(s){Oc("Could not load playwright config from file, falling back to defaults: "+s.message)}}resolveUserReportPathArg(){let s=this.args.find(u=>!u.startsWith("-")&&!u.includes("="));if(!s)return;let c=(0,q1.isAbsolute)(s)?s:(0,q1.resolve)(process.cwd(),s);if(!(0,hv.existsSync)(c))return c;try{if((0,hv.statSync)(c).isDirectory())return(0,q1.join)(c,"index.html")}catch{}return c}loadPlaywrightConfigOptions(){var k,O;let s=t(G=>(Oc("Using report folder",G),(0,q1.join)(G,"index.html")),"makeFilePath"),c=t(G=>(Oc("Using JSON report folder",G),(0,q1.join)(G,"report.json")),"makeJsonFilePath"),u,_,g=!1,b="never";process.env.PLAYWRIGHT_HTML_OUTPUT_DIR&&(u=s(process.env.PLAYWRIGHT_HTML_OUTPUT_DIR));let w=(k=this.playwrightConfig)==null?void 0:k.reporter;if(w instanceof Array){let G=w.filter(ce=>ce instanceof Array&&ce[0]==="html"),J=w.filter(ce=>ce instanceof Array&&ce[0]==="json");if(!process.env.PLAYWRIGHT_HTML_OUTPUT_DIR){let ce=G.find(be=>{var yt;return(yt=be[1])==null?void 0:yt.outputFolder});ce&&(u=s((0,q1.join)(this.checksumRoot,ce[1].outputFolder)))}let ae=J.find(ce=>{var be;return(be=ce[1])==null?void 0:be.outputFile});ae&&(g=!0,_=(0,q1.join)(this.checksumRoot,ae[1].outputFile));for(let ce of G){let be=(O=ce[1])==null?void 0:O.open;if(be==="always"||be==="on-failure"){b=be;break}}}u||(kc.isRepoMode?u=s((0,q1.join)(kc.runtimeRoot,"playwright-report")):u=s((0,q1.join)(this.projectRootDirectory,"playwright-report"))),_||(kc.isRepoMode?_=c((0,q1.join)(kc.runtimeRoot,"playwright-report")):_=c((0,q1.join)(this.projectRootDirectory,"playwright-report"))),this.playwrightConfigOptions={openReportCriteria:b,reportPath:u,jsonReportPath:_,hasJsonReporter:g}}sendReportUploadRequest(){let s=t(O=>{this.testSuiteAnalytics.testSuiteError({error:O}),this.log(O),this.completeIndicators.report=!0,this.testRunMonitorProcess.stdin.write("cli:report=false")},"makeError");if(!this.playwrightConfigOptions)return s("No playwright config options");let{reportPath:c,openReportCriteria:u,jsonReportPath:_,hasJsonReporter:g}=this.playwrightConfigOptions;if(g&&((0,hv.existsSync)(_)?scr(_,this.playwrightIdToReportMetadata,this.pwTestIdToChecksumTestId,this.checksumRoot,this.absoluteProjectRootDirectory):console.warn(`Could not find JSON report file at ${_}`)),!(0,hv.existsSync)(c))return s(`Could not find report file at ${c}`);Oc("Sending report upload request",c);let b=this.saveMetadataToFile((0,q1.dirname)(c)),w={reportPath:c,pathToChecksumMetadata:b,openReportCriteria:u,didFail:this.didFail,checksumRoot:this.checksumRoot,projectRoot:this.absoluteProjectRootDirectory},k=Buffer.from(JSON.stringify(w)).toString("base64");this.testRunMonitorProcess.stdin.write(`cli:report=${k}`)}saveMetadataToFile(s){try{let c=(0,q1.join)(s,`checksum-metadata-${(0,o_t.randomUUID)()}.json`);return(0,hv.writeFileSync)(c,JSON.stringify(this.playwrightIdToReportMetadata,null,2)),c}catch(c){let u=`Error saving metadata to file - ${c.message}`;this.testSuiteAnalytics.testSuiteError({error:u}),this.log(u);return}}startTestMonitorProcess(s){let u=[JSON.stringify({sessionId:s,apiURL:this.config.apiURL,apiKey:this.config.apiKey}),...this.isolationMode?["isolated"]:[]],_={command:"node",args:[(0,q1.join)(__dirname,"test-run-monitor.js"),...u]};return kc.isRepoMode&&(_.command="yarn",_.args=["test-run-monitor",...u]),acr.spawn(_.command,_.args)}startTestRunMonitor(s){return new Promise(c=>{console.log("Starting test run monitor"),this.testRunMonitorProcess=this.startTestMonitorProcess(s),this.testRunMonitorProcess.stdout.on("data",u=>{let _=this.parseTRMMessages(u.toString().trim());if(_.length)for(let g of _){if(g.startsWith("trace")){this.logTRMMessage(g),this.handleTestRunMonitorTrace(g.substring("trace".length+1));continue}if(!g.startsWith("monitor")){this.logTRMMessage(g);continue}let b=t(()=>{let G=g.substring("monitor".length+1),J=G.indexOf("=");if(J===-1)return{key:G,value:""};let[ae,ce]=[G.substring(0,J),G.substring(J+1)];return{key:ae,value:ce}},"getKeyAndValue"),{key:w,value:k}=b();if(!Object.values(h$e).includes(w)){this.logTRMMessage(`Received unknown monitor message: "${g}"`);continue}let O=w;this.logInterProcessEventMessage(O,k),w==="port"?c(k):this.handleTestRunMonitorMessage(O,k)}}),this.testRunMonitorProcess.on("exit",(u,_)=>{let g=`test run monitor process exited with code ${u} and signal ${_}`;this.testSuiteAnalytics.testRunMonitorError({error:g}),this.log(g)}),this.testRunMonitorProcess.stderr.on("data",u=>{let _=`TRM Error: ${u.toString().substring(0,300)}`;this.testSuiteAnalytics.testRunMonitorError({error:_}),this.log(_)}),this.testRunMonitorProcess.on("error",u=>{let _=`Error starting test run monitor: ${u.message}`;this.testSuiteAnalytics.testRunMonitorError({error:_}),this.log(_)})})}logInterProcessEventMessage(s,c){t(()=>{if(!this.logFilter[s])return!0;if(this.logFilter[s].logOnValueChange&&this.logFilter[s].value!==c)return this.logFilter[s].value=c,!0;if(this.logFilter[s].logEveryMS){let _=Date.now();if(_-this.logFilter[s].lastLogTimestamp>=this.logFilter[s].logEveryMS)return this.logFilter[s].lastLogTimestamp=_,!0}return!1},"shouldLog")()&&this.logTRMMessage(`Received monitor message: "${s}${c?`=${c}`:""}"`)}parseTRMMessages(s){let c=this.trmMessagesBuffer+s,u,_,g=t(()=>{u=c.indexOf("{trm}"),_=c.indexOf("{/trm}")},"findIndexes");g();let b=[];for(;u!==-1&&_!==-1&&_>u;){if(b.push(c.substring(u+"{trm}".length,_)),c=c.slice(_+"{/trm}".length).trim(),c.length&&!c.startsWith("{trm}"))return console.warn("Buffered data does not start with start delimiter",c),this.trmMessagesBuffer="",b;this.trmMessagesBuffer=c,g()}return b}setChecksumTestMetadata(s){let{retry:c,type:u,pwTestId:_,checksumTestId:g}=s;this.playwrightIdToReportMetadata[_]||(this.playwrightIdToReportMetadata[_]={}),this.playwrightIdToReportMetadata[_][c]||(this.playwrightIdToReportMetadata[_][c]={}),this.playwrightIdToReportMetadata[_][c][u]=s,this.pwTestIdToChecksumTestId[_]=g}async handleTestRunMonitorTrace(s){let c=s.indexOf("="),[u,_]=c>-1?[s.substring(0,c),s.substring(c+1)]:[s,""];if(!Object.values(Zke).includes(u)){Oc("Unknown trace event",u);return}try{let g=_?JSON.parse(_):{};switch(u){case"Upload Start":this.testSuiteAnalytics.uploadStart({filename:g.filename});break;case"Upload Complete":this.testSuiteAnalytics.uploadComplete({filename:g.filename});break;case"Upload Failed":this.testSuiteAnalytics.uploadFailed({filename:g.filename,error:g.error});break;case"Trace Processing Error":this.testSuiteAnalytics.traceProcessingError({error:g.error});break}}catch(g){Oc(`Error parsing trace payload, ${g.message}`)}}async handleTestRunMonitorMessage(s,c){var u;switch(s){case"uploads-complete-with-errors":console.log("Error uploading test files to Checksum");try{let g=JSON.parse(c);this.testSuiteAnalytics.uploadsCompleteWithErrors(g)}catch{}this.sendProcessingError().then(()=>{this.completeIndicators.upload=!0});break;case"checksumTestMetadata":try{let g=JSON.parse(c);this.setChecksumTestMetadata(g)}catch(g){let b=`Error parsing test metadata event - ${g.message}`;this.testSuiteAnalytics.testSuiteError({error:b}),this.log(b)}break;case"uploads-complete":this.isolationMode||console.log("Test files uploaded successfully"),this.testSuiteAnalytics.allUploadsComplete(),this.sendUploadsComplete().then(()=>{this.completeIndicators.upload=!0});break;case"report-complete":{if(this.isolationMode){this.completeIndicators.report=!0;break}let g=c.slice(0,c.indexOf(":"))==="true",b=c.slice(c.indexOf(":")+1),w="{}";try{if(b&&(0,hv.existsSync)(b)){w=(0,hv.readFileSync)(b,"utf-8");try{(0,hv.unlinkSync)(b)}catch{console.warn(`Failed to delete temporary stats file: ${b}`)}}else console.warn(`Stats file not found at: ${b}`)}catch(O){console.error(`Error reading stats file: ${O.message}`)}let k=this.handleStats(w);if(this.testSuiteAnalytics.testSuiteEnd({passed:k.passed,failed:k.failed,bug:k.bug,total:((u=k.tests)==null?void 0:u.length)||0}),await this.sendTestRunEnd(k),this.completeIndicators.report=!0,g){let O=`${this.checksumAppUrl}/#/test-runs/${this.testSuiteRunId}`,G=`Checksum report URL: ${O}`,J="*".repeat(G.length);console.log(`${J}
|
|
752
|
+
${c.message||c}`),process.exit(1)}}traceChecksumConfig(){if(!this.runtimeAnalytics)return;let n=Object.keys(this.config).reduce((s,c)=>({...s,[c]:typeof this.config[c]=="string"?this.config[c]:JSON.stringify(this.config[c])}),{});this.runtimeAnalytics.checksumConfig(this.config)}processConfigArg(){this.deleteVolatileConfig();let n=this.getChecksumArg("config");if(n)try{this.volatileChecksumConfig=JSON.parse(n)}catch(s){this.volatileChecksumConfig=void 0,console.log("Error parsing cksm config",s.message)}}getNonChecksumArgs(){return this.args.filter(n=>!this.isChecksumArg(n))}getArg(n){var u;let s=this.args.find(_=>_.startsWith(`${n}`)),c=(u=s==null?void 0:s.split("=")[1])==null?void 0:u.trim();return c||!!s}getChecksumArg(n){return this.getArg(`--cksm-${n}`)}getChecksumRootOrigin(){return(0,b0.join)(this.projectRootDirectory,"node_modules","@checksum-ai","runtime","checksum-root")}locateChecksumLibs(){try{this.findProjectRoot(),this.findChecksumRoot()}catch(n){console.log(n.message),process.exit(1)}}findProjectRoot(){let n=process.cwd(),s=n;for(let c=0;c<10;c++){if((0,Ug.existsSync)((0,b0.join)(s,".git"))){if(this.absoluteProjectRootDirectory)break;this.absoluteProjectRootDirectory=s,Oc("Project root directory found at (git root)",this.absoluteProjectRootDirectory)}if((0,b0.parse)(s).root===s)break;s=(0,b0.join)(s,"..")}n=process.cwd();for(let c=0;c<6;c++){let u=(0,b0.join)(n,"node_modules","@checksum-ai","runtime");if((0,Ug.existsSync)(u)){this.projectRootDirectory=n,this.absoluteProjectRootDirectory||(this.absoluteProjectRootDirectory=n),Oc("Project root directory found at (node_modules fallback)",this.projectRootDirectory);return}if((0,b0.parse)(n).root===n)break;n=(0,b0.join)(n,"..")}if(this.absoluteProjectRootDirectory){this.projectRootDirectory=this.absoluteProjectRootDirectory;return}throw new Error(process.env.CHECKSUM_RUNTIME_DEV_MODE?"Could not resolve checksum root origins, tried to look in .git and node modules. please build the runtime":"Could not resolve checksum root origins")}setChecksumRoot(n){this.checksumRoot=n,Oc("Checksum root directory found at",this.checksumRoot),this.createInternalFilesDir()}findChecksumRoot(){if(kc.checksumRootFolderBypass){let c=(0,b0.isAbsolute)(kc.checksumRootFolderBypass)?kc.checksumRootFolderBypass:(0,b0.join)(this.projectRootDirectory,kc.checksumRootFolderBypass);this.setChecksumRoot(c);return}let n=t(c=>{try{return(0,Ug.readdirSync)(c).includes("checksum.config.ts")}catch{return!1}},"containsChecksumConfig"),s=[this.projectRootDirectory];for(;s.length;){let c=s.pop(),u=(0,Ug.readdirSync)(c,{withFileTypes:!0});for(let _ of u){let g=(0,b0.join)(c,_.name),b=g;if(_.isSymbolicLink())try{let w=(0,Ug.readlinkSync)(g);if(!(0,Ug.lstatSync)(w).isDirectory())continue;b=w}catch{continue}else if(!_.isDirectory())continue;if(_.name===Bxe){if(n(b)){this.setChecksumRoot(b);return}}else s.push(g)}}throw new Error("Could not find checksum root folder. Run `npx checksumai init` to create one.")}log(n,s=!1){s?this.printError(n):console.log(n)}runtimeTrace(n,s){this.runtimeAnalytics.eventWithMessage(s,n)}isChecksumArg(n){return n.startsWith("--cksm-")}};t(Kdt,"CLICommandBase");var mv=Kdt;var Ume="--cksm-auto-heal",O5={enable:`${Ume}`,createPr:`${Ume}-create-pr`,repoName:`${Ume}-repo-name`,prNumber:`${Ume}-pr-number`,branch:`${Ume}-branch`};function Xor(i){if(!fCn(i.args,O5.enable))return{kind:"disabled"};let n=jme(i.args,O5.createPr),s=jme(i.args,O5.repoName),c=jme(i.args,O5.prNumber),u=jme(i.args,O5.branch),_=pCn(n,!0);if(_===void 0)return{kind:"error",message:`${O5.createPr}: expected "true" or "false" (or omit a value), got "${n}"`};let g;if(c!==void 0&&c!==""){let J=Number(c);if(!Number.isInteger(J)||J<=0)return{kind:"error",message:`${O5.prNumber}: expected a positive integer, got "${c}"`};g=J}let b=uCn(i.env),w=typeof s=="string"&&s!==""?s:b.repoName,k=typeof u=="string"&&u!==""?u:b.branch,O=g??b.prNumber;if(_&&!w)return{kind:"error",message:`${O5.repoName} is required when auto-create-pr is enabled \u2014 pass --cksm-auto-heal-repo-name=owner/repo or set GITHUB_REPOSITORY / CI_PROJECT_PATH`};if(O!==void 0&&!w)return{kind:"error",message:`${O5.repoName} is required when ${O5.prNumber} is set`};let G={autoCreatePR:_};return k&&(G.branch=k),O!==void 0&&(G.prNumber=O),w&&(G.repoName=w),{kind:"enabled",options:G}}t(Xor,"resolveAutoHealOptions");function uCn(i){var n;if(i.GITHUB_ACTIONS==="true"){let s=i.GITHUB_REPOSITORY||void 0,c=i.GITHUB_HEAD_REF||i.GITHUB_REF_NAME||void 0,u;if(i.GITHUB_EVENT_NAME==="pull_request"){let _=(n=i.GITHUB_REF)==null?void 0:n.match(/^refs\/pull\/(\d+)\//);if(_){let g=Number(_[1]);Number.isInteger(g)&&g>0&&(u=g)}}return{repoName:s,branch:c,prNumber:u}}if(i.GITLAB_CI==="true"){let s=i.CI_PROJECT_PATH||void 0,c=i.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME||i.CI_COMMIT_REF_NAME||void 0,u;if(i.CI_MERGE_REQUEST_IID){let _=Number(i.CI_MERGE_REQUEST_IID);Number.isInteger(_)&&_>0&&(u=_)}return{repoName:s,branch:c,prNumber:u}}return{}}t(uCn,"detectCiContext");function jme(i,n){for(let s of i){if(s===n)return"";if(s.startsWith(`${n}=`))return s.slice(n.length+1).trim()}}t(jme,"findFlagValue");function fCn(i,n){return jme(i,n)!==void 0}t(fCn,"findFlag");function pCn(i,n){if(i===void 0)return n;if(i===""||i==="true")return!0;if(i==="false")return!1}t(pCn,"parseBoolFlag");var Xdt=require("crypto");function ecr(i,n){return{testSuiteRunId:n,appName:"generic-app",commitHash:"generic-commit-hash",envName:"generic-env-name",branch:"generic-branch",checksumRuntimeVersion:i}}t(ecr,"createDefaultTestSuiteRunInfo");function tcr(i,n,s={}){let c=["mock-app","mock-app-2","mock-app-3"];return{...{appName:c[Math.floor(Math.random()*c.length)],commitHash:(0,Xdt.randomUUID)(),envName:"test-environment",branch:(0,Xdt.randomUUID)(),testSuiteRunId:n,checksumRuntimeVersion:i},...s}}t(tcr,"createMockTestSuiteRunInfo");var Qte=require("fs"),Ute=require("path");var e5e=require("fs"),$1=Pa(kKe());var dCn=/^[0-9a-zA-Z]+$/,_Cn=5;function rcr(i){return i?i.length===_Cn&&dCn.test(i):!1}t(rcr,"isValidChecksumTestId");var n_t=class n_t{extract(n,s){if(!n)return;let c=n.split("/"),u=c[c.length-1];if(!u.endsWith(".checksum.spec.ts"))return;let g=u.replace(".checksum.spec.ts","").split(" - ").pop();return rcr(g)?g:void 0}};t(n_t,"FileNameExtractionStrategy");var e_t=n_t,i_t=class i_t{extract(n,s){if(!(!n||!(0,e5e.existsSync)(n)))try{let c=(0,e5e.readFileSync)(n,"utf-8"),u=$1.createSourceFile(n,c,$1.ScriptTarget.Latest,!0),_=new Map,g=t(b=>{if($1.isCallExpression(b)){let w=b;if(this.isTestCall(w.expression)){let k=w.arguments[0];if(k&&$1.isCallExpression(k)&&$1.isIdentifier(k.expression)&&k.expression.text==="defineChecksumTest"){let O=this.extractDefineChecksumTestArgs(k);O&&rcr(O.checksumTestId)&&_.set(O.testTitle,O.checksumTestId)}}}$1.forEachChild(b,g)},"visit");return g(u),s&&_.has(s)?_.get(s):_.size>0?Array.from(_.values())[0]:void 0}catch{return}}isTestCall(n){return $1.isIdentifier(n)&&n.text==="test"?!0:$1.isPropertyAccessExpression(n)?$1.isIdentifier(n.expression)&&n.expression.text==="test":!1}extractDefineChecksumTestArgs(n){let s=n.arguments;if(s.length<2)return;let c=s[0],u=s[1],_=this.extractStringLiteral(c),g=this.extractStringLiteral(u);if(_&&g)return{testTitle:_,checksumTestId:g}}extractStringLiteral(n){if($1.isStringLiteral(n)||$1.isNoSubstitutionTemplateLiteral(n))return n.text}};t(i_t,"FileContentExtractionStrategy");var t_t=i_t,s_t=class s_t{constructor(n){this.strategies=n||[new t_t,new e_t]}extract(n,s){for(let c of this.strategies){let u=c.extract(n,s);if(u)return u}}};t(s_t,"ChecksumTestIdExtractor");var r_t=s_t;var ncr=new r_t;var a_t=t(i=>isNaN(i)?0:i,"getStat"),mCn=t((i,n,s)=>{if(!i)return;let c=s||n,u;if((0,Ute.isAbsolute)(i))u=i;else{let g=(0,Ute.dirname)(n);u=(0,Ute.resolve)(g,i)}return u.replace(c+"/","")},"toRelativeFilePath"),icr=t(i=>{let n=[];if(i.specs&&n.push(...i.specs),i.suites)for(let s of i.suites)n.push(...icr(s));return n},"extractSpecsFromSuite"),hCn=t(i=>{let n=[];for(let s of i.suites)n.push(...icr(s));return n},"extractFlatSpecsMap"),gCn=t((i,n,s,c,u)=>{let{expected:_,unexpected:g,flaky:b}=i.stats,w=[];hCn(i).forEach(G=>{let J=yCn(G.id,n);J&&(G.recovered=!0);let ae=G.tests[0],ce=G.tags.includes("bug")||ae.annotations.some(vt=>vt.type==="bug"),be=ae.annotations&&ae.annotations.length>0?ae.annotations.map(vt=>({type:vt.type,description:vt.description||""})):[],yt=G.tags||[],_t=s[G.id]||ncr.extract(G.file,G.title);w.push({checksumTestId:_t,playwrightTestId:G.id,success:!!G.ok,recovered:J,bug:ce,annotations:be,tags:yt,outcome:ae.status,filePath:G.file,relativeFilePath:c?mCn(G.file,c,u):G.file})});let O={passed:a_t(b)+a_t(_),failed:a_t(g),bug:w.filter(G=>G.bug).length,recovered:w.filter(G=>G.recovered).length,tests:w};return w.forEach(G=>{G.bug&&(G.outcome==="expected"||G.outcome==="flaky"?O.passed--:G.outcome==="unexpected"&&O.failed--)}),O},"modifyStatsPerTestFile"),yCn=t((i,n)=>Object.values(n[i]||{}).some(s=>{var c,u;return(u=(c=s.autoRecoveryMetadata)==null?void 0:c.data)==null?void 0:u.autoRecovered}),"isAutoRecovered"),scr=t((i,n,s,c,u)=>{(0,Qte.cpSync)(i,i+".backup");try{let _=JSON.parse((0,Qte.readFileSync)(i,"utf-8")),g=gCn(_,n,s,c,u);return _.checksumMetadata=g,(0,Qte.writeFileSync)(i,JSON.stringify(_,null,2)),_}catch(_){return console.log("Error processing JSON report",_.message),null}},"processJSONReport");var c_t=class c_t extends mv{constructor(s,c=!1){super(s,c);this.MAX_COMPLETION_WAIT=2*3600*1e3;this.didFail=!1;this.isolationMode=!1;this.reportUploadSucceeded=!1;this.trmMessagesBuffer="";this.completeIndicators={upload:!1,tests:!1,report:!1};this.uploadProgress=0;this.playwrightIdToReportMetadata={};this.pwTestIdToChecksumTestId={};this.logFilter={"upload-progress":{logEveryMS:1e3*30,lastLogTimestamp:0,logOnValueChange:!0,value:""}}}get replMode(){return this.getChecksumArg("repl")??!1}async run(){if(this.loadTracer(),this.validateAuthExists(),this.buildVolatileChecksumConfig(),!await this.getSession())return;this.testSuiteAnalytics=Qme.getInstance(this.testSuiteRunInfo,this.isolationMode),this.testSuiteAnalytics.genericEvent(`Project root found at ${this.projectRootDirectory}`,{message:`Checksum root found at ${this.checksumRoot}`});let s={CHECKSUM_ROOT_FOLDER:this.checksumRoot,CHECKSUM_TEST_SUITE_INFO:JSON.stringify(this.testSuiteRunInfo)},c;try{c=await Zd(this.startTestRunMonitor(this.testSuiteRunId),1e4,"test run monitor timeout"),s.CHECKSUM_UPLOAD_AGENT_PORT=c}catch{let w="Error starting test run monitor. Test results will not be available on checksum.";this.testSuiteAnalytics.testRunMonitorError({error:w}),this.log(w)}s.PW_TEST_HTML_REPORT_OPEN="never",this.isolationMode&&(s.CHECKSUM_ISOLATED_MODE="true"),this.testSuiteRunId&&(s.CHECKSUM_TEST_SUITE_ID=this.testSuiteRunId),this.replMode&&(s.CHECKSUM_REPL="true"),this.getChecksumArg("tm")&&(s.CHECKSUM_SHOW_TIME_MACHINE="true");let u=this.getNonChecksumArgs(),_=this.getChecksumArg("rerun-failed");if(_)try{let b=await this.fetchFailedTestPaths(typeof _=="string"?_:void 0);if(b.length===0){console.log("No non-passing tests found \u2014 nothing to rerun."),await this.shutdown(0);return}console.log(`Re-running ${b.length} non-passing test file(s)`),u.unshift(...b)}catch(b){this.printError(`Failed to fetch failed tests: ${b.message}`),await this.shutdown(1);return}this.replMode&&u.push("--debug");let g=`npx playwright test --config "${this.getPlaywrightConfigFilePath()}" ${u.map(b=>`"${b}"`).join(" ")}`;await this.patchPlaywright();try{console.log(`Tests running with @checksum-ai/runtime version ${this.checksumRuntimeVersion}`),this.replMode?(console.log("Running in REPL mode"),this.execCmd(g,s),await this.execCmd(`node ${__dirname}/repl.js`,s)):(this.testSuiteAnalytics.testSuiteStart(),await this.execCmd(g,s)),console.log(`Tests execution finished. Checksum is finalizing${this.isolationMode?"":" and uploading"} the report, please wait...`)}catch{this.didFail=!0,console.log("Error during test execution: Failed passing test"),this.testSuiteAnalytics.testsFailed()}finally{this.isolationMode||console.log("Waiting for test files to upload to Checksum..."),this.sendReportUploadRequest(),await this.patchPlaywright(!0),this.completeIndicators.tests=!0,await this.handleCompleteMessage()}}async uploadReport(){var u;this.loadTracer(),this.validateAuthExists(),this.buildVolatileChecksumConfig(),this.config.options.hostReports=!0,this.tryLoadPlaywrightConfigFromFile(),this.loadPlaywrightConfigOptions();let s=this.resolveUserReportPathArg();s&&this.playwrightConfigOptions&&(this.playwrightConfigOptions.reportPath=s);let c=(u=this.playwrightConfigOptions)==null?void 0:u.reportPath;if(!c||!(0,hv.existsSync)(c)){this.printError(`Could not find report at ${c??"(unresolved)"}. Pass an explicit path: npx checksumai upload-report <path>`),await this.shutdown(1);return}if(await this.getSession()){this.testSuiteAnalytics=Qme.getInstance(this.testSuiteRunInfo,this.isolationMode);try{await Zd(this.startTestRunMonitor(this.testSuiteRunId),1e4,"test run monitor timeout")}catch(_){this.printError(`Failed to start test run monitor: ${_.message}. Report will not be uploaded.`),await this.shutdown(1);return}console.log(`Uploading report from ${c} to Checksum...`),this.sendReportUploadRequest(),this.completeIndicators.tests=!0,await this.handleCompleteMessage()}}tryLoadPlaywrightConfigFromFile(){if(!this.playwrightConfig)try{let s=this.getPlaywrightConfigFilePath();if((0,hv.existsSync)(s)){let c=require(s),u=(c==null?void 0:c.default)??c;u&&(this.playwrightConfig=u)}}catch(s){Oc("Could not load playwright config from file, falling back to defaults: "+s.message)}}resolveUserReportPathArg(){let s=this.args.find(u=>!u.startsWith("-")&&!u.includes("="));if(!s)return;let c=(0,q1.isAbsolute)(s)?s:(0,q1.resolve)(process.cwd(),s);if(!(0,hv.existsSync)(c))return c;try{if((0,hv.statSync)(c).isDirectory())return(0,q1.join)(c,"index.html")}catch{}return c}loadPlaywrightConfigOptions(){var k,O;let s=t(G=>(Oc("Using report folder",G),(0,q1.join)(G,"index.html")),"makeFilePath"),c=t(G=>(Oc("Using JSON report folder",G),(0,q1.join)(G,"report.json")),"makeJsonFilePath"),u,_,g=!1,b="never";process.env.PLAYWRIGHT_HTML_OUTPUT_DIR&&(u=s(process.env.PLAYWRIGHT_HTML_OUTPUT_DIR));let w=(k=this.playwrightConfig)==null?void 0:k.reporter;if(w instanceof Array){let G=w.filter(ce=>ce instanceof Array&&ce[0]==="html"),J=w.filter(ce=>ce instanceof Array&&ce[0]==="json");if(!process.env.PLAYWRIGHT_HTML_OUTPUT_DIR){let ce=G.find(be=>{var yt;return(yt=be[1])==null?void 0:yt.outputFolder});ce&&(u=s((0,q1.join)(this.checksumRoot,ce[1].outputFolder)))}let ae=J.find(ce=>{var be;return(be=ce[1])==null?void 0:be.outputFile});ae&&(g=!0,_=(0,q1.join)(this.checksumRoot,ae[1].outputFile));for(let ce of G){let be=(O=ce[1])==null?void 0:O.open;if(be==="always"||be==="on-failure"){b=be;break}}}u||(kc.isRepoMode?u=s((0,q1.join)(kc.runtimeRoot,"playwright-report")):u=s((0,q1.join)(this.projectRootDirectory,"playwright-report"))),_||(kc.isRepoMode?_=c((0,q1.join)(kc.runtimeRoot,"playwright-report")):_=c((0,q1.join)(this.projectRootDirectory,"playwright-report"))),this.playwrightConfigOptions={openReportCriteria:b,reportPath:u,jsonReportPath:_,hasJsonReporter:g}}sendReportUploadRequest(){let s=t(O=>{this.testSuiteAnalytics.testSuiteError({error:O}),this.log(O),this.completeIndicators.report=!0,this.testRunMonitorProcess.stdin.write("cli:report=false")},"makeError");if(!this.playwrightConfigOptions)return s("No playwright config options");let{reportPath:c,openReportCriteria:u,jsonReportPath:_,hasJsonReporter:g}=this.playwrightConfigOptions;if(g&&((0,hv.existsSync)(_)?scr(_,this.playwrightIdToReportMetadata,this.pwTestIdToChecksumTestId,this.checksumRoot,this.absoluteProjectRootDirectory):console.warn(`Could not find JSON report file at ${_}`)),!(0,hv.existsSync)(c))return s(`Could not find report file at ${c}`);Oc("Sending report upload request",c);let b=this.saveMetadataToFile((0,q1.dirname)(c)),w={reportPath:c,pathToChecksumMetadata:b,openReportCriteria:u,didFail:this.didFail,checksumRoot:this.checksumRoot,projectRoot:this.absoluteProjectRootDirectory},k=Buffer.from(JSON.stringify(w)).toString("base64");this.testRunMonitorProcess.stdin.write(`cli:report=${k}`)}saveMetadataToFile(s){try{let c=(0,q1.join)(s,`checksum-metadata-${(0,o_t.randomUUID)()}.json`);return(0,hv.writeFileSync)(c,JSON.stringify(this.playwrightIdToReportMetadata,null,2)),c}catch(c){let u=`Error saving metadata to file - ${c.message}`;this.testSuiteAnalytics.testSuiteError({error:u}),this.log(u);return}}startTestMonitorProcess(s){let u=[JSON.stringify({sessionId:s,apiURL:this.config.apiURL,apiKey:this.config.apiKey}),...this.isolationMode?["isolated"]:[]],_={command:"node",args:[(0,q1.join)(__dirname,"test-run-monitor.js"),...u]};return kc.isRepoMode&&(_.command="yarn",_.args=["test-run-monitor",...u]),acr.spawn(_.command,_.args)}startTestRunMonitor(s){return new Promise(c=>{console.log("Starting test run monitor"),this.testRunMonitorProcess=this.startTestMonitorProcess(s),this.testRunMonitorProcess.stdout.on("data",u=>{let _=this.parseTRMMessages(u.toString().trim());if(_.length)for(let g of _){if(g.startsWith("trace")){this.logTRMMessage(g),this.handleTestRunMonitorTrace(g.substring("trace".length+1));continue}if(!g.startsWith("monitor")){this.logTRMMessage(g);continue}let b=t(()=>{let G=g.substring("monitor".length+1),J=G.indexOf("=");if(J===-1)return{key:G,value:""};let[ae,ce]=[G.substring(0,J),G.substring(J+1)];return{key:ae,value:ce}},"getKeyAndValue"),{key:w,value:k}=b();if(!Object.values(h$e).includes(w)){this.logTRMMessage(`Received unknown monitor message: "${g}"`);continue}let O=w;this.logInterProcessEventMessage(O,k),w==="port"?c(k):this.handleTestRunMonitorMessage(O,k)}}),this.testRunMonitorProcess.on("exit",(u,_)=>{let g=`test run monitor process exited with code ${u} and signal ${_}`;this.testSuiteAnalytics.testRunMonitorError({error:g}),this.log(g)}),this.testRunMonitorProcess.stderr.on("data",u=>{let _=`TRM Error: ${u.toString().substring(0,300)}`;this.testSuiteAnalytics.testRunMonitorError({error:_}),this.log(_)}),this.testRunMonitorProcess.on("error",u=>{let _=`Error starting test run monitor: ${u.message}`;this.testSuiteAnalytics.testRunMonitorError({error:_}),this.log(_)})})}logInterProcessEventMessage(s,c){t(()=>{if(!this.logFilter[s])return!0;if(this.logFilter[s].logOnValueChange&&this.logFilter[s].value!==c)return this.logFilter[s].value=c,!0;if(this.logFilter[s].logEveryMS){let _=Date.now();if(_-this.logFilter[s].lastLogTimestamp>=this.logFilter[s].logEveryMS)return this.logFilter[s].lastLogTimestamp=_,!0}return!1},"shouldLog")()&&this.logTRMMessage(`Received monitor message: "${s}${c?`=${c}`:""}"`)}parseTRMMessages(s){let c=this.trmMessagesBuffer+s,u,_,g=t(()=>{u=c.indexOf("{trm}"),_=c.indexOf("{/trm}")},"findIndexes");g();let b=[];for(;u!==-1&&_!==-1&&_>u;){if(b.push(c.substring(u+"{trm}".length,_)),c=c.slice(_+"{/trm}".length).trim(),c.length&&!c.startsWith("{trm}"))return console.warn("Buffered data does not start with start delimiter",c),this.trmMessagesBuffer="",b;this.trmMessagesBuffer=c,g()}return b}setChecksumTestMetadata(s){let{retry:c,type:u,pwTestId:_,checksumTestId:g}=s;this.playwrightIdToReportMetadata[_]||(this.playwrightIdToReportMetadata[_]={}),this.playwrightIdToReportMetadata[_][c]||(this.playwrightIdToReportMetadata[_][c]={}),this.playwrightIdToReportMetadata[_][c][u]=s,this.pwTestIdToChecksumTestId[_]=g}async handleTestRunMonitorTrace(s){let c=s.indexOf("="),[u,_]=c>-1?[s.substring(0,c),s.substring(c+1)]:[s,""];if(!Object.values(Zke).includes(u)){Oc("Unknown trace event",u);return}try{let g=_?JSON.parse(_):{};switch(u){case"Upload Start":this.testSuiteAnalytics.uploadStart({filename:g.filename});break;case"Upload Complete":this.testSuiteAnalytics.uploadComplete({filename:g.filename});break;case"Upload Failed":this.testSuiteAnalytics.uploadFailed({filename:g.filename,error:g.error});break;case"Trace Processing Error":this.testSuiteAnalytics.traceProcessingError({error:g.error});break}}catch(g){Oc(`Error parsing trace payload, ${g.message}`)}}async handleTestRunMonitorMessage(s,c){var u;switch(s){case"uploads-complete-with-errors":console.log("Error uploading test files to Checksum");try{let g=JSON.parse(c);this.testSuiteAnalytics.uploadsCompleteWithErrors(g)}catch{}this.sendProcessingError().then(()=>{this.completeIndicators.upload=!0});break;case"checksumTestMetadata":try{let g=JSON.parse(c);this.setChecksumTestMetadata(g)}catch(g){let b=`Error parsing test metadata event - ${g.message}`;this.testSuiteAnalytics.testSuiteError({error:b}),this.log(b)}break;case"uploads-complete":this.isolationMode||console.log("Test files uploaded successfully"),this.testSuiteAnalytics.allUploadsComplete(),this.sendUploadsComplete().then(()=>{this.completeIndicators.upload=!0});break;case"report-complete":{if(this.isolationMode){this.completeIndicators.report=!0;break}let g=c.slice(0,c.indexOf(":"))==="true",b=c.slice(c.indexOf(":")+1),w="{}";try{if(b&&(0,hv.existsSync)(b)){w=(0,hv.readFileSync)(b,"utf-8");try{(0,hv.unlinkSync)(b)}catch{console.warn(`Failed to delete temporary stats file: ${b}`)}}else console.warn(`Stats file not found at: ${b}`)}catch(O){console.error(`Error reading stats file: ${O.message}`)}let k=this.handleStats(w);if(this.testSuiteAnalytics.testSuiteEnd({passed:k.passed,failed:k.failed,bug:k.bug,total:((u=k.tests)==null?void 0:u.length)||0}),await this.sendTestRunEnd(k),this.completeIndicators.report=!0,this.reportUploadSucceeded=g,g){let O=`${this.checksumAppUrl}/#/test-runs/${this.testSuiteRunId}`,G=`Checksum report URL: ${O}`,J="*".repeat(G.length);console.log(`${J}
|
|
753
753
|
${G}
|
|
754
|
-
${J}`),this.testSuiteAnalytics.reportComplete({stats:k,reportUrl:O})}else this.testSuiteAnalytics.testSuiteError({error:"An error occurred while uploading the test report to Checksum"}),this.testSuiteAnalytics.reportUploadError(),console.log("An error occurred while uploading the test report to Checksum");break}case"upload-progress":{if(this.isolationMode)break;let g=parseInt(c);(g<this.uploadProgress||g>=this.uploadProgress+10||g===100&&this.uploadProgress!==100)&&(this.uploadProgress=g,console.log(`[ Uploads progress: ${this.uploadProgress}% ]`))}break;case"log":console.log(c);break;case"trace":let _=c;if(!Object.values(Zke).includes(_))return;this.testSuiteAnalytics.genericEvent(_);break;case"playwrightConfig":try{this.playwrightConfig||(this.playwrightConfig=JSON.parse(c),this.testSuiteAnalytics.playwrightConfig({config:this.playwrightConfig}),this.loadPlaywrightConfigOptions())}catch(g){let b=`Error parsing playwright config - ${g.message}`;this.testSuiteAnalytics.playwrightConfigError({error:b}),this.log(b)}break;default:this.testSuiteAnalytics.testRunMonitorError({error:`Unhandled test run monitor message: ${s}=${c}`}),this.log(`Unhandled test run monitor message: ${s}=${c}`)}}handleStats(s){let c={};try{s&&s.length>0?(c=JSON.parse(s),c.tests=c.tests.map(u=>{let _=this.playwrightIdToReportMetadata[u.playwrightTestId];if(!_)return u;let g=Object.keys(_??{}),b=!1,w=!1,k=[];g.forEach(G=>{var J;if(_[G]){let ae=_[G].autoRecoverySummaries;(J=ae==null?void 0:ae.data)!=null&&J.length&&k.push(...ae.data)}});let O=g[g.length-1];if(O&&_[O]){let G=_[O].autoRecoveryMetadata;b=(G==null?void 0:G.data.autoRecoveryTriggered)??!1,w=(G==null?void 0:G.data.autoRecovered)??!1}return{...u,recovered:w,autoRecoveryTriggered:b,autoRecoverySummaries:k}})):(this.testSuiteAnalytics.testSuiteError({error:"No stats received from test run monitor"}),Oc("No stats received from test run monitor"))}catch(u){this.testSuiteAnalytics.testSuiteError({error:`Error parsing stats - ${u.message}`}),this.log(`Error parsing stats - ${u.message}`)}return c}async handleCompleteMessage(){let s=Date.now(),c=12;for(;;){if(Date.now()-s>this.MAX_COMPLETION_WAIT){console.log("Warning: Checksum wasn't able to upload all test assets or expected a file the couldn't be found. This might cause issues when viewing the report/trace."),this.testSuiteAnalytics.runtimeTimeout(),this.shutdown(1);return}if(Object.keys(this.completeIndicators).find(u=>!this.completeIndicators[u]))Oc(this.completeIndicators),--c===0&&(this.testSuiteAnalytics.completionStatus({stats:this.completeIndicators}),c=12),await Qh(5e3);else{this.testSuiteAnalytics.runtimeComplete(),this.writeOutputFile(),console.log("Checksum test suite run has completed"),await this.shutdown(this.didFail?1:0);return}}}writeOutputFile(){try{let s=(0,q1.join)(this.getInternalFilesDirPath(),"output.json"),c={testRunId:this.testSuiteRunId
|
|
754
|
+
${J}`),this.testSuiteAnalytics.reportComplete({stats:k,reportUrl:O})}else this.testSuiteAnalytics.testSuiteError({error:"An error occurred while uploading the test report to Checksum"}),this.testSuiteAnalytics.reportUploadError(),console.log("An error occurred while uploading the test report to Checksum");break}case"upload-progress":{if(this.isolationMode)break;let g=parseInt(c);(g<this.uploadProgress||g>=this.uploadProgress+10||g===100&&this.uploadProgress!==100)&&(this.uploadProgress=g,console.log(`[ Uploads progress: ${this.uploadProgress}% ]`))}break;case"log":console.log(c);break;case"trace":let _=c;if(!Object.values(Zke).includes(_))return;this.testSuiteAnalytics.genericEvent(_);break;case"playwrightConfig":try{this.playwrightConfig||(this.playwrightConfig=JSON.parse(c),this.testSuiteAnalytics.playwrightConfig({config:this.playwrightConfig}),this.loadPlaywrightConfigOptions())}catch(g){let b=`Error parsing playwright config - ${g.message}`;this.testSuiteAnalytics.playwrightConfigError({error:b}),this.log(b)}break;default:this.testSuiteAnalytics.testRunMonitorError({error:`Unhandled test run monitor message: ${s}=${c}`}),this.log(`Unhandled test run monitor message: ${s}=${c}`)}}handleStats(s){let c={};try{s&&s.length>0?(c=JSON.parse(s),c.tests=c.tests.map(u=>{let _=this.playwrightIdToReportMetadata[u.playwrightTestId];if(!_)return u;let g=Object.keys(_??{}),b=!1,w=!1,k=[];g.forEach(G=>{var J;if(_[G]){let ae=_[G].autoRecoverySummaries;(J=ae==null?void 0:ae.data)!=null&&J.length&&k.push(...ae.data)}});let O=g[g.length-1];if(O&&_[O]){let G=_[O].autoRecoveryMetadata;b=(G==null?void 0:G.data.autoRecoveryTriggered)??!1,w=(G==null?void 0:G.data.autoRecovered)??!1}return{...u,recovered:w,autoRecoveryTriggered:b,autoRecoverySummaries:k}})):(this.testSuiteAnalytics.testSuiteError({error:"No stats received from test run monitor"}),Oc("No stats received from test run monitor"))}catch(u){this.testSuiteAnalytics.testSuiteError({error:`Error parsing stats - ${u.message}`}),this.log(`Error parsing stats - ${u.message}`)}return c}async handleCompleteMessage(){let s=Date.now(),c=12;for(;;){if(Date.now()-s>this.MAX_COMPLETION_WAIT){console.log("Warning: Checksum wasn't able to upload all test assets or expected a file the couldn't be found. This might cause issues when viewing the report/trace."),this.testSuiteAnalytics.runtimeTimeout(),this.shutdown(1);return}if(Object.keys(this.completeIndicators).find(u=>!this.completeIndicators[u]))Oc(this.completeIndicators),--c===0&&(this.testSuiteAnalytics.completionStatus({stats:this.completeIndicators}),c=12),await Qh(5e3);else{this.testSuiteAnalytics.runtimeComplete(),this.writeOutputFile(),console.log("Checksum test suite run has completed"),await this.shutdown(this.didFail?1:0);return}}}writeOutputFile(){try{let s=(0,q1.join)(this.getInternalFilesDirPath(),"output.json"),c={testRunId:this.testSuiteRunId,...this.reportUploadSucceeded&&{reportUrl:`${this.checksumAppUrl}/#/test-runs/${this.testSuiteRunId}`},failed:this.didFail};(0,hv.writeFileSync)(s,JSON.stringify(c,null,2)),console.log(`Checksum output written to ${s}`)}catch(s){this.log(`Error writing output file: ${s.message}`)}}async fetchWithTimeout(s,c,u=3e4){let _=new AbortController,g=setTimeout(()=>_.abort(),u);try{return await fetch(s,{...c,signal:_.signal})}finally{clearTimeout(g)}}async fetchFailedTestPaths(s){let c=this.config.apiKey,u=this.config.apiURL,_={Accept:"application/json",ChecksumAppCode:c};if(!s){let O=await this.fetchWithTimeout(`${u}/public-api/v1/test-runs/latest`,{headers:_});if(!O.ok){let J=await O.text();throw new Error(`Failed to fetch latest test run: ${O.status} ${J}`)}let G=await O.json();s=G.id,console.log(`Using latest test run: ${s} (branch: ${G.branch})`)}let g=await this.fetchWithTimeout(`${u}/public-api/v1/test-runs/${s}/results`,{headers:_});if(!g.ok){let O=await g.text();throw new Error(`Failed to fetch test run results: ${g.status} ${O}`)}let w=((await g.json()).tests??[]).filter(O=>O.status!=="passed"&&O.status!=="skipped");return[...new Set(w.map(O=>O.testFilePath).filter(Boolean))]}async shutdown(s=0){await this.cleanup(),process.exit(s)}async cleanup(){await super.cleanup(),this.testRunMonitorProcess&&(this.testRunMonitorProcess.stdin.write("cli:shutdown"),this.testRunMonitorProcess.kill())}async getSession(){let s,c;try{if(!this.config.options.hostReports)return this.setIsolatedMode(),!0;if(c=this.config.apiKey,!c||c==="<API key>"){let G="No API key found in checksum config - please set it in the config file - checksum.config.ts";return this.runtimeTrace(G,"Checksum Config Error"),this.log(G,!0),this.shutdown(1),!1}let u=await this.getEnvInfo(),_=Xor({args:this.args,env:process.env});if(_.kind==="error"){let G=`Invalid --cksm-auto-heal* configuration: ${_.message}`;return this.runtimeTrace(G,"Checksum Config Error"),this.log(G,!0),this.shutdown(1),!1}let g=_.kind==="enabled"?_.options:void 0,b=JSON.stringify({...u,isHidden:!!this.config.options.hideReports,...g?{autoHealConfig:g}:{}}),w=await fetch(`${this.config.apiURL}/client-api/test-runs`,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json",ChecksumAppCode:c},body:b}),{uuid:k,appName:O}=await w.json();return this.testSuiteRunId=k,u.commit=u.name,u.name=O,this.testSuiteRunInfo={appName:O,commitHash:u.commitHash,envName:u.name,branch:u.branch,testSuiteRunId:k,checksumRuntimeVersion:this.checksumRuntimeVersion},!0}catch(u){let _=`Error connecting to Checksum, the report will not be hosted - ${u.message}`;return this.runtimeTrace(_,"Test Suite Init Error"),this.log(_),this.setIsolatedMode(),!0}}setIsolatedMode(){this.testSuiteRunId=process.env.CHECKSUM_TEST_SUITE_ID||(0,o_t.randomUUID)(),this.isolationMode=!0,this.completeIndicators.upload=!0,kc.testRun.monitoring.debugAnalytics?this.testSuiteRunInfo=tcr(this.checksumRuntimeVersion,this.testSuiteRunId):this.testSuiteRunInfo=ecr(this.checksumRuntimeVersion,this.testSuiteRunId)}async sendTestRunEnd(s){if(!this.isolationMode)try{let c="{}";try{c=JSON.stringify({...s,endedAt:Date.now()})}catch(u){this.testSuiteAnalytics.testSuiteError({error:`Error stringifying stats ${u.message}`}),this.log(`Error stringifying stats ${u.message}`,!0)}await this.updateTestRun(`${this.config.apiURL}/client-api/test-runs/${this.testSuiteRunId}`,"PATCH",c)}catch(c){return this.testSuiteAnalytics.testSuiteError({error:`Error sending test run end ${c.message}`}),this.log(`Error sending test run end ${c.message}`,!0),null}}async sendUploadsComplete(){if(!this.isolationMode)try{await this.updateTestRun(`${this.config.apiURL}/client-api/test-runs/${this.testSuiteRunId}/uploads-completed`,"PATCH")}catch(s){this.testSuiteAnalytics.testSuiteError({error:`Error sending test run uploads complete - ${s.message}`}),this.log(`Error sending test run uploads complete - ${s.message}`,!0)}}async sendProcessingError(){if(!this.isolationMode)try{await this.updateTestRun(`${this.config.apiURL}/client-api/test-runs/${this.testSuiteRunId}/process-error`,"PATCH")}catch(s){this.testSuiteAnalytics.testSuiteError({error:`Error sending test run processing error - ${s.message}`}),this.log(`Error sending test run processing error - ${s.message}`,!0)}}async updateTestRun(s,c,u=void 0){let _=await fetch(s,{method:c,headers:{Accept:"application/json","Content-Type":"application/json",ChecksumAppCode:this.config.apiKey},body:u});return Oc("Received update test run response from url:",s),await this.logApiResponse(_),_}async logApiResponse(s){try{if(!s.ok){let{status:u,statusText:_}=s,g=await s.text();this.testSuiteAnalytics.testSuiteError({status:u,statusText:_,error:g});return}s.headers.get("Content-Type").includes("application/json")?s.json().then(u=>{Oc("API Response:",u)}):s.text().then(u=>{Oc("API Response:",u)})}catch(c){this.testSuiteAnalytics.testSuiteError({error:`Error logging API response - ${c.message}`})}}logTRMMessage(s){(Array.isArray(s)?s:[s]).forEach(c=>{Oc("[trm]",c)})}};t(c_t,"TestsRunner");var Jme=c_t;var l_t=class l_t extends mv{constructor(n,s=!1){super(n,s)}async run(n){switch(n){default:console.log(`
|
|
755
755
|
Checksum CLI
|
|
756
756
|
Usage: checksum [command] [options]
|
|
757
757
|
|