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