Add Web 2 workspace and controlled deployment updates

This commit is contained in:
AttilaNet
2026-11-10 16:33:14 +08:00
parent 7fe4c0ffdf
commit dbb31707ea
58 changed files with 11283 additions and 0 deletions
+820
View File
@@ -0,0 +1,820 @@
"""Independent HTTP -> WebManager -> existing Engine offline integration checks.
Run: python -m unittest ranch_web.tests.test_web_integration -v
Only the game Client/login boundary and release source are fakes. Each test owns
a new temporary root, disables the scheduler, and blocks external socket I/O.
Fixtures stay in this file to avoid other agents' test/fixture ownership.
"""
from __future__ import annotations
import base64
from contextlib import ExitStack
from copy import deepcopy
import hashlib
import ipaddress
import json
import os
from pathlib import Path
import socket
import subprocess
import sys
import tempfile
import textwrap
import threading
import time
import unittest
from unittest.mock import patch
PAGES = "login incubator bag ranch daily hatching mainline battles gifts commissions exploration maps statistics plans yields".split()
TERMINAL = {"succeeded", "failed", "cancelled"}
TOKEN = "web-integration-synthetic-token-82719"
KEY = "web-integration-synthetic-key-49281"
PASSWORD = "web-integration-synthetic-password-18374"
REQID = 98271948
REQSID = 91283741
ROOT = Path(__file__).resolve().parents[2]
class NoExternalSockets:
"""Allow Windows asyncio's loopback socketpair, reject external addresses."""
def __init__(self):
self.stack = ExitStack()
self.attempts = []
def check(self, address):
host = address[0] if isinstance(address, tuple) else address
if host == "localhost":
return
try:
value = ipaddress.ip_address(host)
if value.is_loopback or (getattr(value, "ipv4_mapped", None) and value.ipv4_mapped.is_loopback):
return
except (ValueError, TypeError):
pass
self.attempts.append(str(host))
raise AssertionError("External socket blocked by Web integration fixture")
def __enter__(self):
for name in ("connect", "connect_ex", "sendto"):
original = getattr(socket.socket, name)
def guarded(sock, *args, _original=original, _name=name, **kwargs):
address = args[-1] if _name == "sendto" else args[0]
self.check(address)
return _original(sock, *args, **kwargs)
self.stack.enter_context(patch.object(socket.socket, name, guarded))
original_dns = socket.getaddrinfo
def dns(host, *args, **kwargs):
self.check(host)
return original_dns(host, *args, **kwargs)
self.stack.enter_context(patch.object(socket, "getaddrinfo", dns))
return self
def __exit__(self, *args):
return self.stack.__exit__(*args)
class SnapshotGate:
def __init__(self):
self.entered = threading.Event()
self.release = threading.Event()
def wait(self):
self.entered.set()
if not self.release.wait(8):
raise AssertionError("Integration snapshot gate was not released")
def seeded_snapshot(catalog, role_id=1):
from ranch_assistant.engine import demo_snapshot
from ranch_assistant.resource_stages import item_requirements, with_resource_data
snapshot = demo_snapshot(catalog)
snapshot.raw["master"]["id"] = role_id
snapshot.raw["master"]["roleName"] = "Integration offline role"
snapshot.raw["teams"][0]["teamName"] = "teamA"
for index, monster in enumerate(snapshot.raw["monsters"][:4]):
monster.update(process=3, mode=2, roomId=1, level=20 - index * 4)
for slot in snapshot.raw["stacks"]:
slot["monsterId"] = 0
snapshot.raw["history"].update(
unlockStage={"0101": [1, 2, 3], "0109": [1, 2, 3]},
passStage={"0101": {"1": True, "2": True, "3": True}, "0109": {"1": True}},
passTask={"1001008": True}, unlockTask={"1001004": True}, openHardMode=True,
)
snapshot.raw["currentTasks"] = [{"id": 711, "typeId": "1001004", "curStep": 1, "stepValue": None}]
snapshot.raw["exploreTeams"] = [{"id": 1, "status": 0, "area": "", "monsters": ""}]
for code in range(1, 6):
item_id = next(key for key in catalog.data["item_info"] if catalog.item_type(key) == code)
snapshot.raw["items"].extend({"id": 11000 + code * 10 + index, "typeId": item_id, "num": count}
for index, count in enumerate((3, 4)))
required_items = {}
for stage in with_resource_data(catalog).data["stage_info"].values():
for item_id, count in item_requirements((stage.get("cost") or {}).get("items") or []).items():
required_items[item_id] = max(required_items.get(item_id, 0), count * 3)
snapshot.raw["items"].extend({"id": 12000 + index, "typeId": item_id, "num": count}
for index, (item_id, count) in enumerate(sorted(required_items.items())))
return snapshot
def real_engine_factory(catalog, client=None, budget_path=None):
from ranch_assistant.engine import Engine
engine = Engine(catalog, client, budget_path)
if client is None:
engine.snapshot = seeded_snapshot(catalog)
role_id = client.session.role_id if client else 1
# Persist through the real SQLite statistics service; no DTOs are injected.
statistics = engine.sweeper.statistics
for source, operation, reward, spent in (
("sweep", "integration-sweep-hit", {"items": [{"typeId": 30005, "num": 2}]}, 10),
("sweep", "integration-sweep-miss", {"items": []}, 10),
("event", "integration-event-hit", {"items": [{"typeId": 30005, "num": 3}]}, 5),
):
statistics.record(role_id, "0101", "3", source, operation, 1, reward, spent=spent)
return engine
class OfflineClient:
def __init__(self, catalog, account="fixture", **kwargs):
from ranch_assistant.api import Session
role_id = 100 + int(hashlib.sha256(account.encode()).hexdigest()[:6], 16)
self.catalog = catalog
self.session = Session(server_url="https://game.invalid/", token=TOKEN, key=KEY,
reqid=REQID, reqsid=REQSID, account_id=account,
server_id=7, role_id=role_id, account_name=account)
self.raw = seeded_snapshot(catalog, role_id).raw
self.trace = kwargs.get("trace")
self.on_session_change = kwargs.get("on_session_change")
self.gate = None
self.fail_snapshot = False
self.reads = 0
self.writes = []
self.closed = False
def enter_server(self, *args, **kwargs):
return {"resCode": 0}
def snapshot(self):
from ranch_assistant.model import Snapshot
self.reads += 1
if self.gate:
self.gate.wait()
if self.fail_snapshot:
raise ValueError("离线读取失败 " + PASSWORD + " token=" + TOKEN + " key=" + KEY)
if self.trace:
self.trace.emit("response", "masterService/masterQueryDetail", http_status=200,
body={"safe_marker": "integration-readable-detail", "token": TOKEN,
"nested": {"password": PASSWORD, "key": KEY, "reqid": REQID}},
message="离线响应 password=" + PASSWORD)
return Snapshot(deepcopy(self.raw))
def call(self, path, payload=None, **kwargs):
self.writes.append((path, deepcopy(payload)))
raise AssertionError("Unexpected game write in integration fixture: " + path)
def close(self):
self.closed = True
class OfflineLoginFactory:
def __init__(self):
self.clients = []
def __call__(self, catalog, device, trace=None):
owner = self
class Flow:
def authenticate(self, account, password):
client = OfflineClient(catalog, account, trace=trace)
owner.clients.append(client)
return client, {"token": TOKEN, "key": KEY,
"servers": [{"id": 7, "serverName": "Offline server", "gameServerAddress": "https://game.invalid/"}],
"masterInfos": [{"serverId": 7, "masterName": "Offline role"}]}, {
"center_url": "https://center.invalid/", "uid": account,
"channel": "DYD", "device_id": "integration-device"}
return Flow()
class WebIntegrationTests(unittest.TestCase):
def setUp(self):
from fastapi.testclient import TestClient
from ranch_web.app import create_app
from ranch_web.config import WebConfig
from ranch_web.service import WebManager
from ranch_web.updates import UpdateService
self.stack = ExitStack()
self.addCleanup(self.stack.close)
self.network = self.stack.enter_context(NoExternalSockets())
self.folder = Path(self.stack.enter_context(tempfile.TemporaryDirectory(prefix="web-integration-")))
self.root = self.folder / "new-web-root"
self.stack.enter_context(patch.dict(os.environ, {"RANCH_WEB_UPDATER_DIR": ""}))
self.source = self.stack.enter_context(patch("ranch_web.updates.github_releases", return_value=[]))
self.login = OfflineLoginFactory()
self.manager = WebManager(self.root, start_scheduler=False,
engine_factory=real_engine_factory, login_factory=self.login)
self.stack.callback(lambda: self.manager.close())
self.updates = UpdateService(self.root, "2.0.0.dev1")
self.app = create_app(manager=self.manager, updates=self.updates,
config=WebConfig(data_dir=self.root))
self.http = self.stack.enter_context(TestClient(self.app, base_url="http://127.0.0.1:8765",
client=("127.0.0.1", 50000)))
self.account = self.create_account()
def tearDown(self):
self.assertEqual(self.network.attempts, [], "An external socket was attempted")
def response(self, response, status=200):
self.assertEqual(response.status_code, status, response.text[:3000])
return response.json()
def create_account(self, demo=True, label="integration"):
value = self.response(self.http.post("/api/accounts", json={"label": label, "demo": demo}), 201)
self.wait_idle(value["id"])
return value["id"]
def path(self, suffix, account=None):
return "/api/accounts/" + (account or self.account) + "/" + suffix
def wait_idle(self, account=None):
deadline = time.monotonic() + 8
while time.monotonic() < deadline:
status = self.manager.status(account or self.account)
if not status["busy"]:
self.assertFalse(status["error"], status)
return status
time.sleep(.005)
self.fail("Account worker did not become idle")
def job(self, value, account=None, expected="succeeded"):
deadline = time.monotonic() + 8
while time.monotonic() < deadline:
current = self.response(self.http.get(self.path("jobs/" + value["id"], account)))
self.assertIn(current["status"], TERMINAL | {"queued", "running"})
if current["status"] in TERMINAL:
self.assertEqual(current["status"], expected, current)
return current
time.sleep(.005)
self.fail("Job did not finish: " + str(value))
def action(self, operation, payload=None, account=None, expected="succeeded"):
value = self.response(self.http.post(self.path("actions/" + operation, account), json=payload or {}), 202)
return self.job(value, account, expected)
def config(self, account=None):
return self.response(self.http.get(self.path("config", account)))
def save(self, value, account=None):
current = self.config(account)
response = self.http.put(self.path("config", account), json={"config": value, "revision": current["revision"]})
self.assertIn(response.status_code, (200, 202), response.text)
pending = response.json()
if pending.get("pending"):
self.job(pending["job"], account)
applied = self.config(account)
self.assertGreater(applied["revision"], current["revision"])
return applied
def view(self, page, query=None, account=None, simple=False):
params = query if simple else {"query": json.dumps(query)} if query is not None else None
value = self.response(self.http.get(self.path("views/" + page, account), params=params))
if value["loading"]:
self.assertIsInstance(value["job_id"], str)
self.job({"id": value["job_id"]}, account)
value = self.response(self.http.get(self.path("views/" + page, account), params=params))
self.assertFalse(value["loading"], value)
self.assertIsInstance(value["revision"], int)
self.assertIsInstance(value["data"], dict)
return value["data"]
def hatch_config(self, account=None, enabled=True, priority=13):
row = self.view("bag", account=account)["rows"][0]
config = deepcopy(self.config(account)["config"])
config["policy"].update(enabled=True, fill=True, buy=False, collect=False, dispatch=False, feed=False,
manual_eggs=True, egg_rules={row["id"]: row["rule_defaults"] | {
"enabled": enabled, "priority": priority, "action": "keep", "keep_count": 2}})
return config, row
def logged_account(self):
account = self.create_account(False, "fake login")
result = self.action("login", {"account": "fixture-account", "password": PASSWORD, "remember": True}, account)
self.assertEqual(result["result"]["servers"][0]["id"], 7)
self.assertTrue(result["result"]["has_saved_password"])
self.action("enter_server", {"server_id": 7}, account)
self.assertTrue(self.manager.status(account)["connected"])
return account, self.login.clients[-1]
def assert_dto(self, page):
from ranch_assistant.engine import Engine
self.assertIs(type(self.manager._worker(self.account).engine), Engine)
dto = self.view(page)
self.assertEqual(dto["page"], page)
self.assertTrue(dto["title"])
for field in ("rows", "columns", "filters", "form"):
self.assertIsInstance(dto[field], list, (page, field))
if page != "login":
self.assertTrue(dto["columns"], page)
for column in dto["columns"]:
self.assertTrue(column["key"], (page, column))
self.assertTrue(column["label"], (page, column))
if "edit" in column:
self.assertIsInstance(column["edit"]["path"], list)
for row in dto["rows"]:
self.assertIn("id", row, (page, row))
for field in dto["form"]:
self.assertTrue({"path", "type", "label", "value"} <= field.keys(), (page, field))
def test_default_queries_schema_and_all_six_bag_categories(self):
schema = self.response(self.http.get("/api/schema"))
self.assertEqual([p["key"] for p in schema["pages"]], PAGES)
self.assertTrue(schema["settings"])
defaults = {"bag": {"category": "eggs"}, "ranch": {"grouped": False},
"mainline": {"category": "mainline", "state": "current"},
"maps": {"category": "all", "only_unlocked": True},
"statistics": {"source": "all", "metric": "rate"}}
for page, filters in defaults.items():
with self.subTest(page=page):
implicit, explicit = self.view(page), self.view(page, filters)
self.assertEqual([r["id"] for r in implicit["rows"]], [r["id"] for r in explicit["rows"]])
values = {f["key"]: f["value"] for f in implicit["filters"]}
for key, value in filters.items():
self.assertEqual(values[key], value)
maps = self.view("maps")
self.assertTrue(maps["groups"])
self.assertTrue(all(group["collapsed"] for group in maps["groups"]))
for category in ("eggs", "usable", "consumables", "materials", "other", "equipment"):
dto = self.view("bag", {"category": category}, simple=True)
self.assertEqual(dto["filters"][0]["value"], category)
self.assertTrue(dto["rows"], category)
if category != "eggs":
self.assertTrue(any(len(row["inventory_ids"]) >= 2 and row["count"] >= 7 for row in dto["rows"]), category)
mixed = self.http.get(self.path("views/bag"), params={"query": "{}", "category": "eggs"})
self.response(mixed, 400)
def test_seeded_maps_tasks_and_exploration_have_business_data(self):
maps = self.view("maps")
stage = next(row for row in maps["rows"] if row["id"] == "0101")
self.assertTrue(stage["unlocked"])
self.assertEqual(len(stage["edits"]["level"]["options"]), 3)
self.assertTrue(stage["costs"])
resource = self.view("maps", {"category": "resource", "only_unlocked": False})
self.assertTrue(resource["rows"])
self.assertTrue(all(row["category"] in ("mine", "ticket") for row in resource["rows"]))
self.assertTrue(any(any(cost["kind"] == "item" for cost in row["costs"]) for row in resource["rows"]))
self.assertTrue(any(any(cost["kind"] == "item" and cost["available"] > 0 for cost in row["costs"]) for row in resource["rows"]))
# Engine deliberately gates full-table task planning until the user
# refreshes tasks or enables that feature. Exercise its real HTTP action.
self.assertEqual(self.manager._worker(self.account).engine.snapshot.raw["currentTasks"][0]["typeId"], "1001004")
self.action("refresh_tasks")
mainline = self.view("mainline")
self.assertTrue(mainline["rows"])
self.assertTrue(any(str(row.get("task_type_id")) == "1001004" for row in mainline["rows"]))
config = self.config()["config"]
config["explore_policy"].update(enabled=True, teams={"1": {"enabled": True, "area": "1", "monsters": [101, 102, 103]}})
self.save(config)
explore = self.view("exploration")
self.assertTrue(explore["rows"])
team = next(row for row in explore["rows"] if str(row["id"]) == "1")
self.assertEqual(team["rule_path"], ["explore_policy", "teams", "1"])
self.assertTrue(team["edits"]["monsters"]["options"])
self.assertEqual(self.manager._worker(self.account).engine.explore_policy.teams["1"].monsters, [101, 102, 103])
self.assertEqual(team["monsters"], [101, 102, 103])
self.assertGreater(team["cost_ti"], 0)
self.assertTrue(any(action.kind == "explore_start" for action in self.manager._worker(self.account).engine.plans()))
self.assertFalse(self.manager.status(self.account)["running"])
def test_job_stays_running_until_persistence_and_status_summary_are_published(self):
account = self.create_account(False, "publication barrier")
self.action("login", {"account": "barrier-account", "password": PASSWORD, "remember": False}, account)
worker = self.manager._worker(account)
gate = SnapshotGate()
self.stack.callback(gate.release.set)
persist = worker._persist_session
def blocked_persist():
if worker.engine.client is not None:
gate.wait()
persist()
with patch.object(worker, "_persist_session", blocked_persist):
value = self.response(self.http.post(self.path("actions/enter_server", account), json={"server_id": 7}), 202)
self.assertTrue(gate.entered.wait(3))
try:
current = self.response(self.http.get(self.path("jobs/" + value["id"], account)))
self.assertEqual(current["status"], "running", "A terminal job must not precede persisted/public status")
finally:
gate.release.set()
self.job(value, account)
self.assertTrue(self.response(self.http.get(self.path("status", account)))["connected"])
def test_seeded_sqlite_statistics_compute_rates_sources_and_stamina(self):
for source, samples, units, rate, efficiency in (("sweep", 2, 2, .5, .1), ("event", 1, 3, 1, .6), ("all", 3, 5, 2/3, .2)):
with self.subTest(source=source):
dto = self.view("statistics", {"stage_id": "0101", "level": "3", "source": source})
row = next(row for row in dto["rows"] if row["id"] == "item:30005")
self.assertEqual((row["samples"], row["units"]), (samples, units))
self.assertAlmostEqual(row["rate"], rate)
self.assertAlmostEqual(row["per_stamina"], efficiency)
self.assertTrue(row["interval"])
point = next(point for point in dto["charts"][0]["series"] if point["id"] == row["id"])
self.assertAlmostEqual(point["value"], rate)
dto = self.view("statistics", {"stage_id": "0101", "level": "3", "metric": "per_stamina"})
point = next(point for point in dto["chart"]["series"] if point["id"] == "item:30005")
self.assertAlmostEqual(point["value"], .2)
def test_dto_egg_edit_autosaves_into_real_engine_plan_and_restart_stays_stopped(self):
config, row = self.hatch_config()
column = next(column for column in self.view("bag")["columns"] if column["key"] == "enabled")
self.assertEqual([row["id"] if part == "{id}" else part for part in column["edit"]["path"]], row["rule_path"] + ["enabled"])
applied = self.save(config)
engine = self.manager._worker(self.account).engine
self.assertEqual(engine.policy.egg_rules[row["id"]]["priority"], 13)
plans = engine.plans()
self.assertTrue(plans)
self.assertTrue(all(plan.kind == "hatch" for plan in plans))
self.assertTrue(all(plan.payload["eggs"][0]["itemId"] in row["inventory_ids"] for plan in plans))
preview = self.view("plans")
self.assertTrue(preview["rows"])
self.assertFalse(engine.auto)
self.action("start")
self.assertTrue(self.manager.status(self.account)["running"])
self.manager.close()
from ranch_web.service import WebManager
self.manager = WebManager(self.root, start_scheduler=False, engine_factory=real_engine_factory, login_factory=self.login)
self.app.state.manager = self.manager
self.wait_idle()
self.assertEqual(self.config(), applied)
self.assertFalse(self.manager.status(self.account)["running"])
self.assertFalse(self.manager._worker(self.account).engine.auto)
def test_same_species_config_and_catalog_overrides_are_independent_between_accounts(self):
other = self.create_account(label="account B")
first, row = self.hatch_config()
second, other_row = self.hatch_config(other, enabled=False, priority=91)
self.assertEqual(row["id"], other_row["id"])
first["overrides"] = {row["id"]: {"sell_money": 123456}}
self.save(first)
self.save(second, other)
engine_a, engine_b = (self.manager._worker(a).engine for a in (self.account, other))
self.assertIsNot(engine_a.catalog, engine_b.catalog)
self.assertEqual(engine_a.catalog.monster(row["id"])["sell_money"], 123456)
self.assertNotEqual(engine_b.catalog.monster(row["id"])["sell_money"], 123456)
self.assertTrue(engine_a.plans())
self.assertEqual(engine_b.plans(), [])
self.assertEqual(next(r for r in self.view("bag", account=other)["rows"] if r["id"] == row["id"])["priority"], 91)
def test_http_reads_with_running_real_engine_do_not_dispatch_game_writes(self):
account, client = self.logged_account()
config, _ = self.hatch_config(account)
self.save(config, account)
self.action("start", account=account)
reads = client.reads
before = deepcopy(client.raw)
for page in PAGES:
self.view(page, account=account)
self.response(self.http.get(self.path("status", account)))
self.assertEqual(client.writes, [])
self.assertEqual(client.reads, reads)
self.assertEqual(client.raw, before)
self.assertTrue(self.manager.status(account)["running"])
def test_blocked_game_job_keeps_http_cached_views_and_other_account_available(self):
account, client = self.logged_account()
cached = self.view("bag", account=account)
gate = client.gate = SnapshotGate()
self.stack.callback(gate.release.set)
refresh = self.response(self.http.post(self.path("actions/refresh", account), json={"page": "incubator"}), 202)
self.assertTrue(gate.entered.wait(3))
for endpoint in ("/api/health", "/api/meta", self.path("status", account), self.path("logs", account), self.path("config", account), self.path("views/bag", account), self.path("views/mainline", account)):
with self.subTest(endpoint=endpoint):
start = time.monotonic()
result = self.response(self.http.get(endpoint))
self.assertLess(time.monotonic() - start, 1.5, endpoint)
self.assertFalse(gate.release.is_set())
if endpoint.endswith("views/bag"):
self.assertEqual(result["data"], cached)
self.assertEqual(self.manager.job(account, refresh["id"])["status"], "running")
self.assertTrue(self.view("bag")["rows"])
old = self.config(account)
config = deepcopy(old["config"])
config["interval"] = 60
pending = self.response(self.http.put(self.path("config", account), json={"config": config, "revision": old["revision"]}), 202)
self.assertTrue(pending["pending"])
self.assertEqual(pending["revision"], old["revision"])
self.assertEqual(self.config(account), old)
gate.release.set()
self.job(refresh, account)
self.job(pending["job"], account)
self.assertEqual(self.config(account)["config"]["interval"], 60)
def test_stop_cancels_queued_start_without_later_resurrection(self):
account, client = self.logged_account()
config, _ = self.hatch_config(account)
self.save(config, account)
gate = client.gate = SnapshotGate()
self.stack.callback(gate.release.set)
refresh = self.response(self.http.post(self.path("actions/refresh", account), json={}), 202)
self.assertTrue(gate.entered.wait(3))
start = self.response(self.http.post(self.path("actions/start", account), json={}), 202)
self.assertEqual(self.manager.job(account, start["id"])["status"], "queued")
begin = time.monotonic()
stop = self.response(self.http.post(self.path("actions/stop", account), json={}), 202)
self.assertLess(time.monotonic() - begin, 1.5)
self.assertEqual(stop["status"], "succeeded")
self.assertFalse(self.manager.status(account)["running"])
self.assertFalse(self.manager._worker(account).engine.auto)
self.job(start, account, "cancelled")
gate.release.set()
self.job(refresh, account)
self.wait_idle(account)
self.assertFalse(self.manager.status(account)["running"])
self.assertFalse(self.manager._worker(account).engine.auto)
self.assertEqual(client.writes, [])
def test_legacy_schema_one_import_is_a_job_and_never_starts_engine(self):
from ranch_assistant.model import Policy
from ranch_assistant.policy_io import portable_policy
config, row = self.hatch_config()
self.save(config)
self.action("start")
engine = self.manager._worker(self.account).engine
bundle = portable_policy(Policy(**config["policy"]), engine.snapshot)
self.assertEqual((bundle["schema"], bundle["assistantVersion"]), (1, "1.2.0"))
bundle["policy"].update(enabled=True, fill=True)
before = deepcopy(engine.snapshot.raw)
value = self.response(self.http.post(self.path("config/import"), json={"config": bundle}), 202)
self.job(value)
self.assertFalse(self.manager.status(self.account)["running"])
self.assertFalse(engine.auto)
self.assertEqual(engine.snapshot.raw, before)
self.assertTrue(self.config()["config"]["policy"]["egg_rules"][row["id"]]["enabled"])
exported = self.job(self.response(self.http.get(self.path("config/export")), 202))["result"]
self.assertEqual(exported["schema"], 2)
self.assertEqual(exported["format"], "ranch-helper-policies")
def test_yield_csv_import_is_saved_and_changes_real_view(self):
row = self.view("bag")["rows"][0]
content = f"typeId,sell_money,sell_exp,time_total\n{row['id']},7777,123,600\n".encode()
result = self.action("import_yields", {"filename": "uploaded.csv", "content_base64": base64.b64encode(content).decode()})["result"]
self.assertEqual(result["count"], 1)
self.assertEqual(self.config()["config"]["overrides"][row["id"]]["sell_money"], 7777)
view = self.view("yields", {"search": row["id"]})
self.assertEqual(next(r for r in view["rows"] if r["id"] == row["id"])["sell_money"], 7777)
def test_http_manual_lock_sell_and_feed_confirmations_reach_real_engine(self):
engine = self.manager._worker(self.account).engine
self.action("lock", {"monster_ids": [101]})
self.assertTrue(engine.snapshot.monsters[101]["lock"])
self.action("unlock", {"monster_ids": [101]})
self.assertFalse(engine.snapshot.monsters[101]["lock"])
before = deepcopy(engine.snapshot.raw)
self.action("sell", {"monster_ids": [101]}, expected="failed")
self.assertEqual(engine.snapshot.raw, before)
self.action("sell", {"monster_ids": [101], "confirm": True})
self.assertNotIn(101, engine.snapshot.monsters)
self.action("feed", {"monster_ids": [102], "target_id": 900}, expected="failed")
self.assertIn(102, engine.snapshot.monsters)
self.action("feed", {"monster_ids": [102], "target_id": 900, "confirm": True})
self.assertNotIn(102, engine.snapshot.monsters)
self.assertIn(900, engine.snapshot.monsters)
self.action("sell", {"monster_ids": [900], "confirm": True}, expected="failed")
self.assertIn(900, engine.snapshot.monsters)
self.assertFalse(self.manager.status(self.account)["running"])
def test_http_expansion_quote_is_confirmed_once_and_bound_to_account(self):
other = self.create_account(label="quote scope")
quote = self.action("quote_expansion", {"kind": "stack", "currency": "money", "position": 6})["result"]
self.assertGreater(quote["cost"], 0)
before = self.manager._worker(self.account).engine.snapshot.raw["master"]["money"]
self.action("expand", {"quote_id": quote["quote_id"], "confirm": True}, other, expected="failed")
self.action("expand", {"quote_id": quote["quote_id"]}, expected="failed")
self.assertEqual(self.manager._worker(self.account).engine.snapshot.raw["master"]["money"], before)
self.action("expand", {"quote_id": quote["quote_id"], "confirm": True})
engine = self.manager._worker(self.account).engine
self.assertEqual(engine.snapshot.raw["master"]["money"], before - quote["cost"])
self.assertTrue(any(row["position"] == 6 for row in engine.snapshot.raw["stacks"]))
self.assertEqual(self.response(self.http.get(self.path("status")))["player"]["money"], before - quote["cost"])
self.action("expand", {"quote_id": quote["quote_id"], "confirm": True}, expected="failed")
self.assertEqual(engine.snapshot.raw["master"]["money"], before - quote["cost"])
def test_local_auth_host_csrf_and_remote_client_boundary(self):
from fastapi.testclient import TestClient
before = self.response(self.http.get("/api/accounts"))
invalid_host = self.response(self.http.post("/api/accounts", json={"demo": True}, headers={"Host": "rebind.invalid"}), 400)
self.assertEqual(invalid_host["error"]["code"], "invalid_host")
for headers in ({"Origin": "https://cross-site.invalid"}, {"Sec-Fetch-Site": "cross-site"}):
self.response(self.http.post("/api/accounts", json={"demo": True}, headers=headers), 403)
self.assertEqual(self.response(self.http.get("/api/accounts")), before)
self.response(self.http.post("/api/accounts", json={"demo": True}, headers={"Origin": "http://127.0.0.1:8765", "Sec-Fetch-Site": "same-origin"}), 201)
with TestClient(self.app, base_url="http://127.0.0.1:8765", client=("203.0.113.2", 51234)) as remote:
self.response(remote.get("/api/accounts", headers={"X-Forwarded-For": "127.0.0.1", "X-Forwarded-Host": "localhost"}), 403)
def test_password_auth_cookie_revocation_and_cross_site_login(self):
from fastapi.testclient import TestClient
from ranch_web.app import create_app
from ranch_web.config import COOKIE_NAME, WebConfig
app = create_app(manager=self.manager, updates=self.updates,
config=WebConfig(data_dir=self.root, password="integration-admin-password"))
with TestClient(app, base_url="http://127.0.0.1:8765", client=("127.0.0.1", 50001)) as http:
self.response(http.get("/api/accounts"), 401)
self.response(http.post("/api/auth/login", json={"password": "wrong"}), 401)
self.response(http.post("/api/auth/login", json={"password": "integration-admin-password"}, headers={"Origin": "https://cross-site.invalid"}), 403)
response = http.post("/api/auth/login", json={"password": "integration-admin-password"})
self.response(response)
self.assertIn("httponly", response.headers["set-cookie"].lower())
self.assertIn("samesite=strict", response.headers["set-cookie"].lower())
token = http.cookies.get(COOKIE_NAME)
self.assertTrue(token)
self.assertNotIn("integration-admin-password", token)
self.response(http.get("/api/accounts"))
self.response(http.post("/api/auth/logout"))
self.response(http.get("/api/accounts", headers={"Cookie": COOKIE_NAME + "=" + token}), 401)
def assert_no_secrets(self, value):
encoded = json.dumps(value, ensure_ascii=False)
for secret in (TOKEN, KEY, PASSWORD, str(REQID), str(REQSID), str(self.root), self.root.as_posix()):
self.assertNotIn(secret, encoded)
self.assertNotIn("Traceback (most recent call last)", encoded)
def test_game_credentials_are_absent_from_http_views_config_jobs_and_logs(self):
account, client = self.logged_account()
for page in PAGES:
self.assert_no_secrets(self.view(page, account=account))
for suffix in ("status", "config", "logs", "logs?debug=true"):
self.assert_no_secrets(self.response(self.http.get(self.path(suffix, account))))
self.assert_no_secrets(self.action("export_config", account=account))
client.fail_snapshot = True
failed = self.action("refresh", account=account, expected="failed")
self.assert_no_secrets(failed)
self.assert_no_secrets(self.response(self.http.get(self.path("logs?debug=true", account))))
self.assert_no_secrets(self.response(self.http.get(self.path("status", account))))
def test_lazy_log_detail_is_available_and_keeps_structure_with_secrets_masked(self):
account, _ = self.logged_account()
logs = self.response(self.http.get(self.path("logs?debug=true", account)))
entries = [entry for entry in logs["entries"] if entry.get("details_available")]
self.assertTrue(entries, logs)
self.assertTrue(all("body" not in entry and "details" not in entry for entry in logs["entries"]))
detail = self.response(self.http.get(self.path("logs/" + str(entries[-1]["id"]), account)))
self.assert_no_secrets(detail)
self.assertIn("integration-readable-detail", json.dumps(detail))
self.assertIn("body", json.dumps(detail))
def test_updates_use_fake_source_and_no_stable_v2_means_no_upgrade(self):
self.response(self.http.get("/api/meta"))
self.response(self.http.get("/api/system/updates"))
self.source.assert_not_called()
for releases in ([], [{"tag_name": "v1.2.0", "draft": False, "prerelease": False}],
[{"tag_name": "v2.0.0.dev1", "draft": False, "prerelease": True},
{"tag_name": "v2.0.0", "draft": True, "prerelease": False},
{"tag_name": "v3.0.0", "draft": False, "prerelease": False}]):
self.source.return_value = releases
result = self.response(self.http.post("/api/system/updates/check"))
self.assertFalse(result["available"])
self.assertIsNone(result["latest_version"])
self.assertEqual(result["status"], "no_release")
self.assertEqual(self.source.call_count, 3)
result = self.response(self.http.post("/api/system/updates/apply", json={}), 202)
self.assertEqual(result["status"], "unavailable")
self.assertFalse((self.root / "update-request.json").exists())
def test_config_revisions_and_invalid_input_are_atomic(self):
old = self.config()
value = deepcopy(old["config"])
value["interval"] = 60
applied = self.save(value)
self.response(self.http.put(self.path("config"), json={"config": old["config"], "revision": old["revision"]}), 409)
bad = deepcopy(value)
bad["policy"]["egg_rules"] = {"1001010": {"enabled": "false"}}
self.response(self.http.put(self.path("config"), json={"config": bad, "revision": applied["revision"]}), 400)
self.assertEqual(self.config(), applied)
self.response(self.http.post(self.path("actions/import_yields"), json={"path": "C:/private/account.csv"}), 400)
def _page_test(page):
def check(self):
self.assert_dto(page)
check.__name__ = "test_page_" + page
return check
for _page in PAGES:
setattr(WebIntegrationTests, "test_page_" + _page, _page_test(_page))
class FreshProcessIntegrationTests(unittest.TestCase):
def child(self, program):
env = {key: value for key, value in os.environ.items() if not key.startswith("RANCH_WEB_")}
env["PYTHONIOENCODING"] = "utf-8"
result = subprocess.run([sys.executable, "-X", "utf8", "-c", textwrap.dedent(program)],
cwd=ROOT, env=env, capture_output=True, text=True, encoding="utf-8", timeout=35,
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0))
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
return json.loads(result.stdout.strip().splitlines()[-1])
def test_fresh_web_process_blocks_gui_imports_and_serves_demo_strategy(self):
result = self.child('''
import importlib.abc, json, sys, tempfile, time
from pathlib import Path
attempted = []
class BlockGUI(importlib.abc.MetaPathFinder):
def find_spec(self, fullname, path=None, target=None):
if fullname.startswith('PySide6') or fullname.endswith('_gui') or fullname == 'ranch_assistant.gui':
attempted.append(fullname)
raise ImportError('GUI import blocked in fresh Web integration process')
sys.meta_path.insert(0, BlockGUI())
from ranch_web.tests.test_web_integration import NoExternalSockets, real_engine_factory
from ranch_web.app import create_app
from ranch_web.config import WebConfig
from ranch_web.service import WebManager
from fastapi.testclient import TestClient
with NoExternalSockets() as network, tempfile.TemporaryDirectory(prefix='web-fresh-process-') as temporary:
manager = WebManager(Path(temporary) / 'new-root', start_scheduler=False, engine_factory=real_engine_factory)
try:
app = create_app(manager=manager, config=WebConfig(data_dir=Path(temporary) / 'new-root'))
with TestClient(app, base_url='http://127.0.0.1', client=('127.0.0.1', 50000)) as http:
assert http.get('/api/health').json()['status'] == 'ok'
account = http.post('/api/accounts', json={'demo': True}).json()['id']
value = http.get('/api/accounts/' + account + '/config').json()
value['config']['interval'] = 60
saved = http.put('/api/accounts/' + account + '/config', json=value)
assert saved.status_code in (200, 202), saved.text
deadline = time.monotonic() + 8
while manager.status(account)['busy'] and time.monotonic() < deadline:
time.sleep(.005)
assert http.get('/api/accounts/' + account + '/config').json()['config']['interval'] == 60
assert http.get('/api/schema').status_code == 200
view = http.get('/api/accounts/' + account + '/views/bag').json()
if view['loading']:
deadline = time.monotonic() + 8
while manager.job(account, view['job_id'])['status'] not in ('succeeded', 'failed') and time.monotonic() < deadline:
time.sleep(.005)
bag = http.get('/api/accounts/' + account + '/views/bag').json()['data']
assert bag['rows'] and bag['columns'][0]['key']
assert not network.attempts
finally:
manager.close()
assert not attempted, attempted
assert not any(n.startswith('PySide6') for n in sys.modules)
print(json.dumps({'health': 'ok', 'demo_rows': len(bag['rows']), 'interval': 60, 'blocked_import_attempts': attempted}))
''')
self.assertEqual(result["blocked_import_attempts"], [])
self.assertGreater(result["demo_rows"], 0)
def test_real_engine_battle_worker_subprocess_still_runs_after_web_imports(self):
result = self.child('''
import json, tempfile
from pathlib import Path
from unittest.mock import patch
import subprocess
from ranch_web.app import create_app
from ranch_web.service import WebManager
from ranch_web.tests.test_web_integration import NoExternalSockets, real_engine_factory
from ranch_assistant.battle_simulator import combat_tables, simulate
row = combat_tables()['monster_info']['1001010']
mon = {'id': 101, 'typeId': 1001010, 'level': 20, 'mode': 2, 'tpLevel': 0, 'feel': 0, 'upStar': 0, 'pAtk': 0, 'pDef': 0, 'pHp': 0, 'pDex': 0}
for name in ('Atk', 'Def', 'Hp', 'Dex'):
mon['g' + name] = (row[name.lower() + '_grow_min'] + row[name.lower() + '_grow_max']) / 2
enemy = {'type': '1001010', 'team': 2, 'mode': 2, 'level': 1, 'atk': 16, 'def': 20, 'hp': 101, 'dex': 4, 'spd_mov': .92, 'spd_atk': 1595, 'atk_dis': 100, 'skill_1': 0, 'skill_2': 0, 'joinTime': -1, 'wave': 1, 'teamPos': 1, 'ai': 0, 'joinPoint': 'point_team2_fore2', 'controller': []}
request = {'context': 'ordinary_stage', 'seed': 0, 'team': 'teamA',
'battle_position': {'battleType': 'stage', 'stageId': 'fixture', 'stageLevel': '1'},
'roster': {'master': {'level': 43}, 'monsters': [mon], 'teams': [{'teamName': 'teamA', 'monsters': [101]}], 'monsterAttributes': []},
'original_data': {'battle': {'time': 30, 'winType': ['1'], 'loseType': ['1', '2'], 'controller': [], 'triggers': [],
'teams': [{'team': 2, 'numMax': 2, 'ap': 80, 'mons': [enemy]}],
'battleField': {'fieldMode': True, 'fieldName': 'default', 'mapAreas': [], 'mapObjects': []}}}}
with NoExternalSockets() as network, tempfile.TemporaryDirectory(prefix='web-battle-process-') as temporary:
manager = WebManager(Path(temporary) / 'new-root', start_scheduler=False, engine_factory=real_engine_factory)
try:
account = manager.create_account(demo=True)['id']
engine = manager._worker(account).engine
engine._select_battle_runtime('experimental')
with patch('subprocess.Popen', wraps=subprocess.Popen) as launch:
result = engine.battles.runtime.run(request, timeout=15)
assert launch.call_count == 1
assert 'ranch_assistant.battle_simulator' in launch.call_args.args[0]
assert result == simulate(request), result
assert result['outcome'] == 'win', result
assert not network.attempts
print(json.dumps({'outcome': result['outcome'], 'execution_mode': result['execution_mode'], 'real_worker_processes': launch.call_count}))
finally:
manager.close()
''')
self.assertEqual(result["real_worker_processes"], 1)
self.assertEqual(result["outcome"], "win")
if __name__ == "__main__":
unittest.main()