|
| 1 | +"""Tests for llms.txt generation.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from rockgarden.config import CollectionConfig, NavLinkConfig |
| 6 | +from rockgarden.content.collection import Collection |
| 7 | +from rockgarden.content.models import Page |
| 8 | +from rockgarden.nav.folder_index import FolderIndex |
| 9 | +from rockgarden.output.llms_txt import build_llms_txt |
| 10 | + |
| 11 | + |
| 12 | +class TestBuildLlmsTxt: |
| 13 | + def _make_page(self, slug, title=None, source_prefix=""): |
| 14 | + source = f"{source_prefix}/{slug}.md" if source_prefix else f"{slug}.md" |
| 15 | + return Page( |
| 16 | + source_path=Path(source), |
| 17 | + slug=slug, |
| 18 | + frontmatter={"title": title} if title else {}, |
| 19 | + ) |
| 20 | + |
| 21 | + def test_title_heading(self): |
| 22 | + result = build_llms_txt([], [], {}, "https://example.com", "My Site") |
| 23 | + assert result.startswith("# My Site\n") |
| 24 | + |
| 25 | + def test_description_blockquote(self): |
| 26 | + result = build_llms_txt( |
| 27 | + [], [], {}, "https://example.com", "My Site", description="A cool site." |
| 28 | + ) |
| 29 | + assert "> A cool site." in result |
| 30 | + |
| 31 | + def test_no_description(self): |
| 32 | + result = build_llms_txt([], [], {}, "https://example.com", "My Site") |
| 33 | + assert ">" not in result |
| 34 | + |
| 35 | + def test_root_pages_under_pages_heading(self): |
| 36 | + pages = [self._make_page("about", "About")] |
| 37 | + result = build_llms_txt(pages, [], {}, "https://example.com", "My Site") |
| 38 | + assert "## Pages" in result |
| 39 | + assert "- [About](https://example.com/about/)" in result |
| 40 | + |
| 41 | + def test_pages_grouped_by_directory(self): |
| 42 | + pages = [ |
| 43 | + self._make_page("docs/intro", "Introduction"), |
| 44 | + self._make_page("docs/setup", "Setup"), |
| 45 | + ] |
| 46 | + result = build_llms_txt(pages, [], {}, "https://example.com", "My Site") |
| 47 | + assert "## docs" in result |
| 48 | + assert "- [Introduction](https://example.com/docs/intro/)" in result |
| 49 | + assert "- [Setup](https://example.com/docs/setup/)" in result |
| 50 | + assert "## Pages" not in result |
| 51 | + |
| 52 | + def test_collection_pages_grouped_separately(self): |
| 53 | + page = self._make_page("blog/post1", "First Post", source_prefix="blog") |
| 54 | + col_config = CollectionConfig(name="Blog", source="blog") |
| 55 | + col = Collection(name="Blog", config=col_config, entries=[page]) |
| 56 | + result = build_llms_txt( |
| 57 | + [page], [], {"blog": col}, "https://example.com", "My Site" |
| 58 | + ) |
| 59 | + assert "## Blog" in result |
| 60 | + assert "- [First Post](https://example.com/blog/post1/)" in result |
| 61 | + # Should not also appear under directory grouping |
| 62 | + lines = result.split("\n") |
| 63 | + assert lines.count("## Blog") == 1 |
| 64 | + assert "## blog" not in result |
| 65 | + |
| 66 | + def test_folder_indexes_included(self): |
| 67 | + folders = [FolderIndex(slug="docs/index", title="Docs", children=[])] |
| 68 | + result = build_llms_txt([], folders, {}, "https://example.com", "My Site") |
| 69 | + assert "## docs" in result |
| 70 | + assert "- [Docs](https://example.com/docs/)" in result |
| 71 | + |
| 72 | + def test_clean_urls_false(self): |
| 73 | + pages = [self._make_page("about", "About")] |
| 74 | + result = build_llms_txt( |
| 75 | + pages, [], {}, "https://example.com", "My Site", clean_urls=False |
| 76 | + ) |
| 77 | + assert "https://example.com/about.html" in result |
| 78 | + |
| 79 | + def test_base_path(self): |
| 80 | + pages = [self._make_page("about", "About")] |
| 81 | + result = build_llms_txt( |
| 82 | + pages, [], {}, "https://example.com", "My Site", base_path="/docs" |
| 83 | + ) |
| 84 | + assert "https://example.com/docs/about/" in result |
| 85 | + |
| 86 | + def test_alphabetical_ordering(self): |
| 87 | + pages = [ |
| 88 | + self._make_page("zebra", "Zebra"), |
| 89 | + self._make_page("apple", "Apple"), |
| 90 | + ] |
| 91 | + result = build_llms_txt(pages, [], {}, "https://example.com", "My Site") |
| 92 | + apple_pos = result.index("Apple") |
| 93 | + zebra_pos = result.index("Zebra") |
| 94 | + assert apple_pos < zebra_pos |
| 95 | + |
| 96 | + def test_collections_before_directories(self): |
| 97 | + page_col = self._make_page("blog/post", "Post", source_prefix="blog") |
| 98 | + page_dir = self._make_page("docs/guide", "Guide") |
| 99 | + col_config = CollectionConfig(name="Blog", source="blog") |
| 100 | + col = Collection(name="Blog", config=col_config, entries=[page_col]) |
| 101 | + result = build_llms_txt( |
| 102 | + [page_col, page_dir], |
| 103 | + [], |
| 104 | + {"blog": col}, |
| 105 | + "https://example.com", |
| 106 | + "My Site", |
| 107 | + ) |
| 108 | + blog_pos = result.index("## Blog") |
| 109 | + docs_pos = result.index("## docs") |
| 110 | + assert blog_pos < docs_pos |
| 111 | + |
| 112 | + def test_empty_site(self): |
| 113 | + result = build_llms_txt([], [], {}, "https://example.com", "My Site") |
| 114 | + assert result == "# My Site\n" |
| 115 | + |
| 116 | + def test_nav_links_section(self): |
| 117 | + links = [ |
| 118 | + NavLinkConfig(label="GitHub", url="https://github.com/example"), |
| 119 | + NavLinkConfig(label="Tags", url="/tags/"), |
| 120 | + ] |
| 121 | + result = build_llms_txt( |
| 122 | + [], [], {}, "https://example.com", "My Site", nav_links=links |
| 123 | + ) |
| 124 | + assert "## Links" in result |
| 125 | + assert "- [GitHub](https://github.com/example)" in result |
| 126 | + assert "- [Tags](/tags/)" in result |
| 127 | + |
| 128 | + def test_nav_links_after_pages(self): |
| 129 | + links = [NavLinkConfig(label="GitHub", url="https://github.com/example")] |
| 130 | + pages = [self._make_page("about", "About")] |
| 131 | + result = build_llms_txt( |
| 132 | + pages, [], {}, "https://example.com", "My Site", nav_links=links |
| 133 | + ) |
| 134 | + pages_pos = result.index("## Pages") |
| 135 | + links_pos = result.index("## Links") |
| 136 | + assert pages_pos < links_pos |
| 137 | + |
| 138 | + def test_nav_links_flattens_nested(self): |
| 139 | + links = [ |
| 140 | + NavLinkConfig( |
| 141 | + label="Parent", |
| 142 | + url="", |
| 143 | + children=[ |
| 144 | + NavLinkConfig(label="Child", url="https://example.com/child"), |
| 145 | + ], |
| 146 | + ), |
| 147 | + ] |
| 148 | + result = build_llms_txt( |
| 149 | + [], [], {}, "https://example.com", "My Site", nav_links=links |
| 150 | + ) |
| 151 | + assert "- [Child](https://example.com/child)" in result |
| 152 | + assert "Parent" not in result |
| 153 | + |
| 154 | + def test_no_nav_links_no_section(self): |
| 155 | + result = build_llms_txt( |
| 156 | + [], [], {}, "https://example.com", "My Site", nav_links=[] |
| 157 | + ) |
| 158 | + assert "## Links" not in result |
| 159 | + |
| 160 | + def test_collection_folder_index_not_duplicated(self): |
| 161 | + page = self._make_page("blog/post1", "First Post", source_prefix="blog") |
| 162 | + folder = FolderIndex(slug="blog/index", title="Blog", children=[]) |
| 163 | + col_config = CollectionConfig(name="Blog", source="blog") |
| 164 | + col = Collection(name="Blog", config=col_config, entries=[page]) |
| 165 | + result = build_llms_txt( |
| 166 | + [page], [folder], {"blog": col}, "https://example.com", "My Site" |
| 167 | + ) |
| 168 | + assert "## Blog" in result |
| 169 | + assert "## blog" not in result |
| 170 | + |
| 171 | + def test_trailing_newline(self): |
| 172 | + result = build_llms_txt( |
| 173 | + [self._make_page("about", "About")], |
| 174 | + [], |
| 175 | + {}, |
| 176 | + "https://example.com", |
| 177 | + "My Site", |
| 178 | + ) |
| 179 | + assert result.endswith("\n") |
0 commit comments