-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_host.js
More file actions
72 lines (57 loc) · 2.42 KB
/
test_host.js
File metadata and controls
72 lines (57 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env node
// test_host.js - Test script for host.js using Chrome's native messaging protocol
const { spawn } = require('child_process');
function testHost() {
console.log('🧪 Testing host.js with ping message...');
const hostPath = '/Users/david.fattal/Documents/Coding/lif_ldi_mac_0.1/host.js';
const host = spawn('node', [hostPath], {
stdio: ['pipe', 'pipe', 'inherit']
});
// Create ping message in Chrome's native messaging format
const message = JSON.stringify({ type: 'ping' });
const messageBuffer = Buffer.from(message);
const lengthBuffer = Buffer.alloc(4);
lengthBuffer.writeUInt32LE(messageBuffer.length, 0);
console.log(`📤 Sending ping message (${messageBuffer.length} bytes): ${message}`);
// Send length prefix + message
host.stdin.write(lengthBuffer);
host.stdin.write(messageBuffer);
// Read response
let responseData = Buffer.alloc(0);
host.stdout.on('data', (data) => {
responseData = Buffer.concat([responseData, data]);
// Try to read complete response
if (responseData.length >= 4) {
const responseLength = responseData.readUInt32LE(0);
if (responseData.length >= 4 + responseLength) {
const responseMessage = responseData.slice(4, 4 + responseLength).toString();
console.log(`📥 Received response (${responseLength} bytes): ${responseMessage}`);
try {
const parsed = JSON.parse(responseMessage);
if (parsed.pong) {
console.log('✅ SUCCESS: Host responded with pong!');
} else if (parsed.error) {
console.log('⚠️ WARNING: Host responded with error:', parsed.error);
} else {
console.log('❓ UNEXPECTED: Host response:', parsed);
}
} catch (e) {
console.log('❌ ERROR: Could not parse response JSON');
}
host.kill();
}
}
});
host.on('error', (error) => {
console.log('❌ ERROR: Failed to spawn host:', error.message);
});
host.on('exit', (code) => {
console.log(`🔚 Host exited with code: ${code}`);
});
// Timeout
setTimeout(() => {
console.log('⏰ TIMEOUT: No response after 5 seconds');
host.kill();
}, 5000);
}
testHost();