@zebrunner/javascript-agent-mocha
Version:
Zebrunner Agent for Mocha
153 lines (116 loc) • 3.95 kB
JavaScript
const { REPORTING_EVENTS } = require('./events');
const { logsDelimiter } = require('./constants');
class EventStorage {
#parallel;
#pid;
#tests;
#testPids;
#events;
#logMessages;
#mochaIdToSuite;
constructor(parallel, pid) {
this.#parallel = parallel;
this.#pid = pid;
this.#tests = [];
this.#testPids = [];
this.#events = [];
this.#logMessages = [];
this.#mochaIdToSuite = new Map();
}
saveTestPid(test) {
this.#testPids.push(test);
}
getSuiteByMochaId(mochaId) {
return this.#mochaIdToSuite.get(mochaId);
}
addSuite(suite) {
this.#mochaIdToSuite.set(suite.__mocha_id__, suite);
}
removeSuite(suite) {
this.#mochaIdToSuite.delete(suite.__mocha_id__);
}
startTest(test) {
const testInfo = { ...test, started: Date.now() };
this.#tests.push(testInfo);
}
processTest(test) {
const index = this.#tests.findIndex((item) => item.uniqueId === test.uniqueId);
if (index !== -1) {
this.#tests[index].processed = Date.now();
}
}
getUnprocessedStartedTests() {
return this.#tests.filter((item) => item.started && !item.processed);
}
isTestStarted(test) {
return this.#tests.find((item) => item.uniqueId === test.uniqueId && item.started);
}
saveEvent(event) {
this.#events.push(event);
}
getTestEvents(test) {
const testEvents = this.#events.filter((item) => item.test.uniqueId === test.uniqueId);
this.#events = this.#events.filter((item) => !testEvents.includes(item));
return testEvents;
}
getLogEvents() {
const logEvents = this.#events.filter((item) => item.event === REPORTING_EVENTS.SEND_TEST_LOGS);
this.#events = this.#events.filter((item) => !logEvents.includes(item));
// console.log('Zebrunner: logEvents');
// console.log(logEvents);
return logEvents;
}
saveLogs(logs) {
this.#logMessages.push(...logs);
}
processLogs() {
return new Promise((resolve) => {
const unprocessedTests = this.getUnprocessedStartedTests();
if (unprocessedTests.length === 0) {
return;
}
unprocessedTests.forEach((test) => {
let logs = this.#logMessages;
if (this.#parallel === true) {
const testPid = this.#testPids.find((item) => item.uniqueId === test.uniqueId);
console.log('Zebrunner: found test for pid');
console.log(testPid);
if (!testPid || !testPid.pid) {
return;
}
const currentPid = this.#pid + Number(testPid.pid) + 1;
const pidLogs = this.#logMessages.filter((log) => log.pid === currentPid);
logs = pidLogs;
console.log('Zebrunner: pid logs');
console.log(pidLogs);
}
if (logs.length === 0) {
return;
}
const startIndex = logs.findIndex((log) => log.message.includes(logsDelimiter.EVENT_TEST_BEGIN));
const finishIndex = logs.findIndex((log) => log.message.includes(logsDelimiter.EVENT_TEST_END));
console.log(`Zebrunner: TEST_LOGS_BEGIN = ${startIndex}`);
console.log(`Zebrunner: TEST_LOGS_END = ${finishIndex}`);
if (startIndex !== -1 && finishIndex !== -1) {
const testLogs = logs.slice(startIndex + 1, finishIndex);
if (testLogs.length !== 0) {
console.log('Zebrunner: found logs');
console.log(testLogs);
this.saveEvent({
event: REPORTING_EVENTS.SEND_TEST_LOGS,
test,
logs: testLogs,
});
}
this.processTest(test);
const toRemove = logs.slice(startIndex, finishIndex + 1);
this.#logMessages = this.#logMessages.filter((item) => !toRemove.includes(item));
// console.log('Zebrunner: unprocessed logs');
// console.log(this.#logMessages);
}
});
resolve();
});
}
}
module.exports = EventStorage;