Files
ag-bids-mcp/tests/test_format.py
T
justin 875a190983 Initial commit: ag-bids MCP server
Exposes live + historical ag-bids commodity data (from the ag-monitor
service at agbids.paul.farm) as MCP tools, sitting behind MetaMCP at
https://mcp.jpaul.io/metamcp/ag-bids/mcp.

Pattern mirrors zerto-docs-rag with one addition: HTTP Basic auth in
front of the streamable-HTTP transport so namespace guessers can't reach
the tools. Stdio transport is unaffected (used by local Claude Desktop
dev).

Tools (markdown returns, ~15 LOC each):
  best_local_bid(commodity)       — where to sell corn/soy/wheat today,
                                    for the current calendar month only
  current_lime_price()            — latest lime quotes ($/ton)
  current_input_price(product?)   — MAP / Potash / Lime
  latest_prices(...)              — filtered snapshot
  price_history(...)              — per-(source,delivery) trend
  list_sources / list_commodities / list_deliveries
  source_health()                 — healthy / stale / down buckets
  todays_summary()                — same shape as morning brief snapshot

Data path: ag-bids-mcp -> X-API-Key -> /api/data/* on ag-monitor
(reuses BRIEF_API_KEY).

Tests: 24 covering the httpx client, markdown formatters, HTTP Basic
middleware (401/200), and JSONL usage logging.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 11:37:46 -04:00

164 lines
5.7 KiB
Python

"""Markdown formatter tests — no network, just JSON-in / markdown-out."""
from __future__ import annotations
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from ag_bids_mcp import format as fmt
def test_fmt_best_with_winner():
payload = {
"commodity": "corn", "today": "2026-05-20",
"best": {
"source_name": "Mercer Landmark — St Henry",
"delivery": "May 2026",
"bid_cents": 491, "basis_cents": 33,
"futures_contract": "ZCK26",
"fetched_at": "2026-05-20T15:00:00+00:00",
},
}
out = fmt.fmt_best("corn", payload)
assert "Mercer Landmark — St Henry" in out
assert "$4.9100/bu" in out
assert "May 2026" in out
assert "+0.33" in out
def test_fmt_best_no_winner():
payload = {"commodity": "wheat", "today": "2026-05-20", "best": None}
out = fmt.fmt_best("wheat", payload)
assert "No current-month" in out
assert "wheat" in out
def test_fmt_inputs_lime_table():
payload = {
"product": "lime", "count": 2,
"rows": [
{"source_name": "Bob's Lime Quote", "commodity": "lime",
"display_name": "Lime", "delivery": "spot",
"bid_cents": 42000, "commodity_kind": "fertilizer",
"fetched_at": "2026-05-20T15:00:00+00:00"},
{"source_name": "Coop X", "commodity": "lime",
"display_name": "Lime", "delivery": "spot",
"bid_cents": 45500, "commodity_kind": "fertilizer",
"fetched_at": "2026-05-20T15:01:00+00:00"},
],
}
out = fmt.fmt_inputs(payload)
assert "LIME prices" in out
assert "$420.00" in out
assert "$455.00" in out
assert "Bob's Lime Quote" in out
def test_fmt_inputs_empty():
out = fmt.fmt_inputs({"product": "lime", "rows": []})
assert "No lime prices on file." in out
def test_fmt_latest_table_picks_correct_unit():
payload = {"count": 2, "rows": [
{"source_name": "Bambauer", "commodity": "corn",
"display_name": "Corn", "delivery": "May 2026",
"bid_cents": 458, "basis_cents": 2, "commodity_kind": "grain",
"futures_contract": "ZCN26", "fetched_at": "2026-05-20T15:00:00+00:00"},
{"source_name": "Coop X", "commodity": "lime",
"display_name": "Lime", "delivery": "spot",
"bid_cents": 42000, "basis_cents": None, "commodity_kind": "fertilizer",
"futures_contract": None, "fetched_at": "2026-05-20T15:00:00+00:00"},
]}
out = fmt.fmt_latest(payload)
assert "$4.5800" in out # grain in /bu (4 decimals)
assert "$420.00" in out # fertilizer in /ton (2 decimals)
def test_fmt_history_with_trend_arrow():
payload = {
"commodity": "corn", "days": 7,
"rows": [
{"source_name": "Andersons", "delivery": "May 2026",
"bid_cents": 480, "basis_cents": 25,
"fetched_at": "2026-05-15T15:00:00+00:00"},
{"source_name": "Andersons", "delivery": "May 2026",
"bid_cents": 496, "basis_cents": 30,
"fetched_at": "2026-05-20T15:00:00+00:00"},
],
}
out = fmt.fmt_history(payload)
assert "▲" in out
assert "$4.8000" in out
assert "$4.9600" in out
def test_fmt_sources_table():
payload = {"sources": [
{"id": 1, "name": "Test Elev", "kind": "elevator",
"last_success_at": "2026-05-20T14:55:00+00:00",
"consecutive_failures": 0, "last_error": None},
]}
out = fmt.fmt_sources(payload)
assert "Test Elev" in out
assert "elevator" in out
def test_fmt_health_buckets():
payload = {"sources": [
{"name": "Healthy", "kind": "elevator",
"last_success_at": "2026-05-20T14:55:00+00:00",
"consecutive_failures": 0, "last_error": None},
{"name": "Stale", "kind": "elevator",
"last_success_at": None,
"consecutive_failures": 0, "last_error": None},
{"name": "Down", "kind": "elevator",
"last_success_at": "2026-05-15T14:55:00+00:00",
"consecutive_failures": 5, "last_error": "boom"},
]}
out = fmt.fmt_health(payload)
assert "Healthy: **1**" in out
assert "Stale (never succeeded): **1**" in out
assert "Down" in out and "**1**" in out
# Down section lists boom
assert "boom" in out
def test_fmt_deliveries():
out = fmt.fmt_deliveries({"commodity": "corn",
"deliveries": ["May 2026", "Jul 2026"]})
assert "- May 2026" in out
assert "- Jul 2026" in out
def test_fmt_commodities_lists_all_six():
out = fmt.fmt_commodities()
for name in ("corn", "soy", "wheat", "map", "potash", "lime"):
assert name in out.lower()
def test_fmt_summary_includes_best_for_today():
payload = {
"today": "2026-05-20", "prev_trading_day": "2026-05-19",
"commodities": [
{"symbol": "corn", "display_name": "Corn",
"futures": {"contract": "ZC=F", "today_last_cents": 458,
"prev_close_cents": 455, "change_cents": 3},
"best_for_today": {
"source_name": "Mercer Landmark — St Henry",
"delivery": "May 2026", "bid_cents": 491, "basis_cents": 33,
}},
{"symbol": "soy", "display_name": "Soybeans",
"futures": {"contract": "ZS=F", "today_last_cents": 1180,
"prev_close_cents": 1177, "change_cents": 3},
"best_for_today": None},
],
}
out = fmt.fmt_summary(payload)
assert "Corn" in out and "Soybeans" in out
assert "Mercer Landmark — St Henry" in out
assert "$4.5800" in out # corn last
assert "No current-month local bid posted" in out # soy fallback