<commit_before>import * as sinon from 'sinon';

import * as Session from '../../../src/chatbot/session.js';

describe('chatbot sessions', function() {
  beforeEach(function() {
    this.dialog = {
      checkIntent: sinon.stub(),
      getSubStateCount: sinon.stub(),
    };

    this.session = new Session(this.dialog);

    this.session._start('SESSION', {}, '');
    this.dialog.getSubStateCount.returns(1);
  });

  describe('#beginDialog', function() {
    it('should push the given dialog to top of the dialog stack',
      function() {
        this.session.beginDialog('test');

        return expect(this.session._state)
          .to.deep.equal([
            ['', 0],
            ['test', 0],
          ]);
      });

    it('should increment the substate of the current dialog', function() {
      this.dialog.getSubStateCount.returns(2);

      this.session.beginDialog('test');

      return expect(this.session._state)
        .to.deep.equal([
          ['', 1],
          ['test', 0],
        ]);
    });

    it(
      'should throw an Error if the dialog is already in the dialog stack',
      function() {
        this.session.beginDialog('foo');
        this.session.beginDialog('bar');

        return expect(this.session.beginDialog, 'foo')
          .to.throw(Error);
      });
  });

  describe('#endDialog', function() {
    it('should return to the previous dialog', function() {
      this.session.beginDialog('foo');
      this.session.beginDialog('bar');
      this.session.endDialog();

      return expect(this.session._state)
        .to.deep.equal([
          ['', 0],
          ['foo', 0],
        ]);
    });
  });

  describe('#switchDialog', function() {
    it('should switch to the given dialog', function() {
      this.session.switchDialog('/test');

      expect(this.session._state[this.session._state.length - 1])
        .to.deep.equal(['test', 0]);
    });
  });

  describe('#checkIntent', function() {
    it('should call Builder.checkIntent', function() {
      this.dialog.checkIntent.returns('OK');

      const ret = this.session.checkIntent('test_intent');

      expect(this.dialog.checkIntent)
        .to.have.been.calledWith('test_intent', this.session);

      return expect(ret)
        .to.equal('OK');
    });
  });

  describe('#_start', function() {
    it('start returns the session object', function() {
      const ret = this.session._start('SESSION', {}, '');
      return expect(ret)
        .to.equal(this.session);
    });

    it('should assign context to session ', function() {
      this.session._start(
        'SESSION', {
          foo: 'bar',
        },
        ''<commit_msg>New session tests<commit_after>import * as sinon from 'sinon';

import * as Session from '../../../src/chatbot/session.js';

describe('chatbot sessions', function() {
  beforeEach(function() {
    this.dialog = {
      checkIntent: sinon.stub(),
      getSubStateCount: sinon.stub(),
    };

    this.session = new Session(this.dialog);

    this.session._start('SESSION', {}, '');
    this.dialog.getSubStateCount.returns(1);
  });

  describe('#beginDialog', function() {
    it('should push the given dialog to top of the dialog stack',
      function() {
        this.session.beginDialog('test');

        return expect(this.session._state)
          .to.deep.equal([
            ['', 0],
            ['test', 0],
          ]);
      });

    it('should increment the substate of the current dialog', function() {
      this.dialog.getSubStateCount.returns(2);

      this.session.beginDialog('test');

      return expect(this.session._state)
        .to.deep.equal([
          ['', 1],
          ['test', 0],
        ]);
    });

    it(
      'should throw an Error if the dialog is already in the dialog stack',
      function() {
        this.session.beginDialog('foo');
        this.session.beginDialog('bar');

        return expect(this.session.beginDialog, 'foo')
          .to.throw(Error);
      });
  });

  describe('#endDialog', function() {
    it('should return to the previous dialog', function() {
      this.session.beginDialog('foo');
      this.session.beginDialog('bar');
      this.session.endDialog();

      return expect(this.session._state)
        .to.deep.equal([
          ['', 0],
          ['foo', 0],
        ]);
    });
  });

  describe('#switchDialog', function() {
    it('should switch to the given dialog', function() {
      this.session.switchDialog('/test');

      expect(this.session._state[this.session._state.length - 1])
        .to.deep.equal(['test', 0]);
    });
  });

  describe('#checkIntent', function() {
    it('should call Builder.checkIntent', function() {
      this.dialog.checkIntent.returns('OK');

      const ret = this.session.checkIntent('test_intent');

      expect(this.dialog.checkIntent)
        .to.have.been.calledWith('test_intent', this.session);

      return expect(ret)
        .to.equal('OK');
    });
  });

  describe('#runActions', function() {
    it('should call _queueFunction 3 times', function() {
      const queueSpy = sinon.spy(this.session, '_queueFunction');
      const actions = ['action1', 'action2', 'action3'];

      this.session.runActions(actions);
      queueSpy.restore();

      return expect(queueSpy.callCount).to.equal(3);
    });
  });

  describe('#prev', function() {
    it('should go to correct state after calling prev', function() {
      this.session._start('SESSION', {}, '');
      const method = this.session.dialog.getSubStateCount;
      this.session.dialog.getSubStateCount = () => 10;
      this.session.beginDialog('dialog', true);
      this.session.prev();

      const expectation = [
        ['', 0],
        ['dialog', 9],
      ];

      this.session.dialog.getSubStateCount = method;

      return expect(this.session._state).to.deep.equal(expectation);
    });
  });

  describe('#_start', function() {
    it('start returns the session object', function() {
      const ret = this.session._start('SESSION', {}, '');
      return expect(ret)
        .to.equal(this.session);
    });

    it('should assign context to session ', function() {
      this.session._start(
        'SESSION', {
          foo: 'bar',
        },
        ''
{'fraction_non_alphanumeric': 0.1690254100997105, 'fraction_numerical': 0.0046638790607912515, 'mean_word_length': 2.464623955431755, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '6179974', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1769, 'n_words': 362}