{"size": 1624, "ext": "js", "max_stars_repo_path": "test/functional/android/apidemos/alerts-specs.js", "max_stars_repo_name": "bellcliff/appium", "max_stars_repo_stars_event_min_datetime": "2016-04-27T02:58:13.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-22T15:26:20.000Z", "max_issues_repo_path": "test/functional/android/apidemos/alerts-specs.js", "max_issues_repo_name": "bellcliff/appium", "max_issues_repo_issues_event_min_datetime": "2022-03-22T18:27:24.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-23T19:15:38.000Z", "max_forks_repo_path": "test/functional/android/apidemos/alerts-specs.js", "max_forks_repo_name": "bellcliff/appium", "max_forks_repo_forks_event_min_datetime": "2015-02-04T05:40:19.000Z", "max_forks_repo_forks_event_max_datetime": "2021-01-22T01:13:23.000Z"}
{"max_stars_count": 3, "max_issues_count": 3, "max_forks_count": 5, "avg_line_length": 27.5254237288, "max_line_length": 78, "alphanum_fraction": 0.5240147783}
"use strict";

var env = require('../../../helpers/env')
  , setup = require("../../common/setup-base")
  , desired = require("./desired")
  , net = require('net')
  , io = require('socket.io-client');

describe("apidemos - alerts @skip-android-all", function () {

  setup(this, desired);

  // set up telnet...
  var runCommands = function (cmds, cb) {
    try {
      var conn = net.createConnection(5554, 'localhost');
      conn.on('connect', function () {
        try {
          for (var i = 0; i < cmds.length; i++) {
            conn.write(cmds[i] + "\n");
          }
          conn.write('quit\n');
          cb(true);
        } catch (err) {
          console.log("Could not run commands: " + err.description);
          cb(undefined);
        }
      }, cb);
    } catch (err) {
      console.log("Couldn't run commands: " + err.description);
    }
  };

  it('should detect low power...', function (done) {
    // setup websocket client...
    var options = {
      transports: ['websocket'],
      'force new connection': true
    };
    try {
      var client = io.connect('http://127.0.0.1:' + env.APPIUM_PORT, options);
      client.on('connect', function () {
        runCommands(['power ac off', 'power capacity 9'], function (success) {
          success.should.equal(true);
        });
      });
      client.on('alert', function () {
        runCommands(['power ac on', 'power capacity 50'], function (success) {
          success.should.equal(true);
          done();
        });
      });
    } catch (err) {
      console.log("Unknown exception: " + err.description);
      done();
    }
  });
});
