-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.ts
96 lines (71 loc) · 3.37 KB
/
index.spec.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
import got from 'got';
import { promises } from 'fs';
import { mocked } from 'ts-jest/utils';
import aocLoader from '.';
jest.mock('fs', () => ({
promises: {
readFile: jest.fn().mockRejectedValue(Error("No such file or directory, open 'filename'")),
writeFile: jest.fn(),
},
}));
jest.mock('got', () => jest.fn().mockResolvedValue('Input from web'));
const readFileMock = mocked(promises.readFile);
const writeFileMock = mocked(promises.writeFile);
const gotMock = mocked(got);
describe('aocLoader', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should throw if no session token is given nor AOC_SESSION is set', async () => {
await expect(aocLoader(2020, 1)).rejects.toThrow(
'No session provided or exist as the AOC_SESSION environment variable',
);
});
it('should use the session argument before AOC_SESSION', async () => {
process.env.AOC_SESSION = 'env-var-token';
const sessionArgument = 'argument-token';
await aocLoader(2020, 1, sessionArgument);
expect(gotMock).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ headers: { Cookie: `session=${sessionArgument}` } }),
);
});
it('should use the AOC_SESSION environment if no session argument is given', async () => {
const envVar = 'env-var-token';
process.env.AOC_SESSION = envVar;
await aocLoader(2020, 1);
expect(gotMock).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ headers: { Cookie: `session=${envVar}` } }),
);
});
it('should throw if the input is not cached and request fails', async () => {
gotMock.mockRejectedValueOnce(Error('Failed to fetch input'));
await expect(aocLoader(2020, 1, 'session-token')).rejects.toThrow(
'Failed to fetch AoC data for year 2020 day 1: Failed to fetch input',
);
});
it('should not write to cache if request fails', async () => {
gotMock.mockRejectedValueOnce('Failed to fetch input');
await expect(aocLoader(2020, 1, 'session-token')).rejects.toThrow();
expect(writeFileMock).not.toHaveBeenCalled();
});
it('should fetch input data from adventofcode.com', async () => {
await expect(aocLoader(2017, 25, 'session-token')).resolves.toEqual('Input from web');
expect(gotMock).toHaveBeenCalledWith('https://adventofcode.com/2017/day/25/input', expect.anything());
});
it('should write to cache file if request is successful and cache does not exist', async () => {
await expect(aocLoader(2020, 1, 'session-token')).resolves.toEqual('Input from web');
expect(writeFileMock).toHaveBeenCalledWith(expect.anything(), 'Input from web');
});
it('should read cache file if it exist and not request input anew', async () => {
readFileMock.mockResolvedValueOnce(Buffer.from('Input from file', 'utf8'));
await expect(aocLoader(2019, 5, 'session-token')).resolves.toEqual('Input from file');
expect(readFileMock).toHaveBeenCalled();
expect(gotMock).not.toHaveBeenCalled();
});
it('should remove trailing newline from input', async () => {
gotMock.mockResolvedValueOnce('Input from web\n');
await expect(aocLoader(2020, 1, 'session-token')).resolves.toEqual('Input from web');
});
});