<commit_before>var assert = require('chai').assert;
var expect = require('chai').expect;
var Terminal = require('../src/xterm');

describe('xterm.js', function() {
  var xterm;

  beforeEach(function () {
    xterm = new Terminal();
    xterm.refresh = function(){};
    xterm.viewport = {
      syncScrollArea: function(){}
    };
  });

  describe('getOption', function() {
    it('should retrieve the option correctly', function() {
      // In the `options` namespace.
      xterm.options.cursorBlink = true;
      assert.equal(xterm.getOption('cursorBlink'), true);

      // On the Terminal instance
      delete xterm.options.cursorBlink;
      xterm.cursorBlink = false;
      assert.equal(xterm.getOption('cursorBlink'), false);
    });
    it('should throw when retrieving a non-existant option', function() {
      assert.throws(xterm.getOption.bind(xterm, 'fake', true));
    });
  });

  describe('setOption', function() {
    it('should set the option correctly', function() {
      xterm.setOption('cursorBlink', true);
      assert.equal(xterm.cursorBlink, true);
      assert.equal(xterm.options.cursorBlink, true);
      xterm.setOption('cursorBlink', false);
      assert.equal(xterm.cursorBlink, false);
      assert.equal(xterm.options.cursorBlink, false);
    });
    it('should throw when setting a non-existant option', function() {
      assert.throws(xterm.setOption.bind(xterm, 'fake', true));
    });
  });

  describe('clear', function() {
    it('should clear a buffer equal to rows', function() {
      var promptLine = xterm.lines[xterm.ybase + xterm.y];
      xterm.clear();
      assert.equal(xterm.y, 0);
      assert.equal(xterm.ybase, 0);
      assert.equal(xterm.ydisp, 0);
      assert.equal(xterm.lines.length, xterm.rows);
      assert.deepEqual(xterm.lines[0], promptLine);
      for (var i = 1; i < xterm.rows; i++) {
        assert.deepEqual(xterm.lines[0], xterm.blankLine());
      }
    });
    it('should clear a buffer larger than rows', function() {
      // Fill the buffer with dummy rows
      for (var i = 0; i < xterm.rows * 2; i++) {
        xterm.write('test\n');
      }

      var promptLine = xterm.lines[xterm.ybase + xterm.y];
      xterm.clear();
      assert.equal(xterm.y, 0);
      assert.equal(xterm.ybase, 0);
      assert.equal(xterm.ydisp, 0);
      assert.equal(xterm.lines.length, xterm.rows);
      assert.deepEqual(xterm.lines[0], promptLine);
      for (var i = 1; i < xterm.rows; i++) {
        assert.deepEqual(xterm.lines[i], xterm.blankLine());
      }
    });
    it('should not break the prompt when cleared twice', function() {
      var promptLine = xterm.lines[xterm.ybase + xterm.y];
      xterm.clear();
      xterm.clear();
      assert.equal(xterm.y, 0);
      assert.equal(xterm.ybase, 0);
      assert.equal(xterm.ydisp, 0);
      assert.equal(xterm.lines.length, xterm.rows);
      assert.deepEqual(xterm.lines[0], promptLine);
      for (var i = 1; i < xterm.rows; i++) {
        assert.deepEqual(xterm.lines[i], xterm.blankLine());
      }
    });
  });

  describe('scroll', function() {
    describe('scrollDisp', function() {
      var startYDisp;
      beforeEach(function() {
        for (var i = 0; i < xterm.rows * 2; i++) {
          xterm.writeln('test');
        }
        startYDisp = xterm.rows + 1;
      });
      it('should scroll a single line', function() {
        assert.equal(xterm.ydisp, startYDisp);
        xterm.scrollDisp(-1);
        assert.equal(xterm.ydisp, startYDisp - 1);
        xterm.scrollDisp(1);
        assert.equal(xterm.ydisp, startYDisp);
      });
      it('should scroll multiple lines', function() {
        assert.equal(xterm.ydisp, startYDisp);
        xterm.scrollDisp(-5);
        assert.equal(xterm.ydisp, startYDisp - 5);
        xterm.scrollDisp(5);
        assert.equal(xterm.ydisp, startYDisp);
      });
      it('should not scroll beyond the bounds of the buffer', function() {
        assert.equal(xterm.ydisp, startYDisp);
        xterm.scrollDisp(1);
        assert.equal(xterm.ydisp, startYDisp);
        for (var i = 0; i < startYDisp; i++) {
          xterm.scrollDisp(-1);
        }
        assert.equal(xterm.ydisp, 0);
        xterm.scrollDisp(-1);
        assert.equal(xterm.ydisp, 0);
      });
    });
  });

  describe('evaluateKeyEscapeSequence', function() {
    it('should return the correct escape sequence for unmodified keys', function() {
      // Backspace
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 8 }).key, '\x7f'); // ^?
      // Tab
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 9 }).key, '\t');
      // Return/enter
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 13 }).key, '\r'); // CR
      // Escape
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 27 }).key, '\x1b');
      // Page up, page down
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 33 }).key, '\x1b[5~'); // CSI 5 ~
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 34 }).key, '\x1b[6~'); // CSI 6 ~
      // End, Home
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 35 }).key, '\x1b[F'); // SS3 F
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 36 }).key, '\x1b[H'); // SS3 H
      // Left, up, right, down arrows
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 37 }).key, '\x1b[D'); // CSI D
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 38 }).key, '\x1b[A'); // CSI A
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 39 }).key, '\x1b[C'); // CSI C
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 40 }).key, '\x1b[B'); // CSI B
      // Insert
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 45 }).key, '\x1b[2~'); // CSI 2 ~
      // Delete<commit_msg>Add tests for scrollPages<commit_after>var assert = require('chai').assert;
var expect = require('chai').expect;
var Terminal = require('../src/xterm');

describe('xterm.js', function() {
  var xterm;

  beforeEach(function () {
    xterm = new Terminal();
    xterm.refresh = function(){};
    xterm.viewport = {
      syncScrollArea: function(){}
    };
  });

  describe('getOption', function() {
    it('should retrieve the option correctly', function() {
      // In the `options` namespace.
      xterm.options.cursorBlink = true;
      assert.equal(xterm.getOption('cursorBlink'), true);

      // On the Terminal instance
      delete xterm.options.cursorBlink;
      xterm.cursorBlink = false;
      assert.equal(xterm.getOption('cursorBlink'), false);
    });
    it('should throw when retrieving a non-existant option', function() {
      assert.throws(xterm.getOption.bind(xterm, 'fake', true));
    });
  });

  describe('setOption', function() {
    it('should set the option correctly', function() {
      xterm.setOption('cursorBlink', true);
      assert.equal(xterm.cursorBlink, true);
      assert.equal(xterm.options.cursorBlink, true);
      xterm.setOption('cursorBlink', false);
      assert.equal(xterm.cursorBlink, false);
      assert.equal(xterm.options.cursorBlink, false);
    });
    it('should throw when setting a non-existant option', function() {
      assert.throws(xterm.setOption.bind(xterm, 'fake', true));
    });
  });

  describe('clear', function() {
    it('should clear a buffer equal to rows', function() {
      var promptLine = xterm.lines[xterm.ybase + xterm.y];
      xterm.clear();
      assert.equal(xterm.y, 0);
      assert.equal(xterm.ybase, 0);
      assert.equal(xterm.ydisp, 0);
      assert.equal(xterm.lines.length, xterm.rows);
      assert.deepEqual(xterm.lines[0], promptLine);
      for (var i = 1; i < xterm.rows; i++) {
        assert.deepEqual(xterm.lines[0], xterm.blankLine());
      }
    });
    it('should clear a buffer larger than rows', function() {
      // Fill the buffer with dummy rows
      for (var i = 0; i < xterm.rows * 2; i++) {
        xterm.write('test\n');
      }

      var promptLine = xterm.lines[xterm.ybase + xterm.y];
      xterm.clear();
      assert.equal(xterm.y, 0);
      assert.equal(xterm.ybase, 0);
      assert.equal(xterm.ydisp, 0);
      assert.equal(xterm.lines.length, xterm.rows);
      assert.deepEqual(xterm.lines[0], promptLine);
      for (var i = 1; i < xterm.rows; i++) {
        assert.deepEqual(xterm.lines[i], xterm.blankLine());
      }
    });
    it('should not break the prompt when cleared twice', function() {
      var promptLine = xterm.lines[xterm.ybase + xterm.y];
      xterm.clear();
      xterm.clear();
      assert.equal(xterm.y, 0);
      assert.equal(xterm.ybase, 0);
      assert.equal(xterm.ydisp, 0);
      assert.equal(xterm.lines.length, xterm.rows);
      assert.deepEqual(xterm.lines[0], promptLine);
      for (var i = 1; i < xterm.rows; i++) {
        assert.deepEqual(xterm.lines[i], xterm.blankLine());
      }
    });
  });

  describe('scroll', function() {
    describe('scrollDisp', function() {
      var startYDisp;
      beforeEach(function() {
        for (var i = 0; i < xterm.rows * 2; i++) {
          xterm.writeln('test');
        }
        startYDisp = xterm.rows + 1;
      });
      it('should scroll a single line', function() {
        assert.equal(xterm.ydisp, startYDisp);
        xterm.scrollDisp(-1);
        assert.equal(xterm.ydisp, startYDisp - 1);
        xterm.scrollDisp(1);
        assert.equal(xterm.ydisp, startYDisp);
      });
      it('should scroll multiple lines', function() {
        assert.equal(xterm.ydisp, startYDisp);
        xterm.scrollDisp(-5);
        assert.equal(xterm.ydisp, startYDisp - 5);
        xterm.scrollDisp(5);
        assert.equal(xterm.ydisp, startYDisp);
      });
      it('should not scroll beyond the bounds of the buffer', function() {
        assert.equal(xterm.ydisp, startYDisp);
        xterm.scrollDisp(1);
        assert.equal(xterm.ydisp, startYDisp);
        for (var i = 0; i < startYDisp; i++) {
          xterm.scrollDisp(-1);
        }
        assert.equal(xterm.ydisp, 0);
        xterm.scrollDisp(-1);
        assert.equal(xterm.ydisp, 0);
      });
    });

    describe('scrollPages', function() {
      var startYDisp;
      beforeEach(function() {
        for (var i = 0; i < xterm.rows * 3; i++) {
          xterm.writeln('test');
        }
        startYDisp = (xterm.rows * 2) + 1;
      });
      it('should scroll a single page', function() {
        assert.equal(xterm.ydisp, startYDisp);
        xterm.scrollPages(-1);
        assert.equal(xterm.ydisp, startYDisp - (xterm.rows - 1));
        xterm.scrollPages(1);
        assert.equal(xterm.ydisp, startYDisp);
      });
      it('should scroll a multiple pages', function() {
        assert.equal(xterm.ydisp, startYDisp);
        xterm.scrollPages(-2);
        assert.equal(xterm.ydisp, startYDisp - (xterm.rows - 1) * 2);
        xterm.scrollPages(2);
        assert.equal(xterm.ydisp, startYDisp);
      });
    });
  });

  describe('evaluateKeyEscapeSequence', function() {
    it('should return the correct escape sequence for unmodified keys', function() {
      // Backspace
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 8 }).key, '\x7f'); // ^?
      // Tab
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 9 }).key, '\t');
      // Return/enter
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 13 }).key, '\r'); // CR
      // Escape
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 27 }).key, '\x1b');
      // Page up, page down
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 33 }).key, '\x1b[5~'); // CSI 5 ~
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 34 }).key, '\x1b[6~'); // CSI 6 ~
      // End, Home
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 35 }).key, '\x1b[F'); // SS3 F
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 36 }).key, '\x1b[H'); // SS3 H
      // Left, up, right, down arrows
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 37 }).key, '\x1b[D'); // CSI D
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 38 }).key, '\x1b[A'); // CSI A
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 39 }).key, '\x1b[C'); // CSI C
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 40 }).key, '\x1b[B'); // CSI B
      // Insert
      assert.equal(xterm.evaluateKeyEscapeSequence({ keyCode: 45 }).key, '\x1b[2~'); // CSI 2 ~
      // Delete
{'fraction_non_alphanumeric': 0.163818123186069, 'fraction_numerical': 0.013140922283134474, 'mean_word_length': 2.689767995240928, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 3, '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': '51150', 'n_tokens_mistral': 4244, 'n_tokens_neox': 4081, 'n_words': 841}