📂 refactor: File Read Operations (#9747)

* fix: axios response logging for text parsing, remove console logging, remove jsdoc

* refactor: error logging in logAxiosError function to handle various error types with type guards

* refactor: enhance text parsing with improved error handling and async file reading

* refactor: replace synchronous file reading with asynchronous methods for improved performance and memory management

* ci: update tests
This commit is contained in:
Danny Avila 2025-09-20 10:17:24 -04:00 committed by GitHub
parent 0352067da2
commit 2489670f54
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 692 additions and 83 deletions

View file

@ -1,6 +1,6 @@
import fs from 'fs';
import path from 'path';
import axios from 'axios';
import { readFileAsString } from './files';
import { loadServiceKey } from './key';
jest.mock('fs');
@ -11,6 +11,10 @@ jest.mock('@librechat/data-schemas', () => ({
},
}));
jest.mock('./files', () => ({
readFileAsString: jest.fn(),
}));
describe('loadServiceKey', () => {
const mockServiceKey = {
type: 'service_account',
@ -49,10 +53,13 @@ describe('loadServiceKey', () => {
it('should load from file path', async () => {
const filePath = '/path/to/service-key.json';
(fs.readFileSync as jest.Mock).mockReturnValue(JSON.stringify(mockServiceKey));
(readFileAsString as jest.Mock).mockResolvedValue({
content: JSON.stringify(mockServiceKey),
bytes: JSON.stringify(mockServiceKey).length,
});
const result = await loadServiceKey(filePath);
expect(fs.readFileSync).toHaveBeenCalledWith(path.resolve(filePath), 'utf8');
expect(readFileAsString).toHaveBeenCalledWith(path.resolve(filePath));
expect(result).toEqual(mockServiceKey);
});
@ -73,9 +80,7 @@ describe('loadServiceKey', () => {
it('should handle file read errors', async () => {
const filePath = '/path/to/nonexistent.json';
(fs.readFileSync as jest.Mock).mockImplementation(() => {
throw new Error('File not found');
});
(readFileAsString as jest.Mock).mockRejectedValue(new Error('File not found'));
const result = await loadServiceKey(filePath);
expect(result).toBeNull();