mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-02-24 19:34:08 +01:00
📂 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:
parent
0352067da2
commit
2489670f54
10 changed files with 692 additions and 83 deletions
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue