forked from wireapp/wire-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestFactory.js
360 lines (310 loc) · 11.9 KB
/
TestFactory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Wire
* Copyright (C) 2018 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/
// @ts-check
/* eslint no-undef: "off" */
import 'core-js/full/reflect';
// Polyfill for "tsyringe" dependency injection
import {ClientClassification, ClientType} from '@wireapp/api-client/lib/client/';
import ko from 'knockout';
import {container} from 'tsyringe';
import {AssetRepository} from 'src/script/assets/AssetRepository';
import {CallingRepository} from 'src/script/calling/CallingRepository';
import {ClientEntity} from 'src/script/client/ClientEntity';
import {ClientRepository} from 'src/script/client/ClientRepository';
import {ClientService} from 'src/script/client/ClientService';
import {ClientState} from 'src/script/client/ClientState';
import {ConnectionRepository} from 'src/script/connection/ConnectionRepository';
import {ConnectionService} from 'src/script/connection/ConnectionService';
import {ConversationRepository} from 'src/script/conversation/ConversationRepository';
import {ConversationService} from 'src/script/conversation/ConversationService';
import {ConversationState} from 'src/script/conversation/ConversationState';
import {MessageRepository} from 'src/script/conversation/MessageRepository';
import {CryptographyRepository} from 'src/script/cryptography/CryptographyRepository';
import {User} from 'src/script/entity/User';
import {EventRepository} from 'src/script/event/EventRepository';
import {EventService} from 'src/script/event/EventService';
import {NotificationService} from 'src/script/event/NotificationService';
import {MediaRepository} from 'src/script/media/MediaRepository';
import {PermissionRepository} from 'src/script/permission/PermissionRepository';
import {PropertiesRepository} from 'src/script/properties/PropertiesRepository';
import {PropertiesService} from 'src/script/properties/PropertiesService';
import {SearchRepository} from 'src/script/search/SearchRepository';
import {SelfService} from 'src/script/self/SelfService';
import {Core} from 'src/script/service/CoreSingleton';
import {createStorageEngine, DatabaseTypes} from 'src/script/service/StoreEngineProvider';
import {StorageService} from 'src/script/storage';
import {StorageRepository} from 'src/script/storage/StorageRepository';
import {TeamRepository} from 'src/script/team/TeamRepository';
import {TeamService} from 'src/script/team/TeamService';
import {TeamState} from 'src/script/team/TeamState';
import {serverTimeHandler} from 'src/script/time/serverTimeHandler';
import {EventTrackingRepository} from 'src/script/tracking/EventTrackingRepository';
import {UserRepository} from 'src/script/user/UserRepository';
import {UserService} from 'src/script/user/UserService';
import {UserState} from 'src/script/user/UserState';
import {entities} from '../api/payloads';
import {SelfRepository} from 'src/script/self/SelfRepository';
import {AudioRepository} from 'src/script/audio/AudioRepository';
export class TestFactory {
constructor() {
container.clearInstances();
}
/**
* @returns {Promise<StorageRepository>} The storage repository.
*/
async exposeStorageActors() {
container.registerInstance(StorageService, new StorageService());
this.storage_service = container.resolve(StorageService);
if (!this.storage_service.db) {
const engine = await createStorageEngine('test', DatabaseTypes.PERMANENT);
this.storage_service.init(engine);
}
this.storage_repository = singleton(StorageRepository, this.storage_service);
return this.storage_repository;
}
/**
* @returns {Promise<CryptographyRepository>} The cryptography repository.
*/
async exposeCryptographyActors() {
await this.exposeStorageActors();
const currentClient = new ClientEntity(true, null);
currentClient.id = entities.clients.john_doe.permanent.id;
this.cryptography_repository = new CryptographyRepository();
return this.cryptography_repository;
}
/**
* @returns {Promise<ClientRepository>} The client repository.
*/
async exposeClientActors() {
await this.exposeCryptographyActors();
this.client_service = new ClientService(this.storage_service);
this.client_repository = new ClientRepository(this.client_service, this.cryptography_repository, new ClientState());
const currentClient = new ClientEntity(false, null);
currentClient.address = '62.96.148.44';
currentClient.class = ClientClassification.DESKTOP;
currentClient.cookie = 'webapp@2153234453@temporary@1470926647664';
currentClient.id = '132b3653b33f851f';
currentClient.label = 'Windows 10';
currentClient.meta = {isVerified: ko.observable(true), primaryKey: 'local_identity'};
currentClient.model = 'Chrome (Temporary)';
currentClient.time = '2016-10-07T16:01:42.133Z';
currentClient.type = ClientType.TEMPORARY;
this.client_repository['clientState'].currentClient = currentClient;
return this.client_repository;
}
/**
* @returns {Promise<EventRepository>} The event repository.
*/
async exposeEventActors() {
await this.exposeUserActors();
this.event_service = new EventService(this.storage_service);
this.notification_service = new NotificationService(this.storage_service);
this.conversation_service = new ConversationService(this.event_service);
this.event_repository = new EventRepository(
this.event_service,
this.notification_service,
serverTimeHandler,
this.user_repository['userState'],
);
return this.event_repository;
}
/**
* @returns {Promise<UserRepository>} The user repository.
*/
async exposeUserActors() {
await this.exposeClientActors();
this.assetRepository = new AssetRepository();
this.connection_service = new ConnectionService();
this.user_service = new UserService(this.storage_service);
this.propertyRepository = new PropertiesRepository(new PropertiesService(), new SelfService());
const userState = new UserState();
const selfUser = new User('self-id');
selfUser.isMe = true;
userState.self(selfUser);
userState.users([selfUser]);
this.user_repository = new UserRepository(
this.user_service,
this.assetRepository,
new SelfService(),
this.client_repository,
serverTimeHandler,
this.propertyRepository,
userState,
);
return this.user_repository;
}
/**
* @returns {Promise<ConnectionRepository>} The connection repository.
*/
async exposeConnectionActors() {
await this.exposeUserActors();
this.connection_service = new ConnectionService();
this.connection_repository = new ConnectionRepository(
this.connection_service,
this.user_repository,
this.self_service,
this.team_service,
);
return this.connection_repository;
}
/**
* @returns {Promise<SearchRepository>} The search repository.
*/
async exposeSearchActors() {
await this.exposeUserActors();
this.search_repository = new SearchRepository(this.user_repository);
return this.search_repository;
}
/**
* @returns {Promise<TeamRepository>} The team repository.
*/
async exposeTeamActors() {
await this.exposeUserActors();
this.team_service = new TeamService();
this.team_service.getAllTeamFeatures = async () => ({});
this.team_repository = new TeamRepository(
this.user_repository,
this.assetRepository,
() => Promise.resolve(),
this.team_service,
this.user_repository['userState'],
new TeamState(this.user_repository['userState']),
);
return this.team_repository;
}
/**
* @returns {Promise<SelfRepository>} The self repository.
*/
async exposeSelfActors() {
await this.exposeUserActors();
await this.exposeTeamActors();
await this.exposeClientActors();
this.self_service = new SelfService();
this.self_repository = new SelfRepository(
this.self_service,
this.user_repository,
this.team_repository,
this.client_repository,
this.user_repository['userState'],
);
return this.self_repository;
}
/**
* @returns {Promise<ConversationRepository>} The conversation repository.
*/
async exposeConversationActors() {
await this.exposeConnectionActors();
await this.exposeTeamActors();
await this.exposeEventActors();
await this.exposeSelfActors();
this.conversation_service = new ConversationService(this.event_service);
this.propertyRepository = new PropertiesRepository(new PropertiesService(), new SelfService());
/** @type {ConversationRepository} */
this.conversation_repository = null;
const conversationState = new ConversationState(
this.user_repository['userState'],
this.team_repository['teamState'],
);
const clientEntity = new ClientEntity(false, null);
clientEntity.address = '192.168.0.1';
clientEntity.class = ClientClassification.DESKTOP;
clientEntity.id = '60aee26b7f55a99f';
const clientState = new ClientState();
clientState.currentClient = clientEntity;
this.message_repository = new MessageRepository(
() => this.conversation_repository,
this.cryptography_repository,
this.event_repository,
this.propertyRepository,
serverTimeHandler,
this.user_repository,
this.assetRepository,
new AudioRepository(),
this.user_repository['userState'],
clientState,
);
const core = container.resolve(Core);
this.conversation_repository = new ConversationRepository(
this.conversation_service,
this.message_repository,
this.connection_repository,
this.event_repository,
this.team_repository,
this.user_repository,
this.self_repository,
this.propertyRepository,
this.calling_repository,
serverTimeHandler,
this.user_repository['userState'],
this.team_repository['teamState'],
conversationState,
this.connection_repository['connectionState'],
core,
);
return this.conversation_repository;
}
/**
* @returns {Promise<CallingRepository>} The call center.
*/
async exposeCallingActors() {
await this.exposeConversationActors();
const mediaRepository = new MediaRepository(new PermissionRepository());
this.calling_repository = new CallingRepository(
this.message_repository,
this.event_repository,
this.user_repository,
mediaRepository.streamHandler,
mediaRepository.devicesHandler,
serverTimeHandler,
undefined,
this.conversation_repository['conversationState'],
);
return this.calling_repository;
}
/**
* @returns {Promise<EventTrackingRepository>} The event tracking repository.
*/
async exposeTrackingActors() {
await this.exposeTeamActors();
this.tracking_repository = new EventTrackingRepository(this.message_repository);
return this.tracking_repository;
}
}
/**
* @template T
* @typedef {{new (...args: any[]): T}} Constructor<T>
*/
/**
* @template T
* @type {Map<Constructor<T>, T>}
*/
const actorsCache = new Map();
/**
* Will instantiate a service only once (uses the global actorsCache to store instances)
* @template T
* @param {Constructor<T>} Service the service to instantiate
* @param {...any} dependencies the dependencies required by the service
* @returns {T} the instantiated service
*/
function singleton(Service, ...dependencies) {
// @ts-ignore
actorsCache.set(Service, actorsCache.get(Service) || new Service(...dependencies));
// @ts-ignore
return actorsCache.get(Service);
}