@cldmv/node-android-tv-remote 1.3.0 → 1.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cldmv/node-android-tv-remote",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "description": "A Node.js library for controlling Android TV devices via ADB keycodes and text input.",
5
5
  "main": "src/lib/android-tv-remote.js",
6
6
  "scripts": {
@@ -1,5 +1,6 @@
1
1
  [
2
2
  "home",
3
+ "wakeup",
3
4
  "back",
4
5
  "menu",
5
6
  "ok",
@@ -58,6 +59,7 @@
58
59
  "numpadAdd",
59
60
  "numpadDot",
60
61
  "numpadComma",
62
+ "enter",
61
63
  "numpadEnter",
62
64
  "numpadEquals",
63
65
  "numpadLeftParen",
@@ -142,6 +142,22 @@ const remoteKeys = require("../data/remote-keys.json");
142
142
  * @returns {Remote}
143
143
  */
144
144
  module.exports = function (config) {
145
+ /**
146
+ * On initialization, check if already connected to the device and set internal state.
147
+ * This ensures the internal state is correct if the device is already connected.
148
+ */
149
+ (async () => {
150
+ try {
151
+ const status = await getConnectionStatus(true);
152
+ if (status === "connected") {
153
+ connected = true;
154
+ if (!quiet) logWithTime("Already connected to " + host + " (on init)");
155
+ startHeartbeat();
156
+ }
157
+ } catch (e) {
158
+ // Ignore errors on init
159
+ }
160
+ })();
145
161
  /**
146
162
  * @type {RemoteConfig}
147
163
  */
@@ -250,12 +266,17 @@ module.exports = function (config) {
250
266
  }
251
267
 
252
268
  /**
253
- * Connects to the ADB device and starts heartbeat/connection check.
254
- * Handles already-connected state gracefully.
255
- * @internal
269
+ * Connects to the ADB device if not already connected.
270
+ * Checks the internal connection state before connecting.
256
271
  * @returns {Promise<void>}
272
+ * @example
273
+ * await remote.connect(); // Only connects if not already connected
257
274
  */
258
275
  function connect() {
276
+ if (connected) {
277
+ if (!quiet) logWithTime("Already connected to " + host);
278
+ return Promise.resolve();
279
+ }
259
280
  return client
260
281
  .connect(ip, port)
261
282
  .then(function () {
@@ -420,7 +441,33 @@ module.exports = function (config) {
420
441
  return !!connected;
421
442
  }
422
443
 
423
- return {
444
+ /**
445
+ * Returns a list of all available keyboard key function names on the live API (keyboard.key).
446
+ * This inspects the actual keyboard.key object, so it always matches the real API surface, including all aliases and dynamic keys.
447
+ * @returns {string[]}
448
+ * @example
449
+ * remote.getKeyboardKeys();
450
+ */
451
+ function getKeyboardKeys() {
452
+ // 'this' is not bound, so we must access the constructed keyboard.key object
453
+ // Only return the top-level key names (not .keycode or .shift)
454
+ if (!this || !this.keyboard || !this.keyboard.key) return [];
455
+ return Object.keys(this.keyboard.key).filter((k) => typeof this.keyboard.key[k] === "function");
456
+ }
457
+
458
+ /**
459
+ * Returns a list of all available press command names on the live API (press object), including aliases and dynamic keys.
460
+ * This inspects the actual press object, so it always matches the real API surface.
461
+ * @returns {string[]}
462
+ * @example
463
+ * remote.getPressCommands();
464
+ */
465
+ function getPressCommands() {
466
+ if (!this || !this.press) return [];
467
+ return Object.keys(this.press).filter((k) => k !== "long" && typeof this.press[k] === "function");
468
+ }
469
+
470
+ const remoteApi = {
424
471
  /**
425
472
  * Returns the current connection status as tracked by the module, or does a live check if requested.
426
473
  * @function
@@ -431,6 +478,24 @@ module.exports = function (config) {
431
478
  * await remote.getConnectionStatus(true); // live check
432
479
  */
433
480
  getConnectionStatus,
481
+ /**
482
+ * Returns a list of all available keyboard key function names on the live API (keyboard.key).
483
+ * @returns {string[]}
484
+ * @example
485
+ * remote.getKeyboardKeys();
486
+ */
487
+ getKeyboardKeys: function () {
488
+ return getKeyboardKeys.call(remoteApi);
489
+ },
490
+ /**
491
+ * Returns a list of all available press command names on the live API (press object), including aliases and dynamic keys.
492
+ * @returns {string[]}
493
+ * @example
494
+ * remote.getPressCommands();
495
+ */
496
+ getPressCommands: function () {
497
+ return getPressCommands.call(remoteApi);
498
+ },
434
499
  /**
435
500
  * Returns true if the module believes it is connected to the device.
436
501
  * @readonly
@@ -740,4 +805,5 @@ module.exports = function (config) {
740
805
  )
741
806
  }
742
807
  };
808
+ return remoteApi;
743
809
  };
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Test for getKeyboardKeys and getPressCommands methods of the android-tv-remote module.
3
+ *
4
+ * Usage: Run with `node test/api-surface.test.js`
5
+ */
6
+
7
+ const assert = require("assert");
8
+ const createRemote = require("../src/lib/android-tv-remote");
9
+
10
+ // Minimal config for instantiation (no real device needed for API surface test)
11
+ const remote = createRemote({ ip: "127.0.0.1", autoConnect: false, maintainConnection: false, quiet: true });
12
+
13
+ /**
14
+ * Test getKeyboardKeys returns all top-level function keys from keyboard.key
15
+ */
16
+ function testGetKeyboardKeys() {
17
+ const keys = remote.getKeyboardKeys();
18
+ console.log("getKeyboardKeys:", keys);
19
+ assert(Array.isArray(keys), "getKeyboardKeys should return an array");
20
+ assert(keys.length > 0, "getKeyboardKeys should return at least one key");
21
+ // Spot check for some expected keys
22
+ ["a", "enter", "space"].forEach((k) => {
23
+ assert(keys.includes(k), `getKeyboardKeys should include '${k}'`);
24
+ });
25
+ // Should not include 'shift' or 'keycode' (sub-objects)
26
+ assert(!keys.includes("shift"), "getKeyboardKeys should not include shift");
27
+ assert(!keys.includes("keycode"), "getKeyboardKeys should not include keycode");
28
+ }
29
+
30
+ /**
31
+ * Test getPressCommands returns all top-level function keys from press
32
+ */
33
+ function testGetPressCommands() {
34
+ const keys = remote.getPressCommands();
35
+ console.log("getPressCommands:", keys);
36
+ assert(Array.isArray(keys), "getPressCommands should return an array");
37
+ assert(keys.length > 0, "getPressCommands should return at least one key");
38
+ // Spot check for some expected keys (may vary by remote-keys.json)
39
+ ["home", "back", "ok", "up", "down", "left", "right"].forEach((k) => {
40
+ assert(keys.includes(k), `getPressCommands should include '${k}'`);
41
+ });
42
+ // Should not include 'long' (sub-object)
43
+ assert(!keys.includes("long"), "getPressCommands should not include long");
44
+ }
45
+
46
+ console.log("Testing getKeyboardKeys...");
47
+ testGetKeyboardKeys();
48
+ console.log("Testing getPressCommands...");
49
+ testGetPressCommands();
50
+ console.log("All API surface tests passed.");
@@ -16,18 +16,27 @@ async function main() {
16
16
 
17
17
  if (!quiet) console.log(`Running in ${mode} mode on ${ip}:${port}`);
18
18
 
19
- const setup = new AndroidTVSetup({ ip, port, quiet });
20
- try {
21
- await setup.connect();
22
- if (mode === "set") {
23
- await setup.setSettings();
24
- }
25
- await setup.ensureAwake();
26
- await setup.disconnect();
27
- } catch (err) {
28
- setup.errorWithTime("Error:", err.message || err);
29
- process.exit(1);
30
- }
19
+ const setup = new AndroidTVSetup({ ip, port, quiet });
20
+ try {
21
+ /**
22
+ * Check if already connected to the device. If not, connect.
23
+ * @returns {Promise<void>}
24
+ */
25
+ const isConnected = typeof setup.isConnected === 'function' ? await setup.isConnected() : false;
26
+ if (!isConnected) {
27
+ await setup.connect();
28
+ } else if (!quiet) {
29
+ console.log(`Already connected to ${ip}:${port}`);
30
+ }
31
+ if (mode === "set") {
32
+ await setup.setSettings();
33
+ }
34
+ await setup.ensureAwake();
35
+ await setup.disconnect();
36
+ } catch (err) {
37
+ setup.errorWithTime("Error:", err.message || err);
38
+ process.exit(1);
39
+ }
31
40
 
32
41
  if (!quiet) console.log(`Ran in ${mode} mode on ${ip}:${port}`);
33
42
  }