1 | "use strict";
|
2 | import expect from 'expect';
|
3 | import Promise from 'bluebird';
|
4 |
|
5 |
|
6 | import './olm-loader';
|
7 |
|
8 | import logger from '../src/logger';
|
9 | import sdk from '..';
|
10 | const MatrixEvent = sdk.MatrixEvent;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | module.exports.syncPromise = function(client, count) {
|
20 | if (count === undefined) {
|
21 | count = 1;
|
22 | }
|
23 | if (count <= 0) {
|
24 | return Promise.resolve();
|
25 | }
|
26 |
|
27 | const p = new Promise((resolve, reject) => {
|
28 | const cb = (state) => {
|
29 | logger.log(`${Date.now()} syncPromise(${count}): ${state}`);
|
30 | if (state == 'SYNCING') {
|
31 | resolve();
|
32 | } else {
|
33 | client.once('sync', cb);
|
34 | }
|
35 | };
|
36 | client.once('sync', cb);
|
37 | });
|
38 |
|
39 | return p.then(() => {
|
40 | return module.exports.syncPromise(client, count-1);
|
41 | });
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | module.exports.beforeEach = function(context) {
|
50 | const desc = context.currentTest.fullTitle();
|
51 |
|
52 | logger.log(desc);
|
53 | logger.log(new Array(1 + desc.length).join("="));
|
54 | };
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | module.exports.mock = function(constr, name) {
|
63 |
|
64 |
|
65 | const HelperConstr = new Function();
|
66 | HelperConstr.prototype = constr.prototype;
|
67 | const result = new HelperConstr();
|
68 | result.toString = function() {
|
69 | return "mock" + (name ? " of " + name : "");
|
70 | };
|
71 | for (const key in constr.prototype) {
|
72 | try {
|
73 | if (constr.prototype[key] instanceof Function) {
|
74 | result[key] = expect.createSpy();
|
75 | }
|
76 | } catch (ex) {
|
77 |
|
78 |
|
79 |
|
80 | }
|
81 | }
|
82 | return result;
|
83 | };
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | module.exports.mkEvent = function(opts) {
|
97 | if (!opts.type || !opts.content) {
|
98 | throw new Error("Missing .type or .content =>" + JSON.stringify(opts));
|
99 | }
|
100 | const event = {
|
101 | type: opts.type,
|
102 | room_id: opts.room,
|
103 | sender: opts.sender || opts.user,
|
104 | content: opts.content,
|
105 | event_id: "$" + Math.random() + "-" + Math.random(),
|
106 | };
|
107 | if (opts.skey !== undefined) {
|
108 | event.state_key = opts.skey;
|
109 | } else if (["m.room.name", "m.room.topic", "m.room.create", "m.room.join_rules",
|
110 | "m.room.power_levels", "m.room.topic",
|
111 | "com.example.state"].indexOf(opts.type) !== -1) {
|
112 | event.state_key = "";
|
113 | }
|
114 | return opts.event ? new MatrixEvent(event) : event;
|
115 | };
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | module.exports.mkPresence = function(opts) {
|
123 | if (!opts.user) {
|
124 | throw new Error("Missing user");
|
125 | }
|
126 | const event = {
|
127 | event_id: "$" + Math.random() + "-" + Math.random(),
|
128 | type: "m.presence",
|
129 | sender: opts.sender || opts.user,
|
130 | content: {
|
131 | avatar_url: opts.url,
|
132 | displayname: opts.name,
|
133 | last_active_ago: opts.ago,
|
134 | presence: opts.presence || "offline",
|
135 | },
|
136 | };
|
137 | return opts.event ? new MatrixEvent(event) : event;
|
138 | };
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | module.exports.mkMembership = function(opts) {
|
154 | opts.type = "m.room.member";
|
155 | if (!opts.skey) {
|
156 | opts.skey = opts.sender || opts.user;
|
157 | }
|
158 | if (!opts.mship) {
|
159 | throw new Error("Missing .mship => " + JSON.stringify(opts));
|
160 | }
|
161 | opts.content = {
|
162 | membership: opts.mship,
|
163 | };
|
164 | if (opts.name) {
|
165 | opts.content.displayname = opts.name;
|
166 | }
|
167 | if (opts.url) {
|
168 | opts.content.avatar_url = opts.url;
|
169 | }
|
170 | return module.exports.mkEvent(opts);
|
171 | };
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | module.exports.mkMessage = function(opts) {
|
183 | opts.type = "m.room.message";
|
184 | if (!opts.msg) {
|
185 | opts.msg = "Random->" + Math.random();
|
186 | }
|
187 | if (!opts.room || !opts.user) {
|
188 | throw new Error("Missing .room or .user from %s", opts);
|
189 | }
|
190 | opts.content = {
|
191 | msgtype: "m.text",
|
192 | body: opts.msg,
|
193 | };
|
194 | return module.exports.mkEvent(opts);
|
195 | };
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | module.exports.MockStorageApi = function() {
|
204 | this.data = {};
|
205 | };
|
206 | module.exports.MockStorageApi.prototype = {
|
207 | get length() {
|
208 | return Object.keys(this.data).length;
|
209 | },
|
210 | key: function(i) {
|
211 | return Object.keys(this.data)[i];
|
212 | },
|
213 | setItem: function(k, v) {
|
214 | this.data[k] = v;
|
215 | },
|
216 | getItem: function(k) {
|
217 | return this.data[k] || null;
|
218 | },
|
219 | removeItem: function(k) {
|
220 | delete this.data[k];
|
221 | },
|
222 | };
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | module.exports.awaitDecryption = function(event) {
|
232 | if (!event.isBeingDecrypted()) {
|
233 | return Promise.resolve(event);
|
234 | }
|
235 |
|
236 | logger.log(`${Date.now()} event ${event.getId()} is being decrypted; waiting`);
|
237 |
|
238 | return new Promise((resolve, reject) => {
|
239 | event.once('Event.decrypted', (ev) => {
|
240 | logger.log(`${Date.now()} event ${event.getId()} now decrypted`);
|
241 | resolve(ev);
|
242 | });
|
243 | });
|
244 | };
|