This repository has been archived by the owner on Jun 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
ng-scoresSpec.js
594 lines (554 loc) · 23.2 KB
/
ng-scoresSpec.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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
describe('ng-scores',function() {
"use strict";
var module;
var $scores;
var $stages;
var $teams;
var $q;
var $interval;
var dummyTeam = {
number: 123,
name: 'foo'
};
var rawMockStage = { id: "test", rounds: 3, name: "Test stage" };
var rawMockScore = {
file: 'somescore.json',
teamNumber: 123,
stageId: "test",
round: 1,
score: 150,
originalScore: 150,
published: false,
edited: undefined,
table: undefined
};
var mockStage;
var mockScore;
var mockTeam;
var fsMock;
beforeEach(function() {
module = factory('services/ng-scores',{
'services/log': logMock,
'services/fs': {},
});
fsMock = createFsMock({
"scores.json": { version: 2, scores: [rawMockScore], sheets: [] },
"stages.json": [rawMockStage],
"teams.json": [dummyTeam]
});
angular.mock.module(module.name);
angular.mock.module(function($provide) {
$provide.value('$fs', fsMock);
});
angular.mock.inject([
"$scores", "$stages", "$teams", "$q", "$interval",
function(_$scores_, _$stages_, _$teams_, _$q_, $_interval_) {
$scores = _$scores_;
$stages = _$stages_;
$teams = _$teams_;
$q = _$q_;
$interval = $_interval_;
}
]);
return $stages.init().then(function() {
mockStage = $stages.get(rawMockStage.id);
return $teams.init();
}).then(function() {
mockTeam = $teams.get(dummyTeam.number);
mockScore = {
file: 'somescore.json',
team: mockTeam,
stage: mockStage,
round: 1,
score: 150,
originalScore: 150
};
return $scores.init();
});
});
// Strip autogenerated properties to (hopefully ;)) arrive at the same
// object as what was used as input to $scores.add().
function filteredScores() {
return $scores.scores.map(function(score) {
return {
file: score.file,
team: score.team,
stage: score.stage,
round: score.round,
score: score.score,
originalScore: score.originalScore
};
});
}
describe('init',function() {
it('should load mock score initially',function() {
expect(filteredScores()).toEqual([mockScore]);
});
});
describe('clear',function() {
it('should clear the scores',function() {
expect(filteredScores()).toEqual([mockScore]);
$scores.clear();
expect(filteredScores()).toEqual([]);
});
});
describe('save',function() {
it('should write scores to scores.json',function() {
return $scores.save().then(function() {
expect(fsMock.write).toHaveBeenCalledWith(
'scores.json',
{
version: 2,
scores: [rawMockScore],
sheets: []
}
);
});
});
it('should log an error if writing fails',function() {
fsMock.write.and.returnValue(Q.reject('write err'));
return $scores.save().then(function() {
expect(logMock).toHaveBeenCalledWith('scores write error','write err');
});
});
});
describe('load',function() {
it('should load from scores.json',function() {
return $scores.load().then(function() {
expect(fsMock.read).toHaveBeenCalledWith('scores.json');
expect(filteredScores()).toEqual([mockScore]);
});
});
it('should log an error if loading fails',function() {
fsMock.read.and.returnValue(Q.reject('read err'));
return $scores.load().then(function() {
expect(logMock).toHaveBeenCalledWith('scores read error','read err');
});
});
});
describe('remove',function() {
it('should remove the provided index', function() {
expect(filteredScores()).toEqual([mockScore]);
$scores.remove(0);
expect(filteredScores()).toEqual([]);
});
});
describe('add',function() {
beforeEach(function() {
$scores.clear();
expect(filteredScores()).toEqual([]);
});
it('should add a score to the list', function() {
$scores.add(mockScore);
expect(filteredScores()).toEqual([mockScore]);
});
it('should allow duplicates', function() {
// Duplicate scores are 'allowed' during adding, but
// are rejected in scoreboard computation.
$scores.add(mockScore);
$scores.add(mockScore);
expect(filteredScores()).toEqual([mockScore, mockScore]);
expect($scores.validationErrors.length).toBeGreaterThan(0);
});
it('should accept numeric scores as strings', function() {
var tmp = angular.copy(mockScore);
tmp.score = String(tmp.score);
$scores.add(tmp);
// Note: the 'accepted' score should really be a number, not a string
expect($scores.scores[0].score).toEqual(150);
expect($scores.validationErrors.length).toEqual(0);
});
it('should accept and convert different casing for DNC', function() {
var tmp = angular.copy(mockScore);
tmp.score = "DnC";
$scores.add(tmp);
expect($scores.scores[0].score).toEqual("dnc");
expect($scores.validationErrors.length).toEqual(0);
});
it('should accept and convert different casing for DSQ', function() {
var tmp = angular.copy(mockScore);
tmp.score = "DsQ";
$scores.add(tmp);
expect($scores.scores[0].score).toEqual("dsq");
expect($scores.validationErrors.length).toEqual(0);
});
it('should reject but convert an empty score', function() {
var tmp = angular.copy(mockScore);
tmp.score = "";
$scores.add(tmp);
expect($scores.scores[0].score).toEqual(null);
expect($scores.validationErrors.length).toEqual(1);
});
it('should store the edited date of a score as string',function() {
var tmp = angular.copy(mockScore);
tmp.edited = new Date(2015,1,7);
$scores.add(tmp);
expect(typeof $scores.scores[0].edited).toBe('string');
});
});
describe('update', function() {
beforeEach(function() {
$scores.clear();
$scores.add(mockScore);
});
it('should mark modified scores', function() {
mockScore.score++;
// Simply changing the added score shouldn't matter...
expect($scores.scores[0].score).toEqual(150);
// ... but updating it should
$scores.update(0, mockScore);
expect($scores.scores[0].originalScore).toEqual(150);
expect($scores.scores[0].score).toEqual(151);
expect($scores.scores[0].modified).toBeTruthy();
expect($scores.scores[0].edited).toBeTruthy();
});
it('should accept numeric scores as strings',function() {
mockScore.score = "151";
$scores.update(0, mockScore);
// Note: the 'accepted' score should really be a number, not a string
expect($scores.scores[0].originalScore).toEqual(150);
expect($scores.scores[0].score).toEqual(151);
});
it('should throw an error if a score out of range is edited',function() {
var f = function() {
$scores.update(-1,mockScore);
};
expect(f).toThrowError('unknown score index: -1');
});
it('should throw an error if a score out of range is edited',function() {
var f = function() {
$scores.update(1,mockScore);
};
expect(f).toThrowError('unknown score index: 1');
});
});
describe('scoreboard', function() {
var board;
beforeEach(function() {
board = $scores.scoreboard;
});
function fillScores(input, allowErrors) {
$scores.beginupdate();
$scores.clear();
input.map(function(score) {
score.published = true;
$scores.add(score);
});
$scores.endupdate();
if (!allowErrors) {
$scores.scores.forEach(function(score) {
expect(score.error).toBeFalsy();
});
}
}
var team1 = { number: 1, name: "Fleppie 1" };
var team2 = { number: 2, name: "Fleppie 2" };
var team3 = { number: 3, name: "Fleppie 3" };
var team4 = { number: 4, name: "Fleppie 4" };
beforeEach(function() {
$teams.clear();
$teams.add(team1);
$teams.add(team2);
$teams.add(team3);
$teams.add(team4);
});
it('should output used stages', function() {
fillScores([]);
expect(Object.keys(board)).toEqual(["test"]);
});
it('should fill in all rounds for a team', function() {
// If a team has played at all (i.e., they have a score for that stage)
// then all other rounds for that team need to have an entry (which can
// be null).
fillScores([
{ team: team1, stage: mockStage, round: 2, score: 10 }
]);
expect(board["test"][0].scores).toEqual([null, 10, null]);
});
it('should rank number > dnc > dsq > null', function() {
fillScores([
{ team: team1, stage: mockStage, round: 1, score: 'dsq' },
{ team: team2, stage: mockStage, round: 1, score: 'dnc' },
{ team: team3, stage: mockStage, round: 1, score: -1 },
{ team: team4, stage: mockStage, round: 1, score: 1 },
]);
var result = board["test"].map(function(entry) {
return {
rank: entry.rank,
teamNumber: entry.team.number,
highest: entry.highest
};
});
expect(result).toEqual([
{ rank: 1, teamNumber: team4.number, highest: 1 },
{ rank: 2, teamNumber: team3.number, highest: -1 },
{ rank: 3, teamNumber: team2.number, highest: 'dnc' },
{ rank: 4, teamNumber: team1.number, highest: 'dsq' },
]);
});
it("should assign equal rank to equal scores", function() {
fillScores([
{ team: team1, stage: mockStage, round: 1, score: 10 },
{ team: team1, stage: mockStage, round: 2, score: 20 },
{ team: team1, stage: mockStage, round: 3, score: 30 },
{ team: team2, stage: mockStage, round: 1, score: 30 },
{ team: team2, stage: mockStage, round: 2, score: 10 },
{ team: team2, stage: mockStage, round: 3, score: 20 },
{ team: team3, stage: mockStage, round: 1, score: 30 },
{ team: team3, stage: mockStage, round: 2, score: 0 },
{ team: team3, stage: mockStage, round: 3, score: 20 },
]);
var result = board["test"].map(function(entry) {
return {
rank: entry.rank,
teamNumber: entry.team.number,
highest: entry.highest
};
});
// Note: for equal ranks, teams are sorted according
// to (ascending) team id
expect(result).toEqual([
{ rank: 1, teamNumber: team1.number, highest: 30 },
{ rank: 1, teamNumber: team2.number, highest: 30 },
{ rank: 2, teamNumber: team3.number, highest: 30 },
]);
});
it("should allow filtering rounds", function() {
fillScores([
{ team: team1, stage: mockStage, round: 1, score: 10 },
{ team: team1, stage: mockStage, round: 2, score: 20 },
{ team: team1, stage: mockStage, round: 3, score: 30 },
{ team: team2, stage: mockStage, round: 1, score: 30 },
{ team: team2, stage: mockStage, round: 2, score: 10 },
{ team: team2, stage: mockStage, round: 3, score: 20 },
{ team: team3, stage: mockStage, round: 1, score: 30 },
{ team: team3, stage: mockStage, round: 2, score: 0 },
{ team: team3, stage: mockStage, round: 3, score: 20 },
]);
var scoreboard = $scores.getRankings({
"test": 2
});
var result = scoreboard["test"].map(function(entry) {
return {
rank: entry.rank,
teamNumber: entry.team.number,
scores: entry.scores
};
});
// Note: for equal ranks, teams are sorted according
// to (ascending) team id
expect(result).toEqual([
{ rank: 1, teamNumber: team2.number, scores: [30, 10] },
{ rank: 2, teamNumber: team3.number, scores: [30, 0] },
{ rank: 3, teamNumber: team1.number, scores: [10, 20] },
]);
});
it("should include but warn about scores for unknown rounds / stages", function() {
fillScores([
{ team: team1, stage: { id: "foo" }, round: 1, score: 0 },
{ team: team1, stage: mockStage, round: 0, score: 0 },
{ team: team1, stage: mockStage, round: 4, score: 0 },
], true);
expect($scores.scores[0].error).toEqual(jasmine.any($scores.UnknownStageError));
expect($scores.scores[1].error).toEqual(jasmine.any($scores.UnknownRoundError));
expect($scores.scores[2].error).toEqual(jasmine.any($scores.UnknownRoundError));
expect(board["test"].length).toEqual(0);
expect($scores.validationErrors.length).toEqual(3);
});
it("should ignore but warn about invalid score", function() {
fillScores([
{ team: team1, stage: mockStage, round: 1, score: "foo" },
{ team: team1, stage: mockStage, round: 2, score: NaN },
{ team: team1, stage: mockStage, round: 3, score: Infinity },
{ team: team2, stage: mockStage, round: 1, score: {} },
{ team: team2, stage: mockStage, round: 2, score: true },
], true);
$scores.scores.forEach(function(score) {
expect(score.error).toEqual(jasmine.any($scores.InvalidScoreError));
});
expect(board["test"].length).toEqual(0);
expect($scores.validationErrors.length).toEqual(5);
});
it("should include but warn about duplicate score", function() {
fillScores([
{ team: team1, stage: mockStage, round: 1, score: 10 },
{ team: team1, stage: mockStage, round: 1, score: 20 },
], true);
expect($scores.validationErrors.length).toBe(2);
expect($scores.scores[0].error).toEqual(jasmine.any($scores.DuplicateScoreError));
expect($scores.scores[1].error).toEqual(jasmine.any($scores.DuplicateScoreError));
// Last score will overwrite any previous entry
expect(board["test"][0].highest).toEqual(20);
});
it("should ignore but warn about invalid team", function() {
$teams.remove(team1.number);
fillScores([
{ team: team1, stage: mockStage, round: 1, score: 10 },
], true);
expect($scores.scores[0].error).toEqual(jasmine.any($scores.UnknownTeamError));
expect($scores.validationErrors.length).toEqual(1);
});
it("should allow resolving error", function() {
fillScores([
{ team: team1, stage: mockStage, round: 1, score: 10 },
{ team: team1, stage: mockStage, round: 1, score: 20 },
], true);
expect($scores.validationErrors.length).toBeGreaterThan(0);
$scores.update(1, { team: team1, stage: mockStage, round: 2, score: 20 });
expect($scores.validationErrors.length).toEqual(0);
});
});
describe("pollSheets", function() {
var importedScore;
var mockFiles;
var mockDirs;
beforeEach(function() {
importedScore = {
file: "sheet_1.json",
team: mockTeam,
stage: mockStage,
round: 1,
score: 456,
originalScore: 456
};
mockFiles = {
"scores.json": { version: 2, scores: [], sheets: [] },
"scoresheets/sheet_1.json": { teamNumber: 123, stageId: "test", round: 1, score: 456 }
};
mockDirs = {
"scoresheets": ["sheet_1.json"],
};
fsMock._setFiles(mockFiles);
fsMock._setDirs(mockDirs);
$scores.clear();
});
it("should pick up a new sheet", function() {
return $scores.pollSheets().then(function() {
expect(filteredScores()).toEqual([importedScore]);
});
});
it("should ignore already processed sheets", function() {
return $scores.pollSheets().then(function() {
expect($scores.scores.length).toEqual(1);
return $scores.pollSheets();
}).then(function() {
expect($scores.scores.length).toEqual(1);
});
});
it("should ignore already processed sheets across loads", function() {
mockFiles["scores.json"] = { version: 2, scores: [], sheets: ["sheet_1.json"] };
return $scores.load().then(function() {
return $scores.pollSheets();
}).then(function() {
expect($scores.scores.length).toEqual(0);
});
});
it("should remember processed sheets", function() {
return $scores.pollSheets().then(function() {
expect(fsMock.write).toHaveBeenCalledWith(
'scores.json',
{
version: 2,
scores: [{
file: "sheet_1.json",
teamNumber: 123,
stageId: "test",
round: 1,
score: 456,
originalScore: 456,
published: false,
edited: undefined,
table: undefined,
}],
sheets: ["sheet_1.json"]
}
);
});
});
describe('clicking the button twice should not poll twice (#172)',function() {
it('should not add the same sheet twice',function() {
return $q.all([
$scores.pollSheets(),
$scores.pollSheets()
]).then(function() {
expect($scores.scores.length).toEqual(1);
});
});
});
describe('error recovery',function() {
it('should continue with no sheets when a 404 is returned',function() {
fsMock.list.and.returnValue(Q.reject({status:404}));
$scores.save = jasmine.createSpy('save');
return $scores.pollSheets().then(function() {
expect(fsMock.write).not.toHaveBeenCalled();
expect($scores.save).not.toHaveBeenCalled();
});
});
it('throw an error if an http error is received',function() {
fsMock.list.and.returnValue(Q.reject({status:500,responseText:'server error',statusText:'foo'}));
return $scores.pollSheets().catch(function(err) {
expect(err.message).toEqual('error 500 (foo): server error');
});
});
it('should rethrow the error if something just goes wrong',function() {
fsMock.list.and.returnValue(Q.reject(new Error('squeek')));
return $scores.pollSheets().catch(function(err) {
expect(err.message).toEqual('squeek');
});
});
it('should throw an unknown error if strange stuff is returned',function() {
fsMock.list.and.returnValue(Q.reject('darn'));
return $scores.pollSheets().catch(function(err) {
expect(err.message).toEqual('unknown error: darn');
});
});
});
});
describe('autoRefresh', function() {
it('should not be started when no-one is interested', function () {
fsMock.read.calls.reset();
$interval.flush($scores.AUTO_REFRESH_INTERVAL + 1);
expect(fsMock.read).not.toHaveBeenCalled();
});
it('should be started when someone is interested', function () {
fsMock.read.calls.reset();
$scores.enableAutoRefresh();
$interval.flush($scores.AUTO_REFRESH_INTERVAL + 1);
expect(fsMock.read).toHaveBeenCalled();
});
it('should keep refreshing when someone is interested', function () {
fsMock.read.calls.reset();
$scores.enableAutoRefresh();
$interval.flush($scores.AUTO_REFRESH_INTERVAL + 1);
expect(fsMock.read).toHaveBeenCalled();
fsMock.read.calls.reset();
$interval.flush($scores.AUTO_REFRESH_INTERVAL + 1);
expect(fsMock.read).toHaveBeenCalled();
});
it('should be stopped when everyone unregistered', function () {
$scores.enableAutoRefresh();
$interval.flush($scores.AUTO_REFRESH_INTERVAL + 1);
$scores.disableAutoRefresh();
fsMock.read.calls.reset();
$interval.flush($scores.AUTO_REFRESH_INTERVAL + 1);
expect(fsMock.read).not.toHaveBeenCalled();
});
it('should skip refresh when busy', function () {
fsMock.read.calls.reset();
$scores.enableAutoRefresh();
$scores.beginupdate();
$interval.flush($scores.AUTO_REFRESH_INTERVAL + 1);
expect(fsMock.read).not.toHaveBeenCalled();
});
it('should continue refresh after it was busy before', function () {
fsMock.read.calls.reset();
$scores.enableAutoRefresh();
$scores.beginupdate();
$interval.flush($scores.AUTO_REFRESH_INTERVAL + 1);
expect(fsMock.read).not.toHaveBeenCalled();
$scores.endupdate();
$interval.flush($scores.AUTO_REFRESH_INTERVAL + 1);
expect(fsMock.read).toHaveBeenCalled();
});
});
});