UNPKG

@gentrace/core

Version:
217 lines (213 loc) 8.15 kB
'use strict'; var acorn = require('acorn'); var globalAxios = require('axios'); var __rest = (undefined && undefined.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; function getSingleParamName(param, index) { if (param.type === "Identifier") { return param.name; } else if (param.type === "AssignmentPattern" && param.left.type === "Identifier") { return param.left.name; } else { // If the parameter is a destructured object/array, we can't get the name return `param${index}`; } } function getParamNamesAnonymousFunction(func) { let inputs = []; try { const result = acorn.parse(`(${func.toString()})`, { ecmaVersion: 2020, }); const firstElement = result.body[0]; if (!firstElement) { return inputs; } if (firstElement.type === "ExpressionStatement") { const expression = firstElement.expression; if (expression.type === "FunctionExpression") { inputs = expression.params.map(getSingleParamName); } } } catch (e) { // Do nothing } return inputs; } function getParamNames(func) { let inputs = []; try { const result = acorn.parse(func.toString(), { ecmaVersion: 2020, }); const functionNode = result.body[0]; if (!functionNode) { return inputs; } if (functionNode.type === "FunctionDeclaration") { inputs = functionNode.params.map(getSingleParamName); } else if (functionNode.type === "ExpressionStatement") { const expression = functionNode.expression; if (expression.type === "ArrowFunctionExpression") { inputs = expression.params.map(getSingleParamName); } } } catch (e) { // There's a chance that the passed function is a regular anonymous function (which is un-parseable by acorn) return getParamNamesAnonymousFunction(func); } return inputs; } function zip(firstCollection, lastCollection) { const length = Math.min(firstCollection.length, lastCollection.length); const zipped = []; for (let index = 0; index < length; index++) { zipped.push([firstCollection[index], lastCollection[index]]); } return zipped; } let TEST_COUNTER = 0; function getTestCounter() { return TEST_COUNTER; } function incrementTestCounter() { TEST_COUNTER += 1; return TEST_COUNTER; } function decrementTestCounter() { TEST_COUNTER -= 1; return TEST_COUNTER; } function getProcessEnv(name) { if (typeof process === "undefined") { return null; } return process.env[name]; } function safeJsonParse(jsonString) { try { return JSON.parse(jsonString !== null && jsonString !== void 0 ? jsonString : ""); } catch (error) { return null; } } function getContextTestCaseFilter(contextOrCaseFilter, caseFilterOrUndefined) { let context, caseFilter; // Determine the overload being used based on the types of arguments if (typeof contextOrCaseFilter === "function") { caseFilter = contextOrCaseFilter; context = undefined; } else { context = contextOrCaseFilter; caseFilter = caseFilterOrUndefined; } return { context, caseFilter }; } function setErrorInterceptor() { globalAxios.interceptors.response.use((response) => response, (error) => { var _a, _b, _c, _d, _e, _f, _g, _h; // Guard against recursive error handling if (error.isInterceptorError) { return Promise.reject(error); } const simplified = { status: (_a = error.response) === null || _a === void 0 ? void 0 : _a.status, method: (_c = (_b = error.config) === null || _b === void 0 ? void 0 : _b.method) === null || _c === void 0 ? void 0 : _c.toUpperCase(), requestUrl: (_d = error.config) === null || _d === void 0 ? void 0 : _d.url, requestData: (_e = error.config) === null || _e === void 0 ? void 0 : _e.data, responseData: (_f = error.response) === null || _f === void 0 ? void 0 : _f.data, message: (_h = (_g = error.response) === null || _g === void 0 ? void 0 : _g.data) === null || _h === void 0 ? void 0 : _h.message, }; const newError = new Error((simplified.message || "Request failed") + ": " + JSON.stringify(simplified, null, 2)); newError.details = JSON.stringify(simplified, null, 2); newError.isInterceptorError = true; // Mark as already processed return Promise.reject(newError); }); } /** * Constructs step runs for a given test case and pipeline run. * * @param {TestCase | TestCaseV2 | LocalTestData} testCase - The test case object. * @param {PipelineRun} pipelineRun - The pipeline run object. * @returns {TestRun} The constructed test run object. */ function constructStepRuns(testCase, pipelineRun) { var _a; let mergedMetadata = {}; const updatedStepRuns = pipelineRun.stepRuns.map((stepRun) => { var _a, _b; let _c = (_a = pipelineRun.context) !== null && _a !== void 0 ? _a : {}, { metadata: thisContextMetadata, previousRunId: _prPreviousRunId } = _c, restThisContext = __rest(_c, ["metadata", "previousRunId"]); let _d = (_b = stepRun.context) !== null && _b !== void 0 ? _b : {}, { metadata: stepRunContextMetadata, previousRunId: _srPreviousRunId } = _d, restStepRunContext = __rest(_d, ["metadata", "previousRunId"]); // Merge metadata mergedMetadata = Object.assign(Object.assign(Object.assign({}, mergedMetadata), thisContextMetadata), stepRunContextMetadata); return { modelParams: stepRun.modelParams, invocation: stepRun.invocation, inputs: stepRun.inputs, outputs: stepRun.outputs, providerName: stepRun.providerName, elapsedTime: stepRun.elapsedTime, startTime: stepRun.startTime, endTime: stepRun.endTime, context: Object.assign(Object.assign({}, restThisContext), restStepRunContext), error: stepRun.error, }; }); const testRun = { caseId: (_a = testCase.id) !== null && _a !== void 0 ? _a : undefined, metadata: mergedMetadata, stepRuns: updatedStepRuns, evaluations: pipelineRun.getLocalEvaluations(), error: pipelineRun.getError(), }; if (testCase.name) { testRun.name = testCase.name; } if (testCase.inputs) { testRun.inputs = testCase.inputs; // testRun.expectedOutputs = testCase.expectedOutputs; } if (pipelineRun.getId()) { testRun.id = pipelineRun.getId(); } return testRun; } /** * Type guard to check if the test case is either TestCase or TestCaseV2 * @param testCase - The test case to check * @returns True if the test case is TestCase or TestCaseV2, false if it's LocalTestData */ function isTestCaseOrTestCaseV2(testCase) { return ("id" in testCase && "pipelineId" in testCase && "datasetId" in testCase); } exports.constructStepRuns = constructStepRuns; exports.decrementTestCounter = decrementTestCounter; exports.getContextTestCaseFilter = getContextTestCaseFilter; exports.getParamNames = getParamNames; exports.getProcessEnv = getProcessEnv; exports.getTestCounter = getTestCounter; exports.incrementTestCounter = incrementTestCounter; exports.isTestCaseOrTestCaseV2 = isTestCaseOrTestCaseV2; exports.safeJsonParse = safeJsonParse; exports.setErrorInterceptor = setErrorInterceptor; exports.zip = zip; //# sourceMappingURL=utils.js.map