Files
WoW-Hybrid-gateway/ops/native_vendor_buy_test.py
praxisandClaude Fable 5.2 8a24c445b0 Content pipeline and operator tools
367 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

63 lines
5.2 KiB
Python

#!/usr/bin/env python3
"""One original merchant UI purchase, checked against real local server state.
No fixture grant, SQL write, packet injection or purchase retry. Inspect the
merchant and supply its current price/coordinates before invoking this script.
"""
import argparse,json
from pathlib import Path
from client_session import Session
import client_ui as ui
from local_tortoise import db
from native_engine_character_test import ROOT,Controller,local_listeners
from native_test_guard import require_local_turtle_ports,require_authenticated_turtle_realm,require_native_gameplay_policy
from native_world_trace import WorldTrace
def inventory(character):
return db(f'SELECT ci.bag,ci.slot,ci.item,ii.itemEntry,ii.count FROM tw_char.character_inventory ci JOIN tw_char.item_instance ii ON ci.item=ii.guid WHERE ci.guid={character} ORDER BY ci.bag,ci.slot;')
def rows(text):return [r.split('\t') for r in text.splitlines()]
def count(items,entry):return sum(int(r[4]) for r in items if int(r[3])==entry and not(r[0]=='0' and 69<=int(r[1])<81))
def main():
p=argparse.ArgumentParser(description=__doc__)
for name in ('character','entry','units','price','money','x','y'):p.add_argument('--'+name,type=int,required=True)
p.add_argument('--output',type=Path,required=True);a=p.parse_args();out=a.output.resolve()
if not out.is_relative_to(ROOT/'runtime') or out.exists() or not out.parent.is_dir() or any(not 0<v<2**32 for v in (a.character,a.entry,a.units,a.price,a.money)) or not(0<a.x<1920 and 0<a.y<1080):p.error('Require new runtime output and bounded explicit purchase baseline')
require_local_turtle_ports(local_listeners());c=Controller(int((ROOT/'runtime/native-client.pid').read_text()));require_authenticated_turtle_realm(c.rpc('inspect-wire-account'));require_native_gameplay_policy(c.rpc('inspect-native-channel-policy'))
baseline=inventory(a.character);before=rows(baseline);money=db(f'SELECT money FROM tw_char.characters WHERE guid={a.character};')
if money!=str(a.money):raise RuntimeError('Saved money differs; no action taken')
prototype=db(f'SELECT buy_count,buy_price FROM tw_world.item_template WHERE entry={a.entry};')
if not prototype or a.units%int(prototype.split('\t')[0]):raise RuntimeError('Source vendor bundle differs; no action taken')
report=dict(pid=c.pid,external_gateway_running=False,entry=a.entry,units=a.units,expected_price=a.price,money_before=a.money,source_template=prototype,before_inventory=baseline,purchase_attempted=False,verified=False);failure=None;old=ui.KEY_DELAY
with WorldTrace(c) as trace:
try:
with Session(c.pid) as s:
if s.state()!='world':raise RuntimeError('Require actual native world')
ui.KEY_DELAY=.03;s.shot(out.with_name(out.stem+'-before.png'));report['purchase_attempted']=True
ui.click_at(s.d,s.win,a.x,a.y,ui.ecodes.BTN_RIGHT)
try:report['native_request']=trace.wait(2,0x3f003b,3)
except RuntimeError as error:
if str(error)!='Bounded native opcode observation expired; do not repeat a state change without inspection':raise
# The bounded32-row metadata ring can overwrite the request
# during a burst even when both genuine result rows remain.
# Never repeat a purchase to repair telemetry. Require the
# real result + delivery + wallet/UI checks below instead.
report['outgoing_metadata_missing']=True
report['source_success']=trace.wait(1,0x1a4,5);report['native_success_queued']=trace.wait(3,0x470172,5)
s.macro('/s .save')
report['money_after']=c.wait(lambda:(v if (v:=db(f'SELECT money FROM tw_char.characters WHERE guid={a.character};'))==str(a.money-a.price) else False),8)
report['after_inventory']=inventory(a.character);after=rows(report['after_inventory'])
if count(after,a.entry)!=count(before,a.entry)+a.units or [r for r in before if int(r[3])!=a.entry]!=[r for r in after if int(r[3])!=a.entry]:raise RuntimeError('Authoritative delivery/original inventory differs; inspect before retry')
s.macro('/run print("GW vendor buy result",GetMoney())');s.shot(out.with_suffix('.png'))
pipe=c.rpc('inspect-engine-pipe');report.update(pipe_error=pipe['error'],pipe_closed=pipe['closed'],verified=not pipe['error'] and not pipe['closed'],ui_review_required=True)
except Exception as error:
failure=type(error).__name__+': '+str(error)
report['saved_money_after_failure']=db(f'SELECT money FROM tw_char.characters WHERE guid={a.character};');report['saved_inventory_after_failure']=inventory(a.character)
finally:ui.KEY_DELAY=old
report.update(trace.report())
if failure:report['failure']=failure
out.write_text(json.dumps(report,indent=2)+'\n');out.chmod(0o600)
print(json.dumps({k:report.get(k) for k in ('pid','native_request','source_success','native_success_queued','money_before','money_after','verified','lost_metadata_rows','observer_error','failure')}))
return 0 if report['verified'] and not trace.error else 2
if __name__=='__main__':raise SystemExit(main())