<commit_before>//
// HTML Worker Tests.
//

"use strict";

QUnit.test("Receive connect from worker", function(assert) {
    assert.timeout(500);
    var done = assert.async();

    var worker = new Worker("connect-worker.js");
    worker.postMessage(["init", {}]);

    worker.onmessage = function(e) {
        var type = e.data.type;
        assert.equal(type, "connect");
        worker.terminate();
        done();
    }
});

QUnit.test("Connect and connack worker", function(assert) {
    assert.timeout(500);
    var done = assert.async();

    var worker = new Worker("hello-world-worker.js");
    worker.postMessage(["init", {}]);

    var connected = false;

    worker.onmessage = function(e) {
        if(!connected) {
            var type = e.data.type;
            assert.equal(type, "connect");
            connected = true;
            worker.postMessage({type: "connack"})
        } else {
            // The hello world worker sends a normal postMessage
            assert.equal(e.data, "Hello world!")
            worker.terminate();
            done();
        }
    }
});

QUnit.test("Connect and subscribe worker", function(assert) {
    assert.timeout(500);
    var done = assert.async();

    var connected = false;
    var subscribed = false;
    var worker = new Worker("subscribe-worker.js");
    worker.postMessage(["init", {}]);

    worker.onmessage = function(e) {
        var type = e.data.type;

        if(!connected) {
            assert.equal(type, "connect");
            connected = true;
            worker.postMessage({type: "connack"})
            return;
        }

        if(!subscribed) {
            assert.equal(type, "subscribe");
            subscribed = true;
            worker.postMessage({type: "suback", packet_id: e.data.packet_id, acks: [0]})
            worker.terminate();
            done();
        }
    }
});

QUnit.test("Connect, subscribe and publish to worker", function(assert) {
    assert.timeout(500);
    var done = assert.async();

    var worker = new Worker("subscribe-publish-worker.js");
    worker.postMessage(["init", {}]);

    var connected = false;
    var subscribed = false;

    worker.onmessage = function(e) {
        var type = e.data.type;

        if(e.data == "Hello to you too") {
            worker.terminate();
            done();
        }

        if(!connected) {
            assert.equal(type, "connect");
            connected = true;
            worker.postMessage({type: "connack"})
            return;
        }

        if(!subscribed) {
            assert.equal(type, "subscribe");
            subscribed = true;

            worker.postMessage({type: "suback", packet_id: e.data.packet_id, acks: [0]});
            worker.postMessage({type: "publish", topic: "test/a/b", payload: "Hi"});

            return;
        }
    }
});

QUnit.test("Connect, subscribe and unsubscribe worker", function(assert) {
    assert.timeout(500);
    var done = assert.async();

    var connected = false;
    var subscribed1 = false;
    var subscribed2 = false;
    var unsubscribed = false;
    var worker = new Worker("subscribe-unsubscribe-worker.js");
    worker.postMessage(["init", {}]);

    worker.onmessage = function(e) {

        if (e.data === 1) {
            worker.terminate();
            done();
        }

        var type = e.data.type;

        if(!connected) {
            assert.equal(type, "connect");
            connected = true;
            worker.postMessage({type: "connack"})
            return;
        }

        if(!subscribed1) {
            assert.equal(type, "subscribe");
            subscribed1 = true;
            worker.postMessage({type: "suback", packet_id: e.data.packet_id, acks: [0]})
            return;
        }

        if(!subscribed2) {
            assert.equal(type, "subscribe");
            subscribed2 = true;
            worker.postMessage({type: "suback", packet_id: e.data.packet_id, acks: [0]})
            worker.postMessage({type: "publish", topic: "test/a/b", payload: "Hi-1"});
            return;
        }

        if(!unsubscribed) {
            assert.equal(type, "unsubscribe");
            unsubscribed = true;
            worker.postMessage({type: "unsuback", packet_id: e.data.packet_id, acks: [0]})
            worker.postMessage({type: "publish", topic: "test/a/b", payload: "Hi-2"});
            worker.postMessage({type: "publish", topic: "test/check", payload: ""});
            return;
        }
    }
});<commit_msg>Removed some worker tests, the protocol is different now<commit_after>//
// HTML Worker Tests.
//

"use strict";

QUnit.test("Receive connect from worker", function(assert) {
    assert.timeout(500);
    var done = assert.async();

    var worker = new Worker("connect-worker.js");
    worker.postMessage(["init", {}]);

    worker.onmessage = function(e) {
        var type = e.data.type;
        assert.equal(type, "connect");
        worker.terminate();
        done();
    }
});

QUnit.test("Connect and subscribe worker", function(assert) {
    assert.timeout(500);
    var done = assert.async();

    var connected = false;
    var subscribed = false;
    var worker = new Worker("subscribe-worker.js");
    worker.postMessage(["init", {}]);

    worker.onmessage = function(e) {
        var type = e.data.type;

        if(!connected) {
            assert.equal(type, "connect");
            connected = true;
            worker.postMessage({type: "connack"})
            return;
        }

        if(!subscribed) {
            assert.equal(type, "subscribe");
            subscribed = true;
            worker.postMessage({type: "suback", packet_id: e.data.packet_id, acks: [0]})
            worker.terminate();
            done();
        }
    }
});

{'fraction_non_alphanumeric': 0.15069686411149827, 'fraction_numerical': 0.0062717770034843206, 'mean_word_length': 2.0504782146652496, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5871914', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1656, 'n_words': 359}