"""The live 997 trace: mature incubator dispatch used an obsolete parameter.""" import copy from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer import threading import unittest from ranch_assistant.api import Client, DEFAULT_KEY, Session, decode, encode from ranch_assistant.engine import Engine, demo_snapshot from ranch_assistant.model import Catalog, Policy class DispatchWireTests(unittest.TestCase): def test_auto_dispatch_from_incubator_uses_active_client_contract(self): catalog = Catalog() state = copy.deepcopy(demo_snapshot(catalog).raw) mon = state["monsters"][0] mon.update(process=2, mode=2, roomId=None) state["monsters"] = [mon] state["stacks"] = [{"id": 1, "position": 0, "monsterId": mon["id"]}] state["items"] = [] calls = [] class Handler(BaseHTTPRequestHandler): def log_message(self, *args): pass def do_POST(self): body = decode(self.rfile.read(int(self.headers["Content-Length"])), DEFAULT_KEY) calls.append((self.path, body)) if self.path.endswith("monsterSell"): # NetMonster.sellMonsters and the successful live trace use # monsterIds. NetStack's unused singular field gets 997. if body.get("monsterIds") != [mon["id"]] or "monsterId" in body: response = {"resCode": 997, "resMsg": "网络错误!"} else: state["monsters"] = [] state["stacks"][0]["monsterId"] = -1 state["master"]["money"] += 150 response = {"resCode": 0, "money": 150} else: response = state | {"resCode": 0} data = encode(response, DEFAULT_KEY) self.send_response(200) self.send_header("Content-Length", str(len(data))) self.end_headers() self.wfile.write(data) server = ThreadingHTTPServer(("127.0.0.1", 0), Handler) thread = threading.Thread(target=server.serve_forever, daemon=True) thread.start() client = Client(Session(f"http://127.0.0.1:{server.server_port}/", "synthetic-token")) try: engine = Engine(catalog, client) engine.policy = Policy(strategy="gold", protect_star=6, protect_quality=5, fill=False, buy=False) engine.auto = True snapshot, _, message = engine.poll() self.assertNotIn(mon["id"], snapshot.monsters) self.assertEqual(snapshot.raw["stacks"][0]["monsterId"], -1) self.assertIn("派遣", message) sells = [body for path, body in calls if path.endswith("monsterSell")] self.assertEqual(len(sells), 1) self.assertEqual(sells[0]["monsterIds"], [mon["id"]]) finally: client.close() server.shutdown() server.server_close() thread.join(2)