Skip to content

Commit

Permalink
Merge pull request #51 from justyns/coderabbit-test
Browse files Browse the repository at this point in the history
Catch errors when SECRETS is empty or doesnt have the right key
  • Loading branch information
justyns authored Jul 28, 2024
2 parents a1f849c + 79cd934 commit 5f8332c
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 4 deletions.
118 changes: 118 additions & 0 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 Down Expand Up @@ -87,3 +91,117 @@ Deno.test("initializeOpenAI should configure the selected model", async () => {
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 5f8332c

Please sign in to comment.