"""Offline synthetic acceptance exercise. No DOM, network, accounts or real operations."""
from pathlib import Path
from datetime import datetime
from decimal import Decimal
import json,time
out=Path(__file__).parent
started=datetime.now().astimezone().isoformat(); tick=time.monotonic()

def pointer(events):
 pending=False;commits=0
 for event in events:
  if event=='down-inside': pending=True
  elif event=='cancel':pending=False
  elif event=='up-inside':
   if pending: commits+=1
   pending=False
  elif event=='up-outside':pending=False
 return commits

def shortcut(enabled,focus,scope,key,printable=True,remapped=False):
 if key not in {'s','j','ga'} or not enabled or not printable or remapped:return False
 return scope=='global' or focus==scope

def relative_help(order):
 i=order.index('help');return [x for x in order[:i] if x in ('intro','form','footer')]

def reuse(previous,process,step3=None,override=False):
 if previous['process']!=process:return 'no-cross-process-reuse'
 if not previous['valid']:return 'request-replacement'
 return step3 if override else previous['value']

cases=[]
def record(topic,name,actual,expected):
 cases.append(dict(topic=topic,case=name,actual=actual,expected=expected,passed=actual==expected))
record('pointer','P1',pointer(['down-inside','move-outside','up-outside']),0)
record('pointer','P2',pointer(['down-inside','up-inside']),1)
record('pointer','P3',pointer(['down-inside','cancel','up-inside']),0)
# Counterexample records a dispatch already committed before cancellation. The model cannot retract it.
record('pointer','P4-counterexample',1+pointer(['cancel','up-inside']),1)
record('shortcut','K1-risk',shortcut(True,'text','global','s'),True)
record('shortcut','K2-off',shortcut(False,'text','global','s'),False)
record('shortcut','K3-outside',shortcut(True,'text','files','j'),False)
record('shortcut','K3-focused',shortcut(True,'files','files','j'),True)
record('shortcut','K4-sequence-risk',shortcut(True,'text','global','ga'),True)
a=['intro','form','help','footer'];b=['intro','help','form','footer']
record('help','H1-relative',relative_help(a)==relative_help(['intro','form','optional-note','help','footer']),True)
record('help','H2-inconsistent',relative_help(a)==relative_help(b),False)
record('help','H3-mobile-same-variation',relative_help(b)==relative_help(['intro','optional-note','help','form','footer']),True)
p={'process':'P9','value':'Demo Studio','valid':True}
record('reuse','R1',reuse(p,'P9'),'Demo Studio')
p['value']='Demo Works'
record('reuse','R2-updated',reuse(p,'P9'),'Demo Works')
record('reuse','R2-explicit-override',reuse(p,'P9','Other Demo',True),'Other Demo')
record('reuse','R3',reuse(p,'P10'),'no-cross-process-reuse')
p['valid']=False
record('reuse','R4',reuse(p,'P9'),'request-replacement')
record('spacing','T1-numeric-inputs',[str(Decimal(16)*x) for x in [Decimal('1.5'),Decimal('2'),Decimal('.12'),Decimal('.16')]],['24.0','32','1.92','2.56'])
result={'startedLocal':started,'endedLocal':datetime.now().astimezone().isoformat(),'actualCheckElapsedSeconds':round(time.monotonic()-tick,3),'cases':cases,'passed':sum(x['passed'] for x in cases),'total':len(cases),'scope':'Finite synthetic event/policy/arithmetic model only; no real browser, screen reader, server, source-system changes or WCAG certification. K1/K4 True are intentionally detected risks, not accepted implementations. H4 absence needs separate assessment. Orientation and content loss remain manual checks.'}
# Results are printed; this downloaded exercise writes no files.
print(json.dumps({'passed':result['passed'],'total':len(cases),'liveIntegrationTested':False}));assert all(x['passed'] for x in cases)
