Initial public release of pdf-tools
Command-line tools for marked-up PDFs and scanned handwritten notes: pdf-scrub, pdf-notes-md, scan-notes-md (Ollama by default, Claude on request), scan-notes-index (per-page search metadata), and scan-notes-stats. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011ZSvbNaLhgFuTm7wmkgbWs
This commit is contained in:
commit
e464871b69
29 changed files with 5296 additions and 0 deletions
291
tests/test_companion.py
Normal file
291
tests/test_companion.py
Normal file
|
|
@ -0,0 +1,291 @@
|
|||
"""Companion files: front matter from paths, and preserving existing text."""
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from pdf_tools import companion as comp
|
||||
from pdf_tools.companion import CompanionSpec, PathRule
|
||||
|
||||
ROOT = Path("/tmp/acme-test-root")
|
||||
|
||||
# A meetings layout like the one in the user's config (test data only; the
|
||||
# code knows nothing about any organization).
|
||||
SPEC = CompanionSpec(
|
||||
type="meeting-notes",
|
||||
fields=["body", "committee"],
|
||||
sections=["Attendees", "Discussion", "Decisions", "Action items"],
|
||||
transcript_section="Discussion",
|
||||
section_text={"Action items": "- [ ] Owner: task (due date)"},
|
||||
title="{body}, {date}",
|
||||
allowed={"body": ["ACME-BOD", "ACMEP-BOM", "ACMEF-BOT"]},
|
||||
acronyms={"acmep", "ceo"},
|
||||
paths=[
|
||||
PathRule("{body}/committees/{committee}/**", title="{body.short} {committee}, {date}"),
|
||||
PathRule("{body}/meetings/**", set={"committee": "board"}),
|
||||
PathRule("chair/{committee}/**", set={"body": "chair"}, title="{committee}, {date}"),
|
||||
PathRule("{body}/{committee}/**", title="{body.short} {committee}, {date}"),
|
||||
PathRule("{body}/**"),
|
||||
],
|
||||
)
|
||||
TITLES = {
|
||||
"ACME-BOD": "ACME Board of Directors", "ACME-BOD.short": "ACME",
|
||||
"ACMEP-BOM": "ACMEP Board of Managers", "ACMEP-BOM.short": "ACMEP",
|
||||
"finance-audit": "Finance and Audit Committee",
|
||||
}
|
||||
|
||||
|
||||
def meta(rel: str) -> comp.NoteMeta:
|
||||
return comp.meta_from_path(ROOT / rel, ROOT, SPEC)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("rel, date, body, committee", [
|
||||
("ACMEP-BOM/committees/finance-audit/2026-10-29-fa/2026-10-29-acmep-fa-notes.pdf",
|
||||
"2026-10-29", "ACMEP-BOM", "finance-audit"),
|
||||
("ACME-BOD/meetings/2026-11-12/2026-11-12-acme-bod-notes.pdf",
|
||||
"2026-11-12", "ACME-BOD", "board"),
|
||||
("chair/acmep-ceo-search/2026-10-30-acmep-ceo-search-notes.pdf",
|
||||
"2026-10-30", "chair", "acmep-ceo-search"),
|
||||
("ACME-BOD/committees/iac/2026-06-xx/notes.pdf",
|
||||
"2026-06-xx", "ACME-BOD", "iac"),
|
||||
("ACMEP-BOM/meetings/2026-06-08/scan.pdf",
|
||||
"2026-06-08", "ACMEP-BOM", "board"),
|
||||
])
|
||||
def test_meta_from_path(rel, date, body, committee):
|
||||
m = meta(rel)
|
||||
assert (m.date, m.fields["body"], m.fields["committee"]) == (date, body, committee)
|
||||
assert m.scan == Path(rel).name
|
||||
|
||||
|
||||
def test_filename_date_wins_over_folder():
|
||||
m = meta("ACMEP-BOM/meetings/2026-06-08/2026-06-09-acmep-notes.pdf")
|
||||
assert m.date == "2026-06-09"
|
||||
|
||||
|
||||
def test_unknown_location_warns():
|
||||
m = meta("inbox/scan.pdf")
|
||||
assert m.fields["body"] == "" and m.date == ""
|
||||
assert any("body" in w for w in m.warnings)
|
||||
assert any("date" in w for w in m.warnings)
|
||||
|
||||
|
||||
def test_partial_date_warns():
|
||||
assert any("no day" in w for w in meta("ACME-BOD/meetings/2026-06-xx/n.pdf").warnings)
|
||||
|
||||
|
||||
def test_titles():
|
||||
def t(rel, titles=TITLES):
|
||||
return comp.title_for(meta(rel), SPEC, titles)
|
||||
assert t("ACMEP-BOM/committees/finance-audit/x/2026-10-29-n.pdf") == \
|
||||
"ACMEP Finance and Audit Committee, October 29, 2026"
|
||||
assert t("ACME-BOD/meetings/2026-11-12/n.pdf") == \
|
||||
"ACME Board of Directors, November 12, 2026"
|
||||
assert t("chair/acmep-ceo-search/2026-10-30-n.pdf") == \
|
||||
"ACMEP CEO Search, October 30, 2026"
|
||||
assert t("chair/acmep-ceo-search/2026-10-30-n.pdf",
|
||||
{"acmep-ceo-search": "ACMEP CEO search"}).startswith("ACMEP CEO search,")
|
||||
assert t("ACME-BOD/meetings/x/n.pdf") == "ACME Board of Directors"
|
||||
|
||||
|
||||
def test_allowed_values_and_rule_order():
|
||||
m = meta("inbox/2026-01-02-n.pdf") # "inbox" is not an allowed body
|
||||
assert m.fields == {"body": "", "committee": ""}
|
||||
m = meta("ACME-BOD/2026-01-02-n.pdf") # matched by the last rule
|
||||
assert m.fields == {"body": "ACME-BOD", "committee": ""}
|
||||
assert any("committee" in w for w in m.warnings)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("pattern, folders, expected", [
|
||||
("a/{x}", ("a", "b"), {"x": "b"}),
|
||||
("a/{x}", ("a", "b", "c"), None),
|
||||
("a/{x}/**", ("a", "b", "c"), {"x": "b"}),
|
||||
("a/**", ("a",), {}),
|
||||
("{x}/b", ("a", "c"), None),
|
||||
])
|
||||
def test_match(pattern, folders, expected):
|
||||
assert comp._match(pattern, folders, {}) == expected
|
||||
|
||||
|
||||
def test_new_companion_matches_template():
|
||||
m = meta("ACMEP-BOM/committees/finance-audit/2026-10-29-fa/2026-10-29-acmep-fa-notes.pdf")
|
||||
text = comp.new_companion(m, ["page one", "page two"], "ollama/gpu-box/gemma4:31b",
|
||||
SPEC, TITLES)
|
||||
assert text.startswith(
|
||||
"---\ndate: 2026-10-29\nbody: ACMEP-BOM\ncommittee: finance-audit\n"
|
||||
"type: meeting-notes\nscan: 2026-10-29-acmep-fa-notes.pdf\nsubjects: []\n"
|
||||
"transcribed_by: ollama/gpu-box/gemma4:31b\n---\n")
|
||||
for h in ("## Attendees", "## Discussion", comp.DRAFT_HEADING,
|
||||
"## Decisions", "## Action items"):
|
||||
assert h in text
|
||||
assert text.index("## Discussion") < text.index(comp.DRAFT_HEADING) < text.index("## Decisions")
|
||||
assert "<!-- page 1 -->" in text and "<!-- page 2 -->" in text
|
||||
assert comp.PENDING_LINE not in text
|
||||
|
||||
|
||||
def test_transcript_keeps_layout_and_fences_headings():
|
||||
page = "# 3 items\n - indented\n\nafter blank"
|
||||
r = comp.render_transcript([page])
|
||||
assert "```text\n# 3 items\n - indented\n\nafter blank\n```" in r
|
||||
r2 = comp.render_transcript(["has ``` inside"])
|
||||
assert "````text\nhas ``` inside\n````" in r2
|
||||
|
||||
|
||||
# ---- preserving existing companion text -----------------------------------
|
||||
|
||||
EXISTING = """\
|
||||
---
|
||||
date: 2026-10-29
|
||||
body: ACMEP-BOM
|
||||
committee: finance-audit
|
||||
type: meeting-notes
|
||||
scan: 2026-10-29-acmep-fa-notes.pdf
|
||||
subjects: [ACMEP performance, audit timeline]
|
||||
---
|
||||
|
||||
# ACMEP Finance and Audit Committee, October 29, 2026
|
||||
|
||||
## Attendees
|
||||
|
||||
Pat, the committee, staff.
|
||||
|
||||
## Discussion
|
||||
|
||||
My own summary paragraph.
|
||||
With an indented line.
|
||||
|
||||
{middle}
|
||||
## Decisions
|
||||
|
||||
- Decision I wrote.
|
||||
|
||||
## Action items
|
||||
|
||||
- [ ] Owner: task (due date)
|
||||
"""
|
||||
|
||||
|
||||
def _mine(text: str) -> list[str]:
|
||||
"""Every line Pat wrote, which must survive any merge."""
|
||||
return [l for l in EXISTING.format(middle="").splitlines() if l.strip()]
|
||||
|
||||
|
||||
def assert_preserved(before: str, after: str):
|
||||
after_lines = after.splitlines()
|
||||
pos = 0
|
||||
for line in _mine(before):
|
||||
assert line in after_lines[pos:], f"lost or reordered: {line!r}"
|
||||
pos = after_lines.index(line, pos) + 1
|
||||
|
||||
|
||||
def test_pending_line_replaced():
|
||||
before = EXISTING.format(middle=comp.PENDING_LINE + "\n")
|
||||
after = comp.merge_companion(before, ["draft"], "ollama/localhost/gemma4:e4b", "Discussion")
|
||||
assert comp.PENDING_LINE not in after
|
||||
assert after.count(comp.DRAFT_HEADING) == 1
|
||||
assert_preserved(before, after)
|
||||
assert after.index("My own summary") < after.index(comp.DRAFT_HEADING) \
|
||||
< after.index("## Decisions")
|
||||
|
||||
|
||||
def test_block_added_when_missing():
|
||||
before = EXISTING.format(middle="")
|
||||
after = comp.merge_companion(before, ["draft"], "ollama/localhost/gemma4:e4b", "Discussion")
|
||||
assert after.count(comp.DRAFT_HEADING) == 1
|
||||
assert_preserved(before, after)
|
||||
assert after.index("With an indented line.") < after.index(comp.DRAFT_HEADING) \
|
||||
< after.index("## Decisions")
|
||||
assert "\n\n## Decisions" in after
|
||||
|
||||
|
||||
def test_existing_block_replaced_not_duplicated():
|
||||
before = EXISTING.format(middle="")
|
||||
once = comp.merge_companion(before, ["first draft"], "ollama/localhost/gemma4:e4b", "Discussion")
|
||||
twice = comp.merge_companion(once, ["second draft"], "ollama/gpu-box/gemma4:31b", "Discussion")
|
||||
assert twice.count(comp.DRAFT_HEADING) == 1
|
||||
assert "second draft" in twice and "first draft" not in twice
|
||||
assert_preserved(before, twice)
|
||||
assert "transcribed_by: ollama/gpu-box/gemma4:31b" in twice
|
||||
assert twice.count("transcribed_by:") == 1
|
||||
|
||||
|
||||
def test_rerun_is_stable():
|
||||
before = EXISTING.format(middle="")
|
||||
once = comp.merge_companion(before, ["same"], "x", "Discussion")
|
||||
assert comp.merge_companion(once, ["same"], "x", "Discussion") == once
|
||||
|
||||
|
||||
def test_old_block_without_end_marker_stops_at_next_heading():
|
||||
before = EXISTING.format(middle=(
|
||||
f"{comp.DRAFT_HEADING}\n\nold draft text\n\n"
|
||||
"### My corrections\n\nKeep this.\n"))
|
||||
after = comp.merge_companion(before, ["new"], "x", "Discussion")
|
||||
assert "old draft text" not in after
|
||||
assert "### My corrections\n\nKeep this." in after
|
||||
assert_preserved(before, after)
|
||||
|
||||
|
||||
def test_hash_line_inside_transcript_does_not_end_block():
|
||||
before = EXISTING.format(middle="")
|
||||
once = comp.merge_companion(before, ["# not a heading\nmore"], "x", "Discussion")
|
||||
twice = comp.merge_companion(once, ["replacement"], "x", "Discussion")
|
||||
assert "not a heading" not in twice and "more\n" not in twice.split("## Decisions")[0]
|
||||
|
||||
|
||||
def test_front_matter_only_gains_transcribed_by():
|
||||
before = EXISTING.format(middle="")
|
||||
after = comp.merge_companion(before, ["d"], "ollama/localhost/gemma4:e4b", "Discussion")
|
||||
fm_before = before.split("\n---\n", 1)[0]
|
||||
fm_after = after.split("\n---\n", 1)[0]
|
||||
assert fm_after == fm_before + "\ntranscribed_by: ollama/localhost/gemma4:e4b"
|
||||
|
||||
|
||||
def test_no_discussion_section_appends_at_end():
|
||||
before = "---\ndate: 2026-10-29\n---\n\n# Notes\n\nFree text.\n"
|
||||
after = comp.merge_companion(before, ["d"], "x")
|
||||
assert after.startswith("---\ndate: 2026-10-29\ntranscribed_by: x\n---\n\n# Notes\n\nFree text.\n\n")
|
||||
assert after.rstrip().endswith(comp.END_MARKER)
|
||||
|
||||
|
||||
def test_write_companion_creates_then_preserves(tmp_path):
|
||||
folder = tmp_path / "ACMEP-BOM" / "committees" / "finance-audit" / "2026-10-29-fa"
|
||||
folder.mkdir(parents=True)
|
||||
pdf = folder / "2026-10-29-acmep-fa-notes.pdf"
|
||||
pdf.write_bytes(b"%PDF-1.4")
|
||||
path, created, _ = comp.write_companion(pdf, tmp_path, ["one"], "x", SPEC)
|
||||
assert created and path == pdf.with_suffix(".md")
|
||||
|
||||
edited = path.read_text().replace("subjects: []", "subjects: [audit]") \
|
||||
.replace("## Attendees\n", "## Attendees\n\nSomeone.\n")
|
||||
path.write_text(edited)
|
||||
_, created, _ = comp.write_companion(pdf, tmp_path, ["two"], "y", SPEC)
|
||||
final = path.read_text()
|
||||
assert not created
|
||||
assert "subjects: [audit]" in final and "Someone." in final
|
||||
assert "two" in final and "\none\n" not in final
|
||||
assert {p.name for p in folder.iterdir()} == {pdf.name, path.name}
|
||||
|
||||
|
||||
# ---- basic layout (no companion table) -----------------------------------
|
||||
|
||||
def test_basic_layout_meta_has_no_body_or_warnings():
|
||||
m = comp.meta_from_path(ROOT / "projects" / "2026-11-02-lab-review-notes.pdf", ROOT)
|
||||
assert (m.date, m.fields, m.warnings) == ("2026-11-02", {}, [])
|
||||
|
||||
|
||||
def test_basic_layout_template():
|
||||
m = comp.meta_from_path(ROOT / "x" / "2026-11-02-lab-review-notes.pdf", ROOT)
|
||||
text = comp.new_companion(m, ["p"], "ollama/localhost/gemma4:e4b")
|
||||
assert text.startswith(
|
||||
"---\ndate: 2026-11-02\ntype: notes\nscan: 2026-11-02-lab-review-notes.pdf\n"
|
||||
"subjects: []\ntranscribed_by: ollama/localhost/gemma4:e4b\n---\n\n"
|
||||
"# Lab Review Notes, November 2, 2026\n\n## Notes\n\n" + comp.DRAFT_HEADING)
|
||||
assert "body:" not in text and "## Attendees" not in text
|
||||
|
||||
|
||||
def test_basic_layout_merge_keeps_text():
|
||||
before = ("---\ndate: 2026-11-02\n---\n\n# Notes\n\n## Notes\n\nMine.\n\n"
|
||||
"## Later\n\nAlso mine.\n")
|
||||
after = comp.merge_companion(before, ["d"], "x")
|
||||
assert after.index("Mine.") < after.index(comp.DRAFT_HEADING) < after.index("## Later")
|
||||
assert "Also mine." in after
|
||||
Loading…
Add table
Add a link
Reference in a new issue