|
| 1 | +"""Config loader tests — pure file/env logic, no engine touched.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import logging |
| 6 | + |
| 7 | +from stackvox import config |
| 8 | +from stackvox.engine import DEFAULT_LANG, DEFAULT_SPEED, DEFAULT_VOICE |
| 9 | + |
| 10 | + |
| 11 | +class TestConfigPath: |
| 12 | + def test_stackvox_config_env_takes_priority(self, monkeypatch, tmp_path): |
| 13 | + monkeypatch.setenv("STACKVOX_CONFIG", str(tmp_path / "elsewhere.toml")) |
| 14 | + assert config.config_path() == tmp_path / "elsewhere.toml" |
| 15 | + |
| 16 | + def test_xdg_config_home_when_set(self, monkeypatch, tmp_path): |
| 17 | + monkeypatch.delenv("STACKVOX_CONFIG", raising=False) |
| 18 | + monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "xdg")) |
| 19 | + assert config.config_path() == tmp_path / "xdg" / "stackvox" / "config.toml" |
| 20 | + |
| 21 | + def test_falls_back_to_home_dotconfig(self, monkeypatch): |
| 22 | + monkeypatch.delenv("STACKVOX_CONFIG", raising=False) |
| 23 | + monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) |
| 24 | + from pathlib import Path |
| 25 | + |
| 26 | + assert config.config_path() == Path.home() / ".config" / "stackvox" / "config.toml" |
| 27 | + |
| 28 | + |
| 29 | +class TestLoadDefaults: |
| 30 | + def test_missing_file_returns_built_in_defaults(self, tmp_path): |
| 31 | + actual = config.load_defaults(tmp_path / "absent.toml") |
| 32 | + assert actual == config.Defaults() |
| 33 | + assert actual.voice == DEFAULT_VOICE |
| 34 | + assert actual.speed == DEFAULT_SPEED |
| 35 | + assert actual.lang == DEFAULT_LANG |
| 36 | + |
| 37 | + def test_full_config_overrides_all_three(self, tmp_path): |
| 38 | + path = tmp_path / "config.toml" |
| 39 | + path.write_text('[defaults]\nvoice = "bf_emma"\nspeed = 1.25\nlang = "en-gb"\n', encoding="utf-8") |
| 40 | + actual = config.load_defaults(path) |
| 41 | + assert actual.voice == "bf_emma" |
| 42 | + assert actual.speed == 1.25 |
| 43 | + assert actual.lang == "en-gb" |
| 44 | + |
| 45 | + def test_partial_config_keeps_built_in_for_missing_keys(self, tmp_path): |
| 46 | + path = tmp_path / "config.toml" |
| 47 | + path.write_text('[defaults]\nvoice = "bf_emma"\n', encoding="utf-8") |
| 48 | + actual = config.load_defaults(path) |
| 49 | + assert actual.voice == "bf_emma" |
| 50 | + # speed and lang come from the engine defaults. |
| 51 | + assert actual.speed == DEFAULT_SPEED |
| 52 | + assert actual.lang == DEFAULT_LANG |
| 53 | + |
| 54 | + def test_empty_file_returns_built_in_defaults(self, tmp_path): |
| 55 | + path = tmp_path / "config.toml" |
| 56 | + path.write_text("", encoding="utf-8") |
| 57 | + assert config.load_defaults(path) == config.Defaults() |
| 58 | + |
| 59 | + def test_malformed_toml_logs_warning_and_returns_defaults(self, tmp_path, caplog): |
| 60 | + path = tmp_path / "config.toml" |
| 61 | + path.write_text("this is not valid = toml\n[defaults\nvoice =", encoding="utf-8") |
| 62 | + with caplog.at_level(logging.WARNING, logger="stackvox.config"): |
| 63 | + actual = config.load_defaults(path) |
| 64 | + assert actual == config.Defaults() |
| 65 | + assert any("malformed stackvox config" in r.message for r in caplog.records) |
| 66 | + |
| 67 | + def test_defaults_section_must_be_a_table(self, tmp_path, caplog): |
| 68 | + """`defaults = "string"` is parseable TOML but the wrong shape — log and ignore.""" |
| 69 | + path = tmp_path / "config.toml" |
| 70 | + path.write_text('defaults = "not-a-table"\n', encoding="utf-8") |
| 71 | + with caplog.at_level(logging.WARNING, logger="stackvox.config"): |
| 72 | + actual = config.load_defaults(path) |
| 73 | + assert actual == config.Defaults() |
| 74 | + assert any("must be a table" in r.message for r in caplog.records) |
| 75 | + |
| 76 | + |
| 77 | +class TestCLIPicksUpConfig: |
| 78 | + """Smoke test: argparse defaults reflect the user's config file.""" |
| 79 | + |
| 80 | + def test_voice_default_comes_from_config(self, mocker, monkeypatch, tmp_path): |
| 81 | + path = tmp_path / "config.toml" |
| 82 | + path.write_text('[defaults]\nvoice = "bf_emma"\nspeed = 1.3\n', encoding="utf-8") |
| 83 | + monkeypatch.setenv("STACKVOX_CONFIG", str(path)) |
| 84 | + |
| 85 | + from stackvox import cli |
| 86 | + |
| 87 | + speak = mocker.patch.object(cli, "_cmd_speak", return_value=0) |
| 88 | + mocker.patch.object(cli.sys, "argv", ["stackvox", "speak", "hello"]) |
| 89 | + mocker.patch.object(cli.sys.stdin, "isatty", return_value=True) |
| 90 | + |
| 91 | + assert cli.main() == 0 |
| 92 | + args = speak.call_args.args[0] |
| 93 | + assert args.voice == "bf_emma" |
| 94 | + assert args.speed == 1.3 |
| 95 | + # Lang wasn't in config; should fall through to built-in default. |
| 96 | + assert args.lang == DEFAULT_LANG |
| 97 | + |
| 98 | + def test_explicit_flag_overrides_config(self, mocker, monkeypatch, tmp_path): |
| 99 | + path = tmp_path / "config.toml" |
| 100 | + path.write_text('[defaults]\nvoice = "bf_emma"\n', encoding="utf-8") |
| 101 | + monkeypatch.setenv("STACKVOX_CONFIG", str(path)) |
| 102 | + |
| 103 | + from stackvox import cli |
| 104 | + |
| 105 | + speak = mocker.patch.object(cli, "_cmd_speak", return_value=0) |
| 106 | + mocker.patch.object(cli.sys, "argv", ["stackvox", "speak", "--voice", "af_sarah", "hello"]) |
| 107 | + mocker.patch.object(cli.sys.stdin, "isatty", return_value=True) |
| 108 | + |
| 109 | + assert cli.main() == 0 |
| 110 | + assert speak.call_args.args[0].voice == "af_sarah" |
0 commit comments