summaryrefslogtreecommitdiff
path: root/src/client-tests/base.js
blob: e922e0f10e83e4967aa3c5f08d202be6bf97e9b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
    This file is part of rhizi, a collaborative knowledge graph editor.
    Copyright (C) 2014-2015  Rhizi

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

var fs = require('fs'),
    jsdom = require('jsdom'),
    addScript = require('./test_util').addScript;

function dump_nodes(window) {
    var nodes = window.force.nodes();
    for(var i = 0 ; i < nodes.length; ++i) {
        console.log('nodes[' + nodes[i].id + '/' + nodes[i].type + '].[xy] = (' + nodes[i].x + ',' + nodes[i].y + ')');
    }
}

function dump_graphviz(force) {
    var i;
    var nodes = force.nodes();
    var links = force.links();
    var q = function(s) {
        if (s.search(' ') == -1) {
            return s;
        }
        return '"' + s + '"';
    };

    console.log('digraph {');
    for (i = 0 ; i < nodes.length; ++i) {
        if (nodes[i].type == 'bubble') {
            console.log(' BUBBLE;');
        } else {
            console.log(' ' + q(nodes[i].name) + ';');
        }
    }
    for (i = 0 ; i < links.length; ++i) {
        var link = links[i];
        console.log(' ' + q(link.__src.name) + ' -> ' + q(link.__dst.name) + ' [label=' + link.name + '];');
    }
    console.log('}');
}

function run_tests(settings) {
    var document = jsdom.jsdom('<html><body></body></html>');
    var window = document.parentWindow;

    window.console.log = console.log; // slightly evil
    window.process = process; // more evil
    window.is_node = true;
    if (settings.created) {
        settings.created(undefined, window);
    }
    addScript(window, '../../res/client/lib/require.js')
        .load_next('../client/app.js')
        .done(function () {
            console.log('test harness: starting script loading with requirejs');
            console.dir(window.rhizi_require_config);
            debugger;
            try {
                window.require.config(window.rhizi_require_config);
                window.requirejs(['main'], function(main) {
                    console.log('test harness: main loaded');
                    main.main();
                    if (settings.done) {
                        settings.done(undefined, window);
                    }
                    process.quit();
                });
            } catch (e) {
                debugger;
            }
        });
    //window.setInterval(function() { console.log('.'); return true; }, 100);
}

function reset(window) {
    window.require('rz_core').graph.clear();
}

function nodes(window) {
    return window.require('rz_core').force.nodes().map(function (n) {
        return n.name;
    });
}

function links(window) {
    return window.require('rz_core').force.links().map(function (l) {
        debugger;
        return [l.__src.name, l.__dst.name, l.name];
    });
}

exports.run_tests = run_tests;
exports.dump_graphviz = dump_graphviz;
exports.reset = reset;
exports.links = links;
exports.nodes = nodes;