Skip to content

Commit 2d4f2d5

Browse files
committed
add new assert action
1 parent fe4cba7 commit 2d4f2d5

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { logger } from '../logger.js';
2+
import { getConfigValue } from '../config.js';
3+
4+
/**
5+
* Asserts that the days puzzle has the level.
6+
* All days have two levels except for the last day which has one.
7+
*/
8+
export const assertPuzzleHasLevel = ({ day, level } = {}) => {
9+
// no need to validate if not the last day.
10+
if (day !== getConfigValue('aoc.validation.days').at(-1)) {
11+
return true;
12+
}
13+
14+
// only level one is valid on the last day.
15+
if (level !== 1) {
16+
logger.error('Day 25 only has one level.');
17+
return false;
18+
}
19+
20+
return true;
21+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { describe, jest, test, afterEach } from '@jest/globals';
2+
import { mockConfig, mockLogger } from '../mocks.js';
3+
4+
// setup mocks
5+
mockLogger();
6+
const { getConfigValue } = mockConfig();
7+
8+
// import after mocks set up
9+
const { assertPuzzleHasLevel } = await import(
10+
'../../src/actions/assertPuzzleHasLevel.js'
11+
);
12+
13+
describe('assertPuzzleHasLevel()', () => {
14+
afterEach(() => {
15+
jest.resetAllMocks();
16+
});
17+
18+
test('returns true if not last day', () => {
19+
const days = [1, 2, 3, 4, 5];
20+
getConfigValue.mockReturnValue(days);
21+
for (let i = 0; i < days.length - 1; i++) {
22+
const result = assertPuzzleHasLevel({ year: 2022, day: days[i], level: 1 });
23+
expect(result).toBe(true);
24+
}
25+
});
26+
27+
test('returns false if last day and level is two', () => {
28+
const days = [1, 2, 3, 4, 5];
29+
getConfigValue.mockReturnValue(days);
30+
const result = assertPuzzleHasLevel({ year: 2022, day: days.at(-1), level: 2 });
31+
expect(result).toBe(false);
32+
});
33+
34+
test('returns true if last day and level is one', () => {
35+
const days = [1, 2, 3, 4, 5];
36+
getConfigValue.mockReturnValue(days);
37+
const result = assertPuzzleHasLevel({ year: 2022, day: days.at(-1), level: 1 });
38+
expect(result).toBe(true);
39+
});
40+
});

0 commit comments

Comments
 (0)