-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain-view-model.ts
403 lines (343 loc) · 13 KB
/
main-view-model.ts
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/// <reference path="declarations.d.ts"/>
import observable = require('tns-core-modules/data/observable');
import observableArray = require('tns-core-modules/data/observable-array');
import http = require('tns-core-modules/http');
import platform = require('tns-core-modules/platform');
import frameModule = require('tns-core-modules/ui/frame');
import stopProcess = require('./stop-process');
declare var global: any;
interface IHostConfiguration {
port: number;
ips: string[];
options: {
debugBrk?: boolean;
debugTransport?: boolean;
appDirectoryRelativePath?: string;
}
}
interface INetworkConfiguration extends IHostConfiguration {
reachableIp: string;
}
interface IKarmaContext {
files: string[];
}
interface IScriptInfo {
url: string;
localPath?: string;
contents?: string;
}
function enableSocketIoDebugging() {
console.log('enabling socket.io debugging');
global.localStorage = {
debug: "*"
};
global.window = global;
}
var config: INetworkConfiguration = require('./config');
config.options = config.options || {};
if (!config.options.appDirectoryRelativePath) {
config.options.appDirectoryRelativePath = "app";
}
export class TestBrokerViewModel extends observable.Observable {
private startEmitted: boolean;
private executed: boolean;
private hasError: boolean;
private karmaRequestedRun: boolean;
private baseUrl: string;
private testResults: observableArray.ObservableArray<any>;
private socket: any; //socket.io socket
private config: any; //karma config
private networkConfig: INetworkConfiguration;
constructor() {
super();
global.__karma__ = this;
if (config.options.debugTransport) {
enableSocketIoDebugging();
}
//debugger;
this.testResults = new observableArray.ObservableArray();
this.set('testResults', this.testResults);
this.set('serverInfo', 'disconnected');
this.set('goToTestsText', 'Run Tests');
this.set('isConnected', false);
this.set('testsPassed', '-');
this.set('testsFailed', '-');
this.set('testsRan', 0);
this.set('testsTotal', 0);
this.startEmitted = false;
this.networkConfig = config;
this.resolveKarmaHost()
.then(() => {
if (this.networkConfig.reachableIp) {
this.connectToKarma();
}
}).catch(e => console.log(e.toString()));
}
public resolveKarmaHost() {
var successfulResolution = new Promise<string>((resolve, reject) => {
var foundKarma = false;
var resolvers = config.ips.map(ip => {
var karmaClientUrl = 'http://' + ip + ':' + config.port + '/context.json';
console.log('NSUTR: fetching ' + karmaClientUrl);
return http.getString({
url: karmaClientUrl,
method: 'GET',
timeout: 3000,
}).then(() => {
console.log('NSUTR: found karma at ' + ip);
if (!foundKarma) {
foundKarma = true;
resolve(ip);
}
}, () => undefined)
});
Promise.all(resolvers)
.then(() => {
if (!foundKarma) {
resolve(null);
}
})
});
return successfulResolution
.then(result => {
if (result) {
this.set('serverInfo', 'found karma at ' + result);
this.networkConfig.reachableIp = result;
} else {
this.set('serverInfo', 'no reachable hosts');
}
});
}
private updateBanner(message: string): (err?: any) => void {
return err => {
this.set('serverInfo', message);
if (err) {
console.log('NSUTR-socket.io: ' + err.toString());
}
}
}
public connectToKarma() {
this.baseUrl = 'http://' + this.networkConfig.reachableIp + ':' + this.networkConfig.port;
console.log('NSUTR: connecting to karma at ' + this.baseUrl);
// shims for engine.io-parser
global.navigator = {
userAgent: 'nativescript',
};
global.document = {
documentElement: {
style: {}
}
};
var io = require('./socket.io');
this.set('serverInfo', 'connecting to ' + this.baseUrl);
var socket = this.socket = io.connect(this.baseUrl, {
forceBase64: true
});
function formatName() {
return `NativeScript / ${platform.device.sdkVersion} (${platform.device.osVersion}; ${platform.device.model})`;
}
var connected = this.updateBanner('connected');
socket.on('connect', err => {
console.log('NSUTR: successfully connected to karma');
delete global.navigator;
delete global.document;
connected();
this.set('isConnected', true);
socket.emit('register', {
id: 'NativeScriptUnit-' + (0 | (Math.random() * 10000)),
name: formatName(),
});
});
socket.on('disconnect', this.updateBanner('disconnected'));
socket.on('reconnecting', this.updateBanner('reconnecting in $ ms...'));
socket.on('reconnect', connected);
socket.on('reconnect_failed', this.updateBanner('failed to reconnect'));
socket.on('info', this.updateBrowsersInfo.bind(this));
socket.on('connect_failed', this.updateBanner('connection failed'));
socket.on('disconnect', () => this.updateBrowsersInfo([]));
socket.on('connect_error', data => console.log('NSUTR: socket.io error on connect: ' + data));
socket.on('execute', this.onKarmaExecute.bind(this));
}
public viewTestRunDetails() {
frameModule.getFrameById('root-frame').navigate('run-details');
}
public beginLocalRun() {
this.config = this.config || { args: [] };
frameModule.getFrameById('root-frame').navigate('tns_modules/nativescript-unit-test-runner/test-run-page');
}
public onKarmaExecute(cfg) {
this.karmaRequestedRun = true;
this.config = cfg;
this.beginLocalRun();
}
public executeTestRun() {
if (this.executed) {
console.log('NSUTR: disregarding second execution');
return;
}
this.executed = true;
this.set('goToTestsText', 'View Test Run');
this.startEmitted = false;
this.hasError = false;
var contextUrl = this.baseUrl + '/context.json';
console.log("NSUTR: downloading " + contextUrl);
http.getString(contextUrl)
.then(content => {
var parsedContent: IKarmaContext = JSON.parse(content);
return parsedContent.files;
})
.then(scriptUrls => {
return Promise.all(scriptUrls.map((url): Promise<IScriptInfo> => {
var appPrefix = `/base/${config.options.appDirectoryRelativePath}/`;
if (url.startsWith(appPrefix)) {
var paramsStart = url.indexOf('?');
var relativePath = url.substring(appPrefix.length, paramsStart);
return Promise.resolve({
url: url,
localPath: '../../' + relativePath,
});
} else {
return http.getString(this.baseUrl + url)
.then(contents => {
return {
url: url,
contents: contents,
};
});
}
}));
})
.then((scriptsContents: IScriptInfo[]) => setTimeout(() => this.runTests(scriptsContents), 0));
}
public runTests(testScripts: IScriptInfo[]): void {
testScripts
.filter(script => this.isTestScript(script.url))
.forEach(script => {
try {
if (script.localPath) {
console.log('NSUTR: require script ' + script.url + ' from ' + script.localPath);
require(script.localPath);
} else {
console.log('NSUTR: eval script ' + script.url);
this.loadShim(script.url);
//call eval indirectly to execute the scripts in the global scope
var geval = eval;
geval(script.contents);
this.completeLoading(script.url);
}
} catch (err) {
this.error(err.toString(), script.localPath || script.url, err.lineNumber || 0);
}
});
if (!this.hasError) {
console.log('NSUTR: beginning test run');
if (config.options.debugBrk) {
/// HINT: If you need to place breakpoints in your tests, navigate to your test files in the Sources panel.
/// Hit the 'Resume script execution' button or F8 to continue to your tests.
debugger;
}
this.start(this.config);
}
}
private isTestScript(url: string): boolean {
return url.startsWith(`/base/${config.options.appDirectoryRelativePath}/tests/`) || !url.startsWith(`/base/${config.options.appDirectoryRelativePath}/`);
}
public updateBrowsersInfo(browsers) {
}
public start(cfg: any) {
this.error("You need to include a test adapter for the testing framework you're using");
}
public info(data) {
if (!this.startEmitted) {
this.socketEmit('start', data);
this.startEmitted = true;
} else {
this.socketEmit('info', data);
}
this.set('testsRunning', true);
this.set('testsPassed', 0);
this.set('testsFailed', 0);
this.set('testsRan', 0);
this.set('testsTotal', data.total);
}
public result(data) {
if (!this.startEmitted) {
this.socketEmit('start', { total: null });
this.startEmitted = true;
}
this.socketEmit('result', data);
var countVar = data.success ? 'testsPassed' : 'testsFailed';
this.set(countVar, this.get(countVar) + 1);
this.set('testsRan', this.get('testsRan') + 1);
this.testResults.push(data);
}
public complete(data?: any) {
console.log("NSUTR: completed test run.");
this.set('testsRunning', false);
delete this.start;
this.socketEmit('complete', data || {}, () => {
console.log('NSUTR: completeAck');
this.socketEmit('disconnect');
setTimeout(() => stopProcess(), 500);
});
}
public error(msg: string, url?: string, line?: number) {
this.hasError = true;
var fullMsg = url ? msg + '\nat ' + url + (line ? ':' + line : '') : msg;
console.log("NSUTR: this.error: " + fullMsg);
this.result({
id: url,
description: `${url} at line ${line}` || "",
log: [msg],
time: 0,
success: false,
suite: [],
})
this.complete();
return false;
}
public socketEmit(...args: any[]) {
if (this.karmaRequestedRun) {
this.socket.emit.apply(this.socket, arguments);
}
}
private loadShim(url: string) {
if (url.indexOf('mocha') !== -1) {
global.window = global;
global.location = { href: '/' };
global.document = {
getElementById: id => null
}
} else if (url.indexOf('chai') !== -1) {
global.__shim_require = global.require;
global.require = function () {
throw Error();
}
global.window = global;
} else if (url.indexOf('qunit.js') !== -1) {
global.define = function (factory) {
global.QUnit = factory();
}
global.define.amd = true;
}
}
private completeLoading(url: string) {
if (url.indexOf('mocha') !== -1) {
delete global.window;
//delete global.location;
delete global.document;
}
if (url.indexOf('chai') !== -1) {
delete global.window;
global.require = global.__shim_require;
delete global.__shim_require;
} else if (url.indexOf('qunit.js') !== -1) {
delete global.define;
}
}
}
export var mainViewModel = new TestBrokerViewModel();
require('application').onUncaughtError = error => {
console.log("NSUTR: uncaught error");
mainViewModel.error(error.message);
}