Files
ea-chatbot-lg/backend/tests/test_vfs_robustness.py

49 lines
1.5 KiB
Python

import pytest
import threading
from ea_chatbot.utils.vfs import safe_vfs_copy
def test_safe_vfs_copy_success():
"""Test standard success case."""
vfs = {
"test.csv": {"content": "data", "metadata": {"type": "csv"}},
"num": 42
}
copied = safe_vfs_copy(vfs)
assert copied == vfs
assert copied is not vfs
assert copied["test.csv"] is not vfs["test.csv"]
def test_safe_vfs_copy_handles_non_copyable():
"""Test replacing uncopyable objects with error placeholders."""
# A threading.Lock is famously uncopyable
lock = threading.Lock()
vfs = {
"safe_file": "important data",
"unsafe_lock": lock
}
copied = safe_vfs_copy(vfs)
# Safe one remains
assert copied["safe_file"] == "important data"
# Unsafe one is REPLACED with an error dict
assert isinstance(copied["unsafe_lock"], dict)
assert "content" in copied["unsafe_lock"]
assert "ERROR" in copied["unsafe_lock"]["content"]
assert copied["unsafe_lock"]["metadata"]["type"] == "error"
assert "lock" in str(copied["unsafe_lock"]["metadata"]["error"]).lower()
# Original is unchanged (it was a lock)
assert vfs["unsafe_lock"] is lock
def test_safe_vfs_copy_preserves_nested_copyable():
"""Test deepcopy still works for complex but copyable objects."""
vfs = {
"data": {"a": [1, 2, 3], "b": {"c": True}}
}
copied = safe_vfs_copy(vfs)
assert copied["data"]["a"] == [1, 2, 3]
assert copied["data"]["a"] is not vfs["data"]["a"]