|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import importlib |
| 4 | +import sys |
| 5 | +from types import ModuleType |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | + |
| 10 | +def reload_special(monkeypatch: pytest.MonkeyPatch, *, llm_off: bool) -> ModuleType: |
| 11 | + if llm_off: |
| 12 | + monkeypatch.setenv('MYCLI_LLM_OFF', '1') |
| 13 | + else: |
| 14 | + monkeypatch.delenv('MYCLI_LLM_OFF', raising=False) |
| 15 | + sys.modules.pop('mycli.packages.special', None) |
| 16 | + return importlib.import_module('mycli.packages.special') |
| 17 | + |
| 18 | + |
| 19 | +def test_special_init_exports_public_names(monkeypatch: pytest.MonkeyPatch) -> None: |
| 20 | + special = reload_special(monkeypatch, llm_off=False) |
| 21 | + |
| 22 | + for name in special.__all__: |
| 23 | + assert hasattr(special, name) |
| 24 | + |
| 25 | + |
| 26 | +def test_special_init_reexports_special_command_api(monkeypatch: pytest.MonkeyPatch) -> None: |
| 27 | + special = reload_special(monkeypatch, llm_off=False) |
| 28 | + special_main = importlib.import_module('mycli.packages.special.main') |
| 29 | + |
| 30 | + assert special.execute is special_main.execute |
| 31 | + assert special.special_command is special_main.special_command |
| 32 | + assert special.CommandNotFound is special_main.CommandNotFound |
| 33 | + |
| 34 | + |
| 35 | +def test_special_init_reexports_io_state_api(monkeypatch: pytest.MonkeyPatch) -> None: |
| 36 | + special = reload_special(monkeypatch, llm_off=False) |
| 37 | + iocommands = importlib.import_module('mycli.packages.special.iocommands') |
| 38 | + |
| 39 | + assert special.set_pager_enabled is iocommands.set_pager_enabled |
| 40 | + assert special.is_pager_enabled is iocommands.is_pager_enabled |
| 41 | + assert special.write_tee is iocommands.write_tee |
| 42 | + |
| 43 | + |
| 44 | +def test_special_init_reexports_dbcommands(monkeypatch: pytest.MonkeyPatch) -> None: |
| 45 | + special = reload_special(monkeypatch, llm_off=False) |
| 46 | + dbcommands = importlib.import_module('mycli.packages.special.dbcommands') |
| 47 | + |
| 48 | + assert special.list_databases is dbcommands.list_databases |
| 49 | + assert special.list_tables is dbcommands.list_tables |
| 50 | + assert special.status is dbcommands.status |
| 51 | + |
| 52 | + |
| 53 | +def test_special_init_uses_llm_implementation_when_enabled(monkeypatch: pytest.MonkeyPatch) -> None: |
| 54 | + special = reload_special(monkeypatch, llm_off=False) |
| 55 | + llm = importlib.import_module('mycli.packages.special.llm') |
| 56 | + |
| 57 | + assert special.FinishIteration is llm.FinishIteration |
| 58 | + assert special.is_llm_command is llm.is_llm_command |
| 59 | + assert special.handle_llm is llm.handle_llm |
| 60 | + assert special.sql_using_llm is llm.sql_using_llm |
| 61 | + |
| 62 | + |
| 63 | +def test_special_init_uses_llm_stubs_when_disabled(monkeypatch: pytest.MonkeyPatch) -> None: |
| 64 | + special = reload_special(monkeypatch, llm_off=True) |
| 65 | + |
| 66 | + assert special.is_llm_command(r'\llm prompt') is False |
| 67 | + with pytest.raises(special.FinishIteration) as handle_exc: |
| 68 | + special.handle_llm(cast_args := object()) |
| 69 | + with pytest.raises(special.FinishIteration) as sql_exc: |
| 70 | + special.sql_using_llm(cast_args) |
| 71 | + |
| 72 | + assert handle_exc.value.results is None |
| 73 | + assert sql_exc.value.results is None |
| 74 | + |
| 75 | + |
| 76 | +def test_special_init_stub_finish_iteration_stores_results(monkeypatch: pytest.MonkeyPatch) -> None: |
| 77 | + special = reload_special(monkeypatch, llm_off=True) |
| 78 | + |
| 79 | + error = special.FinishIteration(results=['done']) |
| 80 | + |
| 81 | + assert error.results == ['done'] |
0 commit comments