120 lines
4.2 KiB
TypeScript
120 lines
4.2 KiB
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import assert from 'assert';
|
|
import { ConfigStore } from './src/configStore';
|
|
import { RevolverEngine } from './src/revolverEngine';
|
|
|
|
async function runTests() {
|
|
console.log('🧪 Starting Revolver & Admin Menu Test Suite...\n');
|
|
|
|
const testConfigPath = path.join(__dirname, 'data', 'test-revolver-config.json');
|
|
if (fs.existsSync(testConfigPath)) {
|
|
fs.unlinkSync(testConfigPath);
|
|
}
|
|
|
|
// TEST 1: ConfigStore Initialization and Persistence
|
|
console.log('1. Testing ConfigStore persistence & CRUD operations...');
|
|
const store = new ConfigStore(testConfigPath);
|
|
assert.strictEqual(fs.existsSync(testConfigPath), true, 'Config file should be created');
|
|
|
|
// Add Providers
|
|
const geminiProv = store.saveProvider({
|
|
name: 'Google AI Studio Main',
|
|
type: 'gemini',
|
|
keys: ['test-gemini-key-1', 'test-gemini-key-2']
|
|
});
|
|
assert.strictEqual(geminiProv.keys.length, 2, 'Should have 2 keys');
|
|
assert.strictEqual(geminiProv.type, 'gemini', 'Type should be gemini');
|
|
|
|
const groqProv = store.saveProvider({
|
|
name: 'Groq Cloud Backup',
|
|
type: 'groq',
|
|
keys: ['gsk-test-key-1']
|
|
});
|
|
assert.strictEqual(groqProv.type, 'groq');
|
|
assert.strictEqual(groqProv.baseUrl, 'https://api.groq.com/openai/v1');
|
|
|
|
// Verify retrieval
|
|
const providers = store.getProviders();
|
|
assert.strictEqual(providers.length, 2, 'Should have 2 providers');
|
|
|
|
// Add Custom Model with multi-tier fallback schema:
|
|
// [Main: Gemini Flash (revolves 2 keys) -> Fallback: Groq Llama 3.3 (1 key)]
|
|
const customModel = store.saveCustomModel({
|
|
name: 'smart-revolver-model',
|
|
description: 'High throughput Gemini with Groq fallback',
|
|
isDefault: true,
|
|
tiers: [
|
|
{
|
|
tierIndex: 0,
|
|
name: 'Primary Tier (Gemini Flash)',
|
|
targets: [
|
|
{ providerId: geminiProv.id, model: 'gemini-2.0-flash-exp' }
|
|
]
|
|
},
|
|
{
|
|
tierIndex: 1,
|
|
name: 'Fallback Tier 1 (Groq Llama)',
|
|
targets: [
|
|
{ providerId: groqProv.id, model: 'llama-3.3-70b-versatile' }
|
|
]
|
|
}
|
|
]
|
|
});
|
|
|
|
assert.strictEqual(customModel.name, 'smart-revolver-model');
|
|
assert.strictEqual(customModel.tiers.length, 2);
|
|
assert.strictEqual(store.getDefaultModel()?.id, customModel.id);
|
|
console.log(' ✓ ConfigStore CRUD and multi-tier model persistence verified.');
|
|
|
|
// TEST 2: Revolver Model Resolution
|
|
console.log('\n2. Testing RevolverEngine model resolution...');
|
|
const engine = new RevolverEngine(store);
|
|
// By exact name
|
|
const res1 = engine.resolveCustomModel('smart-revolver-model');
|
|
assert.strictEqual(res1.customModel.id, customModel.id);
|
|
|
|
// Fallback to default model if omitted or unknown
|
|
const res2 = engine.resolveCustomModel(undefined);
|
|
assert.strictEqual(res2.customModel.id, customModel.id);
|
|
const res3 = engine.resolveCustomModel('unknown-model-xyz');
|
|
assert.strictEqual(res3.customModel.id, customModel.id);
|
|
console.log(' ✓ Model resolution & default fallback verified.');
|
|
|
|
// TEST 3: Key Rotation and Rate-Limit Cooldown
|
|
console.log('\n3. Testing Key Status & Cooldowns...');
|
|
store.updateKeyStats(geminiProv.id, 'test-gemini-key-1', {
|
|
incrementCount: true,
|
|
rateLimitedUntil: Date.now() + 60000
|
|
});
|
|
|
|
const updatedProv = store.getProviderById(geminiProv.id)!;
|
|
const key1 = updatedProv.keys.find(k => k.key === 'test-gemini-key-1')!;
|
|
assert.strictEqual(key1.requestCount, 1);
|
|
assert.ok(key1.rateLimitedUntil && key1.rateLimitedUntil > Date.now());
|
|
console.log(' ✓ Key usage count and rate-limiting cooldown tracking verified.');
|
|
|
|
// TEST 4: Key Masking
|
|
console.log('\n4. Testing Key Masking for Admin Display...');
|
|
const masked1 = store.maskKey('AIzaSyAbcdef123456');
|
|
assert.strictEqual(masked1, 'AIza...3456');
|
|
const maskedShort = store.maskKey('short');
|
|
assert.strictEqual(maskedShort, '****');
|
|
console.log(' ✓ Key masking security verified.');
|
|
|
|
// Clean up test file
|
|
if (fs.existsSync(testConfigPath)) {
|
|
fs.unlinkSync(testConfigPath);
|
|
}
|
|
const tmpPath = `${testConfigPath}.tmp`;
|
|
if (fs.existsSync(tmpPath)) {
|
|
fs.unlinkSync(tmpPath);
|
|
}
|
|
|
|
console.log('\n✅ All unit and logic tests passed successfully!');
|
|
}
|
|
|
|
runTests().catch(err => {
|
|
console.error('❌ Test failed:', err);
|
|
process.exit(1);
|
|
});
|