<commit_before><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test jQluster.RemoteSelectorFactory</title>
<link rel="stylesheet" href="./qunit.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="../js/lib/jquery.js"></script>
<script src="../js/lib/jquery.ellocate.js"></script>
<script src="../js/lib/jquery.xpath.js"></script>
<script src="./qunit.js"></script>
<script src="../js/util.js"></script>
<script src="../js/connection.js"></script>
<script src="../js/local_server.js"></script>
<script src="../js/transport.js"></script>
<script src="../js/remote_selector.js"></script>
<script src="../js/remote_selector_factory.js"></script>
<script src="../js/jquery_adaptor.js"></script>
<script src="./test_utils.js"></script>
<script>
"use strict";


var alice;
var bob;

var setupHTML = function() {
    var html =
          '<ul class="list">'
        +   '<li>1</li>'
        +   '<li>2</li>'
        +   '<li>3</li>'
        + '</ul>';
    $("#qunit-fixture").append(html);
};

var teardownjQluster = function() {
    alice = undefined;
    bob = undefined;
    jQluster.RemoteSelectorFactory.releaseLocalServer();
    jQluster.RemoteSelectorFactory.releaseRegisteredListeners();
};

module("RemoteSelectorFactory", {
    setup: function() {
        alice = new jQluster.RemoteSelectorFactory({
            my_remote_id: "alice",
            transport_id: "local",
        });
        bob = new jQluster.RemoteSelectorFactory({
            my_remote_id: "bob",
            transport_id: "local"
        });
    },
    teardown: teardownjQluster
});

$.each([
    {label: "selector", target: "#hoge",  exp_eval_code: '$("#hoge")'},
    {label: "document", target: document, exp_eval_code: '$(document)'},
    {label: 'window', target: window, exp_eval_code: '$(window)'},
    {label: 'XPath remote pointer (same node)',
     target: {remote_type: "xpath", remote_id: "bob", remote_xpath: "//html[1]/body[1]/div[@id='hoge']"},
     exp_eval_code: "$(document).xpath(\"//html[1]/body[1]/div[@id='hoge']\")"},
    {label: 'XPath remote pointer (different node)',
     target: {remote_type: "xpath", remote_id: "carol", remote_xpath: "//html[1]/body[1]/div[@id='foobar']"},
     exp_remote_id: 'carol', exp_eval_code: "$(document).xpath(\"//html[1]/body[1]/div[@id='foobar']\")"}
], function(i, testcase) {
    if(!testcase.exp_remote_id) {
        testcase.exp_remote_id = "bob";
    }
    var checker = function(remote) {
        is(remote.remote_id, testcase.exp_remote_id, "remote object to bob");
        is(remote._getEvalCode(), testcase.exp_eval_code, "eval code OK");
    };
    test(testcase.label + ", function interface", function() {
        var factory = alice.forRemote("bob");
        var remote = factory(testcase.target);
        checker(remote);
    });
    test(testcase.label + ", direct interface", function() {
        var remote = alice.forRemote("bob", testcase.target);
        checker(remote);
    });
});

module("RemoteSelectorFactory: listen to readiness of remote nodes", {
    setup: function() {
        setupHTML();
        alice = new jQluster.RemoteSelectorFactory({
            my_remote_id: "alice",
            transport_id: "local"
        });
    },
    teardown: teardownjQluster
});

asyncTest("$remote(func) should add callbacks for readiness of the remote node", function() {
    var $remote_bob = alice.forRemote("bob");
    var results = [];
    $remote_bob(function() { results.push(1); });
    $remote_bob(function() { results.push(2); });
    $remote_bob(function() { results.push(3); });
    delay(0.05).then(function() {
        deepEqual(results, [], "callbacks should not be executed yet");
        bob = new jQluster.RemoteSelectorFactory({
            my_remote_id: "bob",
            transport_id: "local",
            notify_to: "alice"
        });
        return delay(0.05);
    }).then(function() {
        deepEqual(results, [1,2,3], "callbacks should be executed in order");
    }).then(null, function(error) {
        ok(false, "error: " + error);
    }).always(function() {
        start();
    });
});

asyncTest("$remote(func) should add callbacks, but they are not executed if not notified", function() {
    var $remote_bob = alice.forRemote("bob");
    var results = [];
    $remote_bob(function() { results.push(1); });
    bob = new jQluster.RemoteSelectorFactory({
        my_remote_id: "bob",
        transport_id: "local"
    });
    delay(0.05).then(function() {
        deepEqual(results, [], "callback never executed.");
    }, function(error) {
        ok(false, "error: " + error);
    }).always(function() {
        start();
    });
});

asyncTest("$remote(func) callback should be exeucted immediately if the remote node is already present.", function() {
    var $remote_bob = alice.forRemote("bob");
    bob = new jQluster.RemoteSelectorFactory({
        my_remote_id: "bob",
        transport_id: "local",
        notify_to: ["alice"]
    });
    var results = [];
    delay(0.05).then(function() {
        $remote_bob(function() { results.push(1); });
        $remote_bob(function() { results.push(2); });
        $remote_bob(function() { results.push(3); });
        return delay(0.05);
    }).then(function() {
        deepEqual(results, [1,2,3], "callbacks executed.");
    }, function(error) {
        ok(false, "error: " + error);
    }).always(function() {
        start();
    });
});

