99 lines
2.8 KiB
Python
99 lines
2.8 KiB
Python
"""Tests for note_capture.py — dumb capture into notes/inbox/ + audit log."""
|
|
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "scripts"))
|
|
import note_capture # noqa: E402
|
|
|
|
FILENAME_RE = re.compile(r"^\d{4}-\d{2}-\d{2}_\d{2}_\d{2}_\d{2}_\d{6}-[a-z0-9-]+\.md$")
|
|
|
|
|
|
@pytest.fixture
|
|
def workspace(tmp_path, monkeypatch):
|
|
inbox = tmp_path / "notes" / "inbox"
|
|
log = tmp_path / "log" / "note.log"
|
|
monkeypatch.setattr(note_capture, "INBOX", inbox)
|
|
monkeypatch.setattr(note_capture, "LOG", log)
|
|
return tmp_path
|
|
|
|
|
|
def _run(text=None, argv_extra=None):
|
|
argv = ["note_capture.py"]
|
|
if text is not None:
|
|
argv += ["--text", text]
|
|
if argv_extra:
|
|
argv += argv_extra
|
|
return argv
|
|
|
|
|
|
def test_slugify_from_words():
|
|
assert note_capture._slugify("Mazání starých images") == "mazani-starych-images"
|
|
|
|
|
|
def test_slugify_caps_word_count():
|
|
assert note_capture._slugify("one two three four five six") == "one-two-three-four"
|
|
|
|
|
|
def test_slugify_bare_url_uses_domain():
|
|
assert (
|
|
note_capture._slugify("https://www.darkove-sklo.com/x/y") == "darkove-sklo-com"
|
|
)
|
|
|
|
|
|
def test_slugify_empty_falls_back():
|
|
assert note_capture._slugify("!!!") == "note"
|
|
|
|
|
|
def test_capture_writes_inbox_file_and_log(workspace, monkeypatch):
|
|
monkeypatch.setattr(
|
|
sys,
|
|
"argv",
|
|
_run("koupit kanistr na vodu", ["--channel", "telegram", "--chat-id", "42"]),
|
|
)
|
|
assert note_capture.main() == 0
|
|
|
|
files = list((workspace / "notes" / "inbox").glob("*.md"))
|
|
assert len(files) == 1
|
|
name = files[0].name
|
|
assert FILENAME_RE.match(name), name
|
|
|
|
content = files[0].read_text(encoding="utf-8")
|
|
assert content.startswith("---\n")
|
|
assert "channel: telegram" in content
|
|
assert 'chat_id: "42"' in content
|
|
assert "koupit kanistr na vodu" in content
|
|
|
|
log_lines = (
|
|
(workspace / "log" / "note.log").read_text(encoding="utf-8").splitlines()
|
|
)
|
|
assert len(log_lines) == 1
|
|
assert "CAPTURE" in log_lines[0]
|
|
assert name in log_lines[0]
|
|
|
|
|
|
def test_capture_no_leftover_tmp_files(workspace, monkeypatch):
|
|
monkeypatch.setattr(sys, "argv", _run("neco"))
|
|
assert note_capture.main() == 0
|
|
tmp_files = list((workspace / "notes" / "inbox").glob(".*"))
|
|
assert tmp_files == []
|
|
|
|
|
|
def test_capture_omits_absent_provenance(workspace, monkeypatch):
|
|
monkeypatch.setattr(sys, "argv", _run("bez kanalu"))
|
|
assert note_capture.main() == 0
|
|
content = next((workspace / "notes" / "inbox").glob("*.md")).read_text(
|
|
encoding="utf-8"
|
|
)
|
|
assert "channel:" not in content
|
|
assert "chat_id:" not in content
|
|
|
|
|
|
def test_capture_empty_input_fails(workspace, monkeypatch):
|
|
monkeypatch.setattr(sys, "argv", _run(" "))
|
|
assert note_capture.main() == 1
|
|
assert list((workspace / "notes" / "inbox").glob("*.md")) == []
|