diff options
| author | Craig Jennings <c@cjennings.net> | 2026-05-30 13:27:29 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-05-30 13:27:29 -0500 |
| commit | a6313954fc297ee4a6c1c42ba903730a364cd5df (patch) | |
| tree | f55cc085d966684253c6e7daaeee27593ca08801 /.ai/scripts/tests/test_cross_project_broadcast.py | |
| parent | 0234e52b727b34ade93961eb05b5638685f4406f (diff) | |
| download | rulesets-a6313954fc297ee4a6c1c42ba903730a364cd5df.tar.gz rulesets-a6313954fc297ee4a6c1c42ba903730a364cd5df.zip | |
test(scripts): cover drill-to-anki internals, broadcast, and daily-prep
I backfilled the gaps left after the flashcard work landed. drill-to-anki.py had tests only for its two default helpers. I added coverage for the core parser and its pieces: parse (section-to-tag mapping, drawer-only body, blank trimming, multiline join, no-card input), strip_org_metadata (drawer and planning-line stripping, unclosed drawer), section_to_tag, escape_html, and the deterministic stable_id. I also filled the remaining drill-deck-stats / drill-deck-diff-ids branches (missing-title and PROPERTIES-mismatch warnings, the appeared-IDs note path).
I added test_cross_project_broadcast.py for the two scripts that had none here: is_broadcastable / discover (SEARCH_ROOTS pointed at a tmp tree) / sender_project / inbox_send_path, plus an ERT suite for daily-prep-agenda.el (dp-iso-date, dp-bucket with the clock pinned, dp-format-entry, and dp-collect end to end on a temp org file).
daily-prep-agenda.el needed one change to be loadable under ERT: its batch entrypoint fired on any load. I gated it behind dp--cli-invocation-p, the same readable-files check lint-org.el already uses, so requiring the file for tests no longer runs the extractor. A real invocation with a file argument still fires. A no-argument run now no-ops instead of printing an empty header.
Diffstat (limited to '.ai/scripts/tests/test_cross_project_broadcast.py')
| -rw-r--r-- | .ai/scripts/tests/test_cross_project_broadcast.py | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/.ai/scripts/tests/test_cross_project_broadcast.py b/.ai/scripts/tests/test_cross_project_broadcast.py new file mode 100644 index 0000000..5919fbf --- /dev/null +++ b/.ai/scripts/tests/test_cross_project_broadcast.py @@ -0,0 +1,116 @@ +"""Tests for cross-project-broadcast.py: project fingerprinting + discovery. + +Plain python3 script. The pure-ish helpers are driven against tmp project +trees; discovery is exercised with SEARCH_ROOTS monkeypatched to the tree, and +the cwd-based helpers with monkeypatch.chdir. +""" +from __future__ import annotations + +import importlib.util +from pathlib import Path + +import pytest + +SCRIPT = Path(__file__).resolve().parents[1] / "cross-project-broadcast.py" + + +@pytest.fixture(scope="module") +def bcast(): + spec = importlib.util.spec_from_file_location("cross_project_broadcast", SCRIPT) + assert spec and spec.loader + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def _make_project(root: Path, name: str, with_inbox: bool = True, + with_protocols: bool = True) -> Path: + p = root / name + (p / ".ai").mkdir(parents=True) + if with_protocols: + (p / ".ai" / "protocols.org").write_text("#+TITLE: protocols\n") + if with_inbox: + (p / "inbox").mkdir() + return p + + +# --- is_broadcastable --- + +def test_is_broadcastable_true_with_protocols_and_inbox(bcast, tmp_path): + assert bcast.is_broadcastable(_make_project(tmp_path, "proj")) is True + + +def test_is_broadcastable_false_without_inbox(bcast, tmp_path): + p = _make_project(tmp_path, "proj", with_inbox=False) + assert bcast.is_broadcastable(p) is False + + +def test_is_broadcastable_false_without_protocols(bcast, tmp_path): + p = _make_project(tmp_path, "proj", with_protocols=False) + assert bcast.is_broadcastable(p) is False + + +def test_is_broadcastable_false_on_plain_dir(bcast, tmp_path): + assert bcast.is_broadcastable(tmp_path) is False + + +# --- discover (SEARCH_ROOTS monkeypatched onto the tmp tree) --- + +def test_discover_finds_broadcastable_subprojects(bcast, tmp_path, monkeypatch): + root = tmp_path / "code" + root.mkdir() + _make_project(root, "alpha") + _make_project(root, "beta") + _make_project(root, "no-inbox", with_inbox=False) # not broadcastable + monkeypatch.setattr(bcast, "SEARCH_ROOTS", [root]) + assert [p.name for p in bcast.discover()] == ["alpha", "beta"] + + +def test_discover_handles_root_that_is_itself_a_project(bcast, tmp_path, monkeypatch): + root = _make_project(tmp_path, ".emacs.d") + monkeypatch.setattr(bcast, "SEARCH_ROOTS", [root]) + assert [p.name for p in bcast.discover()] == [".emacs.d"] + + +def test_discover_dedups_by_basename_across_roots(bcast, tmp_path, monkeypatch): + root1 = tmp_path / "code" + root1.mkdir() + root2 = tmp_path / "projects" + root2.mkdir() + _make_project(root1, "dup") + _make_project(root2, "dup") + monkeypatch.setattr(bcast, "SEARCH_ROOTS", [root1, root2]) + assert [p.name for p in bcast.discover()] == ["dup"] + + +def test_discover_skips_missing_roots(bcast, tmp_path, monkeypatch): + monkeypatch.setattr(bcast, "SEARCH_ROOTS", [tmp_path / "does-not-exist"]) + assert bcast.discover() == [] + + +# --- sender_project / inbox_send_path (cwd-based) --- + +def test_sender_project_returns_basename_inside_an_ai_project(bcast, tmp_path, monkeypatch): + p = _make_project(tmp_path, "myproj") + monkeypatch.chdir(p) + assert bcast.sender_project() == "myproj" + + +def test_sender_project_none_outside_an_ai_project(bcast, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + assert bcast.sender_project() is None + + +def test_inbox_send_path_found_in_project(bcast, tmp_path, monkeypatch): + p = _make_project(tmp_path, "myproj") + (p / ".ai" / "scripts").mkdir() + helper = p / ".ai" / "scripts" / "inbox-send.py" + helper.write_text("# stub\n") + monkeypatch.chdir(p) + assert bcast.inbox_send_path() == helper + + +def test_inbox_send_path_raises_when_missing(bcast, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + with pytest.raises(SystemExit): + bcast.inbox_send_path() |
