#!/usr/bin/env python3
"""E2E: 全局搜索"""
from helper import *

header("Global Search")

# 需要两个项目来验证跨项目搜索
project_a = unique_name("E2E-搜索-A")
project_b = unique_name("E2E-搜索-B")
pid1 = create_project(project_a)
pid2 = create_project(project_b)

create_node(pid1, "Nacos服务发现", "service", "## 端口 8848\nnacos/nacos 登录")
create_node(pid2, "Shiro反序列化", "vulnerability", "## shiro 550\nrememberMe=1")

# ── Successful search ──
results = assert_ok(get("/api/v1/search?q=Nacos"))
assert len(results) >= 1
assert any(r["project_name"] == project_a for r in results)
ok("cross-project search for Nacos")

results = assert_ok(get("/api/v1/search?q=shiro"))
assert len(results) >= 1
ok("search for shiro — case-insensitive")

# ── No results ──
results = assert_ok(get("/api/v1/search?q=xyzzy12345nonexistent"))
assert results == []
ok("empty search result")

# ── Empty query → error ──
r = get("/api/v1/search?q=")
assert r.status_code == 400
ok("empty query returns 400")

# Cleanup
delete_project(pid1)
delete_project(pid2)

summary()
