@cldmv/node-android-tv-remote 1.5.4 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +109 -9
- package/index.cjs +31 -0
- package/index.mjs +25 -0
- package/package.json +14 -5
- package/scripts/{postinstall.js → postinstall.mjs} +2 -2
- package/scripts/{setup-device.js → setup-device.mjs} +3 -3
- package/src/lib/adb/setup.mjs +291 -0
- package/src/lib/{android-tv-remote.js → android-tv-remote.mjs} +389 -146
- package/test/{android-tv-remote.test.js → android-tv-remote.test.mjs} +14 -1
- package/test/{api-surface.test.js → api-surface.test.mjs} +16 -3
- package/test/connect-readall.mjs +130 -0
- package/test/dual/device1.mjs +137 -0
- package/test/dual/device2.mjs +137 -0
- package/test/dual/spawn-dual-servers.mjs +214 -0
- package/test/dual-device-ensureawake.mjs +236 -0
- package/test/event-system-demo.mjs +106 -0
- package/test/{interactive-remote.js → interactive-remote.mjs} +16 -3
- package/test/retry-init-adb.test.mjs +78 -0
- package/scripts/test-keycodes.js +0 -0
- package/src/lib/adb/setup.js +0 -144
- package/test/connect-readall.js +0 -35
|
@@ -1,4 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @Project: @cldmv/node-android-tv-remote
|
|
3
|
+
* @Filename: /test/android-tv-remote.test.mjs
|
|
4
|
+
* @Date: 2025-10-15 10:19:05 -07:00 (1760548745)
|
|
5
|
+
* @Author: Nate Hyson <CLDMV>
|
|
6
|
+
* @Email: <Shinrai@users.noreply.github.com>
|
|
7
|
+
* -----
|
|
8
|
+
* @Last modified by: Nate Hyson <CLDMV> (Shinrai@users.noreply.github.com)
|
|
9
|
+
* @Last modified time: 2025-10-15 10:57:20 -07:00 (1760551040)
|
|
10
|
+
* -----
|
|
11
|
+
* @Copyright: Copyright (c) 2013-2025 Catalyzed Motivation Inc. All rights reserved.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import Remote from "../src/lib/android-tv-remote.mjs";
|
|
2
15
|
|
|
3
16
|
// Usage: Set ANDROID_TV_IP and ANDROID_TV_PORT env vars before running Jest
|
|
4
17
|
const TEST_IP = process.env.ANDROID_TV_IP || "192.168.1.100";
|
|
@@ -1,11 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @Project: @cldmv/node-android-tv-remote
|
|
3
|
+
* @Filename: /test/api-surface.test.mjs
|
|
4
|
+
* @Date: 2025-10-15 10:19:05 -07:00 (1760548745)
|
|
5
|
+
* @Author: Nate Hyson <CLDMV>
|
|
6
|
+
* @Email: <Shinrai@users.noreply.github.com>
|
|
7
|
+
* -----
|
|
8
|
+
* @Last modified by: Nate Hyson <CLDMV> (Shinrai@users.noreply.github.com)
|
|
9
|
+
* @Last modified time: 2025-10-15 10:56:52 -07:00 (1760551012)
|
|
10
|
+
* -----
|
|
11
|
+
* @Copyright: Copyright (c) 2013-2025 Catalyzed Motivation Inc. All rights reserved.
|
|
12
|
+
*/
|
|
13
|
+
|
|
1
14
|
/**
|
|
2
15
|
* Test for getKeyboardKeys and getPressCommands methods of the android-tv-remote module.
|
|
3
16
|
*
|
|
4
|
-
* Usage: Run with `node test/api-surface.test.
|
|
17
|
+
* Usage: Run with `node test/api-surface.test.mjs`
|
|
5
18
|
*/
|
|
6
19
|
|
|
7
|
-
|
|
8
|
-
|
|
20
|
+
import assert from "assert";
|
|
21
|
+
import createRemote from "../src/lib/android-tv-remote.mjs";
|
|
9
22
|
|
|
10
23
|
// Minimal config for instantiation (no real device needed for API surface test)
|
|
11
24
|
const remote = createRemote({ ip: "127.0.0.1", autoConnect: false, maintainConnection: false, quiet: true });
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @Project: @cldmv/node-android-tv-remote
|
|
3
|
+
* @Filename: /test/connect-readall.mjs
|
|
4
|
+
* @Date: 2025-10-15 10:19:05 -07:00 (1760548745)
|
|
5
|
+
* @Author: Nate Hyson <CLDMV>
|
|
6
|
+
* @Email: <Shinrai@users.noreply.github.com>
|
|
7
|
+
* -----
|
|
8
|
+
* @Last modified by: Nate Hyson <CLDMV> (Shinrai@users.noreply.github.com)
|
|
9
|
+
* @Last modified time: 2025-10-15 12:06:51 -07:00 (1760555211)
|
|
10
|
+
* -----
|
|
11
|
+
* @Copyright: Copyright (c) 2013-2025 Catalyzed Motivation Inc. All rights reserved.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
// Test script to connect to an ADB device and read settings.
|
|
15
|
+
// Usage: node test/connect-readall.mjs <ip> [port] [get|set] [--quiet]
|
|
16
|
+
|
|
17
|
+
import AndroidTVSetup from "../src/lib/adb/setup.mjs";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Parse and validate command line arguments with better error handling.
|
|
21
|
+
* @returns {Object} Parsed configuration object
|
|
22
|
+
*/
|
|
23
|
+
function parseArgs() {
|
|
24
|
+
const args = process.argv.slice(2);
|
|
25
|
+
const config = {
|
|
26
|
+
ip: null,
|
|
27
|
+
port: 5555,
|
|
28
|
+
mode: "get",
|
|
29
|
+
quiet: false
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// Check for help flag
|
|
33
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
34
|
+
console.log(`
|
|
35
|
+
Android TV Remote Test Script
|
|
36
|
+
|
|
37
|
+
Usage: node test/connect-readall.mjs <ip> [port] [mode] [options]
|
|
38
|
+
|
|
39
|
+
Arguments:
|
|
40
|
+
ip Device IP address (required)
|
|
41
|
+
port Device port number (optional, default: 5555)
|
|
42
|
+
mode Operation mode: "get" or "set" (optional, default: "get")
|
|
43
|
+
|
|
44
|
+
Options:
|
|
45
|
+
--quiet Suppress log output
|
|
46
|
+
--help, -h Show this help message
|
|
47
|
+
|
|
48
|
+
Examples:
|
|
49
|
+
node test/connect-readall.mjs 192.168.1.100
|
|
50
|
+
node test/connect-readall.mjs 192.168.1.100 5555 set
|
|
51
|
+
node test/connect-readall.mjs 192.168.1.100 5555 get --quiet
|
|
52
|
+
`);
|
|
53
|
+
process.exit(0);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Parse IP address (required)
|
|
57
|
+
if (args.length === 0 || !args[0] || args[0].startsWith("--")) {
|
|
58
|
+
console.error("Error: IP address is required");
|
|
59
|
+
console.error("Usage: node test/connect-readall.mjs <ip> [port] [get|set] [--quiet]");
|
|
60
|
+
console.error("Use --help for more information");
|
|
61
|
+
process.exit(1);
|
|
62
|
+
}
|
|
63
|
+
config.ip = args[0];
|
|
64
|
+
|
|
65
|
+
// Parse port (optional)
|
|
66
|
+
if (args[1] && !args[1].startsWith("--") && !["get", "set"].includes(args[1])) {
|
|
67
|
+
const portNum = parseInt(args[1], 10);
|
|
68
|
+
if (isNaN(portNum) || portNum < 1 || portNum > 65535) {
|
|
69
|
+
console.error(`Error: Invalid port number "${args[1]}". Port must be between 1 and 65535`);
|
|
70
|
+
process.exit(1);
|
|
71
|
+
}
|
|
72
|
+
config.port = portNum;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Parse mode (optional)
|
|
76
|
+
const modeArg = args.find((arg) => ["get", "set"].includes(arg));
|
|
77
|
+
if (modeArg) {
|
|
78
|
+
config.mode = modeArg;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Parse quiet flag
|
|
82
|
+
config.quiet = args.includes("--quiet");
|
|
83
|
+
|
|
84
|
+
return config;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function main() {
|
|
88
|
+
const config = parseArgs();
|
|
89
|
+
const { ip, port, mode, quiet } = config;
|
|
90
|
+
|
|
91
|
+
if (!quiet) {
|
|
92
|
+
console.log(`Running in ${mode} mode on ${ip}:${port}`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const setup = new AndroidTVSetup({ ip, port, quiet, autoConnect: false });
|
|
96
|
+
|
|
97
|
+
// Set up event listeners for proper error handling
|
|
98
|
+
setup.on("error", (errorData) => {
|
|
99
|
+
console.error(`Error from ${errorData.source}:`, errorData.message);
|
|
100
|
+
if (errorData.error && errorData.error.code === "ENOENT") {
|
|
101
|
+
console.error("\nADB is not installed or not in your PATH.");
|
|
102
|
+
console.error("Please install ADB first: https://developer.android.com/studio/releases/platform-tools");
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
setup.on("log", (logData) => {
|
|
107
|
+
if (!quiet || logData.level === "error") {
|
|
108
|
+
const prefix = logData.level === "error" ? "ERROR" : logData.level === "warn" ? "WARN" : "INFO";
|
|
109
|
+
console.log(`[${prefix}] ${logData.message}`);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
await setup.connect();
|
|
115
|
+
if (mode === "set") {
|
|
116
|
+
await setup.setSettings();
|
|
117
|
+
}
|
|
118
|
+
await setup.ensureAwake();
|
|
119
|
+
await setup.disconnect();
|
|
120
|
+
|
|
121
|
+
if (!quiet) {
|
|
122
|
+
console.log(`\nCompleted ${mode} mode on ${ip}:${port}`);
|
|
123
|
+
}
|
|
124
|
+
} catch (err) {
|
|
125
|
+
console.error("Fatal error:", err.message || err);
|
|
126
|
+
process.exit(1);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
main();
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Individual device test script for 10.6.0.133
|
|
3
|
+
* This simulates a single server process managing one Android TV device.
|
|
4
|
+
*
|
|
5
|
+
* Usage: node test/dual/device1.mjs
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import AndroidTVSetup from "../../src/lib/adb/setup.mjs";
|
|
9
|
+
|
|
10
|
+
const DEVICE_IP = "10.6.0.133";
|
|
11
|
+
const DEVICE_PORT = 5555;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Enhanced logger with process identification
|
|
15
|
+
* @param {string} level - Log level
|
|
16
|
+
* @param {string} message - Log message
|
|
17
|
+
* @param {any} data - Additional data
|
|
18
|
+
*/
|
|
19
|
+
const log = (level, message, data = null) => {
|
|
20
|
+
const timestamp = new Date().toISOString();
|
|
21
|
+
const processId = `PID:${process.pid}`;
|
|
22
|
+
const prefix = `[${timestamp}] [${processId}] [${DEVICE_IP}] [${level.toUpperCase()}]`;
|
|
23
|
+
console.log(`${prefix} ${message}`);
|
|
24
|
+
if (data) {
|
|
25
|
+
console.log(`${prefix} Data:`, JSON.stringify(data, null, 2));
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Main test function for device 1
|
|
31
|
+
*/
|
|
32
|
+
const testDevice1 = async () => {
|
|
33
|
+
const startTime = Date.now();
|
|
34
|
+
let setup = null;
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
log("INFO", "=== Device 1 Server Starting ===");
|
|
38
|
+
log("INFO", `Process ID: ${process.pid}`);
|
|
39
|
+
log("INFO", `Target Device: ${DEVICE_IP}:${DEVICE_PORT}`);
|
|
40
|
+
|
|
41
|
+
// Create setup with comprehensive logging
|
|
42
|
+
setup = new AndroidTVSetup({
|
|
43
|
+
ip: DEVICE_IP,
|
|
44
|
+
port: DEVICE_PORT,
|
|
45
|
+
quiet: false,
|
|
46
|
+
autoConnect: false
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Set up event listeners
|
|
50
|
+
setup.on("log", (logData) => {
|
|
51
|
+
log(logData.level, logData.message, logData.data);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
setup.on("error", (errorData) => {
|
|
55
|
+
log("ERROR", `${errorData.source}: ${errorData.message}`, {
|
|
56
|
+
error: errorData.error.message,
|
|
57
|
+
stack: errorData.error.stack
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Add remote event listeners
|
|
62
|
+
if (setup.remote) {
|
|
63
|
+
setup.remote.on("log", (logData) => {
|
|
64
|
+
log(logData.level, `[REMOTE] ${logData.message}`, logData.data);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
setup.remote.on("error", (errorData) => {
|
|
68
|
+
log("ERROR", `[REMOTE] ${errorData.source}: ${errorData.message}`, {
|
|
69
|
+
error: errorData.error.message
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Perform the test sequence
|
|
75
|
+
log("INFO", "Connecting to device...");
|
|
76
|
+
await setup.connect();
|
|
77
|
+
|
|
78
|
+
log("INFO", "Ensuring device is awake...");
|
|
79
|
+
await setup.ensureAwake();
|
|
80
|
+
|
|
81
|
+
// Keep connection alive for a moment to simulate server behavior
|
|
82
|
+
log("INFO", "Simulating server operation (holding connection for 2 seconds)...");
|
|
83
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
84
|
+
|
|
85
|
+
log("INFO", "Disconnecting from device...");
|
|
86
|
+
await setup.disconnect();
|
|
87
|
+
|
|
88
|
+
const totalTime = Date.now() - startTime;
|
|
89
|
+
log("SUCCESS", `=== Device 1 Server Completed Successfully (${totalTime}ms) ===`);
|
|
90
|
+
|
|
91
|
+
process.exit(0);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
const totalTime = Date.now() - startTime;
|
|
94
|
+
log("ERROR", `=== Device 1 Server Failed (${totalTime}ms) ===`, {
|
|
95
|
+
error: error.message,
|
|
96
|
+
stack: error.stack
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// Attempt cleanup
|
|
100
|
+
if (setup) {
|
|
101
|
+
try {
|
|
102
|
+
log("INFO", "Attempting cleanup disconnect...");
|
|
103
|
+
await setup.disconnect();
|
|
104
|
+
log("INFO", "Cleanup successful");
|
|
105
|
+
} catch (cleanupError) {
|
|
106
|
+
log("WARN", "Cleanup failed", { error: cleanupError.message });
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
process.exit(1);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// Handle process signals and errors
|
|
115
|
+
process.on("SIGINT", () => {
|
|
116
|
+
log("INFO", "Received SIGINT, shutting down gracefully...");
|
|
117
|
+
process.exit(0);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
process.on("SIGTERM", () => {
|
|
121
|
+
log("INFO", "Received SIGTERM, shutting down gracefully...");
|
|
122
|
+
process.exit(0);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
process.on("unhandledRejection", (reason, promise) => {
|
|
126
|
+
log("ERROR", "Unhandled Rejection", { reason: reason.message, promise });
|
|
127
|
+
process.exit(1);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
process.on("uncaughtException", (error) => {
|
|
131
|
+
log("ERROR", "Uncaught Exception", { error: error.message, stack: error.stack });
|
|
132
|
+
process.exit(1);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// Start the test
|
|
136
|
+
log("INFO", "Starting Device 1 test process...");
|
|
137
|
+
testDevice1();
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Individual device test script for 10.6.0.18
|
|
3
|
+
* This simulates a single server process managing one Android TV device.
|
|
4
|
+
*
|
|
5
|
+
* Usage: node test/dual/device2.mjs
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import AndroidTVSetup from "../../src/lib/adb/setup.mjs";
|
|
9
|
+
|
|
10
|
+
const DEVICE_IP = "10.6.0.18";
|
|
11
|
+
const DEVICE_PORT = 5555;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Enhanced logger with process identification
|
|
15
|
+
* @param {string} level - Log level
|
|
16
|
+
* @param {string} message - Log message
|
|
17
|
+
* @param {any} data - Additional data
|
|
18
|
+
*/
|
|
19
|
+
const log = (level, message, data = null) => {
|
|
20
|
+
const timestamp = new Date().toISOString();
|
|
21
|
+
const processId = `PID:${process.pid}`;
|
|
22
|
+
const prefix = `[${timestamp}] [${processId}] [${DEVICE_IP}] [${level.toUpperCase()}]`;
|
|
23
|
+
console.log(`${prefix} ${message}`);
|
|
24
|
+
if (data) {
|
|
25
|
+
console.log(`${prefix} Data:`, JSON.stringify(data, null, 2));
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Main test function for device 2
|
|
31
|
+
*/
|
|
32
|
+
const testDevice2 = async () => {
|
|
33
|
+
const startTime = Date.now();
|
|
34
|
+
let setup = null;
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
log("INFO", "=== Device 2 Server Starting ===");
|
|
38
|
+
log("INFO", `Process ID: ${process.pid}`);
|
|
39
|
+
log("INFO", `Target Device: ${DEVICE_IP}:${DEVICE_PORT}`);
|
|
40
|
+
|
|
41
|
+
// Create setup with comprehensive logging
|
|
42
|
+
setup = new AndroidTVSetup({
|
|
43
|
+
ip: DEVICE_IP,
|
|
44
|
+
port: DEVICE_PORT,
|
|
45
|
+
quiet: false,
|
|
46
|
+
autoConnect: false
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Set up event listeners
|
|
50
|
+
setup.on("log", (logData) => {
|
|
51
|
+
log(logData.level, logData.message, logData.data);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
setup.on("error", (errorData) => {
|
|
55
|
+
log("ERROR", `${errorData.source}: ${errorData.message}`, {
|
|
56
|
+
error: errorData.error.message,
|
|
57
|
+
stack: errorData.error.stack
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Add remote event listeners
|
|
62
|
+
if (setup.remote) {
|
|
63
|
+
setup.remote.on("log", (logData) => {
|
|
64
|
+
log(logData.level, `[REMOTE] ${logData.message}`, logData.data);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
setup.remote.on("error", (errorData) => {
|
|
68
|
+
log("ERROR", `[REMOTE] ${errorData.source}: ${errorData.message}`, {
|
|
69
|
+
error: errorData.error.message
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Perform the test sequence
|
|
75
|
+
log("INFO", "Connecting to device...");
|
|
76
|
+
await setup.connect();
|
|
77
|
+
|
|
78
|
+
log("INFO", "Ensuring device is awake...");
|
|
79
|
+
await setup.ensureAwake();
|
|
80
|
+
|
|
81
|
+
// Keep connection alive for a moment to simulate server behavior
|
|
82
|
+
log("INFO", "Simulating server operation (holding connection for 2 seconds)...");
|
|
83
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
84
|
+
|
|
85
|
+
log("INFO", "Disconnecting from device...");
|
|
86
|
+
await setup.disconnect();
|
|
87
|
+
|
|
88
|
+
const totalTime = Date.now() - startTime;
|
|
89
|
+
log("SUCCESS", `=== Device 2 Server Completed Successfully (${totalTime}ms) ===`);
|
|
90
|
+
|
|
91
|
+
process.exit(0);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
const totalTime = Date.now() - startTime;
|
|
94
|
+
log("ERROR", `=== Device 2 Server Failed (${totalTime}ms) ===`, {
|
|
95
|
+
error: error.message,
|
|
96
|
+
stack: error.stack
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// Attempt cleanup
|
|
100
|
+
if (setup) {
|
|
101
|
+
try {
|
|
102
|
+
log("INFO", "Attempting cleanup disconnect...");
|
|
103
|
+
await setup.disconnect();
|
|
104
|
+
log("INFO", "Cleanup successful");
|
|
105
|
+
} catch (cleanupError) {
|
|
106
|
+
log("WARN", "Cleanup failed", { error: cleanupError.message });
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
process.exit(1);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// Handle process signals and errors
|
|
115
|
+
process.on("SIGINT", () => {
|
|
116
|
+
log("INFO", "Received SIGINT, shutting down gracefully...");
|
|
117
|
+
process.exit(0);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
process.on("SIGTERM", () => {
|
|
121
|
+
log("INFO", "Received SIGTERM, shutting down gracefully...");
|
|
122
|
+
process.exit(0);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
process.on("unhandledRejection", (reason, promise) => {
|
|
126
|
+
log("ERROR", "Unhandled Rejection", { reason: reason.message, promise });
|
|
127
|
+
process.exit(1);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
process.on("uncaughtException", (error) => {
|
|
131
|
+
log("ERROR", "Uncaught Exception", { error: error.message, stack: error.stack });
|
|
132
|
+
process.exit(1);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// Start the test
|
|
136
|
+
log("INFO", "Starting Device 2 test process...");
|
|
137
|
+
testDevice2();
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dual server spawner - simulates two independent server processes
|
|
3
|
+
* This script spawns two separate Node.js processes to test concurrent device management.
|
|
4
|
+
*
|
|
5
|
+
* Usage: node test/dual/spawn-dual-servers.mjs
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { spawn } from "child_process";
|
|
9
|
+
import path from "path";
|
|
10
|
+
import { fileURLToPath } from "url";
|
|
11
|
+
|
|
12
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
13
|
+
const __dirname = path.dirname(__filename);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Enhanced logger for the spawner process
|
|
17
|
+
* @param {string} level - Log level
|
|
18
|
+
* @param {string} message - Log message
|
|
19
|
+
* @param {any} data - Additional data
|
|
20
|
+
*/
|
|
21
|
+
const log = (level, message, data = null) => {
|
|
22
|
+
const timestamp = new Date().toISOString();
|
|
23
|
+
const prefix = `[${timestamp}] [SPAWNER] [${level.toUpperCase()}]`;
|
|
24
|
+
console.log(`${prefix} ${message}`);
|
|
25
|
+
if (data) {
|
|
26
|
+
console.log(`${prefix} Data:`, JSON.stringify(data, null, 2));
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Spawns a child process for device testing
|
|
32
|
+
* @param {string} scriptName - Name of the script to run
|
|
33
|
+
* @param {string} deviceId - Device identifier for logging
|
|
34
|
+
* @returns {Promise<{success: boolean, code: number, signal?: string, error?: Error}>}
|
|
35
|
+
*/
|
|
36
|
+
const spawnDeviceProcess = (scriptName, deviceId) => {
|
|
37
|
+
return new Promise((resolve) => {
|
|
38
|
+
const scriptPath = path.join(__dirname, scriptName);
|
|
39
|
+
const startTime = Date.now();
|
|
40
|
+
|
|
41
|
+
log("INFO", `Spawning ${deviceId} process: ${scriptName}`);
|
|
42
|
+
|
|
43
|
+
const childProcess = spawn("node", [scriptPath], {
|
|
44
|
+
stdio: "inherit", // Pass through all output
|
|
45
|
+
cwd: path.resolve(__dirname, "../../"), // Set working directory to repo root
|
|
46
|
+
env: {
|
|
47
|
+
...process.env,
|
|
48
|
+
DEVICE_ID: deviceId // Pass device ID as environment variable
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Handle process events
|
|
53
|
+
childProcess.on("spawn", () => {
|
|
54
|
+
log("INFO", `${deviceId} process spawned successfully (PID: ${childProcess.pid})`);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
childProcess.on("error", (error) => {
|
|
58
|
+
const duration = Date.now() - startTime;
|
|
59
|
+
log("ERROR", `${deviceId} process spawn error (${duration}ms)`, {
|
|
60
|
+
error: error.message,
|
|
61
|
+
script: scriptName
|
|
62
|
+
});
|
|
63
|
+
resolve({ success: false, code: -1, error });
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
childProcess.on("close", (code, signal) => {
|
|
67
|
+
const duration = Date.now() - startTime;
|
|
68
|
+
if (code === 0) {
|
|
69
|
+
log("SUCCESS", `${deviceId} process completed successfully (${duration}ms)`, {
|
|
70
|
+
code,
|
|
71
|
+
signal: signal || "none"
|
|
72
|
+
});
|
|
73
|
+
resolve({ success: true, code, signal });
|
|
74
|
+
} else {
|
|
75
|
+
log("ERROR", `${deviceId} process failed (${duration}ms)`, {
|
|
76
|
+
code,
|
|
77
|
+
signal: signal || "none"
|
|
78
|
+
});
|
|
79
|
+
resolve({ success: false, code, signal });
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
childProcess.on("exit", (code, signal) => {
|
|
84
|
+
const duration = Date.now() - startTime;
|
|
85
|
+
log("INFO", `${deviceId} process exited (${duration}ms)`, {
|
|
86
|
+
code,
|
|
87
|
+
signal: signal || "none"
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Main spawner function
|
|
95
|
+
*/
|
|
96
|
+
const runDualServerTest = async () => {
|
|
97
|
+
const overallStart = Date.now();
|
|
98
|
+
|
|
99
|
+
console.log("========================================");
|
|
100
|
+
console.log("DUAL SERVER SPAWN TEST");
|
|
101
|
+
console.log("========================================");
|
|
102
|
+
console.log(`Spawner Process ID: ${process.pid}`);
|
|
103
|
+
console.log(`Start time: ${new Date().toISOString()}`);
|
|
104
|
+
console.log("Target devices: 10.6.0.133, 10.6.0.18");
|
|
105
|
+
console.log("========================================\n");
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
log("INFO", "Starting concurrent device server processes...");
|
|
109
|
+
|
|
110
|
+
// Spawn both processes concurrently
|
|
111
|
+
const results = await Promise.allSettled([
|
|
112
|
+
spawnDeviceProcess("device1.mjs", "DEVICE-1 (10.6.0.133)"),
|
|
113
|
+
spawnDeviceProcess("device2.mjs", "DEVICE-2 (10.6.0.18)")
|
|
114
|
+
]);
|
|
115
|
+
|
|
116
|
+
const overallTime = Date.now() - overallStart;
|
|
117
|
+
|
|
118
|
+
// Process results
|
|
119
|
+
console.log("\n========================================");
|
|
120
|
+
console.log("DUAL SERVER TEST RESULTS");
|
|
121
|
+
console.log("========================================");
|
|
122
|
+
|
|
123
|
+
const summary = {
|
|
124
|
+
successful: 0,
|
|
125
|
+
failed: 0,
|
|
126
|
+
processes: {}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
results.forEach((result, index) => {
|
|
130
|
+
const deviceNames = ["DEVICE-1 (10.6.0.133)", "DEVICE-2 (10.6.0.18)"];
|
|
131
|
+
const deviceName = deviceNames[index];
|
|
132
|
+
|
|
133
|
+
if (result.status === "fulfilled") {
|
|
134
|
+
const processResult = result.value;
|
|
135
|
+
summary.processes[deviceName] = processResult;
|
|
136
|
+
|
|
137
|
+
if (processResult.success) {
|
|
138
|
+
summary.successful++;
|
|
139
|
+
console.log(`✅ ${deviceName}: SUCCESS (code: ${processResult.code})`);
|
|
140
|
+
} else {
|
|
141
|
+
summary.failed++;
|
|
142
|
+
console.log(`❌ ${deviceName}: FAILED (code: ${processResult.code})`);
|
|
143
|
+
if (processResult.error) {
|
|
144
|
+
console.log(` Error: ${processResult.error.message}`);
|
|
145
|
+
}
|
|
146
|
+
if (processResult.signal) {
|
|
147
|
+
console.log(` Signal: ${processResult.signal}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
} else {
|
|
151
|
+
summary.failed++;
|
|
152
|
+
summary.processes[deviceName] = {
|
|
153
|
+
success: false,
|
|
154
|
+
code: -1,
|
|
155
|
+
error: result.reason
|
|
156
|
+
};
|
|
157
|
+
console.log(`❌ ${deviceName}: PROMISE REJECTED`);
|
|
158
|
+
console.log(` Reason: ${result.reason.message}`);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
console.log("----------------------------------------");
|
|
163
|
+
console.log(`Total execution time: ${overallTime}ms`);
|
|
164
|
+
console.log(`Successful processes: ${summary.successful}/2`);
|
|
165
|
+
console.log(`Failed processes: ${summary.failed}/2`);
|
|
166
|
+
console.log("========================================");
|
|
167
|
+
|
|
168
|
+
// Exit with appropriate code
|
|
169
|
+
const exitCode = summary.failed > 0 ? 1 : 0;
|
|
170
|
+
log("INFO", `Dual server test completed with exit code ${exitCode}`);
|
|
171
|
+
|
|
172
|
+
return summary;
|
|
173
|
+
} catch (error) {
|
|
174
|
+
const overallTime = Date.now() - overallStart;
|
|
175
|
+
log("ERROR", `Fatal error in dual server test (${overallTime}ms)`, {
|
|
176
|
+
error: error.message,
|
|
177
|
+
stack: error.stack
|
|
178
|
+
});
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
// Handle process signals
|
|
184
|
+
process.on("SIGINT", () => {
|
|
185
|
+
log("INFO", "Spawner received SIGINT, shutting down...");
|
|
186
|
+
process.exit(0);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
process.on("SIGTERM", () => {
|
|
190
|
+
log("INFO", "Spawner received SIGTERM, shutting down...");
|
|
191
|
+
process.exit(0);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
process.on("unhandledRejection", (reason, promise) => {
|
|
195
|
+
log("ERROR", "Unhandled Rejection in spawner", { reason: reason.message, promise });
|
|
196
|
+
process.exit(1);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
process.on("uncaughtException", (error) => {
|
|
200
|
+
log("ERROR", "Uncaught Exception in spawner", { error: error.message, stack: error.stack });
|
|
201
|
+
process.exit(1);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
// Run the dual server test
|
|
205
|
+
log("INFO", "Starting dual server spawner...");
|
|
206
|
+
runDualServerTest()
|
|
207
|
+
.then((summary) => {
|
|
208
|
+
const exitCode = summary.failed > 0 ? 1 : 0;
|
|
209
|
+
process.exit(exitCode);
|
|
210
|
+
})
|
|
211
|
+
.catch((error) => {
|
|
212
|
+
log("ERROR", "Fatal error in spawner execution", { error: error.message });
|
|
213
|
+
process.exit(1);
|
|
214
|
+
});
|