The 기능 screen's 기상청 날씨 card stores the KMA API hub key encrypted (kmaApiKey, masked in logs). On start the app passes it to the worker in JARVIS_KMA_API_KEY; the worker takes it out of its environment before any child process starts. Inside Korea getWeather reads 초단기실황 and the coming hour's sky from 초단기예보, and falls back to open-meteo, then met.no, on any KMA failure. The key is in KMA's URLs, so failures are logged by their kind only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
827 lines
34 KiB
Python
827 lines
34 KiB
Python
"""Tests for weather tool."""
|
|
|
|
import pytest
|
|
from unittest.mock import Mock, patch
|
|
import requests
|
|
|
|
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 ``서울`` returns
|
|
no results. 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_empty = Mock()
|
|
geo_empty.raise_for_status = Mock()
|
|
geo_empty.json.return_value = {} # no "results"
|
|
|
|
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],
|
|
},
|
|
}
|
|
|
|
mock_get.side_effect = [geo_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 second 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": "서울"}, 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
|
|
|
|
@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": "서울"}, 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"):
|
|
return self._kma([{"category": c, "obsrValue": v} for c, v in
|
|
[("PTY", pty), ("REH", "72"), ("RN1", "0"), ("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)
|
|
|
|
@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)
|
|
|
|
@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)
|