Skip to content

Commit 566e2ab

Browse files
authored
chore(toolkit-lib): test cases for notices cache (#548)
Test cases for #545 --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent 2eb1b84 commit 566e2ab

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import * as os from 'os';
2+
import * as path from 'path';
3+
import * as fs from 'fs-extra';
4+
import { asIoHelper } from '../../../lib/api/io/private';
5+
import { CachedDataSource } from '../../../lib/api/notices/cached-data-source';
6+
import { TestIoHost } from '../../_helpers';
7+
8+
describe('CachedDataSource', () => {
9+
const ioHost = new TestIoHost('trace');
10+
const ioHelper = asIoHelper(ioHost, 'notices' as any);
11+
let tempDir: string;
12+
let cacheFilePath: string;
13+
14+
beforeEach(() => {
15+
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cdk-test-'));
16+
cacheFilePath = path.join(tempDir, 'nonexistent-dir', 'cache.json');
17+
18+
// Just to be sure, remove directory if it exists
19+
const dirPath = path.dirname(cacheFilePath);
20+
if (fs.existsSync(dirPath)) {
21+
fs.rmdirSync(dirPath, { recursive: true });
22+
}
23+
});
24+
25+
afterEach(() => {
26+
// Clean up temp directory
27+
fs.rmdirSync(tempDir, { recursive: true });
28+
});
29+
30+
test('ensures directory exists when saving cache file', async () => {
31+
// GIVEN
32+
const mockDataSource = {
33+
fetch: jest.fn().mockResolvedValue([{ title: 'Test Notice' }]),
34+
};
35+
const dataSource = new CachedDataSource(ioHelper, cacheFilePath, mockDataSource);
36+
37+
// WHEN
38+
await dataSource.fetch();
39+
40+
// THEN
41+
// Directory should have been created
42+
expect(fs.existsSync(path.dirname(cacheFilePath))).toBe(true);
43+
// Cache file should exist
44+
expect(fs.existsSync(cacheFilePath)).toBe(true);
45+
// Cache file should contain the fetched data
46+
const cachedContent = fs.readJSONSync(cacheFilePath);
47+
expect(cachedContent).toHaveProperty('notices');
48+
expect(cachedContent.notices).toEqual([{ title: 'Test Notice' }]);
49+
});
50+
51+
test('handles errors when ensuring directory exists', async () => {
52+
// GIVEN
53+
const mockDataSource = {
54+
fetch: jest.fn().mockResolvedValue([{ title: 'Test Notice' }]),
55+
};
56+
57+
// Mock fs.ensureFile to throw an error
58+
jest.spyOn(fs, 'ensureFile').mockImplementationOnce(() => {
59+
throw new Error('Failed to create directory');
60+
});
61+
62+
const dataSource = new CachedDataSource(ioHelper, cacheFilePath, mockDataSource);
63+
64+
// WHEN
65+
await dataSource.fetch();
66+
67+
// THEN
68+
// Should have logged the error
69+
ioHost.expectMessage({
70+
level: 'debug',
71+
containing: 'Failed to store notices in the cache: Error: Failed to create directory',
72+
});
73+
74+
// Should still return data from the source
75+
expect(mockDataSource.fetch).toHaveBeenCalled();
76+
});
77+
});

0 commit comments

Comments
 (0)