omni-test-intelligence
Version:
Enterprise Test Intelligence Platform - Simplify test reporting with AI-powered insights.
154 lines (153 loc) • 5.82 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OmniService = void 0;
exports.configureOmniTest = configureOmniTest;
exports.setupOmniAfterEach = setupOmniAfterEach;
const axios_1 = __importDefault(require("axios"));
const promises_1 = __importDefault(require("fs/promises"));
const path_1 = __importDefault(require("path"));
const test_1 = require("@playwright/test");
// Config store
let config = {
BASE_URL: "",
PROJECT_ID: "",
API_KEY: "",
};
/**
* Configure Omni Test API integration.
* Should be called once during test setup to set API details.
*/
function configureOmniTest({ projectId, apiKey, }) {
config.BASE_URL = "https://omni-dashboard-inky.vercel.app/api/v1"; // hardcoded
config.PROJECT_ID = projectId;
config.API_KEY = apiKey;
}
const headers = () => ({
"x-api-key": config.API_KEY,
"Content-Type": "application/json",
Accept: "application/json",
});
exports.OmniService = {
async startBuild(environment = "production") {
const url = `${config.BASE_URL}/projects/${config.PROJECT_ID}/builds?days=7&environment=${environment}`;
const payload = {
duration: 0,
environment,
status: "in_progress",
};
const response = await axios_1.default.post(url, payload, {
headers: headers(),
});
return response.data.build;
},
async completeBuild(buildId, status, duration, environment = "production") {
const url = `${config.BASE_URL}/projects/${config.PROJECT_ID}/builds?build_id=${buildId}`;
const payload = {
progress_status: "completed",
status,
duration,
environment,
};
console.log(url, payload);
const response = await axios_1.default.patch(url, payload, {
headers: headers(),
});
return response.data.build;
},
async createTestCaseWithScreenshots(buildId, testCasePayload, snapshotFolderPath) {
const url = `${config.BASE_URL}/projects/${config.PROJECT_ID}/test-cases`;
const payload = {
build_id: buildId,
test_cases: [testCasePayload],
};
console.log(`Test case payload: `, payload);
try {
const response = await axios_1.default.post(url, payload, { headers: headers() });
const testCase = response.data.test_cases?.[0];
if (testCase?.screenshots?.length) {
for (const screenshot of testCase.screenshots) {
const imagePath = path_1.default.join(snapshotFolderPath, screenshot.name);
const fileData = await promises_1.default.readFile(imagePath);
await axios_1.default.put(screenshot.upload_url, fileData, {
headers: {
"Content-Type": "image/png",
},
maxBodyLength: Infinity,
});
}
}
return response.data;
}
catch (error) {
console.error("Error creating test case or uploading screenshots:", error);
throw error;
}
},
createTestCasePayload({ testInfo, stdout, screenshots, steps, }) {
let normalizedStatus;
switch (testInfo.status) {
case "passed":
case "skipped":
normalizedStatus = testInfo.status;
break;
case "failed":
case "timedOut":
case "interrupted":
default:
normalizedStatus = "failed";
break;
}
const tags = testInfo.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 testStatus = testInfo.status || "unknown";
const defaultLog = {
timestamp: new Date().toISOString(),
level: testStatus === "passed" ? "INFO" : "ERROR",
message: `${testInfo.title} ${testStatus}`,
};
const fullStdout = [defaultLog, ...stdout];
return {
name: testInfo.title,
module: filteredTags.join(", ") || "",
status: normalizedStatus,
duration: testInfo.duration || 0,
steps,
stdout: fullStdout,
screenshots,
priority,
tags: filteredTags,
errorMessage: testInfo.error?.message || "",
errorStack: testInfo.error?.stack || "",
};
},
};
/**
* Sets up the `afterEach` hook to upload test case data and screenshots to Omni.
*/
function setupOmniAfterEach(options) {
const { buildId, snapshotPath, screenshotNames, stdoutLogs = [], steps = [], } = options;
test_1.test.afterEach(async ({}, testInfo) => {
try {
const screenshots = screenshotNames.map((name) => ({
name,
timestamp: new Date().toISOString(),
}));
const payload = exports.OmniService.createTestCasePayload({
testInfo,
stdout: stdoutLogs,
screenshots,
steps,
});
const response = await exports.OmniService.createTestCaseWithScreenshots(buildId, payload, snapshotPath);
console.log(`[Omni] Uploaded test case: "${testInfo.title}"`);
}
catch (err) {
console.error(`[Omni] Failed to upload test case: "${testInfo.title}"`, err);
}
});
}