global.assert = require('assert');
var fs = require('fs');
var Handlebars = require('handlebars');
require('third/string-prototype-codepointat/codepointat.js');

global.Dict = require('js/dict');
global._ = require('third/underscore/underscore.js');
var _ = global._;

// Run all the JS scripts in our test directory.  Tests do NOT run
// in isolation.

var oneFileFilter = [];
var testsDifference = [];
if (process.argv[2] ) {
    oneFileFilter = process.argv
      .slice(2)
      .map(function (filename) {return filename.replace(/\.js$/i, '');});
}

var tests = fs.readdirSync(__dirname)
  .filter(function (filename) {return (/\.js$/i).test(filename);})
  .map(function (filename) {return filename.replace(/\.js$/i, '');});

if (oneFileFilter.length > 0) {
    tests = tests.filter(function (filename) {
        return oneFileFilter.indexOf(filename) !== -1;
    });
    testsDifference = _.difference(oneFileFilter, tests);
}
tests.sort();


var dependencies = [];
var old_builtins = {};

global.set_global = function (name, val) {
    global[name] = val;
    dependencies.push(name);
    return val;
};

global.patch_builtin = function (name, val) {
    old_builtins[name] = global[name];
    global[name] = val;
    return val;
};

global.add_dependencies = function (dct) {
    _.each(dct, function (fn, name) {
        var obj = require(fn);
        set_global(name, obj);
    });
};

function template_dir() {
    return __dirname + '/../../static/templates/';
}

global.make_sure_all_templates_have_been_compiled = function () {
    var dir = template_dir();
    var fns = fs.readdirSync(dir).filter(function (fn) {
        return (/\.handlebars/).test(fn);
    });

    _.each(fns, function (fn) {
        var name = fn.split('.')[0];
        if (!Handlebars.templates[name]) {
            throw "The file " + fn + " has no test coverage.";
        }
    });
};

global.use_template = function (name) {
    if (Handlebars.templates === undefined) {
        Handlebars.templates = {};
    }
    var data = fs.readFileSync(template_dir() + name + '.handlebars').toString();
    Handlebars.templates[name] = Handlebars.compile(data);
};

var output_fn = 'var/.test-js-with-node.html';

(function () {
    var data = '';

    data += '<link href="./static/styles/zulip.css" rel="stylesheet">\n';
    data += '<link href="./static/styles/thirdparty-fonts.css" rel="stylesheet">\n';
    data += '<link href="./static/third/bootstrap/css/bootstrap.css" rel="stylesheet">\n';
    data += '<style type="text/css">.collapse {height: inherit}</style>\n';
    data += '<style type="text/css">body {width: 500px; margin: auto; overflow: scroll}</style>\n';
    data += '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
    data += '<h1>Output of node unit tests</h1>\n';
    fs.writeFileSync(output_fn, data);
}());

global.write_test_output = function (label, output) {
    var data = '';

    data += '<hr>';
    data += '<h3>' + label + '</h3>';
    data += output;
    data += '\n';
    fs.appendFileSync(output_fn, data);
};

global.append_test_output = function (output) {
    fs.appendFileSync(output_fn, output);
};

tests.forEach(function (filename) {
    if (filename === 'index') {
        return;
    }
    console.info('running tests for ' + filename);
    require('./' + filename);

    dependencies.forEach(function (name) {
        delete global[name];
    });
    dependencies = [];
    _.extend(global, old_builtins);
    old_builtins = {};
});

if (oneFileFilter.length > 0 && testsDifference.length > 0) {
    testsDifference.forEach(function (filename) {
        console.log(filename + " does not exist");
    });
    if (oneFileFilter.length > testsDifference.length) {
        console.info("To see more output, open " + output_fn);
    }
} else {
    console.info("To see more output, open " + output_fn);
}

@@ -48,24 +48,52 @@
 uire('fs');%0A
+var path = require('path');%0A
 var Handleba
@@ -2177,26 +2177,306 @@
 var 
-output_fn = 
+mkdir_p = function (path) %7B%0A    // This works like mkdir -p in Unix.%0A    try %7B%0A        fs.mkdirSync(path);%0A    %7D catch(e) %7B%0A        if ( e.code !== 'EEXIST' ) %7B%0A            throw e;%0A        %7D%0A    %7D%0A    return path;%0A%7D;%0A%0Avar output_dir = (function () %7B%0A    mkdir_p('var');%0A    var dir = mkdir_p(
 'var/
-.
 test
@@ -2492,14 +2492,87 @@
 node
+');%0A    return dir;%0A%7D());%0A%0Avar output_fn = path.join(output_dir, 'output
 .html'
+)
 ;%0A%0A(
@@ -2623,32 +2623,36 @@
 = '%3Clink href=%22.
+./..
 /static/styles/z
@@ -2701,32 +2701,36 @@
 = '%3Clink href=%22.
+./..
 /static/styles/t
@@ -2798,16 +2798,20 @@
  href=%22.
+./..
 /static/

Write node test output to var/test-js-with-node/output.html