"""Operational point rollout must target the intended DB and enable chances last.""" import importlib.util import json from pathlib import Path import tempfile import unittest from unittest.mock import Mock, patch ROOT = Path(__file__).resolve().parents[2] spec = importlib.util.spec_from_file_location( 'point_rollout', ROOT / 'tools/ava2-itemshop/configure_point_drops.py') rollout = importlib.util.module_from_spec(spec) spec.loader.exec_module(rollout) class PointRollout(unittest.TestCase): def test_only_reviewed_rules_are_enabled_after_caps_and_eligibility(self): approved = json.loads((ROOT / 'tools/ava2-itemshop/points-wide.json').read_text()) self.assertEqual(rollout.APPROVED, approved) commands = rollout.rollout({}, approved) self.assertEqual(commands[:2], [('points_mob_chance', 0), ('points_stone_chance', 0)]) self.assertEqual(commands[-2:], [('points_mob_chance', 5000), ('points_stone_chance', 10000)]) self.assertIn(('points_daily_cap', 0), commands[2:-2]) self.assertIn(('points_mob_min', 3), commands[2:-2]) self.assertIn(('points_mob_max', 5), commands[2:-2]) self.assertIn(('points_stone_min', 15), commands[2:-2]) self.assertIn(('points_stone_max', 30), commands[2:-2]) self.assertIn(('points_max_overlevel', 15), commands[2:-2]) self.assertEqual(dict(commands), approved) self.assertEqual(rollout.rollout(approved, approved), []) def test_restoring_absent_flags_disables_rewards(self): disabled = {key: 0 for key in rollout.APPROVED} commands = rollout.rollout(rollout.APPROVED, disabled) self.assertTrue(commands) self.assertEqual(dict(commands), disabled) self.assertFalse(any(value for key, value in commands if key in rollout.CHANCES)) def test_schema_and_db_port_mismatches_fail_closed(self): with tempfile.TemporaryDirectory() as directory: config = Path(directory) / 'CONFIG' database = Path(directory) / 'conf.txt' config.write_text('PORT: 13040\nDB_PORT: 15040\nADMINPAGE_IP: 127.0.0.1\n' 'ADMINPAGE_PASSWORD: local-test-only\n') database.write_text('SQL_PLAYER = "localhost player_sb root test"\n' 'SQL_ACCOUNT = "localhost account_sb root test"\n') rollout.validate_target(config, database, 'player_sb', 'account_sb', 15040) with self.assertRaisesRegex(RuntimeError, 'schemas'): rollout.validate_target(config, database, 'player', 'account', 15040) with self.assertRaisesRegex(RuntimeError, 'DB_PORT'): rollout.validate_target(config, database, 'player_sb', 'account_sb', 15000) with self.assertRaisesRegex(RuntimeError, 'schema name'): rollout.validate_target(config, database, 'player; DROP DATABASE player', 'account', 15040) def test_lan_listener_uses_allowlisted_loopback_source_and_checks_event_ack(self): connection = Mock() connection.recv.side_effect = [b'\xff' * 15, rollout.socket.timeout(), b'UNKNOWN\n', b'EVENT FLAG CHANGE points_mob_chance\n'] with patch.object(rollout.socket, 'create_connection', return_value=connection) as connect: admin = rollout.Admin({'port': '13040', 'adminpage_password': 'test-only'}, '192.168.21.203') admin.event('points_mob_chance', 500) connect.assert_called_once_with(('192.168.21.203', 13040), timeout=3, source_address=('127.0.0.1', 0)) self.assertEqual(connection.sendall.call_args_list[-1].args, (b'@EVENT points_mob_chance 500\n',)) def test_unaccepted_event_does_not_count_as_applied(self): connection = Mock() connection.recv.side_effect = [rollout.socket.timeout(), b'UNKNOWN\n', b'UNKNOWN\n'] with patch.object(rollout.socket, 'create_connection', return_value=connection): admin = rollout.Admin({'port': '13040', 'adminpage_password': 'test-only'}, '192.168.21.203') with self.assertRaisesRegex(RuntimeError, 'rejected'): admin.event('points_mob_chance', 500) if __name__ == '__main__': unittest.main()