fix(datastore): alert on open sessions without silent deckeln, support stop by ID
This commit is contained in:
@@ -145,7 +145,7 @@ def test_baby_sleep_open_and_close(temp_datastore: DatastoreEngine):
|
||||
assert summary["open_sleep"]["sleep_type"] == "nap"
|
||||
|
||||
# 2. Prevent overlapping second open sleep
|
||||
with pytest.raises(ValueError, match="already has an open sleep session"):
|
||||
with pytest.raises(ValueError, match="hat bereits ein aktives Schlaf-Event"):
|
||||
temp_datastore.add_event(
|
||||
kind="sleep.session",
|
||||
subject="mila",
|
||||
@@ -573,8 +573,8 @@ 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."""
|
||||
def test_open_session_warning_without_silent_deckeln(temp_datastore: DatastoreEngine):
|
||||
"""Verifies that an unclosed sleep session is NOT silently deckelt, but alerts on every new log."""
|
||||
# 1. Open night sleep session yesterday at 19:15
|
||||
res_night = temp_datastore.add_event(
|
||||
kind="sleep.session",
|
||||
@@ -586,54 +586,65 @@ def test_auto_close_stale_sleep_session(temp_datastore: DatastoreEngine):
|
||||
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
|
||||
# 2. Add midday nap today (12:35 to 12:55)
|
||||
res_nap = temp_datastore.add_event(
|
||||
kind="sleep.session",
|
||||
subject="mila",
|
||||
actor="agent:hebamme",
|
||||
start="2026-10-09T12:35:00+02:00",
|
||||
end="2026-10-09T12:55: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}'")
|
||||
# 3. Verify night session is STILL open (never silently deckelt!)
|
||||
rows = temp_datastore.query(f"SELECT id, start, \"end\", estimated 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"])
|
||||
assert rows[0]["end"] is None
|
||||
assert rows[0]["estimated"] is False
|
||||
|
||||
# 4. Verify that feedback gives prominent notice about the open session
|
||||
feedback = res_nap["feedback"]
|
||||
assert "open_session_warning" in feedback
|
||||
assert "ist noch offen" in feedback["open_session_warning"]
|
||||
assert "ist noch offen" in feedback["day_total_formatted"]
|
||||
|
||||
|
||||
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(
|
||||
def test_stop_event_by_id(temp_datastore: DatastoreEngine):
|
||||
"""Verifies closing a specific open sleep session by ID when multiple might exist."""
|
||||
res1 = 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"},
|
||||
payload={"type": "night"},
|
||||
)
|
||||
night_id = res_night["event"]["id"]
|
||||
id1 = res1["event"]["id"]
|
||||
|
||||
# Add bounded nap from 15:20 to 15:35
|
||||
res_nap = temp_datastore.add_event(
|
||||
res2 = 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"},
|
||||
start="2026-10-09T12:35:00+02:00",
|
||||
payload={"type": "nap"},
|
||||
)
|
||||
assert res_nap["status"] == "success"
|
||||
id2 = res2["event"]["id"]
|
||||
|
||||
# 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
|
||||
# Close specific event id1
|
||||
res_stop = temp_datastore.stop_event(
|
||||
kind="sleep.session",
|
||||
subject="mila",
|
||||
event_id=id1,
|
||||
end_time="2026-10-09T07:15:00+02:00",
|
||||
)
|
||||
assert res_stop["status"] == "success"
|
||||
assert res_stop["event_id"] == id1
|
||||
|
||||
# Verify id1 is closed and id2 is still open
|
||||
row1 = temp_datastore.query(f"SELECT \"end\" FROM events WHERE id = '{id1}'")[0]
|
||||
row2 = temp_datastore.query(f"SELECT \"end\" FROM events WHERE id = '{id2}'")[0]
|
||||
assert row1["end"] is not None
|
||||
assert row2["end"] is None
|
||||
|
||||
|
||||
def test_cli_rejects_invalid_kind(temp_datastore: DatastoreEngine):
|
||||
|
||||
Reference in New Issue
Block a user