Fix MCP cancellation, mirror run state, and battle boundaries
This commit is contained in:
@@ -0,0 +1,111 @@
|
|||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
|
||||||
|
from run_state import prepare_run, save_json, record_claim, claim_is_current, complete_run
|
||||||
|
|
||||||
|
|
||||||
|
class RunStateTest(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.folder = tempfile.TemporaryDirectory()
|
||||||
|
self.addCleanup(self.folder.cleanup)
|
||||||
|
self.home = Path(self.folder.name)
|
||||||
|
self.run_path = self.home / 'mirror_current_run.json'
|
||||||
|
self.context_path = self.home / 'jev_shop_context.json'
|
||||||
|
|
||||||
|
def test_completed_run_starts_with_fresh_floor_and_no_claim_or_upgrades(self):
|
||||||
|
old_run, old_context = prepare_run(self.home)
|
||||||
|
old_run['status'] = 'completed'
|
||||||
|
old_context.update(floor=5, claim_requested=True, symbol_upgraded=True, battle_context=True)
|
||||||
|
save_json(self.run_path, old_run)
|
||||||
|
save_json(self.context_path, old_context)
|
||||||
|
run, context = prepare_run(self.home)
|
||||||
|
self.assertNotEqual(run['run_id'], old_run['run_id'])
|
||||||
|
self.assertEqual(context, {'run_id': run['run_id'], 'floor': 1, 'refresh_used': 0})
|
||||||
|
self.assertFalse(claim_is_current(run, context))
|
||||||
|
|
||||||
|
def test_resume_keeps_matching_run_and_verified_claim(self):
|
||||||
|
old_run, context = prepare_run(self.home)
|
||||||
|
context.update(floor=4, symbol_upgraded=True)
|
||||||
|
record_claim(self.home, old_run, context, {'path': 'completion.png'})
|
||||||
|
run, resumed = prepare_run(self.home, resume=True)
|
||||||
|
self.assertEqual(run['run_id'], old_run['run_id'])
|
||||||
|
self.assertEqual(run['start_utc'], old_run['start_utc'])
|
||||||
|
self.assertEqual(resumed['floor'], 4)
|
||||||
|
self.assertTrue(resumed['symbol_upgraded'])
|
||||||
|
self.assertTrue(claim_is_current(run, resumed))
|
||||||
|
|
||||||
|
def test_explicit_new_run_discards_even_unfinished_state(self):
|
||||||
|
old_run, _ = prepare_run(self.home)
|
||||||
|
run, _ = prepare_run(self.home, new_run=True)
|
||||||
|
self.assertNotEqual(run['run_id'], old_run['run_id'])
|
||||||
|
|
||||||
|
def test_resume_requires_an_unfinished_record(self):
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
prepare_run(self.home, resume=True)
|
||||||
|
run, _ = prepare_run(self.home)
|
||||||
|
run['status'] = 'completed'
|
||||||
|
save_json(self.run_path, run)
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
prepare_run(self.home, resume=True)
|
||||||
|
|
||||||
|
def test_orphan_context_does_not_supply_completion_to_new_run(self):
|
||||||
|
save_json(self.context_path, {'floor': 5, 'claim_requested': True})
|
||||||
|
run, context = prepare_run(self.home)
|
||||||
|
self.assertEqual(context['floor'], 1)
|
||||||
|
self.assertFalse(claim_is_current(run, context))
|
||||||
|
|
||||||
|
def test_legacy_unfinished_run_preserves_upgrades_but_discards_unbound_claim(self):
|
||||||
|
save_json(self.run_path, {'start_utc': '2026-10-02T18:13:22+08:00', 'status': 'user_takeover'})
|
||||||
|
save_json(self.context_path, {'floor': 5, 'symbol_upgraded': True,
|
||||||
|
'claim_requested': True, 'battle_context': True})
|
||||||
|
run, context = prepare_run(self.home, resume=True)
|
||||||
|
self.assertEqual(context['floor'], 5)
|
||||||
|
self.assertTrue(context['symbol_upgraded'])
|
||||||
|
self.assertNotIn('battle_context', context)
|
||||||
|
self.assertFalse(claim_is_current(run, context))
|
||||||
|
self.assertTrue(run['legacy_context_migrated'])
|
||||||
|
|
||||||
|
def test_mismatched_ids_fail_before_overwriting_the_context(self):
|
||||||
|
prepare_run(self.home)
|
||||||
|
save_json(self.context_path, {'run_id': 'another-run', 'floor': 5})
|
||||||
|
before = self.context_path.read_bytes()
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
prepare_run(self.home)
|
||||||
|
self.assertEqual(self.context_path.read_bytes(), before)
|
||||||
|
|
||||||
|
def test_completion_requires_matching_claim_and_keeps_both_screens(self):
|
||||||
|
run, context = prepare_run(self.home)
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
complete_run(self.home, run, context, {'path': 'home.png'})
|
||||||
|
record_claim(self.home, run, context, {'path': 'completion.png'})
|
||||||
|
context['claim_run_id'] = 'another-run'
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
complete_run(self.home, run, context, {'path': 'home.png'})
|
||||||
|
context['claim_run_id'] = run['run_id']
|
||||||
|
complete_run(self.home, run, context, {'path': 'home.png'})
|
||||||
|
saved = json.loads(self.run_path.read_text())
|
||||||
|
self.assertTrue(saved['dungeon_completed'])
|
||||||
|
self.assertEqual(saved['claim_evidence']['path'], 'completion.png')
|
||||||
|
self.assertEqual(saved['completion_evidence']['path'], 'home.png')
|
||||||
|
|
||||||
|
def test_atomic_snapshot_retains_old_data_if_serialization_fails(self):
|
||||||
|
save_json(self.context_path, {'floor': 2})
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
save_json(self.context_path, {'bad': {1, 2}})
|
||||||
|
self.assertEqual(json.loads(self.context_path.read_text()), {'floor': 2})
|
||||||
|
self.assertFalse(list(self.home.glob('*.tmp')))
|
||||||
|
|
||||||
|
def test_new_run_can_recover_from_corrupt_old_files(self):
|
||||||
|
self.run_path.write_text('truncated')
|
||||||
|
self.context_path.write_text('truncated')
|
||||||
|
run, context = prepare_run(self.home, new_run=True)
|
||||||
|
self.assertEqual(run['status'], 'running')
|
||||||
|
self.assertEqual(context['floor'], 1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user