160-tool MCP server for real-time global intelligence from free public APIs: markets, liquidity, prediction markets, SEC filings, conflict, military, aircraft-cohort early warning, migration and border flows, cyber, climate, news and more. Dated observation history, 29 research prompts, cited briefs, geofences, SSE dashboard. MIT, no paid API keys.
Python ai-toolsai-watchanthropicclaudecybersecuritydashboardgeofencinggeopoliticalgeospatialintelligencemarketsmcpmigrationmilitarymodel-context-protocolnuclearosintreal-timespace-weatherthreat-intelligence
"""Tests for analysis/classifier.py — keyword threat classification.
Pure-function tests with realistic headlines. Each positive case is paired
with a negative (a text that must NOT trigger the same outcome) so a broken
classifier cannot pass by over- or under-matching.
"""
from world_intel_mcp.analysis.classifier import (
CATEGORIES,
classify_event,
fetch_classify_event,
)
# Three military keywords ("troops", "artillery", "frontline"), no severity
# modifiers, and no keywords from any other category.
_MILITARY_TEXT = "Russian troops shelled the frontline with artillery near the border"
# No category keyword or severity modifier appears anywhere in this text.
_BENIGN_TEXT = "The bakery unveiled a fresh sourdough recipe on Sunday"
def test_military_headline_classified() -> None:
result = classify_event(_MILITARY_TEXT)
assert result["primary_category"] == "military"
assert (
result["severity"] == CATEGORIES["military"]["severity_base"]
) # 8, no modifiers
# 3 keyword hits -> confidence min(1.0, 3*0.2 + 0.3) = 0.9
assert result["confidence"] == 0.9
assert result["category_count"] == 1
assert result["severity_modifiers"] == []
matched = result["all_categories"][0]["keywords"]
assert "troops" in matched
assert "frontline" in matched
assert result["source"] == "keyword-classifier"
def test_benign_text_is_unclassified() -> None:
"""The load-bearing negative: a classifier that fires on everything
proves nothing when it fires on a real threat."""
result = classify_event(_BENIGN_TEXT)
assert result["primary_category"] == "unclassified"
assert result["severity"] == 0
assert result["confidence"] == 0.0
assert result["all_categories"] == []
assert result["category_count"] == 0
assert result["severity_modifiers"] == []
def test_high_severity_modifier_and_cap_at_ten() -> None:
# 4 nuclear keywords; "nuclear" is also a high-severity modifier (+2),
# so 9 + 2 must cap at 10.
result = classify_event("IAEA warns of uranium enrichment at nuclear facility")
assert result["primary_category"] == "nuclear"
assert result["severity"] == 10
assert result["confidence"] == 1.0 # 4 hits saturates the scale
assert "nuclear" in result["severity_modifiers"]
def test_moderate_severity_modifier_adds_one() -> None:
# Maritime base 5, "attack" is a moderate modifier -> 6.
result = classify_event("Piracy attack on a cargo ship near the strait")
assert result["primary_category"] == "maritime"
assert result["severity"] == 6
assert result["severity_modifiers"] == ["attack"]
def test_high_modifier_wins_over_moderate_no_stacking() -> None:
# Both "killed" (high, +2) and "attack" (moderate, +1) appear; the
# bumps must not stack: maritime base 5 + 2 = 7, not 8.
result = classify_event("Piracy attack near the strait killed three seafarers")
assert result["primary_category"] == "maritime"
assert result["severity"] == 7
assert "killed" in result["severity_modifiers"]
assert "attack" in result["severity_modifiers"]
def test_primary_category_is_most_keyword_hits() -> None:
# cyber matches 2 keywords ("ransomware", "hack"), maritime 1 ("port").
result = classify_event("Ransomware hack hits port operators")
assert result["primary_category"] == "cyber"
assert result["severity"] == CATEGORIES["cyber"]["severity_base"]
cats = [c["category"] for c in result["all_categories"]]
assert cats[0] == "cyber"
assert "maritime" in cats
assert result["category_count"] == 2
def test_single_keyword_confidence_floor() -> None:
# 1 hit -> min(1.0, 0.2 + 0.3) = 0.5
result = classify_event("The satellite mission was a success")
assert result["primary_category"] == "space"
assert result["confidence"] == 0.5
assert result["severity"] == CATEGORIES["space"]["severity_base"]
def test_classification_is_case_insensitive() -> None:
upper = classify_event(_MILITARY_TEXT.upper())
assert upper["primary_category"] == "military"
assert upper["confidence"] == 0.9
async def test_fetch_classify_event_wraps_sync_classifier() -> None:
result = await fetch_classify_event(None, _MILITARY_TEXT)
direct = classify_event(_MILITARY_TEXT)
assert result["primary_category"] == direct["primary_category"]
assert result["severity"] == direct["severity"]
assert result["confidence"] == direct["confidence"]
# ---------------------------------------------------------------------------
# Word-boundary regressions (Phase 24.5): substring matching fired categories
# and severity bumps from the inside of unrelated words — "port" in "report",
# "coup" in "couple". Verb stems must still match their inflections.
# ---------------------------------------------------------------------------
def test_keywords_inside_unrelated_words_do_not_fire() -> None:
# Pre-fix this fired maritime ("port" in "report"), political ("coup" in
# "couple"), social_unrest ("riot" in "patriotic"), and cyber ("apt" in
# "captured").
result = classify_event(
"The report said a couple of patriotic films were captured on camera"
)
assert result["primary_category"] == "unclassified"
assert result["all_categories"] == []
assert result["severity"] == 0
def test_airstrike_is_military_not_labor_strike() -> None:
result = classify_event("Airstrike hits depot near the frontline")
assert result["primary_category"] == "military"
cats = {c["category"] for c in result["all_categories"]}
assert "social_unrest" not in cats
# "strike" inside "airstrike" must not bump severity either.
assert result["severity_modifiers"] == []
assert result["severity"] == CATEGORIES["military"]["severity_base"]
def test_labor_strike_still_fires_social_unrest() -> None:
result = classify_event("General strike and labor dispute paralyze the capital")
assert result["primary_category"] == "social_unrest"
def test_verb_stems_still_match_inflected_forms() -> None:
result = classify_event("SpaceX launched a rocket into orbit")
assert result["primary_category"] == "space"
matched = result["all_categories"][0]["keywords"]
assert {"launch", "rocket", "orbit"} <= set(matched)
def test_protest_stem_matches_protesters() -> None:
result = classify_event("A thousand protesters marched on parliament")
assert result["primary_category"] == "political"
def test_deadline_does_not_bump_severity() -> None:
# "deadline" contains "dead" (a high-severity modifier).
result = classify_event("Sanctions deadline nears for the regime")
assert result["primary_category"] == "political"
assert result["severity_modifiers"] == []
assert result["severity"] == CATEGORIES["political"]["severity_base"]
def test_deadly_still_bumps_severity() -> None:
result = classify_event("Deadly explosion reported at the port")
assert result["primary_category"] == "maritime"
assert "dead" in result["severity_modifiers"]
assert result["severity"] == CATEGORIES["maritime"]["severity_base"] + 2
def test_apt_matches_group_ids_as_words() -> None:
result = classify_event("APT29 exploited a zero-day in the wild")
assert result["primary_category"] == "cyber"
assert "apt" in result["all_categories"][0]["keywords"]