"""Integration tests for storage.get / storage.set — require a live browser.""" import time _KEY_PREFIX = "__pytest_storage_" def test_storage_set_and_get_roundtrip(browser, http_tab): """Set a localStorage key then read it back.""" browser("tabs.active", {"tabId": http_tab["id"]}) key = _KEY_PREFIX + "roundtrip" value = "hello-storage" browser("storage.set", {"key": key, "value": value, "type": "local", "tabId": http_tab["id"]}) result = browser("storage.get", {"key": key, "type": "local", "tabId": http_tab["id"]}) assert result == value def test_storage_get_nonexistent_returns_null(browser, http_tab): """Getting a key that was never set returns None.""" browser("tabs.active", {"tabId": http_tab["id"]}) result = browser("storage.get", { "key": _KEY_PREFIX + "no_such_key_zzz", "type": "local", "tabId": http_tab["id"], }) assert result is None def test_storage_get_all_returns_dict(browser, http_tab): """Calling storage.get without a key dumps all entries as a dict.""" browser("tabs.active", {"tabId": http_tab["id"]}) # Plant at least one key so the dump isn't trivially empty browser("storage.set", {"key": _KEY_PREFIX + "dump", "value": "present", "type": "local", "tabId": http_tab["id"]}) result = browser("storage.get", {"key": None, "type": "local", "tabId": http_tab["id"]}) assert isinstance(result, dict) assert _KEY_PREFIX + "dump" in result def test_storage_session_type_roundtrip(browser, http_tab): """sessionStorage works the same way as localStorage.""" browser("tabs.active", {"tabId": http_tab["id"]}) key = _KEY_PREFIX + "session" value = "ses-val" browser("storage.set", {"key": key, "value": value, "type": "session", "tabId": http_tab["id"]}) result = browser("storage.get", {"key": key, "type": "session", "tabId": http_tab["id"]}) assert result == value def test_storage_overwrite_updates_value(browser, http_tab): """Setting the same key twice reflects the latest value.""" browser("tabs.active", {"tabId": http_tab["id"]}) key = _KEY_PREFIX + "overwrite" browser("storage.set", {"key": key, "value": "v1", "type": "local", "tabId": http_tab["id"]}) browser("storage.set", {"key": key, "value": "v2", "type": "local", "tabId": http_tab["id"]}) result = browser("storage.get", {"key": key, "type": "local", "tabId": http_tab["id"]}) assert result == "v2"