UNPKG

@n1k1t/unit-generator

Version:

Coverage based unit tests AI generator

106 lines 4.29 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AssistantSource = void 0; const path_1 = __importDefault(require("path")); const promises_1 = __importDefault(require("fs/promises")); const lodash_1 = __importDefault(require("lodash")); const uuid_1 = require("uuid"); const child_process_1 = require("child_process"); const cobertura_1 = require("../cobertura"); const project_1 = require("../project"); const utils_1 = require("../../utils"); const file_1 = require("../file"); const spec_1 = require("../spec"); const env_1 = __importDefault(require("../../env")); class AssistantSource { constructor(provided) { this.provided = provided; this.id = this.provided.id ?? (0, uuid_1.v4)(); this.status = (0, utils_1.cast)('PENDING'); this.cobertura = this.provided.cobertura; this.project = this.provided.project; this.temp = this.provided.temp; this.code = this.provided.code; this.spec = this.provided.spec; this.target = this.provided?.target ?? env_1.default.target; this.timestamp = Date.now(); this.iteration = 0; this.saved = null; } refresh() { this.timestamp = Date.now(); this.status = 'PENDING'; return this; } checkHasReachedCoverage() { return this.cobertura.rate >= this.target; } compileSnapshot() { return { cobertura: { uncovered: this.cobertura.uncovered, rate: this.cobertura.rate }, spec: { content: this.spec.content }, }; } /** Saves current current state snapshot */ save() { return Object.assign(this, { saved: this.compileSnapshot() }); } /** Restores latest/provided state snapshot */ async restore(provided) { const snapshot = provided ?? this.saved; if (!snapshot) { return this; } this.cobertura.assign(snapshot.cobertura); await this.spec.write(snapshot.spec.content); return this; } async test(title = '') { const stdout = []; const spawned = (0, child_process_1.spawn)(env_1.default.command, [ `-- ${this.spec.path}`, '--coverage --forceExit', `--testNamePattern="${lodash_1.default.escapeRegExp(title)}"`, `--coverageDirectory="${this.temp}"`, `--collectCoverageFrom="${this.code.path}"`, ], { shell: true }); spawned.stderr.on('data', (chunk) => stdout.push(chunk.toString())); const status = await Promise.race([ new Promise((resolve, reject) => spawned.once('error', (error) => reject(error))), new Promise((resolve) => spawned.once('exit', (code) => resolve(code ?? 1))), ]); await this.cobertura.refresh(); return status !== 0 ? { status: 'FAILED', message: stdout.join('') } : { status: 'PASSED' }; } static async build(location, options) { const id = (0, uuid_1.v4)(); const cwd = options?.cwd ?? process.cwd(); const parsed = path_1.default.parse(location); const temp = path_1.default.join(path_1.default.relative(cwd, path_1.default.join(__dirname, '../../')), 'generated', id); if (!(await promises_1.default.stat(temp).catch(() => null))) { await promises_1.default.mkdir(temp); } const code = await file_1.File.build(location); const spec = await spec_1.Spec.build(path_1.default.join(parsed.dir, `${parsed.name}.spec${parsed.ext}`)); const cobertura = await cobertura_1.Cobertura.build(path_1.default.join(temp, 'cobertura-coverage.xml')); const project = await project_1.Project.build({ cwd }); const source = new AssistantSource({ id, temp, code, spec, cobertura, project, target: options?.target, }); if (options?.rate !== undefined) { cobertura.assign({ rate: options.rate }); } return source; } } exports.AssistantSource = AssistantSource; //# sourceMappingURL=source.js.map