Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions tests/test_current_site_stylesheet_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,109 @@ class StylesheetOrder(unittest.TestCase):
def order(self, path):
return LINK.findall(path.read_text(encoding="utf-8"))

def live_publication(self, root, *, predecessor):
import test_current_site_history as history_controls
harness = history_controls.CurrentSiteHistoryTests()
harness.setUp()
public, payload = harness.prepare(root)
manifest = harness.publish(public)
files = harness.public_files(public)
if predecessor:
# Model the complete prior publication, not an archive-only alias.
# Its exact HTML differs solely in these two literal head links.
for row in manifest["current_observation"]["files"]:
name = row["path"]
current = files[name]
old = current.replace(
b'<link rel="stylesheet" href="../style.css"><link rel="stylesheet" href="../brand.css">',
b'<link rel="stylesheet" href="../brand.css"><link rel="stylesheet" href="../style.css">', 1)
self.assertNotEqual(old, current, name)
self.assertEqual(len(old), len(current))
files[name] = old
row["sha256"] = hashlib.sha256(old).hexdigest()
for alias in manifest["aliases"]:
alias["sha256"] = hashlib.sha256(files[alias["path"]]).hexdigest()
harness.rebind_manifest(files, manifest)
return harness, payload, files, manifest

def test_restore_predecessor_live_publication_then_build_new_style_first(self):
import current_site_history as history
import test_current_site_history as history_controls
with TemporaryDirectory() as tmp:
root = Path(tmp)
for name in ("public", "runner", "next"):
(root / name).mkdir()
harness, _, files, manifest = self.live_publication(root / "public", predecessor=True)
self.assertEqual(len(manifest["current_observation"]["files"]), 50)
archives = {n: b for n, b in files.items()
if n.startswith("snapshots/") and n != "snapshots/index.json"}
runner, payload = harness.prepare(root / "runner")
try:
history.restore_history(runner, history_controls.PublicFixtureReader(files),
expected_manifest_sha=hashlib.sha256(files["snapshots/index.json"]).hexdigest())
except ValueError as error:
self.fail(f"exact predecessor live publication must restore: {error}")
for row in manifest["current_observation"]["files"]:
self.assertEqual((runner / row["path"]).read_bytes(), files[row["path"]])
self.assertEqual(self.order(runner / row["path"]), ["brand.css", "style.css", "current.css"])
result = harness.build.run_build(runner, payload)
self.assertEqual(result.returncode, 0, result.stderr)
fresh = harness.publish(runner)
for row in fresh["current_observation"]["files"]:
self.assertEqual(self.order(runner / row["path"]), ["style.css", "brand.css", "current.css"])
for name, raw in archives.items():
self.assertEqual((runner / name).read_bytes(), raw, name)
next_site, _ = harness.prepare(root / "next")
history.restore_history(next_site, history_controls.PublicFixtureReader(harness.public_files(runner)))
for row in fresh["current_observation"]["files"]:
self.assertEqual((next_site / row["path"]).read_bytes(), (runner / row["path"]).read_bytes())
for name, raw in archives.items():
self.assertEqual((next_site / name).read_bytes(), raw, name)

def test_restore_live_compatibility_refuses_forged_or_incomplete_maps_without_writes(self):
import current_site_history as history
import test_current_site_history as history_controls
with TemporaryDirectory() as tmp:
root = Path(tmp)
(root / "public").mkdir()
harness, _, files, manifest = self.live_publication(root / "public", predecessor=True)
for case in ("non-style", "mixed", "missing", "unmatched"):
with self.subTest(case=case):
forged_files = dict(files)
forged = copy.deepcopy(manifest)
name = "nodes/beta.html"
if case == "non-style":
changed = files[name].replace(b"<code>HOLD</code>", b"<code>OPEN</code>", 1)
self.assertNotEqual(changed, files[name])
elif case == "mixed":
changed = files[name].replace(
b'<link rel="stylesheet" href="../brand.css"><link rel="stylesheet" href="../style.css">',
b'<link rel="stylesheet" href="../style.css"><link rel="stylesheet" href="../brand.css">', 1)
self.assertNotEqual(changed, files[name])
else:
changed = files[name]
if case == "missing":
forged["current_observation"]["files"] = [r for r in forged["current_observation"]["files"] if r["path"] != name]
forged["aliases"] = [r for r in forged["aliases"] if r["path"] != name]
forged_files.pop(name)
else:
forged_files[name] = changed
checksum = "e" * 64 if case == "unmatched" else hashlib.sha256(changed).hexdigest()
for row in forged["current_observation"]["files"] + forged["aliases"]:
if row["path"] == name:
row.update(bytes=len(changed), sha256=checksum)
forged["total_bytes"] = history.history_size(forged["archives"], forged["aliases"], forged["current_observation"])
forged["capacity"] = history.capacity_record(forged["archives"], forged["total_bytes"])
harness.rebind_manifest(forged_files, forged)
(root / case).mkdir()
runner, _ = harness.prepare(root / case)
before = harness.public_files(runner)
reason = "history alias union is incomplete" if case == "missing" else "live static reading does not match the exact observation"
with self.assertRaisesRegex(ValueError, reason):
history.restore_history(runner, history_controls.PublicFixtureReader(forged_files),
expected_manifest_sha=hashlib.sha256(forged_files["snapshots/index.json"]).hexdigest())
self.assertEqual(harness.public_files(runner), before)

