fix(datastore): auto-close stale sleep sessions and validate event kind in CLI

This commit is contained in:
Moritz Graf
2026-10-09 18:46:39 +02:00
parent c9cbc56441
commit 88d49f85e5
2 changed files with 164 additions and 11 deletions
+92
View File
@@ -573,4 +573,96 @@ def test_cli_kind_flag_and_positional(temp_datastore: DatastoreEngine):
assert stop_data["duration_minutes"] == 60
def test_auto_close_stale_sleep_session(temp_datastore: DatastoreEngine):
"""Verifies that an open sleep session from yesterday is auto-closed when a new sleep session is logged."""
# 1. Open night sleep session yesterday at 19:15
res_night = temp_datastore.add_event(
kind="sleep.session",
subject="mila",
actor="agent:hebamme",
start="2026-10-08T19:15:00+02:00",
payload={"type": "night", "location": "bett"},
)
assert res_night["status"] == "success"
night_id = res_night["event"]["id"]
# 2. Add midday nap today at 12:35 without closing the night session first
res_nap = temp_datastore.add_event(
kind="sleep.session",
subject="mila",
actor="agent:hebamme",
start="2026-10-09T12:35:00+02:00",
payload={"type": "nap", "location": "bett"},
)
assert res_nap["status"] == "success"
nap_id = res_nap["event"]["id"]
assert nap_id != night_id
# 3. Query the night session to verify it was auto-closed with estimated=True and end=07:00
rows = temp_datastore.query(f"SELECT id, start, \"end\", estimated, note FROM events WHERE id = '{night_id}'")
assert len(rows) == 1
night_row = rows[0]
assert night_row["end"] is not None
assert "07:00:00" in str(night_row["end"])
assert night_row["estimated"] is True
assert "Auto-beendet" in str(night_row["note"])
def test_auto_close_stale_sleep_session_on_bounded_nap(temp_datastore: DatastoreEngine):
"""Verifies that logging a bounded interval today auto-closes a forgotten night session from yesterday."""
res_night = temp_datastore.add_event(
kind="sleep.session",
subject="mila",
actor="agent:hebamme",
start="2026-10-08T19:15:00+02:00",
payload={"type": "night", "location": "bett"},
)
night_id = res_night["event"]["id"]
# Add bounded nap from 15:20 to 15:35
res_nap = temp_datastore.add_event(
kind="sleep.session",
subject="mila",
actor="agent:hebamme",
start="2026-10-09T15:20:00+02:00",
end="2026-10-09T15:35:00+02:00",
payload={"type": "nap", "location": "bett"},
)
assert res_nap["status"] == "success"
# Verify night session was closed
rows = temp_datastore.query(f"SELECT id, \"end\", estimated FROM events WHERE id = '{night_id}'")
assert rows[0]["end"] is not None
assert rows[0]["estimated"] is True
def test_cli_rejects_invalid_kind(temp_datastore: DatastoreEngine):
"""Verifies CLI rejects boolean words or strings without dots as kind."""
import subprocess
import sys
from pathlib import Path
script_path = Path(__file__).resolve().parent.parent / "scripts" / "datastore.py"
data_dir = str(temp_datastore.data_dir)
proc = subprocess.run(
[
sys.executable,
str(script_path),
"add",
"true",
"--subject",
"moritz",
"--payload",
'{"item": "Snack", "qty": 100}',
],
env=dict(os.environ, OPENCLAW_DATA_DIR=data_dir),
capture_output=True,
text=True,
)
assert proc.returncode != 0
assert "Invalid kind 'true'" in proc.stderr