from pathlib import Path import pytest import afk_developer.config as config_module from afk_developer.config import ( Config, EndpointProfile, EndpointType, LintMode, RoleRoute, _apply_user_config, _load_user_config, save_user_config, ) from afk_developer.backends.models import BackendUsage, ModelUsage def test_default_environment_config_prefers_copilot_without_api_key(monkeypatch): monkeypatch.delenv("DEEPSEEK_API_KEY", raising=False) monkeypatch.delenv("OPENAI_API_KEY", raising=False) monkeypatch.setenv("AFK_CONFIG_PATH", str(Path("missing-config.yml").resolve())) monkeypatch.setattr(config_module, "_load_dotenv", lambda *_paths: None) config = Config.from_env() assert config.is_configured assert config.endpoint_for("reasoner").type is EndpointType.COPILOT assert config.route_for("reasoner").endpoint == "copilot" assert config.route_for("coder") == config.route_for("reasoner") def test_deepseek_environment_preserves_legacy_default_route(monkeypatch): monkeypatch.setenv("DEEPSEEK_API_KEY", "test-key") monkeypatch.setenv("AFK_CONFIG_PATH", str(Path("missing-config.yml").resolve())) config = Config.from_env() assert config.route_for("reasoner").endpoint == "deepseek" assert config.route_for("coder").endpoint == "deepseek" def test_user_config_can_route_reasoner_and_coder_independently(): config = Config() _apply_user_config( config, { "endpoints": { "copilot": {"type": "copilot"}, "deepseek": { "type": "openai_compatible", "base_url": "https://api.deepseek.com", "api_key_ref": "${secret:deepseek-api-key}", }, }, "roles": { "reasoner": {"endpoint": "copilot", "model": "gpt-5", "timeout": 120}, "coder": {"endpoint": "deepseek", "model": "deepseek-v4-pro"}, }, }, ) assert config.reasoner == RoleRoute(endpoint="copilot", model="gpt-5", timeout=120.0) assert config.coder.endpoint == "deepseek" assert config.coder.model == "deepseek-v4-pro" assert config.endpoint_for("coder").api_key_ref == "${secret:deepseek-api-key}" def test_unknown_configured_endpoint_has_actionable_error(): config = Config( endpoints={"copilot": EndpointProfile("copilot", EndpointType.COPILOT)}, reasoner=RoleRoute(endpoint="missing", model="gpt-5"), ) with pytest.raises(ValueError, match="unknown endpoint 'missing'"): config.endpoint_for("reasoner") def test_foreman_and_coder_light_routes_inherit_by_default(): config = Config( endpoints={"copilot": EndpointProfile("copilot", EndpointType.COPILOT)}, ) assert config.route_for("foreman") == config.route_for("reasoner") assert config.route_for("coder_light") == config.route_for("coder") def test_user_config_can_route_foreman_and_coder_light_independently(): config = Config() _apply_user_config( config, { "endpoints": { "copilot": {"type": "copilot"}, "deepseek": { "type": "openai_compatible", "base_url": "https://api.deepseek.com", "api_key_ref": "${secret:deepseek-api-key}", }, }, "roles": { "foreman": {"endpoint": "copilot", "model": "gpt-5.6-terra", "timeout": 300}, "coder_light": {"endpoint": "deepseek", "model": "deepseek-v4-flash"}, }, }, ) assert config.route_for("foreman").model == "gpt-5.6-terra" assert config.route_for("coder_light").endpoint == "deepseek" assert config.route_for("coder_light").model == "deepseek-v4-flash" def test_invalid_reasoning_effort_is_rejected(): with pytest.raises(ValueError, match="invalid reasoning_effort"): _apply_user_config( Config(), {"roles": {"reasoner": {"reasoning_effort": "maximum"}}}, ) def test_save_user_config_writes_valid_yaml_atomically(tmp_path, monkeypatch): target = tmp_path / "config.yml" monkeypatch.setenv("AFK_CONFIG_PATH", str(target)) raw = {"endpoints": {"copilot": {"type": "copilot"}}} assert save_user_config(raw) == target assert _load_user_config() == raw assert not list(tmp_path.glob("tmp*")) def test_review_policy_configures_baseline_mode_and_resume_cap(): config = Config() _apply_user_config( config, {"review": {"lint_mode": "baseline", "max_auto_resume_passes": 4}}, ) assert config.lint_mode is LintMode.BASELINE assert config.max_auto_resume_passes == 4 def test_review_policy_rejects_invalid_auto_resume_cap(): with pytest.raises(ValueError, match="at least 1"): _apply_user_config(Config(), {"review": {"max_auto_resume_passes": 0}}) def test_worker_policy_configures_shell_timeout(): config = Config() _apply_user_config(config, {"worker": {"max_shell_timeout": 300}}) assert config.max_shell_timeout == 300 def test_worker_policy_configures_summarization_max_context_chars(): config = Config() _apply_user_config(config, {"worker": {"summarization_max_context_chars": 123_456}}) assert config.summarization_max_context_chars == 123_456 def test_worker_discovery_progress_defaults_are_enabled(): config = Config() assert config.discovery_progress_turn_limit == 8 assert config.discovery_progress_tool_limit == 20 def test_worker_policy_configures_discovery_progress_limits_and_allows_zero(): config = Config() _apply_user_config( config, { "worker": { "discovery_progress_turn_limit": 0, "discovery_progress_tool_limit": 12, } }, ) assert config.discovery_progress_turn_limit == 0 assert config.discovery_progress_tool_limit == 12 def test_worker_policy_rejects_invalid_discovery_progress_limits(): with pytest.raises(ValueError, match="discovery_progress_turn_limit.*at least 0"): _apply_user_config(Config(), {"worker": {"discovery_progress_turn_limit": -1}}) with pytest.raises(ValueError, match="discovery_progress_tool_limit.*integer"): _apply_user_config(Config(), {"worker": {"discovery_progress_tool_limit": "many"}}) def test_worker_policy_rejects_invalid_shell_timeout(): with pytest.raises(ValueError, match="at least 1"): _apply_user_config(Config(), {"worker": {"max_shell_timeout": 0}}) def test_session_stats_records_copilot_ai_credits_and_model_breakdown(): usage = BackendUsage( prompt_tokens=100, completion_tokens=25, total_tokens=125, cache_read_tokens=50, cache_write_tokens=10, reasoning_tokens=15, api_calls=1, agent_turns=1, copilot_prompts_submitted=1, copilot_nano_aiu=2_500_000_000.0, telemetry_available=True, model_usages=( ModelUsage( model="gpt-5.6-luna", endpoint="copilot", prompt_tokens=100, completion_tokens=25, total_tokens=125, cache_read_tokens=50, cache_write_tokens=10, reasoning_tokens=15, api_calls=1, agent_turns=1, copilot_nano_aiu=2_500_000_000.0, telemetry_available=True, ), ), ) config = Config( endpoints={"copilot": EndpointProfile("copilot", EndpointType.COPILOT)}, coder=RoleRoute(endpoint="copilot", model="gpt-5.6-luna"), ) config.record_usage(usage, role="coder") assert config.stats.api_calls == 1 assert config.stats.total_turns == 1 assert config.stats.copilot_prompts_submitted == 1 assert config.stats.copilot_usage_available assert config.stats.copilot_ai_credits == 2.5 assert config.stats.copilot_metered_value_usd == 0.025 assert config.stats.model_usage["copilot:gpt-5.6-luna"]["cache_read_tokens"] == 50