109 lines
3.1 KiB
Python
109 lines
3.1 KiB
Python
"""Watcher debounce and CLI smoke tests (SPEC 8, 10.1)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from mdcaldav.cli import main
|
|
from mdcaldav.config import Config
|
|
from mdcaldav.index import Index
|
|
from mdcaldav.watcher import Watcher
|
|
|
|
|
|
@pytest.fixture
|
|
def index(vault: Path, tmp_path: Path):
|
|
cfg = Config()
|
|
cfg.vault.path = vault
|
|
cfg.index.db = tmp_path / "index.db"
|
|
idx = Index(cfg)
|
|
idx.rescan()
|
|
yield idx
|
|
idx.close()
|
|
|
|
|
|
def _titles(index: Index) -> set[str]:
|
|
return {t.title for t in index.tasks.values()}
|
|
|
|
|
|
def test_watcher_picks_up_a_new_task(index: Index, vault: Path):
|
|
watcher = Watcher(index.cfg, index)
|
|
watcher.start()
|
|
try:
|
|
rel = vault / "daily/2026-01-07.md"
|
|
rel.write_text(rel.read_text() + "- [ ] Order more compost\n")
|
|
|
|
deadline = time.monotonic() + 5
|
|
while time.monotonic() < deadline:
|
|
if "Order more compost" in _titles(index):
|
|
break
|
|
time.sleep(0.05)
|
|
assert "Order more compost" in _titles(index)
|
|
finally:
|
|
watcher.stop()
|
|
|
|
|
|
def test_watcher_debounces_a_burst(index: Index, vault: Path):
|
|
"""Many rapid saves should collapse into far fewer rescans."""
|
|
watcher = Watcher(index.cfg, index)
|
|
rel = vault / "daily/2026-01-07.md"
|
|
base = index.generation
|
|
|
|
for n in range(10):
|
|
rel.write_text(rel.read_text() + f"- [ ] burst {n}\n")
|
|
watcher.touch(rel)
|
|
watcher.flush()
|
|
|
|
assert index.generation - base == 1 # one rescan, not ten
|
|
assert "burst 9" in _titles(index)
|
|
watcher.stop()
|
|
|
|
|
|
def test_watcher_ignores_paths_outside_the_vault(index: Index, tmp_path: Path):
|
|
watcher = Watcher(index.cfg, index)
|
|
watcher.touch(tmp_path / "elsewhere.md")
|
|
assert watcher._pending == set()
|
|
watcher.stop()
|
|
|
|
|
|
def test_refresh_if_stale_is_a_noop_when_unchanged(index: Index):
|
|
assert index.refresh_if_stale() is False
|
|
|
|
|
|
def test_refresh_if_stale_detects_deletion(index: Index, vault: Path):
|
|
(vault / "daily/2026-01-07.md").unlink()
|
|
assert index.refresh_if_stale() is True
|
|
assert "Book the soil test" not in _titles(index)
|
|
|
|
|
|
def test_cli_scan(vault: Path, tmp_path: Path, capsys, monkeypatch):
|
|
monkeypatch.setenv("HOME", str(tmp_path))
|
|
rc = main(["--vault", str(vault), "scan"])
|
|
out = capsys.readouterr().out
|
|
|
|
assert rc == 0
|
|
assert "Plan the seed order" in out
|
|
assert "Email the council office" in out # nested tasks are shown
|
|
assert "[#A] Book the soil test" in out # priority rendered
|
|
assert "tasks in" in out
|
|
|
|
|
|
def test_cli_doctor(vault: Path, tmp_path: Path, capsys, monkeypatch):
|
|
monkeypatch.setenv("HOME", str(tmp_path))
|
|
rc = main(["--vault", str(vault), "doctor"])
|
|
out = capsys.readouterr().out
|
|
|
|
assert rc == 0
|
|
assert "collections:" in out
|
|
assert "daily" in out
|
|
|
|
|
|
def test_cli_doctor_flags_ambiguous_titles(vault: Path, tmp_path: Path, capsys, monkeypatch):
|
|
monkeypatch.setenv("HOME", str(tmp_path))
|
|
(vault / "daily/dupes.md").write_text("- [ ] same thing\n- [ ] same thing\n")
|
|
|
|
main(["--vault", str(vault), "doctor"])
|
|
assert "ambiguous titles" in capsys.readouterr().out
|