from __future__ import annotations
import os
import tempfile
from mock_monarch.server import reset_fixtures, start_server
from support.mcp_client import McpClient
def before_all(context):
os.environ["MONARCH_NOW"] = "2026-05-15"
_thread, port = start_server()
context.mock_port = port
context.mock_base = f"http://127.0.0.1:{port}"
def before_scenario(context, scenario):
reset_fixtures()
context._transactions = []
context._budgets = []
context.prior_month_spending = 0.0
tmp = tempfile.NamedTemporaryFile(
mode="w", suffix=".toml", delete=False, prefix="monarch_goals_"
)
tmp.write("") tmp.close()
context.goals_file = tmp.name
os.environ["MONARCH_GOALS_FILE"] = tmp.name
client = McpClient(
monarch_base=context.mock_base,
goals_file=context.goals_file,
)
context.mcp_client = client
context.mcp_start_error = None
try:
client.start()
except FileNotFoundError as exc:
context.mcp_start_error = exc
def after_scenario(context, scenario):
if hasattr(context, "mcp_client") and context.mcp_client is not None:
context.mcp_client.stop()
if hasattr(context, "goals_file") and context.goals_file:
try:
os.unlink(context.goals_file)
except OSError:
pass