test_skill_list_routes.py 2.47 KB
#!/usr/bin/env python3
"""Test for Skill List API routes — File 1: app.py"""
import subprocess, json, sys, os

BASE = "http://127.0.0.1:5001"
fail = 0

def check(label, condition):
    global fail
    if condition:
        print(f"  PASS: {label}")
    else:
        print(f"  FAIL: {label}")
        fail += 1

def curl_get(path):
    r = subprocess.run(["curl", "-s", f"{BASE}{path}", "--connect-timeout", "5", "--max-time", "10"],
                       capture_output=True, text=True, timeout=15)
    return r.stdout, r.returncode

print("=== TC1: GET /api/skills ===")
out, _ = curl_get("/api/skills")
try:
    data = json.loads(out)
    check("ok=true", data.get("ok") == True)
    items = data.get("data", [])
    check("data is list", isinstance(items, list))
    check("data has items", len(items) > 0)
    if items:
        item = items[0]
        check("has name", "name" in item)
        check("has use_count", "use_count" in item)
        check("has created_at", "created_at" in item)
        check("has job_count", "job_count" in item)
except Exception as e:
    check(f"parse ok: {e}", False)

print("\n=== TC2: GET /api/skill/feishu-todo-sync ===")
out, _ = curl_get("/api/skill/feishu-todo-sync")
try:
    data = json.loads(out)
    check("ok=true", data.get("ok") == True)
    detail = data.get("data", {})
    check("has content", isinstance(detail.get("content"), str))
    check("has jobs", isinstance(detail.get("jobs"), list))
    check("content non-empty", len(detail.get("content", "")) > 0)
except Exception as e:
    check(f"parse ok: {e}", False)

print("\n=== TC3: GET /api/skill/nonexistent-skill-xyz ===")
out, rc = curl_get("/api/skill/nonexistent-skill-xyz")
try:
    data = json.loads(out)
    check("ok=false (404)", data.get("ok") == False)
except Exception as e:
    check(f"parse ok: {e}", False)

print("\n=== TC4: GET /skills (HTML page) ===")
out, _ = curl_get("/skills")
check("returns HTML", "<html" in out.lower() or "<!doctype" in out.lower())
check("has skills keyword", "skills" in out.lower())

print("\n=== TC5: GET /skill/feishu-todo-sync (HTML page) ===")
out, _ = curl_get("/skill/feishu-todo-sync")
check("returns HTML", "<html" in out.lower() or "<!doctype" in out.lower())
check("has skill_name feishu-todo-sync", "feishu-todo-sync" in out)
check("has SKILL.md tab", "SKILL.md" in out)
check("has tab switching JS", "switchTab" in out)

print(f"\n{'='*40}")
if fail == 0:
    print("ALL TESTS PASSED")
    sys.exit(0)
else:
    print(f"{fail} TEST(S) FAILED")
    sys.exit(1)