Files
WoW-Hybrid-gateway/tests/native_barber_shop_test.py
2026-11-23 03:28:32 +02:00

145 lines
10 KiB
Python

"""Native-styled barbershop window: Octo TW_BARBERSHOP session contract, labels, option wrap and close ownership."""
from pathlib import Path
import subprocess
ROOT=Path(__file__).resolve().parents[1]
lua=r'''
local sent, errors, handlers, prefixes = {}, {}, {}, {}
local function widget()
local w={shown=false,scripts={}}
local mt={}
mt.__index=function(t,k)
if k=='SetPropagateKeyboardInput' then return function(self,on)self.propagate=on end end
if k=='GetPropagateKeyboardInput' then return function(self)return self.propagate==true end end
if k=='SetShown' then return function(self,on)self.shown=on end end
if k=='SetScript' then return function(self,name,fn)self.scripts[name]=fn end end
if k=='Show' then return function(self)if not self.shown then self.shown=true;if self.scripts.OnShow then self.scripts.OnShow(self)end end end end
if k=='Hide' then return function(self)if self.shown then self.shown=false;if self.scripts.OnHide then self.scripts.OnHide(self)end end end end
if k=='IsShown' then return function(self)return self.shown end end
if k=='SetText' then return function(self,text)self.text=text end end
if k=='SetEnabled' then return function(self,on)self.enabled=on end end
if k=='CreateFontString' then return function()return widget()end end
if k=='GetFacing' then return function()return 0 end end
if k=='SetUnit' then return function(self,unit)self.unit=unit;self.units=(self.units or 0)+1 end end
if k=='UndressSlot' then return function(self,slot)self.undressed=slot end end
if k=='StartMoving' or k=='StopMovingOrSizing' then return function()end end
if type(k)=='string' and (k:find('^Set') or k:find('^Register') or k:find('^Enable') or k:find('^Clear')) then return function()end end
return nil
end
return setmetatable(w,mt)
end
local eventFrame, keys, sendResult = nil, nil, 0
function CreateFrame(kind,name,parent,template)
local w=widget();w.kind=kind;w.template=template
if name then _G[name]=w end
if template=='ButtonFrameTemplate' then w.Inset=widget();w.SetTitle=function(self,t)self.title=t end;w.SetPortraitToUnit=function(self,u)self.portrait=u end end
if kind=='Frame' and not name and not parent then eventFrame=w end
if template=='InsecureKeyboardInputPropagatorTemplate' then w.propagate=true;if not keys then keys=w end end
return w
end
function RegisterGameMenuEscHandler() error("Addon registered in the stock Escape dispatcher") end
function InCombatLockdown() return false end
function GetBindingFromClick() return "" end
function StaticPopup_IsAnyDialogShown() return false end
function SpellIsTargeting() return false end
function GetCurrentKeyBoardFocus() return nil end
UIParent=widget();UISpecialFrames={};UIErrorsFrame={AddMessage=function(_,text)errors[#errors+1]=text end}
local race,sex,money='Tauren',2,1000000
function UnitRace()return 'x',race end
function UnitSex()return sex end
function UnitName()return 'Tester' end
function GetMoney()return money end
function GetMoneyString(amount)return 'money:'..amount end
function GetCursorPosition()return 0,0 end
RED_FONT_COLOR={WrapTextInColorCode=function(_,text)return 'red:'..text end}
C_ChatInfo={RegisterAddonMessagePrefix=function(p)prefixes[#prefixes+1]=p end,
SendAddonMessage=function(prefix,text,channel,target)sent[#sent+1]={prefix,text,channel,target}; return sendResult end}
dofile(arg[1]);local shop=WoWHybridUI.barberShop;local session=shop.session
assert(prefixes[1]=='TW_BARBERSHOP' and #sent==0) -- nothing is sent before the server opens a session
local function server(text)eventFrame.scripts.OnEvent(eventFrame,'CHAT_MSG_ADDON','TW_BARBERSHOP',text,'GUILD','Tester')end
local function last()return sent[#sent] and sent[#sent][2] end
-- Labels and counts follow the Octo client table.
assert(shop.Label(1,'Tauren',1)=='Horn Style' and shop.Label(2,'Tauren',1)=='Horn Color' and shop.Label(3,'Tauren',1)=='Facial Hair')
assert(shop.Label(3,'Tauren',2)=='Hair' and shop.Label(3,'NightElf',2)=='Markings' and shop.Label(3,'Gnome',2)=='Earrings')
assert(shop.Label(3,'Human',2)=='Piercings' and shop.Label(3,'Scourge',1)=='Features' and shop.Label(3,'Troll',2)=='Tusks')
assert(shop.Label(1,'Orc',1)=='Hair Style' and shop.Label(2,'Orc',1)=='Hair Color')
assert(shop.Total(1,'Tauren',1)==13 and shop.Total(2,'Tauren',1)==9 and shop.Total(3,'Tauren',1)==9 and shop.Total(1,'Human',2)==26)
assert(shop.Total(1,'Pandaren',1)==nil)
local kind,fields=shop.Parse('S2C_OPEN_BARBER;0;7;;0;;1;');assert(kind=='S2C_OPEN_BARBER' and #fields==7 and fields[2]=='7' and fields[3]=='')
-- Requests are refused while no session is open; foreign prefixes and channels are ignored.
assert(not shop.Step(1,1) and not shop.Purchase() and not shop.Reset() and #sent==0)
eventFrame.scripts.OnEvent(eventFrame,'CHAT_MSG_ADDON','TW_SHOP','S2C_OPEN_BARBER;0;7;;0;;1;','GUILD')
eventFrame.scripts.OnEvent(eventFrame,'CHAT_MSG_ADDON','TW_BARBERSHOP','S2C_OPEN_BARBER;0;7;;0;;1;','WHISPER')
assert(not session.active and not WoWHybridBarberShopFrame)
-- Open: the owner's Orgrimmar reference (Tauren male, Horn Style 8/13, Horn Color 1/9, Facial Hair 2/9).
server('S2C_OPEN_BARBER;0;7;;0;;1;')
local frame=WoWHybridBarberShopFrame
assert(session.active and frame.shown and frame.template=='ButtonFrameTemplate' and frame.title=='Barbershop')
assert(frame.portrait=='player' and frame.Model.unit=='player' and frame.Model.undressed==1 and #UISpecialFrames==0 and keys.shown and keys.propagate)
assert(frame.Rows[1].Label.text=='Horn Style 8/13' and frame.Rows[2].Label.text=='Horn Color 1/9' and frame.Rows[3].Label.text=='Facial Hair 2/9')
assert(frame.Okay.enabled==false and frame.Reset.enabled==false and frame.Cost.text=='money:0')
-- A step only asks the server (0-based index); the reply moves the selection and sets the price.
frame.Rows[1].Next.scripts.OnClick()
assert(last()=='C2S_PREVIEW_STYLE;8' and sent[#sent][3]=='WHISPER' and sent[#sent][4]=='Tester' and frame.Rows[1].Label.text=='Horn Style 8/13')
server('S2C_UPDATE_PREVIEW;42340;8;0;1')
assert(frame.Rows[1].Label.text=='Horn Style 9/13' and frame.Cost.text=='money:42340' and frame.Okay.enabled and frame.Reset.enabled)
frame.Rows[2].Previous.scripts.OnClick();assert(last()=='C2S_PREVIEW_COLOR;8') -- wraps 1 -> 9
frame.Rows[3].Next.scripts.OnClick();assert(last()=='C2S_PREVIEW_FACIAL_HAIR;2')
server('S2C_UPDATE_PREVIEW;74095;8;1;2');session.selected[1]=13;frame.Rows[1].Next.scripts.OnClick();assert(last()=='C2S_PREVIEW_STYLE;0')
session.selected[1]=9
money=100;eventFrame.scripts.OnEvent(eventFrame,'PLAYER_MONEY');assert(frame.Cost.text=='red:money:74095');money=1000000
-- The preview redraws the model when the server changes the character.
local draws=frame.Model.units;eventFrame.scripts.OnEvent(eventFrame,'UNIT_MODEL_CHANGED','target');assert(frame.Model.units==draws)
eventFrame.scripts.OnEvent(eventFrame,'UNIT_MODEL_CHANGED','player');assert(frame.Model.units==draws+1)
-- Failed purchase keeps the state; success makes the selection the saved look.
frame.Okay.scripts.OnClick();assert(last()=='C2S_PURCHASE')
server('S2C_ERROR;2');assert(errors[#errors]=='You do not have enough money.' and frame.Okay.enabled)
server('S2C_PURCHASE_RESULT;0;8;1;2');assert(frame.Okay.enabled)
server('S2C_PURCHASE_RESULT;1;8;1;2');assert(frame.Okay.enabled==false and frame.Cost.text=='money:0' and session.old[1]==9 and session.old[2]==2)
-- Reset returns to the saved look locally and tells the server.
server('S2C_UPDATE_PREVIEW;21170;8;2;2')
sendResult=3 -- locally throttled; the server cannot reset or purchase this preview
local lastErrorCount=#errors
assert(not shop.Step(1,1) and not shop.Purchase())
frame.Reset.scripts.OnClick()
assert(#errors==lastErrorCount+3 and frame.Rows[2].Label.text=='Horn Color 3/9')
assert(frame.Cost.text=='money:21170' and frame.Okay.enabled and frame.Reset.enabled)
sendResult=0
frame.Reset.scripts.OnClick()
assert(last()=='C2S_RESET' and frame.Rows[2].Label.text=='Horn Color 2/9' and frame.Okay.enabled==false)
-- Cancel/Escape ends the session once; the server's close afterwards is harmless.
local count=#sent;keys.scripts.OnKeyDown(keys,"W");assert(keys.propagate and frame:IsShown());keys.scripts.OnKeyDown(keys,"ESCAPE");assert(not keys.propagate and not frame:IsShown())
assert(not frame.shown and last()=='C2S_CLOSE' and #sent==count+1 and not session.active)
server('S2C_CLOSE_BARBER');assert(#sent==count+1)
-- Server-side close hides the window without another request; a second open works.
server('S2C_OPEN_BARBER;0;8;;1;;2;');assert(frame.shown and frame.Rows[1].Label.text=='Horn Style 9/13')
count=#sent;server('S2C_CLOSE_BARBER');assert(not frame.shown and not session.active and #sent==count)
-- A stale server session (window gone after a reload) is closed on error 3 only.
server('S2C_ERROR;3');assert(last()=='C2S_CLOSE' and #sent==count+1)
server('S2C_ERROR;1');assert(#sent==count+1 and errors[#errors]=='You are not currently in a barbershop session.')
-- The server barbershop exchanges the original ordinals for all three editable
-- axes. Exercise each selector and its wrap at both ends for both model sexes.
for _, fixture in ipairs({{'Goblin',2,14,5,10},{'Goblin',3,17,5,5},
{'BloodElf',2,16,16,11},{'BloodElf',3,20,16,11}}) do
race,sex=fixture[1],fixture[2]
server('S2C_OPEN_BARBER;0;0;;0;;0;')
for axis,command in ipairs({'C2S_PREVIEW_STYLE','C2S_PREVIEW_COLOR','C2S_PREVIEW_FACIAL_HAIR'}) do
local total=fixture[axis+2]
for index=0,total-1 do
local values={0,0,0};values[axis]=index
server(('S2C_UPDATE_PREVIEW;1;%d;%d;%d'):format(values[1],values[2],values[3]))
frame.Rows[axis].Next.scripts.OnClick()
assert(last()==command..';'..((index+1)%total))
frame.Rows[axis].Previous.scripts.OnClick()
assert(last()==command..';'..((index-1)%total))
end
end
server('S2C_CLOSE_BARBER');assert(not session.active)
end
-- Unknown race: no total shown, no wrap below the first option.
race='Pandaren';server('S2C_OPEN_BARBER;0;0;;0;;0;');assert(frame.Rows[1].Label.text=='Hair Style 1')
count=#sent;frame.Rows[1].Previous.scripts.OnClick();assert(#sent==count)
frame.Rows[1].Next.scripts.OnClick();assert(last()=='C2S_PREVIEW_STYLE;1')
'''
subprocess.run(['lua','-',str(ROOT/'client_addons/WoWHybridUI/NativeBarberShop.lua')],input=lua.encode(),check=True)
print('Native barbershop window: server-owned session/preview/price/purchase, original labels and counts, close ownership and stale-session recovery PASS.')