Skip to content

Commit

Permalink
Catch errors when SECRETS is empty or doesnt have the right key
Browse files Browse the repository at this point in the history
  • Loading branch information
justyns committed Jul 28, 2024
1 parent 9718924 commit aa50176
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 42 deletions.
194 changes: 156 additions & 38 deletions src/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ ai:
provider: gemini
baseUrl: https://api.gemini.ai/v1
secretName: GOOGLE_AI_STUDIO_KEY
imageModels:
- name: dall-e
provider: dalle
modelName: dall-e
embeddingModels:
- name: text-embedding-3-small
provider: openai
Expand All @@ -45,45 +49,159 @@ OPENAI_API_KEY: bar
`;

Deno.test("initializeOpenAI should set aiSettings correctly", async () => {
try {
await syscall("mock.setPage", "SETTINGS", settingsPageSample);
await syscall("mock.setPage", "SECRETS", secretsPageSample);
await initializeOpenAI();
assertEquals(
aiSettings.textModels.length,
2,
"initializeOpenAI did not set the correct number of text models",
);
assertEquals(
aiSettings.textModels[0].name,
"gpt-4o",
"initializeOpenAI did not set the correct text model name",
);
} catch (error) {
console.error(
"Error in test 'initializeOpenAI should set aiSettings correctly':",
error,
);
throw error;
}
try {
await syscall("mock.setPage", "SETTINGS", settingsPageSample);
await syscall("mock.setPage", "SECRETS", secretsPageSample);
await initializeOpenAI();
assertEquals(
aiSettings.textModels.length,
2,
"initializeOpenAI did not set the correct number of text models",
);
assertEquals(
aiSettings.textModels[0].name,
"gpt-4o",
"initializeOpenAI did not set the correct text model name",
);
} catch (error) {
console.error(
"Error in test 'initializeOpenAI should set aiSettings correctly':",
error,
);
throw error;
}
});

Deno.test("initializeOpenAI should configure the selected model", async () => {
try {
await syscall("mock.setPage", "SETTINGS", settingsPageSample);
await syscall("mock.setPage", "SECRETS", secretsPageSample);
await initializeOpenAI();
await getAndConfigureModel();
assertEquals(
aiSettings.textModels[0].name,
"gpt-4o",
"getAndConfigureModel did not configure the correct text model",
);
} catch (error) {
console.error(
"Error in test 'initializeOpenAI should configure the selected model':",
error,
);
throw error;
}
try {
await syscall("mock.setPage", "SETTINGS", settingsPageSample);
await syscall("mock.setPage", "SECRETS", secretsPageSample);
await initializeOpenAI();
await getAndConfigureModel();
assertEquals(
aiSettings.textModels[0].name,
"gpt-4o",
"getAndConfigureModel did not configure the correct text model",
);
} catch (error) {
console.error(
"Error in test 'initializeOpenAI should configure the selected model':",
error,
);
throw error;
}
});

Deno.test("initializeOpenAI should set image models correctly", async () => {
try {
await syscall("mock.setPage", "SETTINGS", settingsPageSample);
await syscall("mock.setPage", "SECRETS", secretsPageSample);
await initializeOpenAI();
assertEquals(
aiSettings.imageModels.length,
1,
"initializeOpenAI did not set the correct number of image models",
);
assertEquals(
aiSettings.imageModels[0].name,
"dall-e",
"initializeOpenAI did not set the correct image model name",
);
} catch (error) {
console.error(
"Error in test 'initializeOpenAI should set image models correctly':",
error,
);
throw error;
}
});

Deno.test("initializeOpenAI should set embedding models correctly", async () => {
try {
await syscall("mock.setPage", "SETTINGS", settingsPageSample);
await syscall("mock.setPage", "SECRETS", secretsPageSample);
await initializeOpenAI();
assertEquals(
aiSettings.embeddingModels.length,
2,
"initializeOpenAI did not set the correct number of embedding models",
);
assertEquals(
aiSettings.embeddingModels[0].name,
"text-embedding-3-small",
"initializeOpenAI did not set the correct embedding model name",
);
} catch (error) {
console.error(
"Error in test 'initializeOpenAI should set embedding models correctly':",
error,
);
throw error;
}
});

Deno.test("initializeOpenAI should handle missing settings gracefully", async () => {
try {
await syscall("mock.setPage", "SETTINGS", "");
await syscall("mock.setPage", "SECRETS", secretsPageSample);
await initializeOpenAI();
assertEquals(
aiSettings.textModels.length,
0,
"initializeOpenAI did not handle missing settings correctly",
);
} catch (error) {
console.error(
"Error in test 'initializeOpenAI should handle missing settings gracefully':",
error,
);
throw error;
}
});

Deno.test("initializeOpenAI should throw an error if the API key is empty", async () => {
try {
const secretsPage = '```yaml\nOPENAI_API_KEY: ""\n```';
await syscall("mock.setPage", "SETTINGS", settingsPageSample);
await syscall("mock.setPage", "SECRETS", secretsPage);
await initializeOpenAI();
} catch (error) {
assertEquals(
error.message,
"AI API key is missing. Please set it in the secrets page.",
"initializeOpenAI did not handle empty secrets correctly",
);
}
});

Deno.test("initializeOpenAI should throw an error if the API secret is missing", async () => {
try {
const secretsPage = "```yaml\n\n```";
await syscall("mock.setPage", "SETTINGS", settingsPageSample);
await syscall("mock.setPage", "SECRETS", secretsPage);
await initializeOpenAI();
} catch (error) {
assertEquals(
error.message,
"Failed to read the AI API key. Please check the SECRETS page.",
"initializeOpenAI did not handle missing secrets correctly",
);
}
});

Deno.test(
"initializeOpenAI should throw an error if secrets page does not exist",
{ sanitizeOps: false, sanitizeResources: false },
async () => {
try {
await syscall("mock.setPage", "SETTINGS", settingsPageSample);
await initializeOpenAI();
} catch (error) {
assertEquals(
error.message,
"Failed to read the AI API key. Please check the SECRETS page.",
"initializeOpenAI did not handle missing secrets correctly",
);
}
},
);
15 changes: 11 additions & 4 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,17 @@ export async function configureSelectedModel(model: ModelConfig) {
}
model.requireAuth = model.requireAuth ?? aiSettings.requireAuth;
if (model.requireAuth) {
const newApiKey = await readSecret(model.secretName || "OPENAI_API_KEY");
if (newApiKey !== apiKey) {
apiKey = newApiKey;
log("client", "API key updated");
try {
const newApiKey = await readSecret(model.secretName || "OPENAI_API_KEY");
if (newApiKey !== apiKey) {
apiKey = newApiKey;
log("client", "API key updated");
}
} catch (error) {
console.error("Error reading secret:", error);
throw new Error(
"Failed to read the AI API key. Please check the SECRETS page.",
);
}
}
if (model.requireAuth && !apiKey) {
Expand Down

0 comments on commit aa50176

Please sign in to comment.