import copy import unittest from unittest.mock import patch from ranch_assistant.breakthrough import BreakthroughPlanner, PATH, plan_breakthrough from ranch_assistant.model import Catalog, Policy, Snapshot def snapshot_with(**monster): mon = {"id": 900, "typeId": 1001010, "process": 3, "mode": 2, "potentialLevel": 1, "level": 1, "pAtk": 4, "pDef": 8, "pHp": 105, "pDex": 1} mon.update(monster) return Snapshot({"master": {"id": 1}, "monsters": [mon], "items": [ {"id": 600, "typeId": 30058, "num": 4}], "stacks": [], "rooms": [{"id": 1, "typeId": 1000}], "teams": []}) class BreakthroughTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.catalog = Catalog() def setUp(self): self.snapshot = snapshot_with() self.policy = Policy(feed_targets={"900": 1}) self.planner = BreakthroughPlanner(self.catalog) def test_original_first_breakthrough_payload_and_chinese_description(self): action = self.planner.plan(self.snapshot, self.policy) self.assertEqual(action.path, PATH) self.assertEqual(action.payload, {"monId": 900, "itemIds": [600]}) self.assertEqual(action.kind, "breakthrough") self.assertIn("突破", action.description) self.assertIn("2 级", action.description) self.assertNotIn("monsterPotential", action.description) def test_incomplete_potential_blocks_even_at_ordinary_level_cap(self): self.snapshot.monsters[900].update(pAtk=3, level=10) self.assertIsNone(self.planner.plan(self.snapshot, self.policy)) self.assertIn("攻击 3/4", self.planner.notes[0]) def test_ordinary_level_below_cap_does_not_block_full_potential(self): self.assertEqual(self.snapshot.monsters[900]["level"], 1) self.assertIsNotNone(self.planner.plan(self.snapshot, self.policy)) def test_missing_or_insufficient_material_never_returns_action(self): self.snapshot.raw["items"][0]["num"] = 3 self.assertIsNone(self.planner.plan(self.snapshot, self.policy)) self.assertIn("3/4", self.planner.notes[0]) self.snapshot.raw["items"] = [] self.assertIsNone(self.planner.plan(self.snapshot, self.policy)) def test_multiple_stacks_use_first_stack_id_and_total_stock(self): self.snapshot.raw["items"] = [ {"id": 600, "typeId": 30058, "num": 1}, {"id": 601, "typeId": 30058, "num": 2}, {"id": 602, "typeId": 30058, "num": 8}, ] action = self.planner.plan(self.snapshot, self.policy) self.assertEqual(action.payload["itemIds"], [600]) def test_priority_skips_blocked_candidate_and_uses_next(self): other = copy.deepcopy(self.snapshot.monsters[900]) other.update(id=901, pAtk=3) self.snapshot.raw["monsters"].append(other) self.policy.feed_targets = {"901": 2, "900": 1} self.assertEqual(self.planner.plan(self.snapshot, self.policy).payload["monId"], 900) def test_independent_targets_replace_feed_targets(self): other = copy.deepcopy(self.snapshot.monsters[900]) other["id"] = 901 self.snapshot.raw["monsters"].append(other) action = self.planner.plan(self.snapshot, self.policy, targets={"901": 1}) self.assertEqual(action.payload["monId"], 901) def test_non_ranch_disabled_and_maximum_level_do_not_plan(self): for process in (1, 2): self.snapshot.monsters[900]["process"] = process self.assertIsNone(self.planner.plan(self.snapshot, self.policy)) self.snapshot.monsters[900].update(process=3, potentialLevel=7) self.assertIsNone(self.planner.plan(self.snapshot, self.policy)) self.assertIsNone(self.planner.plan(self.snapshot, self.policy, enabled=False)) def test_invalid_and_incomplete_snapshots_are_safe(self): for field in ("potentialLevel", "pAtk", "pDef", "pHp", "pDex"): snapshot = snapshot_with() snapshot.monsters[900][field] = None self.assertIsNone(BreakthroughPlanner(self.catalog).plan(snapshot, self.policy)) snapshot = snapshot_with() snapshot.monsters[900]["typeId"] = 999999999 self.assertIsNone(BreakthroughPlanner(self.catalog).plan(snapshot, self.policy)) def test_duplicate_item_instances_block_instead_of_double_spending(self): self.snapshot.raw["items"] = [ {"id": 600, "typeId": 30058, "num": 2}, {"id": 600, "typeId": 30058, "num": 2}, ] self.assertIsNone(self.planner.plan(self.snapshot, self.policy)) self.assertIn("实例重复", self.planner.notes[0]) def test_second_stage_matches_original_element_table(self): # 1-star job 5, level 1->2; level 2 requires lv1 x4 + lv2 x1. self.snapshot.monsters[900].update(potentialLevel=2, pAtk=16, pDef=30, pHp=345, pDex=4) self.snapshot.raw["items"] = [{"id": 600, "typeId": 30058, "num": 4}, {"id": 601, "typeId": 30041, "num": 1}] action = self.planner.plan(self.snapshot, self.policy) self.assertEqual(action.payload, {"monId": 900, "itemIds": [600, 601]}) def test_functional_integration_is_pure_and_makes_no_client_call(self): with patch("ranch_assistant.api.Client.call") as call: action = plan_breakthrough(self.catalog, self.snapshot, self.policy) self.assertEqual(action.payload["monId"], 900) call.assert_not_called() if __name__ == "__main__": unittest.main()