Skip to content
Snippets Groups Projects
index.test.ts 6.95 KiB
import assert from "assert";
import connect from "../../src";
import { ConnectionOptions } from "../../src/types";
import config from "./config.json";

describe("Test general API", () => {
  let device: any = null;

  before(async () => {
    device = await connect(config.ws as ConnectionOptions);
  });

  // GET

  it("get existing path resolves without error", async () => {
    const desc = await device.get("Device.DeviceInfo.Description");
    assert.strictEqual(typeof desc, "string");
  });

  it("get multiple paths resolves without error", async () => {
    const resp = await device.get([
      "Device.DeviceInfo.Description",
      "Device.WiFi.",
    ]);
    assert.strictEqual(Array.isArray(resp), true);
  });

  it("get incorrect path throws an error", async () => {
    await device
      .get("Device.Not.A.Thing")
      .then((msg) => {
        throw msg;
      })
      .catch((err) => assert.strictEqual(typeof err, "object"));
  });

  // SET

  it("set with value", async () => {
    const alias = await device.get("Device.WiFi.Radio.1.Alias");
    await device.set("Device.WiFi.Radio.1.Alias", alias);
  });

  it("set with object", async () => {
    const Alias = await device.get("Device.WiFi.Radio.1.Alias");
    await device.set("Device.WiFi.Radio.1.", { Alias });
  });

  it("set throws an error on incorrect path", async () => {
    await device
      .set("Device.Not.A.Path", { Cat: "cute" })
      .then((msg) => {
        throw msg;
      })
      .catch((err) => assert.strictEqual(typeof err, "object"));
  });

  // OPERATE

  it("operate creates a working command", async () => {
    const [ping] = await device.operate("Device.IP.Diagnostics.IPPing()");
    const results = await ping({ Host: "iopsys.eu" });
    assert.strictEqual(typeof results, "object");
  });

  // ADD / DELETE

  it("add and delete new object", async () => {
    const newPath = await device.add("Device.NAT.PortMapping.");
    await device.del(newPath);
  });

  it("add and delete new object with initial values", async () => {
    const newPath = await device.add("Device.NAT.PortMapping.", {
      Description: "webserver1-set",
      ExternalPort: "80",
      Protocol: "TCP",
      Interface: "Device.IP.Interface.1",
      Enable: "true",
      InternalClient: "192.168.1.125",
      InternalPort: "5000",
    });
    const { Description } = await device.get(newPath);
    await device.del(newPath);
    assert.strictEqual(Description, "webserver1-set");
  });

  // RESOLVE

  it("resolves references in get call", async () => {
    const msg = await device
      .get("Device.Bridging.Bridge.1.Port.1.")
      .then(device.resolve);
    assert.strictEqual(typeof msg.LowerLayers, "object");
  });

  it("resolves string reference in get call", async () => {
    const msg = await device
      .get("Device.Bridging.Bridge.1.Port.1.LowerLayers")
      .then(device.resolve);
    assert.strictEqual(typeof msg, "object");
  });

  it("returns string when it cannot resolve path", async () => {
    const msg = await device.resolve("Device.Not.A.Path");
    assert.strictEqual(typeof msg, "string");
  });

  it("returns string when it cannot resolve path inside object", async () => {
    const msg = await device.resolve({ reference: "Device.Not.A.Path" });
    assert.strictEqual(msg.reference, "Device.Not.A.Path");
  });

  // Instances

  it("instances resolves without error", async () => {
    const msg = await device.instances("Device.WiFi.");
    assert.strictEqual(typeof msg, "object");
  });

  it("instances throws error on incorrect path", async () => {
    await device
      .instances("Device.Cat.")
      .then((msg) => {
        throw msg;
      })
      .catch((err) => assert.strictEqual(typeof err, "object"));
  });

  // Proto

  it("proto resolves without error", async () => {
    const msg = await device.supportedProto("Device.WiFi.");
    assert.strictEqual(msg, "1.0");
  });

  // Supported

  it("supportedDM resolves without error", async () => {
    const msg = await device.supportedDM("Device.WiFi.");
    assert.strictEqual(typeof msg, "object");
  });

  it("supportedDM throws error on incorrect path", async () => {
    await device
      .supportedDM("Device.Cat.")
      .then((msg) => {
        throw msg;
      })
      .catch((err) => assert.strictEqual(typeof err, "object"));
  });

  // Subscribe

  it("subscribe callsback without error", async () => {
    const clearSub = await device.subscribe(
      { notif: "ObjectCreation", reference: "Device.NAT.PortMapping." },
      (msg) => {
        assert.strictEqual(typeof msg, "object");
      }
    );
    const np = await device.add("Device.NAT.PortMapping.");
    clearSub();
    await device.del(np);
  });

  it("subscribe throws error without callback", async () => {
    const clearSub = await device
      .subscribe(
        { notif: "ObjectCreation", reference: "Device.NAT.PortMapping." },
        (msg) => {
          throw msg;
        }
      )
      .catch((err) => assert.strictEqual(typeof err, "object"));
    clearSub();
  });

  // Options

  it("get with timeout option crashes if it takes too long", async () => {
    await device
      .options({ timeout: 0 })
      .get("Device.DeviceInfo.Description")
      .then((msg) => {
        throw msg;
      })
      .catch((err) => {
        assert.strictEqual(err.startsWith("timeout of"), true);
      });
  });

  it("get with timeout option returns without error", async () => {
    await device
      .options({ timeout: -1 })
      .get("Device.DeviceInfo.SerialNumber")
      .then((msg) => assert.strictEqual(typeof msg, "string"))
      .catch((err) => {
        throw err;
      });
  });

  it("get with double options setup returns without error", async () => {
    await device
      .options({ timeout: -1 })
      .options({ preCall: () => null })
      .get("Device.DeviceInfo.SerialNumber")
      .then((msg) => assert.strictEqual(typeof msg, "string"))
      .catch((err) => {
        throw err;
      });
  });

  it("get with pre and post call executes without error", async () => {
    await device
      .options({
        preCall: (name) => assert.strictEqual(name, "get"),
        postCall: (name) => assert.strictEqual(name, "get"),
      })
      .get("Device.DeviceInfo.SerialNumber");
  });

  it("downloaddiagnostic() executes without error", async () => {
    const [op, clearOp] = await device.operate(
      "Device.IP.Diagnostics.DownloadDiagnostics()"
    );
    const input = {
      Interface: 'Device.IP.Interface.[Name=="wan"].',
      DownloadURL: "http://ipv4.download.thinkbroadband.com/5MB.zip",
    };
    const res = await op(input);
    await clearOp();
    
    assert.strictEqual(typeof res, "object");
  }).timeout(10000);

  after(async () => {
    await device.disconnect();
  });
});

describe.skip("Test specific connection types", () => {
  it("Connect using url", async () => {
    let usp = await connect(config.url);
    const desc = await usp.get("Device.DeviceInfo.Description");
    await usp.disconnect();
    assert.strictEqual(typeof desc, "string");
  });
});