zotero-kb/tests/test_zotero_reader.py
2026-04-16 13:34:39 +08:00

75 lines
2.6 KiB
Python

from pathlib import Path
from tests.fixtures.build_zotero_fixture import build_fixture_zotero_dir
from zotero_kb.bridge import read_selected_keys
from zotero_kb.zotero_reader import ZoteroReader
def test_read_selected_items_from_fixture(tmp_path: Path) -> None:
fixture_dir = tmp_path / "zotero"
fixture_dir.mkdir()
build_fixture_zotero_dir(fixture_dir)
reader = ZoteroReader(fixture_dir)
items = reader.read_items(["PAPER0001"])
assert len(items) == 1
item = items[0]
assert item.item_key == "PAPER0001"
assert item.title == "Card Pipelines for Research Writing"
assert item.tags == ["llm", "writing"]
assert item.collection_paths == [["Theory", "Drafting"]]
assert item.notes == ["Merged notes matter."]
assert item.attachment_texts[0].startswith("This paper studies")
def test_read_selected_keys_from_bridge_snapshot(tmp_path: Path) -> None:
bridge_file = tmp_path / "selected-items.json"
bridge_file.write_text('{"selected_keys": ["PAPER0001", "PAPER0002"]}', encoding="utf-8")
assert read_selected_keys(bridge_file) == ["PAPER0001", "PAPER0002"]
def test_search_items_returns_matching_library_records(tmp_path: Path) -> None:
fixture_dir = tmp_path / "zotero"
fixture_dir.mkdir()
build_fixture_zotero_dir(fixture_dir)
reader = ZoteroReader(fixture_dir)
results = reader.search_items("research", limit=5)
assert len(results) == 1
assert results[0]["item_key"] == "PAPER0001"
assert results[0]["title"] == "Card Pipelines for Research Writing"
def test_build_collection_tree_returns_descendant_counts(tmp_path: Path) -> None:
fixture_dir = tmp_path / "zotero"
fixture_dir.mkdir()
build_fixture_zotero_dir(fixture_dir)
reader = ZoteroReader(fixture_dir)
tree = reader.get_collection_tree()
assert len(tree) == 1
root = tree[0]
assert root["collection_key"] == "COLL0001"
assert root["name"] == "Theory"
assert root["direct_item_count"] == 0
assert root["descendant_item_count"] == 1
assert root["children"][0]["collection_key"] == "COLL0002"
assert root["children"][0]["direct_item_count"] == 1
def test_get_collection_items_includes_descendants(tmp_path: Path) -> None:
fixture_dir = tmp_path / "zotero"
fixture_dir.mkdir()
build_fixture_zotero_dir(fixture_dir)
reader = ZoteroReader(fixture_dir)
items = reader.get_collection_items("COLL0001", include_descendants=True)
assert [item["item_key"] for item in items] == ["PAPER0001"]
assert items[0]["collection_paths"] == [["Theory", "Drafting"]]
assert items[0]["title"] == "Card Pipelines for Research Writing"