73 lines
1.9 KiB
Python
73 lines
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
import scripts.common as common
|
|
import scripts.scan_zpwiki as scanner
|
|
|
|
|
|
def write_page(path: Path, text: str) -> None:
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(text, encoding="utf-8")
|
|
|
|
|
|
def test_scan_pages_creates_document_manifest(
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
root = tmp_path / "zpwiki"
|
|
pages = root / "pages"
|
|
output = tmp_path / "data" / "documents.json"
|
|
|
|
write_page(
|
|
pages / "a" / "README.md",
|
|
"""---
|
|
title: Prvý dokument
|
|
published: true
|
|
taxonomy:
|
|
category: [project]
|
|
tag: [rag]
|
|
---
|
|
# Úvod
|
|
|
|
Prvý obsah.
|
|
""",
|
|
)
|
|
write_page(
|
|
pages / "b" / "README.md",
|
|
"# Druhý dokument\n\nDruhý obsah.",
|
|
)
|
|
|
|
monkeypatch.setattr(common, "ZPWIKI_ROOT", root)
|
|
monkeypatch.setattr(common, "PAGES_ROOT", pages)
|
|
monkeypatch.setattr(scanner, "ZPWIKI_ROOT", root)
|
|
monkeypatch.setattr(scanner, "PAGES_ROOT", pages)
|
|
monkeypatch.setattr(scanner, "DOCUMENTS_FILE", output)
|
|
|
|
documents = scanner.scan_pages()
|
|
|
|
assert len(documents) == 2
|
|
assert output.exists()
|
|
|
|
stored = json.loads(output.read_text(encoding="utf-8"))
|
|
assert stored == documents
|
|
assert all("content" not in document for document in documents)
|
|
assert all("content_preview" in document for document in documents)
|
|
assert all(document["content_length"] > 0 for document in documents)
|
|
assert documents[0]["title"] == "Prvý dokument"
|
|
assert documents[1]["title"] == "Druhý dokument"
|
|
|
|
|
|
def test_scan_pages_rejects_missing_pages_directory(
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
missing = tmp_path / "missing-pages"
|
|
monkeypatch.setattr(scanner, "PAGES_ROOT", missing)
|
|
|
|
with pytest.raises(SystemExit, match="Neexistuje priečinok"):
|
|
scanner.scan_pages()
|