#!/usr/bin/env python3 """Verify treasure key and box stacking decrement logic in char_item.cpp.""" from pathlib import Path import re import unittest ROOT = Path(__file__).resolve().parents[2] class TreasureKeyStackTest(unittest.TestCase): def test_single_item_count_decrement(self): source = (ROOT / 'src/server/game/src/char_item.cpp').read_text(encoding='latin1') key_block = source.split('case ITEM_TREASURE_KEY:', 1)[1].split('case ITEM_GIFTBOX:', 1)[0] # Verify that RemoveItem is NOT used on key or box self.assertNotIn('ITEM_MANAGER::instance().RemoveItem(item);', key_block) self.assertNotIn('ITEM_MANAGER::instance().RemoveItem(item2);', key_block) # Verify single item decrement SetCount(GetCount() - 1) self.assertIn('item->SetCount(item->GetCount() - 1);', key_block) self.assertIn('item2->SetCount(item2->GetCount() - 1);', key_block) def test_failure_guards_preserve_stacks(self): source = (ROOT / 'src/server/game/src/char_item.cpp').read_text(encoding='latin1') key_block = source.split('case ITEM_TREASURE_KEY:', 1)[1].split('case ITEM_GIFTBOX:', 1)[0] # Decrement must occur inside the successful GiveItemFromSpecialItemGroup branch match = re.search(r'if\s*\(\s*GiveItemFromSpecialItemGroup\([^\)]+\)\s*\)\s*\{([^}]+)\}', key_block) self.assertIsNotNone(match) inside_if = match.group(1) self.assertIn('item->SetCount(item->GetCount() - 1);', inside_if) self.assertIn('item2->SetCount(item2->GetCount() - 1);', inside_if) if __name__ == '__main__': unittest.main()