const assert = require('assert');
const {
  connectionsEnabled,
  disconnectConnections,
} = require('../connection-mode');

assert.strictEqual(connectionsEnabled({ autoConnect: true }), true);
assert.strictEqual(connectionsEnabled({}), true);
assert.strictEqual(connectionsEnabled({ autoConnect: false }), false);
assert.strictEqual(connectionsEnabled(null), false);

const cleared = [];
const peerSocket = {
  destroyed: false,
  destroy() {
    this.destroyed = true;
  },
};
const directSocket = {
  destroyed: false,
  destroy() {
    this.destroyed = true;
  },
};
const peers = {
  remote: {
    socket: peerSocket,
  },
};
const directConnecting = {
  '169.254.1.2': directSocket,
};

assert.strictEqual(disconnectConnections({
  peers,
  directConnecting,
  clearPeer(peer) {
    cleared.push(peer);
  },
}), 2);
assert.strictEqual(peerSocket.destroyed, true);
assert.strictEqual(directSocket.destroyed, true);
assert.deepStrictEqual(Object.keys(directConnecting), []);
assert.deepStrictEqual(cleared, [peers.remote]);

console.log('connection mode tests passed');
