#!/usr/bin/env python3 """ Tests for AVA2 Stage 5 Part 2: Offline Shop (ENABLE_OFFLINESHOP_SYSTEM) & Shop Search (ENABLE_SHOP_SEARCH_SYSTEM). Validates packet headers, data structures, 72319 slot expansion, database schemas, and manager dispatch. """ import unittest import os import re SERVER_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) def read_file(rel_path): with open(os.path.join(SERVER_ROOT, rel_path), "r", encoding="latin1") as f: return f.read() class TestOfflineShop(unittest.TestCase): def test_service_macros(self): service_h = read_file("src/server/common/service.h") self.assertTrue(re.search(r'#define\s+ENABLE_OFFLINESHOP_SYSTEM', service_h)) self.assertTrue(re.search(r'#define\s+ENABLE_SHOP_SEARCH_SYSTEM', service_h)) def test_length_constants(self): length_h = read_file("src/server/common/length.h") self.assertTrue(re.search(r'OFFLINE_SHOP_HOST_ITEM_MAX_NUM\s*=\s*80', length_h)) self.assertTrue(re.search(r'SHOP_SIGN_MAX_LEN\s*=\s*45', length_h)) def test_packet_headers(self): packet_h = read_file("src/server/game/src/packet.h") self.assertTrue(re.search(r'HEADER_CG_OFFLINE_SHOP\s*=\s*57', packet_h)) self.assertTrue(re.search(r'HEADER_CG_MY_OFFLINE_SHOP\s*=\s*58', packet_h)) self.assertTrue(re.search(r'HEADER_CG_SHOP_SEARCH\s*=\s*143', packet_h)) self.assertTrue(re.search(r'HEADER_GC_OFFLINE_SHOP\s*=\s*47', packet_h)) self.assertTrue(re.search(r'HEADER_GC_OFFLINE_SHOP_SIGN\s*=\s*48', packet_h)) self.assertTrue(re.search(r'HEADER_GC_SHOPSEARCH_SET\s*=\s*156', packet_h)) # Verify TPacketCGShop structure self.assertTrue(re.search(r'typedef\s+struct\s+command_shop\s*\{[^}]*char\s+title\[50\s*\+\s*1\];[^}]*DWORD\s+vid;[^}]*BYTE\s+pos;[^}]*\}\s*TPacketCGShop;', packet_h, re.DOTALL)) # Verify Subheader enum self.assertIn("SHOP_SUBHEADER_CG_ADD_ITEM", packet_h) self.assertIn("SHOP_SUBHEADER_CG_REMOVE_ITEM", packet_h) self.assertIn("SHOP_SUBHEADER_CG_DESTROY_OFFLINE_SHOP", packet_h) self.assertIn("SHOP_SUBHEADER_CG_TAKE_MONEY", packet_h) self.assertIn("SHOP_SUBHEADER_CG_OPEN_SLOT", packet_h) self.assertIn("SHOP_SUBHEADER_CG_BUY", packet_h) # Verify GC subheaders self.assertIn("SHOP_SUBHEADER_GC_REFRESH_MONEY", packet_h) self.assertIn("SHOP_SUBHEADER_GC_REFRESH_DISPLAY_COUNT", packet_h) self.assertIn("SHOP_SUBHEADER_GC_CHECK_RESULT", packet_h) self.assertIn("SHOP_SUBHEADER_GC_REALWATCHER_COUNT", packet_h) def test_packet_info_registration(self): packet_info_cpp = read_file("src/server/game/src/packet_info.cpp") self.assertTrue(re.search(r'Set\(HEADER_CG_OFFLINE_SHOP,\s*sizeof\(TPacketCGShop\),\s*"OfflineShop",\s*true\);', packet_info_cpp)) self.assertTrue(re.search(r'Set\(HEADER_CG_MY_OFFLINE_SHOP,\s*sizeof\(TPacketCGMyOfflineShop\),\s*"MyOfflineShop",\s*true\);', packet_info_cpp)) self.assertTrue(re.search(r'Set\(HEADER_CG_SHOP_SEARCH,\s*sizeof\(TPacketCGShopSearch\),\s*"ShopSearch",\s*false\);', packet_info_cpp)) def test_database_tables_and_packets(self): tables_h = read_file("src/server/common/tables.h") self.assertTrue(re.search(r'HEADER_GD_OFFLINESHOP\s*=\s*146', tables_h)) self.assertTrue(re.search(r'HEADER_DG_OFFLINESHOP\s*=\s*66', tables_h)) self.assertIn("typedef struct SOfflineShopItemTable", tables_h) self.assertIn("typedef struct SOfflineShopAddItem", tables_h) self.assertIn("typedef struct SOfflineShop", tables_h) self.assertIn("typedef struct SPlayerTable", tables_h) self.assertTrue(re.search(r'unsigned\s+long\s+long\s+shopFlag;', tables_h)) def test_72319_slot_expansion(self): offlineshop_manager_cpp = read_file("src/server/game/src/offlineshop_manager.cpp") # Check requirement and deduction of item 72319 self.assertTrue(re.search(r'ch->CountSpecifyItem\(72319\)\s*<=\s*0', offlineshop_manager_cpp)) self.assertTrue(re.search(r'ch->RemoveSpecifyItem\(72319,\s*1\)', offlineshop_manager_cpp)) self.assertIn("SET_BIT(myFlag, flag)", offlineshop_manager_cpp) self.assertIn("ch->SetOfflineShopFlag(myFlag)", offlineshop_manager_cpp) def test_sql_schema_and_migrations(self): port_offlineshop_cpp = read_file("src/server/game/src/port_offlineshop.cpp") self.assertTrue(re.search(r'CREATE TABLE IF NOT EXISTS\s+`?offline_shop`?', port_offlineshop_cpp)) self.assertTrue(re.search(r'CREATE TABLE IF NOT EXISTS\s+`?offline_shop_item`?', port_offlineshop_cpp)) self.assertTrue(re.search(r'CREATE TABLE IF NOT EXISTS\s+`?offline_shop_log`?', port_offlineshop_cpp)) self.assertTrue(re.search(r'CREATE TABLE IF NOT EXISTS\s+`?offline_shop_log_important`?', port_offlineshop_cpp)) self.assertIn("shop_money BIGINT NOT NULL DEFAULT 0", port_offlineshop_cpp) self.assertIn("shopFlag BIGINT UNSIGNED NOT NULL DEFAULT 0", port_offlineshop_cpp) # Ensure ClientManagerOfflineshop doesn't use hardcoded `player.` database prefix client_manager_offlineshop = read_file("src/server/db/src/ClientManagerOfflineshop.cpp") self.assertNotIn("player.offline_shop", client_manager_offlineshop) self.assertNotIn("player.player", client_manager_offlineshop) self.assertIn("offline_shop_item", client_manager_offlineshop) self.assertIn("offline_shop", client_manager_offlineshop) def test_input_handlers(self): input_main_cpp = read_file("src/server/game/src/input_main.cpp") self.assertTrue(re.search(r'int\s+CInputMain::OfflineShop\(LPCHARACTER\s+ch,\s*const\s+char\s*\*\s*data,\s*size_t\s+uiBytes\)', input_main_cpp)) self.assertTrue(re.search(r'int\s+CInputMain::MyOfflineShop\(LPCHARACTER\s+ch,\s*const\s+char\s*\*\s*c_pData,\s*size_t\s+uiBytes\)', input_main_cpp)) self.assertTrue(re.search(r'void\s+CInputMain::ShopSearch\(LPCHARACTER\s+ch,\s*const\s+char\*\s*data\)', input_main_cpp)) self.assertIn("case HEADER_CG_OFFLINE_SHOP:", input_main_cpp) self.assertIn("case HEADER_CG_MY_OFFLINE_SHOP:", input_main_cpp) self.assertIn("case HEADER_CG_SHOP_SEARCH:", input_main_cpp) def test_character_methods(self): char_h = read_file("src/server/game/src/char.h") self.assertTrue(re.search(r'bool\s+CanOpenOfflineShop\(\);', char_h)) self.assertTrue(re.search(r'bool\s+CanBuyItemOfflineShop\(\);', char_h)) self.assertTrue(re.search(r'bool\s+IsOfflineShopNPC\(\)', char_h)) self.assertTrue(re.search(r'unsigned\s+long\s+long\s+GetOfflineShopFlag\(\)', char_h)) self.assertTrue(re.search(r'void\s+SetOfflineShopFlag\(unsigned\s+long\s+long', char_h)) char_cpp = read_file("src/server/game/src/char.cpp") self.assertIn("tab.shopFlag = GetOfflineShopFlag();", char_cpp) self.assertIn("SetOfflineShopFlag(t->shopFlag);", char_cpp) if __name__ == "__main__": unittest.main()