From 7a6a41a72ef4c65e85e7ded6825cb39176f2556a Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Tue, 5 Mar 2024 14:33:45 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20fix(ci):=20update=20failing=20`i?= =?UTF-8?q?nitializeClient`=20tests=20with=20new=20expected=20values=20(#1?= =?UTF-8?q?982)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(ci): update failing tests with new expected values from `getUserKey` * refactor: safer optional chaining, and ensure apiKey is defined --- .../Endpoints/custom/initializeClient.js | 4 ++-- .../Endpoints/gptPlugins/initializeClient.js | 4 ++-- .../gptPlugins/initializeClient.spec.js | 3 ++- .../Endpoints/openAI/initializeClient.js | 4 ++-- .../Endpoints/openAI/initializeClient.spec.js | 23 ++++++++++--------- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/api/server/services/Endpoints/custom/initializeClient.js b/api/server/services/Endpoints/custom/initializeClient.js index 18ca8c4455..a80f5efaa7 100644 --- a/api/server/services/Endpoints/custom/initializeClient.js +++ b/api/server/services/Endpoints/custom/initializeClient.js @@ -60,8 +60,8 @@ const initializeClient = async ({ req, res, endpointOption }) => { } } - let apiKey = userProvidesKey ? userValues.apiKey : CUSTOM_API_KEY; - let baseURL = userProvidesURL ? userValues.baseURL : CUSTOM_BASE_URL; + let apiKey = userProvidesKey ? userValues?.apiKey : CUSTOM_API_KEY; + let baseURL = userProvidesURL ? userValues?.baseURL : CUSTOM_BASE_URL; if (!apiKey) { throw new Error(`${endpoint} API key not provided.`); diff --git a/api/server/services/Endpoints/gptPlugins/initializeClient.js b/api/server/services/Endpoints/gptPlugins/initializeClient.js index a596c7820b..2920a58917 100644 --- a/api/server/services/Endpoints/gptPlugins/initializeClient.js +++ b/api/server/services/Endpoints/gptPlugins/initializeClient.js @@ -63,8 +63,8 @@ const initializeClient = async ({ req, res, endpointOption }) => { } } - let apiKey = userProvidesKey ? userValues.apiKey : credentials[endpoint]; - let baseURL = userProvidesURL ? userValues.baseURL : baseURLOptions[endpoint]; + let apiKey = userProvidesKey ? userValues?.apiKey : credentials[endpoint]; + let baseURL = userProvidesURL ? userValues?.baseURL : baseURLOptions[endpoint]; const clientOptions = { contextStrategy, diff --git a/api/server/services/Endpoints/gptPlugins/initializeClient.spec.js b/api/server/services/Endpoints/gptPlugins/initializeClient.spec.js index 01718c31d2..1b7147d9f7 100644 --- a/api/server/services/Endpoints/gptPlugins/initializeClient.spec.js +++ b/api/server/services/Endpoints/gptPlugins/initializeClient.spec.js @@ -340,9 +340,10 @@ describe('gptPlugins/initializeClient', () => { test('should initialize client with default options when certain env vars are not set', async () => { delete process.env.DEBUG_OPENAI; delete process.env.OPENAI_SUMMARIZE; + process.env.OPENAI_API_KEY = 'some-api-key'; const req = { - body: { key: null, endpoint: 'openAI' }, + body: { key: null, endpoint: EModelEndpoint.gptPlugins }, user: { id: '123' }, app, }; diff --git a/api/server/services/Endpoints/openAI/initializeClient.js b/api/server/services/Endpoints/openAI/initializeClient.js index 06dad5963a..9dd9765dd0 100644 --- a/api/server/services/Endpoints/openAI/initializeClient.js +++ b/api/server/services/Endpoints/openAI/initializeClient.js @@ -50,8 +50,8 @@ const initializeClient = async ({ req, res, endpointOption }) => { } } - let apiKey = userProvidesKey ? userValues.apiKey : credentials[endpoint]; - let baseURL = userProvidesURL ? userValues.baseURL : baseURLOptions[endpoint]; + let apiKey = userProvidesKey ? userValues?.apiKey : credentials[endpoint]; + let baseURL = userProvidesURL ? userValues?.baseURL : baseURLOptions[endpoint]; const clientOptions = { debug: isEnabled(DEBUG_OPENAI), diff --git a/api/server/services/Endpoints/openAI/initializeClient.spec.js b/api/server/services/Endpoints/openAI/initializeClient.spec.js index bf31d2d9bf..1a53f95b3d 100644 --- a/api/server/services/Endpoints/openAI/initializeClient.spec.js +++ b/api/server/services/Endpoints/openAI/initializeClient.spec.js @@ -94,7 +94,7 @@ describe('initializeClient', () => { process.env.OPENAI_SUMMARIZE = 'false'; const req = { - body: { key: null, endpoint: 'openAI' }, + body: { key: null, endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -137,7 +137,7 @@ describe('initializeClient', () => { process.env.DEBUG_OPENAI = 'true'; const req = { - body: { key: null, endpoint: 'openAI' }, + body: { key: null, endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -154,7 +154,7 @@ describe('initializeClient', () => { process.env.OPENAI_SUMMARIZE = 'true'; const req = { - body: { key: null, endpoint: 'openAI' }, + body: { key: null, endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -172,7 +172,7 @@ describe('initializeClient', () => { process.env.PROXY = 'http://proxy'; const req = { - body: { key: null, endpoint: 'openAI' }, + body: { key: null, endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -193,7 +193,7 @@ describe('initializeClient', () => { const expiresAt = new Date(Date.now() - 10000).toISOString(); // Expired const req = { - body: { key: expiresAt, endpoint: 'openAI' }, + body: { key: expiresAt, endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -209,7 +209,7 @@ describe('initializeClient', () => { delete process.env.AZURE_API_KEY; const req = { - body: { key: null, endpoint: 'openAI' }, + body: { key: null, endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -226,7 +226,7 @@ describe('initializeClient', () => { const req = { body: { key: new Date(Date.now() + 10000).toISOString(), - endpoint: 'openAI', + endpoint: EModelEndpoint.openAI, }, user: { id: '123', @@ -253,7 +253,7 @@ describe('initializeClient', () => { test('should throw an error if the user-provided key is invalid', async () => { const invalidKey = new Date(Date.now() - 100000).toISOString(); const req = { - body: { key: invalidKey, endpoint: 'openAI' }, + body: { key: invalidKey, endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -272,7 +272,7 @@ describe('initializeClient', () => { test('should throw an error when user-provided values are not valid JSON', async () => { process.env.OPENAI_API_KEY = 'user_provided'; const req = { - body: { key: new Date(Date.now() + 10000).toISOString(), endpoint: 'openAI' }, + body: { key: new Date(Date.now() + 10000).toISOString(), endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -315,9 +315,10 @@ describe('initializeClient', () => { test('should initialize client with default options when certain env vars are not set', async () => { delete process.env.DEBUG_OPENAI; delete process.env.OPENAI_SUMMARIZE; + process.env.OPENAI_API_KEY = 'some-api-key'; const req = { - body: { key: null, endpoint: 'openAI' }, + body: { key: null, endpoint: EModelEndpoint.openAI }, user: { id: '123' }, app, }; @@ -336,7 +337,7 @@ describe('initializeClient', () => { const req = { body: { key: new Date(Date.now() + 10000).toISOString(), - endpoint: 'openAI', + endpoint: EModelEndpoint.openAI, }, user: { id: '123',