mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-22 19:30:15 +01:00
ci: Mock getAppConfig in various tests to provide default configurations
This commit is contained in:
parent
2501d11fa0
commit
b2b2aee945
7 changed files with 150 additions and 23 deletions
|
|
@ -2,6 +2,14 @@ const { Constants } = require('librechat-data-provider');
|
||||||
const { initializeFakeClient } = require('./FakeClient');
|
const { initializeFakeClient } = require('./FakeClient');
|
||||||
|
|
||||||
jest.mock('~/db/connect');
|
jest.mock('~/db/connect');
|
||||||
|
jest.mock('~/server/services/Config', () => ({
|
||||||
|
getAppConfig: jest.fn().mockResolvedValue({
|
||||||
|
// Default app config for tests
|
||||||
|
paths: { uploads: '/tmp' },
|
||||||
|
fileStrategy: 'local',
|
||||||
|
memory: { disabled: false },
|
||||||
|
}),
|
||||||
|
}));
|
||||||
jest.mock('~/models', () => ({
|
jest.mock('~/models', () => ({
|
||||||
User: jest.fn(),
|
User: jest.fn(),
|
||||||
Key: jest.fn(),
|
Key: jest.fn(),
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,27 @@ const mockPluginService = {
|
||||||
|
|
||||||
jest.mock('~/server/services/PluginService', () => mockPluginService);
|
jest.mock('~/server/services/PluginService', () => mockPluginService);
|
||||||
|
|
||||||
|
jest.mock('~/server/services/Config', () => ({
|
||||||
|
getAppConfig: jest.fn().mockResolvedValue({
|
||||||
|
// Default app config for tool tests
|
||||||
|
paths: { uploads: '/tmp' },
|
||||||
|
fileStrategy: 'local',
|
||||||
|
filteredTools: [],
|
||||||
|
includedTools: [],
|
||||||
|
}),
|
||||||
|
getCachedTools: jest.fn().mockResolvedValue({
|
||||||
|
// Default cached tools for tests
|
||||||
|
dalle: {
|
||||||
|
type: 'function',
|
||||||
|
function: {
|
||||||
|
name: 'dalle',
|
||||||
|
description: 'DALL-E image generation',
|
||||||
|
parameters: {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
|
||||||
const { BaseLLM } = require('@langchain/openai');
|
const { BaseLLM } = require('@langchain/openai');
|
||||||
const { Calculator } = require('@langchain/community/tools/calculator');
|
const { Calculator } = require('@langchain/community/tools/calculator');
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,29 @@ const request = require('supertest');
|
||||||
const { MongoMemoryServer } = require('mongodb-memory-server');
|
const { MongoMemoryServer } = require('mongodb-memory-server');
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
|
|
||||||
jest.mock('~/server/services/Config/loadCustomConfig', () => {
|
jest.mock('~/server/services/Config', () => ({
|
||||||
return jest.fn(() => Promise.resolve({}));
|
loadCustomConfig: jest.fn(() => Promise.resolve({})),
|
||||||
});
|
getCustomConfig: jest.fn(() => Promise.resolve({})),
|
||||||
|
setAppConfig: jest.fn(),
|
||||||
|
getAppConfig: jest.fn().mockResolvedValue({
|
||||||
|
paths: {
|
||||||
|
uploads: '/tmp',
|
||||||
|
dist: '/tmp/dist',
|
||||||
|
fonts: '/tmp/fonts',
|
||||||
|
assets: '/tmp/assets',
|
||||||
|
},
|
||||||
|
fileStrategy: 'local',
|
||||||
|
imageOutputType: 'PNG',
|
||||||
|
}),
|
||||||
|
setCachedTools: jest.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
jest.mock('~/app/clients/tools', () => ({
|
||||||
|
createOpenAIImageTools: jest.fn(() => []),
|
||||||
|
createYouTubeTools: jest.fn(() => []),
|
||||||
|
manifestToolMap: {},
|
||||||
|
toolkits: [],
|
||||||
|
}));
|
||||||
|
|
||||||
describe('Server Configuration', () => {
|
describe('Server Configuration', () => {
|
||||||
// Increase the default timeout to allow for Mongo cleanup
|
// Increase the default timeout to allow for Mongo cleanup
|
||||||
|
|
@ -31,6 +51,22 @@ describe('Server Configuration', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
|
// Create the required directories and files for the test
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
const dirs = ['/tmp/dist', '/tmp/fonts', '/tmp/assets'];
|
||||||
|
dirs.forEach((dir) => {
|
||||||
|
if (!fs.existsSync(dir)) {
|
||||||
|
fs.mkdirSync(dir, { recursive: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
fs.writeFileSync(
|
||||||
|
path.join('/tmp/dist', 'index.html'),
|
||||||
|
'<!DOCTYPE html><html><head><title>LibreChat</title></head><body><div id="root"></div></body></html>',
|
||||||
|
);
|
||||||
|
|
||||||
mongoServer = await MongoMemoryServer.create();
|
mongoServer = await MongoMemoryServer.create();
|
||||||
process.env.MONGO_URI = mongoServer.getUri();
|
process.env.MONGO_URI = mongoServer.getUri();
|
||||||
process.env.PORT = '0'; // Use a random available port
|
process.env.PORT = '0'; // Use a random available port
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,15 @@ jest.mock('~/server/services/UserService', () => ({
|
||||||
checkUserKeyExpiry: jest.requireActual('~/server/services/UserService').checkUserKeyExpiry,
|
checkUserKeyExpiry: jest.requireActual('~/server/services/UserService').checkUserKeyExpiry,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
jest.mock('~/server/services/Config', () => ({
|
||||||
|
getAppConfig: jest.fn().mockResolvedValue({
|
||||||
|
azureAssistants: {
|
||||||
|
apiKey: 'test-key',
|
||||||
|
baseURL: 'https://test.url',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
const tenDaysFromToday = new Date(today.setDate(today.getDate() + 10));
|
const tenDaysFromToday = new Date(today.setDate(today.getDate() + 10));
|
||||||
const isoString = tenDaysFromToday.toISOString();
|
const isoString = tenDaysFromToday.toISOString();
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,12 @@ jest.mock('~/server/services/Config', () => ({
|
||||||
headers: { 'x-user': '{{LIBRECHAT_USER_ID}}', 'x-email': '{{LIBRECHAT_USER_EMAIL}}' },
|
headers: { 'x-user': '{{LIBRECHAT_USER_ID}}', 'x-email': '{{LIBRECHAT_USER_EMAIL}}' },
|
||||||
models: { default: ['test-model'] },
|
models: { default: ['test-model'] },
|
||||||
}),
|
}),
|
||||||
|
getAppConfig: jest.fn().mockResolvedValue({
|
||||||
|
'test-endpoint': {
|
||||||
|
apiKey: 'test-key',
|
||||||
|
baseURL: 'https://test.com',
|
||||||
|
},
|
||||||
|
}),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
jest.mock('~/server/services/ModelService', () => ({
|
jest.mock('~/server/services/ModelService', () => ({
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,14 @@ jest.mock('~/server/services/UserService', () => ({
|
||||||
getUserKey: jest.fn().mockImplementation(() => ({})),
|
getUserKey: jest.fn().mockImplementation(() => ({})),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
jest.mock('~/server/services/Config', () => ({
|
||||||
|
getAppConfig: jest.fn().mockResolvedValue({
|
||||||
|
google: {
|
||||||
|
apiKey: 'test-key',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
|
||||||
const app = { locals: {} };
|
const app = { locals: {} };
|
||||||
|
|
||||||
describe('google/initializeClient', () => {
|
describe('google/initializeClient', () => {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,13 @@
|
||||||
jest.mock('~/cache/getLogStores');
|
jest.mock('~/cache/getLogStores', () => ({
|
||||||
|
getLogStores: jest.fn().mockReturnValue({
|
||||||
|
get: jest.fn().mockResolvedValue({
|
||||||
|
openAI: { apiKey: 'test-key' },
|
||||||
|
}),
|
||||||
|
set: jest.fn(),
|
||||||
|
delete: jest.fn(),
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
|
||||||
const { EModelEndpoint, ErrorTypes, validateAzureGroups } = require('librechat-data-provider');
|
const { EModelEndpoint, ErrorTypes, validateAzureGroups } = require('librechat-data-provider');
|
||||||
const { getUserKey, getUserKeyValues } = require('~/server/services/UserService');
|
const { getUserKey, getUserKeyValues } = require('~/server/services/UserService');
|
||||||
const initializeClient = require('./initialize');
|
const initializeClient = require('./initialize');
|
||||||
|
|
@ -11,6 +20,38 @@ jest.mock('~/server/services/UserService', () => ({
|
||||||
checkUserKeyExpiry: jest.requireActual('~/server/services/UserService').checkUserKeyExpiry,
|
checkUserKeyExpiry: jest.requireActual('~/server/services/UserService').checkUserKeyExpiry,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
jest.mock('~/server/services/Config', () => ({
|
||||||
|
getAppConfig: jest.fn().mockResolvedValue({
|
||||||
|
openAI: {
|
||||||
|
apiKey: 'test-key',
|
||||||
|
},
|
||||||
|
azureOpenAI: {
|
||||||
|
apiKey: 'test-azure-key',
|
||||||
|
modelNames: ['gpt-4-vision-preview', 'gpt-3.5-turbo', 'gpt-4'],
|
||||||
|
modelGroupMap: {
|
||||||
|
'gpt-4-vision-preview': {
|
||||||
|
group: 'librechat-westus',
|
||||||
|
deploymentName: 'gpt-4-vision-preview',
|
||||||
|
version: '2024-02-15-preview',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
groupMap: {
|
||||||
|
'librechat-westus': {
|
||||||
|
apiKey: 'WESTUS_API_KEY',
|
||||||
|
instanceName: 'librechat-westus',
|
||||||
|
version: '2023-12-01-preview',
|
||||||
|
models: {
|
||||||
|
'gpt-4-vision-preview': {
|
||||||
|
deploymentName: 'gpt-4-vision-preview',
|
||||||
|
version: '2024-02-15-preview',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
|
||||||
describe('initializeClient', () => {
|
describe('initializeClient', () => {
|
||||||
// Set up environment variables
|
// Set up environment variables
|
||||||
const originalEnvironment = process.env;
|
const originalEnvironment = process.env;
|
||||||
|
|
@ -79,7 +120,7 @@ describe('initializeClient', () => {
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const { modelNames, modelGroupMap, groupMap } = validateAzureGroups(validAzureConfigs);
|
const { modelNames } = validateAzureGroups(validAzureConfigs);
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.resetModules(); // Clears the cache
|
jest.resetModules(); // Clears the cache
|
||||||
|
|
@ -112,25 +153,29 @@ describe('initializeClient', () => {
|
||||||
test('should initialize client with Azure credentials when endpoint is azureOpenAI', async () => {
|
test('should initialize client with Azure credentials when endpoint is azureOpenAI', async () => {
|
||||||
process.env.AZURE_API_KEY = 'test-azure-api-key';
|
process.env.AZURE_API_KEY = 'test-azure-api-key';
|
||||||
(process.env.AZURE_OPENAI_API_INSTANCE_NAME = 'some-value'),
|
(process.env.AZURE_OPENAI_API_INSTANCE_NAME = 'some-value'),
|
||||||
(process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME = 'some-value'),
|
(process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME = 'some-value'),
|
||||||
(process.env.AZURE_OPENAI_API_VERSION = 'some-value'),
|
(process.env.AZURE_OPENAI_API_VERSION = 'some-value'),
|
||||||
(process.env.AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME = 'some-value'),
|
(process.env.AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME = 'some-value'),
|
||||||
(process.env.AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME = 'some-value'),
|
(process.env.AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME = 'some-value'),
|
||||||
(process.env.OPENAI_API_KEY = 'test-openai-api-key');
|
(process.env.OPENAI_API_KEY = 'test-openai-api-key');
|
||||||
process.env.DEBUG_OPENAI = 'false';
|
process.env.DEBUG_OPENAI = 'false';
|
||||||
process.env.OPENAI_SUMMARIZE = 'false';
|
process.env.OPENAI_SUMMARIZE = 'false';
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
body: { key: null, endpoint: 'azureOpenAI' },
|
body: {
|
||||||
|
key: null,
|
||||||
|
endpoint: 'azureOpenAI',
|
||||||
|
model: 'gpt-4-vision-preview',
|
||||||
|
},
|
||||||
user: { id: '123' },
|
user: { id: '123' },
|
||||||
app,
|
app,
|
||||||
};
|
};
|
||||||
const res = {};
|
const res = {};
|
||||||
const endpointOption = { modelOptions: { model: 'test-model' } };
|
const endpointOption = {};
|
||||||
|
|
||||||
const client = await initializeClient({ req, res, endpointOption });
|
const client = await initializeClient({ req, res, endpointOption });
|
||||||
|
|
||||||
expect(client.openAIApiKey).toBe('test-azure-api-key');
|
expect(client.openAIApiKey).toBe('WESTUS_API_KEY');
|
||||||
expect(client.client).toBeInstanceOf(OpenAIClient);
|
expect(client.client).toBeInstanceOf(OpenAIClient);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -291,7 +336,7 @@ describe('initializeClient', () => {
|
||||||
let userValues = getUserKey();
|
let userValues = getUserKey();
|
||||||
try {
|
try {
|
||||||
userValues = JSON.parse(userValues);
|
userValues = JSON.parse(userValues);
|
||||||
} catch (e) {
|
} catch {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
type: ErrorTypes.INVALID_USER_KEY,
|
type: ErrorTypes.INVALID_USER_KEY,
|
||||||
|
|
@ -307,6 +352,9 @@ describe('initializeClient', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should initialize client correctly for Azure OpenAI with valid configuration', async () => {
|
test('should initialize client correctly for Azure OpenAI with valid configuration', async () => {
|
||||||
|
// Set up Azure environment variables
|
||||||
|
process.env.WESTUS_API_KEY = 'test-westus-key';
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
body: {
|
body: {
|
||||||
key: null,
|
key: null,
|
||||||
|
|
@ -314,15 +362,6 @@ describe('initializeClient', () => {
|
||||||
model: modelNames[0],
|
model: modelNames[0],
|
||||||
},
|
},
|
||||||
user: { id: '123' },
|
user: { id: '123' },
|
||||||
app: {
|
|
||||||
locals: {
|
|
||||||
[EModelEndpoint.azureOpenAI]: {
|
|
||||||
modelNames,
|
|
||||||
modelGroupMap,
|
|
||||||
groupMap,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
const res = {};
|
const res = {};
|
||||||
const endpointOption = {};
|
const endpointOption = {};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue