@gentrace/core
Version:
Core Gentrace Node.JS library
78 lines (74 loc) • 3.83 kB
JavaScript
;
var utils = require('./utils.js');
var init = require('./init.js');
var testCase = require('./test-case.js');
var testResult = require('./test-result.js');
var pipelineMethods = require('./pipeline-methods.js');
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
function runTestCore(pipelineSlug, handler, context, caseFilter, datasetId) {
return __awaiter(this, void 0, void 0, function* () {
utils.incrementTestCounter();
try {
const allPipelines = yield pipelineMethods.getPipelines();
const matchingPipeline = allPipelines.find((pipeline) => pipeline.slug === pipelineSlug);
if (!matchingPipeline) {
throw new Error(`Could not find the specified pipeline (${pipelineSlug})`);
}
const testCases = datasetId
? yield testCase.getTestCasesForDataset(datasetId)
: yield testCase.getTestCases(pipelineSlug);
const testRuns = [];
for (const testCase of testCases) {
if (caseFilter && !caseFilter(testCase)) {
continue;
}
const [, pipelineRun] = yield handler(testCase);
const testRun = utils.constructStepRuns(testCase, pipelineRun);
testRuns.push(testRun);
}
if (!init.globalGentraceApi) {
throw new Error("Gentrace API key not initialized. Call init() first.");
}
const body = testResult.constructSubmissionPayloadAdvanced(matchingPipeline.id, testRuns, context);
const response = yield init.globalGentraceApi.v1TestResultPost(body);
return response.data;
}
catch (e) {
throw e;
}
finally {
utils.decrementTestCounter();
}
});
}
function runTest(pipelineSlug, handler, contextOrCaseFilter, caseFilterOrUndefined) {
return __awaiter(this, void 0, void 0, function* () {
const { context, caseFilter } = utils.getContextTestCaseFilter(contextOrCaseFilter, caseFilterOrUndefined);
return runTestCore(pipelineSlug, handler, context, caseFilter);
});
}
/**
* Runs a test for a given pipeline slug and dataset ID.
* @param {string} pipelineSlug: The slug of the pipeline
* @param {string} datasetId: The ID of the dataset
* @param {function} handler: The handler function that will be called for each test case
* @param {ResultContext | function} [contextOrCaseFilter]: An optional context object that will be passed to the Gentrace API
* @param {function} [caseFilterOrUndefined]: An optional filter function that will be called for each test case
*/
function runTestWithDataset(pipelineSlug, datasetId, handler, contextOrCaseFilter, caseFilterOrUndefined) {
return __awaiter(this, void 0, void 0, function* () {
const { context, caseFilter } = utils.getContextTestCaseFilter(contextOrCaseFilter, caseFilterOrUndefined);
return runTestCore(pipelineSlug, handler, context, caseFilter, datasetId);
});
}
exports.runTest = runTest;
exports.runTestWithDataset = runTestWithDataset;
//# sourceMappingURL=run-test.js.map