"""puga.network.combine(): the transfer/freight logic, with fake simulate() results (no network calls).""" import pytest from puga import network def fake_results(monkeypatch, per_base): """per_base: {name: flows dict} -> patches simulate() to return {"flows": ..., "profit": sum handled by caller}.""" def fake_simulate(plan, recipes, buildings, resources, fertility, price, faction=None, permits=(1, 2), built=None): return {"flows": per_base[plan["_name"]], "profit": per_base[plan["_name"]].pop("_profit")} monkeypatch.setattr(network, "simulate", fake_simulate) def node(name): return network.BaseNode(name, {"_name": name}, [], [], [], 0.0, lambda t, side="both": 100.0) def test_transfer_fully_matched_only_charges_freight(monkeypatch): fake_results(monkeypatch, { "a": {"LST": {"out": 20.0, "inp": 0.0}, "_profit": 1000.0}, "b": {"LST": {"out": 0.0, "inp": 20.0}, "_profit": 500.0}, }) monkeypatch.setattr(network.fio, "materials", lambda: [{"Ticker": "LST", "Weight": 2.73, "Volume": 1.0}]) r = network.combine({"a": node("a"), "b": node("b")}, [network.Transfer("LST", "a", "b", trip_cost=1000, cargo=500)]) t = r["ledger"][0] assert t["moved"] == 20.0 and t["shortfall"] == 0 and t["surplus"] == 0 assert r["total_profit"] == pytest.approx(1000 + 500 - t["freight"]) def test_transfer_shortfall_is_flagged_not_double_charged(monkeypatch): fake_results(monkeypatch, { "a": {"LST": {"out": 5.0, "inp": 0.0}, "_profit": 1000.0}, "b": {"LST": {"out": 0.0, "inp": 20.0}, "_profit": 500.0}, # needs 20, source only makes 5 }) monkeypatch.setattr(network.fio, "materials", lambda: [{"Ticker": "LST", "Weight": 2.73, "Volume": 1.0}]) r = network.combine({"a": node("a"), "b": node("b")}, [network.Transfer("LST", "a", "b", trip_cost=1000, cargo=500)]) t = r["ledger"][0] assert t["moved"] == 5.0 and t["shortfall"] == 15.0 and t["surplus"] == 0 # b's own 'profit' already priced the missing 15 units at market (simulate()'s job) - combine() must not touch that def test_transfer_surplus_is_flagged_and_sold_by_source(monkeypatch): fake_results(monkeypatch, { "a": {"LST": {"out": 40.0, "inp": 0.0}, "_profit": 1000.0}, "b": {"LST": {"out": 0.0, "inp": 9.0}, "_profit": 500.0}, }) monkeypatch.setattr(network.fio, "materials", lambda: [{"Ticker": "LST", "Weight": 2.73, "Volume": 1.0}]) r = network.combine({"a": node("a"), "b": node("b")}, [network.Transfer("LST", "a", "b", trip_cost=1000, cargo=500)]) t = r["ledger"][0] assert t["moved"] == 9.0 and t["surplus"] == 31.0 and t["shortfall"] == 0 def test_freight_scales_with_weight_and_cargo_cap(monkeypatch): fake_results(monkeypatch, { "a": {"LST": {"out": 100.0, "inp": 0.0}, "_profit": 0.0}, "b": {"LST": {"out": 0.0, "inp": 100.0}, "_profit": 0.0}, }) monkeypatch.setattr(network.fio, "materials", lambda: [{"Ticker": "LST", "Weight": 2.73, "Volume": 1.0}]) r = network.combine({"a": node("a"), "b": node("b")}, [network.Transfer("LST", "a", "b", trip_cost=1000, cargo=100)]) t = r["ledger"][0] assert t["weight"] == pytest.approx(273.0) assert t["trips"] == pytest.approx(2.73) # 273 t / 100 t cargo cap assert t["freight"] == pytest.approx(2730.0)