fix: improve API key error messages and add tests for credential checks#41
Conversation
MicaPerdomo
left a comment
There was a problem hiding this comment.
Review: fix: improve API key error messages and add tests for credential checks
1. PR Summary
- What it changes: Replaces
not api_basechecks withnot api_key and not COGSOL_AUTH_CLIENT_IDchecks in all 3 commands (chat, migrate, importagent), improves error messages with onboarding instructions, and adds tests. - What it solves: Issue #40 — error messages when credentials were missing were confusing and referenced
COGSOL_API_BASEwhich is no longer required. - Areas affected: 3 management commands,
CogSolClient._refresh_bearer_token, README, new tests. - Risk level: Medium
2. Findings
Important — Check semantics change but don't cover both cases
The PR replaces not api_base with not api_key and not COGSOL_AUTH_CLIENT_ID. The old check was dead code (get_cognitive_api_base_url() always returns a string due to its hardcoded fallbacks), so the change improves the situation.
However, api_base is still used after the check (passed to CogSolClient). If someone explicitly set COGSOL_API_BASE="", they'd pass the credentials check but get a cryptic error from the client. Unlikely thanks to the fallback, but ideally both validations should be kept.
Important — importagent.py has import os inside handle()
In importagent.py line 294, there's a local import os inside handle(). The other two commands import it at module level. The new code adds os.environ.get("COGSOL_AUTH_CLIENT_ID") which depends on this import. Should be moved to top-level for consistency.
Important — Output inconsistency: print_error vs print
chat.pyusesprint_error()(with ANSI formatting)migrate.pyusesprint()with a manual "Error:" prefiximportagent.pyusesprint()with a manual "Error:" prefix
Since print_error already exists, all 3 commands should use it for a consistent user experience.
Minor — Tests are overly granular
293 lines of tests for a simple change. Many tests verify the same thing split into individual assertions (one test for "No API credentials found", another for "COGSOL_API_KEY", another for "onboarding URL" — all on the same output from the same command). Could be consolidated: one test per command validating the important parts of the message + one test for the happy path.
Minor — _bare_client() accesses internal attributes
The helper uses CogSolClient.__new__ and sets attributes manually. If __init__ changes, these tests won't break when they should. Fragile but acceptable for this scope.
3. Regression risks
- Low: The old check never triggered, so replacing it doesn't change the actual flow.
- Medium: If any user was running commands without
COGSOL_API_KEYorCOGSOL_AUTH_CLIENT_ID(only with a customCOGSOL_API_BASE), they'd now be blocked. Worth checking if that use case exists.
4. Missing or recommended tests
- Test for
COGSOL_API_KEY=""(empty string) — does it pass the check or not? - Test for both variables present simultaneously
- Consider consolidating tests to reduce verbosity
5. Final verdict
Approvable with minor changes
The change significantly improves the user experience. Recommended adjustments before merge:
- Unify
print_errorvsprintacross all 3 commands - Move the
import osinimportagent.pyto top-level - Consider consolidating tests
Description
Improves the error messages shown when API credentials are missing across
chat,importagent, andmigratemanagement commands, and in the core authentication client. Previous messages were vague and only referencedCOGSOL_API_BASE; the new messages correctly check forCOGSOL_API_KEY(orCOGSOL_AUTH_CLIENT_IDas fallback) and guide the user on how to obtain and configure their credentials.Type of Change
Related Issues
Fixes #40
Changes Made
COGSOL_API_BASE is requiredmessages with descriptive errors that check for the actual credential variables (COGSOL_API_KEY/COGSOL_AUTH_CLIENT_ID)https://onboarding.cogsol.aiin the error output forchat,importagent, andmigratecommandsCOGSOL_AUTH_SECRETerror incogsol/core/api.pywith the same onboarding URL guidancetests/test_api_key_errors.pywith unit tests covering all missing-credential scenarios for the three commands and the API clientTesting Done