|
| 1 | +import importlib |
| 2 | +import sys |
| 3 | +from pathlib import Path |
| 4 | +import textwrap |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture(scope="session") |
| 10 | +def ingest_module(): |
| 11 | + project_root = Path(__file__).resolve().parents[1] |
| 12 | + config_path = project_root / "config.yaml" |
| 13 | + created = False |
| 14 | + |
| 15 | + if not config_path.exists(): |
| 16 | + config_path.write_text( |
| 17 | + textwrap.dedent( |
| 18 | + """ |
| 19 | + logseq_root: /tmp |
| 20 | + include_dirs: [] |
| 21 | + file_exts: [] |
| 22 | + exclude_globs: [] |
| 23 | + models: |
| 24 | + llm: llama3.1 |
| 25 | + embedding: nomic-embed-text |
| 26 | + storage: |
| 27 | + chroma_path: /tmp/chroma |
| 28 | + retrieval: |
| 29 | + top_k: 5 |
| 30 | + mmr: false |
| 31 | + chunk: |
| 32 | + chunk_size: 512 |
| 33 | + chunk_overlap: 50 |
| 34 | + """ |
| 35 | + ).strip() |
| 36 | + ) |
| 37 | + created = True |
| 38 | + |
| 39 | + added_to_path = False |
| 40 | + if str(project_root) not in sys.path: |
| 41 | + sys.path.insert(0, str(project_root)) |
| 42 | + added_to_path = True |
| 43 | + |
| 44 | + try: |
| 45 | + if "ingest" in sys.modules: |
| 46 | + module = sys.modules["ingest"] |
| 47 | + else: |
| 48 | + module = importlib.import_module("ingest") |
| 49 | + yield module |
| 50 | + finally: |
| 51 | + if added_to_path and str(project_root) in sys.path: |
| 52 | + sys.path.remove(str(project_root)) |
| 53 | + if created and config_path.exists(): |
| 54 | + config_path.unlink() |
| 55 | + |
| 56 | + |
| 57 | +def test_normalize_logseq_links(ingest_module): |
| 58 | + text = "Follow [[Page Name]] then see ((abc123))." |
| 59 | + result = ingest_module.normalize_logseq_links(text) |
| 60 | + assert result == "Follow Page Name then see [ref:abc123]." |
| 61 | + |
| 62 | + |
| 63 | +def test_parse_tags_combines_sources(ingest_module): |
| 64 | + text = """ |
| 65 | + #alpha introduces the topic |
| 66 | + Another line with #beta and #alpha |
| 67 | + tags:: gamma, beta , delta |
| 68 | + """ |
| 69 | + result = ingest_module.parse_tags(text) |
| 70 | + assert result == ["alpha", "beta", "delta", "gamma"] |
| 71 | + |
| 72 | + |
| 73 | +def test_page_title_from_path(ingest_module): |
| 74 | + path = "/tmp/logseq/pages/project_notes.md" |
| 75 | + assert ingest_module.page_title_from_path(path) == "project-notes" |
| 76 | + |
| 77 | + |
| 78 | +def test_collect_files_respects_ext_and_excludes(tmp_path, ingest_module): |
| 79 | + pages = tmp_path / "pages" |
| 80 | + journals = tmp_path / "journals" |
| 81 | + archive = pages / "archive" |
| 82 | + pages.mkdir() |
| 83 | + journals.mkdir() |
| 84 | + archive.mkdir() |
| 85 | + |
| 86 | + keep_pages = pages / "alpha.md" |
| 87 | + keep_journal = journals / "2025-01-01.md" |
| 88 | + ignore_ext = pages / "ignore.txt" |
| 89 | + excluded = archive / "old.md" |
| 90 | + |
| 91 | + keep_pages.write_text("alpha") |
| 92 | + keep_journal.write_text("journal") |
| 93 | + ignore_ext.write_text("nope") |
| 94 | + excluded.write_text("archive") |
| 95 | + |
| 96 | + found = ingest_module.collect_files( |
| 97 | + str(tmp_path), |
| 98 | + ["pages", "journals"], |
| 99 | + [".md"], |
| 100 | + ["pages/archive/*"], |
| 101 | + ) |
| 102 | + |
| 103 | + assert set(found) == {str(keep_pages), str(keep_journal)} |
| 104 | + |
| 105 | + |
| 106 | +def test_load_documents_applies_metadata(monkeypatch, tmp_path, ingest_module): |
| 107 | + docs_dir = tmp_path / "pages" |
| 108 | + docs_dir.mkdir() |
| 109 | + doc_path = docs_dir / "demo_page.md" |
| 110 | + doc_path.write_text( |
| 111 | + """ |
| 112 | + #alpha tag at the top |
| 113 | + tags:: beta, alpha |
| 114 | + Content referencing [[Other Page]] and ((xyz789)). |
| 115 | + """ |
| 116 | + ) |
| 117 | + |
| 118 | + class DummyDocument: |
| 119 | + def __init__(self, text, metadata): |
| 120 | + self.text = text |
| 121 | + self.metadata = metadata |
| 122 | + |
| 123 | + monkeypatch.setattr(ingest_module, "Document", DummyDocument) |
| 124 | + |
| 125 | + docs = ingest_module.load_documents([str(doc_path)]) |
| 126 | + |
| 127 | + assert len(docs) == 1 |
| 128 | + doc = docs[0] |
| 129 | + assert doc.text.strip().startswith("#alpha tag at the top") |
| 130 | + assert "[[" not in doc.text and "((" not in doc.text |
| 131 | + assert doc.metadata["source"] == str(doc_path) |
| 132 | + assert doc.metadata["title"] == "demo-page" |
| 133 | + assert doc.metadata["tags"] == "alpha, beta" |
| 134 | + assert doc.metadata["basename"] == "demo_page.md" |
| 135 | + assert doc.metadata["dir"] == "pages" |
0 commit comments