Skip to content

Commit 297816f

Browse files
committed
test: add unit tests for Advent scheduler tracker file operations
1 parent f16dabf commit 297816f

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

test/advent-scheduler.test.mjs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import assert from 'node:assert/strict';
2+
import { promises as fs } from 'node:fs';
3+
import test from 'node:test';
4+
5+
const TEST_TRACKER_FILE = 'test-advent-tracker.json';
6+
7+
/**
8+
* Helper function to load tracker data
9+
*/
10+
async function loadTracker(filename) {
11+
try {
12+
const data = await fs.readFile(filename, 'utf-8');
13+
return JSON.parse(data);
14+
} catch (error) {
15+
return {};
16+
}
17+
}
18+
19+
/**
20+
* Helper function to save tracker data
21+
*/
22+
async function saveTracker(filename, data) {
23+
await fs.writeFile(filename, JSON.stringify(data, null, 2), 'utf-8');
24+
}
25+
26+
/**
27+
* Helper function to clean up test tracker file
28+
*/
29+
async function cleanupTestTracker() {
30+
try {
31+
await fs.unlink(TEST_TRACKER_FILE);
32+
} catch (error) {
33+
// File might not exist, that's fine
34+
}
35+
}
36+
37+
test('advent scheduler: tracker file operations', async (t) => {
38+
await t.test('should create empty tracker if file does not exist', async () => {
39+
await cleanupTestTracker();
40+
const tracker = await loadTracker(TEST_TRACKER_FILE);
41+
assert.deepEqual(tracker, {});
42+
});
43+
44+
await t.test('should save and load tracker data correctly', async () => {
45+
const testData = {
46+
'2025': [1, 2, 3],
47+
'2026': [1],
48+
};
49+
await saveTracker(TEST_TRACKER_FILE, testData);
50+
const loaded = await loadTracker(TEST_TRACKER_FILE);
51+
assert.deepEqual(loaded, testData);
52+
});
53+
54+
await t.test('should track multiple days per year', async () => {
55+
const tracker = {
56+
'2025': [1, 5, 10, 15, 20, 25],
57+
};
58+
await saveTracker(TEST_TRACKER_FILE, tracker);
59+
const loaded = await loadTracker(TEST_TRACKER_FILE);
60+
assert.equal(loaded['2025'].length, 6);
61+
assert.ok(loaded['2025'].includes(1));
62+
assert.ok(loaded['2025'].includes(25));
63+
});
64+
65+
// Cleanup
66+
await cleanupTestTracker();
67+
});

0 commit comments

Comments
 (0)