Spaces:
Sleeping
Sleeping
File size: 13,274 Bytes
7d7841f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | import sys
import unittest
from dataclasses import replace
from pathlib import Path
from unittest.mock import AsyncMock, patch
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from app.checks.extraction_readiness import ExtractionReadinessInput, analyse_extraction_readiness
from app.checks.llms_txt import check_llms_txt
from app.checks.schema_ld import check_schema_ld
from app.checks.search_blurb import SearchBlurbAnalysis
from app.checks.search_discovery import (
FeedFreshnessResult,
SamplePageMetadataResult,
TrustSurfaceResult,
check_search_discovery,
)
from app.checks.ssr import check_ssr
from app.fetch_evidence import collect_fetch_evidence
from app.rubric import ESSENTIALS_CHECKED_MAX, ESSENTIALS_CHECK_GROUP_COUNT
from backend.tests.fixtures import make_audit_context, make_fetch_result
def _page(title: str, path: str) -> str:
description = (
f"{title} explains reliable machine-readable website content with clear "
"metadata, structured facts, and accessible source text for public crawlers."
)
words = " ".join(
f"reliable machine readable website content metadata structured facts accessible source crawler {index}"
for index in range(12)
)
return f"""
<html><head>
<title>{title}</title>
<meta name="description" content="{description}">
<meta property="og:description" content="{description}">
<meta name="twitter:description" content="{description}">
<link rel="canonical" href="https://example.com{path}">
<script type="application/ld+json">
{{"@context":"https://schema.org","@type":"Organization","name":"Example",
"url":"https://example.com","logo":"https://example.com/logo.png",
"sameAs":["https://social.example/example"]}}
</script>
</head><body><main><h1>{title}</h1><p>{words}</p></main></body></html>
"""
class RuntimeIntegrationTests(unittest.IsolatedAsyncioTestCase):
def setUp(self) -> None:
sitemap = """
<urlset>
<url><loc>https://example.com</loc><lastmod>2026-07-17</lastmod></url>
<url><loc>https://example.com/about</loc><lastmod>2026-07-17</lastmod></url>
</urlset>
"""
self.context = make_audit_context(
homepage_html=_page("Machine-readable homepage", ""),
robots_text=(
"User-agent: *\nAllow: /\nSitemap: https://example.com/sitemap.xml"
),
sitemap_text=sitemap,
)
self.calls: list[tuple[str, str]] = []
async def _fetch(self, url: str, **kwargs):
self.calls.append((url, kwargs.get("accept", "")))
if url == "https://example.com/alternate-index.xml":
return make_fetch_result(
url,
"""
<sitemapindex>
<sitemap><loc>https://example.com/child-sitemap.xml</loc></sitemap>
<sitemap><loc>https://example.com/child-sitemap.xml</loc></sitemap>
</sitemapindex>
""",
headers={"content-type": "application/xml"},
)
if url == "https://example.com/child-sitemap.xml":
return make_fetch_result(
url,
"""
<urlset>
<url><loc>https://example.com/about</loc><lastmod>2026-07-17</lastmod></url>
</urlset>
""",
headers={"content-type": "application/xml"},
)
if url == "https://example.com/llms.txt":
return make_fetch_result(
url,
"# Example\n\nMachine-readable documentation: https://example.com/about",
headers={"content-type": "text/plain"},
)
if url == "https://example.com/about":
return make_fetch_result(
url,
_page("About Machine-readable Example", "/about"),
headers={"content-type": "text/html"},
)
markdown = "# Machine-readable content\n\n" + " ".join(
"reliable machine readable website content metadata structured facts accessible source crawler"
for _ in range(8)
)
return make_fetch_result(
url,
markdown,
headers={"content-type": "text/markdown", "vary": "Accept"},
)
async def _bundle(self, include_ecommerce: bool = False):
with (
patch("app.fetch_evidence.fetch_url", new=self._fetch),
patch("app.checks.sitemap_analysis.fetch_url", new=self._fetch),
):
return await collect_fetch_evidence(self.context, include_ecommerce)
async def test_bundle_reuses_root_sitemap_homepage_and_sample_fetches(self) -> None:
bundle = await self._bundle()
requested_urls = [url for url, _ in self.calls]
self.assertEqual(requested_urls.count("https://example.com/sitemap.xml"), 0)
self.assertEqual(requested_urls.count("https://example.com"), 1)
self.assertEqual(requested_urls.count("https://example.com/about"), 1)
self.assertEqual(requested_urls.count("https://example.com/about/index.md"), 1)
self.assertEqual(requested_urls.count("https://example.com/llms.txt"), 1)
self.assertEqual(len(bundle.search_blurb.pages), 2)
self.assertEqual(len(bundle.sample_page_responses), 2)
async def test_fetches_each_alternate_and_child_sitemap_once(self) -> None:
self.context = make_audit_context(
homepage_html=_page("Machine-readable homepage", ""),
robots_text=(
"User-agent: *\nAllow: /\n"
"Sitemap: https://example.com/sitemap.xml\n"
"Sitemap: https://example.com/alternate-index.xml\n"
"Sitemap: https://example.com/alternate-index.xml"
),
sitemap_text=self.context.sitemap.text,
)
bundle = await self._bundle()
requested_urls = [url for url, _ in self.calls]
self.assertEqual(requested_urls.count("https://example.com/alternate-index.xml"), 1)
self.assertEqual(requested_urls.count("https://example.com/child-sitemap.xml"), 1)
self.assertTrue(bundle.sitemap_sample.is_index)
self.assertEqual(bundle.sitemap_sample.sitemap_count, 3)
async def test_reuses_homepage_markdown_by_redirect_final_url(self) -> None:
redirected_url = "https://example.com/home"
candidate_url = f"{redirected_url}/index.md"
sitemap = (
"<urlset><url><loc>https://example.com/home</loc>"
"<lastmod>2026-07-17</lastmod></url></urlset>"
)
self.context = make_audit_context(
homepage_html=_page("Machine-readable homepage", "/home"),
homepage_final_url=redirected_url,
sitemap_text=sitemap,
)
markdown = "# Machine-readable content\n\n" + " ".join(
"reliable machine readable website content metadata structured facts accessible source crawler"
for _ in range(8)
)
async def redirect_fetch(url: str, **kwargs):
self.calls.append((url, kwargs.get("accept", "")))
if url == "https://example.com":
return make_fetch_result(
url,
markdown,
final_url=redirected_url,
headers={"content-type": "text/markdown", "vary": "Accept"},
)
if url == "https://example.com/llms.txt":
return make_fetch_result(
url,
"# Example\n\nMachine-readable documentation: https://example.com/home",
headers={"content-type": "text/plain"},
)
self.fail(f"unexpected fetch: {url}")
with (
patch("app.fetch_evidence.fetch_url", new=redirect_fetch),
patch("app.checks.sitemap_analysis.fetch_url", new=redirect_fetch),
):
bundle = await collect_fetch_evidence(self.context)
requested_urls = [url for url, _ in self.calls]
self.assertNotIn(candidate_url, requested_urls)
self.assertEqual(len(bundle.sample_markdown_by_page), 1)
page_url, response = bundle.sample_markdown_by_page[0]
self.assertEqual(page_url, redirected_url)
self.assertEqual(response.final_url, redirected_url)
async def test_good_bundle_preserves_authoritative_row_scores(self) -> None:
bundle = await self._bundle()
freshness = FeedFreshnessResult(True, ["recent update surface"], [], [])
trust = TrustSurfaceResult(
found={"about": ["sitemap"], "contact": ["homepage"], "privacy": ["homepage"]},
positives=["core trust surfaces are discoverable"],
issues=[],
caveats=[],
)
with (
patch(
"app.checks.search_discovery._freshness_surface",
new=AsyncMock(return_value=freshness),
),
patch("app.checks.search_discovery._trust_surface_discovery", return_value=trust),
):
search = await check_search_discovery(self.context, False, bundle)
llms = await check_llms_txt(self.context, bundle)
schema = await check_schema_ld(self.context, False, bundle)
ssr = await check_ssr(self.context, bundle)
self.assertEqual((search.state, search.score), ("pass", 4))
self.assertEqual((llms.state, llms.score), ("pass", 5))
self.assertEqual((schema.state, schema.score), ("pass", 5))
self.assertEqual((ssr.state, ssr.score), ("pass", 4))
self.assertIn("no Firecrawl or other extraction-provider API was called", llms.finding)
async def test_blurb_issue_only_caps_existing_sampled_metadata_point(self) -> None:
bundle = await self._bundle()
degraded = replace(
bundle,
search_blurb=SearchBlurbAnalysis(
pages=bundle.search_blurb.pages,
positives=(),
issues=("2 sampled page(s) reuse a normalized title across 1 duplicate group(s).",),
caveats=(),
),
)
freshness = FeedFreshnessResult(True, ["recent update surface"], [], [])
trust = TrustSurfaceResult({}, [], [], [])
with (
patch(
"app.checks.search_discovery._freshness_surface",
new=AsyncMock(return_value=freshness),
),
patch("app.checks.search_discovery._trust_surface_discovery", return_value=trust),
):
result = await check_search_discovery(self.context, False, degraded)
self.assertEqual((result.state, result.score), ("partial", 3))
self.assertIn("search-blurb proxy", result.finding)
self.assertIn("actual DuckDuckGo and Bing snippet display was not verified", result.finding)
already_degraded = SamplePageMetadataResult(
False, [], ["sampled metadata is incomplete"], []
)
with (
patch(
"app.checks.search_discovery._freshness_surface",
new=AsyncMock(return_value=freshness),
),
patch("app.checks.search_discovery._trust_surface_discovery", return_value=trust),
patch(
"app.checks.search_discovery._sample_pages_have_discovery_metadata",
new=AsyncMock(return_value=already_degraded),
),
):
result = await check_search_discovery(self.context, False, degraded)
self.assertEqual(result.score, 3)
async def test_hidden_app_shell_caps_only_an_otherwise_passing_ssr_row(self) -> None:
bundle = await self._bundle()
hidden_words = " ".join(f"important content {index}" for index in range(100))
html = (
'<html><body><main hidden><h1>Hidden</h1><p>'
+ hidden_words
+ '</p></main><div id="app"></div>'
+ "".join(f'<script src="/{index}.js"></script>' for index in range(4))
+ "</body></html>"
)
hidden_context = make_audit_context(homepage_html=html)
extraction = analyse_extraction_readiness(
ExtractionReadinessInput(url="https://example.com", raw_html=html)
)
result = await check_ssr(
hidden_context,
replace(bundle, extraction_readiness=extraction),
)
self.assertEqual((result.state, result.score), ("partial", 3))
self.assertIn("hidden source node", result.finding)
async def test_scope_off_excludes_commerce_coherence_and_contract_is_fixed(self) -> None:
bundle = await self._bundle(include_ecommerce=False)
schema = await check_schema_ld(self.context, False, bundle)
self.assertEqual(bundle.extraction_readiness.commerce_missing_fields, ())
self.assertEqual(bundle.extraction_readiness.commerce_schema_visible_mismatches, ())
self.assertNotIn("Visible product content", schema.finding)
self.assertEqual(ESSENTIALS_CHECK_GROUP_COUNT, 13)
self.assertEqual(ESSENTIALS_CHECKED_MAX, 56)
if __name__ == "__main__":
unittest.main()
|