test: consolidate repetitive cases across the suite

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-25 14:28:21 +08:00
1 parent c406024b59
commit 6eccc2859a
31 files changed
+745 -1315

No files matched your search

+56 -77
View File
@@ -583,6 +583,22 @@ def test_provider_logout_openai_codex_removes_local_oauth_files(tmp_path, monkey
assert "Logged out from OpenAI Codex" in result.stdout
def test_provider_logout_github_copilot_removes_local_oauth_files(tmp_path, monkeypatch):
token_path = tmp_path / "auth" / "github-copilot.json"
lock_path = token_path.with_suffix(".lock")
token_path.parent.mkdir(parents=True, exist_ok=True)
token_path.write_text("{}", encoding="utf-8")
lock_path.write_text("", encoding="utf-8")
monkeypatch.setenv("OAUTH_CLI_KIT_TOKEN_PATH", str(token_path))
result = runner.invoke(app, ["provider", "logout", "github-copilot"])
assert result.exit_code == 0
assert not token_path.exists()
assert not lock_path.exists()
assert "Logged out from GitHub Copilot" in result.stdout
def test_provider_logout_openai_codex_succeeds_when_no_local_oauth_file(monkeypatch, tmp_path):
token_path = tmp_path / "auth" / "codex.json"
monkeypatch.setenv("OAUTH_CLI_KIT_TOKEN_PATH", str(token_path))
@@ -634,22 +650,6 @@ def test_provider_logout_xai_grok_uses_explicit_config_path(tmp_path, monkeypatc
assert "Using config:" in result.stdout
def test_provider_logout_github_copilot_removes_local_oauth_files(tmp_path, monkeypatch):
token_path = tmp_path / "auth" / "github-copilot.json"
lock_path = token_path.with_suffix(".lock")
token_path.parent.mkdir(parents=True, exist_ok=True)
token_path.write_text("{}", encoding="utf-8")
lock_path.write_text("", encoding="utf-8")
monkeypatch.setenv("OAUTH_CLI_KIT_TOKEN_PATH", str(token_path))
result = runner.invoke(app, ["provider", "logout", "github-copilot"])
assert result.exit_code == 0
assert not token_path.exists()
assert not lock_path.exists()
assert "Logged out from GitHub Copilot" in result.stdout
def test_provider_logout_github_copilot_succeeds_when_no_local_oauth_file(monkeypatch, tmp_path):
token_path = tmp_path / "auth" / "github-copilot.json"
monkeypatch.setenv("OAUTH_CLI_KIT_TOKEN_PATH", str(token_path))
@@ -1000,48 +1000,43 @@ def test_config_accepts_camel_case_explicit_provider_name_for_coding_plan():
assert config.get_api_base() == "https://ark.cn-beijing.volces.com/api/coding/v3"
def test_config_accepts_lm_studio_without_api_key_and_uses_default_localhost_api_base():
@pytest.mark.parametrize(
"provider_name, api_base, config_key",
[
pytest.param(
"lm_studio",
"http://localhost:1234/v1",
"lmStudio",
id="lm_studio_without_api_key_and_uses_default_localhost_api_base",
),
pytest.param(
"atomic_chat",
"http://localhost:1337/v1",
"atomicChat",
id="atomic_chat_without_api_key_and_uses_default_localhost_api_base",
),
],
)
def test_local_provider_defaults_without_api_key(provider_name, api_base, config_key):
config = Config.model_validate(
{
"agents": {
"defaults": {
"provider": "lm_studio",
"provider": provider_name,
"model": "local-model",
}
},
"providers": {
"lmStudio": {
config_key: {
"apiKey": None,
}
},
}
)
assert config.get_provider_name() == "lm_studio"
assert config.get_provider_name() == provider_name
assert config.get_api_key() is None
assert config.get_api_base() == "http://localhost:1234/v1"
def test_config_accepts_atomic_chat_without_api_key_and_uses_default_localhost_api_base():
config = Config.model_validate(
{
"agents": {
"defaults": {
"provider": "atomic_chat",
"model": "local-model",
}
},
"providers": {
"atomicChat": {
"apiKey": None,
}
},
}
)
assert config.get_provider_name() == "atomic_chat"
assert config.get_api_key() is None
assert config.get_api_base() == "http://localhost:1337/v1"
assert config.get_api_base() == api_base
def test_find_by_name_accepts_camel_case_and_hyphen_aliases():
@@ -1441,44 +1436,28 @@ def test_make_provider_strips_dynamic_custom_route_prefix_from_request_model():
assert body["model"] == "gpt-4o-mini"
def test_make_provider_preserves_namespaced_model_for_forced_dynamic_provider():
@pytest.mark.parametrize(
"provider_name, model",
[
pytest.param(
"my-company-api",
"openai/gpt-4o-mini",
id="preserves_namespaced_model_for_forced_dynamic_provider",
),
pytest.param(
"auto",
"my-company-api/openai/gpt-4o-mini",
id="strips_dynamic_custom_route_prefix_once",
),
],
)
def test_make_provider_preserves_dynamic_provider_model_namespace(provider_name, model):
config = Config.model_validate(
{
"agents": {
"defaults": {
"provider": "my-company-api",
"model": "openai/gpt-4o-mini",
}
},
"providers": {
"my-company-api": {
"apiBase": "https://example.com/v1",
}
},
}
)
provider = make_provider(config)
kwargs = provider._build_kwargs(
messages=[{"role": "user", "content": "hi"}],
tools=None,
model=None,
max_tokens=16,
temperature=0.1,
reasoning_effort=None,
tool_choice=None,
)
assert kwargs["model"] == "openai/gpt-4o-mini"
def test_make_provider_strips_dynamic_custom_route_prefix_once():
config = Config.model_validate(
{
"agents": {
"defaults": {
"provider": "auto",
"model": "my-company-api/openai/gpt-4o-mini",
"provider": provider_name,
"model": model,
}
},
"providers": {
+9 -27
View File
@@ -34,11 +34,17 @@ async def test_interactive_retry_wait_is_rendered_as_progress_even_when_progress
@pytest.mark.asyncio
async def test_reasoning_displayed_when_show_reasoning_enabled():
"""Reasoning content should be displayed when show_reasoning is True."""
@pytest.mark.parametrize(
"send_progress",
[
pytest.param(True, id="displayed_when_show_reasoning_enabled"),
pytest.param(False, id="shown_when_send_progress_disabled"),
],
)
async def test_reasoning_display_is_independent_of_send_progress(send_progress):
calls: list[str] = []
channels_config = SimpleNamespace(
send_progress=True, send_tool_hints=False, show_reasoning=True,
send_progress=send_progress, send_tool_hints=False, show_reasoning=True,
)
msg = SimpleNamespace(
content="Let me think about this...",
@@ -183,27 +189,3 @@ async def test_non_reasoning_progress_not_affected_by_show_reasoning():
assert handled is True
assert calls == ["working on it..."]
@pytest.mark.asyncio
async def test_reasoning_shown_when_send_progress_disabled():
"""Reasoning display is governed by `show_reasoning` alone, independent
of `send_progress` — the two knobs are orthogonal."""
calls: list[str] = []
channels_config = SimpleNamespace(
send_progress=False, send_tool_hints=False, show_reasoning=True,
)
msg = SimpleNamespace(
content="Let me think about this...",
event=ProgressEvent(content="Let me think about this...", reasoning=True),
metadata={},
)
with patch(
"nanobot.cli.terminal._print_cli_reasoning",
side_effect=lambda t, th, r=None: calls.append(t),
):
handled = await terminal._maybe_print_interactive_progress(msg, None, channels_config)
assert handled is True
assert calls == ["Let me think about this..."]
+12 -20
View File
@@ -1,10 +1,19 @@
from types import SimpleNamespace
import pytest
from nanobot.cli.webui_support import _prepare_webui_bundle_for_gateway
from nanobot.config.schema import Config
def test_source_checkout_preserves_warn_only_gateway_startup(monkeypatch) -> None:
@pytest.mark.parametrize(
"mode",
[
pytest.param("warn", id="source_checkout_preserves_warn_only_gateway_startup"),
pytest.param("skip", id="skip_mode_does_not_build_the_source_webui_bundle"),
],
)
def test_source_checkout_respects_webui_build_mode(monkeypatch, mode) -> None:
modes: list[str] = []
monkeypatch.setattr(
"nanobot.cli.webui_support.inspect_webui_bundle",
@@ -16,23 +25,6 @@ def test_source_checkout_preserves_warn_only_gateway_startup(monkeypatch) -> Non
lambda **kwargs: modes.append(kwargs["mode"]),
)
_prepare_webui_bundle_for_gateway(Config(), mode="warn")
_prepare_webui_bundle_for_gateway(Config(), mode=mode)
assert modes == ["warn"]
def test_skip_mode_does_not_build_the_source_webui_bundle(monkeypatch) -> None:
modes: list[str] = []
monkeypatch.setattr(
"nanobot.cli.webui_support.inspect_webui_bundle",
lambda: SimpleNamespace(source_available=True),
)
monkeypatch.setattr("nanobot.cli.webui_support._webui_channel_enabled", lambda _config: True)
monkeypatch.setattr(
"nanobot.cli.webui_support.ensure_webui_bundle",
lambda **kwargs: modes.append(kwargs["mode"]),
)
_prepare_webui_bundle_for_gateway(Config(), mode="skip")
assert modes == ["skip"]
assert modes == [mode]