126 lines
4.2 KiB
Python
126 lines
4.2 KiB
Python
"""item_text is the indexer's text for one item."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from bib.item import Item, Rule
|
|
from bib.store import Store
|
|
from llm.source import item_text
|
|
|
|
DOCKET = "CMS-2019-0111"
|
|
CID = f"{DOCKET}-0042"
|
|
LONG = "We object to the E/M consolidation proposed in this rule. " * 3
|
|
|
|
|
|
def _comment(store: Store, *, abstract: str = "Inline abstract body.") -> str:
|
|
key = store.create(
|
|
Item(
|
|
item_type="report",
|
|
title="A comment",
|
|
url=f"https://www.regulations.gov/comment/{CID}",
|
|
abstract=abstract,
|
|
date_published="2019-09-27",
|
|
)
|
|
)
|
|
store.add_tag(key, "doctype:comment")
|
|
return key
|
|
|
|
|
|
def test_missing_item_is_none(tmp_path: Path):
|
|
store = Store(":memory:", storage_dir=tmp_path)
|
|
try:
|
|
assert item_text(store, "MISSING1") is None
|
|
finally:
|
|
store.close()
|
|
|
|
|
|
def test_comment_body_prefers_combined_then_abstract(tmp_path: Path):
|
|
store = Store(":memory:", storage_dir=tmp_path / "storage")
|
|
root = tmp_path / "comments"
|
|
try:
|
|
key = _comment(store)
|
|
assert "Inline abstract" in (item_text(store, key, root=root) or "")
|
|
comment_dir = root / DOCKET / CID
|
|
comment_dir.mkdir(parents=True)
|
|
(comment_dir / "combined.md").write_text(
|
|
f"---\ncomment_id: {CID}\n---\n\n{LONG}\n", encoding="utf-8"
|
|
)
|
|
text = item_text(store, key, root=root) or ""
|
|
assert "E/M consolidation" in text
|
|
assert "comment_id" not in text
|
|
(comment_dir / "combined.md").write_text(
|
|
"---\ncomment_id: x\n---\n", encoding="utf-8"
|
|
)
|
|
assert item_text(store, key, root=root) == "Inline abstract body."
|
|
finally:
|
|
store.close()
|
|
|
|
|
|
def test_comment_without_abstract_or_extract_is_empty(tmp_path: Path):
|
|
store = Store(":memory:", storage_dir=tmp_path)
|
|
try:
|
|
key = _comment(store, abstract="")
|
|
assert item_text(store, key, root=tmp_path / "empty") == ""
|
|
finally:
|
|
store.close()
|
|
|
|
|
|
def test_corpus_abstract_and_empty_item(tmp_path: Path):
|
|
store = Store(":memory:", storage_dir=tmp_path)
|
|
try:
|
|
key = store.create(
|
|
Item(
|
|
key="CORPUS01",
|
|
item_type="report",
|
|
title="A report",
|
|
abstract="Report body.",
|
|
)
|
|
)
|
|
assert item_text(store, key) == "Report body."
|
|
empty = store.create(Item(key="CORPUS02", item_type="report", title="Bare"))
|
|
assert item_text(store, empty) == ""
|
|
finally:
|
|
store.close()
|
|
|
|
|
|
def test_rule_text_comes_from_its_txt_attachment(tmp_path: Path):
|
|
store = Store(":memory:", storage_dir=tmp_path / "storage")
|
|
try:
|
|
key = store.create(Rule(key="RULEKEY1", title="A rule"))
|
|
assert item_text(store, key) == ""
|
|
src = tmp_path / "rule.txt"
|
|
src.write_text(LONG, encoding="utf-8")
|
|
store.attach_file(key, src)
|
|
assert "E/M consolidation" in (item_text(store, key) or "")
|
|
finally:
|
|
store.close()
|
|
|
|
|
|
def test_named_attachment_extract(tmp_path: Path):
|
|
store = Store(":memory:", storage_dir=tmp_path / "storage")
|
|
root = tmp_path / "comments"
|
|
try:
|
|
key = _comment(store, abstract="abstract only")
|
|
comment_dir = root / DOCKET / CID
|
|
comment_dir.mkdir(parents=True)
|
|
(comment_dir / "letter.txt").write_text(LONG, encoding="utf-8")
|
|
(comment_dir / "short.txt").write_text("too short", encoding="utf-8")
|
|
assert "E/M consolidation" in (
|
|
item_text(store, key, attachment="letter.txt", root=root) or ""
|
|
)
|
|
assert item_text(store, key, attachment="short.txt", root=root) == ""
|
|
assert item_text(store, key, attachment="missing.txt", root=root) == ""
|
|
# a bib attachment is found the same way when it is not in the comment dir
|
|
src = tmp_path / "note.txt"
|
|
src.write_text(LONG, encoding="utf-8")
|
|
other = store.create(
|
|
Item(key="CORPUS03", item_type="report", title="With file", abstract="abs")
|
|
)
|
|
store.attach_file(other, src)
|
|
assert "E/M consolidation" in (
|
|
item_text(store, other, attachment="note.txt") or ""
|
|
)
|
|
finally:
|
|
store.close()
|