Files

311 lines
11 KiB
Python

"""Tests for the wiki module."""
from __future__ import annotations
import json
import tempfile
from pathlib import Path
import git
import pytest
from fiona.wiki.ignore import (
gitignore_patterns_from_text,
path_is_ignored,
repository_gitignore_text,
)
from fiona.wiki.wiki import (
copy_directory_tree,
get_ignore_patterns,
get_llm_wiki_source_path,
wiki_init,
)
def _relative_files(root: Path) -> set[Path]:
"""Collect all file paths under root, relative to root."""
return {p.relative_to(root) for p in root.rglob("*") if p.is_file()}
def test_wiki_init_is_1_to_1_copy_with_source():
"""Test wiki_init copies every template file, skipping nothing.
Also verifies that git repository is initialized with .gitignore
excluding personal/ directory.
"""
with tempfile.TemporaryDirectory() as tmpdir:
wiki_init(tmpdir)
template_files = _relative_files(get_llm_wiki_source_path())
copied_files = _relative_files(Path(tmpdir))
# Gitignored template artifacts are not part of the copy.
patterns = gitignore_patterns_from_text(repository_gitignore_text())
gitignored = {
path for path in template_files
if path_is_ignored(path.as_posix(), patterns)
}
assert (template_files - gitignored).issubset(copied_files)
assert gitignored.isdisjoint(copied_files)
# Git-related files should also be present
gitignore_path = Path(tmpdir) / ".gitignore"
git_dir = Path(tmpdir) / ".git"
assert gitignore_path.exists(), ".gitignore should be created"
assert git_dir.exists(), ".git directory should be created"
# Verify .gitignore excludes personal/
gitignore_content = gitignore_path.read_text()
assert "personal/" in gitignore_content, ".gitignore should exclude personal/"
# Verify git repo has initial commit.
# AI/LLM: close the handle before TemporaryDirectory cleanup on Windows.
repo = git.Repo(tmpdir)
try:
assert len(list(repo.iter_commits())) >= 1, "Should have at least one commit"
finally:
repo.close()
def test_copy_directory_tree_copies_python_files(tmp_path):
"""Test copy_directory_tree does not skip Python files."""
source = tmp_path / "source"
source.mkdir()
(source / "script.py").write_text("print('hello')\n", encoding="utf-8")
destination = tmp_path / "dest"
copy_directory_tree(source, destination)
assert (destination / "script.py").read_text(encoding="utf-8") == (
"print('hello')\n"
)
def test_copy_directory_tree_skips_repository_gitignore(tmp_path):
"""Test byte-code caches listed in the repository gitignore are not copied."""
source = tmp_path / "source"
scripts = source / "utils" / "scripts"
(scripts / "__pycache__").mkdir(parents=True)
(scripts / "example.py").write_text("print(1)\n", encoding="utf-8")
(scripts / "__pycache__" / "example.pyc").write_bytes(b"pyc")
destination = tmp_path / "dest"
copy_directory_tree(source, destination)
assert (destination / "utils" / "scripts" / "example.py").is_file()
assert not (destination / "utils" / "scripts" / "__pycache__").exists()
def test_wiki_init_appends_repository_gitignore(tmp_path):
"""Test init keeps existing ignore lines and adds the repository set."""
(tmp_path / ".gitignore").write_text("keep-me\n", encoding="utf-8")
wiki_init(str(tmp_path))
text = (tmp_path / ".gitignore").read_text(encoding="utf-8")
assert "keep-me" in text.splitlines()
assert "__pycache__/" in text.splitlines()
assert "personal/" in text.splitlines()
def test_wiki_init_skips_gitignored_template_file(tmp_path, monkeypatch):
"""Test init does not copy a template file the repository gitignore matches."""
source = tmp_path / "template"
scripts = source / "utils" / "scripts"
(scripts / "__pycache__").mkdir(parents=True)
(scripts / "example.py").write_text("print(1)\n", encoding="utf-8")
(scripts / "__pycache__" / "example.pyc").write_bytes(b"\x00pyc")
(source / "fiona-settings.json").write_text("{}\n", encoding="utf-8")
monkeypatch.setattr(
"fiona.wiki.wiki.get_llm_wiki_source_path", lambda: source
)
wiki_init(str(tmp_path / "wiki"))
wiki_root = tmp_path / "wiki"
assert (wiki_root / "utils" / "scripts" / "example.py").is_file()
assert not (wiki_root / "utils" / "scripts" / "__pycache__").exists()
def test_copy_directory_tree_respects_ignore_patterns(tmp_path):
"""Test matching files are skipped, everything else is copied."""
source = tmp_path / "source"
(source / "scripts").mkdir(parents=True)
(source / "scripts" / "keep.py").write_text("keep\n", encoding="utf-8")
(source / "scripts" / "skip.py").write_text("skip\n", encoding="utf-8")
(source / "README.md").write_text("readme\n", encoding="utf-8")
destination = tmp_path / "dest"
copy_directory_tree(source, destination, ignore_patterns=["*/skip.py"])
assert (destination / "scripts" / "keep.py").exists()
assert (destination / "README.md").exists()
assert not (destination / "scripts" / "skip.py").exists()
def test_copy_directory_tree_ignore_directory_skips_subtree(tmp_path):
"""Test an ignored directory is skipped with its whole subtree."""
source = tmp_path / "source"
(source / "secret").mkdir(parents=True)
(source / "secret" / "data.md").write_text("data\n", encoding="utf-8")
(source / "README.md").write_text("readme\n", encoding="utf-8")
destination = tmp_path / "dest"
copy_directory_tree(source, destination, ignore_patterns=["secret"])
assert not (destination / "secret").exists()
assert (destination / "README.md").exists()
def test_get_ignore_patterns_reads_template_settings(tmp_path):
"""Test patterns come from fiona.llm-wiki.ignore_files."""
(tmp_path / "fiona-settings.json").write_text(json.dumps({
"fiona": {"llm-wiki": {"ignore_files": ["a.py", "b/*"]}}
}), encoding="utf-8")
assert get_ignore_patterns(tmp_path) == ["a.py", "b/*"]
def test_get_ignore_patterns_defaults_to_empty(tmp_path):
"""Test a missing settings file means no ignore patterns."""
assert get_ignore_patterns(tmp_path) == []
def test_get_ignore_patterns_invalid_list_raises(tmp_path):
"""Test a non-list ignore_files value fails loudly."""
(tmp_path / "fiona-settings.json").write_text(json.dumps({
"fiona": {"llm-wiki": {"ignore_files": "nope"}}
}), encoding="utf-8")
with pytest.raises(ValueError):
get_ignore_patterns(tmp_path)
def test_wiki_init_skips_ignored_files(tmp_path, monkeypatch):
"""Test wiki_init excludes files listed in the template's ignore_files."""
template = tmp_path / "template"
(template / "utils").mkdir(parents=True)
(template / "utils" / "example.py").write_text("x = 1\n", encoding="utf-8")
(template / "README.md").write_text("readme\n", encoding="utf-8")
(template / "fiona-settings.json").write_text(json.dumps({
"fiona": {"llm-wiki": {"ignore_files": ["utils/example.py"]}}
}), encoding="utf-8")
monkeypatch.setattr(
"fiona.wiki.wiki.get_llm_wiki_source_path", lambda: template
)
wiki_init(str(tmp_path / "wiki"))
wiki_root = tmp_path / "wiki"
assert (wiki_root / "README.md").exists()
assert (wiki_root / "fiona-settings.json").exists()
assert not (wiki_root / "utils" / "example.py").exists()
def test_wiki_init():
"""Test that wiki_init creates the correct directory structure."""
with tempfile.TemporaryDirectory() as tmpdir:
result = wiki_init(tmpdir)
assert result.startswith("Successfully initialized llm-wiki directory at:")
base_path = Path(tmpdir)
# Check that key directories were created
assert (base_path / "wiki").exists()
assert (base_path / "ingest").exists()
assert (base_path / "personal").exists()
assert (base_path / "raw").exists()
assert (base_path / "utils").exists()
# Check that key files were created
assert (base_path / "AGENTS.md").exists()
assert (base_path / "README.md").exists()
assert (base_path / "fiona-settings.json").exists()
assert (base_path / "wiki" / "index.md").exists()
assert (base_path / "wiki" / "log.md").exists()
assert (base_path / "wiki" / "schema.md").exists()
def test_copy_directory_tree_missing_source_raises(tmp_path):
"""Test copy_directory_tree refuses a missing source directory."""
with pytest.raises(FileNotFoundError):
copy_directory_tree(tmp_path / "missing", tmp_path / "dest")
def test_copy_directory_tree_copies_binary_files(tmp_path):
"""Test copy_directory_tree falls back to shutil for binary files."""
source = tmp_path / "source"
source.mkdir()
binary = b"\x89PNG\r\n\x1a\n\x00\x00"
(source / "image.png").write_bytes(binary)
destination = tmp_path / "dest"
copy_directory_tree(source, destination)
assert (destination / "image.png").read_bytes() == binary
def test_wiki_init_refuses_existing_wiki(tmp_path):
"""Test wiki_init refuses to run on an already initialized directory."""
wiki_init(str(tmp_path))
with pytest.raises(FileExistsError):
wiki_init(str(tmp_path))
def test_wiki_init_refusal_names_conflicting_indicators(tmp_path):
"""Test the refusal message names every conflicting indicator file."""
wiki_init(str(tmp_path))
with pytest.raises(FileExistsError) as excinfo:
wiki_init(str(tmp_path))
message = str(excinfo.value)
for indicator in (
"AGENTS.md",
"fiona-settings.json",
"wiki/",
"ingest/",
"README.md",
):
assert indicator in message
def test_wiki_init_refuses_partial_llm_wiki(tmp_path):
"""Test a directory with only some llm-wiki indicators is refused."""
(tmp_path / "wiki").mkdir()
(tmp_path / "wiki" / "index.md").write_text(
"user content\n", encoding="utf-8"
)
with pytest.raises(FileExistsError) as excinfo:
wiki_init(str(tmp_path))
assert "wiki/" in str(excinfo.value)
def test_wiki_init_rerun_does_not_clobber_user_content(tmp_path):
"""Test a refused re-run leaves user-modified files untouched."""
wiki_init(str(tmp_path))
log = tmp_path / "wiki" / "log.md"
modified_log = log.read_text(encoding="utf-8") + "user entry\n"
log.write_text(modified_log, encoding="utf-8")
note = tmp_path / "wiki" / "concepts" / "user-note.md"
note.parent.mkdir(parents=True, exist_ok=True)
note.write_text("user note\n", encoding="utf-8")
with pytest.raises(FileExistsError):
wiki_init(str(tmp_path))
assert log.read_text(encoding="utf-8") == modified_log
assert note.read_text(encoding="utf-8") == "user note\n"
def test_wiki_init_missing_template_raises(tmp_path, monkeypatch):
"""Test wiki_init reports a missing template directory."""
monkeypatch.setattr(
"fiona.wiki.wiki.get_llm_wiki_source_path", lambda: tmp_path / "no-template"
)
with pytest.raises(FileNotFoundError):
wiki_init(str(tmp_path))