<commit_before>set_global('$', global.make_zjquery());

add_dependencies({
    util: 'js/util.js',
});

var util = global.util;
var _ = global._;

(function test_CachedValue() {
    var x = 5;

    var cv = new util.CachedValue({
        compute_value: function () {
            return x * 2;
        },
    });

    assert.equal(cv.get(), 10);

    x = 6;
    assert.equal(cv.get(), 10);
    cv.reset();
    assert.equal(cv.get(), 12);

}());

(function test_extract_pm_recipients() {
    assert.equal(util.extract_pm_recipients('<EMAIL>, <EMAIL>').length, 2);
    assert.equal(util.extract_pm_recipients('<EMAIL>, ').length, 1);
}());

(function test_rtrim() {
    assert.equal(util.rtrim('foo'), 'foo');
    assert.equal(util.rtrim('  foo'), '  foo');
    assert.equal(util.rtrim('foo  '), 'foo');
}());

(function test_lower_bound() {
    var arr = [10, 20, 30, 40, 50];
    assert.equal(util.lower_bound(arr, 5), 0);
    assert.equal(util.lower_bound(arr, 10), 0);
    assert.equal(util.lower_bound(arr, 15), 1);
    assert.equal(util.lower_bound(arr, 50), 4);
    assert.equal(util.lower_bound(arr, 55), 5);
    assert.equal(util.lower_bound(arr, 2, 4, 31), 3);

    arr = [{x: 10}, {x: 20}, {x:30}];

    function compare(a, b) {
        return a.x < b;
    }

    assert.equal(util.lower_bound(arr, 5, compare), 0);
    assert.equal(util.lower_bound(arr, 10, compare), 0);
    assert.equal(util.lower_bound(arr, 15, compare), 1);

}());

(function test_same_recipient() {
    assert(util.same_recipient(
        {type: 'stream', stream_id: 101, subject: 'Bar'},
        {type: 'stream', stream_id: 101, subject: 'bar'}));

    assert(!util.same_recipient(
        {type: 'stream', stream_id: 101, subject: 'Bar'},
        {type: 'stream', stream_id: 102, subject: 'whatever'}));

    assert(util.same_recipient(
        {type: 'private', to_user_ids: '101,102'},
        {type: 'private', to_user_ids: '101,102'}));

    assert(!util.same_recipient(
        {type: 'private', to_user_ids: '101,102'},
        {type: 'private', to_user_ids: '103'}));

    assert(!util.same_recipient(
        {type: 'stream', stream_id: 101, subject: 'Bar'},
        {type: 'private'}));

}());

(function test_robust_uri_decode() {
    assert.equal(util.robust_uri_decode('xxx%3Ayyy'), 'xxx:yyy');
    assert.equal(util.robust_uri_decode('xxx%3'), 'xxx');
}());


(function test_array_compare() {
    assert(util.array_compare([], []));
    assert(util.array_compare([1,2,3], [1,2,3]));
    assert(!util.array_compare([1,2], [1,2,3]));
    assert(!util.array_compare([1,2,3], [1,2]));
    assert(!util.array_compare([1,2,3,4], [1,2,3,5]));
}());

(function test_normalize_recipients() {
    assert.equal(
        util.normalize_recipients('<EMAIL>, <EMAIL>, <EMAIL>, <EMAIL> '),
        '<EMAIL>,<EMAIL>,<EMAIL>,<EMAIL>');
}());

