2026-06-03 07:33:42 +08:00
|
|
|
"""Reset a corrupt last_consolidated offset instead of hiding history (#4066)."""
|
|
|
|
|
|
2026-10-29 18:28:44 +08:00
|
|
|
from nanobot.session.manager import Session
|
2026-06-03 07:33:42 +08:00
|
|
|
|
|
|
|
|
|
2026-06-03 10:37:32 +08:00
|
|
|
def _session(count: int, last_consolidated: object) -> Session:
|
2026-06-03 07:33:42 +08:00
|
|
|
msgs = [{"role": "user", "content": f"msg{i}"} for i in range(count)]
|
|
|
|
|
return Session(key="chan:chat", messages=msgs, last_consolidated=last_consolidated)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_out_of_range_offset_is_reset():
|
|
|
|
|
assert _session(10, 999).last_consolidated == 0
|
|
|
|
|
assert _session(3, -5).last_consolidated == 0
|
|
|
|
|
|
|
|
|
|
|
2026-06-03 10:37:32 +08:00
|
|
|
def test_non_integer_offset_is_reset():
|
|
|
|
|
for offset in ("999", None, 0.5, True):
|
|
|
|
|
assert _session(3, offset).last_consolidated == 0
|
|
|
|
|
|
|
|
|
|
|
2026-06-03 07:33:42 +08:00
|
|
|
def test_valid_offset_is_preserved():
|
|
|
|
|
session = _session(10, 4)
|
|
|
|
|
assert session.last_consolidated == 4
|
2026-08-27 21:21:15 +08:00
|
|
|
assert session.last_archived == 4
|
2026-09-08 13:16:45 +08:00
|
|
|
assert session.get_history() == [
|
|
|
|
|
{"role": "user", "content": f"msg{i}"} for i in range(4, 10)
|
|
|
|
|
]
|
2026-07-25 21:48:20 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_session_post_init_coerces_null_metadata():
|
|
|
|
|
session = Session(key="chan:chat", metadata=None) # type: ignore[arg-type]
|
|
|
|
|
assert session.metadata == {}
|