Files
marketplace-ai/tests/test_netexpert_package.py
2026-11-27 14:11:56 +03:00

168 lines
6.2 KiB
Python

import json
import re
from pathlib import Path
from urllib.parse import urlsplit
import pytest
import yaml
ROOT = Path(__file__).resolve().parents[1]
PLUGIN = ROOT / "plugins" / "netexpert"
MANIFEST = Path(".codex-plugin/plugin.json")
SKILLS = {
"diagnostics",
"dns",
"linux-networking",
"network-change-review",
"network-security",
"networking",
"openwrt",
"performance",
"protocol-development",
"reverse-proxy",
"sing-box",
"vpn-protocols",
"xray",
}
def read_json(path: Path) -> dict:
return json.loads(path.read_text(encoding="utf-8"))
def test_codex_manifest_bundles_skills_and_mcp() -> None:
assert not (PLUGIN / "plugin.json").exists()
manifest = read_json(PLUGIN / MANIFEST)
assert manifest["name"] == "netexpert"
assert manifest["skills"] == "./skills/"
assert manifest["mcpServers"] == "./.mcp.json"
assert (PLUGIN / manifest["skills"]).is_dir()
assert (PLUGIN / manifest["mcpServers"]).is_file()
def test_marketplace_registration() -> None:
marketplace = read_json(ROOT / ".agents" / "plugins" / "marketplace.json")
assert marketplace["name"] == "creative-ai"
assert marketplace["interface"]["displayName"] == "Creative AI Marketplace"
entries = {entry["name"]: entry for entry in marketplace["plugins"]}
assert len(entries) == len(marketplace["plugins"])
assert {"netexpert", "server-ops"} <= entries.keys()
for name in ("netexpert", "server-ops"):
source = entries[name]["source"]
assert source["source"] == "local"
assert source["path"].startswith("./")
plugin = (ROOT / source["path"]).resolve(strict=True)
assert plugin.is_relative_to(ROOT)
manifest = MANIFEST if name == "netexpert" else Path("plugin.json")
assert read_json(plugin / manifest)["name"] == name
assert entries["netexpert"]["policy"]["installation"] == "AVAILABLE"
display = read_json(PLUGIN / MANIFEST)["interface"]
assert display["displayName"] == "NetExpert — VPN & Networking Expert"
def test_mcp_manifest_streamable_http() -> None:
servers = read_json(PLUGIN / ".mcp.json")["mcpServers"]
assert set(servers) == {"netexpert"}
server = servers["netexpert"]
assert set(server) == {"type", "url", "bearer_token_env_var"}
assert server["type"] == "http"
assert server["bearer_token_env_var"] == "NETEXPERT_MCP_TOKEN"
url = urlsplit(server["url"])
assert url.hostname == "netexpert-mcp.creative-tg1.ru"
assert url.scheme == "https"
assert url.port is None
assert url.path == "/netexpert/mcp"
assert not (
url.query
or url.fragment
or url.username
or url.password
or server.get("headers")
)
def test_skill_inventory() -> None:
assert {
path.parent.name for path in (PLUGIN / "skills").glob("*/SKILL.md")
} == SKILLS
@pytest.mark.parametrize("name", sorted(SKILLS))
def test_skill_metadata(name: str) -> None:
content = (PLUGIN / "skills" / name / "SKILL.md").read_text(encoding="utf-8")
separator, metadata, instructions = content.split("---", maxsplit=2)
assert not separator
skill = yaml.safe_load(metadata)
assert skill["name"] == name
assert re.fullmatch(r"[a-z0-9]+(?:-[a-z0-9]+)*", name)
assert 1 <= len(name) <= 64
assert 1 <= len(skill["description"]) <= 1024
assert "get_document" in instructions
assert "search_" in instructions
shared = "[shared knowledge workflow](../knowledge-workflow.md)"
assert instructions.index(shared) < instructions.index("`search_docs`")
def test_diagnostic_playbooks_and_change_review_are_linked() -> None:
skills = PLUGIN / "skills"
diagnostics = (skills / "diagnostics" / "SKILL.md").read_text(encoding="utf-8")
reference = "references/symptom-playbooks.md"
assert f"]({reference})" in diagnostics
playbooks = (skills / "diagnostics" / reference).read_text(encoding="utf-8")
for title in (
"VPN shows connected and traffic stalls",
"DNS fails for some clients",
"Small requests succeed and large ones stall",
):
assert f"## {title}" in playbooks
for skill in ("xray", "sing-box", "openwrt"):
instructions = (skills / skill / "SKILL.md").read_text(encoding="utf-8")
assert "](../network-change-review/SKILL.md)" in instructions
def test_shared_knowledge_workflow() -> None:
workflow = (PLUGIN / "skills" / "knowledge-workflow.md").read_text(encoding="utf-8")
for action in (
"submit_source",
"request_refresh",
"report_gap",
"report_unavailable",
"retire_source",
"restore_source",
"status",
):
assert f"`{action}`" in workflow
assert "wait until it is indexed" in workflow
assert "`replacement_source_id`" in workflow
def test_mcp_errors_have_specific_recovery() -> None:
workflow = (PLUGIN / "skills" / "knowledge-workflow.md").read_text(encoding="utf-8")
for signal, guidance in (
("`401`", "`NETEXPERT_MCP_TOKEN`"),
("`403`", "Host or Origin"),
("`429`", "`Retry-After`"),
("`503`", "`restore_pending`"),
("Optional tool missing", "Use the core tools"),
):
assert any(
signal in line and guidance in line for line in workflow.splitlines()
)
assert (
"Only a successful search with `not_found` indicates an index miss." in workflow
)
def test_web_fallback_follows_mcp_evidence() -> None:
workflow = (PLUGIN / "skills" / "knowledge-workflow.md").read_text(encoding="utf-8")
mcp, web = workflow.split("Use client-side web search or browsing only if")
assert "start with `search_docs` and set `freshness` to `cached`" in mcp
assert "`get_document`" in mcp and "at most one useful MCP follow-up" in mcp
assert "set `freshness` to `require_check` if current evidence matters" in mcp
gap = mcp.split("On `not_found`", 1)[1]
assert gap.index("`miss_guidance`") < gap.index("`get_source_status`")
assert "MCP is unavailable" in web and "the task permits it" in web
assert "primary publisher" in web and "what could not be verified" in web
assert "explicitly call `knowledge` with action `submit_source`" in web
assert "private data and credentials" in web and "out of web queries" in web