import hashlib,json,sys
from pathlib import Path
sys.path.insert(0,str(Path.cwd()))
from tests.support import load_contract
from premier_demo.client import PremierClient
from premier_demo.contract import ContractError
contract=load_contract()
class CapturedReply(PremierClient):
    def __init__(self,reply):
        super().__init__(contract,'http://127.0.0.1:1','ORG')
        self.reply=reply
        self.sends=0
    def _send(self,key,header,body):
        self.sends+=1
        return 200,self.reply
rows=[]
for label,status in [('documented-comment',{'StatusCode':1120,'Severity':'Info'}),('missing-code',{'Severity':'Info'}),('authored-unrecognized-code',{'StatusCode':777777,'Severity':'Warning'}),('error-control',{'StatusCode':777777,'Severity':'Error'})]:
    reply={'Status':status}
    try:
        contract.validate_response('party',200,reply)
        schema='accepted'
    except ContractError:
        schema='rejected'
    client=CapturedReply(reply)
    try:
        result=client.party_accounts('P1')
    except Exception as exc:
        result={'exception':type(exc).__name__,'reason':str(exc)}
    rows.append({'label':label,'authoredReply':reply,'realPinnedSchema':schema,'actualPartyAccountsResult':result,'simulatedSends':client.sends})
print(json.dumps({'sourceRevision':'b5a308ecd9acdc795ce56260ec81c21c879aa9c7','clientSha256':hashlib.sha256(Path('premier_demo/client.py').read_bytes()).hexdigest(),'scope':'Authored response substitution through original client and actual pinned Premier contract; real request/header/response validation; no socket, vendor service, model, voice, or bank call. This is a counterexample, not a recorded service response.','rows':rows},indent=2),flush=True)
assert all(row['realPinnedSchema']=='rejected' for row in rows)
assert all(row['actualPartyAccountsResult']==[] for row in rows[:3])
assert rows[3]['actualPartyAccountsResult']=={'exception':'Unavailable','reason':'invalid_response'}
assert all(row['simulatedSends']==1 for row in rows)
