<commit_before>describe('nexusClient.js', function() {
  beforeEach(function() {
    // Stub WebSocket
    this.ws = {
      send: newSpy('send'),
      close: newSpy('close'),
    },
    stub(window).WebSocket.toReturn(this.ws);

    // Defaults
    this.defaultServer = 'ws://localhost:3000';
    this.defaultHostList = [
      {
        hostName: 'one',
        hostID: 1,
      },
      {
        hostName: 'two',
        hostID: 2,
      }
    ];
    this.defaultHost = this.defaultHostList[0];

    // Helper functions
    this.newClient = (nexusServer=this.defaultServer, autoConnectOptions) => {
      return this.client = new nexusClient(nexusServer, autoConnectOptions);
    };

    this.newConnectingClient = () => {
      this.newClient();
      this.triggerServerConnected();
      this.client.connect(this.defaultHost, ()=>{});
      return this.client;
    };

    this.triggerServerConnected = () => {
      this.ws.onopen && this.ws.onopen();
    };

    this.triggerHostList = () => {
      const data = JSON.stringify({
        type: 'LIST',
        payload: this.defaultHostList,
      });
      this.ws.onmessage && this.ws.onmessage({ data });
    };

    this.triggerHostConnected = () => {
      const data = JSON.stringify({
        type: 'CONNECTED',
      });
      this.ws.onmessage && this.ws.onmessage({ data });
    };

    this.triggerMessage = (data) => {
      this.ws.onmessage && this.ws.onmessage({ data });
    }
  });

  describe('new nexusClient(nexusServer, autoConnectOptions)', function() {
    it('requires a nexusServer address', function() {
      expect(()=>new nexusClient()).toThrow(new Error('Missing nexusServer address'));
    });

    it('connects to the nexus server provided', function() {
      this.newClient(this.defaultServer);
      expect(window.WebSocket).toHaveBeenCalledWith(this.defaultServer);
    });

    describe('when provided an autoConnectOptions', function() {
      it('tries to connect with the given options', function() {
        const hostID = 8;
        const hostName = 'fight club';
        const connectRequest = JSON.stringify({
          type: 'CONNECT',
          hostID,
          hostName,
        })

        this.newClient(this.defaultServer, {
          hostID,
          hostName,
        });

        expect(this.ws.send).not.toHaveBeenCalledWith(connectRequest);

        this.triggerServerConnected(); // simulate server connection

        expect(this.ws.send).toHaveBeenCalledWith(connectRequest);
      });
    });

    describe('when not provided an autoConnectOptions', function() {
      it('does not send anything after connecting to server', function() {
        this.newClient(this.defaultServer);

        this.triggerServerConnected(); // simulate server connection

        expect(this.ws.send).not.toHaveBeenCalled();
      });
    });

    describe('when it connects to the server', function() {
      it('calls the .onServerConnect callback', function() {
        const callback = newSpy('onServerConnect');
        this.newClient().onServerConnect = callback;

        this.triggerServerConnected();

        expect(callback).toHaveBeenCalled();
      });
    });
  });

  describe('when not connected to a host', function() {
    describe('#getHostList(callback)', function() {
      it('sends a request', function() {
        this.newClient();
        this.triggerServerConnected();
        this.client.getHostList(()=>{});
        expect(this.ws.send).toHaveBeenCalledWith(JSON.stringify({
          type: 'LIST',
        }));
      });

      it('calls the callback when we get the list', function() {
        const callback = newSpy('onHostList');
        this.newClient();
        this.triggerServerConnected();
        this.client.getHostList(callback);

        this.triggerHostList();

        expect(callback).toHaveBeenCalledWith(this.defaultHostList);
      });
    });

    describe('#connect({hostName, hostID}, callback)', function() {
      it('sends a connection request', function() {
        const hostName = 'blah';
        const hostID = 6;
        this.newClient();
        this.triggerServerConnected();
        this.client.connect({hostName, hostID});<commit_msg>Add .onHostList test<commit_after>describe('nexusClient.js', function() {
  beforeEach(function() {
    // Stub WebSocket
    this.ws = {
      send: newSpy('send'),
      close: newSpy('close'),
    },
    stub(window).WebSocket.toReturn(this.ws);

    // Defaults
    this.defaultServer = 'ws://localhost:3000';
    this.defaultHostList = [
      {
        hostName: 'one',
        hostID: 1,
      },
      {
        hostName: 'two',
        hostID: 2,
      }
    ];
    this.defaultHost = this.defaultHostList[0];

    // Helper functions
    this.newClient = (nexusServer=this.defaultServer, autoConnectOptions) => {
      return this.client = new nexusClient(nexusServer, autoConnectOptions);
    };

    this.newConnectingClient = () => {
      this.newClient();
      this.triggerServerConnected();
      this.client.connect(this.defaultHost, ()=>{});
      return this.client;
    };

    this.triggerServerConnected = () => {
      this.ws.onopen && this.ws.onopen();
    };

    this.triggerHostList = () => {
      const data = JSON.stringify({
        type: 'LIST',
        payload: this.defaultHostList,
      });
      this.ws.onmessage && this.ws.onmessage({ data });
    };

    this.triggerHostConnected = () => {
      const data = JSON.stringify({
        type: 'CONNECTED',
      });
      this.ws.onmessage && this.ws.onmessage({ data });
    };

    this.triggerMessage = (data) => {
      this.ws.onmessage && this.ws.onmessage({ data });
    }
  });

  describe('new nexusClient(nexusServer, autoConnectOptions)', function() {
    it('requires a nexusServer address', function() {
      expect(()=>new nexusClient()).toThrow(new Error('Missing nexusServer address'));
    });

    it('connects to the nexus server provided', function() {
      this.newClient(this.defaultServer);
      expect(window.WebSocket).toHaveBeenCalledWith(this.defaultServer);
    });

    describe('when provided an autoConnectOptions', function() {
      it('tries to connect with the given options', function() {
        const hostID = 8;
        const hostName = 'fight club';
        const connectRequest = JSON.stringify({
          type: 'CONNECT',
          hostID,
          hostName,
        })

        this.newClient(this.defaultServer, {
          hostID,
          hostName,
        });

        expect(this.ws.send).not.toHaveBeenCalledWith(connectRequest);

        this.triggerServerConnected(); // simulate server connection

        expect(this.ws.send).toHaveBeenCalledWith(connectRequest);
      });
    });

    describe('when not provided an autoConnectOptions', function() {
      it('does not send anything after connecting to server', function() {
        this.newClient(this.defaultServer);

        this.triggerServerConnected(); // simulate server connection

        expect(this.ws.send).not.toHaveBeenCalled();
      });
    });

    describe('when it connects to the server', function() {
      it('calls the .onServerConnect callback', function() {
        const callback = newSpy('onServerConnect');
        this.newClient().onServerConnect = callback;

        this.triggerServerConnected();

        expect(callback).toHaveBeenCalled();
      });
    });
  });

  describe('when not connected to a host', function() {
    describe('#getHostList(callback)', function() {
      it('sends a request', function() {
        this.newClient();
        this.triggerServerConnected();
        this.client.getHostList(()=>{});
        expect(this.ws.send).toHaveBeenCalledWith(JSON.stringify({
          type: 'LIST',
        }));
      });

      it('calls the callback when we get the list', function() {
        const callback = newSpy('onHostList');
        this.newClient();
        this.triggerServerConnected();
        this.client.getHostList(callback);

        this.triggerHostList();

        expect(callback).toHaveBeenCalledWith(this.defaultHostList);
      });

      it('calls .onHostList if no callback is provided', function() {
        const callback = newSpy('onHostList');
        this.newClient();
        this.triggerServerConnected();
        this.client.onHostList = callback;
        this.client.getHostList();

        this.triggerHostList();

        expect(callback).toHaveBeenCalledWith(this.defaultHostList);
      });
    });

    describe('#connect({hostName, hostID}, callback)', function() {
      it('sends a connection request', function() {
        const hostName = 'blah';
        const hostID = 6;
        this.newClient();
        this.triggerServerConnected();
        this.client.connect({hostName, hostID});
{'fraction_non_alphanumeric': 0.14267806267806268, 'fraction_numerical': 0.0020512820512820513, 'mean_word_length': 2.589366053169734, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7534862', 'n_tokens_mistral': 2515, 'n_tokens_neox': 2451, 'n_words': 465}