1886da342Sopenharmony_ci/*
2886da342Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3886da342Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4886da342Sopenharmony_ci * you may not use this file except in compliance with the License.
5886da342Sopenharmony_ci * You may obtain a copy of the License at
6886da342Sopenharmony_ci *
7886da342Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8886da342Sopenharmony_ci *
9886da342Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10886da342Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11886da342Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12886da342Sopenharmony_ci * See the License for the specific language governing permissions and
13886da342Sopenharmony_ci * limitations under the License.
14886da342Sopenharmony_ci */
15886da342Sopenharmony_ci
16886da342Sopenharmony_ciimport SysTestKit from '../kit/SysTestKit';
17886da342Sopenharmony_ciimport { collectCoverageData } from '../coverage/coverageCollect';
18886da342Sopenharmony_ciimport { TAG, PrintTag } from '../../Constant';
19886da342Sopenharmony_ci
20886da342Sopenharmony_ciclass OhReport {
21886da342Sopenharmony_ci  constructor(attr) {
22886da342Sopenharmony_ci    this.delegator = attr.delegator;
23886da342Sopenharmony_ci    this.abilityDelegatorArguments = attr.abilityDelegatorArguments;
24886da342Sopenharmony_ci    this.id = 'report';
25886da342Sopenharmony_ci    this.index = 0;
26886da342Sopenharmony_ci    this.duration = 0;
27886da342Sopenharmony_ci    this.currentThreadName = 'mainThread';
28886da342Sopenharmony_ci  }
29886da342Sopenharmony_ci
30886da342Sopenharmony_ci  init(coreContext) {
31886da342Sopenharmony_ci    this.coreContext = coreContext;
32886da342Sopenharmony_ci    this.suiteService = this.coreContext.getDefaultService('suite');
33886da342Sopenharmony_ci    this.specService = this.coreContext.getDefaultService('spec');
34886da342Sopenharmony_ci    if (SysTestKit.workerPort !== null) {
35886da342Sopenharmony_ci      this.currentThreadName = SysTestKit.workerPort.name;
36886da342Sopenharmony_ci    }
37886da342Sopenharmony_ci  }
38886da342Sopenharmony_ci
39886da342Sopenharmony_ci  taskStart() {
40886da342Sopenharmony_ci  }
41886da342Sopenharmony_ci
42886da342Sopenharmony_ci  async taskDone() {
43886da342Sopenharmony_ci    let summary = this.suiteService.getSummary();
44886da342Sopenharmony_ci    if (this.abilityDelegatorArguments !== null) {
45886da342Sopenharmony_ci      this.taskDoneTime = new Date().getTime();
46886da342Sopenharmony_ci      const configService = this.coreContext.getDefaultService('config');
47886da342Sopenharmony_ci      const suiteService = this.coreContext.getDefaultService('suite');
48886da342Sopenharmony_ci      const specService = this.coreContext.getDefaultService('spec');
49886da342Sopenharmony_ci      if (configService['coverage'] === 'true') {
50886da342Sopenharmony_ci        await collectCoverageData();
51886da342Sopenharmony_ci      }
52886da342Sopenharmony_ci      let message = '\n' + `${PrintTag.OHOS_REPORT_RESULT}: stream=Tests run: ` + summary.total + ', Failure: ' + summary.failure;
53886da342Sopenharmony_ci      message += ', Error: ' + summary.error;
54886da342Sopenharmony_ci      message += ', Pass: ' + summary.pass;
55886da342Sopenharmony_ci      message += ', Ignore: ' + summary.ignore;
56886da342Sopenharmony_ci      if (specService.skipSpecNum > 0) {
57886da342Sopenharmony_ci        message += ', SkipSpec: ' + specService.skipSpecNum;
58886da342Sopenharmony_ci      }
59886da342Sopenharmony_ci      message += '\n' + `${PrintTag.OHOS_REPORT_CODE}: ` + (summary.failure > 0 ? -1 : 0) + '\n';
60886da342Sopenharmony_ci      let isHasError = summary.failure > 0 || summary.error > 0;
61886da342Sopenharmony_ci      let config = this.coreContext.getDefaultService('config');
62886da342Sopenharmony_ci      if (config.isBreakOnError() && isHasError) {
63886da342Sopenharmony_ci        // 未执行全部说明
64886da342Sopenharmony_ci        message += '\n' + `${PrintTag.OHOS_REPORT_RESULT}: breakOnError model, Stopping whole test suite if one specific test case failed or error` + '\n';
65886da342Sopenharmony_ci      }
66886da342Sopenharmony_ci      message += `${PrintTag.OHOS_REPORT_STATUS}: taskconsuming=` + summary.duration + '\n';
67886da342Sopenharmony_ci      console.info(`${message}`);
68886da342Sopenharmony_ci      await SysTestKit.print(message);
69886da342Sopenharmony_ci    }
70886da342Sopenharmony_ci    if (SysTestKit.workerPort === null || SysTestKit.workerPort === undefined) {
71886da342Sopenharmony_ci      // 主线程执行完成 结束任务。
72886da342Sopenharmony_ci      console.info(`${TAG}report print success`);
73886da342Sopenharmony_ci      this.delegator.finishTest('your test finished!!!', 0, () => { });
74886da342Sopenharmony_ci    } else {
75886da342Sopenharmony_ci      // worker线程执行完成将数据发送到主线程中。
76886da342Sopenharmony_ci      let sendData = {
77886da342Sopenharmony_ci        currentThreadName: this.currentThreadName,
78886da342Sopenharmony_ci        summary: summary
79886da342Sopenharmony_ci      };
80886da342Sopenharmony_ci      console.info(`${TAG}, send data to mainThread, ${this.currentThreadName}, ${JSON.stringify(sendData)}`);
81886da342Sopenharmony_ci      SysTestKit.workerPort.postMessage(sendData);
82886da342Sopenharmony_ci    }
83886da342Sopenharmony_ci  }
84886da342Sopenharmony_ci
85886da342Sopenharmony_ci  incorrectFormat() {
86886da342Sopenharmony_ci    if (this.coreContext.getDefaultService('config').filterValid.length !== 0) {
87886da342Sopenharmony_ci      var value = this.coreContext.getDefaultService('config').filterValid;
88886da342Sopenharmony_ci      var message = 'this param ' + value.join(',') + ' is invalid' + '\n';
89886da342Sopenharmony_ci      this.delegator.finishTest(message, 0, () => {
90886da342Sopenharmony_ci      });
91886da342Sopenharmony_ci    }
92886da342Sopenharmony_ci  }
93886da342Sopenharmony_ci
94886da342Sopenharmony_ci  incorrectTestSuiteFormat() {
95886da342Sopenharmony_ci    if (this.coreContext.getDefaultService('config').filterXdescribe.length !== 0) {
96886da342Sopenharmony_ci      let value = this.coreContext.getDefaultService('config').filterXdescribe;
97886da342Sopenharmony_ci      let message = 'xdescribe ' + value.join(',') + ' should not contain it' + '\n';
98886da342Sopenharmony_ci      this.delegator.finishTest(message, 0, () => {
99886da342Sopenharmony_ci      });
100886da342Sopenharmony_ci    }
101886da342Sopenharmony_ci  }
102886da342Sopenharmony_ci  async suiteStart() {
103886da342Sopenharmony_ci    if (this.abilityDelegatorArguments !== null) {
104886da342Sopenharmony_ci      let specArr = [];
105886da342Sopenharmony_ci      this.suiteService.getAllChildSuiteNum(this.suiteService.getCurrentRunningSuite(), specArr);
106886da342Sopenharmony_ci      let message = '\n' + `${PrintTag.OHOS_REPORT_SUM}: ` + specArr.length;
107886da342Sopenharmony_ci      this.suiteService.setCurrentRunningSuiteDesc(this.suiteService.getRootSuite(), this.suiteService.getCurrentRunningSuite(), '');
108886da342Sopenharmony_ci      message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: class=` + this.suiteService.getCurrentRunningSuiteDesc() + '\n';
109886da342Sopenharmony_ci      if (this.suiteService.currentRunningSuite.isSkip) {
110886da342Sopenharmony_ci        message += `${PrintTag.OHOS_REPORT_STATUS}: skipReason=` + this.suiteService.currentRunningSuite.skipReason + '\n';
111886da342Sopenharmony_ci      }
112886da342Sopenharmony_ci      if (SysTestKit.workerPort !== null) {
113886da342Sopenharmony_ci        message += `${PrintTag.OHOS_REPORT_STATUS}: currentWorkerName=` + this.currentThreadName;
114886da342Sopenharmony_ci      }
115886da342Sopenharmony_ci      console.info(`${message}`);
116886da342Sopenharmony_ci      await SysTestKit.print(message);
117886da342Sopenharmony_ci      console.info(`${TAG}${this.suiteService.getCurrentRunningSuite().description} suiteStart print success`);
118886da342Sopenharmony_ci    }
119886da342Sopenharmony_ci  }
120886da342Sopenharmony_ci
121886da342Sopenharmony_ci  async suiteDone() {
122886da342Sopenharmony_ci    if (this.abilityDelegatorArguments !== null) {
123886da342Sopenharmony_ci      const currentRunningSuite = this.suiteService.getCurrentRunningSuite();
124886da342Sopenharmony_ci      this.suiteService.setCurrentRunningSuiteDesc(this.suiteService.getRootSuite(), this.suiteService.getCurrentRunningSuite(), '');
125886da342Sopenharmony_ci      let message = '\n' + `${PrintTag.OHOS_REPORT_STATUS}: class=` + this.suiteService.getCurrentRunningSuiteDesc();
126886da342Sopenharmony_ci      if (this.suiteService.currentRunningSuite.isSkip && this.suiteService.currentRunningSuite.skipReason !== '') {
127886da342Sopenharmony_ci        message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: skipReason=` + this.suiteService.currentRunningSuite.skipReason;
128886da342Sopenharmony_ci      }
129886da342Sopenharmony_ci      message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: suiteconsuming=` + this.suiteService.getCurrentRunningSuite().duration;
130886da342Sopenharmony_ci      if (currentRunningSuite.hookError) {
131886da342Sopenharmony_ci        message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: ${currentRunningSuite.hookError.message}`;
132886da342Sopenharmony_ci      }
133886da342Sopenharmony_ci      message += '\n';
134886da342Sopenharmony_ci      if (SysTestKit.workerPort !== null) {
135886da342Sopenharmony_ci        message += `${PrintTag.OHOS_REPORT_STATUS}: currentWorkerName=` + this.currentThreadName;
136886da342Sopenharmony_ci      }
137886da342Sopenharmony_ci      console.info(`${message}`);
138886da342Sopenharmony_ci      await SysTestKit.print(message);
139886da342Sopenharmony_ci      console.info(`${TAG}${this.suiteService.getCurrentRunningSuite().description} suiteDone print success`);
140886da342Sopenharmony_ci    }
141886da342Sopenharmony_ci  }
142886da342Sopenharmony_ci
143886da342Sopenharmony_ci  async specStart() {
144886da342Sopenharmony_ci    if (this.abilityDelegatorArguments !== null) {
145886da342Sopenharmony_ci      let message = '\n' + `${PrintTag.OHOS_REPORT_STATUS}: class=` + this.suiteService.getCurrentRunningSuiteDesc();
146886da342Sopenharmony_ci      message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: current=` + (++this.index);
147886da342Sopenharmony_ci      message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: id=JS`;
148886da342Sopenharmony_ci      message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: numtests=` + this.specService.getTestTotal();
149886da342Sopenharmony_ci      message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: stream=`;
150886da342Sopenharmony_ci      message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: test=` + this.specService.currentRunningSpec.description;
151886da342Sopenharmony_ci      message += '\n' + `${PrintTag.OHOS_REPORT_STATUS_CODE}: 1` + '\n';
152886da342Sopenharmony_ci      if (this.specService.currentRunningSpec.isSkip) {
153886da342Sopenharmony_ci        message += `${PrintTag.OHOS_REPORT_STATUS}: skipReason=` + this.specService.currentRunningSpec.skipReason + '\n';
154886da342Sopenharmony_ci      }
155886da342Sopenharmony_ci      if (SysTestKit.workerPort !== null) {
156886da342Sopenharmony_ci        message += `${PrintTag.OHOS_REPORT_STATUS}: currentWorkerName=` + this.currentThreadName;
157886da342Sopenharmony_ci      }
158886da342Sopenharmony_ci      console.info(`${message}`);
159886da342Sopenharmony_ci      await SysTestKit.print(message);
160886da342Sopenharmony_ci      console.info(`${TAG}${this.specService.currentRunningSpec.description} specStart start print success`);
161886da342Sopenharmony_ci    }
162886da342Sopenharmony_ci  }
163886da342Sopenharmony_ci
164886da342Sopenharmony_ci  async specDone() {
165886da342Sopenharmony_ci    if (this.abilityDelegatorArguments !== null) {
166886da342Sopenharmony_ci      let message = '\n' + `${PrintTag.OHOS_REPORT_STATUS}: class=` + this.suiteService.getCurrentRunningSuiteDesc();
167886da342Sopenharmony_ci      message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: current=` + (this.index);
168886da342Sopenharmony_ci      message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: id=JS`;
169886da342Sopenharmony_ci      message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: numtests=` + this.specService.getTestTotal();
170886da342Sopenharmony_ci      let messageStack = '';
171886da342Sopenharmony_ci      let messageCode = '';
172886da342Sopenharmony_ci      if (this.specService.currentRunningSpec.error) {
173886da342Sopenharmony_ci        messageStack = `${PrintTag.OHOS_REPORT_STATUS}: stack=` + this.specService.currentRunningSpec.error?.stack?.slice(0, -1);
174886da342Sopenharmony_ci        messageCode += `${PrintTag.OHOS_REPORT_STATUS}: stream=`;
175886da342Sopenharmony_ci        messageCode += this.specService.currentRunningSpec.expectMsg !== '' ?
176886da342Sopenharmony_ci          `message: ${this.specService.currentRunningSpec.expectMsg}, Error in ${this.specService.currentRunningSpec.description}, ${this.specService.currentRunningSpec.error?.message}` :
177886da342Sopenharmony_ci          `Error in ${this.specService.currentRunningSpec.description}, ${this.specService.currentRunningSpec.error?.message}`;
178886da342Sopenharmony_ci        messageCode += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: test=` + this.specService.currentRunningSpec.description;
179886da342Sopenharmony_ci        messageCode += '\n' + `${PrintTag.OHOS_REPORT_STATUS_CODE}: -1` + '\n';
180886da342Sopenharmony_ci      } else if (this.specService.currentRunningSpec) {
181886da342Sopenharmony_ci        if (this.specService.currentRunningSpec.fail) {
182886da342Sopenharmony_ci          messageStack += `${PrintTag.OHOS_REPORT_STATUS}: stack=` + this.specService.currentRunningSpec.fail?.stack?.slice(0, -1);
183886da342Sopenharmony_ci          messageCode += `${PrintTag.OHOS_REPORT_STATUS}: stream=`;
184886da342Sopenharmony_ci          messageCode += this.specService.currentRunningSpec.expectMsg !== '' ?
185886da342Sopenharmony_ci            `message: ${this.specService.currentRunningSpec.expectMsg}, Error in ${this.specService.currentRunningSpec.description}, ${this.specService.currentRunningSpec.fail?.message}` :
186886da342Sopenharmony_ci            `Error in ${this.specService.currentRunningSpec.description}, ${this.specService.currentRunningSpec.fail?.message}`;
187886da342Sopenharmony_ci          messageCode += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: test=` + this.specService.currentRunningSpec.description;
188886da342Sopenharmony_ci          messageCode += '\n' + `${PrintTag.OHOS_REPORT_STATUS_CODE}: -2` + '\n';
189886da342Sopenharmony_ci        } else {
190886da342Sopenharmony_ci          messageStack += `${PrintTag.OHOS_REPORT_STATUS}: stream=`;
191886da342Sopenharmony_ci          messageCode += `${PrintTag.OHOS_REPORT_STATUS}: test=` + this.specService.currentRunningSpec.description;
192886da342Sopenharmony_ci          messageCode += '\n' + `${PrintTag.OHOS_REPORT_STATUS_CODE}: 0` + '\n';
193886da342Sopenharmony_ci          messageCode += this.specService.currentRunningSpec.isSkip ? (`${PrintTag.OHOS_REPORT_STATUS}: skipReason=` + this.specService.currentRunningSpec.skipReason + '\n') : '';
194886da342Sopenharmony_ci        }
195886da342Sopenharmony_ci      } else {
196886da342Sopenharmony_ci        messageCode += '\n';
197886da342Sopenharmony_ci      }
198886da342Sopenharmony_ci      messageCode += `${PrintTag.OHOS_REPORT_STATUS}: consuming=` + this.specService.currentRunningSpec.duration + '\n';
199886da342Sopenharmony_ci      if (SysTestKit.workerPort !== null) {
200886da342Sopenharmony_ci        messageCode += `${PrintTag.OHOS_REPORT_STATUS}: currentWorkerName=` + this.currentThreadName;
201886da342Sopenharmony_ci      }
202886da342Sopenharmony_ci      console.info(`${message}`);
203886da342Sopenharmony_ci      console.info(`\n${messageStack}`);
204886da342Sopenharmony_ci      console.info(`\n${messageCode}`);
205886da342Sopenharmony_ci      await SysTestKit.print(message);
206886da342Sopenharmony_ci      await SysTestKit.print(messageStack);
207886da342Sopenharmony_ci      await SysTestKit.print(messageCode);
208886da342Sopenharmony_ci      console.info(`${TAG}${this.specService.currentRunningSpec.description} specDone end print success`);
209886da342Sopenharmony_ci    }
210886da342Sopenharmony_ci  }
211886da342Sopenharmony_ci}
212886da342Sopenharmony_ci
213886da342Sopenharmony_ciexport default OhReport;
214