UNPKG

omni-test-intelligence

Version:

Enterprise Test Intelligence Platform - Simplify test reporting with AI-powered insights.

178 lines (177 loc) 7.66 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); const uploadTestCase_1 = require("./uploadTestCase"); const chalk_1 = __importDefault(require("chalk")); class MyReporter { constructor(options = {}) { this.buildId = ''; this.testPromises = []; console.log(chalk_1.default.cyan('[OmniReporter] Setup with customOption:', options.customOption)); } onBegin(config, suite) { const BASE_URL = 'https://omni-dashboard-inky.vercel.app/api/v1'; const PROJECT_ID = process.env.PROJECT_ID; const API_KEY = process.env.API_KEY; const environment = 'production'; if (!PROJECT_ID || !API_KEY) { console.error(chalk_1.default.red('[OmniReporter] Error: PROJECT_ID and API_KEY environment variables are required')); return; } const url = `${BASE_URL}/projects/${PROJECT_ID}/builds?days=7&environment=${environment}`; const payload = { duration: 0, environment, status: 'in_progress', }; return axios_1.default.post(url, payload, { headers: { 'x-api-key': API_KEY, 'Content-Type': 'application/json', 'Accept': 'application/json' }, }) .then(response => { this.buildId = response.data.build.build_id; console.log(chalk_1.default.green('[OmniReporter] Build Started:', this.buildId)); }) .catch(error => { console.error(chalk_1.default.red('[OmniReporter] Failed to start build:', error?.response?.data || error.message)); if (error?.response?.status === 401) { console.error(chalk_1.default.red('[OmniReporter] Authentication failed. Please check your API_KEY')); } throw error; }); } onTestBegin(test) { console.log(chalk_1.default.cyan('[OmniReporter] Starting test:', test.title)); } onTestEnd(test, result) { const title = test.title; const duration = result.duration; const status = result.status; const errorMessage = result.error?.message || ''; const errorStack = result.error?.stack || ''; const tags = test.tags?.map((tag) => tag.replace(/^@/, '')) || []; const priorityTag = tags.find((t) => /^P[0-3]$/.test(t)); const priority = priorityTag || 'P1'; const filteredTags = tags.filter((t) => t !== priority); const defaultLog = { timestamp: new Date().toISOString(), level: status === 'passed' ? 'INFO' : 'ERROR', message: `${title} ${status}`, }; const stdout = [defaultLog]; const steps = result.steps.map((step, index) => ({ name: step.title, sequence_number: index + 1, duration: step.duration, status: 'passed', })); const screenshots = result.attachments .filter((att) => att.contentType === 'image/png' && att.path) .map((att) => ({ name: att.name, })); const screenshotMeta = screenshots.map((s) => ({ name: s.name, timestamp: new Date().toISOString(), })); const screenshotsPath = result.attachments .filter((att) => att.contentType === 'image/png' && att.path) .map((att) => ({ name: att.name, path: att.path, })); const tracesPath = result.attachments .filter((att) => att.name === 'trace' && att.contentType === 'application/zip' && att.path) .map((att) => ({ name: 'trace.zip', path: att.path, })); const testCasePayload = { name: title, module: filteredTags.join(', ') || 'General', priority: priority || 'P1', tags: filteredTags.length > 0 ? filteredTags : ['general'], status: status === 'passed' || status === 'skipped' ? status : 'failed', duration, steps, stdout, screenshots: screenshotMeta, traces: [{ "name": "trace.zip" }], annotations: result.annotations, error_message: errorMessage, error_stack_trace: errorStack, }; const url = `https://omni-dashboard-inky.vercel.app/api/v1/projects/${process.env.PROJECT_ID}/test-cases`; const payload = { build_id: this.buildId, test_cases: [testCasePayload], }; if (!process.env.API_KEY) { console.error(chalk_1.default.red('[OmniReporter] Error: API_KEY is required')); return; } const headers = { 'x-api-key': process.env.API_KEY, 'Content-Type': 'application/json', 'Accept': 'application/json' }; console.log(chalk_1.default.cyan('[OmniReporter] Uploading test case:', title)); const testPromise = (0, uploadTestCase_1.uploadTestCase)({ url, payload, headers }, screenshotsPath, tracesPath) .then(testCase => { console.log(chalk_1.default.green('[OmniReporter] Test case uploaded:', title)); return testCase; }) .catch(error => { console.error(chalk_1.default.red('[OmniReporter] Failed to upload test case:', title), error?.response?.data || error.message); throw error; }); this.testPromises.push(testPromise); return testPromise; } async onEnd(result) { try { await Promise.all(this.testPromises); const BASE_URL = 'https://omni-dashboard-inky.vercel.app/api/v1'; const PROJECT_ID = process.env.PROJECT_ID; const API_KEY = process.env.API_KEY; const environment = 'production'; const roundedDuration = Math.round(result.duration); if (!PROJECT_ID || !API_KEY) { console.error(chalk_1.default.red('[OmniReporter] Error: PROJECT_ID and API_KEY environment variables are required')); return; } if (!this.buildId) { console.error(chalk_1.default.red('[OmniReporter] Error: No build ID available to complete the build')); return; } const url = `${BASE_URL}/projects/${PROJECT_ID}/builds?build_id=${this.buildId}`; const payload = { progress_status: 'completed', status: result.status, duration: roundedDuration, environment, }; const response = await axios_1.default.patch(url, payload, { headers: { 'x-api-key': API_KEY, 'Content-Type': 'application/json', 'Accept': 'application/json' }, }); console.log(chalk_1.default.green('[OmniReporter] Build Completed:', response.data.build.build_id)); } catch (error) { console.error(chalk_1.default.red('[OmniReporter] Failed to complete build:', error?.response?.data || error.message)); if (error?.response?.status === 401) { console.error(chalk_1.default.red('[OmniReporter] Authentication failed. Please check your API_KEY')); } } } } exports.default = MyReporter;