EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
test-command-execution.js
Go to the documentation of this file.
1/**
2 * Test script to verify command execution works end-to-end
3 * Tests the execute_script functionality added to agent v2
4 */
5
6const WebSocket = require('ws');
7
8// Agent UUID from the VM
9const agentUuid = '2c3e2d7a-c521-4844-b44b-a30b4c2ec4d8';
10const wsUrl = 'wss://rmm-psa-backend-t9f7k.ondigitalocean.app/api/ws';
11
12console.log('Connecting to WebSocket as backend...');
13const ws = new WebSocket(`${wsUrl}?agent_uuid=${agentUuid}`);
14
15ws.on('open', () => {
16 console.log('āœ… Connected to WebSocket');
17
18 // Send a test script execution
19 const testCommand = {
20 type: 'execute_script',
21 execution_id: 'test-' + Date.now(),
22 script_id: 'test-script-1',
23 code: 'echo "Hello from Agent v2!"; echo "Current directory: $PWD"; whoami'
24 };
25
26 console.log('\nšŸ“¤ Sending command execution:', testCommand);
27 ws.send(JSON.stringify(testCommand));
28
29 // Timeout after 30 seconds
30 setTimeout(() => {
31 console.log('\nā±ļø Test timeout - closing connection');
32 ws.close();
33 process.exit(0);
34 }, 30000);
35});
36
37ws.on('message', (data) => {
38 try {
39 const message = JSON.parse(data.toString());
40
41 switch (message.type) {
42 case 'script_output':
43 console.log(`šŸ“ Output [${message.execution_id}]:`, message.line);
44 break;
45
46 case 'script_complete':
47 console.log(`āœ… Script completed [${message.execution_id}] - Exit code: ${message.exit_code}`);
48 setTimeout(() => {
49 ws.close();
50 process.exit(0);
51 }, 1000);
52 break;
53
54 case 'script_error':
55 console.log(`āŒ Script error [${message.execution_id}]:`, message.error);
56 setTimeout(() => {
57 ws.close();
58 process.exit(1);
59 }, 1000);
60 break;
61
62 default:
63 console.log('šŸ“Ø Received:', message.type);
64 }
65 } catch (err) {
66 console.error('Error parsing message:', err);
67 }
68});
69
70ws.on('error', (error) => {
71 console.error('āŒ WebSocket error:', error.message);
72 process.exit(1);
73});
74
75ws.on('close', () => {
76 console.log('šŸ”Œ Connection closed');
77});