tsonik
Version:
A TypeScript client library for the Iconik API based on Swagger documentation
163 lines • 5.86 kB
JavaScript
;
/**
* Shared utilities for integration tests
* Provides reliable test data setup and cleanup
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.trackCreatedAsset = exports.trackCreatedCollection = exports.trackCreatedFile = exports.trackCreatedFileset = exports.trackCreatedFormat = exports.withTestData = exports.validateTestEnvironment = exports.cleanupTestData = exports.setupTestData = exports.createTestAsset = exports.createTestClient = exports.getTestConfig = void 0;
const client_1 = require("../client");
/**
* Get test configuration from environment variables
* Throws error if not properly configured
*/
function getTestConfig() {
const baseUrl = process.env.ICONIK_BASE_URL;
const appId = process.env.ICONIK_APP_ID;
const authToken = process.env.ICONIK_AUTH_TOKEN;
if (!baseUrl || !appId || !authToken) {
throw new Error('Integration test environment not configured. Required environment variables: ' +
'ICONIK_BASE_URL, ICONIK_APP_ID, ICONIK_AUTH_TOKEN');
}
return { baseUrl, appId, authToken };
}
exports.getTestConfig = getTestConfig;
/**
* Create a test client
*/
function createTestClient(config) {
return new client_1.Tsonik({
baseUrl: config.baseUrl,
appId: config.appId,
authToken: config.authToken
});
}
exports.createTestClient = createTestClient;
/**
* Create a test asset for use in integration tests
*/
async function createTestAsset(client, title = 'Integration Test Asset') {
const assetData = {
title,
description: `Test asset created by integration tests at ${new Date().toISOString()}`,
type: 'ASSET',
status: 'ACTIVE'
};
const response = await client.assets.createAsset(assetData);
if (response.status !== 201) {
throw new Error(`Failed to create test asset: ${response.status}`);
}
return response.data;
}
exports.createTestAsset = createTestAsset;
/**
* Setup test data for integration tests
* Creates necessary test assets and returns TestData object
*/
async function setupTestData(customTitle) {
const config = getTestConfig();
const client = createTestClient(config);
// Create a test asset
const testAsset = await createTestAsset(client, customTitle);
return {
client,
testAsset,
testAssetId: testAsset.id,
createdAssetIds: [testAsset.id],
createdFormatIds: [],
createdFilesetIds: [],
createdFileIds: [],
createdCollectionIds: []
};
}
exports.setupTestData = setupTestData;
/**
* Cleanup test data
* Attempts to delete all created resources
*/
async function cleanupTestData(testData) {
const { client } = testData;
// Note: The order matters - delete dependent resources first
// Clean up formats (if delete endpoint exists)
// Formats are typically cleaned up automatically when assets are deleted
// Clean up filesets (if delete endpoint exists)
// Filesets are typically cleaned up automatically when assets are deleted
// Clean up files (if delete endpoint exists)
// Files are typically cleaned up automatically when assets are deleted
// Clean up collections
for (const collectionId of testData.createdCollectionIds) {
try {
await client.collections.deleteCollection(collectionId);
console.log(`Cleaned up test collection: ${collectionId}`);
}
catch (error) {
console.warn(`Failed to cleanup collection ${collectionId}:`, error);
}
}
// Clean up assets last
for (const assetId of testData.createdAssetIds) {
try {
await client.assets.deleteAsset(assetId);
console.log(`Cleaned up test asset: ${assetId}`);
}
catch (error) {
console.warn(`Failed to cleanup asset ${assetId}:`, error);
}
}
}
exports.cleanupTestData = cleanupTestData;
/**
* Ensure test environment is ready
* Validates that we can connect and perform basic operations
*/
async function validateTestEnvironment(client) {
try {
// Try to list assets to validate connection
const response = await client.assets.listAssets({ per_page: 1 });
if (response.status !== 200) {
throw new Error(`API connection failed with status: ${response.status}`);
}
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to validate test environment: ${errorMessage}`);
}
}
exports.validateTestEnvironment = validateTestEnvironment;
/**
* Wrapper for integration tests that handles setup and cleanup
*/
async function withTestData(testFn, customTitle) {
const testData = await setupTestData(customTitle);
try {
await validateTestEnvironment(testData.client);
return await testFn(testData);
}
finally {
await cleanupTestData(testData);
}
}
exports.withTestData = withTestData;
/**
* Add tracking for created resources
*/
function trackCreatedFormat(testData, formatId) {
testData.createdFormatIds.push(formatId);
}
exports.trackCreatedFormat = trackCreatedFormat;
function trackCreatedFileset(testData, filesetId) {
testData.createdFilesetIds.push(filesetId);
}
exports.trackCreatedFileset = trackCreatedFileset;
function trackCreatedFile(testData, fileId) {
testData.createdFileIds.push(fileId);
}
exports.trackCreatedFile = trackCreatedFile;
function trackCreatedCollection(testData, collectionId) {
testData.createdCollectionIds.push(collectionId);
}
exports.trackCreatedCollection = trackCreatedCollection;
function trackCreatedAsset(testData, assetId) {
testData.createdAssetIds.push(assetId);
}
exports.trackCreatedAsset = trackCreatedAsset;
//# sourceMappingURL=test-utils.js.map