donobu
Version:
Create browser automations with an LLM agent and replay them as Playwright scripts.
114 lines • 4.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestsPersistenceDonobuApi = void 0;
const TestNotFoundException_1 = require("../../exceptions/TestNotFoundException");
const PaginatedResult_1 = require("../../models/PaginatedResult");
const TestMetadata_1 = require("../../models/TestMetadata");
const DonobuApiClient_1 = require("../DonobuApiClient");
const normalizeFlowMetadata_1 = require("../normalizeFlowMetadata");
class TestsPersistenceDonobuApi extends DonobuApiClient_1.DonobuApiClient {
constructor(baseUrl, apiKey) {
super(baseUrl, apiKey);
}
async createTest(testMetadata) {
const response = await this.jsonRequest('/v1/tests', 'POST', {
id: testMetadata.id,
name: testMetadata.name,
suiteId: testMetadata.suiteId ?? null,
nextRunMode: testMetadata.nextRunMode,
record: testMetadata,
});
if (!response.ok) {
throw new Error(`Failed to create test: ${response.status} ${response.statusText}`);
}
}
async updateTest(testMetadata) {
const testId = encodeURIComponent(testMetadata.id);
const response = await this.jsonRequest(`/v1/tests/${testId}`, 'PUT', {
id: testMetadata.id,
name: testMetadata.name,
suiteId: testMetadata.suiteId ?? null,
nextRunMode: testMetadata.nextRunMode,
record: testMetadata,
});
if (response.status === 404) {
throw TestNotFoundException_1.TestNotFoundException.forId(testMetadata.id);
}
if (!response.ok) {
throw new Error(`Failed to update test: ${response.status} ${response.statusText}`);
}
}
async getTestById(testId) {
const response = await this.jsonRequest(`/v1/tests/${encodeURIComponent(testId)}`, 'GET');
if (response.status === 404) {
throw TestNotFoundException_1.TestNotFoundException.forId(testId);
}
if (!response.ok) {
throw new Error(`Failed to get test: ${response.status} ${response.statusText}`);
}
const json = (await response.json());
return TestMetadata_1.TestMetadataSchema.parse(json.record);
}
async getTests(query) {
const params = new URLSearchParams();
if (query.name) {
params.set('name', query.name);
}
if (query.partialName) {
params.set('partial_name', query.partialName);
}
if (query.suiteId) {
params.set('suiteId', query.suiteId);
}
if (query.sortBy) {
params.set('sort_by', query.sortBy);
}
if (query.sortOrder) {
params.set('sort_order', query.sortOrder);
}
if (query.limit) {
params.set('limit', query.limit.toString());
}
if (query.pageToken) {
params.set('pageToken', query.pageToken);
}
const qs = params.toString();
const path = `/v1/tests${qs ? `?${qs}` : ''}`;
const response = await this.jsonRequest(path, 'GET');
if (!response.ok) {
throw new Error(`Failed to list tests: ${response.status} ${response.statusText}`);
}
// Validate the basic pagination shape of the API response, but keep the
// items as `unknown` for now.
const json = PaginatedResult_1.PaginatedResultSchema.parse(await response.json());
return {
items: json.items.map((item) => {
// Cast this enough to be able to easily pull out the `latestFlow`
// field. It'll be properly validated by the schema in the `return`
// statement below, after normalizing the flow metadata.
const testListItem = item;
// Run full validation after normalizing the flow metadata.
return TestMetadata_1.TestListItemSchema.parse({
...testListItem,
latestFlow: testListItem.latestFlow
? (0, normalizeFlowMetadata_1.normalizeFlowMetadata)(testListItem.latestFlow)
: null,
});
}),
nextPageToken: json.nextPageToken,
};
}
async deleteTest(testId) {
// The Donobu API cascade deletes all flows associated with the test, via a
// DB FK constraint.
const response = await this.jsonRequest(`/v1/tests/${encodeURIComponent(testId)}`, 'DELETE');
if (!response.ok) {
if (response.status === 404) {
throw TestNotFoundException_1.TestNotFoundException.forId(testId);
}
throw new Error(`Failed to delete test: ${response.status} ${response.statusText}`);
}
}
}
exports.TestsPersistenceDonobuApi = TestsPersistenceDonobuApi;
//# sourceMappingURL=TestsPersistenceDonobuApi.js.map