Files
WoW-Hybrid-gateway/tests/editor_ui_launcher_test.py

112 lines
6.5 KiB
Python

import copy
import datetime
import gzip
import json
from pathlib import Path
import sqlite3
import subprocess
import sys
import tempfile
import unittest
from unittest.mock import patch
ROOT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(ROOT/'ops/editor'))
import launcher
import session_capture as captures
import spawn_undo as undo
from editor_spawn_export_test import Export, state
from editor_spawn_undo_test import Undo
def snapshot(at):
data=state();data['captured_utc']=datetime.datetime.fromtimestamp(at,datetime.timezone.utc).isoformat();return data
def operation(kind,guid='1',entry='100',at=1030,capture_id=None):
return {'format':2,'session':'fixture','operations':{1:{'kind':kind,'table':'creature','guid':guid,'entry':entry,'at':at,
'capture_id':capture_id or 'capture-00000000001000000000.json.gz'}},'rows':{},'lastChange':{},'log':{}}
class Editor(unittest.TestCase):
def setup_store(self,root,data=None):
addon=root/'addon';addon.mkdir();out=root/'session'
launcher.session_setup(out,addon,{'character':'EDITOR','guid':'77'},data or snapshot(1000),{'creature':{'100':'Wolf'},'gameobject':{}},session='fixture')
return out,addon
def test_lua_and_saved_variables(self):
with tempfile.TemporaryDirectory() as d:
export=Path(d)/'saved.lua'
subprocess.run(['lua5.1',str(ROOT/'tests/editor_ui_test.lua'),str(ROOT/'client_addons/OctoEditor/EditorCore.lua'),str(export)],check=True)
saved=undo.read_saved_variables(export.read_text())
self.assertEqual(saved['format'],2);self.assertEqual(len(undo.array(saved['operations'])),6)
self.assertEqual(undo.array(undo.array(saved['rows'])[0]['values'])[2],'1.2345678901234567')
subprocess.run(['luac5.1','-p',str(ROOT/'client_addons/OctoEditor/EditorUI.lua')],check=True)
def test_identity_gate(self):
def account_with_rank(query):
self.assertIn('a.rank',query)
self.assertNotIn('a.security',query)
return 'EDITFIXTURE\t3\t77\t1\n'
good=launcher.editor_identity(account_with_rank)
self.assertEqual(good['guid'],'77')
for reply in ['', 'PROXYONE\t5\t1\t1\n','Blesisi\t5\t1\t1\n','TEST\t2\t1\t1\n','TEST\t5\t1\t2\n']:
with self.assertRaises(ValueError):launcher.editor_identity(lambda _,r=reply:r)
def test_capture_full_rows_and_lua_data(self):
with tempfile.TemporaryDirectory() as d:
out,addon=self.setup_store(Path(d));latest=captures.newest(out/'snapshots','creature','1',1001)
self.assertEqual(latest['tables'],snapshot(1000)['tables'])
names={'creature':{'100':'Wolf "quoted"\n-- not Lua'},'gameobject':{}}
captures.capture(out/'snapshots',addon,'fixture','EDITOR',names,snapshot(1015))
subprocess.run(['luac5.1','-p',str(addon/'Capture.lua')],check=True)
self.assertEqual(json.loads((out/'snapshots/status.json').read_text())['captured'],1015)
self.assertIn('capture-00000000001015000000.json.gz',(addon/'Capture.lua').read_text())
with self.assertRaises(FileExistsError):captures.capture(out/'snapshots',addon,'fixture','EDITOR',names,snapshot(1015))
def test_newest_deleted_row_and_cli(self):
with tempfile.TemporaryDirectory() as d:
root=Path(d);out,addon=self.setup_store(root)
newer=snapshot(1015);newer['tables']['creature']['rows']['1'][2]='-12.345678901234567'
captures.capture(out/'snapshots',addon,'fixture','EDITOR',{},newer)
current=copy.deepcopy(newer);del current['tables']['creature']['rows']['1']
saved=operation('delete');sql=undo.undo_operation(saved,current,1,out/'snapshots')
self.assertIn('-12.345678901234567',sql);self.assertIn('INSERT INTO `creature`',sql)
text='OctoEditorUndo = '+captures.lua({'format':2,'session':'fixture','operations':[saved['operations'][1]],'rows':[],'lastChange':{},'log':[]})
sv=root/'SavedVariables.lua';sv.write_text(text);now=root/'current.json';now.write_text(json.dumps(current));result=root/'undo.sql'
subprocess.run([sys.executable,str(ROOT/'ops/editor/spawn_undo.py'),str(sv),str(result),'--entry','1','--current',str(now),'--snapshots',str(out/'snapshots')],check=True,stdout=subprocess.DEVNULL)
self.assertEqual(result.read_text(),sql)
with self.assertRaises(ValueError):undo.undo_operation(saved,newer,1,out/'snapshots')
saved['session']='another';
with self.assertRaises(ValueError):undo.undo_operation(saved,current,1,out/'snapshots')
def test_add_move_undo_guards(self):
with tempfile.TemporaryDirectory() as d:
out,_=self.setup_store(Path(d));current=snapshot(1030)
current['tables']['creature']['rows']['3']=['3','300','5','0']
sql=undo.undo_operation(operation('add','3','300'),current,1,out/'snapshots')
self.assertIn('DELETE FROM `creature`',sql);self.assertIn('<=>',sql)
current['tables']['creature']['rows']['1'][2]='9.876543210987654'
sql=undo.undo_operation(operation('move'),current,1,out/'snapshots')
self.assertIn('UPDATE `creature`',sql);self.assertIn('9.876543210987654',sql);self.assertIn('1.23456789',sql)
with self.assertRaises(ValueError):undo.undo_operation(operation('add'),current,1,out/'snapshots')
with self.assertRaises(ValueError):undo.undo_operation(operation('move'),snapshot(1030),1,out/'snapshots')
bad=operation('delete');bad['operations'][1]['capture_id']='../../capture.json.gz'
with self.assertRaises(ValueError):undo.undo_operation(bad,current,1,out/'snapshots')
def test_owned_install_and_failed_refresh(self):
with tempfile.TemporaryDirectory() as d:
root=Path(d);addon=launcher.install_addon(root,ROOT/'client_addons/OctoEditor')
self.assertIn('localOnly = false',(addon/'Capture.lua').read_text())
launcher.install_addon(root,ROOT/'client_addons/OctoEditor')
(addon/'.local-editor-owned').unlink()
with self.assertRaises(ValueError):launcher.install_addon(root,ROOT/'client_addons/OctoEditor')
class Stop:
n=0
def wait(self,_):self.n+=1;return self.n>1
with patch.object(captures,'capture',side_effect=RuntimeError('fixture failure')):
captures.watch(root/'snapshots',addon,'fixture','EDITOR',{},Stop())
self.assertFalse(json.loads((root/'snapshots/status.json').read_text())['healthy'])
if __name__=='__main__':unittest.main()