UNPKG

6.89 kBJavaScriptView Raw
1"use strict";
2import 'source-map-support/register';
3const sdk = require("../..");
4const MatrixClient = sdk.MatrixClient;
5const HttpBackend = require("matrix-mock-request");
6const utils = require("../test-utils");
7
8import expect from 'expect';
9import Promise from 'bluebird';
10
11describe("MatrixClient opts", function() {
12 const baseUrl = "http://localhost.or.something";
13 let client = null;
14 let httpBackend = null;
15 const userId = "@alice:localhost";
16 const userB = "@bob:localhost";
17 const accessToken = "aseukfgwef";
18 const roomId = "!foo:bar";
19 const syncData = {
20 next_batch: "s_5_3",
21 presence: {},
22 rooms: {
23 join: {
24 "!foo:bar": { // roomId
25 timeline: {
26 events: [
27 utils.mkMessage({
28 room: roomId, user: userB, msg: "hello",
29 }),
30 ],
31 prev_batch: "f_1_1",
32 },
33 state: {
34 events: [
35 utils.mkEvent({
36 type: "m.room.name", room: roomId, user: userB,
37 content: {
38 name: "Old room name",
39 },
40 }),
41 utils.mkMembership({
42 room: roomId, mship: "join", user: userB, name: "Bob",
43 }),
44 utils.mkMembership({
45 room: roomId, mship: "join", user: userId, name: "Alice",
46 }),
47 utils.mkEvent({
48 type: "m.room.create", room: roomId, user: userId,
49 content: {
50 creator: userId,
51 },
52 }),
53 ],
54 },
55 },
56 },
57 },
58 };
59
60 beforeEach(function() {
61 utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
62 httpBackend = new HttpBackend();
63 });
64
65 afterEach(function() {
66 httpBackend.verifyNoOutstandingExpectation();
67 return httpBackend.stop();
68 });
69
70 describe("without opts.store", function() {
71 beforeEach(function() {
72 client = new MatrixClient({
73 request: httpBackend.requestFn,
74 store: undefined,
75 baseUrl: baseUrl,
76 userId: userId,
77 accessToken: accessToken,
78 scheduler: new sdk.MatrixScheduler(),
79 });
80 });
81
82 afterEach(function() {
83 client.stopClient();
84 });
85
86 it("should be able to send messages", function(done) {
87 const eventId = "$flibble:wibble";
88 httpBackend.when("PUT", "/txn1").respond(200, {
89 event_id: eventId,
90 });
91 client.sendTextMessage("!foo:bar", "a body", "txn1").done(function(res) {
92 expect(res.event_id).toEqual(eventId);
93 done();
94 });
95 httpBackend.flush("/txn1", 1);
96 });
97
98 it("should be able to sync / get new events", async function() {
99 const expectedEventTypes = [ // from /initialSync
100 "m.room.message", "m.room.name", "m.room.member", "m.room.member",
101 "m.room.create",
102 ];
103 client.on("event", function(event) {
104 expect(expectedEventTypes.indexOf(event.getType())).toNotEqual(
105 -1, "Recv unexpected event type: " + event.getType(),
106 );
107 expectedEventTypes.splice(
108 expectedEventTypes.indexOf(event.getType()), 1,
109 );
110 });
111 httpBackend.when("GET", "/pushrules").respond(200, {});
112 httpBackend.when("POST", "/filter").respond(200, { filter_id: "foo" });
113 httpBackend.when("GET", "/sync").respond(200, syncData);
114 await client.startClient();
115 await httpBackend.flush("/pushrules", 1);
116 await httpBackend.flush("/filter", 1);
117 await Promise.all([
118 httpBackend.flush("/sync", 1),
119 utils.syncPromise(client),
120 ]);
121 expect(expectedEventTypes.length).toEqual(
122 0, "Expected to see event types: " + expectedEventTypes,
123 );
124 });
125 });
126
127 describe("without opts.scheduler", function() {
128 beforeEach(function() {
129 client = new MatrixClient({
130 request: httpBackend.requestFn,
131 store: new sdk.MemoryStore(),
132 baseUrl: baseUrl,
133 userId: userId,
134 accessToken: accessToken,
135 scheduler: undefined,
136 });
137 });
138
139 it("shouldn't retry sending events", function(done) {
140 httpBackend.when("PUT", "/txn1").fail(500, {
141 errcode: "M_SOMETHING",
142 error: "Ruh roh",
143 });
144 client.sendTextMessage("!foo:bar", "a body", "txn1").done(function(res) {
145 expect(false).toBe(true, "sendTextMessage resolved but shouldn't");
146 }, function(err) {
147 expect(err.errcode).toEqual("M_SOMETHING");
148 done();
149 });
150 httpBackend.flush("/txn1", 1);
151 });
152
153 it("shouldn't queue events", function(done) {
154 httpBackend.when("PUT", "/txn1").respond(200, {
155 event_id: "AAA",
156 });
157 httpBackend.when("PUT", "/txn2").respond(200, {
158 event_id: "BBB",
159 });
160 let sentA = false;
161 let sentB = false;
162 client.sendTextMessage("!foo:bar", "a body", "txn1").done(function(res) {
163 sentA = true;
164 expect(sentB).toBe(true);
165 });
166 client.sendTextMessage("!foo:bar", "b body", "txn2").done(function(res) {
167 sentB = true;
168 expect(sentA).toBe(false);
169 });
170 httpBackend.flush("/txn2", 1).done(function() {
171 httpBackend.flush("/txn1", 1).done(function() {
172 done();
173 });
174 });
175 });
176
177 it("should be able to send messages", function(done) {
178 httpBackend.when("PUT", "/txn1").respond(200, {
179 event_id: "foo",
180 });
181 client.sendTextMessage("!foo:bar", "a body", "txn1").done(function(res) {
182 expect(res.event_id).toEqual("foo");
183 done();
184 });
185 httpBackend.flush("/txn1", 1);
186 });
187 });
188});