This repository was archived by the owner on Aug 7, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcache.test.js
More file actions
195 lines (160 loc) · 5.41 KB
/
Copy pathcache.test.js
File metadata and controls
195 lines (160 loc) · 5.41 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* Unit tests for input validation functions.
*/
import {jest} from '@jest/globals'
// Create mock functions for fs/promises
const mockAccess = jest.fn()
const mockMkdir = jest.fn()
const mockReadFile = jest.fn()
const mockWriteFile = jest.fn()
const mockUnlink = jest.fn()
// Mock fs/promises module using unstable_mockModule for ESM support
jest.unstable_mockModule('fs/promises', () => ({
access: mockAccess,
mkdir: mockMkdir,
readFile: mockReadFile,
writeFile: mockWriteFile,
unlink: mockUnlink,
}))
/**
* Unit tests for the cache utility class.
*/
const {default: cacheInstance} = await import('../../src/util/cache.js')
import mockLog from '@mocks/util/log.js'
let logger
/**
* Test suite for the cache function and Log class.
*/
describe('cache', () => {
beforeEach(() => {
// Reset the logger before each test
mockLog._reset() // Reset the mock log state
logger = mockLog('test', 'test-token', true)
// Default mock behavior (success)
mockAccess.mockResolvedValue(undefined)
mockMkdir.mockResolvedValue(undefined)
mockReadFile.mockResolvedValue('{}')
mockWriteFile.mockResolvedValue(undefined)
mockUnlink.mockResolvedValue(undefined)
})
afterEach(() => {
// Clean up the logger reference
logger = null
// Reset the mock state
mockLog._reset()
// Reset any global state if needed
jest.restoreAllMocks()
})
/**
* Test basic cache functionality
*/
describe('basic functionality', () => {
/**
* Test that cache returns a singleton instance.
*/
test('should return singleton instance', () => {
const cache1 = cacheInstance(null, logger)
const cache2 = cacheInstance(null, logger)
expect(cache1).toBe(cache2)
})
/**
* Test cache functionality if needed
*/
test('cache should have expected methods', () => {
const cache = cacheInstance(null, logger)
expect(cache).toHaveProperty('save')
expect(cache).toHaveProperty('load')
expect(cache).toHaveProperty('clear')
expect(cache).toHaveProperty('exists')
expect(typeof cache.save).toBe('function')
expect(typeof cache.load).toBe('function')
expect(typeof cache.clear).toBe('function')
expect(typeof cache.exists).toBe('function')
})
/**
* Test that cache methods behave as expected
*/
test('cache methods should work correctly', async () => {
const cache = cacheInstance(null, logger)
const data = {key: 'value'}
// Configure readFile to return saved data on first call, then fail
mockReadFile.mockResolvedValueOnce(JSON.stringify(data)).mockRejectedValueOnce(new Error('File not found'))
// Test save method
await expect(cache.save(data)).resolves.toEqual(true)
// Test load method
await expect(cache.load()).resolves.toEqual(data)
// Test exists method
await expect(cache.exists()).resolves.toBe(true)
// Test clear method
await expect(cache.clear()).resolves.toBe(true)
// Test that cache is empty after clearing
await expect(cache.load()).resolves.toBeNull()
})
})
/**
* Test property getters and setters
*/
describe('property getters and setters', () => {
/**
* Test getter and setter methods for path property
*/
test('getter and setter for path property should work correctly', () => {
const cache = cacheInstance(null, logger)
const initialPath = cache.path
const newPath = `${process.cwd()}/cache/new-path.json`
// Test getter - path is sanitized (resolved) on construction
expect(initialPath).toBe(`${process.cwd()}/cache/report.json`)
// Test setter - path stays within CACHE_ROOT
cache.path = newPath
expect(cache.path).toBe(newPath)
})
test('setter should reject null bytes in path', () => {
const cache = cacheInstance(null, logger)
expect(() => {
cache.path = '/tmp/\0malicious'
}).toThrow('Path must not contain null bytes')
})
})
/**
* Test error handling
*/
describe('error handling', () => {
/**
* Test save method error handling
*/
test('save method should handle errors properly', async () => {
const cache = cacheInstance(null, logger)
// Mock writeFile to throw an error
mockWriteFile.mockImplementation(() => {
throw new Error('Mock save error')
})
// Attempt to save data and expect it to return null
const data = {key: 'value'}
await expect(cache.save(data)).resolves.toBeNull()
})
/**
* Test clear method error handling
*/
test('clear method should handle errors properly', async () => {
const cache = cacheInstance(null, logger)
// Mock unlink to throw an error
mockUnlink.mockImplementation(() => {
throw new Error('Mock clear error')
})
// Attempt to clear cache and expect it to return false
await expect(cache.clear()).resolves.toBe(false)
})
/**
* Test exists method when file doesn't exist
*/
test('exists method should return false when file does not exist', async () => {
const cache = cacheInstance(null, logger)
// Mock access to throw an error indicating file does not exist
mockAccess.mockImplementation(() => {
throw new Error('File does not exist')
})
// Check if cache exists and expect it to return false
await expect(cache.exists()).resolves.toBe(false)
})
})
})