<commit_before>"use strict";
var sdk = require("../..");
var HttpBackend = require("../mock-request");
var utils = require("../test-utils");

describe("MatrixClient opts", function() {
    var baseUrl = "http://localhost.or.something";
    var client, httpBackend;
    var userId = "@alice:localhost";
    var accessToken = "aseukfgwef";

    beforeEach(function() {
        utils.beforeEach(this);
        httpBackend = new HttpBackend();
        sdk.request(httpBackend.requestFn);
        client = sdk.createClient({
            baseUrl: baseUrl,
            userId: userId,
            accessToken: accessToken
        });
    });

    afterEach(function() {
        httpBackend.verifyNoOutstandingExpectation();
    });

    describe("without opts.store", function() {
        xit("should be able to send messages", function() {

        });

        xit("should be able to sync / get new events", function() {
            // use 'events' emissions.
        });
    });

    describe("without opts.scheduler", function() {
        xit("shouldn't retry sending events", function() {

        });

        xit("shouldn't queue events", function() {

        });

        xit("should be able to send messages", function() {

        });
    });
});<commit_msg>Implement MatrixClient tests without a scheduler/store.<commit_after>"use strict";
var sdk = require("../..");
var MatrixClient = sdk.MatrixClient;
var HttpBackend = require("../mock-request");
var utils = require("../test-utils");

describe("MatrixClient opts", function() {
    var baseUrl = "http://localhost.or.something";
    var client, httpBackend;
    var userId = "@alice:localhost";
    var userB = "@bob:localhost";
    var accessToken = "aseukfgwef";
    var roomId = "!foo:bar";
    var eventData = {
        chunk: [],
        start: "s",
        end: "e"
    };
    var initialSync = {
        end: "s_5_3",
        presence: [],
        rooms: [{
            membership: "join",
            room_id: roomId,
            messages: {
                start: "f_1_1",
                end: "f_2_2",
                chunk: [
                    utils.mkMessage({
                        room: roomId, user: userB, msg: "hello"
                    })
                ]
            },
            state: [
                utils.mkEvent({
                    type: "m.room.name", room: roomId, user: userB,
                    content: {
                        name: "Old room name"
                    }
                }),
                utils.mkMembership({
                    room: roomId, mship: "join", user: userB, name: "Bob"
                }),
                utils.mkMembership({
                    room: roomId, mship: "join", user: userId, name: "Alice"
                }),
                utils.mkEvent({
                    type: "m.room.create", room: roomId, user: userId,
                    content: {
                        creator: userId
                    }
                })
            ]
        }]
    };

    beforeEach(function() {
        utils.beforeEach(this);
        httpBackend = new HttpBackend();
    });

    afterEach(function() {
        httpBackend.verifyNoOutstandingExpectation();
    });

    describe("without opts.store", function() {
        beforeEach(function() {
            client = new MatrixClient({
                request: httpBackend.requestFn,
                store: undefined,
                baseUrl: baseUrl,
                userId: userId,
                accessToken: accessToken,
                scheduler: new sdk.MatrixScheduler()
            });
        });

        it("should be able to send messages", function(done) {
            var eventId = "$flibble:wibble";
            httpBackend.when("PUT", "/txn1").respond(200, {
                event_id: eventId
            });
            client.sendTextMessage("!foo:bar", "a body", "txn1").done(function(res) {
                expect(res.event_id).toEqual(eventId);
                done();
            });
            httpBackend.flush("/txn1", 1);
        });

        it("should be able to sync / get new events", function(done) {
            var expectedEventTypes = [ // from /initialSync
                "m.room.message", "m.room.name", "m.room.member", "m.room.member",
                "m.room.create"
            ];
            client.on("event", function(event) {
                expect(expectedEventTypes.indexOf(event.getType())).not.toEqual(
                    -1, "Recv unexpected event type: " + event.getType()
                );
                expectedEventTypes.splice(
                    expectedEventTypes.indexOf(event.getType()), 1
                );
            });
            httpBackend.when("GET", "/initialSync").respond(200, initialSync);
            httpBackend.when("GET", "/events").respond(200, eventData);
            client.startClient();
            httpBackend.flush("/initialSync", 1).then(function() {
                return httpBackend.flush("/events", 1);
            }).done(function() {
                expect(expectedEventTypes.length).toEqual(
                    0, "Expected to see event types: " + expectedEventTypes
                );
                done();
            });
        });
    });

    describe("without opts.scheduler", function() {
        beforeEach(function() {
            client = new MatrixClient({
                request: httpBackend.requestFn,
                store: new sdk.MatrixInMemoryStore(),
                baseUrl: baseUrl,
                userId: userId,
                accessToken: accessToken,
                scheduler: undefined
            });
        });

        it("shouldn't retry sending events", function(done) {
            httpBackend.when("PUT", "/txn1").fail(500, {
                errcode: "M_SOMETHING",
                error: "Ruh roh"
            });
            client.sendTextMessage("!foo:bar", "a body", "txn1").done(function(res) {
                expect(false).toBe(true, "sendTextMessage resolved but shouldn't");
            }, function(err) {
                expect(err.errcode).toEqual("M_SOMETHING");
                done();
            });
            httpBackend.flush("/txn1", 1);
        });

        it("shouldn't queue events", function(done) {
            httpBackend.when("PUT", "/txn1").respond(200, {
                event_id: "AAA"
            });
            httpBackend.when("PUT", "/txn2").respond(200, {
                event_id: "BBB"
            });
            var sentA = false;
            var sentB = false;
            client.sendTextMessage("!foo:bar", "a body", "txn1").done(function(res) {
                sentA = true;
                expect(sentB).toBe(true);
            });
            client.sendTextMessage("!foo:bar", "b body", "txn2").done(function(res) {
                sentB = true;
                expect(sentA).toBe(false);
            });
            httpBackend.flush("/txn2", 1).done(function() {
                httpBackend.flush("/txn1", 1).done(function() {
                    done();
                });
            });
        });

        it("should be able to send messages", function(done) {
            httpBackend.when("PUT", "/txn1").respond(200, {
                event_id: "foo"
            });
            client.sendTextMessage("!foo:bar", "a body", "txn1").done(function(res) {
                expect(res.event_id).toEqual("foo");
                done();
            });
            httpBackend.flush("/txn1", 1);
        });
    });
});
{'fraction_non_alphanumeric': 0.14156225006664888, 'fraction_numerical': 0.00693148493735004, 'mean_word_length': 1.512726054922974, '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': '4894976', 'n_tokens_mistral': 2067, 'n_tokens_neox': 1989, 'n_words': 397}