68 lines
2.3 KiB
JavaScript
68 lines
2.3 KiB
JavaScript
|
|
#!/usr/bin/env node
|
||
|
|
|
||
|
|
// Simple test script to demonstrate the Home Server Agent functionality
|
||
|
|
import { execSync } from 'child_process';
|
||
|
|
|
||
|
|
const API_BASE = 'http://localhost:3000';
|
||
|
|
const API_TOKEN = 'dev-token-123';
|
||
|
|
|
||
|
|
console.log('🎮 Home Server Agent Test Script\n');
|
||
|
|
|
||
|
|
// Helper function to make API requests
|
||
|
|
function makeRequest(endpoint, method = 'GET', needsAuth = true) {
|
||
|
|
const url = `${API_BASE}${endpoint}`;
|
||
|
|
let cmd;
|
||
|
|
|
||
|
|
if (process.platform === 'win32') {
|
||
|
|
// Windows PowerShell
|
||
|
|
if (needsAuth) {
|
||
|
|
cmd = `powershell "Invoke-RestMethod -Uri '${url}' -Method ${method} -Headers @{'Authorization'='Bearer ${API_TOKEN}'}"`;
|
||
|
|
} else {
|
||
|
|
cmd = `powershell "Invoke-RestMethod -Uri '${url}' -Method ${method}"`;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
// Unix-like systems
|
||
|
|
if (needsAuth) {
|
||
|
|
cmd = `curl -s -X ${method} -H "Authorization: Bearer ${API_TOKEN}" ${url}`;
|
||
|
|
} else {
|
||
|
|
cmd = `curl -s -X ${method} ${url}`;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
const result = execSync(cmd, { encoding: 'utf8' });
|
||
|
|
return result;
|
||
|
|
} catch (error) {
|
||
|
|
console.error(`Error making request to ${endpoint}:`, error.message);
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Test sequence
|
||
|
|
console.log('1. Testing health check (no auth required)...');
|
||
|
|
const healthResult = makeRequest('/health', 'GET', false);
|
||
|
|
console.log(' ✅ Health check result:', healthResult ? 'OK' : 'FAILED');
|
||
|
|
|
||
|
|
console.log('\n2. Testing server status...');
|
||
|
|
const statusResult = makeRequest('/api/status');
|
||
|
|
console.log(' ✅ Server status retrieved');
|
||
|
|
|
||
|
|
console.log('\n3. Testing game server list...');
|
||
|
|
const listResult = makeRequest('/api/gameserver/list');
|
||
|
|
console.log(' ✅ Available game servers retrieved');
|
||
|
|
|
||
|
|
console.log('\n4. Testing API documentation...');
|
||
|
|
const rootResult = makeRequest('/', 'GET', false);
|
||
|
|
console.log(' ✅ API documentation retrieved');
|
||
|
|
|
||
|
|
console.log('\n5. Testing specific game server status...');
|
||
|
|
const minecraftStatus = makeRequest('/api/gameserver/minecraft/status');
|
||
|
|
console.log(' ✅ Minecraft server status retrieved');
|
||
|
|
|
||
|
|
console.log('\n🎉 All tests completed successfully!');
|
||
|
|
console.log('\nNext steps:');
|
||
|
|
console.log('- Start a game server: POST /api/gameserver/start/minecraft');
|
||
|
|
console.log('- Stop a game server: POST /api/gameserver/stop/minecraft');
|
||
|
|
console.log('- Check active ports: GET /api/status/ports');
|
||
|
|
console.log('\nNote: Game servers require Docker to be running.');
|