Coverage for tests/test_llms.py: 93%

90 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-25 22:09 +0000

1import re 

2import unittest 

3 

4from werkzeug.exceptions import MethodNotAllowed, NotFound 

5from werkzeug.routing.exceptions import RequestRedirect 

6 

7from webapp.app import create_app 

8from webapp.llms import store_llm 

9 

10ROOT_CATCH_ALL = re.compile(r"/<[^>]+>") 

11 

12 

13class TestDiscoveredPages(unittest.TestCase): 

14 def setUp(self): 

15 self.app = create_app(testing=True) 

16 self.pages = store_llm.pages(self.app) 

17 self.paths = {page["path"] for page in self.pages} 

18 

19 def test_the_marketing_pages_are_discovered(self): 

20 self.assertLessEqual( 

21 { 

22 "/about", 

23 "/about/contact-us", 

24 "/about/publish", 

25 "/account/agreement", 

26 "/build", 

27 "/store", 

28 }, 

29 self.paths, 

30 ) 

31 

32 def test_every_page_has_a_title_and_a_description(self): 

33 for page in self.pages: 

34 self.assertTrue(page["title"], page["path"]) 

35 self.assertTrue(page["description"], page["path"]) 

36 

37 def test_titles_come_from_the_template_metadata(self): 

38 page = next(page for page in self.pages if page["path"] == "/about") 

39 

40 self.assertEqual(page["title"], "About Snaps") 

41 self.assertIn("Snaps are app packages", page["description"]) 

42 

43 def test_snap_and_publisher_pages_are_left_out(self): 

44 self.assertNotIn("/snaps", self.paths) 

45 self.assertNotIn("/validation-sets", self.paths) 

46 

47 def test_noindex_pages_are_left_out(self): 

48 self.assertNotIn("/about/thank-you", self.paths) 

49 

50 def test_redirects_and_machine_formats_are_left_out(self): 

51 for path in ["/community", "/create", "/account.json", "/sitemap.xml"]: 

52 self.assertNotIn(path, self.paths) 

53 

54 def test_routes_rendering_the_same_page_are_listed_once(self): 

55 self.assertIn("/store", self.paths) 

56 self.assertNotIn("/explore", self.paths) 

57 self.assertNotIn("/search", self.paths) 

58 

59 def test_every_page_matches_a_real_route(self): 

60 adapter = self.app.url_map.bind("snapcraft.io") 

61 unresolved = [] 

62 

63 for path in sorted(self.paths): 

64 try: 

65 rule, _ = adapter.match(path, return_rule=True) 

66 except RequestRedirect: 

67 continue 

68 except (NotFound, MethodNotAllowed): 

69 unresolved.append(path) 

70 continue 

71 

72 if ROOT_CATCH_ALL.fullmatch(rule.rule): 

73 unresolved.append(path) 

74 

75 self.assertEqual( 

76 unresolved, [], f"llms.txt links go nowhere: {unresolved}" 

77 ) 

78 

79 def test_pages_are_grouped_into_known_sections(self): 

80 sections = [ 

81 group["section"] for group in store_llm.llms_sections(self.pages) 

82 ] 

83 

84 self.assertIn("Main pages", sections) 

85 self.assertIn("Publishing a snap", sections) 

86 self.assertNotIn("Other pages", sections) 

87 self.assertEqual(sections[-1], "Optional") 

88 

89 

90class TestRoutes(unittest.TestCase): 

91 def setUp(self): 

92 self.app = create_app(testing=True) 

93 self.client = self.app.test_client() 

94 

95 def test_llms_txt_lists_the_pages(self): 

96 response = self.client.get("/llms.txt") 

97 

98 self.assertEqual(response.status_code, 200) 

99 self.assertIn("text/plain", response.headers["Content-Type"]) 

100 body = response.data.decode() 

101 self.assertTrue(body.startswith("# Snapcraft\n")) 

102 self.assertIn("## Main pages", body) 

103 self.assertIn("https://snapcraft.io/about.md", body) 

104 self.assertIn("https://snapcraft.io/llms-full.txt", body) 

105 

106 def test_the_sitemap_lists_the_same_pages(self): 

107 body = self.client.get("/sitemap-links.xml").data.decode() 

108 

109 for path in store_llm.sitemap_paths(self.app): 

110 self.assertIn(f"<loc>https://snapcraft.io{path}</loc>", body) 

111 

112 def test_suffix_serves_markdown(self): 

113 for path in ["/index.md", "/about.md", "/build.md", "/inkscape.md"]: 

114 response = self.client.get(path) 

115 

116 self.assertEqual(response.status_code, 200, path) 

117 self.assertIn("text/markdown", response.content_type, path) 

118 

119 def test_pages_behind_login_have_no_markdown_version(self): 

120 for path in ["/snaps.md", "/account/snaps.md", "/validation-sets.md"]: 

121 self.assertEqual(self.client.get(path).status_code, 404, path) 

122 

123 self.assertNotEqual(self.client.get("/snaps").status_code, 404) 

124 

125 def test_the_markdown_link_follows_the_spec(self): 

126 links = { 

127 "/": "https://snapcraft.io/index.md", 

128 "/about": "https://snapcraft.io/about.md", 

129 } 

130 

131 for path, href in links.items(): 

132 body = self.client.get(path).data.decode() 

133 

134 self.assertIn( 

135 f'<link rel="alternate" type="text/markdown" href="{href}"', 

136 body, 

137 ) 

138 self.assertIn( 

139 '<link rel="describedby" href="https://snapcraft.io/llms.txt"', 

140 body, 

141 ) 

142 

143 def test_content_anchor_is_present_on_every_page(self): 

144 for path in ["/", "/about", "/build", "/iot"]: 

145 body = self.client.get(path).data.decode() 

146 

147 self.assertEqual( 

148 body.count('id="main-content"'), 

149 1, 

150 f"{path} needs exactly one content anchor", 

151 )