-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.test.js
84 lines (77 loc) · 2.48 KB
/
main.test.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
const { expect } = require('@jest/globals');
const testInput = require('./test-input');
const {
shiftToMinute,
incrementSleepingMinutes,
createGuardShiftData,
extractTimeStampAsNumber,
sortShiftsByDate,
calculateMostLikelyGuardsMostLikelyMinute,
} = require('./main');
describe('shiftToMinute', () => {
test('converts single digit minute to index', () => {
expect(shiftToMinute('[1518-11-01 00:05] falls asleep')).toBe(5);
});
test('converts double digit minute to index', () => {
expect(shiftToMinute('[1518-11-01 00:25] wakes up')).toBe(25);
});
});
describe('incrementSleepingMinutes', () => {
test('increments all minutes up to but excluding sleeping index', () => {
const guardMinutes = [0, 1, 2, 3];
expect(incrementSleepingMinutes(guardMinutes, 1, 3)).toEqual([0, 2, 3, 3]);
});
});
describe('createGuardShiftData', () => {
test('maps sleeping minutes for a simple, single shift', () => {
const shift = [
'[1518-11-01 00:00] Guard #10 begins shift',
'[1518-11-01 00:01] falls asleep',
'[1518-11-01 00:05] wakes up',
];
expect(createGuardShiftData(shift)).toHaveProperty('#10');
expect(createGuardShiftData(shift)[`#10`].slice(0, 6)).toEqual([
0, 1, 1, 1, 1, 0,
]);
});
});
describe('extractTimeStampAsNumber', () => {
test('extracts a timestamp as a number', () => {
expect(
extractTimeStampAsNumber('[1518-11-01 00:00] Guard #10 begins shift')
).toBe(151811010000);
});
});
describe('sortShiftsByDate', () => {
test('doesnt change sorted list', () => {
const shifts = [
'[1518-11-01 00:00] Guard #10 begins shift',
'[1518-11-01 00:01] falls asleep',
'[1518-11-01 00:05] wakes up',
];
const shiftsCopy = shifts.slice();
expect(sortShiftsByDate(shifts)).toEqual(shiftsCopy);
});
test('sorts unsorted list', () => {
const unsortedShifts = [
'[1518-11-01 00:05] wakes up',
'[1518-11-01 00:01] falls asleep',
'[1518-11-01 00:00] Guard #10 begins shift',
];
const sortedShifts = [
'[1518-11-01 00:00] Guard #10 begins shift',
'[1518-11-01 00:01] falls asleep',
'[1518-11-01 00:05] wakes up',
];
expect(sortShiftsByDate(unsortedShifts)).toEqual(sortedShifts);
});
});
describe('calculateMostLikelyGuardsMostLikelyMinute', () => {
test('works with the test input', () => {
expect(calculateMostLikelyGuardsMostLikelyMinute(testInput)).toEqual({
id: '#10',
sleepMins: 50,
sleepiestMin: 24,
});
});
});