<commit_before>/// <reference path="../../../lib/DefinitelyTyped/jasmine/jasmine.d.ts"/>
/// <reference path="../../../lib/DefinitelyTyped/q/Q.d.ts"/>

import Config = require("./Config");
import WS = require("./WS");

export var register = () => {
    describe("Services/WS", () => {
        var config: Config.Type = {
            templatePath: "mock",
            jsonPrefix: "mock",
            wsuri: "mock"
        };

        // constructor for a mock raw web socket that leaks the
        // constructee for inspection.
        var wsRaw: any;
        var constructRawWebSocket = (uri: string): WS.RawWebSocket => {
            wsRaw = <any>jasmine.createSpyObj("RawWebSocketMock", ["send", "onmessage", "onerror", "onopen", "onclose"]);
            return wsRaw;
        };
        var ws;

        beforeEach(() => {
            ws = WS.factory(config, constructRawWebSocket);
        });

        it("does not initially call the send method of web socket", () => {
            expect(wsRaw.send.calls.any()).toEqual(false);
        });

        it("returns '0' as callback handle on first registration", () => {
            expect(ws.register("/adhocracy", () => null)).toEqual("0");
        });

        it("returns '1' as callback handle on second registration", () => {
            ws.register("/adhocracy", () => null);
            expect(ws.register("/adhocracy/somethingelse", () => null)).toEqual("1");
        });

        it("calls the send method of web socket on every register to different resources", () => {
            expect(wsRaw.send.calls.count()).toEqual(0);
            ws.register("/adhocracy", () => null);
            expect(wsRaw.send.calls.count()).toEqual(1);
            ws.register("/adhocracy/somethingelse", () => null);
            expect(wsRaw.send.calls.count()).toEqual(2);
        });

        it("sends only one subscription for multiple registrations to same resource", () => {
            var resource = "/adhocracy/sidty";
            ws.register(resource, () => null);
            ws.register(resource, () => null);
            expect(wsRaw.send.calls.count()).toEqual(1);
            ws.register(resource, () => null);
            expect(wsRaw.send.calls.count()).toEqual(1);
        });

        it("calls callbacks only between being registered and being unregistered", () => {
            // register spy object as callback
            var cb = {
                cb: (event) => null
            };
            (<any>spyOn(cb, "cb")).and.callThrough();
            var handle: string = ws.register("/adhocracy/somethingactive", cb.cb);

            expect((<any>cb.cb).calls.count()).toEqual(0);

            // trigger event
            wsRaw.onmessage({ data: JSON.stringify({
                resource: "/adhocracy/somethingactive",
                event: "new_child",
                child: "/adhocracy/somethingactive/fiyudt8y"
            }, null, 2)});

            expect((<any>cb.cb).calls.count()).toEqual(1);

            // unregister callback
            ws.unregister("/adhocracy/somethingactive", handle);

            // trigger another event
            wsRaw.onmessage({ data: JSON.stringify({
                resource: "/adhocracy/somethingactive",
                event: "new_child",
                child: "/adhocracy/somethingactive/q2u7"
            }, null, 2)});

            expect((<any>cb.cb).calls.count()).toEqual(1);
        });

        it("throws an exception on un-registration of a non-existing handle", () => {
            expect(() => ws.unregister("/adhocracy/somethingelse", "81")).toThrow();
        });
    });
};
<commit_msg>add FIXME: spec module structure<commit_after>/// <reference path="../../../lib/DefinitelyTyped/jasmine/jasmine.d.ts"/>
/// <reference path="../../../lib/DefinitelyTyped/q/Q.d.ts"/>

import Config = require("./Config");
import WS = require("./WS");

export var register = () => {
    // FIXME: This should be ``describe("WS")`` and should be wrapped in a
    // ``describe("Services")`` to mirror the module structure. This is not
    // currently possible because the script that generates AdhocracySpec.ts
    // includes all *Spec.ts modules.

    describe("Services/WS", () => {
        var config: Config.Type = {
            templatePath: "mock",
            jsonPrefix: "mock",
            wsuri: "mock"
        };

        // constructor for a mock raw web socket that leaks the
        // constructee for inspection.
        var wsRaw: any;
        var constructRawWebSocket = (uri: string): WS.RawWebSocket => {
            wsRaw = <any>jasmine.createSpyObj("RawWebSocketMock", ["send", "onmessage", "onerror", "onopen", "onclose"]);
            return wsRaw;
        };
        var ws;

        beforeEach(() => {
            ws = WS.factory(config, constructRawWebSocket);
        });

        it("does not initially call the send method of web socket", () => {
            expect(wsRaw.send.calls.any()).toEqual(false);
        });

        it("returns '0' as callback handle on first registration", () => {
            expect(ws.register("/adhocracy", () => null)).toEqual("0");
        });

        it("returns '1' as callback handle on second registration", () => {
            ws.register("/adhocracy", () => null);
            expect(ws.register("/adhocracy/somethingelse", () => null)).toEqual("1");
        });

        it("calls the send method of web socket on every register to different resources", () => {
            expect(wsRaw.send.calls.count()).toEqual(0);
            ws.register("/adhocracy", () => null);
            expect(wsRaw.send.calls.count()).toEqual(1);
            ws.register("/adhocracy/somethingelse", () => null);
            expect(wsRaw.send.calls.count()).toEqual(2);
        });

        it("sends only one subscription for multiple registrations to same resource", () => {
            var resource = "/adhocracy/sidty";
            ws.register(resource, () => null);
            ws.register(resource, () => null);
            expect(wsRaw.send.calls.count()).toEqual(1);
            ws.register(resource, () => null);
            expect(wsRaw.send.calls.count()).toEqual(1);
        });

        it("calls callbacks only between being registered and being unregistered", () => {
            // register spy object as callback
            var cb = {
                cb: (event) => null
            };
            (<any>spyOn(cb, "cb")).and.callThrough();
            var handle: string = ws.register("/adhocracy/somethingactive", cb.cb);

            expect((<any>cb.cb).calls.count()).toEqual(0);

            // trigger event
            wsRaw.onmessage({ data: JSON.stringify({
                resource: "/adhocracy/somethingactive",
                event: "new_child",
                child: "/adhocracy/somethingactive/fiyudt8y"
            }, null, 2)});

            expect((<any>cb.cb).calls.count()).toEqual(1);

            // unregister callback
            ws.unregister("/adhocracy/somethingactive", handle);

            // trigger another event
            wsRaw.onmessage({ data: JSON.stringify({
                resource: "/adhocracy/somethingactive",
                event: "new_child",
                child: "/adhocracy/somethingactive/q2u7"
            }, null, 2)});

            expect((<any>cb.cb).calls.count()).toEqual(1);
        });

        it("throws an exception on un-registration of a non-existing handle", () => {
            expect(() => ws.unregister("/adhocracy/somethingelse", "81")).toThrow();
        });
    });
};

{'fraction_non_alphanumeric': 0.16511689691817216, 'fraction_numerical': 0.005047821466524973, 'mean_word_length': 2.009192645883293, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 59, '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': '4046172', 'n_tokens_mistral': 2125, 'n_tokens_neox': 2012, 'n_words': 448}