udphole

Basic UDP wormhole proxy
git clone git://git.finwo.net/app/udphole
Log | Files | Refs | README | LICENSE

system-commands.js (3734B)


      1 const path = require('path');
      2 const {
      3   spawnDaemon,
      4   killDaemon,
      5   killAllDaemons,
      6   connectApi,
      7   apiCommand
      8 } = require('./helpers');
      9 
     10 const CONFIG_PATH = path.join(__dirname, 'config-tcp.ini');
     11 const API_PORT = 9123;
     12 
     13 async function runTest() {
     14   let daemon = null;
     15   let apiSock = null;
     16   let returnCode = 0;
     17 
     18   console.log('=== System Commands Test ===');
     19   console.log('Testing: system.load and session.count commands\n');
     20 
     21   try {
     22     console.log('1. Spawning daemon...');
     23     daemon = await spawnDaemon(CONFIG_PATH);
     24     console.log(`   Daemon started (PID: ${daemon.pid})`);
     25 
     26     console.log('2. Connecting to API...');
     27     apiSock = await connectApi(API_PORT);
     28     console.log('   Connected to API');
     29 
     30     console.log('3. Authenticating...');
     31     let resp = await apiCommand(apiSock, 'auth', 'finwo', 'testsecret');
     32     console.log(`   Auth response: ${resp}`);
     33     if (resp !== 'OK') throw new Error('Authentication failed');
     34 
     35     console.log('4. Testing session.count (no sessions)...');
     36     resp = await apiCommand(apiSock, 'session.count');
     37     console.log(`   session.count: ${resp}`);
     38     if (typeof resp !== 'number' || resp !== 0) {
     39       throw new Error(`Expected session.count = 0, got ${resp}`);
     40     }
     41 
     42     console.log('5. Creating a session...');
     43     resp = await apiCommand(apiSock, 'session.create', 'test-session', '60');
     44     console.log(`   Session create: ${resp}`);
     45 
     46     console.log('6. Testing session.count (with 1 session)...');
     47     resp = await apiCommand(apiSock, 'session.count');
     48     console.log(`   session.count: ${resp}`);
     49     if (typeof resp !== 'number' || resp !== 1) {
     50       throw new Error(`Expected session.count = 1, got ${resp}`);
     51     }
     52 
     53     console.log('7. Creating another session...');
     54     resp = await apiCommand(apiSock, 'session.create', 'test-session-2', '60');
     55     console.log(`   Session create: ${resp}`);
     56 
     57     console.log('8. Testing session.count (with 2 sessions)...');
     58     resp = await apiCommand(apiSock, 'session.count');
     59     console.log(`   session.count: ${resp}`);
     60     if (typeof resp !== 'number' || resp !== 2) {
     61       throw new Error(`Expected session.count = 2, got ${resp}`);
     62     }
     63 
     64     console.log('9. Testing system.load...');
     65     resp = await apiCommand(apiSock, 'system.load');
     66     console.log(`   system.load: ${JSON.stringify(resp)}`);
     67     if (!Array.isArray(resp) || resp.length !== 6) {
     68       throw new Error(`Expected array with 6 elements, got ${JSON.stringify(resp)}`);
     69     }
     70     if (resp[0] !== '1min' || resp[2] !== '5min' || resp[4] !== '15min') {
     71       throw new Error(`Expected keys [1min, 5min, 15min], got ${JSON.stringify(resp)}`);
     72     }
     73     const load1 = parseFloat(resp[1]);
     74     const load5 = parseFloat(resp[3]);
     75     const load15 = parseFloat(resp[5]);
     76     console.log(`   Parsed loads: 1min=${load1}, 5min=${load5}, 15min=${load15}`);
     77     if (isNaN(load1) || isNaN(load5) || isNaN(load15)) {
     78       throw new Error('Load values should be valid numbers');
     79     }
     80 
     81     console.log('10. Destroying a session...');
     82     resp = await apiCommand(apiSock, 'session.destroy', 'test-session');
     83     console.log(`   Session destroy: ${resp}`);
     84 
     85     console.log('11. Testing session.count (after destroy)...');
     86     resp = await apiCommand(apiSock, 'session.count');
     87     console.log(`   session.count: ${resp}`);
     88     if (typeof resp !== 'number' || resp !== 1) {
     89       throw new Error(`Expected session.count = 1, got ${resp}`);
     90     }
     91 
     92     console.log('\n✓ PASS: All system commands work correctly');
     93 
     94   } catch (err) {
     95     console.error(`\n✗ FAIL: ${err.message}`);
     96     returnCode = 1;
     97   } finally {
     98     if (apiSock) apiSock.end();
     99     if (daemon) await killDaemon(daemon);
    100     await killAllDaemons();
    101     process.exit(returnCode);
    102   }
    103 }
    104 
    105 runTest();