Bundled MAP_PROMPT/SUMMARY_PROMPT from the customer1 deepseek configmap. Env wins over prompt files; blank compose injection is treated as unset. Parser maps market_overview JSON onto the dashboard ticker/map contract.
97 lines
3.5 KiB
Python
97 lines
3.5 KiB
Python
from intel import parse_reduce_json, clamp_coords, select_ticker, select_map
|
|
|
|
FENCED = """```json
|
|
{"summary_en": "Brief.", "ticker": [
|
|
{"headline": "Blast in Kyiv", "importance": "critical", "url": "https://ex", "location_name": "Kyiv"}
|
|
], "map_items": [
|
|
{"headline": "Blast in Kyiv", "importance": "critical", "location_name": "Kyiv, Ukraine",
|
|
"lat": 50.45, "lon": 30.52, "location_confidence": "city", "category": "military/conflict", "url": "https://ex"}
|
|
]}
|
|
```"""
|
|
|
|
def test_parse_strips_fence_and_think_tags():
|
|
raw = "<think>nope</think>\n" + FENCED
|
|
out = parse_reduce_json(raw)
|
|
assert out["summary_en"] == "Brief."
|
|
assert len(out["ticker"]) == 1
|
|
|
|
def test_parse_empty_and_garbage_returns_empty_struct():
|
|
assert parse_reduce_json("")["summary_en"] == ""
|
|
assert parse_reduce_json("not json")["ticker"] == []
|
|
|
|
def test_clamp_coords_drops_out_of_range_and_unknown():
|
|
assert clamp_coords(50.45, 30.52) == (50.45, 30.52)
|
|
assert clamp_coords(95.0, 10.0) is None
|
|
assert clamp_coords(None, 10.0) is None
|
|
assert clamp_coords("50.45", "30.52") == (50.45, 30.52)
|
|
|
|
def test_select_ticker_keeps_critical_high_caps_12():
|
|
rows = [{"headline": f"h{i}", "importance": "critical"} for i in range(15)]
|
|
rows.append({"headline": "skip", "importance": "low"})
|
|
out = select_ticker(rows)
|
|
assert len(out) == 12
|
|
assert all(r["importance"] in ("critical", "high") for r in out)
|
|
|
|
def test_select_map_requires_valid_coords_and_flag():
|
|
items = [
|
|
{"headline": "A", "importance": "critical", "lat": 50.45, "lon": 30.52, "location_name": "Kyiv"},
|
|
{"headline": "B", "importance": "critical", "lat": None, "lon": None, "location_name": "Unknown"},
|
|
{"headline": "C", "importance": "low", "lat": 1.0, "lon": 2.0, "location_name": "x"},
|
|
]
|
|
out = select_map(items)
|
|
assert [r["headline"] for r in out] == ["A"]
|
|
|
|
def test_select_map_caps_20():
|
|
items = [
|
|
{"headline": f"h{i}", "importance": "critical", "lat": 1.0, "lon": 2.0}
|
|
for i in range(25)
|
|
]
|
|
out = select_map(items)
|
|
assert len(out) == 20
|
|
assert all(r["importance"] in ("critical", "high") for r in out)
|
|
|
|
|
|
def test_parse_k8s_market_intel_reduce_json():
|
|
raw = """{
|
|
"market_overview": {
|
|
"overall_sentiment": "bearish",
|
|
"sentiment_score": -40,
|
|
"key_themes": ["oil supply", "rates"],
|
|
"critical_events": [
|
|
{
|
|
"headline": "Strike on Kharkiv",
|
|
"impact_level": "high",
|
|
"description": "Infrastructure hit.",
|
|
"location_name": "Kharkiv",
|
|
"lat": 49.99,
|
|
"lon": 36.23
|
|
},
|
|
{
|
|
"headline": "Mild CPI print",
|
|
"impact_level": "low",
|
|
"description": "No surprise."
|
|
}
|
|
]
|
|
},
|
|
"geopolitical_osint": {
|
|
"active_conflicts": [
|
|
{
|
|
"region": "Ukraine",
|
|
"status": "escalating",
|
|
"intelligence": "Front-line push near Kharkiv",
|
|
"lat": 50.0,
|
|
"lon": 36.2
|
|
}
|
|
]
|
|
}
|
|
}"""
|
|
out = parse_reduce_json(raw)
|
|
assert "bearish" in out["summary_en"]
|
|
assert "Strike on Kharkiv" in out["summary_en"]
|
|
assert [t["headline"] for t in out["ticker"]] == ["Strike on Kharkiv"]
|
|
assert out["ticker"][0]["importance"] == "high"
|
|
mapped = select_map(out["map_items"])
|
|
headlines = [r["headline"] for r in mapped]
|
|
assert "Front-line push near Kharkiv" in headlines
|
|
assert "Strike on Kharkiv" in headlines
|
|
|