"""Tests for the machine-readable side of the site: JSON-LD, social cards, the sitemap's dates, robots headers, the 404, the Atom feed, llms.txt and the favicon. The JSON-LD is checked against the properties schema.org and Google Search Central document for each type (REQUIRED below, read 2026-11); Google's Rich Results Test itself cannot be run here. The site is built into web/.cache/test-site, as the other page tests do. """ import html import json import os import re import subprocess import sys import unittest import xml.etree.ElementTree as ET WEB = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ROOT = os.path.dirname(WEB) sys.path.insert(0, WEB) import serve # noqa: E402 from olt import browse, cards, common, docs, feed, records, regions, seo, site, thumbs # noqa: E402 OUT = os.path.join(WEB, ".cache", "test-site") SITE = site.load() BASE = SITE.base_url # The properties each type must carry: Google's required ones where it has a feature # (BreadcrumbList, ListItem, Dataset), and the ones that make the node say what it is otherwise. REQUIRED = { "ItemPage": ("@id", "url", "name", "mainEntity", "dateModified"), "CollectionPage": ("@id", "url", "name", "mainEntity", "dateModified"), "Place": ("name",), "TouristAttraction": ("@id", "name", "url", "additionalProperty"), # a trail or route "ItemList": ("itemListElement", "numberOfItems"), "BreadcrumbList": ("itemListElement",), "ListItem": ("position", "name"), "WebSite": ("name", "url"), "Organization": ("name", "url", "logo"), "Dataset": ("name", "description", "license", "creator", "distribution", "keywords", "spatialCoverage", "temporalCoverage", "dateModified", "version", "isAccessibleForFree", "variableMeasured", "measurementTechnique"), "DataDownload": ("contentUrl", "encodingFormat"), "PropertyValue": ("name",), } # Things a page about a trail must not claim: the dataset holds none of them. FORBIDDEN = {"aggregateRating", "review", "reviews", "openingHours", "openingHoursSpecification", "priceRange", "offers", "price", "telephone", "ratingValue", "potentialAction"} def missing(node): """The required properties a node lacks, by its type; ListItem needs item or url as well.""" types = node.get("@type") types = types if isinstance(types, list) else [types] out = [] for t in types: out += [k for k in REQUIRED.get(t, ()) if node.get(k) in (None, "", [], {})] if t == "ListItem" and not (node.get("item") or node.get("url")): out.append("item") if t == "Dataset" and "description" in node and not 50 <= len(node["description"]) <= 5000: out.append("description (50 to 5000 characters)") return out def read(rel): with open(os.path.join(OUT, *rel.split("/")), encoding="utf-8") as f: return f.read() def ld_blocks(doc): """The raw text of every JSON-LD script in an HTML document.""" return re.findall(r'', doc, re.S) def nodes_of(doc): """Every JSON-LD node on a page, flattened out of any @graph.""" out = [] for raw in ld_blocks(doc): obj = json.loads(raw) out += obj["@graph"] if "@graph" in obj else [obj] return out def walk(obj): """Every dict inside a JSON-LD value.""" if isinstance(obj, dict): yield obj for v in obj.values(): yield from walk(v) elif isinstance(obj, list): for v in obj: yield from walk(v) def meta(doc, key, attr="property"): m = re.search(rf']*>(.*?)', doc, re.S) if not m: return [] items = [] for li in re.findall(r"