The FileCache class lacked comprehensive unit tests, making it difficult to verify its behavior and catch potential regressions.
Date objects were not being properly handled during serialization and deserialization in the FileCache, causing inconsistencies between original and cached FileInfo objects.
Solution
This PR addresses both issues:
Comprehensive Unit Tests:
Implemented a new test file tests/unit/file-cache.test.ts
Implemented custom JSON parsing and stringifying methods in FileCache to ensure proper handling of Date objects:
In loadCache: Added a custom reviver function to JSON.parse to convert ISO date strings back into Date objects
In saveCache: Added a custom replacer function to JSON.stringify to convert Date objects to ISO string format
Changes
Added new file tests/unit/file-cache.test.ts
Modified src/utils/file-cache.ts:
Updated loadCache method with custom JSON parsing
Updated saveCache method with custom JSON stringifying
Testing
The new test suite in file-cache.test.ts covers the core functionality of FileCache, including the proper handling of Date objects. These tests ensure that:
File information is correctly stored and retrieved
The cache is properly persisted to disk and can be reloaded
Date objects are correctly serialized and deserialized
FileCache Tests and Date Serialization Fix
Problem
Solution
This PR addresses both issues:
Comprehensive Unit Tests:
tests/unit/file-cache.test.ts
Date Serialization Fix:
loadCache
: Added a custom reviver function toJSON.parse
to convert ISO date strings back into Date objectssaveCache
: Added a custom replacer function toJSON.stringify
to convert Date objects to ISO string formatChanges
tests/unit/file-cache.test.ts
src/utils/file-cache.ts
:loadCache
method with custom JSON parsingsaveCache
method with custom JSON stringifyingTesting
The new test suite in
file-cache.test.ts
covers the core functionality of FileCache, including the proper handling of Date objects. These tests ensure that: