|
| 1 | +""" |
| 2 | +IX-HapticSight — Tests for the in-memory runtime session store. |
| 3 | +
|
| 4 | +These tests verify the basic session-lifecycle guarantees for the |
| 5 | +backend-agnostic SessionStore introduced under `src/ohip_runtime/`. |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +import sys |
| 10 | + |
| 11 | +# Make both `ohip` and `ohip_runtime` importable without packaging |
| 12 | +sys.path.insert(0, os.path.abspath("src")) |
| 13 | + |
| 14 | +from ohip.schemas import SafetyLevel # noqa: E402 |
| 15 | +from ohip_runtime.session_store import SessionStore # noqa: E402 |
| 16 | +from ohip_runtime.state import ( # noqa: E402 |
| 17 | + ExecutionState, |
| 18 | + InteractionSession, |
| 19 | + InteractionState, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +def make_session(session_id: str) -> InteractionSession: |
| 24 | + return InteractionSession( |
| 25 | + session_id=session_id, |
| 26 | + subject_id=f"{session_id}-subject", |
| 27 | + interaction_state=InteractionState.IDLE, |
| 28 | + execution_state=ExecutionState.IDLE, |
| 29 | + safety_level=SafetyLevel.GREEN, |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +def test_create_and_get_session_snapshot(): |
| 34 | + store = SessionStore() |
| 35 | + created = store.create(make_session("s-1")) |
| 36 | + |
| 37 | + assert created.session_id == "s-1" |
| 38 | + assert store.exists("s-1") is True |
| 39 | + assert store.count() == 1 |
| 40 | + |
| 41 | + loaded = store.get("s-1") |
| 42 | + assert loaded is not None |
| 43 | + assert loaded.session_id == "s-1" |
| 44 | + assert loaded.subject_id == "s-1-subject" |
| 45 | + |
| 46 | + |
| 47 | +def test_duplicate_create_raises_without_overwrite(): |
| 48 | + store = SessionStore() |
| 49 | + store.create(make_session("s-2")) |
| 50 | + |
| 51 | + try: |
| 52 | + store.create(make_session("s-2")) |
| 53 | + raised = False |
| 54 | + except ValueError: |
| 55 | + raised = True |
| 56 | + |
| 57 | + assert raised is True |
| 58 | + assert store.count() == 1 |
| 59 | + |
| 60 | + |
| 61 | +def test_create_with_overwrite_replaces_existing(): |
| 62 | + store = SessionStore() |
| 63 | + |
| 64 | + original = make_session("s-3") |
| 65 | + store.create(original) |
| 66 | + |
| 67 | + replacement = make_session("s-3") |
| 68 | + replacement.subject_id = "updated-subject" |
| 69 | + replacement.interaction_state = InteractionState.VERIFY |
| 70 | + |
| 71 | + store.create(replacement, overwrite=True) |
| 72 | + |
| 73 | + loaded = store.require("s-3") |
| 74 | + assert loaded.subject_id == "updated-subject" |
| 75 | + assert loaded.interaction_state == InteractionState.VERIFY |
| 76 | + |
| 77 | + |
| 78 | +def test_get_returns_detached_snapshot(): |
| 79 | + store = SessionStore() |
| 80 | + store.create(make_session("s-4")) |
| 81 | + |
| 82 | + loaded = store.require("s-4") |
| 83 | + loaded.subject_id = "mutated-locally" |
| 84 | + loaded.interaction_state = InteractionState.CONTACT |
| 85 | + |
| 86 | + fresh = store.require("s-4") |
| 87 | + assert fresh.subject_id == "s-4-subject" |
| 88 | + assert fresh.interaction_state == InteractionState.IDLE |
| 89 | + |
| 90 | + |
| 91 | +def test_update_replaces_existing_snapshot(): |
| 92 | + store = SessionStore() |
| 93 | + session = make_session("s-5") |
| 94 | + store.create(session) |
| 95 | + |
| 96 | + updated = store.require("s-5") |
| 97 | + updated.execution_state = ExecutionState.READY |
| 98 | + updated.interaction_state = InteractionState.APPROACH |
| 99 | + |
| 100 | + store.update(updated) |
| 101 | + |
| 102 | + loaded = store.require("s-5") |
| 103 | + assert loaded.execution_state == ExecutionState.READY |
| 104 | + assert loaded.interaction_state == InteractionState.APPROACH |
| 105 | + |
| 106 | + |
| 107 | +def test_update_unknown_session_raises_key_error(): |
| 108 | + store = SessionStore() |
| 109 | + |
| 110 | + try: |
| 111 | + store.update(make_session("missing")) |
| 112 | + raised = False |
| 113 | + except KeyError: |
| 114 | + raised = True |
| 115 | + |
| 116 | + assert raised is True |
| 117 | + |
| 118 | + |
| 119 | +def test_upsert_and_bulk_upsert_work(): |
| 120 | + store = SessionStore() |
| 121 | + |
| 122 | + assert store.count() == 0 |
| 123 | + |
| 124 | + store.upsert(make_session("s-6")) |
| 125 | + assert store.count() == 1 |
| 126 | + |
| 127 | + written = store.bulk_upsert( |
| 128 | + [ |
| 129 | + make_session("s-7"), |
| 130 | + make_session("s-8"), |
| 131 | + ] |
| 132 | + ) |
| 133 | + assert written == 2 |
| 134 | + assert store.count() == 3 |
| 135 | + assert store.list_ids() == ["s-6", "s-7", "s-8"] |
| 136 | + |
| 137 | + |
| 138 | +def test_delete_and_clear(): |
| 139 | + store = SessionStore() |
| 140 | + store.bulk_upsert( |
| 141 | + [ |
| 142 | + make_session("s-9"), |
| 143 | + make_session("s-10"), |
| 144 | + ] |
| 145 | + ) |
| 146 | + |
| 147 | + assert store.count() == 2 |
| 148 | + |
| 149 | + deleted = store.delete("s-9") |
| 150 | + assert deleted is True |
| 151 | + assert store.exists("s-9") is False |
| 152 | + assert store.count() == 1 |
| 153 | + |
| 154 | + store.clear() |
| 155 | + assert store.count() == 0 |
| 156 | + assert store.list_sessions() == [] |
0 commit comments