import json from pathlib import Path from zotero_kb.cards import CardBuilder from zotero_kb.zotero_reader import ZoteroItemRecord class FakeLlmClient: def generate_card(self, source_bundle: dict[str, object]) -> dict[str, object]: return { "summary": "Merged metadata and attachment text improve drafting support.", "core_claims": [ "Project-scoped cards reduce irrelevant retrieval.", ], "methods": [ "Combines notes, metadata, and full text.", ], "evidence": [ "The merged pipeline improved citation precision.", ], "quotable_passages": [ "Project-scoped card retrieval improves citation precision during drafting.", ], "writing_hints": [ "Use when arguing for scoped retrieval during drafting.", ], "keywords": ["retrieval", "writing"], } def test_build_card_writes_markdown_and_indexes(tmp_path: Path) -> None: item = ZoteroItemRecord( item_key="PAPER0001", title="Card Pipelines for Research Writing", creators=["Alice Smith", "Bob Li"], year="2024", item_type="journalArticle", abstract="Merged metadata and notes improve drafting support.", tags=["llm", "writing"], collection_paths=[["Theory", "Drafting"]], notes=["Merged notes matter."], attachment_texts=["This paper studies card pipelines for research writing."], ) builder = CardBuilder(workspace_dir=tmp_path, llm_client=FakeLlmClient()) result = builder.build_or_update(item) expected_path = ( tmp_path / "library" / "collections" / "Theory" / "Drafting" / "Card Pipelines for Research Writing [PAPER0001].md" ) assert result.card_path == expected_path assert result.card_path.read_text(encoding="utf-8").startswith("---") cards_index = json.loads((tmp_path / "library" / "index" / "cards.json").read_text(encoding="utf-8")) assert cards_index["PAPER0001"]["title"] == "Card Pipelines for Research Writing" items_index = json.loads((tmp_path / "library" / "index" / "items.json").read_text(encoding="utf-8")) assert items_index["PAPER0001"]["card_path"] == str(expected_path) source_bundle = json.loads( (tmp_path / "library" / "cache" / "source-bundles" / "PAPER0001.json").read_text(encoding="utf-8") ) assert source_bundle["item_key"] == "PAPER0001"