forked from Rockachopa/Timmy-time-dashboard
Analyze test coverage (75.3% → 85.4%) and add functional test suites for the major gaps identified: - test_agent_core.py: Full coverage for agent_core/interface.py (0→100%) and agent_core/ollama_adapter.py (0→100%) — data classes, factories, abstract enforcement, perceive/reason/act/recall workflow, effect logging - test_docker_runner.py: Full coverage for swarm/docker_runner.py (0→100%) — container spawn/stop/list lifecycle with mocked subprocess - test_timmy_tools.py: Tool usage tracking, persona toolkit mapping, catalog generation, graceful degradation without Agno - test_routes_tools.py: /tools page, API stats endpoint, and WebSocket /swarm/live connect/disconnect/send lifecycle (41→82%) - test_voice_tts_functional.py: VoiceTTS init, speak, volume clamping, voice listing, graceful degradation (41→94%) - test_watchdog_functional.py: _run_tests, watch loop state transitions, regression detection, KeyboardInterrupt (47→97%) - test_lnd_backend.py: LND init from params/env, grpc stub enforcement, method-level BackendNotAvailableError, settle returns False (25→61%) - test_swarm_routes_functional.py: Agent spawn/stop, task CRUD, auction, insights, UI partials, error paths (63→92%) https://claude.ai/code/session_01WU4h3cQQiouMwmgYmAgkMM
101 lines
3.4 KiB
Python
101 lines
3.4 KiB
Python
"""Functional tests for self_tdd.watchdog — continuous test runner.
|
|
|
|
All subprocess calls are mocked to avoid running real pytest.
|
|
"""
|
|
|
|
from unittest.mock import patch, MagicMock, call
|
|
|
|
import pytest
|
|
|
|
from self_tdd.watchdog import _run_tests, watch
|
|
|
|
|
|
class TestRunTests:
|
|
@patch("self_tdd.watchdog.subprocess.run")
|
|
def test_run_tests_passing(self, mock_run):
|
|
mock_run.return_value = MagicMock(
|
|
returncode=0,
|
|
stdout="5 passed\n",
|
|
stderr="",
|
|
)
|
|
passed, output = _run_tests()
|
|
assert passed is True
|
|
assert "5 passed" in output
|
|
|
|
@patch("self_tdd.watchdog.subprocess.run")
|
|
def test_run_tests_failing(self, mock_run):
|
|
mock_run.return_value = MagicMock(
|
|
returncode=1,
|
|
stdout="2 failed, 3 passed\n",
|
|
stderr="ERRORS",
|
|
)
|
|
passed, output = _run_tests()
|
|
assert passed is False
|
|
assert "2 failed" in output
|
|
assert "ERRORS" in output
|
|
|
|
@patch("self_tdd.watchdog.subprocess.run")
|
|
def test_run_tests_command_format(self, mock_run):
|
|
mock_run.return_value = MagicMock(returncode=0, stdout="", stderr="")
|
|
_run_tests()
|
|
cmd = mock_run.call_args[0][0]
|
|
assert "pytest" in " ".join(cmd)
|
|
assert "tests/" in cmd
|
|
assert "-q" in cmd
|
|
assert "--tb=short" in cmd
|
|
assert mock_run.call_args[1]["capture_output"] is True
|
|
assert mock_run.call_args[1]["text"] is True
|
|
|
|
|
|
class TestWatch:
|
|
@patch("self_tdd.watchdog.time.sleep")
|
|
@patch("self_tdd.watchdog._run_tests")
|
|
@patch("self_tdd.watchdog.typer")
|
|
def test_watch_first_pass(self, mock_typer, mock_tests, mock_sleep):
|
|
"""First iteration: None→passing → should print green message."""
|
|
call_count = 0
|
|
|
|
def side_effect():
|
|
nonlocal call_count
|
|
call_count += 1
|
|
if call_count >= 2:
|
|
raise KeyboardInterrupt
|
|
return (True, "all good")
|
|
|
|
mock_tests.side_effect = side_effect
|
|
watch(interval=10)
|
|
# Should have printed green "All tests passing" message
|
|
mock_typer.secho.assert_called()
|
|
|
|
@patch("self_tdd.watchdog.time.sleep")
|
|
@patch("self_tdd.watchdog._run_tests")
|
|
@patch("self_tdd.watchdog.typer")
|
|
def test_watch_regression(self, mock_typer, mock_tests, mock_sleep):
|
|
"""Regression: passing→failing → should print red message + output."""
|
|
results = [(True, "ok"), (False, "FAILED: test_foo"), KeyboardInterrupt]
|
|
idx = 0
|
|
|
|
def side_effect():
|
|
nonlocal idx
|
|
if idx >= len(results):
|
|
raise KeyboardInterrupt
|
|
r = results[idx]
|
|
idx += 1
|
|
if isinstance(r, type) and issubclass(r, BaseException):
|
|
raise r()
|
|
return r
|
|
|
|
mock_tests.side_effect = side_effect
|
|
watch(interval=5)
|
|
# Should have printed red "Regression detected" at some point
|
|
secho_calls = [str(c) for c in mock_typer.secho.call_args_list]
|
|
assert any("Regression" in c for c in secho_calls) or any("RED" in c for c in secho_calls)
|
|
|
|
@patch("self_tdd.watchdog.time.sleep")
|
|
@patch("self_tdd.watchdog._run_tests")
|
|
@patch("self_tdd.watchdog.typer")
|
|
def test_watch_keyboard_interrupt(self, mock_typer, mock_tests, mock_sleep):
|
|
mock_tests.side_effect = KeyboardInterrupt
|
|
watch(interval=60)
|
|
mock_typer.echo.assert_called() # "Watchdog stopped"
|