Files
WoW-Hybrid-gateway/tests/owned_fixture_cleanup_test.py
praxisandClaude Fable 5.2 7f2139d8d0 Offline test fixtures
883 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

133 lines
6.5 KiB
Python

#!/usr/bin/env python3
"""Owned fixture cleanup preserves progress without losing rank safety."""
import sys
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'ops'))
from owned_fixture_cleanup import cleanup_owned_fixtures, stage_owned_fixture
class FixtureCleanupTest(unittest.TestCase):
def stage(self, position=None, flight_node_ids=None, rank=4):
calls = []
record = {}
stage_owned_fixture(sql=calls.append, ownership_where=lambda *_: 'owned_guard',
record=record, guid=4509, account=511, username='first',
rank=rank, position=position, flight_node_ids=flight_node_ids)
return record, calls
def test_default_staging_does_not_update_position_or_flight(self):
record, calls = self.stage()
self.assertEqual(calls, [])
self.assertFalse(record['fixture_position_staged'])
self.assertFalse(record['fixture_flight_staged'])
def test_pose_is_staged_only_when_explicitly_supplied(self):
record, calls = self.stage(position={'map': 0, 'x': 12.5, 'y': -3.0, 'z': 9.0})
self.assertEqual(len(calls), 1)
self.assertIn('position_x=12.5', calls[0])
self.assertNotIn('c.money=', calls[0])
self.assertTrue(record['fixture_position_staged'])
self.assertFalse(record['fixture_flight_staged'])
def test_flight_fields_are_staged_only_with_explicit_nodes(self):
record, calls = self.stage(flight_node_ids=[1, 32, 33, 256])
self.assertEqual(len(calls), 1)
self.assertIn('c.money=100000', calls[0])
self.assertIn("UNHEX('", calls[0])
self.assertNotIn('position_x=', calls[0])
self.assertFalse(record['fixture_position_staged'])
self.assertTrue(record['fixture_flight_staged'])
def test_invalid_flight_staging_fails_before_even_pose_mutation(self):
for nodes, rank in (([], 4), ([0], 4), ([257], 4), ([1], 0)):
with self.subTest(nodes=nodes, rank=rank):
calls = []
with self.assertRaises(ValueError):
stage_owned_fixture(sql=calls.append, ownership_where=lambda *_: 'guard', record={},
guid=4509, account=511, username='first', rank=rank,
position={'map': 0, 'x': 1, 'y': 2, 'z': 3}, flight_node_ids=nodes)
self.assertEqual(calls, [])
def execute_cleanup(self, *, restore=False, error=None, exit_code=0,
online='0', rank='4', rank_changed=True, locked=True):
calls = []
sleeps = []
def sql(query):
calls.append(query)
if query.startswith('SELECT online'):
return online
if query.startswith('SELECT rank'):
return rank
return ''
record = {'runner_exit': exit_code}
if error:
record['error'] = error
cleanup_owned_fixtures(
sql=sql, ownership_where=lambda guid, account, username: f'guard_guid={guid} AND guard_account={account}',
sleep=sleeps.append, record=record, locked=locked,
first=(4509, ['0', '12', '1.0', '2.0', '3.0', '1.5', '0', '4'], 511, 'first'),
second=(4514, ['1', '14', '4.0', '5.0', '6.0', '2.5', '0', '4'], 513, 'second'),
flight_before=[100, '00AB', 3, '00CD', 1024],
second_rank_changed=rank_changed, restore_fixture=restore)
return record, calls, sleeps
def test_success_preserves_characters_and_restores_temporary_rank(self):
record, calls, _ = self.execute_cleanup()
self.assertFalse(record['characters_restored'])
self.assertFalse(record['flight_fields_restored'])
self.assertEqual(record['fixture_cleanup'], 'preserved')
self.assertTrue(record['second_rank_restored'])
self.assertEqual(sum(q.startswith('SELECT online') for q in calls), 2)
self.assertFalse(any(q.startswith('UPDATE tw_char.characters') for q in calls))
self.assertTrue(any(q.startswith('UPDATE tw_logon.account SET rank=4') for q in calls))
self.assertTrue(any(q.startswith('SELECT rank FROM tw_logon.account') for q in calls))
def test_error_does_not_undo_character_progress(self):
for error, exit_code in (('input failed', None), (None, 1)):
with self.subTest(error=error, exit_code=exit_code):
record, calls, _ = self.execute_cleanup(error=error, exit_code=exit_code)
self.assertEqual(record['fixture_cleanup'], 'preserved')
self.assertFalse(record['characters_restored'])
self.assertFalse(any(q.startswith('UPDATE tw_char.characters') for q in calls))
self.assertTrue(record['second_rank_restored'])
def test_explicit_restore_writes_positions_and_flight_fields(self):
record, calls, _ = self.execute_cleanup(restore=True)
updates = [q for q in calls if q.startswith('UPDATE tw_char.characters')]
self.assertEqual(len(updates), 3)
self.assertIn('c.position_x=1.0', updates[0])
self.assertIn('c.position_x=4.0', updates[1])
self.assertIn('c.money=100', updates[2])
self.assertIn("c.taximask=UNHEX('00AB')", updates[2])
self.assertTrue(record['characters_restored'])
self.assertTrue(record['flight_fields_restored'])
self.assertEqual(record['fixture_cleanup'], 'requested')
self.assertTrue(record['second_rank_restored'])
def test_online_fixture_blocks_cleanup_and_writes(self):
calls = []
with self.assertRaisesRegex(RuntimeError, 'still online'):
cleanup_owned_fixtures(
sql=lambda q: calls.append(q) or '1',
ownership_where=lambda *_: 'guard', sleep=lambda _: None,
record={}, locked=True,
first=(4509, ['0'] * 8, 511, 'first'), second=(0, None, 0, None),
flight_before=None, second_rank_changed=False, restore_fixture=True)
self.assertEqual(len(calls), 35)
self.assertTrue(all(q.startswith('SELECT online') for q in calls))
def test_no_lock_does_not_read_or_change_fixtures(self):
record, calls, sleeps = self.execute_cleanup(restore=True, locked=False)
self.assertEqual(calls, [])
self.assertEqual(sleeps, [])
self.assertFalse(record['characters_restored'])
self.assertEqual(record['fixture_cleanup'], 'not-attempted-no-lock')
if __name__ == '__main__':
unittest.main()