def test_live_node_pages_load_style_before_brand(self):
pages = sorted((SITE / "nodes").glob("*.html"))
self.assertTrue(pages)
Expand Down
18 changes: 12 additions & 6 deletions tools/current_site_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,10 @@ def restore_history(site, reader=None, expected_manifest_sha=None):
raise ValueError("published current observation has different mathematical material")
ensure_render_budget(data, remote)
live = render_live_current_v2(data, remote)
if [descriptor(k, v) for k, v in sorted(live.items())] != manifest["current_observation"]["files"]:
# Restore the exact predecessor publication only as a complete map.
# New builds/publications keep the style-first renderer default.
live = render_live_current_v2(data, remote, style_first=False)
if [descriptor(k, v) for k, v in sorted(live.items())] != manifest["current_observation"]["files"]:
raise ValueError("live static reading does not match the exact observation")
historical = {}
Expand Down Expand Up @@ -1500,19 +1504,21 @@ def _live_v2_current_section(data, meta):
'</section>')


def _live_v2_static_page(data, meta, title, body):
def _live_v2_static_page(data, meta, title, body, *, style_first=True):
return (f'<!doctype html>\n{_V1_GENERATED}\n<html lang="en"><head><meta charset="utf-8">\n'
'<meta name="viewport" content="width=device-width, initial-scale=1">\n'
'<meta http-equiv="Content-Security-Policy" content="default-src \'none\'; style-src \'self\'; img-src \'self\'; script-src \'none\'; base-uri \'none\'; form-action \'none\'">\n'
f'<title>{_v1_escape(title)} · Current research dependency atlas</title>\n'
'<link rel="stylesheet" href="../style.css"><link rel="stylesheet" href="../brand.css"><link rel="stylesheet" href="../current.css">\n'
'</head><body><main class="node-static">\n'
+ ('<link rel="stylesheet" href="../style.css"><link rel="stylesheet" href="../brand.css"><link rel="stylesheet" href="../current.css">\n'
if style_first else
'<link rel="stylesheet" href="../brand.css"><link rel="stylesheet" href="../style.css"><link rel="stylesheet" href="../current.css">\n')
+ '</head><body><main class="node-static">\n'
'<nav aria-label="Research site"><a href="../index.html">Home</a> · <a href="../explore.html">Explore</a> · <a href="../research.html">Research</a> · <a href="../workspace.html#inventory">Library</a> · <a href="../dependencies.html#current-atlas">Current dependencies</a> · <a href="index.html">Current static graph index</a></nav>\n'
+ _live_v2_current_section(data, meta)
+ '\n' + body + '\n</main></body></html>\n').encode("utf-8")


def render_live_current_v2(data, meta):
def render_live_current_v2(data, meta, *, style_first=True):
"""Render current static routes from the full fresh schema-1 observation."""
graph = data["graph"]["data"]
pages = {}
Expand All @@ -1525,7 +1531,7 @@ def render_live_current_v2(data, meta):
+ '\n<h2>Original node metadata</h2><pre>'
+ _v1_escape(json.dumps(node, ensure_ascii=False, indent=2, sort_keys=True)) + '</pre>\n'
+ _v1_neighbors(data, node_id))
pages[f"nodes/{node_id}.html"] = _live_v2_static_page(data, meta, node_id, body)
pages[f"nodes/{node_id}.html"] = _live_v2_static_page(data, meta, node_id, body, style_first=style_first)
items = "\n".join(f'<li><a href="{_v1_escape(node_id)}.html">{_v1_escape(node_id)}</a> · {_v1_escape(node.get("kind", "not_recorded"))} · <code>{_v1_escape(node.get("classification", "not_recorded"))}</code></li>'
for node_id, node in graph["nodes"].items())
required = sum(edge["required"] for edge in graph["edges"])
Expand All @@ -1537,7 +1543,7 @@ def render_live_current_v2(data, meta):
f'<p>{len(data["sources"])} indexed files at the pinned repository commits. Legacy, quarantine, recovery, scratch and operational paths are excluded from this discovery population.</p>\n<ul>\n'
+ _v1_source_list(data["sources"]) + '\n</ul>\n'
+ _v2_equation_collection(data))
pages["nodes/index.html"] = _live_v2_static_page(data, meta, "Current static graph, equation excerpts and source index", body)
pages["nodes/index.html"] = _live_v2_static_page(data, meta, "Current static graph, equation excerpts and source index", body, style_first=style_first)
return pages


Expand Down