from memory_system_api.store import OpenVikingUserKeyStore


def test_store_persists_openviking_user_session_and_task_metadata(tmp_path):
    db_path = tmp_path / "memory.sqlite3"
    store = OpenVikingUserKeyStore(str(db_path))

    store.save_user_key("userA", "userA-key")
    store.save_session("userA", "sessionA1")
    store.save_task(
        user_id="userA",
        session_id="sessionA1",
        task_id="task-1",
        archive_uri="viking://session/userA/sessionA1/history/archive_001",
    )

    reopened = OpenVikingUserKeyStore(str(db_path))

    assert reopened.get_user_key("userA") == "userA-key"
    assert reopened.user_key_matches("userA", "userA-key")
    assert reopened.get_session("userA", "sessionA1") == {
        "user_id": "userA",
        "session_id": "sessionA1",
        "latest_task_id": "task-1",
        "latest_archive_uri": "viking://session/userA/sessionA1/history/archive_001",
    }
    assert reopened.get_task("task-1") == {
        "task_id": "task-1",
        "user_id": "userA",
        "session_id": "sessionA1",
        "archive_uri": "viking://session/userA/sessionA1/history/archive_001",
    }
