"""Fork tooling: any group may clone, parameterize, and track upstream (Article 7).""" from __future__ import annotations import shutil import pytest import yaml from tests.helpers import ( PROJECT_ROOT, create_fork, tree_text, upstream_status, upstream_status_fn, ) FORK_PARAMS = {"quorum": 0.77, "pool_cap": 12345} def _upstream(tmp_path): dest = tmp_path / "upstream" shutil.copytree(PROJECT_ROOT / "constitution", dest / "constitution") return dest def test_fork_copies_full_constitution(tmp_path): upstream = _upstream(tmp_path) dest = tmp_path / "household" create_fork(upstream, dest, name="household", params=FORK_PARAMS) upstream_kernel = sorted(p.name for p in (upstream / "constitution" / "kernel").glob("*.yaml")) fork_kernel = sorted(p.name for p in (dest / "constitution" / "kernel").glob("*.yaml")) assert fork_kernel == upstream_kernel assert (dest / "constitution" / "invariants.yaml").exists() def test_fork_writes_manifest_referencing_upstream(tmp_path): upstream = _upstream(tmp_path) dest = tmp_path / "household" create_fork(upstream, dest, name="household", params=FORK_PARAMS) manifest_found = False for candidate in list(dest.glob("*.y*ml")) + list(dest.glob("*.json")) + list( (dest / "constitution").glob("*.y*ml") ): text = candidate.read_text(encoding="utf-8") if "upstream" in text: manifest_found = True break assert manifest_found, "fork must record its upstream provenance in a manifest file" def test_fork_records_parameter_overrides(tmp_path): upstream = _upstream(tmp_path) dest = tmp_path / "household" create_fork(upstream, dest, name="household", params=FORK_PARAMS) everything = tree_text(dest) assert "12345" in everything, "parameter overrides must be applied or recorded in the fork" def test_fork_name_is_recorded(tmp_path): upstream = _upstream(tmp_path) dest = tmp_path / "dao-fork" create_fork(upstream, dest, name="orbital-dao", params={}) assert "orbital-dao" in tree_text(dest) def test_fork_kernel_content_matches_upstream(tmp_path): upstream = _upstream(tmp_path) dest = tmp_path / "household" create_fork(upstream, dest, name="household", params=FORK_PARAMS) for article in (upstream / "constitution" / "kernel").glob("*.yaml"): forked = dest / "constitution" / "kernel" / article.name assert yaml.safe_load(forked.read_text(encoding="utf-8")) == yaml.safe_load( article.read_text(encoding="utf-8") ), f"fork must not silently alter kernel article {article.name}" def test_upstream_drift_is_visible(tmp_path): if upstream_status_fn() is None: pytest.skip("no upstream-status function exposed by govtool.fork") upstream = _upstream(tmp_path) dest = tmp_path / "household" create_fork(upstream, dest, name="household", params={}) before = str(upstream_status(dest, upstream)) article = sorted((upstream / "constitution" / "kernel").glob("*.yaml"))[0] article.write_text( article.read_text(encoding="utf-8") + "\ntest_upstream_drift_marker: true\n", encoding="utf-8", ) after = str(upstream_status(dest, upstream)) assert before != after, "changing upstream must change the reported upstream status"