Fix bug 1: Only fetch Anthropic models when API key exists
getAnthropicModels() was called unconditionally on mount, causing a console error when no API key was set. Now chains the call after getAnthropicApiKeyExists() confirms a key is present. Includes regression test added before the fix per bug workflow. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -592,4 +592,17 @@ describe("Chat review panel", () => {
|
||||
|
||||
expect(await screen.findByText("Cannot read stories")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("does not fetch Anthropic models when no API key exists", async () => {
|
||||
mockedApi.getAnthropicApiKeyExists.mockResolvedValue(false);
|
||||
mockedApi.getAnthropicModels.mockClear();
|
||||
|
||||
render(<Chat projectPath="/tmp/project" onCloseProject={vi.fn()} />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockedApi.getAnthropicApiKeyExists).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
expect(mockedApi.getAnthropicModels).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -174,26 +174,21 @@ export function Chat({ projectPath, onCloseProject }: ChatProps) {
|
||||
.getAnthropicApiKeyExists()
|
||||
.then((exists) => {
|
||||
setHasAnthropicKey(exists);
|
||||
if (!exists) return;
|
||||
return api.getAnthropicModels().then((models) => {
|
||||
if (models.length > 0) {
|
||||
const sortedModels = models.sort((a, b) =>
|
||||
a.toLowerCase().localeCompare(b.toLowerCase()),
|
||||
);
|
||||
setClaudeModels(sortedModels);
|
||||
} else {
|
||||
setClaudeModels([]);
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
setHasAnthropicKey(false);
|
||||
});
|
||||
|
||||
api
|
||||
.getAnthropicModels()
|
||||
.then((models) => {
|
||||
if (models.length > 0) {
|
||||
const sortedModels = models.sort((a, b) =>
|
||||
a.toLowerCase().localeCompare(b.toLowerCase()),
|
||||
);
|
||||
setClaudeModels(sortedModels);
|
||||
} else {
|
||||
setClaudeModels([]);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
setClaudeModels([]);
|
||||
});
|
||||
}, []);
|
||||
|
||||
Reference in New Issue
Block a user