smartnetwork/test/test.ts

58 lines
2.2 KiB
TypeScript

import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
import * as smartnetwork from '../ts/index.js';
let testSmartNetwork: smartnetwork.SmartNetwork;
tap.test('should create a valid instance of SmartNetwork', async () => {
testSmartNetwork = new smartnetwork.SmartNetwork();
expect(testSmartNetwork).toBeInstanceOf(smartnetwork.SmartNetwork);
});
tap.test('should perform a speedtest', async () => {
const result = await testSmartNetwork.getSpeed();
console.log(`Download speed for this instance is ${result.downloadSpeed}`);
console.log(`Upload speed for this instance is ${result.uploadSpeed}`);
// verify speeds are returned as strings and parse to positive numbers
expect(typeof result.downloadSpeed).toEqual('string');
expect(typeof result.uploadSpeed).toEqual('string');
expect(parseFloat(result.downloadSpeed)).toBeGreaterThan(0);
expect(parseFloat(result.uploadSpeed)).toBeGreaterThan(0);
});
tap.test('should determine wether a port is free', async () => {
await expectAsync(testSmartNetwork.isLocalPortUnused(8080)).toBeTrue();
});
tap.test('should scan a port', async () => {
await expectAsync(testSmartNetwork.isRemotePortAvailable('lossless.com:443')).toBeTrue();
await expectAsync(testSmartNetwork.isRemotePortAvailable('lossless.com', 443)).toBeTrue();
await expectAsync(testSmartNetwork.isRemotePortAvailable('lossless.com:444')).toBeFalse();
});
tap.test('should get gateways', async () => {
const gateways = await testSmartNetwork.getGateways();
console.log(gateways);
// verify gateways object has at least one interface
expect(typeof gateways).toEqual('object');
expect(Object.keys(gateways).length).toBeGreaterThan(0);
});
tap.test('should get the default gateway', async () => {
const defaultGw = await testSmartNetwork.getDefaultGateway();
console.log(defaultGw);
// verify default gateway contains ipv4 and ipv6 info
expect(defaultGw).toBeDefined();
expect(defaultGw.ipv4).toBeDefined();
expect(defaultGw.ipv6).toBeDefined();
});
tap.test('should get public ips', async () => {
const ips = await testSmartNetwork.getPublicIps();
console.log(ips);
// verify public IPs object contains v4 and v6 properties
expect(ips).toHaveProperty('v4');
expect(ips).toHaveProperty('v6');
});
tap.start();