Files
Nexus/tests/test_server_search_history.py
T
Nexus Agent 130cc840c7 feat: 服务器搜索历史持久化到 MySQL
按管理员将 Servers 页搜索记录存入 admin_ui_preferences,换设备登录仍可恢复;首次自动迁移 localStorage。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-11 17:46:52 +08:00

66 lines
2.0 KiB
Python

"""Tests for servers page search history payload rules and API."""
from __future__ import annotations
import pytest
from server.utils.server_search_history import (
MAX_SERVER_SEARCH_HISTORY,
apply_search_record,
parse_search_state,
)
def test_apply_search_record_dedupes_and_caps():
state = {"history": ["b", "c"], "last": "b"}
next_state = apply_search_record(state, "a")
assert next_state["last"] == "a"
assert next_state["history"] == ["a", "b", "c"]
long_state = {"history": [f"q{i}" for i in range(MAX_SERVER_SEARCH_HISTORY)], "last": "q0"}
capped = apply_search_record(long_state, "new")
assert capped["history"][0] == "new"
assert len(capped["history"]) == MAX_SERVER_SEARCH_HISTORY
def test_apply_search_record_clear_last_on_empty_query():
state = {"history": ["a"], "last": "a"}
cleared = apply_search_record(state, " ")
assert cleared["last"] is None
assert cleared["history"] == ["a"]
def test_parse_search_state_normalizes():
raw = {"history": [" 1.2.3.4 ", 123, "1.2.3.4"], "last": " x "}
parsed = parse_search_state(raw)
assert parsed["history"] == ["1.2.3.4"]
assert parsed["last"] == "x"
@pytest.mark.asyncio
async def test_search_history_api_roundtrip(integration_env, api_client, auth_headers):
r = await api_client.get("/api/servers/search-history", headers=auth_headers)
assert r.status_code == 200
assert r.json() == {"history": [], "last": None}
r = await api_client.put(
"/api/servers/search-history",
headers=auth_headers,
json={"query": "10.0.0.1"},
)
assert r.status_code == 200
body = r.json()
assert body["last"] == "10.0.0.1"
assert body["history"] == ["10.0.0.1"]
r = await api_client.put(
"/api/servers/search-history",
headers=auth_headers,
json={"query": "web-01"},
)
body = r.json()
assert body["history"] == ["web-01", "10.0.0.1"]
r = await api_client.get("/api/servers/search-history", headers=auth_headers)
assert r.json()["last"] == "web-01"