163 lines
7 KiB
TypeScript
163 lines
7 KiB
TypeScript
import http from 'http';
|
|
import assert from 'assert';
|
|
import app from './src/index';
|
|
import { config } from './src/config';
|
|
|
|
async function runIntegrationTests() {
|
|
console.log('🧪 Starting HTTP Integration Tests...\n');
|
|
|
|
const server = http.createServer(app);
|
|
await new Promise<void>((resolve) => server.listen(0, resolve));
|
|
const port = (server.address() as any).port;
|
|
const baseUrl = `http://localhost:${port}`;
|
|
const validKey = config.apiPassword!;
|
|
|
|
try {
|
|
// 1. GET /admin UI & Subpages
|
|
console.log('1. Testing GET /admin redirect and dedicated subpages...');
|
|
const adminRedirectRes = await fetch(`${baseUrl}/admin`, { redirect: 'manual' });
|
|
assert.strictEqual(adminRedirectRes.status, 302, 'GET /admin should redirect to /admin/models');
|
|
assert.ok(adminRedirectRes.headers.get('location')?.endsWith('/admin/models'));
|
|
|
|
const subpages = [
|
|
{ path: '/admin/models', marker: 'Configured Custom Models' },
|
|
{ path: '/admin/providers', marker: 'API Providers & Key Pools' },
|
|
{ path: '/admin/playground', marker: 'Revolver Playground' },
|
|
{ path: '/admin/telemetry', marker: 'Live Provider & Key Health' },
|
|
{ path: '/admin/live_logs', marker: 'Live Request Log Watcher' }
|
|
];
|
|
|
|
for (const sp of subpages) {
|
|
const spRes = await fetch(`${baseUrl}${sp.path}`);
|
|
assert.strictEqual(spRes.status, 200, `Subpage ${sp.path} should return 200`);
|
|
const spHtml = await spRes.text();
|
|
assert.ok(spHtml.includes('GeminiRevolver — Admin Console'), `${sp.path} should render Admin Console layout`);
|
|
assert.ok(spHtml.includes('auth-gate-section'), `${sp.path} should include auth gate`);
|
|
assert.ok(spHtml.includes(sp.marker), `${sp.path} should include page marker '${sp.marker}'`);
|
|
}
|
|
|
|
// Verify Live Logs inspector tabs do NOT have the .tab-btn class collision
|
|
const liveLogsHtml = await (await fetch(`${baseUrl}/admin/live_logs`)).text();
|
|
assert.ok(liveLogsHtml.includes('class="insp-tab-btn active" data-insp-tab="insp-input"'), 'Inspector tabs must use isolated .insp-tab-btn class');
|
|
assert.ok(!liveLogsHtml.includes('tab-btn active insp-tab-btn'), 'Inspector tabs must not collide with .tab-btn');
|
|
console.log(' ✓ Admin root redirect and all 5 subpages (/admin/models, /admin/providers, /admin/playground, /admin/telemetry, /admin/live_logs) verified.');
|
|
|
|
// 2. Auth tests on /api/admin/verify
|
|
console.log('\n2. Testing /api/admin/verify authentication...');
|
|
const noAuth = await fetch(`${baseUrl}/api/admin/verify`);
|
|
assert.strictEqual(noAuth.status, 401, 'Should fail without auth');
|
|
|
|
const badAuth = await fetch(`${baseUrl}/api/admin/verify`, {
|
|
headers: { 'Authorization': 'Bearer wrong_password' }
|
|
});
|
|
assert.strictEqual(badAuth.status, 401, 'Should fail with bad key');
|
|
|
|
const goodAuth = await fetch(`${baseUrl}/api/admin/verify`, {
|
|
headers: { 'Authorization': `Bearer ${validKey}` }
|
|
});
|
|
assert.strictEqual(goodAuth.status, 200, 'Should succeed with valid API password');
|
|
const goodJson = await goodAuth.json() as any;
|
|
assert.strictEqual(goodJson.valid, true);
|
|
console.log(' ✓ Admin authentication verification verified.');
|
|
|
|
// 3. Provider CRUD via API
|
|
console.log('\n3. Testing Provider CRUD via /api/admin/providers...');
|
|
const createProvRes = await fetch(`${baseUrl}/api/admin/providers`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': `Bearer ${validKey}`,
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
name: 'Groq Primary Integration Test',
|
|
type: 'groq',
|
|
baseUrl: 'https://api.groq.com/openai/v1',
|
|
keys: ['gsk_live_test_key_1', 'gsk_live_test_key_2']
|
|
})
|
|
});
|
|
assert.strictEqual(createProvRes.status, 200);
|
|
const createdProv = await createProvRes.json() as any;
|
|
assert.strictEqual(createdProv.name, 'Groq Primary Integration Test');
|
|
assert.strictEqual(createdProv.keys.length, 2);
|
|
|
|
// Config endpoint test
|
|
const configRes = await fetch(`${baseUrl}/api/admin/config`, {
|
|
headers: { 'Authorization': `Bearer ${validKey}` }
|
|
});
|
|
const configData = await configRes.json() as any;
|
|
const fetchedProv = configData.providers.find((p: any) => p.id === createdProv.id);
|
|
assert.ok(fetchedProv);
|
|
assert.strictEqual(fetchedProv.keys[0].key, 'gsk_...ey_1', 'Keys should be masked in normal view');
|
|
|
|
console.log(' ✓ Provider creation and masked retrieval verified.');
|
|
|
|
// 4. Custom Model CRUD via API
|
|
console.log('\n4. Testing Custom Model CRUD via /api/admin/models...');
|
|
const createModelRes = await fetch(`${baseUrl}/api/admin/models`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': `Bearer ${validKey}`,
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
name: 'integration-custom-model',
|
|
description: 'Model created during integration testing',
|
|
isDefault: true,
|
|
tiers: [
|
|
{
|
|
tierIndex: 0,
|
|
name: 'Tier 0: Main',
|
|
targets: [
|
|
{ providerId: createdProv.id, model: 'llama-3.3-70b-versatile' }
|
|
]
|
|
}
|
|
]
|
|
})
|
|
});
|
|
assert.strictEqual(createModelRes.status, 200);
|
|
const createdModel = await createModelRes.json() as any;
|
|
assert.strictEqual(createdModel.name, 'integration-custom-model');
|
|
assert.strictEqual(createdModel.isDefault, true);
|
|
|
|
// Test GET /v1/models (Client perspective)
|
|
console.log('\n5. Testing GET /v1/models reflects custom models...');
|
|
const v1ModelsRes = await fetch(`${baseUrl}/v1/models`, {
|
|
headers: { 'Authorization': `Bearer ${validKey}` }
|
|
});
|
|
assert.strictEqual(v1ModelsRes.status, 200);
|
|
const v1Models = await v1ModelsRes.json() as any;
|
|
const foundModel = v1Models.data.find((m: any) => m.id === 'integration-custom-model');
|
|
assert.ok(foundModel, 'Created custom model must appear in GET /v1/models');
|
|
console.log(' ✓ Custom model successfully exposed to OpenAI clients via /v1/models.');
|
|
|
|
// 6. Keys status telemetry endpoint
|
|
console.log('\n6. Testing GET /api/admin/keys-status...');
|
|
const statusRes = await fetch(`${baseUrl}/api/admin/keys-status`, {
|
|
headers: { 'Authorization': `Bearer ${validKey}` }
|
|
});
|
|
assert.strictEqual(statusRes.status, 200);
|
|
const statusData = await statusRes.json() as any;
|
|
assert.ok(Array.isArray(statusData.providers));
|
|
console.log(' ✓ Live key status telemetry verified.');
|
|
|
|
// Clean up created entities
|
|
await fetch(`${baseUrl}/api/admin/models/${createdModel.id}`, {
|
|
method: 'DELETE',
|
|
headers: { 'Authorization': `Bearer ${validKey}` }
|
|
});
|
|
await fetch(`${baseUrl}/api/admin/providers/${createdProv.id}`, {
|
|
method: 'DELETE',
|
|
headers: { 'Authorization': `Bearer ${validKey}` }
|
|
});
|
|
console.log(' ✓ Cleanup completed.');
|
|
|
|
console.log('\n🎉 ALL HTTP INTEGRATION TESTS PASSED!');
|
|
} finally {
|
|
server.close();
|
|
}
|
|
}
|
|
|
|
runIntegrationTests().catch(err => {
|
|
console.error('❌ Integration test failed:', err);
|
|
process.exit(1);
|
|
});
|