forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptionless-spec.ts
More file actions
300 lines (258 loc) · 12 KB
/
exceptionless-spec.ts
File metadata and controls
300 lines (258 loc) · 12 KB
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
/// <reference path="../typings/tsd.d.ts" />
/// <reference path="exceptionless.ts" />
//import { Configuration, ExceptionlessClient } from 'Exceptionless';
module Exceptionless {
describe('ExceptionlessClient', () => {
it('should use event reference ids', (done) => {
var error = new Error('From Unit Test');
var client = new ExceptionlessClient('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', 'http://localhost:50000');
expect(client.config.lastReferenceIdManager.getLast()).toBe(null);
client.submitException(error).then(
() => expect(client.config.lastReferenceIdManager.getLast()).toBe(null),
() => expect(client.config.lastReferenceIdManager.getLast()).toBe(null)
);
var numberOfPlugins = client.config.plugins.length;
client.config.useReferenceIds();
expect(client.config.plugins.length).toBe(numberOfPlugins + 1);
client.submitException(error)
.then(
() => expect(client.config.lastReferenceIdManager.getLast()).not.toBe(null),
() => expect(client.config.lastReferenceIdManager.getLast()).toBe(null))
.then(done);
}, 5000);
});
describe('Configuration', () => {
it('should set the api key to null and enabled to false', () => {
var config = new Configuration(null);
expect(config.apiKey).toBe(null);
expect(config.enabled).toBe(false);
});
it('should set the api key and enabled to true', () => {
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', 'http://localhost:50000');
expect(config).not.toBe(null);
expect(config.apiKey).toBe('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw');
expect(config.enabled).toBe(true);
});
it('apply client configuration', () => {
var config = new Configuration(null);
expect(config.apiKey).toBe(null);
expect(config.enabled).toBe(false);
config.apiKey = 'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw';
expect(config.apiKey).toBe('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw');
expect(config.enabled).toBe(true);
});
it('should not add duplicate plugin', () => {
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw');
expect(config.plugins).not.toBe(null);
while(config.plugins.length > 0) {
config.removePlugin(config.plugins[0]);
}
config.addPlugin('test', 20, (context:EventPluginContext) => {});
config.addPlugin('test', 20, (context:EventPluginContext) => {});
expect(config.plugins.length).toBe(1);
});
it('should generate plugin name and priority', () => {
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw');
expect(config.plugins).not.toBe(null);
while(config.plugins.length > 0) {
config.removePlugin(config.plugins[0]);
}
config.addPlugin(null, null, (context:EventPluginContext) => {});
expect(config.plugins.length).toBe(1);
expect(config.plugins[0].name).not.toBe(null);
expect(config.plugins[0].priority).toBe(0);
});
it('should sort plugins by priority', () => {
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw');
expect(config.plugins).not.toBe(null);
while(config.plugins.length > 0) {
config.removePlugin(config.plugins[0]);
}
config.addPlugin('3', 3, (context:EventPluginContext) => {});
config.addPlugin('1', 1, (context:EventPluginContext) => {});
config.addPlugin('2', 2, (context:EventPluginContext) => {});
expect(config.plugins.length).toBe(3);
expect(config.plugins[0].priority).toBe(1);
expect(config.plugins[1].priority).toBe(2);
expect(config.plugins[2].priority).toBe(3);
});
});
describe('EventQueue', () => {
it('should enqueue event', () => {
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', 'http://localhost:50000');
expect(config.storage.count()).toBe(0);
var event:IEvent = { type: 'log', reference_id: '123454321' };
config.queue.enqueue(event);
expect(config.storage.count()).toBe(1);
});
it('should process queue', () => {
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', 'http://localhost:50000');
expect(config.storage.count()).toBe(0);
var event:IEvent = { type: 'log', reference_id: '123454321' };
config.queue.enqueue(event);
expect(config.storage.count()).toBe(1);
config.queue.process();
expect(config.storage.count()).toBe(0);
});
it('should discard event submission', () => {
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', 'http://localhost:50000');
expect(config.storage.count()).toBe(0);
config.queue.suspendProcessing(1, true);
var event:IEvent = { type: 'log', reference_id: '123454321' };
config.queue.enqueue(event);
expect(config.storage.count()).toBe(0);
});
it('should suspend processing', (done) => {
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', 'http://localhost:50000');
expect(config.storage.count()).toBe(0);
config.queue.suspendProcessing(.0001);
var event:IEvent = { type: 'log', reference_id: '123454321' };
config.queue.enqueue(event);
expect(config.storage.count()).toBe(1);
setTimeout(() => {
expect(config.storage.count()).toBe(0);
done();
}, 10000);
}, 21000);
});
describe('SubmissionClient', () => {
it('should submit events', (done) => {
function processResponse(response:SubmissionResponse) {
if (response.success) {
expect(response.message).toBe(null);
} else {
expect(response.message).toBe('Unable to connect to server.');
}
}
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', 'http://localhost:50000');
var submissionClient = new SubmissionClient();
submissionClient.submit([{ type: 'log', message: 'From js client', reference_id: '123454321' }], config)
.then(processResponse, processResponse)
.then(done);
}, 5000);
it('should submit invalid object data', (done) => {
function processResponse(response:SubmissionResponse) {
if (response.success) {
expect(response.message).toBe(null);
} else {
expect(response.message).toBe('Unable to connect to server.');
}
}
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', 'http://localhost:50000');
var event:IEvent = { type: 'log', message: 'From js client', reference_id: '123454321', data: {
name: 'blake',
age: function() { throw new Error('Test'); }
}};
var submissionClient = new SubmissionClient();
submissionClient.submit([event], config)
.then(processResponse, processResponse)
.then(done);
}, 5000);
it('should submit user description', (done) => {
function processResponse(response:SubmissionResponse) {
if (response.success) {
expect(response.message).toBe(null);
} else {
expect(response.message).toBe('Unable to connect to server.');
}
}
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', 'http://localhost:50000');
var submissionClient = new SubmissionClient();
submissionClient.submitDescription('123454321', { email_address: 'norply@exceptionless.io', description: 'unit test' } , config)
.then(processResponse, processResponse)
.then(done);
}, 5000);
it('should get project settings', (done) => {
function processResponse(response:SettingsResponse) {
if (response.success) {
expect(response.message).toBe(null);
expect(response.settings).not.toBe(null);
expect(response.settingsVersion).toBeGreaterThan(-1);
} else {
expect(response.message).toBe('Unable to connect to server.');
}
}
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', 'http://localhost:50000');
var submissionClient = new SubmissionClient();
submissionClient.getSettings(config)
.then(processResponse, processResponse)
.then(done);
}, 5000);
});
describe('Storage', () => {
it('should save events', () => {
var storage = new InMemoryStorage<IEvent>();
var key = 'ex-LhhP1C9gi-q-';
var event1:IEvent = { type: 'log', reference_id: key + '123454321' };
var event2:IEvent = { type: 'log', reference_id: key + '098765432' };
expect(storage.count()).toBe(0);
storage.save(event1.reference_id, event);
expect(storage.count()).toBe(1);
expect(storage.count(key)).toBe(1);
storage.save(event2.reference_id, event);
expect(storage.count()).toBe(2);
expect(storage.count(key)).toBe(2);
expect(storage.count(key + '1')).toBe(1);
});
it('should get saved events', () => {
var storage = new InMemoryStorage<IEvent>();
var key = 'ex-LhhP1C9gi-q-';
var event1:IEvent = { type: 'log', reference_id: key + '11' };
var event2:IEvent = { type: 'log', reference_id: key + '12' };
var event3:IEvent = { type: 'log', reference_id: key + '13' };
var event4:IEvent = { type: 'log', reference_id: key + '14' };
var event5:IEvent = { type: 'log', reference_id: key + '15' };
var event6:IEvent = { type: 'log', reference_id: key + '16' };
expect(storage.count()).toBe(0);
storage.save(event1.reference_id, event1);
storage.save(event2.reference_id, event2);
storage.save(event3.reference_id, event3);
storage.save(event4.reference_id, event4);
storage.save(event5.reference_id, event5);
storage.save(event6.reference_id, event6);
expect(storage.count()).toBe(6);
var ev = storage.get(event1.reference_id, 1)[0];
expect(ev).toBe(event1);
expect(storage.count()).toBe(5);
var events = storage.get(key, 2);
expect(events.length).toBe(2);
expect(storage.count()).toBe(3);
events = storage.get(key);
expect(events.length).toBe(3);
expect(storage.count()).toBe(0);
});
it('should clear all events', () => {
var storage = new InMemoryStorage<IEvent>();
var key = 'ex-LhhP1C9gi-q-';
var event1:IEvent = { type: 'log', reference_id: key + '11' };
var event2:IEvent = { type: 'log', reference_id: key + '12' };
var event3:IEvent = { type: 'log', reference_id: key + '13' };
var event4:IEvent = { type: 'log', reference_id: key + '14' };
var event5:IEvent = { type: 'log', reference_id: key + '15' };
var event6:IEvent = { type: 'log', reference_id: key + '16' };
expect(storage.count()).toBe(0);
storage.save(event1.reference_id, event1);
storage.save(event2.reference_id, event2);
storage.save(event3.reference_id, event3);
storage.save(event4.reference_id, event4);
storage.save(event5.reference_id, event5);
storage.save(event6.reference_id, event6);
expect(storage.count()).toBe(6);
storage.clear(event1.reference_id);
expect(storage.count()).toBe(5);
storage.clear();
expect(storage.count()).toBe(0);
});
});
describe('Utils', () => {
it('should parse version from url', () => {
expect(Utils.parseVersion('https://code.jquery.com/jquery-2.1.3.js')).toBe('2.1.3');
expect(Utils.parseVersion('//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css')).toBe('3.3.4');
expect(Utils.parseVersion('https://cdnjs.cloudflare.com/ajax/libs/1140/2.0/1140.css')).toBe('2.0');
expect(Utils.parseVersion('https://cdnjs.cloudflare.com/ajax/libs/Base64/0.3.0/base64.min.js')).toBe('0.3.0');
expect(Utils.parseVersion('https://cdnjs.cloudflare.com/ajax/libs/angular-google-maps/2.1.0-X.10/angular-google-maps.min.js')).toBe('2.1.0-X.10');
expect(Utils.parseVersion('https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/2.1.8-M1/swagger-ui.min.js')).toBe('2.1.8-M1');
expect(Utils.parseVersion('https://cdnjs.cloudflare.com/BLAH/BLAH.min.js')).toBe(null);
});
});
}