<commit_before>import chai, {expect} from "chai";
import chaiAsPromised from "chai-as-promised";
import sinon from "sinon";
import sinonChai from "sinon-chai";
import EventEmitter from "wolfy87-eventemitter";

import * as subscriptionsMixin from "base-mixins/subscriptions";

chai.use(chaiAsPromised);
chai.use(sinonChai);

describe("`subscriptions` mixin", () => {

    describe("`ready` event handler", () => {

        it("triggers the `ready` event on the appropriate subscriptions", () => {
            const instance = {
                ddp: new EventEmitter()
            };
            subscriptionsMixin.init.call(instance);
            const emit1 = sinon.spy();
            instance.subscriptions.cache.byId["id1"] = {emit: emit1};
            const emit2 = sinon.spy();
            instance.subscriptions.cache.byId["id2"] = {emit: emit2};
            const emit3 = sinon.spy();
            instance.subscriptions.cache.byId["id3"] = {emit: emit3};
            instance.ddp.emit("ready", {
                subs: ["id1", "id2"]
            });
            expect(emit1).to.have.callCount(1);
            expect(emit2).to.have.callCount(1);
            expect(emit3).to.have.callCount(0);
        });

    });

    describe("`nosub` event handler", () => {

        it("triggers the `error` event on the appropriate subscription if an error occurred", () => {
            const instance = {
                ddp: new EventEmitter()
            };
            subscriptionsMixin.init.call(instance);
            const emit1 = sinon.spy();
            instance.subscriptions.cache.byId["id1"] = {emit: emit1};
            const emit2 = sinon.spy();
            instance.subscriptions.cache.byId["id2"] = {emit: emit2};
            instance.ddp.emit("nosub", {
                id: "id1",
                error: {}
            });
            expect(emit1).to.have.been.calledWith("error", {});
            expect(emit2).to.have.callCount(0);
        });

        it("does not trigger the `error` event if no error occurred", () => {
            const instance = {
                ddp: new EventEmitter()
            };
            subscriptionsMixin.init.call(instance);
            const emit1 = sinon.spy();
            instance.subscriptions.cache.byId["id1"] = {emit: emit1};
            const emit2 = sinon.spy();
            instance.subscriptions.cache.byId["id2"] = {emit: emit2};
            instance.ddp.emit("nosub", {
                id: "id1"
            });
            expect(emit1).to.have.callCount(0);
            expect(emit2).to.have.callCount(0);
        });

        it("deletes the subscription from the cache", () => {
            const instance = {
                ddp: new EventEmitter()
            };
            subscriptionsMixin.init.call(instance);
            instance.subscriptions.cache.add({
                id: "id1",
                fingerprint: "id1"
            });
            instance.subscriptions.cache.add({
                id: "id2",
                fingerprint: "id2"
            });
            instance.ddp.emit("nosub", {
                id: "id1"
            });
            expect(instance.subscriptions.cache.get("id1")).to.equal(null);
            expect(instance.subscriptions.cache.get("id2")).not.to.equal(null);
        });

    });

    describe("`connected` event handler", () => {

        it("triggers a re-subscription to cached subscriptions which are not still in ddp's queue", () => {
            const instance = {
                ddp: new EventEmitter(),
                subscribe: sinon.spy(),
                resubscribe: sinon.spy()
            };
            subscriptionsMixin.init.call(instance);
            instance.subscriptions.cache.add({
                id: "id1",
                fingerprint: "id1",
                name: "n1",
                params: ["1", "11", "111"],
                stillInQueue: false
            });
            instance.subscriptions.cache.add({
                id: "id2",
                fingerprint: "id2",
                name: "n2",
                params: ["2", "22", "222"],
                stillInQueue: false
            });
            instance.ddp.emit("connected");
            expect(instance.resubscribe).to.have.callCount(2);
        });

        it("doesn't trigger a re-subscription to cached subscriptions which are still in ddp's queue", () => {
            const instance = {
                ddp: new EventEmitter(),
                subscribe: sinon.spy(),
                resubscribe: sinon.spy()
            };
            subscriptionsMixin.init.call(instance);
            instance.subscriptions.cache.add({
                id: "id1",
                fingerprint: "id1",
                name: "1",
                params: ["1", "11", "111"],
                stillInQueue: true
            });
            instance.subscriptions.cache.add({
                id: "id2",
                fingerprint: "id2",
                name: "2",
                params: ["2", "22", "222"],
                stillInQueue: false
            });

            instance.ddp.status = 'connected';
            instance.ddp.emit("connected");

            // re-subscribe should only be called for subscriptions
            // those were not in queue already
            expect(instance.resubscribe).to.have.callCount(1);

            // all subscriptions should be removed from the queue
            instance.subscriptions.cache.forEach(sub => {
                expect(sub).to.have.property('stillInQueue', false);
            });

            // re a re-connection happens, resubscribe should be 
            // called twice more, reconnecting both subscribtions
            instance.ddp.emit("connected");
            expect(instance.resubscribe).to.have.callCount(3);
        });

    });

    describe("`subscribe` method", () => {

        it("returns the subscription from cache if it's present", () => {
            const cachedSub = {};
            const instance = {
                subscriptions: {
                    cache: {
                        get: () => cachedSub
                    }
                },
                ddp: {
                    sub: sinon.spy()
                }
            };
            const sub = subscriptionsMixin.subscribe.call(instance, "name", "param");<commit_msg>fix quotes<commit_after>import chai, {expect} from "chai";
import chaiAsPromised from "chai-as-promised";
import sinon from "sinon";
import sinonChai from "sinon-chai";
import EventEmitter from "wolfy87-eventemitter";

import * as subscriptionsMixin from "base-mixins/subscriptions";

chai.use(chaiAsPromised);
chai.use(sinonChai);

describe("`subscriptions` mixin", () => {

    describe("`ready` event handler", () => {

        it("triggers the `ready` event on the appropriate subscriptions", () => {
            const instance = {
                ddp: new EventEmitter()
            };
            subscriptionsMixin.init.call(instance);
            const emit1 = sinon.spy();
            instance.subscriptions.cache.byId["id1"] = {emit: emit1};
            const emit2 = sinon.spy();
            instance.subscriptions.cache.byId["id2"] = {emit: emit2};
            const emit3 = sinon.spy();
            instance.subscriptions.cache.byId["id3"] = {emit: emit3};
            instance.ddp.emit("ready", {
                subs: ["id1", "id2"]
            });
            expect(emit1).to.have.callCount(1);
            expect(emit2).to.have.callCount(1);
            expect(emit3).to.have.callCount(0);
        });

    });

    describe("`nosub` event handler", () => {

        it("triggers the `error` event on the appropriate subscription if an error occurred", () => {
            const instance = {
                ddp: new EventEmitter()
            };
            subscriptionsMixin.init.call(instance);
            const emit1 = sinon.spy();
            instance.subscriptions.cache.byId["id1"] = {emit: emit1};
            const emit2 = sinon.spy();
            instance.subscriptions.cache.byId["id2"] = {emit: emit2};
            instance.ddp.emit("nosub", {
                id: "id1",
                error: {}
            });
            expect(emit1).to.have.been.calledWith("error", {});
            expect(emit2).to.have.callCount(0);
        });

        it("does not trigger the `error` event if no error occurred", () => {
            const instance = {
                ddp: new EventEmitter()
            };
            subscriptionsMixin.init.call(instance);
            const emit1 = sinon.spy();
            instance.subscriptions.cache.byId["id1"] = {emit: emit1};
            const emit2 = sinon.spy();
            instance.subscriptions.cache.byId["id2"] = {emit: emit2};
            instance.ddp.emit("nosub", {
                id: "id1"
            });
            expect(emit1).to.have.callCount(0);
            expect(emit2).to.have.callCount(0);
        });

        it("deletes the subscription from the cache", () => {
            const instance = {
                ddp: new EventEmitter()
            };
            subscriptionsMixin.init.call(instance);
            instance.subscriptions.cache.add({
                id: "id1",
                fingerprint: "id1"
            });
            instance.subscriptions.cache.add({
                id: "id2",
                fingerprint: "id2"
            });
            instance.ddp.emit("nosub", {
                id: "id1"
            });
            expect(instance.subscriptions.cache.get("id1")).to.equal(null);
            expect(instance.subscriptions.cache.get("id2")).not.to.equal(null);
        });

    });

    describe("`connected` event handler", () => {

        it("triggers a re-subscription to cached subscriptions which are not still in ddp's queue", () => {
            const instance = {
                ddp: new EventEmitter(),
                subscribe: sinon.spy(),
                resubscribe: sinon.spy()
            };
            subscriptionsMixin.init.call(instance);
            instance.subscriptions.cache.add({
                id: "id1",
                fingerprint: "id1",
                name: "n1",
                params: ["1", "11", "111"],
                stillInQueue: false
            });
            instance.subscriptions.cache.add({
                id: "id2",
                fingerprint: "id2",
                name: "n2",
                params: ["2", "22", "222"],
                stillInQueue: false
            });
            instance.ddp.emit("connected");
            expect(instance.resubscribe).to.have.callCount(2);
        });

        it("doesn't trigger a re-subscription to cached subscriptions which are still in ddp's queue", () => {
            const instance = {
                ddp: new EventEmitter(),
                subscribe: sinon.spy(),
                resubscribe: sinon.spy()
            };
            subscriptionsMixin.init.call(instance);
            instance.subscriptions.cache.add({
                id: "id1",
                fingerprint: "id1",
                name: "1",
                params: ["1", "11", "111"],
                stillInQueue: true
            });
            instance.subscriptions.cache.add({
                id: "id2",
                fingerprint: "id2",
                name: "2",
                params: ["2", "22", "222"],
                stillInQueue: false
            });

            instance.ddp.status = "connected";
            instance.ddp.emit("connected");

            // re-subscribe should only be called for subscriptions
            // those were not in queue already
            expect(instance.resubscribe).to.have.callCount(1);

            // all subscriptions should be removed from the queue
            instance.subscriptions.cache.forEach(sub => {
                expect(sub).to.have.property("stillInQueue", false);
            });

            // re a re-connection happens, resubscribe should be 
            // called twice more, reconnecting both subscribtions
            instance.ddp.emit("connected");
            expect(instance.resubscribe).to.have.callCount(3);
        });

    });

    describe("`subscribe` method", () => {

        it("returns the subscription from cache if it's present", () => {
            const cachedSub = {};
            const instance = {
                subscriptions: {
                    cache: {
                        get: () => cachedSub
                    }
                },
                ddp: {
                    sub: sinon.spy()
                }
            };
            const sub = subscriptionsMixin.subscribe.call(instance, "name", "param");
{'fraction_non_alphanumeric': 0.13426108958646318, 'fraction_numerical': 0.013600063256108168, 'mean_word_length': 1.52555910543131, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3504894', 'n_tokens_mistral': 3534, 'n_tokens_neox': 3365, 'n_words': 686}