#!/usr/bin/env python3
"""
Test Date Feature for viz and raw commands
測試日期功能
"""
import sys
from pathlib import Path
from datetime import datetime, timedelta
# Add src to path
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
from weather_mcp import WeatherMCPClient
def test_viz_with_date():
"""測試 viz 功能使用指定日期"""
print("=" * 60)
print("測試 viz 功能 - 指定日期")
print("=" * 60)
client = WeatherMCPClient()
# Test yesterday
yesterday = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d")
print(f"\n📊 獲取昨天 ({yesterday}) 東京的天氣數據...")
result = client.get_city_weather("Tokyo", hours=24, target_date=yesterday)
if "error" in result:
print(f"❌ 錯誤:{result['error']}")
return
weather = result.get("weather", {})
hourly = weather.get("hourly", {})
temps = hourly.get("temperature_2m", [])
print(f"✅ 成功取得 {len(temps)} 筆溫度數據")
if temps:
print(f" 溫度範圍:{min(temps):.1f}°C ~ {max(temps):.1f}°C")
print()
def test_raw_with_date():
"""測試 raw 功能使用指定日期"""
print("=" * 60)
print("測試 raw 功能 - 指定日期")
print("=" * 60)
client = WeatherMCPClient()
# Test tomorrow
tomorrow = (datetime.now() + timedelta(days=1)).strftime("%Y-%m-%d")
print(f"\n📋 獲取明天 ({tomorrow}) 台北的原始數據...")
result = client.get_city_weather("Taipei", hours=12, target_date=tomorrow)
if "error" in result:
print(f"❌ 錯誤:{result['error']}")
return
print(f"✅ 成功取得數據")
print(f" 城市:{result.get('display_name', 'Unknown')}")
weather = result.get("weather", {})
if weather:
hourly = weather.get("hourly", {})
if hourly:
times = hourly.get("time", [])
temps = hourly.get("temperature_2m", [])
print(f" 時間範圍:{times[0] if times else 'N/A'} ~ {times[-1] if times else 'N/A'}")
print(f" 溫度預測:{min(temps):.1f}°C ~ {max(temps):.1f}°C")
print()
def test_today_default():
"""測試不提供日期時的預設行為"""
print("=" * 60)
print("測試預設行為 - 今天")
print("=" * 60)
client = WeatherMCPClient()
print(f"\n🌤️ 獲取今天倫敦的天氣(不指定日期)...")
result = client.get_city_weather("London", hours=12)
if "error" in result:
print(f"❌ 錯誤:{result['error']}")
return
weather = result.get("weather", {})
hourly = weather.get("hourly", {})
temps = hourly.get("temperature_2m", [])
print(f"✅ 成功取得 {len(temps)} 筆數據")
if temps:
print(f" 溫度範圍:{min(temps):.1f}°C ~ {max(temps):.1f}°C")
print()
def main():
"""主程式"""
print("\n🌤️ 日期功能測試\n")
test_viz_with_date()
test_raw_with_date()
test_today_default()
print("✅ 所有測試完成!")
print("\n💡 使用方法:")
print(" 命令列:python main.py --mode viz --city Tokyo --date 2025-10-01")
print(" 互動式:viz Tokyo 2025-10-01")
print(" 互動式:raw London 2025-09-30")
if __name__ == "__main__":
main()