hermes-currenttime-hint/tests/test_stamper.py
Shihfu Juan 2d2b0397cc
refactor: defer _now() + _build_hint() until inside lock, drop unused now_dt param
- _now() and _build_hint() now run inside the lock, only when a
  trigger actually fires — no wasted datetime work on non-inject turns
- _should_inject() signature simplified to (state, now_monotonic)
- time sampling is now entirely inside the lock boundary
2026-07-10 20:31:01 +08:00

180 lines
6.6 KiB
Python

"""Tests for the time-stamper core logic."""
from __future__ import annotations
import importlib.util
import re
import sys
from datetime import UTC, datetime
from pathlib import Path
from zoneinfo import ZoneInfo
import pytest
PLUGIN_DIR = Path(__file__).resolve().parent.parent
PLUGIN_INIT = PLUGIN_DIR / "__init__.py"
def _load_plugin():
spec = importlib.util.spec_from_file_location("hermes_currenttime_hint", PLUGIN_INIT)
assert spec is not None and spec.loader is not None
module = importlib.util.module_from_spec(spec)
# Register in sys.modules so dataclass decorator can resolve the module.
sys.modules["hermes_currenttime_hint"] = module
spec.loader.exec_module(module)
return module
plugin = _load_plugin()
TIME_INTERVAL_SECONDS = plugin.TIME_INTERVAL_SECONDS
USER_MESSAGE_INTERVAL = plugin.USER_MESSAGE_INTERVAL
_SessionState = plugin._SessionState
_build_hint = plugin._build_hint
_format_time = plugin._format_time
_on_pre_llm_call = plugin._on_pre_llm_call
_should_inject = plugin._should_inject
reset_state = plugin.reset_state
@pytest.fixture(autouse=True)
def _clean_state() -> None:
"""Each test starts with an empty state dict."""
reset_state()
# ---------------------------------------------------------------------------
# Pure decision function: _should_inject
# ---------------------------------------------------------------------------
class TestShouldInject:
def test_first_call_always_injects(self) -> None:
state = _SessionState()
assert _should_inject(state, now_monotonic=100.0)
def test_count_below_interval_does_not_inject(self) -> None:
state = _SessionState(
count=USER_MESSAGE_INTERVAL - 1,
last_inserted_at=100.0,
initialized=True,
)
# 100s later, far below 30-minute wall-clock trigger.
assert _should_inject(state, now_monotonic=200.0) is False
def test_count_at_interval_injects(self) -> None:
state = _SessionState(
count=USER_MESSAGE_INTERVAL,
last_inserted_at=100.0,
initialized=True,
)
assert _should_inject(state, now_monotonic=100.1) is True
def test_count_above_interval_still_injects(self) -> None:
state = _SessionState(
count=USER_MESSAGE_INTERVAL + 10,
last_inserted_at=100.0,
initialized=True,
)
assert _should_inject(state, now_monotonic=100.1) is True
def test_wall_clock_below_interval_does_not_inject(self) -> None:
state = _SessionState(
count=1,
last_inserted_at=100.0,
initialized=True,
)
now = 100.0 + TIME_INTERVAL_SECONDS - 1
assert _should_inject(state, now_monotonic=now) is False
def test_wall_clock_at_interval_injects(self) -> None:
state = _SessionState(
count=1,
last_inserted_at=100.0,
initialized=True,
)
now = 100.0 + TIME_INTERVAL_SECONDS
assert _should_inject(state, now_monotonic=now) is True
def test_wall_clock_above_interval_injects(self) -> None:
state = _SessionState(
count=1,
last_inserted_at=100.0,
initialized=True,
)
now = 100.0 + TIME_INTERVAL_SECONDS + 3600
assert _should_inject(state, now_monotonic=now) is True
def test_first_call_with_high_count_still_injects(self) -> None:
"""If somehow count is already > 0 but initialized is False, sentinel wins."""
state = _SessionState(count=999, initialized=False, last_inserted_at=0.0)
assert _should_inject(state, now_monotonic=5000.0) is True
# ---------------------------------------------------------------------------
# Tag format
# ---------------------------------------------------------------------------
class TestBuildHint:
def test_tag_format_is_single_line_with_offset(self) -> None:
dt = datetime(2026, 7, 10, 18, 30, 45, tzinfo=ZoneInfo("Asia/Taipei"))
hint = _build_hint(dt)
assert hint == "<current-time>2026-07-10T18:30:45+08:00</current-time>"
def test_format_time_utc(self) -> None:
dt = datetime(2026, 7, 10, 10, 30, 45, tzinfo=UTC)
assert _format_time(dt) == "2026-07-10T10:30:45+00:00"
def test_format_time_negative_offset(self) -> None:
dt = datetime(2026, 7, 10, 5, 30, 45, tzinfo=ZoneInfo("America/New_York"))
# New York in July is EDT = UTC-4.
assert _format_time(dt) == "2026-07-10T05:30:45-04:00"
def test_format_time_strips_microseconds(self) -> None:
dt = datetime(2026, 7, 10, 18, 30, 45, 123456, tzinfo=ZoneInfo("Asia/Taipei"))
assert _format_time(dt) == "2026-07-10T18:30:45+08:00"
# ---------------------------------------------------------------------------
# Hook callback: _on_pre_llm_call (integration of state + decision)
# ---------------------------------------------------------------------------
class TestOnPreLlmCall:
def test_empty_session_id_returns_none(self) -> None:
assert _on_pre_llm_call(session_id="") is None
def test_first_call_always_returns_hint(self) -> None:
result = _on_pre_llm_call(session_id="sess-A")
assert result is not None
assert result.startswith("<current-time>")
assert result.endswith("</current-time>")
def test_subsequent_calls_skip_until_interval(self) -> None:
sid = "sess-interval"
# First call: inject.
assert _on_pre_llm_call(session_id=sid) is not None
# Next (USER_MESSAGE_INTERVAL - 1) calls: no inject.
for _ in range(USER_MESSAGE_INTERVAL - 1):
assert _on_pre_llm_call(session_id=sid) is None
# The Nth call after the first: inject.
assert _on_pre_llm_call(session_id=sid) is not None
def test_tag_format_matches_iso_8601(self) -> None:
result = _on_pre_llm_call(session_id="sess-format")
assert result is not None
# Body should be ISO 8601 with offset, second precision.
assert re.fullmatch(
r"<current-time>\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+\-]\d{2}:\d{2}</current-time>",
result,
)
def test_separate_sessions_have_independent_counters(self) -> None:
sid_a, sid_b = "sess-A", "sess-B"
# Each first call injects.
assert _on_pre_llm_call(session_id=sid_a) is not None
assert _on_pre_llm_call(session_id=sid_b) is not None
# Each then needs USER_MESSAGE_INTERVAL - 1 more calls before next inject.
for _ in range(USER_MESSAGE_INTERVAL - 1):
assert _on_pre_llm_call(session_id=sid_a) is None
assert _on_pre_llm_call(session_id=sid_b) is None