The source line (KMA, open-meteo or met.no for now; KMA and open-meteo day counts for a forecast) only went to debug_log, which is off by default and muted while the worker's brain answers, so a live answer such as "흐림" could not be traced to its service. It now goes to the llm log tab without the place or the key. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1288 lines
59 KiB
Python
1288 lines
59 KiB
Python
"""Tests for weather tool."""
|
|
|
|
import pytest
|
|
from unittest.mock import Mock, patch
|
|
import requests
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
from src.jarvis.tools.builtin.weather import (
|
|
WeatherTool,
|
|
WMO_CODES,
|
|
WMO_CODES_KO,
|
|
_extract_place_from_user_text,
|
|
_romanise_place,
|
|
)
|
|
from src.jarvis.tools.base import ToolContext
|
|
from src.jarvis.tools.types import ToolExecutionResult
|
|
|
|
|
|
class TestWeatherTool:
|
|
"""Test weather tool functionality."""
|
|
|
|
def setup_method(self):
|
|
"""Set up test fixtures."""
|
|
self.tool = WeatherTool()
|
|
self.context = Mock(spec=ToolContext)
|
|
self.context.user_print = Mock()
|
|
self.context.cfg = Mock()
|
|
# Default to empty user text + empty ollama config so the auto-detect
|
|
# fallback path short-circuits the LLM-backed place extractor. Tests
|
|
# that want to exercise the extractor override these.
|
|
self.context.redacted_text = ""
|
|
self.context.cfg.ollama_base_url = ""
|
|
self.context.cfg.ollama_chat_model = ""
|
|
self.context.cfg.tool_router_model = ""
|
|
self.context.cfg.intent_judge_model = ""
|
|
|
|
def test_tool_properties(self):
|
|
"""Test tool metadata properties."""
|
|
assert self.tool.name == "getWeather"
|
|
assert "weather" in self.tool.description.lower()
|
|
assert self.tool.inputSchema["type"] == "object"
|
|
# Location is optional - uses user's detected location as fallback
|
|
assert "location" in self.tool.inputSchema["properties"]
|
|
assert self.tool.inputSchema["required"] == []
|
|
|
|
@patch('requests.get')
|
|
def test_run_success(self, mock_get):
|
|
"""Test successful weather retrieval with current + forecast data."""
|
|
# First call: geocoding
|
|
geo_response = Mock()
|
|
geo_response.status_code = 200
|
|
geo_response.json.return_value = {
|
|
"results": [{
|
|
"latitude": 51.5074,
|
|
"longitude": -0.1278,
|
|
"name": "London",
|
|
"country": "United Kingdom",
|
|
"admin1": "England"
|
|
}]
|
|
}
|
|
geo_response.raise_for_status = Mock()
|
|
|
|
# Second call: weather (now includes hourly + daily forecast)
|
|
weather_response = Mock()
|
|
weather_response.status_code = 200
|
|
weather_response.json.return_value = {
|
|
"current": {
|
|
"time": "2026-04-08T14:00",
|
|
"temperature_2m": 15.5,
|
|
"apparent_temperature": 14.0,
|
|
"relative_humidity_2m": 65,
|
|
"weather_code": 2,
|
|
"wind_speed_10m": 12.0,
|
|
"wind_gusts_10m": 20.0
|
|
},
|
|
"hourly": {
|
|
"time": [f"2026-04-08T{h:02d}:00" for h in range(24)],
|
|
"temperature_2m": [10 + h * 0.5 for h in range(24)],
|
|
"weather_code": [2] * 24,
|
|
},
|
|
"daily": {
|
|
"time": [f"2026-04-{8+d:02d}" for d in range(7)],
|
|
"weather_code": [2, 3, 61, 0, 1, 2, 3],
|
|
"temperature_2m_max": [16, 14, 12, 17, 18, 15, 13],
|
|
"temperature_2m_min": [8, 7, 5, 9, 10, 8, 6],
|
|
},
|
|
}
|
|
weather_response.raise_for_status = Mock()
|
|
|
|
mock_get.side_effect = [geo_response, weather_response]
|
|
|
|
args = {"location": "London"}
|
|
result = self.tool.run(args, self.context)
|
|
|
|
assert isinstance(result, ToolExecutionResult)
|
|
assert result.success is True
|
|
# One ready-to-speak sentence: place, condition, rounded temperature
|
|
# and the feels-like temperature when it differs after rounding.
|
|
assert "London" in result.reply_text
|
|
assert WMO_CODES_KO[2] in result.reply_text
|
|
assert str(round(15.5)) in result.reply_text
|
|
assert str(round(14.0)) in result.reply_text
|
|
self.context.user_print.assert_called()
|
|
|
|
@patch('src.jarvis.tools.builtin.weather._romanise_place')
|
|
@patch('requests.get')
|
|
def test_run_non_latin_location_romanises_and_retries(self, mock_get, mock_romanise):
|
|
"""A Korean city name Open-Meteo can't geocode is romanised and retried.
|
|
|
|
Open-Meteo's geocoder only matches Latin spellings, so ``서울`` is first
|
|
looked up on OpenStreetMap (any language). When that finds nothing,
|
|
the tool must romanise to ``Seoul`` and retry rather than
|
|
dead-ending with "could not find location" (the failure the Korean-
|
|
locked deployment hit on every weather request)."""
|
|
mock_romanise.return_value = "Seoul"
|
|
|
|
geo_seoul = Mock()
|
|
geo_seoul.raise_for_status = Mock()
|
|
geo_seoul.json.return_value = {
|
|
"results": [{
|
|
"latitude": 37.566,
|
|
"longitude": 126.9784,
|
|
"name": "Seoul",
|
|
"country": "South Korea",
|
|
"admin1": "Seoul",
|
|
}]
|
|
}
|
|
|
|
weather_response = Mock()
|
|
weather_response.raise_for_status = Mock()
|
|
weather_response.json.return_value = {
|
|
"current": {
|
|
"time": "2026-06-12T14:00",
|
|
"temperature_2m": 24.0,
|
|
"apparent_temperature": 25.0,
|
|
"relative_humidity_2m": 55,
|
|
"weather_code": 0,
|
|
"wind_speed_10m": 6.0,
|
|
"wind_gusts_10m": 10.0,
|
|
},
|
|
"hourly": {
|
|
"time": [f"2026-06-12T{h:02d}:00" for h in range(24)],
|
|
"temperature_2m": [20 + h * 0.2 for h in range(24)],
|
|
"weather_code": [0] * 24,
|
|
},
|
|
"daily": {
|
|
"time": [f"2026-06-{12+d:02d}" for d in range(7)],
|
|
"weather_code": [0, 1, 2, 3, 1, 0, 2],
|
|
"temperature_2m_max": [25, 26, 24, 23, 27, 28, 25],
|
|
"temperature_2m_min": [16, 17, 15, 14, 18, 19, 16],
|
|
},
|
|
}
|
|
|
|
osm_empty = Mock(raise_for_status=Mock())
|
|
osm_empty.json.return_value = []
|
|
mock_get.side_effect = [osm_empty, geo_seoul, weather_response]
|
|
|
|
result = self.tool.run({"location": "서울"}, self.context)
|
|
|
|
assert result.success is True
|
|
assert "Seoul" in result.reply_text
|
|
mock_romanise.assert_called_once()
|
|
# The romanised name must be what the geocode actually queried.
|
|
assert mock_get.call_args_list[1].kwargs["params"]["name"] == "Seoul"
|
|
|
|
@patch('requests.get')
|
|
def test_run_comma_place_falls_back_to_wider_area(self, mock_get):
|
|
"""A "district, city, country" name the geocoder can't match is retried
|
|
without its leading parts, so a location taken from the context (e.g.
|
|
a detected "Gangnam-gu, Seoul, South Korea") still gets weather."""
|
|
known = {
|
|
"Seoul, South Korea": {
|
|
"latitude": 37.566, "longitude": 126.9784,
|
|
"name": "Seoul", "country": "South Korea", "admin1": "Seoul",
|
|
},
|
|
}
|
|
|
|
def fake_get(url, params=None, timeout=None):
|
|
resp = Mock()
|
|
resp.raise_for_status = Mock()
|
|
if "geocoding" in url:
|
|
place = known.get(params["name"])
|
|
resp.json.return_value = {"results": [place]} if place else {}
|
|
else:
|
|
resp.json.return_value = {
|
|
"current": {
|
|
"temperature_2m": 12.0, "apparent_temperature": 11.0,
|
|
"weather_code": 0,
|
|
},
|
|
}
|
|
return resp
|
|
|
|
mock_get.side_effect = fake_get
|
|
|
|
result = self.tool.run({"location": "Gangnam-gu, Seoul, South Korea"}, self.context)
|
|
|
|
assert result.success is True
|
|
assert "Seoul" in result.reply_text
|
|
|
|
def test_romanise_place_skips_ascii_names(self):
|
|
"""ASCII names already geocode, so no LLM round-trip is spent on them."""
|
|
cfg = Mock()
|
|
cfg.ollama_base_url = "http://x"
|
|
cfg.ollama_chat_model = "qwen2.5:3b"
|
|
cfg.tool_router_model = ""
|
|
cfg.intent_judge_model = ""
|
|
assert _romanise_place("Seoul", cfg) is None
|
|
|
|
@patch('requests.get')
|
|
def test_run_location_not_found(self, mock_get):
|
|
"""Test weather with unknown location."""
|
|
geo_response = Mock()
|
|
geo_response.status_code = 200
|
|
geo_response.json.return_value = {"results": []} # No results
|
|
geo_response.raise_for_status = Mock()
|
|
|
|
mock_get.return_value = geo_response
|
|
|
|
args = {"location": "Nonexistent Place XYZ"}
|
|
result = self.tool.run(args, self.context)
|
|
|
|
assert isinstance(result, ToolExecutionResult)
|
|
assert result.success is False
|
|
assert "could not find" in result.reply_text.lower()
|
|
|
|
@patch('src.jarvis.tools.builtin.weather.get_location_info')
|
|
def test_run_empty_location_uses_fallback(self, mock_location):
|
|
"""Test weather with empty location uses user's detected location as fallback."""
|
|
# When location detection fails, should return error
|
|
mock_location.return_value = {"error": "Location not available"}
|
|
|
|
args = {"location": ""}
|
|
result = self.tool.run(args, self.context)
|
|
|
|
assert isinstance(result, ToolExecutionResult)
|
|
assert result.success is False
|
|
assert result.reply_text and any(kw in result.reply_text.lower() for kw in ("location", "city"))
|
|
|
|
@patch('src.jarvis.tools.builtin.weather.get_location_info')
|
|
def test_run_none_location_uses_fallback(self, mock_location):
|
|
"""Test weather with location=None uses user's detected location (not geocode 'None')."""
|
|
# When location detection fails, should return error - NOT try to geocode "None"
|
|
mock_location.return_value = {"error": "Location not available"}
|
|
|
|
# LLM may pass location: null/None instead of omitting the field
|
|
args = {"location": None}
|
|
result = self.tool.run(args, self.context)
|
|
|
|
assert isinstance(result, ToolExecutionResult)
|
|
assert result.success is False
|
|
# Should use fallback, not geocode the string "None"
|
|
assert result.reply_text and any(kw in result.reply_text.lower() for kw in ("location", "city"))
|
|
# Verify location detection was called (fallback was attempted)
|
|
mock_location.assert_called_once()
|
|
|
|
@patch('requests.get')
|
|
@patch('src.jarvis.tools.builtin.weather.get_location_info')
|
|
def test_without_a_place_the_place_set_in_the_settings_is_used(self, mock_location, mock_get):
|
|
"""Live 2026-11-16: detection failed on the game PC and "날씨 어때" got "tell me the city";
|
|
the place set in the settings (brain.location, default 대한민국) is the answer instead."""
|
|
self.context.cfg.location_name = "South Korea"
|
|
geo = Mock(raise_for_status=Mock())
|
|
geo.json.return_value = {"results": [{"latitude": 36.5, "longitude": 127.75, "name": "South Korea",
|
|
"country": "South Korea"}]}
|
|
weather = Mock(raise_for_status=Mock())
|
|
weather.json.return_value = {"current": {"temperature_2m": 12.0, "apparent_temperature": 12.0,
|
|
"weather_code": 0}}
|
|
mock_get.side_effect = [geo, weather]
|
|
result = self.tool.run({}, self.context)
|
|
assert result.success, result.reply_text
|
|
assert mock_get.call_args_list[0].kwargs["params"]["name"] == "South Korea"
|
|
mock_location.assert_not_called()
|
|
# A place the person asked about still wins over the set one.
|
|
mock_get.side_effect = [geo, weather]
|
|
self.tool.run({"location": "Busan"}, self.context)
|
|
assert mock_get.call_args_list[2].kwargs["params"]["name"] == "Busan"
|
|
|
|
@patch('src.jarvis.tools.builtin.weather.get_location_info')
|
|
def test_run_no_args_uses_fallback(self, mock_location):
|
|
"""Test weather with no arguments uses user's detected location as fallback."""
|
|
# When location detection fails, should return error
|
|
mock_location.return_value = {"error": "Location not available"}
|
|
|
|
result = self.tool.run(None, self.context)
|
|
|
|
assert isinstance(result, ToolExecutionResult)
|
|
assert result.success is False
|
|
assert result.reply_text and any(kw in result.reply_text.lower() for kw in ("location", "city"))
|
|
|
|
@patch('requests.get')
|
|
@patch('src.jarvis.tools.builtin.weather.get_location_info')
|
|
def test_run_no_location_with_successful_fallback(self, mock_location, mock_get):
|
|
"""Test weather with no location but successful user location detection."""
|
|
# Mock successful location detection with coordinates (no geocoding needed)
|
|
mock_location.return_value = {
|
|
"city": "London",
|
|
"region": "England",
|
|
"country": "United Kingdom",
|
|
"latitude": 51.5074,
|
|
"longitude": -0.1278
|
|
}
|
|
|
|
# Mock weather response (no geocoding call needed - we use coordinates directly)
|
|
weather_response = Mock()
|
|
weather_response.status_code = 200
|
|
weather_response.json.return_value = {
|
|
"current": {
|
|
"temperature_2m": 15.5,
|
|
"apparent_temperature": 14.0,
|
|
"relative_humidity_2m": 65,
|
|
"weather_code": 2,
|
|
"wind_speed_10m": 12.0,
|
|
"wind_gusts_10m": 20.0
|
|
}
|
|
}
|
|
weather_response.raise_for_status = Mock()
|
|
|
|
mock_get.return_value = weather_response
|
|
|
|
# Call with no location - should use fallback coordinates directly
|
|
result = self.tool.run({}, self.context)
|
|
|
|
assert isinstance(result, ToolExecutionResult)
|
|
assert result.success is True
|
|
assert "London" in result.reply_text
|
|
# Verify location detection was called
|
|
mock_location.assert_called_once()
|
|
# Verify only one request (weather, not geocoding)
|
|
assert mock_get.call_count == 1
|
|
|
|
@patch('requests.get')
|
|
@patch('src.jarvis.tools.builtin.weather._extract_place_from_user_text')
|
|
@patch('src.jarvis.tools.builtin.weather.get_location_info')
|
|
def test_auto_detect_fail_falls_back_to_user_text(
|
|
self, mock_location, mock_extract, mock_get,
|
|
):
|
|
"""When auto-detect fails but the user's utterance names a city, the
|
|
tool must pull that city from the text and fetch weather for it — not
|
|
ask the user to repeat themselves. Regression for the "I need it for
|
|
London" → "please tell me which city" ping-pong loop.
|
|
"""
|
|
mock_location.return_value = {"error": "Location not available"}
|
|
mock_extract.return_value = "London"
|
|
|
|
geo_response = Mock()
|
|
geo_response.status_code = 200
|
|
geo_response.json.return_value = {
|
|
"results": [{
|
|
"latitude": 51.5074,
|
|
"longitude": -0.1278,
|
|
"name": "London",
|
|
"country": "United Kingdom",
|
|
"admin1": "England",
|
|
}]
|
|
}
|
|
geo_response.raise_for_status = Mock()
|
|
|
|
weather_response = Mock()
|
|
weather_response.status_code = 200
|
|
weather_response.json.return_value = {
|
|
"current": {
|
|
"time": "2026-04-20T14:00",
|
|
"temperature_2m": 12.0,
|
|
"apparent_temperature": 10.0,
|
|
"relative_humidity_2m": 70,
|
|
"weather_code": 2,
|
|
"wind_speed_10m": 8.0,
|
|
"wind_gusts_10m": 12.0,
|
|
}
|
|
}
|
|
weather_response.raise_for_status = Mock()
|
|
|
|
mock_get.side_effect = [geo_response, weather_response]
|
|
|
|
self.context.redacted_text = "I need it for London"
|
|
|
|
# No location in args, auto-detect fails, extractor recovers "London".
|
|
result = self.tool.run({}, self.context)
|
|
|
|
assert result.success is True
|
|
assert "London" in result.reply_text
|
|
mock_extract.assert_called_once()
|
|
# The extractor must have seen the user's utterance, not the args.
|
|
called_text = mock_extract.call_args[0][0]
|
|
assert "London" in called_text
|
|
|
|
@patch('src.jarvis.tools.builtin.weather._extract_place_from_user_text')
|
|
@patch('src.jarvis.tools.builtin.weather.get_location_info')
|
|
def test_auto_detect_fail_and_no_place_in_text_asks_user(
|
|
self, mock_location, mock_extract,
|
|
):
|
|
"""If auto-detect fails AND the user's utterance doesn't name a place,
|
|
the tool should still ask for one — extraction is a best-effort
|
|
fallback, not a silent guess."""
|
|
mock_location.return_value = {"error": "Location not available"}
|
|
mock_extract.return_value = None
|
|
|
|
self.context.redacted_text = "what's the weather"
|
|
|
|
result = self.tool.run({}, self.context)
|
|
|
|
assert result.success is False
|
|
assert result.reply_text and any(
|
|
kw in result.reply_text.lower() for kw in ("location", "city")
|
|
)
|
|
|
|
@patch('requests.get')
|
|
def test_when_open_meteo_does_not_answer_met_no_gives_the_weather(self, mock_get):
|
|
"""Live 2026-11-18: the open-meteo forecast timed out (geocoding had worked) and the
|
|
answer was that the weather service was unavailable."""
|
|
geo = Mock(status_code=200, raise_for_status=Mock())
|
|
geo.json.return_value = {"results": [{"latitude": 37.57, "longitude": 126.98,
|
|
"name": "서울", "country": "대한민국"}]}
|
|
met = Mock(status_code=200, raise_for_status=Mock())
|
|
met.json.return_value = {"properties": {"timeseries": [{"data": {
|
|
"instant": {"details": {"air_temperature": 8.6, "relative_humidity": 96.0,
|
|
"wind_speed": 2.2}},
|
|
"next_1_hours": {"summary": {"symbol_code": "lightrainshowers_night"}}}}]}}
|
|
|
|
def get(url, *a, **k):
|
|
if "geocoding" in url:
|
|
return geo
|
|
if "open-meteo" in url:
|
|
raise requests.exceptions.Timeout("timed out")
|
|
assert "met.no" in url
|
|
return met
|
|
mock_get.side_effect = get
|
|
|
|
result = self.tool.run({"location": "Seoul"}, self.context)
|
|
|
|
assert result.success is True
|
|
assert "서울" in result.reply_text
|
|
assert WMO_CODES_KO[80] in result.reply_text
|
|
assert str(round(8.6)) in result.reply_text
|
|
|
|
@patch('requests.get')
|
|
def test_the_log_names_met_no_when_it_gave_the_weather_now(self, mock_get, capfd):
|
|
geo = Mock(status_code=200, raise_for_status=Mock())
|
|
geo.json.return_value = {"results": [{"latitude": 37.57, "longitude": 126.98,
|
|
"name": "서울", "country": "대한민국"}]}
|
|
met = Mock(status_code=200, raise_for_status=Mock())
|
|
met.json.return_value = {"properties": {"timeseries": [{"data": {
|
|
"instant": {"details": {"air_temperature": 8.6}},
|
|
"next_1_hours": {"summary": {"symbol_code": "cloudy"}}}}]}}
|
|
|
|
def get(url, *a, **k):
|
|
if "geocoding" in url:
|
|
return geo
|
|
if "open-meteo" in url:
|
|
raise requests.exceptions.Timeout("timed out")
|
|
return met
|
|
mock_get.side_effect = get
|
|
|
|
self.tool.run({"location": "Seoul"}, self.context)
|
|
|
|
err = capfd.readouterr().err
|
|
assert "weather now: met.no" in err and "서울" not in err
|
|
|
|
@pytest.mark.parametrize("symbol", ["lightsleet", "sleet_day", "heavysleetshowers_night"])
|
|
def test_met_no_sleet_stays_sleet_not_freezing_rain(self, symbol):
|
|
from src.jarvis.tools.builtin.weather import _met_no_wmo
|
|
code = _met_no_wmo(symbol)
|
|
assert "sleet" in WMO_CODES[code].lower() or "snow" in WMO_CODES[code].lower()
|
|
assert "freezing" not in WMO_CODES[code].lower()
|
|
assert "진눈깨비" in WMO_CODES_KO[code]
|
|
|
|
@patch('requests.get')
|
|
def test_when_both_weather_services_fail_the_answer_says_so(self, mock_get):
|
|
geo = Mock(status_code=200, raise_for_status=Mock())
|
|
geo.json.return_value = {"results": [{"latitude": 37.57, "longitude": 126.98,
|
|
"name": "서울", "country": "대한민국"}]}
|
|
|
|
def get(url, *a, **k):
|
|
if "geocoding" in url:
|
|
return geo
|
|
raise requests.exceptions.Timeout("timed out")
|
|
mock_get.side_effect = get
|
|
|
|
result = self.tool.run({"location": "Seoul"}, self.context)
|
|
|
|
assert result.success is False
|
|
assert "unavailable" in result.reply_text.lower()
|
|
|
|
@patch('requests.get')
|
|
def test_run_network_timeout(self, mock_get):
|
|
"""Test weather with network timeout."""
|
|
mock_get.side_effect = requests.exceptions.Timeout("Connection timed out")
|
|
|
|
args = {"location": "London"}
|
|
result = self.tool.run(args, self.context)
|
|
|
|
assert isinstance(result, ToolExecutionResult)
|
|
assert result.success is False
|
|
assert "timeout" in result.reply_text.lower() or "taking too long" in result.reply_text.lower()
|
|
|
|
@patch('requests.get')
|
|
def test_run_network_error(self, mock_get):
|
|
"""Test weather with network error."""
|
|
mock_get.side_effect = requests.exceptions.ConnectionError("Network error")
|
|
|
|
args = {"location": "London"}
|
|
result = self.tool.run(args, self.context)
|
|
|
|
assert isinstance(result, ToolExecutionResult)
|
|
assert result.success is False
|
|
assert "unavailable" in result.reply_text.lower()
|
|
|
|
def test_wmo_codes_coverage(self):
|
|
"""Test that WMO codes dictionary has expected entries."""
|
|
# Check some key weather codes
|
|
assert WMO_CODES[0] == "Clear sky"
|
|
assert WMO_CODES[3] == "Overcast"
|
|
assert WMO_CODES[61] == "Slight rain"
|
|
assert WMO_CODES[95] == "Thunderstorm"
|
|
# Ensure there are many codes covered
|
|
assert len(WMO_CODES) >= 20
|
|
|
|
@patch('requests.get')
|
|
def test_reply_is_current_conditions_only(self, mock_get):
|
|
"""The reply is one sentence about now: the forecast the API returns is
|
|
left out so a small model has nothing to ramble over."""
|
|
geo_response = Mock()
|
|
geo_response.status_code = 200
|
|
geo_response.json.return_value = {
|
|
"results": [{
|
|
"latitude": 41.6938,
|
|
"longitude": 44.8015,
|
|
"name": "Tbilisi",
|
|
"country": "Georgia",
|
|
"admin1": "Tbilisi"
|
|
}]
|
|
}
|
|
geo_response.raise_for_status = Mock()
|
|
|
|
weather_response = Mock()
|
|
weather_response.status_code = 200
|
|
weather_response.json.return_value = {
|
|
"current": {
|
|
"time": "2026-04-08T10:00",
|
|
"temperature_2m": 12.0,
|
|
"apparent_temperature": 10.0,
|
|
"relative_humidity_2m": 70,
|
|
"weather_code": 61,
|
|
"wind_speed_10m": 8.0,
|
|
"wind_gusts_10m": 15.0
|
|
},
|
|
"hourly": {
|
|
"time": [f"2026-04-08T{h:02d}:00" for h in range(24)],
|
|
"temperature_2m": [8, 8, 7, 7, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16, 15, 14, 13, 12, 11, 10, 9, 9, 8],
|
|
"weather_code": [61] * 12 + [2] * 12,
|
|
},
|
|
"daily": {
|
|
"time": [f"2026-04-{8+d:02d}" for d in range(7)],
|
|
"weather_code": [61, 3, 0, 1, 2, 61, 0],
|
|
"temperature_2m_max": [16, 18, 20, 19, 17, 14, 21],
|
|
"temperature_2m_min": [7, 8, 10, 9, 8, 6, 11],
|
|
},
|
|
}
|
|
weather_response.raise_for_status = Mock()
|
|
|
|
mock_get.side_effect = [geo_response, weather_response]
|
|
|
|
result = self.tool.run({"location": "Tbilisi"}, self.context)
|
|
|
|
assert result.success is True
|
|
assert str(round(12.0)) in result.reply_text
|
|
assert WMO_CODES_KO[61] in result.reply_text
|
|
assert "\n" not in result.reply_text.strip()
|
|
assert "15:00" not in result.reply_text
|
|
assert "2026-04-09" not in result.reply_text
|
|
|
|
@patch('requests.get')
|
|
def test_temperature_has_no_unit_for_the_model_to_echo(self, mock_get):
|
|
"""Only the rounded Celsius number is given, with no °C/°F or
|
|
Fahrenheit value a model could read out as "25도 Celsius"."""
|
|
geo_response = Mock()
|
|
geo_response.status_code = 200
|
|
geo_response.json.return_value = {
|
|
"results": [{
|
|
"latitude": 40.7128,
|
|
"longitude": -74.0060,
|
|
"name": "New York",
|
|
"country": "United States",
|
|
"admin1": "New York"
|
|
}]
|
|
}
|
|
geo_response.raise_for_status = Mock()
|
|
|
|
weather_response = Mock()
|
|
weather_response.status_code = 200
|
|
weather_response.json.return_value = {
|
|
"current": {
|
|
"temperature_2m": 20.0, # 68°F
|
|
"apparent_temperature": 18.0,
|
|
"relative_humidity_2m": 50,
|
|
"weather_code": 0,
|
|
"wind_speed_10m": 5.0,
|
|
"wind_gusts_10m": None
|
|
}
|
|
}
|
|
weather_response.raise_for_status = Mock()
|
|
|
|
mock_get.side_effect = [geo_response, weather_response]
|
|
|
|
args = {"location": "New York"}
|
|
result = self.tool.run(args, self.context)
|
|
|
|
assert result.success is True
|
|
assert str(round(20.0)) in result.reply_text # Celsius
|
|
assert str(round(20.0 * 9 / 5 + 32)) not in result.reply_text # Fahrenheit
|
|
assert "°" not in result.reply_text
|
|
|
|
|
|
class TestExtractPlaceFromUserText:
|
|
"""Unit tests for the small-model fallback place extractor."""
|
|
|
|
def _cfg(self):
|
|
cfg = Mock()
|
|
cfg.ollama_base_url = "http://localhost:11434"
|
|
cfg.ollama_chat_model = "gemma4:e2b"
|
|
cfg.tool_router_model = ""
|
|
cfg.intent_judge_model = ""
|
|
cfg.llm_tools_timeout_sec = 8.0
|
|
return cfg
|
|
|
|
def test_empty_text_returns_none(self):
|
|
assert _extract_place_from_user_text("", self._cfg()) is None
|
|
assert _extract_place_from_user_text(" ", self._cfg()) is None
|
|
|
|
def test_none_cfg_returns_none(self):
|
|
assert _extract_place_from_user_text("weather in London", None) is None
|
|
|
|
def test_unconfigured_model_returns_none(self):
|
|
cfg = Mock()
|
|
cfg.ollama_base_url = ""
|
|
cfg.ollama_chat_model = ""
|
|
cfg.tool_router_model = ""
|
|
cfg.intent_judge_model = ""
|
|
assert _extract_place_from_user_text("weather in London", cfg) is None
|
|
|
|
@patch("src.jarvis.tools.builtin.weather.call_llm_direct", create=True)
|
|
def test_extracts_clean_place_name(self, _mock_direct):
|
|
"""Patch the import inside the function by intercepting call_llm_direct."""
|
|
from src.jarvis.llm import call_llm_direct as real_fn # noqa: F401
|
|
|
|
with patch("src.jarvis.llm.call_llm_direct", return_value="London"):
|
|
got = _extract_place_from_user_text("I need it for London", self._cfg())
|
|
assert got == "London"
|
|
|
|
def test_strips_quotes_and_punctuation(self):
|
|
with patch("src.jarvis.llm.call_llm_direct", return_value="'Paris'."):
|
|
got = _extract_place_from_user_text("weather paris?", self._cfg())
|
|
assert got == "Paris"
|
|
|
|
def test_none_sentinel_returns_none(self):
|
|
for sentinel in ("none", "None", "NONE", "n/a", "unknown"):
|
|
with patch("src.jarvis.llm.call_llm_direct", return_value=sentinel):
|
|
assert _extract_place_from_user_text(
|
|
"what's the weather", self._cfg()
|
|
) is None
|
|
|
|
def test_sentence_response_rejected(self):
|
|
"""If the model explains instead of answering, treat it as no-place."""
|
|
with patch(
|
|
"src.jarvis.llm.call_llm_direct",
|
|
return_value="The user did not name a place.",
|
|
):
|
|
got = _extract_place_from_user_text("weather today", self._cfg())
|
|
assert got is None
|
|
|
|
def test_overlong_response_rejected(self):
|
|
with patch("src.jarvis.llm.call_llm_direct", return_value="x" * 200):
|
|
got = _extract_place_from_user_text("weather", self._cfg())
|
|
assert got is None
|
|
|
|
|
|
class TestKmaWeather:
|
|
"""Inside Korea, with the app's KMA key, the current weather comes from KMA (기상청 API허브)."""
|
|
|
|
KEY = "kma-test-key-123"
|
|
|
|
def setup_method(self):
|
|
self.tool = WeatherTool()
|
|
self.context = Mock(spec=ToolContext)
|
|
self.context.user_print = Mock()
|
|
self.context.cfg = Mock()
|
|
self.context.cfg.location_name = None
|
|
self.context.redacted_text = ""
|
|
|
|
@staticmethod
|
|
def _geo(country_code):
|
|
geo = Mock(status_code=200, raise_for_status=Mock())
|
|
geo.json.return_value = {"results": [{"latitude": 37.566, "longitude": 126.9784,
|
|
"name": "Seoul", "country_code": country_code}]}
|
|
return geo
|
|
|
|
@staticmethod
|
|
def _kma(items, code="00"):
|
|
r = Mock(status_code=200, raise_for_status=Mock())
|
|
r.json.return_value = {"response": {"header": {"resultCode": code},
|
|
"body": {"items": {"item": items}}}}
|
|
return r
|
|
|
|
def _obs(self, pty="0", t1h="4.9", rn1="0"):
|
|
return self._kma([{"category": c, "obsrValue": v} for c, v in
|
|
[("PTY", pty), ("REH", "72"), ("RN1", rn1), ("T1H", t1h), ("WSD", "2.5")]])
|
|
|
|
def _sky(self, first="3"):
|
|
# The coming hours, out of order: the earliest one is the sky now.
|
|
return self._kma([
|
|
{"category": "SKY", "fcstDate": "20261118", "fcstTime": "1100", "fcstValue": "1"},
|
|
{"category": "SKY", "fcstDate": "20261118", "fcstTime": "1000", "fcstValue": first},
|
|
{"category": "T1H", "fcstDate": "20261118", "fcstTime": "1000", "fcstValue": "6"},
|
|
])
|
|
|
|
def _routes(self, country_code="KR", obs=None, sky=None, calls=None):
|
|
weather = Mock(status_code=200, raise_for_status=Mock())
|
|
weather.json.return_value = {"current": {"temperature_2m": 11.0, "weather_code": 0}}
|
|
|
|
def get(url, *a, **k):
|
|
if calls is not None:
|
|
calls.append((url, k.get("params") or {}))
|
|
if "geocoding" in url:
|
|
return self._geo(country_code)
|
|
if "getUltraSrtNcst" in url:
|
|
return obs if obs is not None else self._obs()
|
|
if "getUltraSrtFcst" in url:
|
|
return sky if sky is not None else self._sky()
|
|
assert "open-meteo" in url, url
|
|
return weather
|
|
return get
|
|
|
|
@patch('requests.get')
|
|
def test_with_a_key_korean_weather_comes_from_kma(self, mock_get, monkeypatch):
|
|
monkeypatch.setenv("JARVIS_KMA_API_KEY", self.KEY)
|
|
calls = []
|
|
mock_get.side_effect = self._routes(calls=calls)
|
|
|
|
result = self.tool.run({"location": "Seoul"}, self.context)
|
|
|
|
assert result.success is True
|
|
assert "구름 많음" in result.reply_text and "기온 5도" in result.reply_text
|
|
assert not any("api.open-meteo.com/v1/forecast" in u for u, _ in calls)
|
|
kma = [p for u, p in calls if "apihub.kma.go.kr" in u]
|
|
assert len(kma) == 2 and all(p["authKey"] == self.KEY for p in kma)
|
|
assert all((p["nx"], p["ny"]) == (60, 127) for p in kma)
|
|
|
|
@pytest.mark.parametrize("key, source", [(True, "KMA"), (False, "open-meteo")])
|
|
@patch('requests.get')
|
|
def test_the_log_names_the_service_that_gave_the_weather_now_and_not_the_place(
|
|
self, mock_get, key, source, monkeypatch, capfd):
|
|
"""Live 2026-11-18: "흐림" could have come from either service, and nothing said which."""
|
|
if key:
|
|
monkeypatch.setenv("JARVIS_KMA_API_KEY", self.KEY)
|
|
else:
|
|
monkeypatch.delenv("JARVIS_KMA_API_KEY", raising=False)
|
|
mock_get.side_effect = self._routes()
|
|
|
|
self.tool.run({"location": "Seoul"}, self.context)
|
|
|
|
err = capfd.readouterr().err
|
|
assert f"weather now: {source}" in err
|
|
assert "Seoul" not in err and self.KEY not in err
|
|
|
|
@patch('requests.get')
|
|
def test_precipitation_wins_over_the_sky(self, mock_get, monkeypatch):
|
|
monkeypatch.setenv("JARVIS_KMA_API_KEY", self.KEY)
|
|
mock_get.side_effect = self._routes(obs=self._obs(pty="3", t1h="-1.4"), sky=self._sky(first="1"))
|
|
|
|
result = self.tool.run({"location": "Seoul"}, self.context)
|
|
|
|
assert "눈" in result.reply_text and "맑음" not in result.reply_text
|
|
assert "기온 -1도" in result.reply_text
|
|
|
|
@patch('requests.get')
|
|
def test_without_a_key_open_meteo_answers_also_in_korea(self, mock_get, monkeypatch):
|
|
monkeypatch.delenv("JARVIS_KMA_API_KEY", raising=False)
|
|
calls = []
|
|
mock_get.side_effect = self._routes(calls=calls)
|
|
|
|
result = self.tool.run({"location": "Seoul"}, self.context)
|
|
|
|
assert "기온 11도" in result.reply_text
|
|
assert not any("apihub.kma.go.kr" in u for u, _ in calls)
|
|
|
|
@patch('requests.get')
|
|
def test_outside_korea_kma_is_not_asked(self, mock_get, monkeypatch):
|
|
monkeypatch.setenv("JARVIS_KMA_API_KEY", self.KEY)
|
|
calls = []
|
|
mock_get.side_effect = self._routes(country_code="JP", calls=calls)
|
|
|
|
result = self.tool.run({"location": "Fukuoka"}, self.context)
|
|
|
|
assert "기온 11도" in result.reply_text
|
|
assert not any("apihub.kma.go.kr" in u for u, _ in calls)
|
|
|
|
@pytest.mark.parametrize("failure", ["result_code", "timeout", "no_sky"])
|
|
@patch('requests.get')
|
|
def test_when_kma_fails_open_meteo_answers_and_the_key_is_not_logged(
|
|
self, mock_get, failure, monkeypatch):
|
|
monkeypatch.setenv("JARVIS_KMA_API_KEY", self.KEY)
|
|
if failure == "result_code": # e.g. the API's use not applied for
|
|
routes = self._routes(obs=self._kma([], code="30"))
|
|
elif failure == "timeout":
|
|
base = self._routes()
|
|
|
|
def routes(url, *a, **k):
|
|
if "apihub.kma.go.kr" in url:
|
|
raise requests.exceptions.Timeout(f"timed out: {url}?authKey={self.KEY}")
|
|
return base(url, *a, **k)
|
|
else:
|
|
routes = self._routes(sky=self._kma([]))
|
|
mock_get.side_effect = routes
|
|
|
|
with patch("src.jarvis.tools.builtin.weather.debug_log") as log:
|
|
result = self.tool.run({"location": "Seoul"}, self.context)
|
|
|
|
assert result.success is True and "기온 11도" in result.reply_text
|
|
assert not any(self.KEY in str(c) for c in log.call_args_list)
|
|
|
|
@patch('requests.get')
|
|
def test_asked_in_detail_kma_gives_humidity_wind_and_the_last_hours_rain(self, mock_get, monkeypatch):
|
|
monkeypatch.setenv("JARVIS_KMA_API_KEY", self.KEY)
|
|
mock_get.side_effect = self._routes(obs=self._obs(pty="1", rn1="1.5"))
|
|
|
|
result = self.tool.run({"location": "Seoul", "detail": True}, self.context)
|
|
|
|
text = result.reply_text
|
|
assert "습도 72%" in text and "바람 초속 2.5미터" in text and "지난 1시간 강수량 1.5mm" in text
|
|
|
|
@patch('requests.get')
|
|
def test_not_asked_in_detail_the_answer_stays_short(self, mock_get, monkeypatch):
|
|
monkeypatch.setenv("JARVIS_KMA_API_KEY", self.KEY)
|
|
mock_get.side_effect = self._routes(obs=self._obs(pty="1", rn1="1.5"))
|
|
|
|
result = self.tool.run({"location": "Seoul"}, self.context)
|
|
|
|
assert "습도" not in result.reply_text and "바람" not in result.reply_text
|
|
|
|
@patch('requests.get')
|
|
def test_asked_in_detail_open_meteo_gives_humidity_and_wind_without_a_dry_hours_rain(
|
|
self, mock_get, monkeypatch):
|
|
monkeypatch.delenv("JARVIS_KMA_API_KEY", raising=False)
|
|
calls = []
|
|
base = self._routes(calls=calls)
|
|
weather = Mock(status_code=200, raise_for_status=Mock())
|
|
weather.json.return_value = {"current": {"temperature_2m": 11.0, "weather_code": 0,
|
|
"relative_humidity_2m": 55, "wind_speed_10m": 10.8,
|
|
"precipitation": 0.0}}
|
|
|
|
def get(url, *a, **k):
|
|
r = base(url, *a, **k)
|
|
return weather if "api.open-meteo.com/v1/forecast" in url else r
|
|
mock_get.side_effect = get
|
|
|
|
result = self.tool.run({"location": "Seoul", "detail": True}, self.context)
|
|
|
|
assert "습도 55%" in result.reply_text and "바람 초속 3미터" in result.reply_text
|
|
assert "강수량" not in result.reply_text
|
|
(p,) = [p for u, p in calls if "api.open-meteo.com/v1/forecast" in u]
|
|
assert "precipitation" in p["current"].split(",")
|
|
|
|
@pytest.mark.parametrize("place,lat,lon,grid", [
|
|
("서울", 37.5665, 126.9780, (60, 127)),
|
|
("부산", 35.1796, 129.0756, (98, 76)),
|
|
("제주", 33.4996, 126.5312, (53, 38)),
|
|
])
|
|
def test_kma_grid_point(self, place, lat, lon, grid):
|
|
from src.jarvis.tools.builtin.weather import _kma_grid
|
|
assert _kma_grid(lat, lon) == grid, place
|
|
|
|
@patch('requests.get')
|
|
def test_kma_base_times_are_the_latest_published(self, mock_get):
|
|
"""초단기실황 for hour H is out at about H:40, 초단기예보 for H:30 at about H:45 (KST)."""
|
|
from datetime import datetime, timezone, timedelta
|
|
from src.jarvis.tools.builtin.weather import _kma_current
|
|
calls = []
|
|
mock_get.side_effect = self._routes(calls=calls)
|
|
|
|
# 00:20 KST on 1 Jan: both still from the day before.
|
|
_kma_current(37.566, 126.978, self.KEY,
|
|
now=datetime(2026, 12, 31, 15, 20, tzinfo=timezone.utc))
|
|
(_, obs), (_, fc) = calls
|
|
assert (obs["base_date"], obs["base_time"]) == ("20261231", "2300")
|
|
assert (fc["base_date"], fc["base_time"]) == ("20261231", "2330")
|
|
|
|
calls.clear()
|
|
_kma_current(37.566, 126.978, self.KEY,
|
|
now=datetime(2026, 11, 18, 9, 50, tzinfo=timezone(timedelta(hours=9))))
|
|
(_, obs), (_, fc) = calls
|
|
assert obs["base_time"] == "0900" and fc["base_time"] == "0930"
|
|
|
|
@patch('requests.get')
|
|
def test_the_worker_takes_the_key_out_of_the_environment_and_still_uses_it(self, mock_get, monkeypatch):
|
|
"""No child process (MCP servers, MeloTTS) inherits the key; the weather tool keeps it."""
|
|
import os
|
|
import src.jarvis.tools.builtin.weather as weather
|
|
monkeypatch.setenv("JARVIS_KMA_API_KEY", self.KEY)
|
|
monkeypatch.setattr(weather, "_kma_key", "")
|
|
weather.take_kma_key()
|
|
assert "JARVIS_KMA_API_KEY" not in os.environ
|
|
calls = []
|
|
mock_get.side_effect = self._routes(calls=calls)
|
|
|
|
result = self.tool.run({"location": "Seoul"}, self.context)
|
|
|
|
assert "흐림" in result.reply_text or "구름 많음" in result.reply_text
|
|
assert any(p.get("authKey") == self.KEY for u, p in calls if "apihub.kma.go.kr" in u)
|
|
|
|
|
|
class TestForecast:
|
|
"""A later day (days_ahead/days): KMA 단기예보 and 중기예보 inside Korea, else open-meteo's daily forecast."""
|
|
|
|
KEY = "kma-test-key-123"
|
|
# Wednesday 18 November 2026, 10:20 KST.
|
|
NOW = datetime(2026, 11, 18, 10, 20, tzinfo=timezone(timedelta(hours=9)))
|
|
|
|
def setup_method(self):
|
|
self.tool = WeatherTool()
|
|
self.context = Mock(spec=ToolContext)
|
|
self.context.user_print = Mock()
|
|
self.context.cfg = Mock()
|
|
self.context.cfg.location_name = None
|
|
self.context.redacted_text = ""
|
|
|
|
@staticmethod
|
|
def _ok(payload):
|
|
r = Mock(status_code=200, raise_for_status=Mock())
|
|
r.json.return_value = payload
|
|
return r
|
|
|
|
def _kma(self, items):
|
|
return self._ok({"response": {"header": {"resultCode": "00"}, "body": {"items": {"item": items}}}})
|
|
|
|
@staticmethod
|
|
def _vilage_items():
|
|
"""단기예보: tomorrow (19th) clear in the morning, rain in the afternoon; Saturday (21st) cloudy."""
|
|
items = []
|
|
for date, sky_am, sky_pm, pty_pm, pops in [
|
|
("20261119", "1", "4", "1", ("10", "60")),
|
|
("20261120", "3", "3", "0", ("20", "20")),
|
|
("20261121", "4", "4", "0", ("30", "30")),
|
|
]:
|
|
for hour in range(0, 24):
|
|
t = f"{hour:02d}00"
|
|
am = hour < 12
|
|
items += [
|
|
{"category": "SKY", "fcstDate": date, "fcstTime": t, "fcstValue": sky_am if am else sky_pm},
|
|
{"category": "PTY", "fcstDate": date, "fcstTime": t, "fcstValue": "0" if am else pty_pm},
|
|
{"category": "POP", "fcstDate": date, "fcstTime": t, "fcstValue": pops[0] if am else pops[1]},
|
|
{"category": "TMP", "fcstDate": date, "fcstTime": t, "fcstValue": str(hour % 7)},
|
|
{"category": "REH", "fcstDate": date, "fcstTime": t, "fcstValue": "60" if am else "80"},
|
|
{"category": "WSD", "fcstDate": date, "fcstTime": t, "fcstValue": str(1 + hour % 4)},
|
|
]
|
|
items += [{"category": "TMN", "fcstDate": date, "fcstTime": "0600", "fcstValue": "-1.0"},
|
|
{"category": "TMX", "fcstDate": date, "fcstTime": "1500", "fcstValue": "4.0"}]
|
|
return items
|
|
|
|
def _routes(self, country_code="KR", lat=37.566, lon=126.9784, calls=None, fail_kma=False):
|
|
def get(url, *a, **k):
|
|
params = k.get("params") or {}
|
|
if calls is not None:
|
|
calls.append((url, params))
|
|
if "geocoding" in url:
|
|
return self._ok({"results": [{"latitude": lat, "longitude": lon, "name": "Seoul",
|
|
"country_code": country_code}]})
|
|
if "apihub.kma.go.kr" in url:
|
|
if fail_kma:
|
|
raise requests.exceptions.Timeout(f"timed out: {url}?authKey={self.KEY}")
|
|
if "getVilageFcst" in url:
|
|
return self._kma(self._vilage_items())
|
|
if "getMidTa" in url:
|
|
return self._kma([{"regId": params["regId"], "taMin4": -4, "taMax4": 5,
|
|
"taMin5": -2, "taMax5": 7, "taMin10": 2, "taMax10": 10}])
|
|
if "getMidLandFcst" in url:
|
|
return self._kma([{"regId": params["regId"], "wf4Am": "맑음", "wf4Pm": "구름많음",
|
|
"rnSt4Am": 10, "rnSt4Pm": 20, "wf5Am": "흐림", "wf5Pm": "흐리고 비",
|
|
"rnSt5Am": 30, "rnSt5Pm": 60, "wf10": "맑음", "rnSt10": 10}])
|
|
raise AssertionError(url)
|
|
assert "api.open-meteo.com/v1/forecast" in url, url
|
|
return self._ok({"current": {"temperature_2m": 11.0, "weather_code": 0}, "daily": {
|
|
"time": [f"2026-11-{d}" for d in range(18, 29)],
|
|
"weather_code": [0, 61, 3, 2, 0, 0, 0, 0, 0, 0, 0],
|
|
"temperature_2m_min": [1.2, 2.6, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
"temperature_2m_max": [9.0, 11.4, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
"precipitation_probability_max": [0, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
"wind_speed_10m_max": [9.0, 18.0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
"precipitation_sum": [0.0, 12.5, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
}})
|
|
return get
|
|
|
|
def _run(self, args, monkeypatch, key=True, now=None, **routes):
|
|
if key:
|
|
monkeypatch.setenv("JARVIS_KMA_API_KEY", self.KEY)
|
|
else:
|
|
monkeypatch.delenv("JARVIS_KMA_API_KEY", raising=False)
|
|
calls = []
|
|
with patch('requests.get', side_effect=self._routes(calls=calls, **routes)), \
|
|
patch('src.jarvis.tools.builtin.weather._now', return_value=now or self.NOW):
|
|
result = self.tool.run({"location": "Seoul", **args}, self.context)
|
|
return result, calls
|
|
|
|
def test_the_schema_offers_a_later_day_and_a_span_of_days(self):
|
|
props = self.tool.inputSchema["properties"]
|
|
assert props["days_ahead"]["type"] == "integer" and props["days"]["type"] == "integer"
|
|
assert self.tool.inputSchema["required"] == []
|
|
|
|
def test_tomorrow_in_korea_comes_from_the_kma_short_term_forecast(self, monkeypatch):
|
|
result, calls = self._run({"days_ahead": 1}, monkeypatch)
|
|
|
|
assert result.success is True
|
|
text = result.reply_text
|
|
assert text.startswith("내일(11월 19일 목요일) Seoul 날씨는")
|
|
assert "오전 맑음" in text and "오후 비" in text
|
|
assert "최저 -1도" in text and "최고 4도" in text and "강수확률 60%" in text
|
|
assert not any("open-meteo.com/v1/forecast" in u for u, _ in calls)
|
|
vilage = [p for u, p in calls if "getVilageFcst" in u]
|
|
assert len(vilage) == 1 and vilage[0]["authKey"] == self.KEY
|
|
assert (vilage[0]["nx"], vilage[0]["ny"]) == (60, 127)
|
|
assert (vilage[0]["base_date"], vilage[0]["base_time"]) == ("20261118", "0800")
|
|
assert not any("MidFcstInfoService" in u for u, _ in calls)
|
|
|
|
def test_five_days_ahead_in_korea_comes_from_the_kma_mid_term_forecast(self, monkeypatch):
|
|
result, calls = self._run({"days_ahead": 5}, monkeypatch)
|
|
|
|
text = result.reply_text
|
|
assert text.startswith("11월 23일 월요일 Seoul 날씨는")
|
|
assert "오전 흐림" in text and "오후 흐리고 비" in text
|
|
assert "최저 -2도" in text and "최고 7도" in text and "강수확률 60%" in text
|
|
mid = {u.rsplit("/", 1)[-1]: p for u, p in calls if "MidFcstInfoService" in u}
|
|
assert mid["getMidTa"]["regId"] == "11B10101" and mid["getMidLandFcst"]["regId"] == "11B00000"
|
|
assert mid["getMidTa"]["tmFc"] == "202611180600" and mid["getMidTa"]["authKey"] == self.KEY
|
|
assert not any("getVilageFcst" in u for u, _ in calls)
|
|
|
|
def test_the_weekend_spans_the_short_and_mid_term_forecasts(self, monkeypatch):
|
|
result, _ = self._run({"days_ahead": 3, "days": 2}, monkeypatch)
|
|
|
|
lines = result.reply_text.split("\n")
|
|
assert len(lines) == 2
|
|
assert lines[0].startswith("11월 21일 토요일") and "흐림" in lines[0] and "강수확률 30%" in lines[0]
|
|
assert lines[1].startswith("11월 22일 일요일") and "오전 맑음" in lines[1] and "오후 구름많음" in lines[1]
|
|
assert "최저 -4도" in lines[1] and "최고 5도" in lines[1]
|
|
|
|
def test_the_same_weather_all_day_is_said_once(self, monkeypatch):
|
|
result, _ = self._run({"days_ahead": 2}, monkeypatch)
|
|
|
|
assert result.reply_text.startswith("모레(11월 20일 금요일) Seoul 날씨는 구름 많음,")
|
|
assert "오전" not in result.reply_text
|
|
|
|
@pytest.mark.parametrize("lat,lon,city,region", [
|
|
(35.1796, 129.0756, "11H20201", "11H20000"), # 부산
|
|
(33.4996, 126.5312, "11G00201", "11G00000"), # 제주
|
|
(35.9676, 126.7369, "21F10501", "11F10000"), # 군산
|
|
(37.7519, 128.8761, "11D20501", "11D20000"), # 강릉
|
|
])
|
|
def test_the_mid_term_forecast_is_for_the_nearest_kma_city_and_its_region(
|
|
self, lat, lon, city, region, monkeypatch):
|
|
_, calls = self._run({"days_ahead": 6}, monkeypatch, lat=lat, lon=lon)
|
|
|
|
mid = {u.rsplit("/", 1)[-1]: p for u, p in calls if "MidFcstInfoService" in u}
|
|
assert mid["getMidTa"]["regId"] == city and mid["getMidLandFcst"]["regId"] == region
|
|
|
|
@pytest.mark.parametrize("now,vilage,tmfc", [
|
|
(datetime(2026, 11, 18, 1, 50), ("20261117", "2300"), "202611171800"),
|
|
(datetime(2026, 11, 18, 2, 20), ("20261118", "0200"), "202611171800"),
|
|
(datetime(2026, 11, 18, 6, 40), ("20261118", "0500"), "202611180600"),
|
|
(datetime(2026, 11, 18, 23, 59), ("20261118", "2300"), "202611181800"),
|
|
])
|
|
def test_kma_forecasts_are_the_latest_published(self, now, vilage, tmfc, monkeypatch):
|
|
"""단기예보 is out about 10 minutes after 02, 05, ..., 23 h; 중기예보 about 30 minutes after 06 and 18 h."""
|
|
now = now.replace(tzinfo=timezone(timedelta(hours=9)))
|
|
_, calls = self._run({"days_ahead": 1}, monkeypatch, now=now)
|
|
_, mid_calls = self._run({"days_ahead": 7}, monkeypatch, now=now)
|
|
|
|
(p,) = [p for u, p in calls if "getVilageFcst" in u]
|
|
assert (p["base_date"], p["base_time"]) == vilage
|
|
assert {p["tmFc"] for u, p in mid_calls if "MidFcstInfoService" in u} == {tmfc}
|
|
|
|
def test_an_evening_mid_term_forecast_from_yesterday_counts_its_days_from_yesterday(self, monkeypatch):
|
|
# 05:00 on the 19th: the latest 중기예보 is the 18th's 18:00, whose day 5 is the 23rd = 4 days ahead.
|
|
now = datetime(2026, 11, 19, 5, 0, tzinfo=timezone(timedelta(hours=9)))
|
|
result, _ = self._run({"days_ahead": 4}, monkeypatch, now=now)
|
|
|
|
assert result.reply_text.startswith("11월 23일 월요일") and "최고 7도" in result.reply_text
|
|
|
|
def test_when_kma_fails_open_meteo_gives_the_forecast_and_the_key_is_not_logged(self, monkeypatch):
|
|
with patch("src.jarvis.tools.builtin.weather.debug_log") as log:
|
|
result, calls = self._run({"days_ahead": 1}, monkeypatch, fail_kma=True)
|
|
|
|
assert result.success is True
|
|
assert result.reply_text.startswith("내일(11월 19일 목요일) Seoul 날씨는 약한 비,")
|
|
assert "최저 3도" in result.reply_text and "최고 11도" in result.reply_text
|
|
assert "강수확률 80%" in result.reply_text
|
|
assert not any(self.KEY in str(c) for c in log.call_args_list)
|
|
|
|
@pytest.mark.parametrize("fail_kma, counts", [(False, "1 from KMA, 0 from open-meteo"),
|
|
(True, "0 from KMA, 1 from open-meteo")])
|
|
def test_the_log_says_how_many_forecast_days_each_service_gave(self, fail_kma, counts, monkeypatch, capfd):
|
|
self._run({"days_ahead": 1}, monkeypatch, fail_kma=fail_kma)
|
|
|
|
err = capfd.readouterr().err
|
|
assert f"weather days 1-1: {counts}" in err
|
|
assert "Seoul" not in err and self.KEY not in err
|
|
|
|
def test_outside_korea_the_forecast_is_open_meteos_daily_one(self, monkeypatch):
|
|
result, calls = self._run({"days_ahead": 1}, monkeypatch, country_code="JP")
|
|
|
|
assert "약한 비" in result.reply_text and "최고 11도" in result.reply_text
|
|
assert not any("apihub.kma.go.kr" in u for u, _ in calls)
|
|
(p,) = [p for u, p in calls if "open-meteo.com/v1/forecast" in u]
|
|
assert "precipitation_probability_max" in p["daily"] and p["forecast_days"] >= 2
|
|
|
|
def test_this_week_starts_with_the_weather_now(self, monkeypatch):
|
|
"""days_ahead 0 with more days: the current weather, then a line for each day."""
|
|
result, _ = self._run({"days": 3}, monkeypatch, key=False)
|
|
|
|
lines = result.reply_text.split("\n")
|
|
assert lines[0].startswith("지금 Seoul 날씨는")
|
|
assert lines[1].startswith("오늘(11월 18일 수요일)") and lines[2].startswith("내일(11월 19일 목요일)")
|
|
assert lines[3].startswith("모레(11월 20일 금요일)") and len(lines) == 4
|
|
|
|
def test_more_than_ten_days_ahead_is_the_tenth_day(self, monkeypatch):
|
|
result, _ = self._run({"days_ahead": 30, "days": 5}, monkeypatch)
|
|
|
|
assert result.reply_text.split("\n") == [result.reply_text]
|
|
assert result.reply_text.startswith("11월 28일 토요일") and "맑음" in result.reply_text
|
|
|
|
def test_when_no_forecast_is_available_the_answer_says_so(self, monkeypatch):
|
|
def down(url, *a, **k):
|
|
if "geocoding" in url:
|
|
return self._ok({"results": [{"latitude": 1.0, "longitude": 2.0, "name": "X",
|
|
"country_code": "FR"}]})
|
|
raise requests.exceptions.ConnectionError("down")
|
|
with patch('requests.get', side_effect=down), \
|
|
patch('src.jarvis.tools.builtin.weather._now', return_value=self.NOW):
|
|
result = self.tool.run({"location": "X", "days_ahead": 2}, self.context)
|
|
|
|
assert result.success is False and "forecast" in result.reply_text.lower()
|
|
|
|
# 단기예보's 소나기 (PTY 4) and the detail a person asks for on a later day.
|
|
|
|
def test_short_term_showers_are_said_not_dropped_for_a_clear_sky(self, monkeypatch):
|
|
items = [{"category": c, "fcstDate": "20261119", "fcstTime": f"{h:02d}00", "fcstValue": v}
|
|
for h in range(24) for c, v in
|
|
[("SKY", "1"), ("PTY", "4" if h >= 12 else "0"), ("POP", "60" if h >= 12 else "0"),
|
|
("TMP", "5")]]
|
|
items += [{"category": "TMN", "fcstDate": "20261119", "fcstTime": "0600", "fcstValue": "-1.0"},
|
|
{"category": "TMX", "fcstDate": "20261119", "fcstTime": "1500", "fcstValue": "4.0"}]
|
|
with patch.object(TestForecast, "_vilage_items", staticmethod(lambda: items)):
|
|
result, _ = self._run({"days_ahead": 1}, monkeypatch)
|
|
|
|
assert "오전 맑음, 오후 소나기" in result.reply_text, result.reply_text
|
|
|
|
def test_asked_in_detail_the_short_term_day_has_humidity_and_the_strongest_wind(self, monkeypatch):
|
|
result, _ = self._run({"days_ahead": 1, "detail": True}, monkeypatch)
|
|
|
|
assert "습도 70%" in result.reply_text and "최대 바람 초속 4미터" in result.reply_text
|
|
|
|
def test_asked_in_detail_open_meteos_day_has_the_strongest_wind_and_the_rain(self, monkeypatch):
|
|
result, calls = self._run({"days_ahead": 1, "detail": True}, monkeypatch, country_code="JP")
|
|
|
|
(p,) = [p for u, p in calls if "open-meteo.com/v1/forecast" in u]
|
|
assert {"wind_speed_10m_max", "precipitation_sum"} <= set(p["daily"].split(","))
|
|
assert "최대 바람 초속 5미터" in result.reply_text and "강수량 12.5mm" in result.reply_text
|
|
|
|
def test_the_schema_offers_detail(self):
|
|
assert self.tool.inputSchema["properties"]["detail"]["type"] == "boolean"
|
|
|
|
|
|
class TestDistrictPlaces:
|
|
"""A district such as 부산 연제구 is found by OpenStreetMap's search (any language) when
|
|
open-meteo's geocoder, which knows cities only, does not find it; the weather is for that spot."""
|
|
|
|
KEY = "kma-test-key-123"
|
|
|
|
def setup_method(self):
|
|
self.tool = WeatherTool()
|
|
self.context = Mock(spec=ToolContext)
|
|
self.context.user_print = Mock()
|
|
self.context.cfg = Mock()
|
|
self.context.cfg.location_name = None
|
|
self.context.redacted_text = ""
|
|
|
|
@staticmethod
|
|
def _ok(payload):
|
|
r = Mock(status_code=200, raise_for_status=Mock())
|
|
r.json.return_value = payload
|
|
return r
|
|
|
|
def _routes(self, calls, osm=None, osm_fails=False):
|
|
def get(url, *a, **k):
|
|
params = k.get("params") or {}
|
|
calls.append((url, params, k.get("headers") or {}))
|
|
if "geocoding-api.open-meteo.com" in url:
|
|
return self._ok({})
|
|
if "nominatim.openstreetmap.org" in url:
|
|
if osm_fails:
|
|
raise requests.exceptions.Timeout("timed out")
|
|
return self._ok(osm if osm is not None else [{
|
|
"lat": "35.1762", "lon": "129.0797", "name": "연제구", "addresstype": "borough",
|
|
"address": {"borough": "연제구", "city": "부산광역시", "country_code": "kr"}}])
|
|
if "getUltraSrtNcst" in url:
|
|
return self._ok({"response": {"header": {"resultCode": "00"}, "body": {"items": {"item": [
|
|
{"category": c, "obsrValue": v} for c, v in
|
|
[("PTY", "0"), ("REH", "60"), ("T1H", "8.2"), ("WSD", "3.1")]]}}}})
|
|
if "getUltraSrtFcst" in url:
|
|
return self._ok({"response": {"header": {"resultCode": "00"}, "body": {"items": {"item": [
|
|
{"category": "SKY", "fcstDate": "20261118", "fcstTime": "1200", "fcstValue": "1"}]}}}})
|
|
assert "api.open-meteo.com/v1/forecast" in url, url
|
|
return self._ok({"current": {"temperature_2m": 11.0, "weather_code": 0}})
|
|
return get
|
|
|
|
@patch('src.jarvis.tools.builtin.weather._romanise_place')
|
|
def test_a_district_gets_its_own_spots_weather_from_kma(self, mock_romanise, monkeypatch):
|
|
from src.jarvis.tools.builtin.weather import _kma_grid
|
|
monkeypatch.setenv("JARVIS_KMA_API_KEY", self.KEY)
|
|
calls = []
|
|
with patch('requests.get', side_effect=self._routes(calls)):
|
|
result = self.tool.run({"location": "부산 연제구"}, self.context)
|
|
|
|
assert result.success is True
|
|
assert result.reply_text.startswith("지금 부산 연제구 날씨는 맑음") and "기온 8도" in result.reply_text
|
|
kma = [p for u, p, _ in calls if "apihub.kma.go.kr" in u]
|
|
assert kma and all((p["nx"], p["ny"]) == _kma_grid(35.1762, 129.0797) for p in kma)
|
|
mock_romanise.assert_not_called()
|
|
|
|
def test_the_place_search_asks_for_one_place_with_its_country_and_names_the_app(self, monkeypatch):
|
|
monkeypatch.delenv("JARVIS_KMA_API_KEY", raising=False)
|
|
calls = []
|
|
with patch('requests.get', side_effect=self._routes(calls)):
|
|
self.tool.run({"location": "부산 연제구"}, self.context)
|
|
|
|
((_, params, headers),) = [c for c in calls if "nominatim.openstreetmap.org" in c[0]]
|
|
assert params["q"] == "부산 연제구" and params["limit"] == 1 and params["addressdetails"] == 1
|
|
assert "jarvis" in headers.get("User-Agent", "").lower()
|
|
|
|
@pytest.mark.parametrize("osm,osm_fails", [([], False), (None, True), ({"error": "x"}, False)])
|
|
@patch('src.jarvis.tools.builtin.weather._romanise_place', return_value=None)
|
|
def test_when_the_place_search_finds_nothing_or_fails_the_place_is_not_found(
|
|
self, _rom, osm, osm_fails, monkeypatch):
|
|
monkeypatch.delenv("JARVIS_KMA_API_KEY", raising=False)
|
|
calls = []
|
|
with patch('requests.get', side_effect=self._routes(calls, osm=osm, osm_fails=osm_fails)):
|
|
result = self.tool.run({"location": "없는 동네"}, self.context)
|
|
|
|
assert result.success is False and "could not find" in result.reply_text.lower()
|
|
|
|
@patch('src.jarvis.tools.builtin.weather._romanise_place')
|
|
def test_a_non_latin_city_name_is_not_matched_loosely_by_open_meteo(self, mock_romanise, monkeypatch):
|
|
"""Live 2026-11-18: open-meteo matched "부산" to a village called Pusan 140 km from Busan."""
|
|
monkeypatch.delenv("JARVIS_KMA_API_KEY", raising=False)
|
|
calls = []
|
|
osm = [{"lat": "35.1799", "lon": "129.0752", "name": "부산광역시", "address": {"country_code": "kr"}}]
|
|
with patch('requests.get', side_effect=self._routes(calls, osm=osm)):
|
|
result = self.tool.run({"location": "부산"}, self.context)
|
|
|
|
assert result.reply_text.startswith("지금 부산 날씨는")
|
|
assert not any("geocoding-api.open-meteo.com" in u for u, _, _ in calls)
|
|
(_, p, _), = [c for c in calls if "api.open-meteo.com/v1/forecast" in c[0]]
|
|
assert (p["latitude"], p["longitude"]) == (35.1799, 129.0752)
|
|
mock_romanise.assert_not_called()
|
|
|
|
def test_a_latin_name_open_meteo_knows_does_not_ask_openstreetmap(self, monkeypatch):
|
|
monkeypatch.delenv("JARVIS_KMA_API_KEY", raising=False)
|
|
calls = []
|
|
base = self._routes(calls)
|
|
|
|
def get(url, *a, **k):
|
|
if "geocoding-api.open-meteo.com" in url:
|
|
calls.append((url, k.get("params"), {}))
|
|
return self._ok({"results": [{"latitude": 51.5, "longitude": -0.12, "name": "London",
|
|
"country_code": "GB"}]})
|
|
return base(url, *a, **k)
|
|
with patch('requests.get', side_effect=get):
|
|
result = self.tool.run({"location": "London"}, self.context)
|
|
|
|
assert result.success and not any("nominatim" in u for u, _, _ in calls)
|
|
|
|
|
|
|
|
def test_the_day_schema_gives_each_coming_weekdays_days_ahead_from_today():
|
|
"""The model is handed the count instead of working it out (qwen3:8b counted a Wednesday's weekend as 2)."""
|
|
today = datetime.now()
|
|
text = WeatherTool().inputSchema["properties"]["days_ahead"]["description"]
|
|
saturday = (5 - today.weekday()) % 7 or 7
|
|
assert f"From today, {today.strftime('%A')}:" in text
|
|
assert f"{(today + timedelta(days=saturday)).strftime('%A')} {saturday}" in text
|