Files
smartregistry/test/helpers/registry.ts
T

97 lines
3.2 KiB
TypeScript

import * as qenv from '@push.rocks/qenv';
import * as smartbucket from '@push.rocks/smartbucket';
import { SmartRegistry } from '../../ts/classes.smartregistry.js';
import type { IStorageHooks } from '../../ts/core/interfaces.storage.js';
import type { IUpstreamProvider } from '../../ts/upstream/interfaces.upstream.js';
import { buildTestRegistryConfig, createDefaultTestUpstreamProvider } from './registryconfig.js';
import { generateTestRunId } from './ids.js';
export {
calculateDigest,
createTestManifest,
createTestPackument,
createTestPom,
createTestJar,
calculateMavenChecksums,
createComposerZip,
createPythonWheel,
createPythonSdist,
calculatePypiHashes,
createRubyGem,
calculateRubyGemsChecksums,
} from './fixtures.js';
export { createMockAuthProvider, createTrackingUpstreamProvider } from './providers.js';
export { buildTestRegistryConfig, createDefaultTestUpstreamProvider } from './registryconfig.js';
export { createTestStorageBackend } from './storagebackend.js';
export { generateTestRunId } from './ids.js';
export { createTestTokens } from './tokens.js';
export { createTrackingHooks, createQuotaHooks } from './storagehooks.js';
const testQenv = new qenv.Qenv('./', './.nogit');
export async function cleanupS3Bucket(prefix?: string): Promise<void> {
const s3AccessKey = await testQenv.getEnvVarOnDemand('S3_ACCESSKEY');
const s3SecretKey = await testQenv.getEnvVarOnDemand('S3_SECRETKEY');
const s3Endpoint = await testQenv.getEnvVarOnDemand('S3_ENDPOINT');
const s3Port = await testQenv.getEnvVarOnDemand('S3_PORT');
const s3 = new smartbucket.SmartBucket({
accessKey: s3AccessKey || 'minioadmin',
accessSecret: s3SecretKey || 'minioadmin',
endpoint: s3Endpoint || 'localhost',
port: parseInt(s3Port || '9000', 10),
useSsl: false,
});
try {
const bucket = await s3.getBucketByName('test-registry');
if (bucket) {
if (prefix) {
// Delete only objects with the given prefix
for await (const path of bucket.listAllObjects(prefix)) {
await bucket.fastRemove({ path });
}
} else {
// Delete all objects in the bucket
for await (const path of bucket.listAllObjects()) {
await bucket.fastRemove({ path });
}
}
}
} catch (error) {
// Bucket might not exist yet, that's fine
console.log('Cleanup: No bucket to clean or error:', error);
}
}
/**
* Create a test SmartRegistry instance with all protocols enabled
*/
export async function createTestRegistry(options?: {
registryUrl?: string;
storageHooks?: IStorageHooks;
}): Promise<SmartRegistry> {
const config = await buildTestRegistryConfig(options);
const registry = new SmartRegistry(config);
await registry.init();
return registry;
}
/**
* Create a test SmartRegistry instance with upstream provider configured
*/
export async function createTestRegistryWithUpstream(
upstreamProvider?: IUpstreamProvider
): Promise<SmartRegistry> {
const config = await buildTestRegistryConfig({
upstreamProvider: upstreamProvider || createDefaultTestUpstreamProvider(),
});
const registry = new SmartRegistry(config);
await registry.init();
return registry;
}