|
1
|
"""Tests for CLI UX improvements — doctor, init wizard, and tab completion.""" |
|
2
|
|
|
3
|
import os |
|
4
|
from unittest.mock import MagicMock, patch |
|
5
|
|
|
6
|
from click.testing import CliRunner |
|
7
|
|
|
8
|
from video_processor.cli.commands import cli |
|
9
|
from video_processor.cli.companion import CompanionREPL |
|
10
|
from video_processor.cli.doctor import ( |
|
11
|
check_api_keys, |
|
12
|
check_dotenv, |
|
13
|
check_ffmpeg, |
|
14
|
check_optional_deps, |
|
15
|
check_python_version, |
|
16
|
format_results, |
|
17
|
run_all_checks, |
|
18
|
) |
|
19
|
|
|
20
|
|
|
21
|
class TestDoctor: |
|
22
|
def test_check_python_version(self): |
|
23
|
name, status, detail = check_python_version() |
|
24
|
assert name == "Python" |
|
25
|
assert status == "ok" |
|
26
|
|
|
27
|
def test_check_ffmpeg_found(self): |
|
28
|
with patch("video_processor.cli.doctor.shutil") as mock_shutil: |
|
29
|
mock_shutil.which.return_value = "/usr/bin/ffmpeg" |
|
30
|
name, status, detail = check_ffmpeg() |
|
31
|
assert status == "ok" |
|
32
|
|
|
33
|
def test_check_ffmpeg_missing(self): |
|
34
|
with patch("video_processor.cli.doctor.shutil") as mock_shutil: |
|
35
|
mock_shutil.which.return_value = None |
|
36
|
name, status, detail = check_ffmpeg() |
|
37
|
assert status == "missing" |
|
38
|
|
|
39
|
def test_check_api_keys_with_key(self): |
|
40
|
with patch.dict(os.environ, {"OPENAI_API_KEY": "sk-test1234567890"}): |
|
41
|
results = check_api_keys() |
|
42
|
openai = [r for r in results if r[0].strip() == "OpenAI"] |
|
43
|
assert len(openai) == 1 |
|
44
|
assert openai[0][1] == "ok" |
|
45
|
assert "sk-t" in openai[0][2] |
|
46
|
|
|
47
|
def test_check_api_keys_without_key(self): |
|
48
|
with patch.dict(os.environ, {}, clear=True): |
|
49
|
results = check_api_keys() |
|
50
|
openai = [r for r in results if "OpenAI" in r[0]] |
|
51
|
assert openai[0][1] == "not set" |
|
52
|
|
|
53
|
def test_check_dotenv_exists(self, tmp_path, monkeypatch): |
|
54
|
monkeypatch.chdir(tmp_path) |
|
55
|
(tmp_path / ".env").write_text("KEY=val\n") |
|
56
|
name, status, detail = check_dotenv() |
|
57
|
assert status == "ok" |
|
58
|
|
|
59
|
def test_check_dotenv_missing(self, tmp_path, monkeypatch): |
|
60
|
monkeypatch.chdir(tmp_path) |
|
61
|
name, status, detail = check_dotenv() |
|
62
|
assert status == "not found" |
|
63
|
|
|
64
|
def test_check_optional_deps(self): |
|
65
|
results = check_optional_deps() |
|
66
|
assert len(results) > 0 |
|
67
|
# All results should have 3 elements |
|
68
|
for name, status, detail in results: |
|
69
|
assert status in ("ok", "not installed") |
|
70
|
|
|
71
|
def test_format_results(self): |
|
72
|
results = [ |
|
73
|
("Python", "ok", "3.12.0"), |
|
74
|
("FFmpeg", "missing", "Install it"), |
|
75
|
] |
|
76
|
output = format_results(results) |
|
77
|
assert "PlanOpticon Doctor" in output |
|
78
|
assert "[ok]" in output |
|
79
|
assert "[XX]" in output |
|
80
|
|
|
81
|
def test_run_all_checks(self): |
|
82
|
with patch( |
|
83
|
"video_processor.integrators.graph_discovery.find_nearest_graph", |
|
84
|
return_value=None, |
|
85
|
): |
|
86
|
results = run_all_checks() |
|
87
|
assert len(results) > 5 |
|
88
|
# Should have section headers |
|
89
|
sections = [r for r in results if r[1] == "section"] |
|
90
|
assert len(sections) >= 2 |
|
91
|
|
|
92
|
def test_doctor_cli_command(self): |
|
93
|
runner = CliRunner() |
|
94
|
with patch( |
|
95
|
"video_processor.integrators.graph_discovery.find_nearest_graph", |
|
96
|
return_value=None, |
|
97
|
): |
|
98
|
result = runner.invoke(cli, ["doctor"]) |
|
99
|
assert result.exit_code == 0 |
|
100
|
assert "PlanOpticon Doctor" in result.output |
|
101
|
|
|
102
|
|
|
103
|
class TestInitWizard: |
|
104
|
def test_init_cli_command_exists(self): |
|
105
|
runner = CliRunner() |
|
106
|
result = runner.invoke(cli, ["init", "--help"]) |
|
107
|
assert result.exit_code == 0 |
|
108
|
assert "setup wizard" in result.output.lower() or "wizard" in result.output.lower() |
|
109
|
|
|
110
|
def test_wizard_provider_selection(self): |
|
111
|
"""Test the wizard runs with simulated input.""" |
|
112
|
runner = CliRunner() |
|
113
|
# Select provider 1 (OpenAI), enter a key, decline additional providers |
|
114
|
result = runner.invoke( |
|
115
|
cli, |
|
116
|
["init"], |
|
117
|
input="1\nsk-test-key-1234567890\nn\n", |
|
118
|
) |
|
119
|
assert result.exit_code == 0 |
|
120
|
assert "Setup complete" in result.output |
|
121
|
|
|
122
|
def test_wizard_ollama_provider(self): |
|
123
|
"""Test selecting Ollama (no API key needed).""" |
|
124
|
runner = CliRunner() |
|
125
|
with patch( |
|
126
|
"video_processor.cli.init_wizard.shutil.which", |
|
127
|
return_value="/usr/local/bin/ollama", |
|
128
|
): |
|
129
|
with patch("subprocess.run") as mock_run: |
|
130
|
mock_run.return_value = MagicMock(returncode=0, stdout="NAME\nllama3\n") |
|
131
|
result = runner.invoke( |
|
132
|
cli, |
|
133
|
["init"], |
|
134
|
input="4\nn\n", |
|
135
|
) |
|
136
|
assert result.exit_code == 0 |
|
137
|
assert "Setup complete" in result.output |
|
138
|
|
|
139
|
|
|
140
|
class TestCompanionTabCompletion: |
|
141
|
def test_commands_list_exists(self): |
|
142
|
assert len(CompanionREPL.COMMANDS) > 10 |
|
143
|
assert "/help" in CompanionREPL.COMMANDS |
|
144
|
assert "/quit" in CompanionREPL.COMMANDS |
|
145
|
|
|
146
|
def test_setup_readline_no_crash(self): |
|
147
|
"""Readline setup should not crash even if readline is unavailable.""" |
|
148
|
repl = CompanionREPL() |
|
149
|
# Just ensure it doesn't raise |
|
150
|
repl._setup_readline() |
|
151
|
|
|
152
|
def test_all_commands_in_dispatch(self): |
|
153
|
"""Every command in COMMANDS should be handled by handle_input.""" |
|
154
|
repl = CompanionREPL() |
|
155
|
for cmd in CompanionREPL.COMMANDS: |
|
156
|
if cmd in ("/quit", "/exit"): |
|
157
|
result = repl.handle_input(cmd) |
|
158
|
assert result == "__QUIT__" |
|
159
|
else: |
|
160
|
result = repl.handle_input(cmd) |
|
161
|
assert "Unknown command" not in result, f"{cmd} not handled" |
|
162
|
|