Files
javis_bot/tests/test_location_context.py
javis-bot cda8db7fce fix: play on YouTube in one step, Korea as the default location, searches in the user's language
- botBrowser `play`: searches YouTube for the words as said, opens the first video (not an ad,
  without the playlist) and starts it, in one call (an address given opens that page); `type`
  also takes `query`. Live 0.1.19: the model stopped on YouTube's home page or the results, a
  retry was refused as a third call, and the answer still said the video played.
- `brain.location` (기본 위치, default 대한민국) goes to the brain as `location_name`: the time line
  says it without GeoIP (no database on the game PC), and getWeather without a place uses it.
- Planner and webSearch keep search words in the user's language (never translated into
  English), so recommendations fit where the user is.

Evals (qwen3:8b, 3 runs): play ending on a playing video found with the words as said 0/6 -> 5/6;
Korean searches for Korean requests 6/9 -> 8/9.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 17:26:30 +09:00

144 lines
6.5 KiB
Python

import types
from unittest.mock import patch
from jarvis.reply.engine import run_reply_engine
from jarvis.utils.location import (
get_location_context,
_get_external_ip_automatically,
)
class DummyDB:
pass
class DummyDialogueMemory:
def has_recent_messages(self):
return False
def get_recent_messages(self):
return []
def add_message(self, role, content):
pass
class DummyTTS:
enabled = False
def _make_cfg(**overrides):
# Minimal settings object with required attributes referenced in engine
base = {
'ollama_base_url': 'http://127.0.0.1:11434',
'ollama_chat_model': 'gemma4',
'ollama_embed_model': 'nomic-embed-text',
'llm_profile_select_timeout_sec': 0.1,
'llm_tools_timeout_sec': 0.1,
'llm_embed_timeout_sec': 0.1,
'llm_chat_timeout_sec': 0.1,
'agentic_max_turns': 1,
'active_profiles': ['developer'],
'voice_debug': False,
'memory_enrichment_max_results': 0,
'mcps': {},
'location_enabled': True,
'location_auto_detect': False,
'location_ip_address': None,
'location_cgnat_resolve_public_ip': True,
}
base.update(overrides)
return types.SimpleNamespace(**base)
def test_get_location_context_disabled_flag():
cfg = _make_cfg(location_enabled=False)
# Direct call should be 'Location: Unknown' since we bypass engine wrapper
direct = get_location_context(config_ip=None, auto_detect=False, resolve_cgnat_public_ip=True)
# But engine should inject a context message that explicitly shows disabled
# We can't fully run LLM chat here (would require external service), so instead
# we call the internal helper indirectly by simulating run_reply_engine with 0 turns.
# Set agentic_max_turns=0 to skip loop and ensure no network activity.
cfg.agentic_max_turns = 0
reply = run_reply_engine(DummyDB(), cfg, DummyTTS(), "test message", DummyDialogueMemory())
# Engine returns None because no turns executed, but we assert that our disabled
# logic produced 'Location: Disabled' rather than attempting lookup (cannot easily
# capture printed system messages without refactor, so just ensure direct value plausible)
assert direct in ("Location: Unknown", "Location: Disabled")
def test_auto_detect_falls_back_to_opendns_when_upnp_and_socket_fail():
"""OpenDNS DNS query is the final fallback in auto-detection (step 3)."""
with patch("jarvis.utils.location._get_external_ip_via_upnp", return_value=None), \
patch("jarvis.utils.location._get_external_ip_via_socket", return_value=None), \
patch("jarvis.utils.location._resolve_public_ip_via_opendns", return_value="93.184.216.34") as mock_dns:
result = _get_external_ip_automatically()
mock_dns.assert_called_once()
assert result == "93.184.216.34"
def test_auto_detect_skips_opendns_when_upnp_succeeds():
"""OpenDNS is not called when UPnP already returned a public IP."""
with patch("jarvis.utils.location._get_external_ip_via_upnp", return_value="203.0.113.1"), \
patch("jarvis.utils.location._resolve_public_ip_via_opendns") as mock_dns:
result = _get_external_ip_automatically()
mock_dns.assert_not_called()
assert result == "203.0.113.1"
def test_auto_detect_skips_opendns_when_socket_succeeds():
"""OpenDNS is not called when socket heuristic already returned a public IP."""
with patch("jarvis.utils.location._get_external_ip_via_upnp", return_value=None), \
patch("jarvis.utils.location._get_external_ip_via_socket", return_value="198.51.100.5"), \
patch("jarvis.utils.location._resolve_public_ip_via_opendns") as mock_dns:
result = _get_external_ip_automatically()
mock_dns.assert_not_called()
assert result == "198.51.100.5"
def test_auto_detect_returns_none_when_all_methods_fail():
"""Returns None when UPnP, socket, and OpenDNS all fail."""
with patch("jarvis.utils.location._get_external_ip_via_upnp", return_value=None), \
patch("jarvis.utils.location._get_external_ip_via_socket", return_value=None), \
patch("jarvis.utils.location._resolve_public_ip_via_opendns", return_value=None):
result = _get_external_ip_automatically()
assert result is None
def test_auto_detect_rejects_private_ip_from_opendns():
"""Private IPs from OpenDNS are rejected (not returned as valid)."""
with patch("jarvis.utils.location._get_external_ip_via_upnp", return_value=None), \
patch("jarvis.utils.location._get_external_ip_via_socket", return_value=None), \
patch("jarvis.utils.location._resolve_public_ip_via_opendns", return_value="192.168.1.1"):
result = _get_external_ip_automatically()
assert result is None
def test_a_place_set_in_the_settings_is_the_location_and_detection_is_not_tried():
"""brain.location (default 대한민국) reaches the brain as location_name: the time line says it,
without GeoIP (which has no database on the game PC: "Location: Unknown" before)."""
from jarvis.reply import engine
with patch.object(engine, "get_location_context_with_timezone") as detect:
line = engine._live_time_location_string(types.SimpleNamespace(location_enabled=True, location_name="대한민국"))
assert "Location: 대한민국" in line and "Current local time" in line
detect.assert_not_called()
# Location turned off still wins; no place set means detection as before.
off = engine._live_time_location_string(types.SimpleNamespace(location_enabled=False, location_name="대한민국"))
assert "Location: Disabled" in off
with patch.object(engine, "get_location_context_with_timezone", return_value=("Location: Seoul", None)) as detect:
assert "Location: Seoul" in engine._live_time_location_string(types.SimpleNamespace(location_enabled=True, location_name=None))
detect.assert_called_once()
def test_the_settings_file_carries_the_place():
import json
import tempfile
from jarvis.config import load_settings
with tempfile.NamedTemporaryFile("w", suffix=".json", delete=False) as f:
json.dump({"location_name": " 대한민국 "}, f)
with patch.dict("os.environ", {"JARVIS_CONFIG_PATH": f.name}):
assert load_settings().location_name == "대한민국"
with tempfile.NamedTemporaryFile("w", suffix=".json", delete=False) as f:
json.dump({"location_name": None}, f)
with patch.dict("os.environ", {"JARVIS_CONFIG_PATH": f.name}):
assert load_settings().location_name is None