@hivetechs/hive-ai
Version:
Real-time streaming AI consensus platform with HTTP+SSE MCP integration for Claude Code, VS Code, Cursor, and Windsurf - powered by OpenRouter's unified API
299 lines โข 11 kB
JavaScript
/**
* Test Script for Database Cleanup System
*
* Comprehensive testing for the cleanup-models functionality
* including edge cases, error handling, and safety features.
*/
import { runDatabaseCleanup } from './database-cleanup.js';
/**
* Create test invalid models in the database
*/
async function createTestInvalidModels(db) {
const testModels = [
'anthropic/claude-opus-4', // Non-existent model
'openai/gpt-5-turbo', // Non-existent model
'google/gemini-ultra-2', // Non-existent model
'meta/llama-4-70b' // Non-existent model
];
for (const modelId of testModels) {
const [provider, name] = modelId.split('/');
await db.run(`
INSERT OR IGNORE INTO openrouter_models
(openrouter_id, name, provider_id, provider_name, created_at, last_updated, is_active)
VALUES (?, ?, ?, ?, ?, ?, ?)
`, [
modelId,
name,
provider,
provider,
Date.now(),
new Date().toISOString(),
1
]);
}
return testModels;
}
/**
* Create test pipeline profile that references invalid models
*/
async function createTestProfileWithInvalidModels(db, invalidModels) {
const profileId = 'test-profile-' + Date.now();
// Get or create internal IDs for the invalid models
const modelInternalIds = {};
for (const modelId of invalidModels.slice(0, 4)) { // Use first 4 for the 4 stages
const model = await db.get('SELECT internal_id FROM openrouter_models WHERE openrouter_id = ?', [modelId]);
if (model) {
modelInternalIds[modelId] = model.internal_id;
}
}
await db.run(`
INSERT OR IGNORE INTO pipeline_profiles
(id, name, generator_model_internal_id, refiner_model_internal_id,
validator_model_internal_id, curator_model_internal_id,
generator_model, refiner_model, validator_model, curator_model,
created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`, [
profileId,
'Test Profile with Invalid Models',
modelInternalIds[invalidModels[0]] || 1,
modelInternalIds[invalidModels[1]] || 2,
modelInternalIds[invalidModels[2]] || 3,
modelInternalIds[invalidModels[3]] || 4,
invalidModels[0],
invalidModels[1] || invalidModels[0],
invalidModels[2] || invalidModels[0],
invalidModels[3] || invalidModels[0],
new Date().toISOString(),
new Date().toISOString()
]);
return profileId;
}
/**
* Clean up test data
*/
async function cleanupTestData(db, testModels, testProfileId) {
// Remove test profile
await db.run('DELETE FROM pipeline_profiles WHERE id = ?', [testProfileId]);
// Remove test models
for (const modelId of testModels) {
await db.run('DELETE FROM openrouter_models WHERE openrouter_id = ?', [modelId]);
}
}
/**
* Test cases for the cleanup system
*/
const testCases = [
{
name: 'Dry Run Test',
description: 'Test dry run mode - should not make any actual changes',
args: { dryRun: true, force: true, batchSize: 10 },
validation: async (result) => {
// In dry run, no models should be removed
return result.removedModels.length === 0 && result.stats.modelsRemoved === 0;
}
},
{
name: 'Live Cleanup Test',
description: 'Test actual cleanup with force flag',
args: { dryRun: false, force: true, batchSize: 10 },
validation: async (result) => {
// Should have removed the test invalid models
return result.removedModels.length > 0 && result.stats.modelsRemoved > 0;
}
},
{
name: 'Batch Size Test',
description: 'Test different batch sizes',
args: { dryRun: true, force: true, batchSize: 5 },
validation: async (result) => {
// Should still work with smaller batch size
return result.stats.totalModelsChecked > 0;
}
},
{
name: 'Timeout Test',
description: 'Test with shorter timeout',
args: { dryRun: true, force: true, timeout: 10000 },
validation: async (result) => {
// Should handle timeout gracefully
return true; // Any result is acceptable for timeout test
}
},
{
name: 'Profile Update Test',
description: 'Test that profiles with invalid models are updated',
setup: async () => {
const { getDatabase } = await import('../storage/unified-database.js');
const db = await getDatabase();
const testModels = await createTestInvalidModels(db);
const testProfileId = await createTestProfileWithInvalidModels(db, testModels);
// Store for cleanup
global.__testModels = testModels;
global.__testProfileId = testProfileId;
},
args: { dryRun: true, force: true, batchSize: 10 },
validation: async (result) => {
// Should find profiles that need updating
return result.updatedProfiles.length > 0;
},
cleanup: async () => {
const { getDatabase } = await import('../storage/unified-database.js');
const db = await getDatabase();
await cleanupTestData(db, global.__testModels, global.__testProfileId);
}
},
{
name: 'Error Handling Test',
description: 'Test error handling with invalid API key',
args: { dryRun: true, force: true, batchSize: 10 },
setup: async () => {
// Temporarily break the API key
const { setConfig } = await import('../storage/unified-database.js');
await setConfig('openrouter_api_key_backup', await import('../storage/unified-database.js').then(db => db.getOpenRouterApiKey()) || '');
await setConfig('openrouter_api_key', 'invalid-key');
},
validation: async (result) => {
// Should have errors due to invalid API key
return result.errors.length > 0;
},
cleanup: async () => {
// Restore the API key
const { setConfig, getConfig } = await import('../storage/unified-database.js');
const backupKey = await getConfig('openrouter_api_key_backup');
if (backupKey) {
await setConfig('openrouter_api_key', backupKey);
}
}
}
];
/**
* Run a single test case
*/
async function runTestCase(testCase) {
console.log(`\n๐งช Running test: ${testCase.name}`);
console.log(`๐ ${testCase.description}`);
try {
// Setup
if (testCase.setup) {
console.log('โ๏ธ Setting up test...');
await testCase.setup();
}
// Run the cleanup
console.log('๐ Running cleanup...');
const result = await runDatabaseCleanup(testCase.args);
// Validate results
console.log('โ
Validating results...');
const passed = await testCase.validation(result);
// Cleanup
if (testCase.cleanup) {
console.log('๐งน Cleaning up test...');
await testCase.cleanup();
}
if (passed) {
console.log(`โ
Test passed: ${testCase.name}`);
}
else {
console.log(`โ Test failed: ${testCase.name}`);
}
return { passed };
}
catch (error) {
console.log(`โ Test error: ${testCase.name} - ${error.message}`);
// Still try to cleanup
if (testCase.cleanup) {
try {
await testCase.cleanup();
}
catch (cleanupError) {
console.log(`โ ๏ธ Cleanup error: ${cleanupError.message}`);
}
}
return { passed: false, error: error.message };
}
}
/**
* Run all test cases
*/
export async function runCleanupSystemTests() {
console.log('๐งช Starting Database Cleanup System Tests\n');
console.log('โ ๏ธ These tests will create and remove test data in your database.');
console.log('๐ Make sure you have a backup before running these tests.\n');
// Check prerequisites
try {
const { getOpenRouterApiKey } = await import('../storage/unified-database.js');
const apiKey = await getOpenRouterApiKey();
if (!apiKey) {
throw new Error('OpenRouter API key not configured');
}
console.log('โ
Prerequisites check passed\n');
}
catch (error) {
console.log('โ Prerequisites check failed:', error.message);
console.log('Please run: hive-ai provider configure openrouter <your-key>');
return;
}
const results = {
total: testCases.length,
passed: 0,
failed: 0,
errors: []
};
// Run each test case
for (const testCase of testCases) {
const result = await runTestCase(testCase);
if (result.passed) {
results.passed++;
}
else {
results.failed++;
if (result.error) {
results.errors.push(`${testCase.name}: ${result.error}`);
}
}
// Add delay between tests
await new Promise(resolve => setTimeout(resolve, 1000));
}
// Summary
console.log('\n๐ Test Results Summary:');
console.log(` Total tests: ${results.total}`);
console.log(` Passed: ${results.passed}`);
console.log(` Failed: ${results.failed}`);
if (results.errors.length > 0) {
console.log('\nโ Errors:');
results.errors.forEach(error => {
console.log(` โข ${error}`);
});
}
if (results.failed === 0) {
console.log('\n๐ All tests passed! The cleanup system is working correctly.');
}
else {
console.log('\nโ ๏ธ Some tests failed. Please review the errors above.');
}
console.log('\n๐ก The cleanup system is now ready for production use.');
console.log('๐ก Run "hive-ai cleanup-models --dry-run" to preview cleanup operations.');
}
/**
* CLI function to run tests
*/
export async function runCleanupTestsTool() {
try {
await runCleanupSystemTests();
return {
result: 'โ
Database cleanup system tests completed.\n\n' +
'See the detailed output above for test results.\n\n' +
'๐ก Use "hive-ai cleanup-models --dry-run" to preview cleanup operations.'
};
}
catch (error) {
return {
result: `โ Test execution failed: ${error.message}\n\n` +
'Please check your configuration and try again.'
};
}
}
// Export for CLI usage
export const cleanupTestsToolName = 'test_cleanup_system';
export const cleanupTestsToolDescription = 'Run comprehensive tests for the database cleanup system';
//# sourceMappingURL=test-cleanup-system.js.map