|
| 1 | +"""Tests for continuous todo2code Intent-vs-Reality monitoring.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import sys |
| 7 | +import types |
| 8 | +from pathlib import Path |
| 9 | +from types import SimpleNamespace |
| 10 | + |
| 11 | +from wup.config import load_config, save_config |
| 12 | +from wup.intent_monitor import IntentAuditResult, Todo2CodeIntentMonitor |
| 13 | +from wup.models.config import IntentMonitoringConfig, ProjectConfig, WupConfig |
| 14 | +from wup.testql_watcher import TestQLWatcher |
| 15 | + |
| 16 | + |
| 17 | +def _diagnostics(tmp_path: Path, items: list[dict]) -> Path: |
| 18 | + path = tmp_path / "diagnostics.json" |
| 19 | + path.write_text(json.dumps({"diagnostics": items}), encoding="utf-8") |
| 20 | + return path |
| 21 | + |
| 22 | + |
| 23 | +def test_config_round_trip_preserves_intent_monitoring(tmp_path: Path) -> None: |
| 24 | + config = WupConfig( |
| 25 | + project=ProjectConfig(name="demo"), |
| 26 | + intent_monitoring=IntentMonitoringConfig( |
| 27 | + enabled=True, |
| 28 | + runner="python", |
| 29 | + cli_path="/opt/todo2code/dist/src/cli.js", |
| 30 | + interval_s=120, |
| 31 | + mode="require-llm", |
| 32 | + docs_llm=True, |
| 33 | + fail_codes=["PLANNED_NOT_IMPLEMENTED"], |
| 34 | + ), |
| 35 | + ) |
| 36 | + save_config(config, tmp_path / "wup.yaml") |
| 37 | + |
| 38 | + loaded = load_config(tmp_path) |
| 39 | + |
| 40 | + assert loaded.intent_monitoring.enabled is True |
| 41 | + assert loaded.intent_monitoring.runner == "python" |
| 42 | + assert loaded.intent_monitoring.interval_s == 120 |
| 43 | + assert loaded.intent_monitoring.mode == "require-llm" |
| 44 | + assert loaded.intent_monitoring.docs_llm is True |
| 45 | + assert loaded.intent_monitoring.fail_codes == ["PLANNED_NOT_IMPLEMENTED"] |
| 46 | + |
| 47 | + |
| 48 | +def test_cli_monitor_maps_blocking_diagnostics_to_down(tmp_path: Path, monkeypatch) -> None: |
| 49 | + path = _diagnostics( |
| 50 | + tmp_path, |
| 51 | + [{"severity": "blocking", "code": "PLANNED_NOT_IMPLEMENTED"}], |
| 52 | + ) |
| 53 | + calls = [] |
| 54 | + |
| 55 | + def fake_run(command, **kwargs): |
| 56 | + calls.append((command, kwargs)) |
| 57 | + payload = {"diagnosticsPath": str(path), "runDirectory": str(tmp_path / "run")} |
| 58 | + return SimpleNamespace(returncode=0, stdout=json.dumps(payload), stderr="") |
| 59 | + |
| 60 | + monkeypatch.setattr("wup.intent_monitor.subprocess.run", fake_run) |
| 61 | + results = [] |
| 62 | + config = IntentMonitoringConfig( |
| 63 | + enabled=True, |
| 64 | + command=["node", "/opt/todo2code/cli.js"], |
| 65 | + mode="deterministic", |
| 66 | + ) |
| 67 | + monitor = Todo2CodeIntentMonitor(tmp_path, config, results.append) |
| 68 | + |
| 69 | + result = monitor.run_once() |
| 70 | + |
| 71 | + assert result.status == "down" |
| 72 | + assert len(result.diagnostics) == 1 |
| 73 | + assert results == [result] |
| 74 | + command, kwargs = calls[0] |
| 75 | + assert command[:3] == ["node", "/opt/todo2code/cli.js", "pipeline"] |
| 76 | + assert "--no-docs-llm" in command |
| 77 | + assert "--no-summary-llm" in command |
| 78 | + assert kwargs.get("shell", False) is False |
| 79 | + |
| 80 | + |
| 81 | +def test_cli_monitor_resolves_single_javascript_launcher(tmp_path: Path, monkeypatch) -> None: |
| 82 | + path = _diagnostics(tmp_path, []) |
| 83 | + cli_path = tmp_path / "cli.js" |
| 84 | + cli_path.write_text("", encoding="utf-8") |
| 85 | + observed = {} |
| 86 | + |
| 87 | + monkeypatch.setattr("wup.intent_monitor.shutil.which", lambda command: str(cli_path)) |
| 88 | + |
| 89 | + def fake_run(command, **kwargs): |
| 90 | + observed["command"] = command |
| 91 | + return SimpleNamespace( |
| 92 | + returncode=0, |
| 93 | + stdout=json.dumps({"diagnosticsPath": str(path)}), |
| 94 | + stderr="", |
| 95 | + ) |
| 96 | + |
| 97 | + monkeypatch.setattr("wup.intent_monitor.subprocess.run", fake_run) |
| 98 | + monitor = Todo2CodeIntentMonitor( |
| 99 | + tmp_path, |
| 100 | + IntentMonitoringConfig(enabled=True, command=["t2c"]), |
| 101 | + lambda result: None, |
| 102 | + ) |
| 103 | + |
| 104 | + result = monitor.run_once() |
| 105 | + |
| 106 | + assert result.status == "up" |
| 107 | + assert observed["command"][:2] == ["node", str(cli_path.resolve())] |
| 108 | + |
| 109 | + |
| 110 | +def test_monitor_filters_codes_and_maps_review_required_to_degraded(tmp_path: Path) -> None: |
| 111 | + path = _diagnostics( |
| 112 | + tmp_path, |
| 113 | + [ |
| 114 | + {"severity": "review_required", "code": "CHANGELOG_WITHOUT_IMPLEMENTATION"}, |
| 115 | + {"severity": "blocking", "code": "UNRELATED"}, |
| 116 | + ], |
| 117 | + ) |
| 118 | + config = IntentMonitoringConfig( |
| 119 | + fail_codes=["CHANGELOG_WITHOUT_IMPLEMENTATION"] |
| 120 | + ) |
| 121 | + monitor = Todo2CodeIntentMonitor(tmp_path, config, lambda result: None) |
| 122 | + |
| 123 | + result = monitor._result_from_payload({"diagnosticsPath": str(path)}) |
| 124 | + |
| 125 | + assert result.status == "degraded" |
| 126 | + assert [item["code"] for item in result.diagnostics] == [ |
| 127 | + "CHANGELOG_WITHOUT_IMPLEMENTATION" |
| 128 | + ] |
| 129 | + |
| 130 | + |
| 131 | +def test_monitor_reports_runner_failure_as_down(tmp_path: Path, monkeypatch) -> None: |
| 132 | + monkeypatch.setattr( |
| 133 | + "wup.intent_monitor.subprocess.run", |
| 134 | + lambda *args, **kwargs: SimpleNamespace( |
| 135 | + returncode=2, stdout="", stderr="structured output invalid" |
| 136 | + ), |
| 137 | + ) |
| 138 | + results = [] |
| 139 | + monitor = Todo2CodeIntentMonitor( |
| 140 | + tmp_path, IntentMonitoringConfig(enabled=True), results.append |
| 141 | + ) |
| 142 | + |
| 143 | + result = monitor.run_once() |
| 144 | + |
| 145 | + assert result.status == "down" |
| 146 | + assert "structured output invalid" in result.message |
| 147 | + assert results == [result] |
| 148 | + |
| 149 | + |
| 150 | +def test_python_runner_uses_todo2code_sdk_bridge(tmp_path: Path, monkeypatch) -> None: |
| 151 | + path = _diagnostics(tmp_path, []) |
| 152 | + observed = {} |
| 153 | + |
| 154 | + class FakeRuntime: |
| 155 | + def __init__(self, root, **kwargs): |
| 156 | + observed["root"] = root |
| 157 | + observed["init"] = kwargs |
| 158 | + |
| 159 | + def invoke(self, arguments): |
| 160 | + observed["arguments"] = arguments |
| 161 | + payload = {"diagnosticsPath": str(path), "runDirectory": "python-run"} |
| 162 | + return SimpleNamespace(stdout=json.dumps(payload)) |
| 163 | + |
| 164 | + module = types.ModuleType("todo2code") |
| 165 | + module.TypeScriptRuntime = FakeRuntime |
| 166 | + monkeypatch.setitem(sys.modules, "todo2code", module) |
| 167 | + config = IntentMonitoringConfig( |
| 168 | + enabled=True, |
| 169 | + runner="python", |
| 170 | + cli_path="/opt/todo2code/dist/src/cli.js", |
| 171 | + mode="prefer-llm", |
| 172 | + docs_llm=True, |
| 173 | + ) |
| 174 | + monitor = Todo2CodeIntentMonitor(tmp_path, config, lambda result: None) |
| 175 | + |
| 176 | + result = monitor.run_once() |
| 177 | + |
| 178 | + assert result.status == "up" |
| 179 | + assert observed["root"] == tmp_path.resolve() |
| 180 | + assert observed["init"]["cli_path"] == config.cli_path |
| 181 | + assert observed["arguments"][:2] == ["pipeline", str(tmp_path.resolve())] |
| 182 | + assert observed["arguments"][observed["arguments"].index("--markdown-mode") + 1] == "prefer-llm" |
| 183 | + assert "--no-docs-llm" not in observed["arguments"] |
| 184 | + assert "--no-communication" in observed["arguments"] |
| 185 | + |
| 186 | + |
| 187 | +def test_testql_watcher_projects_intent_result_as_project_health() -> None: |
| 188 | + watcher = object.__new__(TestQLWatcher) |
| 189 | + watcher.config = SimpleNamespace(project=SimpleNamespace(name="workspace")) |
| 190 | + transitions = [] |
| 191 | + watcher._record_health_transition = lambda **values: transitions.append(values) |
| 192 | + result = IntentAuditResult( |
| 193 | + status="down", |
| 194 | + message="todo2code found 3 intent issue(s), 1 blocking", |
| 195 | + diagnostics_path="/tmp/diagnostics.json", |
| 196 | + ) |
| 197 | + |
| 198 | + watcher._record_intent_audit(result) |
| 199 | + |
| 200 | + assert transitions == [ |
| 201 | + { |
| 202 | + "service": "workspace:intent", |
| 203 | + "status": "down", |
| 204 | + "stage": "intent", |
| 205 | + "message": result.message, |
| 206 | + "track_file": result.diagnostics_path, |
| 207 | + } |
| 208 | + ] |
0 commit comments