-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: clear provider guidance on praisonai --init when no LLM key set #2338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+183
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/praisonai/tests/unit/cli/test_init_provider_guard.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| """ | ||
| Unit tests for the `praisonai --init` provider pre-flight guard. | ||
|
|
||
| Verifies that when no LLM provider credential is configured, the guard returns a | ||
| clear, actionable message (pointing at `praisonai setup`) instead of letting the | ||
| `--init` flow call the LLM and surface a raw stack trace. When a provider IS | ||
| configured, the guard must return None so generation proceeds. | ||
| """ | ||
|
|
||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| try: | ||
| import praisonai.cli.main as cli_main | ||
| from praisonai.cli.main import _provider_preflight_message | ||
| except ImportError as e: # pragma: no cover - environment guard | ||
| pytest.skip(f"Could not import praisonai.cli.main: {e}", allow_module_level=True) | ||
|
|
||
|
|
||
| class TestProviderPreflightMessage: | ||
| def test_returns_message_when_unconfigured(self): | ||
| with patch("praisonai.llm.credentials.is_configured", return_value=False): | ||
| msg = _provider_preflight_message() | ||
|
|
||
| assert msg is not None | ||
| assert "No LLM provider is configured" in msg | ||
| # Points beginners at the no-export interactive setup. | ||
| assert "praisonai setup" in msg | ||
| # Mentions multiple providers so users know it's not OpenAI-only. | ||
| assert "Anthropic" in msg and "Gemini" in msg | ||
|
|
||
| def test_returns_none_when_configured(self): | ||
| with patch("praisonai.llm.credentials.is_configured", return_value=True): | ||
| assert _provider_preflight_message() is None | ||
|
|
||
| def test_never_blocks_on_internal_error(self): | ||
| # If the credential check itself raises, the guard must not block a | ||
| # potentially configured user (returns None -> generation proceeds). | ||
| with patch( | ||
| "praisonai.llm.credentials.is_configured", | ||
| side_effect=RuntimeError("boom"), | ||
| ): | ||
| assert _provider_preflight_message() is None | ||
|
|
||
| def test_injects_stored_credentials_before_gating(self): | ||
| # A key stored via `praisonai setup` must be exported into the env | ||
| # before gating, otherwise the env-only AutoGenerator path would still | ||
| # fail with a raw auth error despite the guard passing. | ||
| with patch( | ||
| "praisonai.llm.credentials.inject_credentials_into_env" | ||
| ) as inject, patch( | ||
| "praisonai.llm.credentials.is_configured", return_value=True | ||
| ): | ||
| assert _provider_preflight_message() is None | ||
| inject.assert_called_once() | ||
|
|
||
| def test_gates_on_runtime_model_override(self): | ||
| # With a stale OpenAI model override and only a non-OpenAI key, the | ||
| # gate must use the exact runtime model so the mismatch is caught here | ||
| # (message returned) rather than failing later at the LLM call. | ||
| with patch.dict( | ||
| "os.environ", {"MODEL_NAME": "gpt-4o-mini"}, clear=False | ||
| ), patch( | ||
| "praisonai.llm.credentials.inject_credentials_into_env" | ||
| ), patch( | ||
| "praisonai.llm.credentials.is_configured", return_value=False | ||
| ) as is_configured: | ||
| msg = _provider_preflight_message() | ||
|
|
||
| assert msg is not None | ||
| # The runtime model override must be passed through to the gate. | ||
| is_configured.assert_called_once_with(model="gpt-4o-mini") | ||
|
|
||
|
|
||
| class TestInitGuardWiring: | ||
| def test_init_returns_early_without_calling_generator(self): | ||
| # The real --init path must print guidance and return early WITHOUT | ||
| # constructing the AutoGenerator when no provider is configured. This | ||
| # protects against the guard being removed or moved after generation. | ||
| instance = cli_main.PraisonAI.__new__(cli_main.PraisonAI) | ||
| instance.agent_file = "agents.yaml" | ||
| instance.config_list = [{"model": "gpt-4o-mini"}] | ||
| instance.framework = None | ||
| instance.auto = False | ||
| instance.init = False | ||
| instance.topic = "" | ||
|
|
||
| # Use a plain namespace (not MagicMock) so unset attributes don't read | ||
| # as truthy and trip earlier branches before the --init guard. | ||
| from types import SimpleNamespace | ||
|
|
||
| args = SimpleNamespace( | ||
| command=None, | ||
| framework=None, | ||
| model=None, | ||
| prompt_flag=None, | ||
| file=None, | ||
| direct_prompt=None, | ||
| deploy=False, | ||
| auto=False, | ||
| init="build me a team", | ||
| ui=None, | ||
| merge=False, | ||
| ) | ||
|
|
||
| with patch.object(instance, "parse_args", return_value=(args, [])), patch.object( | ||
| cli_main, "_load_env_once" | ||
| ), patch.object( | ||
| instance, "read_stdin_if_available", return_value=None | ||
| ), patch.object( | ||
| instance, "read_file_if_provided", return_value=None | ||
| ), patch.object( | ||
| cli_main, "_provider_preflight_message", return_value="SETUP GUIDANCE" | ||
| ), patch.object( | ||
| cli_main, "_get_auto_generator" | ||
| ) as get_gen, patch("builtins.print"): | ||
| result = instance.main() | ||
|
|
||
| assert result == "SETUP GUIDANCE" | ||
| get_gen.assert_not_called() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check still disagrees with the runtime path for OpenRouter models.
resolve_llm_endpoint()treatsopenrouter/...as requiringOPENROUTER_API_KEY, but the credential guard path does not map OpenRouter for model-scoped checks or stored credential injection. WhenMODEL_NAME=openrouter/...is set and the key was saved throughpraisonai setup, this guard can proceed while the later generator path never exportsOPENROUTER_API_KEY, so--initcan still end in the raw provider auth failure instead of the setup guidance.