"""Tests for plugin-storage plugin""" import os import sys import tempfile import json import pytest from pathlib import Path PLUGIN_DIR = Path(__file__).parent.parent / "store" / "NebulaShell" / "plugin-storage" sys.path.insert(0, str(PLUGIN_DIR)) import importlib.util spec = importlib.util.spec_from_file_location("storage_main", str(PLUGIN_DIR / "main.py")) main_module = importlib.util.module_from_spec(spec) spec.loader.exec_module(main_module) PluginStorage = main_module.PluginStorage class TestPluginStorage: @pytest.fixture def storage(self, tmp_path): s = PluginStorage() s._base_dir = tmp_path / "plugin-storage" s._base_dir.mkdir(parents=True, exist_ok=True) return s def test_set_and_get(self, storage): storage.set("test-plugin", "name", "hello") assert storage.get("test-plugin", "name") == "hello" def test_get_default(self, storage): assert storage.get("test-plugin", "missing", "default") == "default" def test_get_nonexistent(self, storage): assert storage.get("test-plugin", "missing") is None def test_delete(self, storage): storage.set("test-plugin", "key", "val") assert storage.get("test-plugin", "key") == "val" storage.delete("test-plugin", "key") assert storage.get("test-plugin", "key") is None def test_list_keys(self, storage): storage.set("test-plugin", "a", 1) storage.set("test-plugin", "b", 2) keys = storage.list_keys("test-plugin") assert "a" in keys assert "b" in keys def test_clear(self, storage): storage.set("test-plugin", "x", 1) storage.clear("test-plugin") assert storage.get("test-plugin", "x") is None def test_raw_storage(self, storage): storage.set_raw("test-plugin", "data.bin", b"hello world") assert storage.get_raw("test-plugin", "data.bin") == b"hello world" def test_delete_raw(self, storage): storage.set_raw("test-plugin", "tmp.bin", b"123") assert storage.get_raw("test-plugin", "tmp.bin") is not None storage.delete_raw("test-plugin", "tmp.bin") assert storage.get_raw("test-plugin", "tmp.bin") is None def test_storage_size(self, storage): storage.set("test-plugin", "a", "hello") size = storage.get_storage_size("test-plugin") assert size > 0 def test_get_info(self, storage): info = storage.get_info() assert "base_dir" in info assert "plugins" in info def test_lifecycle(self, storage): storage.init() storage.start() storage.stop() def test_json_types(self, storage): data = {"nested": [1, 2, 3], "flag": True, "val": None} storage.set("test-plugin", "complex", data) assert storage.get("test-plugin", "complex") == data if __name__ == '__main__': pytest.main([__file__, '-v'])