omni-test-intelligence
Version:
Enterprise Test Intelligence Platform - Simplify test reporting with AI-powered insights.
86 lines (85 loc) • 4.15 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.uploadTestCase = uploadTestCase;
// uploadTestCase.ts
const axios_1 = __importDefault(require("axios"));
const promises_1 = __importDefault(require("fs/promises"));
const chalk_1 = __importDefault(require("chalk"));
function uploadTestCase({ url, payload, headers }, screenshotsPath, tracesPath) {
return axios_1.default.post(url, payload, { headers })
.then(response => {
const testCase = response.data.test_cases?.[0];
if (!testCase) {
throw new Error('No test case data received from server');
}
const uploadPromises = [];
// Handle screenshots upload
if (testCase.screenshots && screenshotsPath) {
const screenshotPromises = testCase.screenshots.map((screenshot) => {
const localScreenshot = screenshotsPath.find((s) => s.name === screenshot.name);
if (!localScreenshot) {
console.warn(chalk_1.default.yellow('[OmniReporter] Screenshot not found locally:', screenshot.name));
return Promise.resolve();
}
return promises_1.default.readFile(localScreenshot.path)
.then(fileData => {
return axios_1.default.put(screenshot.upload_url, fileData, {
headers: { 'Content-Type': 'image/png' },
maxBodyLength: Infinity,
});
})
.then(() => {
console.log(chalk_1.default.green('[OmniReporter] Uploaded screenshot:', screenshot.name));
})
.catch(uploadErr => {
console.error(chalk_1.default.red('[OmniReporter] Failed to upload screenshot:', screenshot.name), uploadErr);
throw uploadErr;
});
});
uploadPromises.push(...screenshotPromises);
}
// Handle traces upload
if (testCase.traces && tracesPath) {
const tracePromises = testCase.traces.map((trace) => {
const localTrace = tracesPath.find((t) => t.name === trace.name);
if (!localTrace) {
console.warn(chalk_1.default.yellow('[OmniReporter] Trace not found locally:', trace.name));
return Promise.resolve();
}
return promises_1.default.readFile(localTrace.path)
.then(fileData => {
return axios_1.default.put(trace.upload_url, fileData, {
headers: { 'Content-Type': 'application/zip' },
maxBodyLength: Infinity,
});
})
.then(() => {
console.log(chalk_1.default.green('[OmniReporter] Uploaded trace:', trace.name));
})
.catch(uploadErr => {
console.error(chalk_1.default.red('[OmniReporter] Failed to upload trace:', trace.name), uploadErr);
throw uploadErr;
});
});
uploadPromises.push(...tracePromises);
}
return Promise.all(uploadPromises)
.then(() => testCase)
.catch(error => {
console.error(chalk_1.default.red('[OmniReporter] Error uploading files:'), error);
throw error;
});
})
.catch(error => {
console.error(chalk_1.default.red('[OmniReporter] Failed to upload test case:'), error);
if (axios_1.default.isAxiosError(error)) {
console.error(chalk_1.default.red('[OmniReporter] Response data:'), error.response?.data);
console.error(chalk_1.default.red('[OmniReporter] Response status:'), error.response?.status);
console.error(chalk_1.default.red('[OmniReporter] Response headers:'), error.response?.headers);
}
throw error;
});
}