refactor(loadConfigModels): Fallback to Default Models if Fetch Fails (#2236)

This commit is contained in:
Danny Avila 2024-03-29 10:43:36 -04:00 committed by GitHub
parent a00756c469
commit 3a1d07136c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 64 additions and 0 deletions

View file

@ -69,6 +69,8 @@ async function loadConfigModels(req) {
apiKey: API_KEY,
name,
userIdQuery: models.userIdQuery,
}).then((result) => {
return !result?.length ? models.default ?? [] : result;
});
uniqueKeyToNameMap[uniqueKey] = uniqueKeyToNameMap[uniqueKey] || [];
uniqueKeyToNameMap[uniqueKey].push(name);

View file

@ -262,4 +262,66 @@ describe('loadConfigModels', () => {
}),
);
});
it('falls back to default models if fetching returns an empty array', async () => {
getCustomConfig.mockResolvedValue({
endpoints: {
custom: [
{
name: 'EmptyFetchModel',
apiKey: 'API_KEY',
baseURL: 'http://example.com',
models: {
fetch: true,
default: ['defaultModel1', 'defaultModel2'],
},
},
],
},
});
fetchModels.mockResolvedValue([]);
const result = await loadConfigModels(mockRequest);
expect(fetchModels).toHaveBeenCalledWith(
expect.objectContaining({
name: 'EmptyFetchModel',
apiKey: 'API_KEY',
}),
);
expect(result.EmptyFetchModel).toEqual(['defaultModel1', 'defaultModel2']);
});
it('falls back to default models if fetching returns a falsy value', async () => {
getCustomConfig.mockResolvedValue({
endpoints: {
custom: [
{
name: 'FalsyFetchModel',
apiKey: 'API_KEY',
baseURL: 'http://example.com',
models: {
fetch: true,
default: ['defaultModel1', 'defaultModel2'],
},
},
],
},
});
fetchModels.mockResolvedValue(false);
const result = await loadConfigModels(mockRequest);
expect(fetchModels).toHaveBeenCalledWith(
expect.objectContaining({
name: 'FalsyFetchModel',
apiKey: 'API_KEY',
}),
);
expect(result.FalsyFetchModel).toEqual(['defaultModel1', 'defaultModel2']);
});
});