mirror of
https://github.com/AgriciDaniel/claude-seo.git
synced 2026-11-25 07:30:08 +01:00
Closes gap items #9 (LCP subparts), #11 (Speculation Rules / bfcache / prerender2), #18 (IndexNow), and #21 (Unlighthouse) from the v2 plan. scripts/preload_check.py - Audits Speculation Rules (inline + Speculation-Rules header), bfcache signals (Cache-Control: no-store, unload listener, beforeunload), deprecated <link rel="prerender">, and LCP preload hints (fetchpriority=high on hero img/video). - 0–100 score across 4 dimensions, 25 pts each. - Recommendation engine surfaces the highest-leverage fix per page. - LCP is the binding CWV constraint per Web Almanac 2025 (77% of mobile passes INP, only 62% passes LCP). Speculation Rules + bfcache are the cheapest large LCP wins without a content rewrite. scripts/indexnow_submit.py - Submits up to 10k URLs to the IndexNow umbrella endpoint (api.indexnow.org). Bing, Yandex, Seznam, Naver consume from a single POST. - Pre-flight: verifies the keyLocation file is published and matches the declared key (the #1 onboarding mistake users make). - Pre-flight: every URL flows through url_safety.validate_url_strict so we never ship a private-IP link to the API. - Cross-host guard (host must appear in every submitted URL). - 8–128 char key length per spec; 10k cap per batch. - Note: Google does NOT consume IndexNow (Gary Illyes, SOTR podcast). This is specifically a non-Google indexing signal. scripts/lcp_subparts.py - Decomposes p75 LCP into its four CrUX subparts: time_to_first_byte, resource_load_delay, resource_load_duration, element_render_delay - Identifies the dominant subpart (>= 40% of overall LCP) and emits a remediation hint matched to that subpart (TTFB -> origin/CDN, load_delay -> preload, load_duration -> srcset+AVIF/WebP, render_delay -> remove blocking CSS/JS above the fold). - Uses the same google_auth.get_api_key() path the rest of the Google API scripts use. scripts/unlighthouse_run.py - Thin wrapper around the MIT-licensed Unlighthouse CLI (unlighthouse.dev). Runs a multi-page Lighthouse audit on demand and ingests the ci-result.json summary. - npx-based invocation pinned to unlighthouse-cli@^0.13. - url_safety pre-flight + Node-availability check; both surface as clean errors rather than subprocess crashes. tests/test_technical_depth.py (17 cases) - preload_check: speculation inline + header detection, deprecated prerender flag, bfcache killers (no-store + unload), LCP fetchpriority detection, score 100 on well-optimised page, score ≤ 50 on bfcache-killing page. - indexnow_submit: empty URL list, key-too-short, cross-host URLs, 10k batch cap, private-IP refusal via url_safety. - lcp_subparts: SSRF guard, missing-API-key path. - unlighthouse_run: SSRF guard, Node-missing path. Live CrUX + real Node subprocess paths are deferred to manual smoke tests; the unit tests cover every validation branch + every public function's failure modes. .github/workflows/ci.yml - Added the 4 new scripts to py_compile (39 -> 43 scripts). Test count: 174 -> 191 (+17). Full suite green. Local-only commit on v2. No push. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
176 lines
6.4 KiB
Python
176 lines
6.4 KiB
Python
"""
|
|
Tests for the v2 Checkpoint 3 technical-depth scripts.
|
|
|
|
Coverage:
|
|
scripts/preload_check.py — Speculation Rules / bfcache / preload
|
|
scripts/indexnow_submit.py — payload validation + key verification
|
|
scripts/lcp_subparts.py — pure SSRF guard + result-shape (no live CrUX)
|
|
scripts/unlighthouse_run.py — SSRF guard + Node-availability check
|
|
|
|
lcp_subparts and unlighthouse are heavy network/process integrations;
|
|
we test their non-network paths only. Real-API integration is
|
|
deferred to manual smoke tests.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
_SCRIPTS = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "scripts")
|
|
if _SCRIPTS not in sys.path:
|
|
sys.path.insert(0, _SCRIPTS)
|
|
|
|
import preload_check # noqa: E402
|
|
import indexnow_submit # noqa: E402
|
|
import lcp_subparts # noqa: E402
|
|
import unlighthouse_run # noqa: E402
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# preload_check
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_preload_detects_speculation_rules_inline_block() -> None:
|
|
html = '<script type="speculationrules">{"prefetch":[{"urls":["/next"]}]}</script>'
|
|
result = preload_check.analyse(html, {})
|
|
assert result["speculation_rules"]["inline_blocks"] == 1
|
|
assert "prefetch" in result["speculation_rules"]["actions"]
|
|
|
|
|
|
def test_preload_detects_speculation_rules_header() -> None:
|
|
result = preload_check.analyse("", {"Speculation-Rules": "/rules.json"})
|
|
assert result["speculation_rules"]["header_present"] is True
|
|
|
|
|
|
def test_preload_detects_deprecated_prerender_link() -> None:
|
|
html = '<link rel="prerender" href="/next">'
|
|
result = preload_check.analyse(html, {})
|
|
assert result["prerender_links"] == 1
|
|
assert any("deprecated" in r.lower() for r in result["recommendations"])
|
|
|
|
|
|
def test_preload_detects_bfcache_killer_no_store() -> None:
|
|
result = preload_check.analyse("<html></html>", {"Cache-Control": "no-store"})
|
|
assert result["bfcache_signals"]["cache_control_no_store"] is True
|
|
assert any("no-store" in r for r in result["recommendations"])
|
|
|
|
|
|
def test_preload_detects_bfcache_killer_unload_listener() -> None:
|
|
html = "<script>addEventListener('unload', () => {});</script>"
|
|
result = preload_check.analyse(html, {})
|
|
assert result["bfcache_signals"]["unload_listener"] is True
|
|
|
|
|
|
def test_preload_detects_lcp_fetchpriority_high() -> None:
|
|
html = '<img src="/hero.webp" fetchpriority="high">'
|
|
result = preload_check.analyse(html, {})
|
|
assert result["lcp_resource_hints"]["preload_lcp_candidate"] is True
|
|
assert result["lcp_resource_hints"]["fetchpriority_high"] >= 1
|
|
|
|
|
|
def test_preload_score_100_on_well_optimised_page() -> None:
|
|
html = (
|
|
'<script type="speculationrules">{"prefetch":[{"urls":["/next"]}]}</script>'
|
|
'<link rel="preload" as="image" href="/hero.webp" fetchpriority="high">'
|
|
'<img src="/hero.webp" fetchpriority="high">'
|
|
)
|
|
result = preload_check.analyse(html, {})
|
|
assert result["score"] == 100
|
|
assert result["recommendations"] == []
|
|
|
|
|
|
def test_preload_score_low_on_bfcache_killer() -> None:
|
|
html = "<script>addEventListener('unload', () => {});</script>"
|
|
result = preload_check.analyse(html, {"Cache-Control": "no-store"})
|
|
# Loses points for: no speculation rules, no fetchpriority, two bfcache
|
|
# killers, but gains 25 for no deprecated prerender = ~25/100.
|
|
assert result["score"] <= 50
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# indexnow_submit — validation only (no network)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_indexnow_rejects_empty_url_list() -> None:
|
|
result = indexnow_submit.submit("example.com", "k" * 32,
|
|
"https://example.com/k.txt", [])
|
|
assert result["ok"] is False
|
|
assert "empty" in result["error"]
|
|
|
|
|
|
def test_indexnow_rejects_key_too_short() -> None:
|
|
result = indexnow_submit.submit("example.com", "short",
|
|
"https://example.com/k.txt",
|
|
["https://example.com/a"])
|
|
assert result["ok"] is False
|
|
assert "key must be" in result["error"]
|
|
|
|
|
|
def test_indexnow_rejects_cross_host_urls() -> None:
|
|
result = indexnow_submit.submit(
|
|
"example.com", "k" * 32, "https://example.com/k.txt",
|
|
["https://example.com/ok", "https://other.example/bad"],
|
|
)
|
|
assert result["ok"] is False
|
|
assert "host" in result["error"]
|
|
|
|
|
|
def test_indexnow_rejects_batch_above_spec_cap() -> None:
|
|
big = [f"https://example.com/p{i}" for i in range(10001)]
|
|
result = indexnow_submit.submit(
|
|
"example.com", "k" * 32, "https://example.com/k.txt", big,
|
|
)
|
|
assert result["ok"] is False
|
|
assert "10000" in result["error"]
|
|
|
|
|
|
def test_indexnow_rejects_private_ip_in_url_list() -> None:
|
|
result = indexnow_submit.submit(
|
|
"10.0.0.1", "k" * 32, "https://10.0.0.1/k.txt",
|
|
["http://10.0.0.1/admin"],
|
|
)
|
|
assert result["ok"] is False
|
|
assert "url_safety" in result["error"]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# lcp_subparts — SSRF + missing-key paths
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_lcp_subparts_blocks_ssrf() -> None:
|
|
result = lcp_subparts.analyse("http://10.0.0.1/")
|
|
assert "error" in result
|
|
assert "url_safety" in result["error"]
|
|
|
|
|
|
def test_lcp_subparts_reports_missing_api_key() -> None:
|
|
with patch.object(lcp_subparts, "get_api_key", return_value=None):
|
|
result = lcp_subparts.analyse("https://example.com/")
|
|
assert "error" in result
|
|
assert "API key" in result["error"]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# unlighthouse_run — SSRF + Node-missing paths
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_unlighthouse_blocks_ssrf() -> None:
|
|
result = unlighthouse_run.run("http://192.168.1.1/")
|
|
assert result["ok"] is False
|
|
assert "url_safety" in result["error"]
|
|
|
|
|
|
def test_unlighthouse_reports_missing_node(monkeypatch) -> None:
|
|
monkeypatch.setattr(unlighthouse_run.shutil, "which", lambda _name: None)
|
|
result = unlighthouse_run.run("https://example.com/")
|
|
assert result["ok"] is False
|
|
assert "npx" in result["error"].lower() or "node" in result["error"].lower()
|