103 lines
5.4 KiB
Python
103 lines
5.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Execute the generated menu and compatibility helper with the server's Lua 5.0."""
|
|
import importlib.util
|
|
import json
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
ROOT=Path(__file__).resolve().parent
|
|
def module(name):
|
|
spec=importlib.util.spec_from_file_location(name,ROOT/(name+'.py'))
|
|
result=importlib.util.module_from_spec(spec);spec.loader.exec_module(result);return result
|
|
|
|
|
|
class FollowupTests(unittest.TestCase):
|
|
def test_live_menu_and_quest_helper(self):
|
|
text=module('generate').generate()
|
|
self.assertEqual(text,(ROOT/'ava2_travel.quest').read_text())
|
|
body=text.split(' function destinations()',1)[1].split(' when ',1)[0]
|
|
body='ava2_travel={}\nfunction ava2_travel.destinations()'+body
|
|
body=body.replace('function menu()','function ava2_travel.menu()')
|
|
body+='''
|
|
local choices={} local cursor=0 local warps={} local level=120 local allowed=true local map=1
|
|
say_title=function() end say=function() end
|
|
select_table=function(options)
|
|
assert(table.getn(options)<=8)
|
|
cursor=cursor+1
|
|
assert(choices[cursor] and choices[cursor]<=table.getn(options))
|
|
return choices[cursor]
|
|
end
|
|
select=function(...) return select_table(arg) end
|
|
pc={get_level=function() return level end,can_warp=function() return allowed end,
|
|
warp=function(x,y) table.insert(warps,{x,y}) end,get_map_index=function() return map end}
|
|
local targets=ava2_travel.destinations()
|
|
for i=1,table.getn(targets) do
|
|
local function run(lvl,canWarp,confirm)
|
|
level=lvl allowed=canWarp warps={} cursor=0 choices={}
|
|
if i>5 then table.insert(choices,6) end
|
|
table.insert(choices,math.mod(i-1,5)+1)
|
|
if lvl>=targets[i][4] then table.insert(choices,confirm) end
|
|
ava2_travel.menu()
|
|
assert(cursor==table.getn(choices))
|
|
end
|
|
run(120,true,1)
|
|
assert(table.getn(warps)==1 and warps[1][1]==targets[i][2] and warps[1][2]==targets[i][3])
|
|
run(targets[i][4]-1,true,1) assert(table.getn(warps)==0)
|
|
run(120,false,1) assert(table.getn(warps)==0)
|
|
run(120,true,2) assert(table.getn(warps)==0)
|
|
end
|
|
choices={7} cursor=0 ava2_travel.menu() assert(cursor==1)
|
|
'''
|
|
body+=(ROOT/'quest_compat.lua').read_text()
|
|
body+='''
|
|
for i=1,400 do
|
|
map=i
|
|
assert((is_destination_village(1)~=nil)==(i==1 or i==21 or i==41))
|
|
assert((is_destination_village(2)~=nil)==(i==3 or i==23 or i==43))
|
|
assert((is_destination_village(3)~=nil)==(i==1 or i==21 or i==41 or i==3 or i==23 or i==43))
|
|
assert((is_destination_village(65)~=nil)==(i==65))
|
|
end
|
|
local existing=function() return "preserved" end
|
|
is_destination_village=existing
|
|
'''
|
|
body+=(ROOT/'quest_compat.lua').read_text()
|
|
body+='assert(is_destination_village==existing)\nprint("PASS: 10 destinations, levels, cancel, busy, paging; all quest village lookups")\n'
|
|
lua=ROOT.parents[3]/'metin2-server/src/server/liblua'
|
|
sources=[p for p in sorted((lua/'src').glob('*.c')) if p.name!='ltests.c']
|
|
sources += [lua/'src/lib'/name for name in ('lauxlib.c','lbaselib.c','ltablib.c','lstrlib.c','lmathlib.c')]
|
|
with tempfile.TemporaryDirectory(prefix='mt-followup-') as folder:
|
|
folder=Path(folder);binary=folder/'lua'
|
|
subprocess.run(['cc','-w','-I'+str(lua/'include'),'-I'+str(lua/'src'),
|
|
str(ROOT.parent/'ring-warp/tests/lua_runner.c'),*[str(p) for p in sources],'-lm','-o',str(binary)],check=True)
|
|
script=folder/'test.lua';script.write_bytes(module('install').gbk(body))
|
|
subprocess.run([str(binary),str(script)],check=True)
|
|
data=json.loads((ROOT/'destinations.json').read_text())['destinations']
|
|
self.assertEqual({r['id']:r['map'] for r in data if r['mode']=='entrance'},{212:62,352:61,355:301})
|
|
|
|
def test_install_backup_idempotence_and_rollback(self):
|
|
installer=module('install')
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
folder=Path(folder);quest=folder/'quest';stage=folder/'compiled';quest.mkdir();stage.mkdir()
|
|
original={'quest_functions':b'existing\n','locale_list':b'old.quest\n','questlib.lua':b'-- original\n'}
|
|
for name,data in original.items():(quest/name).write_bytes(data)
|
|
(stage/'quest_functions').write_bytes(b'existing\npc.open_sash\n')
|
|
for name in ('sash','ava2_travel'):(stage/(name+'.quest')).write_bytes(b'compiled source')
|
|
(stage/'object/state').mkdir(parents=True);(stage/'object/state/sash').write_bytes(b'state')
|
|
installer.install(quest,stage,folder/'backup')
|
|
self.assertEqual((quest/'questlib.lua').read_bytes().count(b'-- PORT AVA2 missing quest helper'),1)
|
|
installer.install(quest,stage,folder/'backup2')
|
|
self.assertEqual((quest/'questlib.lua').read_bytes().count(b'-- PORT AVA2 missing quest helper'),1)
|
|
installer.rollback(quest,folder/'backup2')
|
|
installer.rollback(quest,folder/'backup')
|
|
for name,data in original.items():self.assertEqual((quest/name).read_bytes(),data)
|
|
self.assertFalse((quest/'object/state/sash').exists())
|
|
installer.install(quest,stage,folder/'backup3')
|
|
(quest/'questlib.lua').write_bytes(b'newer change')
|
|
with self.assertRaises(ValueError):installer.rollback(quest,folder/'backup3')
|
|
self.assertEqual((quest/'questlib.lua').read_bytes(),b'newer change')
|
|
|
|
|
|
if __name__=='__main__':unittest.main()
|