@gentrace/core
Version:
Core Gentrace Node.JS library
163 lines (160 loc) • 7.37 kB
JavaScript
import { globalGentraceApi, globalGentraceApiV2 } from './init.mjs';
import { getPipelines } from './pipeline-methods.mjs';
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 isTestCaseSingle(response) {
return response.caseId !== undefined;
}
function isUUID(str) {
const uuidPattern = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/;
return uuidPattern.test(str);
}
/**
* Retrieves test cases for a given pipeline from the Gentrace API.
* @async
* @param {string} pipelineSlug - The slug of the pipeline to filter test cases by.
* @returns {Promise<Array<TestCase>>} - A promise that resolves to an array of test cases.
* @throws {Error} - Throws an error if the Gentrace API key is not initialized or if the pipeline is not found.
* @remarks The golden dataset for the specified pipeline will be used.
*/
const getTestCases = (pipelineSlug) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
if (!globalGentraceApi) {
throw new Error("Gentrace API key not initialized. Call init() first.");
}
if (!pipelineSlug) {
throw new Error("pipelineSlug must be defined.");
}
let pipelineId;
if (!isUUID(pipelineSlug)) {
const allPipelines = yield getPipelines();
const matchingPipeline = allPipelines.find((pipeline) => pipeline.slug === pipelineSlug);
if (!matchingPipeline) {
throw new Error(`Could not find the specified pipeline (${pipelineSlug})`);
}
pipelineId = matchingPipeline.id;
}
else {
pipelineId = pipelineSlug;
}
const response = yield globalGentraceApi.v1TestCaseGet(undefined, pipelineId, pipelineSlug);
return (_a = response.data.testCases) !== null && _a !== void 0 ? _a : [];
});
/**
* Retrieves test cases for a specific dataset from the Gentrace API.
* @async
* @param {string} datasetId - The ID of the dataset to retrieve test cases for.
* @returns {Promise<Array<TestCase>>} - A promise that resolves to an array of test cases.
* @throws {Error} - Throws an error if the Gentrace API key is not initialized.
*/
const getTestCasesForDataset = (datasetId) => __awaiter(void 0, void 0, void 0, function* () {
var _b;
if (!globalGentraceApi) {
throw new Error("Gentrace API key not initialized. Call init() first.");
}
if (!datasetId) {
throw new Error("datasetId must be defined.");
}
const response = yield globalGentraceApi.v1TestCaseGet(datasetId);
return (_b = response.data.testCases) !== null && _b !== void 0 ? _b : [];
});
const getTestCase = (id) => __awaiter(void 0, void 0, void 0, function* () {
if (!globalGentraceApiV2) {
throw new Error("Gentrace API key not initialized. Call init() first.");
}
const response = yield globalGentraceApiV2.v2TestCasesIdGet(id);
const testCase = response.data;
return testCase;
});
/**
* Creates a single test case for a given pipeline ID from the Gentrace API
*
* @async
* @param {CreateSingleTestCase} payload - New test case payload
* @throws {Error} Throws an error if the SDK is not initialized. Call init() first.
* @returns {Promise<string>} A Promise that resolves to the created case ID
* @remarks If a pipeline slug is specified, the golden dataset will be used.
* If a datasetId is provided, it will be used instead.
*/
const createTestCase = (payload) => __awaiter(void 0, void 0, void 0, function* () {
if (!globalGentraceApi) {
throw new Error("Gentrace API key not initialized. Call init() first.");
}
const response = yield globalGentraceApi.v1TestCasePost(payload);
const data = response.data;
if (!isTestCaseSingle(data)) {
throw new Error("Expected a single test case to be created.");
}
return data.caseId;
});
/**
* Creates multiple test cases for a given pipeline ID from the Gentrace API
*
* @async
* @param {CreateMultipleTestCases} payload - New test case payloads
* @throws {Error} Throws an error if the SDK is not initialized. Call init() first.
* @returns {Promise<string>} A Promise that resolves to the number of test cases successfully created
* @remarks If a pipeline slug is specified, the golden dataset will be used.
* If a datasetId is provided, it will be used instead.
*/
const createTestCases = (payload) => __awaiter(void 0, void 0, void 0, function* () {
if (!globalGentraceApi) {
throw new Error("Gentrace API key not initialized. Call init() first.");
}
const response = yield globalGentraceApi.v1TestCasePost(payload);
const data = response.data;
if (isTestCaseSingle(data)) {
throw new Error("Expected multiple test cases to be created.");
}
return data.creationCount;
});
/**
* Updates a single test case in the Gentrace API
*
* @async
* @param {UpdateTestCase} payload - The payload containing the test case update information
* @throws {Error} Throws an error if the SDK is not initialized or if the test case ID is invalid
* @returns {Promise<string>} A Promise that resolves to the updated case ID
* @remarks This function updates an existing test case with the provided information.
* The payload should include the test case ID and any fields to be updated.
*/
const updateTestCase = (payload) => __awaiter(void 0, void 0, void 0, function* () {
if (!globalGentraceApi) {
throw new Error("Gentrace API key not initialized. Call init() first.");
}
const { id } = payload;
if (!isUUID(id)) {
throw new Error("Expected a valid test case ID.");
}
const response = yield globalGentraceApi.v1TestCasePatch(payload);
const data = response.data;
return data.caseId;
});
/**
* Deletes a single test case from the Gentrace API
*
* @async
* @param {string} id - The ID of the test case to delete
* @throws {Error} Throws an error if the SDK is not initialized or if the test case ID is invalid
* @returns {Promise<boolean>} A Promise that resolves to true if the test case was successfully deleted
* @remarks This function deletes an existing test case with the provided ID.
*/
const deleteTestCase = (id) => __awaiter(void 0, void 0, void 0, function* () {
if (!globalGentraceApiV2) {
throw new Error("Gentrace API key not initialized. Call init() first.");
}
if (!isUUID(id)) {
throw new Error("Expected a valid test case ID.");
}
const response = yield globalGentraceApiV2.v2TestCasesIdDelete(id);
return response.data.success;
});
export { createTestCase, createTestCases, deleteTestCase, getTestCase, getTestCases, getTestCasesForDataset, updateTestCase };
//# sourceMappingURL=test-case.mjs.map