(function test_random_int() {
    var min = 0;
    var max = 100;

    _.times(500, function () {
        var val = util.random_int(min, max);
        assert(min <= val);
        assert(val <= max);<commit_msg>node tests: Get util.js to 100% node coverage.<commit_after>set_global('$', global.make_zjquery());
set_global('page_params', {});
set_global('document', {});
set_global('window', {});

add_dependencies({
    util: 'js/util.js',
});

var util = global.util;
var _ = global._;

(function test_CachedValue() {
    var x = 5;

    var cv = new util.CachedValue({
        compute_value: function () {
            return x * 2;
        },
    });

    assert.equal(cv.get(), 10);

    x = 6;
    assert.equal(cv.get(), 10);
    cv.reset();
    assert.equal(cv.get(), 12);

}());

(function test_extract_pm_recipients() {
    assert.equal(util.extract_pm_recipients('<EMAIL>, <EMAIL>').length, 2);
    assert.equal(util.extract_pm_recipients('<EMAIL>, ').length, 1);
}());

(function test_is_pm_recipient() {
    var message = { reply_to: '<EMAIL>,<EMAIL>,<EMAIL>' };
    assert(util.is_pm_recipient('<EMAIL>', message));
    assert(util.is_pm_recipient('<EMAIL>', message));
    assert(!util.is_pm_recipient('<EMAIL>', message));
}());

(function test_rtrim() {
    assert.equal(util.rtrim('foo'), 'foo');
    assert.equal(util.rtrim('  foo'), '  foo');
    assert.equal(util.rtrim('foo  '), 'foo');
}());

(function test_lower_bound() {
    var arr = [10, 20, 30, 40, 50];
    assert.equal(util.lower_bound(arr, 5), 0);
    assert.equal(util.lower_bound(arr, 10), 0);
    assert.equal(util.lower_bound(arr, 15), 1);
    assert.equal(util.lower_bound(arr, 50), 4);
    assert.equal(util.lower_bound(arr, 55), 5);
    assert.equal(util.lower_bound(arr, 2, 4, 31), 3);

    arr = [{x: 10}, {x: 20}, {x:30}];

    function compare(a, b) {
        return a.x < b;
    }

    assert.equal(util.lower_bound(arr, 5, compare), 0);
    assert.equal(util.lower_bound(arr, 10, compare), 0);
    assert.equal(util.lower_bound(arr, 15, compare), 1);

}());

(function test_same_recipient() {
    assert(util.same_recipient(
        {type: 'stream', stream_id: 101, subject: 'Bar'},
        {type: 'stream', stream_id: 101, subject: 'bar'}));

    assert(!util.same_recipient(
        {type: 'stream', stream_id: 101, subject: 'Bar'},
        {type: 'stream', stream_id: 102, subject: 'whatever'}));

    assert(util.same_recipient(
        {type: 'private', to_user_ids: '101,102'},
        {type: 'private', to_user_ids: '101,102'}));

    assert(!util.same_recipient(
        {type: 'private', to_user_ids: '101,102'},
        {type: 'private', to_user_ids: '103'}));

    assert(!util.same_recipient(
        {type: 'stream', stream_id: 101, subject: 'Bar'},
        {type: 'private'}));

    assert(!util.same_recipient(
        {type: 'private', to_user_ids: undefined},
        {type: 'private'}));

    assert(!util.same_recipient(
        {type: 'unknown type'},
        {type: 'unknown type'}));

    assert(!util.same_recipient(
        undefined,
        {type: 'private'}));

    assert(!util.same_recipient(undefined, undefined));
}());

(function test_execute_early() {
    var doc = $(document);
    doc.one = function (event_name, f) {
        assert.equal(event_name, 'phantom_page_loaded');
        f();
    };

    var called = false;
    var func = function () { called = true; };

    global.page_params.test_suite = true;
    util.execute_early(func);
    assert(called);

    called = false;
    global.page_params.test_suite = false;
    util.execute_early(func);
    assert(called);
}());

(function test_robust_uri_decode() {
    assert.equal(util.robust_uri_decode('xxx%3Ayyy'), 'xxx:yyy');
    assert.equal(util.robust_uri_decode('xxx%3'), 'xxx');

    set_global('decodeURIComponent', function () { throw 'foo'; });
    try {
        util.robust_uri_decode('%E0%A4%A');
    } catch (e) {
        assert(e, 'foo');
    }
}());

(function test_is_mobile() {
    global.window.navigator = { userAgent: "Android" };
    assert(util.is_mobile());

    global.window.navigator = { userAgent: "Not mobile" };
    assert(!util.is_mobile());
}());

(function test_array_compare() {
    assert(util.array_compare([], []));
    assert(util.array_compare([1,2,3], [1,2,3]));
    assert(!util.array_compare([1,2], [1,2,3]));
    assert(!util.array_compare([1,2,3], [1,2]));
    assert(!util.array_compare([1,2,3,4], [1,2,3,5]));
}());

(function test_normalize_recipients() {
    assert.equal(
        util.normalize_recipients('<EMAIL>, <EMAIL>, <EMAIL>, <EMAIL> '),
        '<EMAIL>,<EMAIL>,<EMAIL>,<EMAIL>');
}());

(function test_random_int() {
    var min = 0;
    var max = 100;

    _.times(500, function () {
        var val = util.random_int(min, max);
        assert(min <= val);
        assert(val <= max);
{'fraction_non_alphanumeric': 0.20308617758598144, 'fraction_numerical': 0.03282332941022623, 'mean_word_length': 3.5361803084223014, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 31, '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': '1190421', 'n_tokens_mistral': 2986, 'n_tokens_neox': 2756, 'n_words': 494}