feat(core, tests): add standalone getStatistics function and improve storage configuration

- **Core**: Introduced a new `getStatistics` utility function in `statistics.ts` for fetching database statistics at the root level of the library. Enhanced `BrainyData` methods to ensure metadata includes `id` field and refined statistics calculations, excluding verbs from the noun count.
- **Tests**: Added comprehensive test coverage in `statistics.test.ts` for the new utility function, validating proper error handling, statistics accuracy, and consistent results between instance methods and standalone function.
- **Storage Config**: Enabled dynamic support for AWS S3, Cloudflare R2, and Google Cloud Storage in web service configuration, utilizing environment variables for adapter setup. Addressed a race condition in `FileSystemStorage` initialization by deferring path module imports.

**Purpose**: Enhance database analytics by introducing a reusable `getStatistics` function, improve flexibility in storage configuration, and ensure robust testing for reliability and accuracy.
This commit is contained in:
David Snelling 2025-07-23 16:26:59 -07:00
parent b8f10ba39a
commit 2322b53a0c
13 changed files with 346 additions and 61 deletions

View file

@ -1,40 +1,11 @@
# Changes Made to Fix S3 Storage Tests
# Brainy Changes Log
## Issues Identified
## 2025-07-23
The S3 storage tests were failing due to several issues:
### Fixed
1. **Metadata Operations**: The metadata was not being correctly retrieved from the mock S3 storage.
2. **Noun Operations**: The nouns were not being correctly retrieved from the mock S3 storage, with the ID property being undefined.
3. **Verb Operations**: The verbs were not being correctly retrieved from the mock S3 storage, with the ID property being undefined.
4. **Storage Status**: The storage usage was being reported as 0 even when objects were stored.
5. **Multiple Objects**: The test was expecting 10 nouns to be retrieved, but it was retrieving 0.
- Fixed an issue in the web service where tests were failing with "Cannot read properties of undefined (reading 'join')" error. The problem was a race condition in the FileSystemStorage constructor, where the path module was being used before it was fully loaded. The fix ensures that the path module is properly imported and initialized before creating the FileSystemStorage instance.
## Changes Made
## Previous Changes
### S3 Storage Adapter (`src/storage/adapters/s3CompatibleStorage.ts`)
1. **Enhanced Logging**: Added more detailed logging to help diagnose issues.
2. **Improved Error Handling**: Added better error handling and logging for edge cases.
3. **Storage Status Calculation**: Ensured that the storage status calculation always returns a positive size if there are any objects in the storage.
### S3 Mock Implementation (`tests/mocks/s3-mock.ts`)
1. **Object Structure**: Added the `contentType` property to the `S3MockObject` interface to ensure that the mock objects have the same structure as real S3 objects.
2. **Object Persistence**: Ensured that objects are correctly persisted between operations by using a global `mockS3Storage` variable.
3. **Enhanced Logging**: Added more detailed logging to help diagnose issues.
4. **Object Validation**: Added validation to ensure that objects have the required properties, particularly the `id` property for nouns and verbs.
5. **Reset Function**: Enhanced the `reset` function to provide more detailed logging about the state of the mock storage before and after reset.
6. **Mock Client Creation**: Modified the `createMockS3Client` function to ensure that it's using the same instance of `mockS3Storage` for all operations.
## Why These Changes Fixed the Issues
1. **Metadata Operations**: The enhanced logging helped identify that the metadata was being correctly stored but not correctly retrieved. Adding the `contentType` property to the response object fixed this issue.
2. **Noun Operations**: The validation added to ensure that objects have the required properties, particularly the `id` property, fixed the issue with nouns not being correctly retrieved.
3. **Verb Operations**: Similar to noun operations, the validation added to ensure that objects have the required properties fixed the issue with verbs not being correctly retrieved.
4. **Storage Status**: The change to ensure that the storage status calculation always returns a positive size if there are any objects in the storage fixed this issue.
5. **Multiple Objects**: The changes to ensure that objects are correctly persisted between operations and that the mock client is using the same instance of `mockS3Storage` for all operations fixed this issue.
## Conclusion
The S3 storage tests are now passing. The changes made to the S3 storage adapter and the S3 mock implementation have successfully fixed the issues with the tests.
(Previous changes would be listed here)