Files
Nexus/tests/test_server_search_history.py
T
Nexus Agent e0133b9098 feat(push,terminal): 推送搜索历史5条与终端中间搜索
推送页独立 combobox 历史(MySQL push_search,上限5);终端空态搜索回中间卡片,右侧栏仅列表。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-12 01:58:33 +08:00

97 lines
3.2 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_PUSH_SEARCH_HISTORY,
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"
def test_push_search_history_caps_at_five():
raw = {"history": [f"q{i}" for i in range(8)], "last": "q0"}
parsed = parse_search_state(raw, max_history=MAX_PUSH_SEARCH_HISTORY)
assert len(parsed["history"]) == MAX_PUSH_SEARCH_HISTORY
state = {"history": [f"q{i}" for i in range(MAX_PUSH_SEARCH_HISTORY)], "last": "q0"}
capped = apply_search_record(state, "new", max_history=MAX_PUSH_SEARCH_HISTORY)
assert capped["history"][0] == "new"
assert len(capped["history"]) == MAX_PUSH_SEARCH_HISTORY
@pytest.mark.asyncio
async def test_push_search_history_api_roundtrip(integration_env, api_client, auth_headers):
r = await api_client.get("/api/servers/push-search-history", headers=auth_headers)
assert r.status_code == 200
assert r.json() == {"history": [], "last": None}
for i in range(6):
r = await api_client.put(
"/api/servers/push-search-history",
headers=auth_headers,
json={"query": f"push-{i}"},
)
assert r.status_code == 200
body = r.json()
assert len(body["history"]) == MAX_PUSH_SEARCH_HISTORY
assert body["history"][0] == "push-5"
@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"