"""Validate built metadata, structured data, assets, sitemap, and internal links.""" from html.parser import HTMLParser from pathlib import Path from urllib.parse import urlsplit, unquote, urljoin import json import struct import sys from datetime import datetime import xml.etree.ElementTree as ET root = Path(sys.argv[1] if len(sys.argv) > 1 else 'dist') class Page(HTMLParser): def __init__(self): super().__init__(); self.meta = []; self.links = []; self.ids = set(); self.h1 = 0; self.title = 0; self.schema = []; self.in_schema = False; self.buffer = ''; self.title_text = ''; self.in_title = False; self.lang = None def handle_starttag(self, tag, attrs): a = dict(attrs) if 'id' in a: assert a['id'] not in self.ids, ('duplicate ID', a['id']) self.ids.add(a['id']) if tag == 'html': self.lang = a.get('lang') if tag == 'img': assert 'alt' in a, ('missing image alt', a.get('src')) if tag == 'h1': self.h1 += 1 if tag == 'title': self.title += 1; self.in_title = True if tag == 'meta': self.meta.append(a) if tag == 'link': self.links.append((tag, a)) if tag in ('a', 'img', 'source', 'track', 'script', 'video'): self.links.append((tag, a)) if tag == 'script' and a.get('type') == 'application/ld+json': self.in_schema = True; self.buffer = '' def handle_data(self, text): if self.in_schema: self.buffer += text if self.in_title: self.title_text += text def handle_endtag(self, tag): if tag == 'title': self.in_title = False if tag == 'script' and self.in_schema: self.schema.append(json.loads(self.buffer)); self.in_schema = False pages = {} for path in root.rglob('index.html'): route = '/' + str(path.parent.relative_to(root)).replace('\\','/') + '/' route = '/' if route == '/./' else route page = Page(); page.feed(path.read_text()); pages[route] = page descriptions = set() titles = set() origin = 'https://pulsecanvas.ai' def meta_value(page, key, value): matches = [m.get('content', '') for m in page.meta if m.get(key) == value] assert len(matches) == 1 and matches[0].strip(), (value, 'missing or duplicate metadata') return matches[0] def local_asset(url): parsed = urlsplit(url) assert parsed.scheme == 'https' and parsed.netloc == 'pulsecanvas.ai', ('asset must be canonical HTTPS', url) asset = root / unquote(parsed.path).lstrip('/') assert asset.is_file(), ('missing metadata/schema asset', url) return asset def schema_assets(value): if isinstance(value, dict): for key, child in value.items(): if key in ['image', 'screenshot', 'logo', 'thumbnailUrl', 'contentUrl']: for url in child if isinstance(child, list) else [child]: if isinstance(url, str) and url.startswith(origin): local_asset(url) schema_assets(child) elif isinstance(value, list): for child in value: schema_assets(child) for route, page in pages.items(): assert page.h1 == 1 and page.title == 1, (route, 'heading/title count') assert page.lang == 'en', (route, 'document language') assert page.title_text.strip() and page.title_text not in titles, (route, 'empty/duplicate title') titles.add(page.title_text) robots = meta_value(page, 'name', 'robots') assert 'noindex' not in robots and 'nofollow' not in robots, (route, 'indexing blocked') assert meta_value(page, 'name', 'viewport').startswith('width=device-width') description = [m['content'] for m in page.meta if m.get('name') == 'description'] assert len(description) == 1 and description[0] not in descriptions, (route, 'description') descriptions.add(description[0]) canonical = [a['href'] for tag, a in page.links if a.get('rel') == 'canonical'] assert canonical == ['https://pulsecanvas.ai' + route], (route, canonical) for property in ['og:title','og:description','og:type','og:url','og:site_name','og:locale','og:image','og:image:secure_url','og:image:type','og:image:width','og:image:height','og:image:alt']: meta_value(page, 'property', property) assert meta_value(page, 'property', 'og:url') == canonical[0] assert meta_value(page, 'property', 'og:description') == description[0] assert meta_value(page, 'name', 'twitter:card') == 'summary_large_image' for name in ['twitter:title','twitter:description','twitter:image','twitter:image:alt']: meta_value(page, 'name', name) og_image = meta_value(page, 'property', 'og:image') assert meta_value(page, 'property', 'og:image:secure_url') == og_image assert meta_value(page, 'name', 'twitter:image') == og_image assert meta_value(page, 'name', 'twitter:title') == meta_value(page, 'property', 'og:title') assert meta_value(page, 'name', 'twitter:description') == description[0] image = local_asset(og_image) with image.open('rb') as png: header = png.read(24) assert header[:8] == b'\x89PNG\r\n\x1a\n', ('invalid PNG', image) width, height = struct.unpack('>II', header[16:24]) assert (width, height) == (1200, 630), (route, 'social image dimensions') assert meta_value(page, 'property', 'og:image:width') == str(width) assert meta_value(page, 'property', 'og:image:height') == str(height) assert meta_value(page, 'property', 'og:image:type') == 'image/png' assert len(page.schema) == 1 graph = page.schema[0]['@graph'] schema_assets(graph) if route.startswith('/guides/') and route != '/guides/': article = next(node for node in graph if node['@type'] == 'TechArticle') assert article['headline'] == page.title_text and article['author'] and article['image'] if route == '/': assert any(node['@type'] == 'SoftwareApplication' and node['offers']['price'] == 0 for node in graph) for node in graph: if node['@type'] == 'VideoObject': published = datetime.fromisoformat(node['uploadDate'].replace('Z', '+00:00')) assert published.tzinfo is not None, (route, 'video publication timezone') assert node['contentUrl'].startswith('https://media.protomota.com/video/'), (route, 'video plays from R2') assert not any('aggregateRating' in node or 'review' in node for node in graph), 'No fabricated reviews' for tag, attrs in page.links: for attribute in ['href','src','poster','data-poster']: url = attrs.get(attribute, '') if not url or urlsplit(url).scheme or url.startswith('//'): continue parsed = urlsplit(url); path = unquote(parsed.path) if tag == 'use': continue if not path: if parsed.fragment: assert parsed.fragment in page.ids, (route,url) continue if not path.startswith('/'): path = urlsplit(urljoin(origin + route, url)).path target = root / path.lstrip('/') if path == '/': target = root/'index.html' if path.endswith('/') and path != '/': target /= 'index.html' assert target.exists(), (route, 'missing asset/link',url) if parsed.fragment and path in pages: assert parsed.fragment in pages[path].ids, (route,url) urls = {node.text for node in ET.parse(root/'sitemap.xml').getroot().iter('{http://www.sitemaps.org/schemas/sitemap/0.9}loc')} assert urls == {'https://pulsecanvas.ai'+route for route in pages} robots = (root/'robots.txt').read_text() assert 'Disallow' not in robots and robots.count('Allow: /') == 2 for crawler in ['OAI-SearchBot', 'GPTBot', 'ClaudeBot', 'Claude-SearchBot', 'PerplexityBot', 'Google-Extended']: assert f'User-agent: {crawler}\n' in robots, crawler llms = (root/'llms.txt').read_text() assert llms.startswith('# PulseCanvas Studio\n') and 'https://pulsecanvas.ai/download/' in llms support = next(node for node in pages['/help/'].schema[0]['@graph'] if node['@type'] == 'FAQPage') assert len(support['mainEntity']) >= 3 and all(q['name'] and q['acceptedAnswer']['text'] for q in support['mainEntity']) assert 'Sitemap: https://pulsecanvas.ai/sitemap.xml' in robots assert {'/download/', '/help/'} <= pages.keys() print(f'{len(pages)} pages: unique metadata, social-image files/dimensions, JSON-LD assets, sitemap, crawler directives, image alt text, and internal links passed.')