"""Spine L1 gate: every mutating route must write a ledger row."""
import pytest

MUTATING = {"POST", "PUT", "PATCH", "DELETE"}
ALLOWLIST_READONLY = set()


@pytest.fixture(scope="module")
def app():
    from app import app as flask_app
    return flask_app


def _mutating(app):
    return {r.rule for r in app.url_map.iter_rules()
            if r.methods & MUTATING and r.endpoint != "static"}


def _audited(app):
    return {r.rule for r in app.url_map.iter_rules()
            if r.endpoint != "static"
            and getattr(app.view_functions.get(r.endpoint), "_audited_action", None)}


def test_every_mutating_route_writes_the_ledger(app):
    missing = _mutating(app) - _audited(app) - ALLOWLIST_READONLY
    assert missing == set(), (
        f"{len(missing)} mutating routes bypass the ledger:\n"
        + "\n".join(sorted(missing))
    )
