import json from pathlib import Path import pytest from tracker import ( IncidentNotFoundError, IncidentStore, InvalidIncidentError, build_cli, ) from typer.testing import CliRunner def test_incident_store_sqlite_lifecycle(tmp_path: Path) -> None: store = IncidentStore(tmp_path / "incidents.db") store.init_schema() with pytest.raises(InvalidIncidentError): store.add(" ", "high") with pytest.raises(InvalidIncidentError): store.add("DB down", "urgent") inc1 = store.add("K8s node NotReady", "critical") inc2 = store.add("High disk usage", "medium") assert inc1.status == "open" assert len(store.list_incidents()) == 2 assert [i.id for i in store.list_incidents(severity="critical")] == [inc1.id] resolved = store.resolve(inc1.id) assert resolved.status == "resolved" assert store.stats() == {"open": 1, "resolved": 1} with pytest.raises(IncidentNotFoundError): store.resolve(9999) assert inc2.id != inc1.id def test_incident_cli_end_to_end(tmp_path: Path) -> None: db_path = tmp_path / "cli.db" app = build_cli(db_path) runner = CliRunner() res_add = runner.invoke(app, ["add", "API 502 spike", "--severity", "high"]) assert res_add.exit_code == 0 created = json.loads(res_add.stdout) assert created["severity"] == "high" res_stats = runner.invoke(app, ["stats"]) assert res_stats.exit_code == 0 assert json.loads(res_stats.stdout) == {"open": 1, "resolved": 0}