Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cache): add FileCache tests and fix Date serialization #12

Merged
merged 1 commit into from
Jul 20, 2024

Conversation

gmickel
Copy link
Owner

@gmickel gmickel commented Jul 20, 2024

FileCache Tests and Date Serialization Fix

Problem

  1. The FileCache class lacked comprehensive unit tests, making it difficult to verify its behavior and catch potential regressions.
  2. 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:

  1. Comprehensive Unit Tests:

    • Implemented a new test file tests/unit/file-cache.test.ts
    • Added tests covering key FileCache functionalities including:
      • Storing and retrieving file information
      • Handling non-existent files
      • Updating cache when file content changes
      • Persisting cache to disk and loading it
      • Clearing the cache
  2. Date Serialization Fix:

    • 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

- Implement comprehensive unit tests for FileCache class
- Resolve Date serialization issue in FileCache
- Add custom JSON reviver in loadCache to parse date strings into Date objects
- Implement custom JSON replacer in saveCache to convert Date objects to ISO strings
- Ensure consistent Date handling when persisting and retrieving cache entries
@gmickel gmickel merged commit 01297fb into main Jul 20, 2024
5 checks passed
@gmickel gmickel deleted the add-file-cache-tests branch July 20, 2024 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant