<commit_before>/* global describe, it, beforeEach */

var chai, assert, sinon, spy, TreeModel;
chai = require('chai');
sinon = require('sinon');
spy = sinon.spy;
TreeModel = require('../src/TreeModel');
assert = chai.assert;
chai.Assertion.includeStack = true;

describe('TreeModel', function () {
  'use strict';

  function idEq(id) {
    return function (node) {
      return node.model.id === id;
    };
  }

  describe('with default configuration', function () {
    var treeModel;

    beforeEach(function () {
      treeModel = new TreeModel();
    });

    describe('parse()', function () {
      it('should throw an error when model is a number (not an object)', function () {
        assert.throws(treeModel.parse.bind(treeModel, 1), TypeError, 'Model must be of type object.');
      });

      it('should throw an error when model is a string (not an object)', function () {
        assert.throws(treeModel.parse.bind(treeModel, 'string'), TypeError, 'Model must be of type object.');
      });

      it('should throw an error when some child in the model is not an object', function () {
        assert.throws(
          treeModel.parse.bind(treeModel, {children: ['string']}),
          TypeError,
          'Model must be of type object.');
      });

      it('should create a root node when given a model without children', function () {
        var root;

        root = treeModel.parse({id: 1});

        assert.isUndefined(root.parent);
        assert.isArray(root.children);
        assert.lengthOf(root.children, 0);
        assert.deepEqual(root.model, {id: 1});
      });

      it('should create a root and the respective children when given a model with children', function () {
        var root, node12;

        root = treeModel.parse({
          id: 1,
          children: [
            {
              id: 11,
              children: [{id: 111}]
            },
            {
              id: 12,
              children: [{id: 121}, {id: 122}]
            }
          ]
        });

        assert.isUndefined(root.parent);
        assert.isArray(root.children);
        assert.lengthOf(root.children, 2);
        assert.deepEqual(root.model, {
          id: 1,
          children: [
            {
              id: 11,
              children: [{id: 111}]
            },
            {
              id: 12,
              children: [{id: 121}, {id: 122}]
            }
          ]
        });

        assert.deepEqual(root, root.children[0].parent);
        assert.deepEqual(root, root.children[1].parent);

        node12 = root.children[1];
        assert.isArray(node12.children);
        assert.lengthOf(node12.children, 2);
        assert.deepEqual(node12.model, {
          id: 12,
          children: [{id: 121}, {id: 122}]
        });

        assert.deepEqual(node12, node12.children[0].parent);
        assert.deepEqual(node12, node12.children[1].parent);
      });
    });

    describe('addChild()', function () {
      var root;

      beforeEach(function () {
        root = treeModel.parse({id: 1, children: [{id: 11}, {id: 12}]});
      });

      it('should add child to the end', function () {
        root.addChild(treeModel.parse({id: 13}));
        root.addChild(treeModel.parse({id: 10}));
        assert.deepEqual(root.model.children, [{id: 11}, {id: 12}, {id: 13}, {id: 10}]);
      });
    });

    describe('getPath()', function () {<commit_msg>updates tests to illustrate `Array.sort` instability in `parse()`<commit_after>/* global describe, it, beforeEach */

var chai, assert, sinon, spy, TreeModel;
chai = require('chai');
sinon = require('sinon');
spy = sinon.spy;
TreeModel = require('../src/TreeModel');
assert = chai.assert;
chai.Assertion.includeStack = true;

describe('TreeModel', function () {
  'use strict';

  function idEq(id) {
    return function (node) {
      return node.model.id === id;
    };
  }

  describe('with default configuration', function () {
    var treeModel;

    beforeEach(function () {
      treeModel = new TreeModel();
    });

    describe('parse()', function () {
      it('should throw an error when model is a number (not an object)', function () {
        assert.throws(treeModel.parse.bind(treeModel, 1), TypeError, 'Model must be of type object.');
      });

      it('should throw an error when model is a string (not an object)', function () {
        assert.throws(treeModel.parse.bind(treeModel, 'string'), TypeError, 'Model must be of type object.');
      });

      it('should throw an error when some child in the model is not an object', function () {
        assert.throws(
          treeModel.parse.bind(treeModel, {children: ['string']}),
          TypeError,
          'Model must be of type object.');
      });

      it('should create a root node when given a model without children', function () {
        var root;

        root = treeModel.parse({id: 1});

        assert.isUndefined(root.parent);
        assert.isArray(root.children);
        assert.lengthOf(root.children, 0);
        assert.deepEqual(root.model, {id: 1});
      });

      it('should create a root and the respective children when given a model with children', function () {
        var root, node12;

        root = treeModel.parse({
          id: 1,
          children: [
            {
              id: 11,
              children: [{id: 111}]
            },
            {
              id: 12,
              children: [{id: 121}, {id: 122}, {id: 123}, {id: 124}, {id: 125}, {id: 126}, {id: 127}, {id: 128}, {id: 129}]
            }
          ]
        });

        assert.isUndefined(root.parent);
        assert.isArray(root.children);
        assert.lengthOf(root.children, 2);
        assert.deepEqual(root.model, {
          id: 1,
          children: [
            {
              id: 11,
              children: [{id: 111}]
            },
            {
              id: 12,
              children: [{id: 121}, {id: 122}, {id: 123}, {id: 124}, {id: 125}, {id: 126}, {id: 127}, {id: 128}, {id: 129}]
            }
          ]
        });

        assert.deepEqual(root, root.children[0].parent);
        assert.deepEqual(root, root.children[1].parent);

        node12 = root.children[1];
        assert.isArray(node12.children);
        assert.lengthOf(node12.children, 9);
        assert.deepEqual(node12.model, {
          id: 12,
	  children: [{id: 121}, {id: 122}, {id: 123}, {id: 124}, {id: 125}, {id: 126}, {id: 127}, {id: 128}, {id: 129}]
        });

        assert.deepEqual(node12, node12.children[0].parent);
        assert.deepEqual(node12, node12.children[1].parent);
      });
    });

    describe('addChild()', function () {
      var root;

      beforeEach(function () {
        root = treeModel.parse({id: 1, children: [{id: 11}, {id: 12}]});
      });

      it('should add child to the end', function () {
        root.addChild(treeModel.parse({id: 13}));
        root.addChild(treeModel.parse({id: 10}));
        assert.deepEqual(root.model.children, [{id: 11}, {id: 12}, {id: 13}, {id: 10}]);
      });
    });

    describe('getPath()', function () {
{'fraction_non_alphanumeric': 0.15635593220338984, 'fraction_numerical': 0.032062146892655366, 'mean_word_length': 2.102979842243646, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5651892', 'n_tokens_mistral': 2301, 'n_tokens_neox': 2069, 'n_words': 553}