/**
 * Created by Mai on 2017/4/1.
 */

const fs = require('fs');
var test = require('tape');
var idTreeUtil = null;
var idTreeSetting = {
    id: 'ID',
    pid: 'ParentID',
    nid: 'NextSiblingID',
    rootId: -1,
    autoUpdate: false
};
var testTreeData = null;
var testTree = null;

var getKeyIDs = function (node) {
    var data = [];
    if (node) {
        data.push(node.getID());
        data.push(node.getParentID());
        data.push(node.getNextSiblingID());
    } else {
        for (var i = 1; i <= 3; i++) {
            data.push(-1);
        }
    }
    return data;
}

test('loading idTree Module: ', function (t) {
    var idTreeCode = fs.readFileSync(__dirname + '../../../../public/web/id_tree.js', 'utf8', 'r');
    eval(idTreeCode);
    idTreeUtil = idTree;
    t.pass('pass for loading idTree Module');
    t.end();
});

test('loading Testing Data: ', function (t) {
    var idTreeDataCode = fs.readFileSync(__dirname + '../../../tmp_data/data_15690.js', 'utf8', 'r');
    eval(idTreeDataCode);
    testTreeData = datas;
    t.pass('pass for loading Testing Data');
    t.end();
});

test('idTree loading Testing Data: ', function (t) {
    t.plan(1);
    testTree = idTreeUtil.createNew(idTreeSetting);
    testTree.loadDatas(testTreeData);
    t.equal(testTree.count(), testTreeData.length);
    t.end();
});

test('upMove Node: ', function (t) {
    t.plan(3);
    var testNodeID = 13431;
    var testResult = [[13429, 13427, 13431], [13430, 13427, 13432], [13431, 13427, 13430]];
    var node = testTree.findNode(testNodeID), pre = node.preSibling, prePre = pre ? pre.preSibling : null;
    node.upMove();
    t.deepEqual(getKeyIDs(prePre), testResult[0]);
    t.deepEqual(getKeyIDs(pre), testResult[1]);
    t.deepEqual(getKeyIDs(node), testResult[2]);
    t.end();
});

test('downMove Node: ', function (t) {
    t.plan(3);
    var testNodeID = 13431;
    var testResult = [[13429, 13427, 13430], [13430, 13427, 13431], [13431, 13427, 13432]];
    var node = testTree.findNode(testNodeID), pre = node.preSibling, next = node.nextSibling;
    node.downMove();
    t.deepEqual(getKeyIDs(pre), testResult[0]);
    t.deepEqual(getKeyIDs(next), testResult[1]);
    t.deepEqual(getKeyIDs(node), testResult[2]);
    t.end();
});

test('upLevel Node: ', function (t) {
    t.plan(4);
    var testNodeID = 13431;
    var node = testTree.findNode(testNodeID), orgPre = node.preSibling, orgNext = node.nextSibling, orgParent = node.parent;
    var orgChildrenCount = node.children.length, orgNextSiblingCount = orgParent.children.length - node.siblingIndex() - 1;
    node.upLevel();
    t.equal(orgNext.parent, node);
    t.equal(node.children.length, orgChildrenCount + orgNextSiblingCount);
    t.equal(orgPre.nextSibling, null);
    t.equal(orgParent.nextSibling, node);
    t.end();
});

test('downLevel Node: ', function (t) {
    t.plan(4);
    var testNodeID = 13431;
    var node = testTree.findNode(testNodeID), orgPre = node.preSibling, orgNext = node.nextSibling, orgParent = node.parent;
    var newPre = orgPre.lastChild();
    node.downLevel();
    t.equal(orgPre.nextSibling, orgNext);
    t.equal(orgPre.lastChild(), node);
    t.equal(newPre.nextSibling, node);
    t.equal(node.nextSibling, null);
    t.end();
});

test('insert Node: ', function (t) {
    t.plan(5);
    var parent = testTree.findNode(13574), next = testTree.findNode(13598);
    var orgTreeCount = testTree.count(), orgParentChildrenCount = parent.children.length, orgNextPre = next.preSibling;
    var newNode = testTree.insert(parent.getID(), next.getID());
    t.equal(testTree.count(), orgTreeCount + 1);
    t.equal(parent.children.length, orgParentChildrenCount + 1);
    t.equal(orgNextPre.nextSibling, newNode);
    t.equal(newNode.parent, parent);
    t.equal(newNode.nextSibling, next);
    t.end();
});

test('delete Node: ', function (t) {
    t.plan(3);
    var testNodeID = 13598;
    var node = testTree.findNode(testNodeID), orgParent = node.parent, orgPre = node.preSibling, orgNext = node.nextSibling;
    var orgTreeCount = testTree.count(), orgParentChildrenCount = orgParent.children.length;
    testTree.delete(node);
    t.equal(testTree.count(), orgTreeCount - node.posterityCount() - 1);
    t.equal(orgParent.children.length, orgParentChildrenCount - 1);
    t.equal(orgPre.nextSibling, orgNext);
    t.end();
});
