import pytest from pydantic import ValidationError from estimator.config.settings import Settings SECRETS = { "EST_DATABASE__DSN": "postgresql://u:secretpass@localhost/db", "EST_LLM__ANTHROPIC__API_KEY": "sk-secret-key", } def _env(monkeypatch, **extra): for k, v in {**SECRETS, **extra}.items(): monkeypatch.setenv(k, v) def test_loads_repo_default_toml(monkeypatch): _env(monkeypatch) s = Settings() assert s.pricing.pm_pct == 0.15 assert s.pricing.min_rounding.step == 1000 assert s.llm.steps.critic.provider == "anthropic" assert s.llm.steps.critic.model == "claude-sonnet-5-5" assert s.llm.steps.critic.temperature is None assert s.market.active_version == "2026Q4" def test_env_overrides_toml(monkeypatch): _env(monkeypatch, EST_PRICING__MARKUP_PCT="0.35", EST_LLM__STEPS__CRITIC__MAX_TOKENS="999") s = Settings() assert s.pricing.markup_pct == 0.35 assert s.llm.steps.critic.max_tokens == 999 assert s.llm.steps.estimate.max_tokens == 8000 # sibling keeps the TOML value assert s.pricing.pm_pct == 0.15 # untouched sibling keeps the TOML value def test_env_specific_toml_applies(monkeypatch): _env(monkeypatch, EST_ENV="dev") assert Settings().app.log_level == "DEBUG" def test_missing_env_toml_is_ignored(monkeypatch): _env(monkeypatch, EST_ENV="nonexistent") assert Settings().app.log_level == "INFO" def test_secrets_required(): with pytest.raises(ValidationError): Settings() def test_secrets_not_in_repr(monkeypatch): _env(monkeypatch) text = repr(Settings()) assert "sk-secret-key" not in text assert "secretpass" not in text def test_unknown_key_in_toml_fails(monkeypatch, tmp_path): (tmp_path / "default.toml").write_text("[pricing]\npm_pcnt = 0.2\n") _env(monkeypatch, EST_CONFIG_DIR=str(tmp_path)) with pytest.raises(ValidationError): Settings() def test_api_key_map(monkeypatch): _env(monkeypatch, EST_API__KEYS="client_a:key1,client_b:key2") assert Settings().api.key_map == {"key1": "client_a", "key2": "client_b"} def test_step_can_switch_provider_via_env(monkeypatch): _env( monkeypatch, EST_LLM__STEPS__CRITIC__PROVIDER="openai", EST_LLM__STEPS__CRITIC__MODEL="gpt-test", EST_LLM__OPENAI__API_KEY="sk-openai-secret", ) s = Settings() assert s.llm.steps.critic.provider == "openai" assert s.llm.steps.critic.model == "gpt-test" assert s.llm.steps.decompose.provider == "anthropic" assert "sk-openai-secret" not in repr(s) def test_route_to_provider_without_key_fails(monkeypatch): _env(monkeypatch, EST_LLM__STEPS__CRITIC__PROVIDER="openai") with pytest.raises(ValidationError, match="openai"): Settings() def test_fallback_provider_also_needs_key(monkeypatch): _env( monkeypatch, EST_LLM__STEPS__CRITIC__FALLBACK__PROVIDER="openai", EST_LLM__STEPS__CRITIC__FALLBACK__MODEL="gpt-test", ) with pytest.raises(ValidationError, match="critic.fallback"): Settings() def test_unused_provider_needs_no_key(monkeypatch): _env(monkeypatch) # only the Anthropic key is set, every step routes to Anthropic assert Settings().llm.openai.api_key is None def test_no_keys_at_all_fails(monkeypatch): monkeypatch.setenv("EST_DATABASE__DSN", "sqlite:///:memory:") with pytest.raises(ValidationError, match="anthropic"): Settings() def test_pricing_defaults_follow_the_agreed_reserve_policy(monkeypatch): _env(monkeypatch) p = Settings().pricing assert (p.pm_pct, p.markup_pct, p.reserve_floor_pct, p.reserve_z) == (0.15, 0.20, 0.10, 1.04) assert p.default_engagement == "fixed_price" def test_old_pricing_keys_are_rejected_not_silently_ignored(monkeypatch, tmp_path): for old in ("margin_pct", "risk_pct", "confidence_z"): (tmp_path / "default.toml").write_text(f"[pricing]\n{old} = 0.2\n") _env(monkeypatch, EST_CONFIG_DIR=str(tmp_path)) with pytest.raises(ValidationError): Settings() @pytest.mark.parametrize("key", ["PM_PCT", "MARKUP_PCT", "RESERVE_FLOOR_PCT", "RESERVE_Z"]) def test_negative_pricing_parameters_are_rejected(monkeypatch, key): _env(monkeypatch, **{f"EST_PRICING__{key}": "-0.1"}) with pytest.raises(ValidationError): Settings() def test_default_engagement_is_validated_and_switchable(monkeypatch): _env(monkeypatch, EST_PRICING__DEFAULT_ENGAGEMENT="time_and_materials") assert Settings().pricing.default_engagement == "time_and_materials" _env(monkeypatch, EST_PRICING__DEFAULT_ENGAGEMENT="hourly") with pytest.raises(ValidationError): Settings() def test_vat_defaults_follow_the_client_region(monkeypatch): _env(monkeypatch) vat = Settings().pricing.vat assert vat.default_rates == {"ru": 0.22, "eu": 0.0} assert vat.choices == [0.22, 0.10, 0.07, 0.05] assert vat.allowed() == {0.0, 0.22, 0.10, 0.07, 0.05} def test_vat_rates_are_validated(monkeypatch): _env(monkeypatch, EST_PRICING__VAT__DEFAULT_RATES='{"ru": 1.5}') with pytest.raises(ValidationError): Settings() _env(monkeypatch, EST_PRICING__VAT__DEFAULT_RATES='{"ru": 0.2}', EST_PRICING__VAT__CHOICES="[0]") with pytest.raises(ValidationError): Settings() def test_vat_can_be_reconfigured_for_another_regime(monkeypatch): _env(monkeypatch, EST_PRICING__VAT__DEFAULT_RATES='{"ru": 0.0, "eu": 0.0}') assert Settings().pricing.vat.default_rates["ru"] == 0.0