asyncTest("$remote(func) callback should not be executed if the remote node is present but it does not notify you.", function() {
    var $remote_bob = alice.forRemote("bob");
    bob = new jQluster.RemoteSelectorFactory({
        my_remote_id: "bob",
        transport_id: "local",
        notify_to: ["carol", "david"]
    });
    var results = [];
    delay(0.05).then(function() {
        $remote_bob(function() { results.push(1); });
        return delay(0.05);
    }).then(function() {
        deepEqual(results, [], "callback should not be executed.");
    }, function(error) {
        ok(false, "error: " + error);
    }).always(function() {
        start();
    });
});

asyncTest("All $remote(func) callbacks should be executed if they are notified", function() {
    var $rbob_alice = alice.forRemote("bob");
    var carol = new jQluster.RemoteSelectorFactory({
        my_remote_id: "carol", transport_id: "local"
    });
    var $rbob_carol = carol.forRemote("bob");
    var david = new jQluster.RemoteSelectorFactory({
        my_remote_id: "david", transport_id: "local"
    });
    var $rbob_david = david.forRemote("bob");
    var callbacked = {
        alice: 0, carol: 0, david: 0
    };
    $rbob_alice(function() { callbacked.alice += 1; });
    $rbob_carol(function() { callbacked.carol += 1; });
    $rbob_david(function() { callbacked.david += 1; });
    delay(0.05).then(function() {
        deepEqual(callbacked, {alice:0, carol:0, david:0});
        bob = new jQluster.RemoteSelectorFactory({
            my_remote_id: "bob", transport_id: "local",
            notify_to: ["alice", "carol"]
        });
        return delay(0.05);
    }).then(function() {
        deepEqual(callbacked, {alice: 1, carol: 1, david: 0});
    }, function(error) {
        ok(false, "error: " + error);
    }).always(function() {<commit_msg>jstest/factory: add tests to check the argument of the readiness callback is the remote jQuery function.<commit_after><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test jQluster.RemoteSelectorFactory</title>
<link rel="stylesheet" href="./qunit.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="../js/lib/jquery.js"></script>
<script src="../js/lib/jquery.ellocate.js"></script>
<script src="../js/lib/jquery.xpath.js"></script>
<script src="./qunit.js"></script>
<script src="../js/util.js"></script>
<script src="../js/connection.js"></script>
<script src="../js/local_server.js"></script>
<script src="../js/transport.js"></script>
<script src="../js/remote_selector.js"></script>
<script src="../js/remote_selector_factory.js"></script>
<script src="../js/jquery_adaptor.js"></script>
<script src="./test_utils.js"></script>
<script>
"use strict";


var alice;
var bob;

var setupHTML = function() {
    var html =
          '<ul class="list">'
        +   '<li>1</li>'
        +   '<li>2</li>'
        +   '<li>3</li>'
        + '</ul>';
    $("#qunit-fixture").append(html);
};

var teardownjQluster = function() {
    alice = undefined;
    bob = undefined;
    jQluster.RemoteSelectorFactory.releaseLocalServer();
    jQluster.RemoteSelectorFactory.releaseRegisteredListeners();
};

module("RemoteSelectorFactory", {
    setup: function() {
        alice = new jQluster.RemoteSelectorFactory({
            my_remote_id: "alice",
            transport_id: "local",
        });
        bob = new jQluster.RemoteSelectorFactory({
            my_remote_id: "bob",
            transport_id: "local"
        });
    },
    teardown: teardownjQluster
});

$.each([
    {label: "selector", target: "#hoge",  exp_eval_code: '$("#hoge")'},
    {label: "document", target: document, exp_eval_code: '$(document)'},
    {label: 'window', target: window, exp_eval_code: '$(window)'},
    {label: 'XPath remote pointer (same node)',
     target: {remote_type: "xpath", remote_id: "bob", remote_xpath: "//html[1]/body[1]/div[@id='hoge']"},
     exp_eval_code: "$(document).xpath(\"//html[1]/body[1]/div[@id='hoge']\")"},
    {label: 'XPath remote pointer (different node)',
     target: {remote_type: "xpath", remote_id: "carol", remote_xpath: "//html[1]/body[1]/div[@id='foobar']"},
     exp_remote_id: 'carol', exp_eval_code: "$(document).xpath(\"//html[1]/body[1]/div[@id='foobar']\")"}
], function(i, testcase) {
    if(!testcase.exp_remote_id) {
        testcase.exp_remote_id = "bob";
    }
    var checker = function(remote) {
        is(remote.remote_id, testcase.exp_remote_id, "remote object to bob");
        is(remote._getEvalCode(), testcase.exp_eval_code, "eval code OK");
    };
    test(testcase.label + ", function interface", function() {
        var factory = alice.forRemote("bob");
        var remote = factory(testcase.target);
        checker(remote);
    });
    test(testcase.label + ", direct interface", function() {
        var remote = alice.forRemote("bob", testcase.target);
        checker(remote);
    });
});

module("RemoteSelectorFactory: listen to readiness of remote nodes", {
    setup: function() {
        setupHTML();
        alice = new jQluster.RemoteSelectorFactory({
            my_remote_id: "alice",
            transport_id: "local"
        });
    },
    teardown: teardownjQluster
});

asyncTest("$remote(func) should add callbacks for readiness of the remote node", function() {
    var $remote_bob = alice.forRemote("bob");
    var results = [];
    $remote_bob(function($arg) {
        is($arg, $remote_bob);
        results.push(1);
    });
    $remote_bob(function() { results.push(2); });
    $remote_bob(function() { results.push(3); });
    delay(0.05).then(function() {
        deepEqual(results, [], "callbacks should not be executed yet");
        bob = new jQluster.RemoteSelectorFactory({
            my_remote_id: "bob",
            transport_id: "local",
            notify_to: "alice"
        });
        return delay(0.05);
    }).then(function() {
        deepEqual(results, [1,2,3], "callbacks should be executed in order");
    }).then(null, function(error) {
        ok(false, "error: " + error);
    }).always(function() {
        start();
    });
});

asyncTest("$remote(func) should add callbacks, but they are not executed if not notified", function() {
    var $remote_bob = alice.forRemote("bob");
    var results = [];
    $remote_bob(function() { results.push(1); });
    bob = new jQluster.RemoteSelectorFactory({
        my_remote_id: "bob",
        transport_id: "local"
    });
    delay(0.05).then(function() {
        deepEqual(results, [], "callback never executed.");
    }, function(error) {
        ok(false, "error: " + error);
    }).always(function() {
        start();
    });
});

asyncTest("$remote(func) callback should be exeucted immediately if the remote node is already present.", function() {
    var $remote_bob = alice.forRemote("bob");
    bob = new jQluster.RemoteSelectorFactory({
        my_remote_id: "bob",
        transport_id: "local",
        notify_to: ["alice"]
    });
    var results = [];
    delay(0.05).then(function() {
        $remote_bob(function($arg) {
            is($arg, $remote_bob);
            results.push(1);
        });
        $remote_bob(function() { results.push(2); });
        $remote_bob(function() { results.push(3); });
        return delay(0.05);
    }).then(function() {
        deepEqual(results, [1,2,3], "callbacks executed.");
    }, function(error) {
        ok(false, "error: " + error);
    }).always(function() {
        start();
    });
});

asyncTest("$remote(func) callback should not be executed if the remote node is present but it does not notify you.", function() {
    var $remote_bob = alice.forRemote("bob");
    bob = new jQluster.RemoteSelectorFactory({
        my_remote_id: "bob",
        transport_id: "local",
        notify_to: ["carol", "david"]
    });
    var results = [];
    delay(0.05).then(function() {
        $remote_bob(function() { results.push(1); });
        return delay(0.05);
    }).then(function() {
        deepEqual(results, [], "callback should not be executed.");
    }, function(error) {
        ok(false, "error: " + error);
    }).always(function() {
        start();
    });
});

asyncTest("All $remote(func) callbacks should be executed if they are notified", function() {
    var $rbob_alice = alice.forRemote("bob");
    var carol = new jQluster.RemoteSelectorFactory({
        my_remote_id: "carol", transport_id: "local"
    });
    var $rbob_carol = carol.forRemote("bob");
    var david = new jQluster.RemoteSelectorFactory({
        my_remote_id: "david", transport_id: "local"
    });
    var $rbob_david = david.forRemote("bob");
    var callbacked = {
        alice: 0, carol: 0, david: 0
    };
    $rbob_alice(function($arg) {
        is($arg, $rbob_alice);
        callbacked.alice += 1;
    });
    $rbob_carol(function($arg) {
        is($arg, $rbob_carol);
        callbacked.carol += 1;
    });
    $rbob_david(function() { callbacked.david += 1; });
    delay(0.05).then(function() {
        deepEqual(callbacked, {alice:0, carol:0, david:0});
        bob = new jQluster.RemoteSelectorFactory({
            my_remote_id: "bob", transport_id: "local",
            notify_to: ["alice", "carol"]
        });
        return delay(0.05);
    }).then(function() {
        deepEqual(callbacked, {alice: 1, carol: 1, david: 0});
    }, function(error) {
        ok(false, "error: " + error);
    }).always(function() {
{'fraction_non_alphanumeric': 0.1836982148871674, 'fraction_numerical': 0.008757157292017514, 'mean_word_length': 3.0607221006564553, 'pattern_counts': {'":': 0, '<': 95, '<?xml version=': 0, '>': 95, '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': '1313365', 'n_tokens_mistral': 4928, 'n_tokens_neox': 4814, 'n_words': 973}