"""End-to-end vote gate: one amendment passes, two are blocked. This is the heart of the milestone: proposal -> ballots -> eligibility -> quorum -> threshold -> gate verdict, all against the real shipped constitution. Optimism in the defaults, paranoia in the tests. """ from __future__ import annotations from tests.helpers import ( cast_ballot, create_proposal, gate_passed, proposal_id_of, run_gate, ) NEW_USERLAND_MODULE = """\ id: pet-policy title: Household Pet Policy version: 0.1.0 params: max_pets: 3 rules: - id: pp-1 text: No citizen may keep more than max_pets pets without a minor amendment. - id: pp-2 text: Pet disputes are resolved by the interpretation procedure of kernel article 10. """ def _kernel_change(repo): article = sorted((repo / "constitution" / "kernel").glob("*.yaml"))[0] rel = article.relative_to(repo).as_posix() new_text = article.read_text(encoding="utf-8") + "\ntest_amendment_marker: supermajority-bypass-attempt\n" return {rel: new_text} def test_userland_amendment_passes_the_gate(repo, citizens): prop = create_proposal( repo, proposer_id=citizens[0]["id"], title="Adopt the household pet policy module", changes={"constitution/userland/pet-policy.yaml": NEW_USERLAND_MODULE}, ) pid = proposal_id_of(prop) ballots = [] for citizen in citizens[:4]: ballots.append(cast_ballot(citizen, pid, "yes", repo=repo)) ballots.append(cast_ballot(citizens[4], pid, "no", repo=repo)) # citizens[5] does not vote: turnout 5/6 ≈ 83%, support 4/5 = 80%. result = run_gate(repo, pid, ballots=ballots) assert gate_passed(result), f"a 4-1 userland amendment with 83% turnout must ratify; got {result!r}" def test_kernel_amendment_without_supermajority_is_blocked(repo, citizens): prop = create_proposal( repo, proposer_id=citizens[0]["id"], title="Weaken the voting article", changes=_kernel_change(repo), ) pid = proposal_id_of(prop) ballots = [] for citizen in citizens[:3]: ballots.append(cast_ballot(citizen, pid, "yes", repo=repo)) for citizen in citizens[3:5]: ballots.append(cast_ballot(citizen, pid, "no", repo=repo)) # 3 yes / 2 no = 60% support: a simple majority, but a kernel (major) # change requires a supermajority. The gate must block it. result = run_gate(repo, pid, ballots=ballots) assert not gate_passed(result), ( f"a kernel change with only 60% support must be blocked by the supermajority rule; got {result!r}" ) def test_amendment_without_quorum_is_blocked(repo, citizens): prop = create_proposal( repo, proposer_id=citizens[0]["id"], title="Quietly adopt a module while nobody is voting", changes={"constitution/userland/sneaky-module.yaml": "id: sneaky\ntitle: Sneaky module\n"}, ) pid = proposal_id_of(prop) ballots = [cast_ballot(citizens[0], pid, "yes", repo=repo)] # One unanimous-yes ballot out of six citizens: 17% turnout. Whatever # the support percentage says, quorum is not met and the gate must block. result = run_gate(repo, pid, ballots=ballots) assert not gate_passed(result), ( f"a single ballot out of six citizens cannot ratify anything; got {result!r}" ) def test_gate_verdicts_are_deterministic(repo, citizens): prop = create_proposal( repo, proposer_id=citizens[0]["id"], title="Determinism check", changes={"constitution/userland/determinism-module.yaml": "id: determinism\ntitle: Determinism\n"}, ) pid = proposal_id_of(prop) ballots = [cast_ballot(c, pid, "yes", repo=repo) for c in citizens] first = gate_passed(run_gate(repo, pid, ballots=ballots)) second = gate_passed(run_gate(repo, pid, ballots=ballots)) assert first == second, "running the gate twice on identical inputs must agree" assert first is True, "a unanimous yes with full turnout must ratify"