Adds app/services/diagnose.py with parse_time_window() (dateparser-backed NL time phrase extraction with 60-min fallback) and diagnose() (layered FTS + window search returning severity/source summary). Includes 5 TDD tests.
52 lines
2.3 KiB
Python
52 lines
2.3 KiB
Python
"""Tests for app/services/diagnose.py — parse_time_window."""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
from unittest.mock import patch
|
|
|
|
|
|
from app.services.diagnose import parse_time_window
|
|
|
|
|
|
def test_no_time_phrase_falls_back_to_last_60_min():
|
|
with patch("app.services.diagnose._HAS_DATEPARSER", True), \
|
|
patch("app.services.diagnose._search_dates", return_value=None):
|
|
since, until, keywords = parse_time_window("plex stopped playing audio")
|
|
assert since is not None and until is not None
|
|
assert keywords == "plex stopped playing audio"
|
|
diff = (datetime.fromisoformat(until) - datetime.fromisoformat(since)).total_seconds()
|
|
assert abs(diff - 3600) < 5
|
|
|
|
|
|
def test_time_phrase_detected_produces_30min_window():
|
|
dt = datetime(2026, 5, 11, 14, 0, tzinfo=timezone.utc)
|
|
with patch("app.services.diagnose._HAS_DATEPARSER", True), \
|
|
patch("app.services.diagnose._search_dates", return_value=[("around 2pm", dt)]):
|
|
since, until, keywords = parse_time_window("plex stopped audio around 2pm")
|
|
diff = (datetime.fromisoformat(until) - datetime.fromisoformat(since)).total_seconds()
|
|
assert abs(diff - 3600) < 5
|
|
|
|
|
|
def test_time_phrase_stripped_from_keywords():
|
|
dt = datetime(2026, 5, 11, 14, 0, tzinfo=timezone.utc)
|
|
with patch("app.services.diagnose._HAS_DATEPARSER", True), \
|
|
patch("app.services.diagnose._search_dates", return_value=[("around 2pm", dt)]):
|
|
_, _, keywords = parse_time_window("plex stopped audio around 2pm")
|
|
assert "around 2pm" not in keywords
|
|
assert "plex" in keywords
|
|
|
|
|
|
def test_no_dateparser_falls_back_to_60min():
|
|
with patch("app.services.diagnose._HAS_DATEPARSER", False):
|
|
since, until, keywords = parse_time_window("plex stopped playing audio")
|
|
assert keywords == "plex stopped playing audio"
|
|
diff = (datetime.fromisoformat(until) - datetime.fromisoformat(since)).total_seconds()
|
|
assert abs(diff - 3600) < 5
|
|
|
|
|
|
def test_keywords_cleaned_of_extra_spaces():
|
|
dt = datetime(2026, 5, 11, 14, 0, tzinfo=timezone.utc)
|
|
with patch("app.services.diagnose._HAS_DATEPARSER", True), \
|
|
patch("app.services.diagnose._search_dates", return_value=[("around 2pm", dt)]):
|
|
_, _, keywords = parse_time_window("plex stopped audio around 2pm extra")
|
|
assert " " not in keywords
|