"""The suite must not read whichever config.local.yaml this machine has. `load_config` merges the sibling overlay and `dispatcher.cfg` binds at import, so before conftest.py set ROUTER_IGNORE_LOCAL_CONFIG the whole suite inherited the deployment's settings. With `classifier.mode: local_encoder` in the overlay -- what this deployment actually runs -- nine tests failed with assertions like `assert 'local_encoder' == 'local_llm'`. Reproduced both directions on the same tree: overlay absent 1523 passed, overlay present 9 failed. The failures looked like regressions, which is the expensive part: a deployment that had configured anything could not trust its own test run. """ from __future__ import annotations import os from pathlib import Path import yaml import dispatcher from config import load_config ROOT = Path(__file__).resolve().parent.parent def test_the_env_guard_is_set_for_the_suite(): """conftest.py sets this before anything imports dispatcher.""" assert os.environ.get("ROUTER_IGNORE_LOCAL_CONFIG") def test_dispatcher_cfg_matches_config_yaml_not_the_overlay(): """The classifier mode the suite sees is the one config.yaml declares. This is the specific value that broke nine tests, so it is the one asserted rather than a generic property. """ base = yaml.safe_load((ROOT / "config" / "config.yaml").read_text()) declared = (base.get("classifier") or {}).get("mode", "local_llm") assert dispatcher.cfg.classifier.mode == declared def test_a_real_overlay_on_disk_does_not_reach_the_suite(): """If this deployment has an overlay, it is being ignored right now.""" overlay = ROOT / "config" / "config.local.yaml" if not overlay.exists(): return # nothing to prove on a clean checkout local = yaml.safe_load(overlay.read_text()) or {} overridden = (local.get("classifier") or {}).get("mode") if overridden is None: return base = yaml.safe_load((ROOT / "config" / "config.yaml").read_text()) declared = (base.get("classifier") or {}).get("mode", "local_llm") if overridden == declared: return # the overlay agrees; nothing to distinguish assert dispatcher.cfg.classifier.mode == declared, ( "the machine's config.local.yaml is leaking into the test suite" ) def test_include_overlay_true_still_merges(tmp_path): """The opt-in exists so overlay behaviour itself stays testable.""" base = yaml.safe_load((ROOT / "config" / "config.yaml").read_text()) base_path = tmp_path / "config.yaml" base_path.write_text(yaml.safe_dump(base, sort_keys=False)) (tmp_path / "config.local.yaml").write_text( yaml.safe_dump({"logging": {"level": "error"}}) ) merged = load_config(str(base_path), include_overlay=True) assert merged.logging.level == "error" def test_include_overlay_false_ignores_it(tmp_path): base = yaml.safe_load((ROOT / "config" / "config.yaml").read_text()) base_path = tmp_path / "config.yaml" base_path.write_text(yaml.safe_dump(base, sort_keys=False)) (tmp_path / "config.local.yaml").write_text( yaml.safe_dump({"logging": {"level": "error"}}) ) assert load_config(str(base_path), include_overlay=False).logging.level != "error" def test_the_default_follows_the_env_var(tmp_path, monkeypatch): """include_overlay=None consults the environment, which is how the production path keeps merging while the suite does not.""" base = yaml.safe_load((ROOT / "config" / "config.yaml").read_text()) base_path = tmp_path / "config.yaml" base_path.write_text(yaml.safe_dump(base, sort_keys=False)) (tmp_path / "config.local.yaml").write_text( yaml.safe_dump({"logging": {"level": "error"}}) ) monkeypatch.delenv("ROUTER_IGNORE_LOCAL_CONFIG", raising=False) assert load_config(str(base_path)).logging.level == "error" monkeypatch.setenv("ROUTER_IGNORE_LOCAL_CONFIG", "1") assert load_config(str(base_path)).logging.level != "error"