1195 lines
44 KiB
Python
1195 lines
44 KiB
Python
import os
|
|
import sys
|
|
import json
|
|
import uuid
|
|
import time
|
|
import queue
|
|
import threading
|
|
import subprocess
|
|
import logging
|
|
from pathlib import Path
|
|
from datetime import datetime, timedelta
|
|
from flask import Flask, request, jsonify, Response, send_from_directory
|
|
from flask_cors import CORS
|
|
|
|
from agent.ui_analyzer import UIAnalyzer
|
|
from agent.code_analyzer import CodeAnalyzer
|
|
from agent.domain_extractor import DomainExtractor
|
|
from agent.llm_client import LLMClient, LLMConfig
|
|
from agent.prompt_builder import PromptBuilder
|
|
from agent.test_generator import TestGenerator
|
|
from agent.app import TestAgent
|
|
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(name)s: %(message)s")
|
|
logger = logging.getLogger("testagent-platform")
|
|
|
|
app = Flask(__name__)
|
|
CORS(app)
|
|
|
|
PROJECT_ROOT = Path(__file__).parent.parent
|
|
WORK_DIR = PROJECT_ROOT / "work"
|
|
OUTPUT_DIR = PROJECT_ROOT / "output"
|
|
REPORTS_DIR = PROJECT_ROOT / "platform_reports"
|
|
ARTIFACTS_DIR = PROJECT_ROOT / "platform_artifacts"
|
|
|
|
for d in [WORK_DIR, OUTPUT_DIR, REPORTS_DIR, ARTIFACTS_DIR, ARTIFACTS_DIR / "screenshots"]:
|
|
d.mkdir(parents=True, exist_ok=True)
|
|
|
|
DB_PATH = PROJECT_ROOT / "platform_db.json"
|
|
DB_LOCK = threading.Lock()
|
|
|
|
tasks_db: dict[str, dict] = {}
|
|
reports_db: dict[str, dict] = {}
|
|
process_pids: dict[str, int] = {}
|
|
log_queues: dict[str, queue.Queue] = {}
|
|
repos_db: dict[str, dict] = {}
|
|
|
|
EXISTING_TESTS_ROOT = PROJECT_ROOT / "tests"
|
|
ROBOGO_SCRIPTS_ROOT = PROJECT_ROOT / "framework" / "scripts"
|
|
ROBOGO_BUSINESS_ROOT = PROJECT_ROOT / "framework" / "business"
|
|
ROBOGO_PROJECT_ROOT = PROJECT_ROOT
|
|
|
|
|
|
def _load_db():
|
|
global tasks_db, reports_db, repos_db
|
|
if DB_PATH.exists():
|
|
with open(DB_PATH, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
tasks_db = data.get("tasks", {})
|
|
reports_db = data.get("reports", {})
|
|
repos_db = data.get("repos", {})
|
|
logger.info(f"数据库加载: {len(tasks_db)} 任务, {len(reports_db)} 报告, {len(repos_db)} 仓库")
|
|
|
|
|
|
def _save_db():
|
|
with DB_LOCK:
|
|
purged = 0
|
|
active_tasks = [t for t in tasks_db.values() if not t.get("is_deleted")]
|
|
if len(active_tasks) > 100:
|
|
sorted_tasks = sorted(active_tasks, key=lambda t: t.get("created_at", ""))
|
|
for t in sorted_tasks[:50]:
|
|
tid = t["id"]
|
|
tasks_db[tid]["is_deleted"] = True
|
|
_cleanup_assets(tid)
|
|
purged += 1
|
|
|
|
data = {"tasks": tasks_db, "reports": reports_db, "repos": repos_db}
|
|
with open(DB_PATH, "w", encoding="utf-8") as f:
|
|
json.dump(data, f, ensure_ascii=False, indent=2)
|
|
if purged:
|
|
logger.info(f"数据库清理: {purged} 个旧任务")
|
|
|
|
|
|
def _cleanup_assets(tid: str):
|
|
screenshot_dir = ARTIFACTS_DIR / "screenshots"
|
|
if screenshot_dir.exists():
|
|
for f in screenshot_dir.iterdir():
|
|
if f.name.startswith(tid):
|
|
f.unlink()
|
|
|
|
|
|
def _run_task_process(task: dict):
|
|
tid = task["id"]
|
|
q = queue.Queue()
|
|
log_queues[tid] = q
|
|
|
|
def _log(level, msg):
|
|
ts = datetime.now().strftime("%H:%M:%S")
|
|
q.put({"ts": ts, "level": level, "msg": msg})
|
|
|
|
def _run():
|
|
task["status"] = "running"
|
|
task["started_at"] = datetime.now().isoformat()
|
|
_save_db()
|
|
_log("INFO", f"任务开始: {task['name']}")
|
|
|
|
task_type = task.get("task_type", "scan")
|
|
logs_all = []
|
|
|
|
try:
|
|
if task_type == "scan":
|
|
_run_scan(task, _log, logs_all)
|
|
elif task_type == "analyze":
|
|
_run_analyze(task, _log, logs_all)
|
|
elif task_type == "generate":
|
|
_run_generate(task, _log, logs_all)
|
|
elif task_type == "run_existing":
|
|
_run_existing_tests(task, _log, logs_all)
|
|
elif task_type == "repo_full":
|
|
_run_repo_full(task, _log, logs_all)
|
|
elif task_type == "diff":
|
|
_run_diff(task, _log, logs_all)
|
|
elif task_type == "ui_check":
|
|
_run_ui_check(task, _log, logs_all)
|
|
elif task_type == "ui_fix":
|
|
_run_ui_fix(task, _log, logs_all)
|
|
else:
|
|
_log("ERROR", f"未知任务类型: {task_type}")
|
|
|
|
task["status"] = "pass"
|
|
_log("SUCCESS", "任务执行完成")
|
|
except Exception as e:
|
|
task["status"] = "fail"
|
|
_log("ERROR", f"任务失败: {e}")
|
|
logger.exception(f"任务 {tid} 执行失败")
|
|
|
|
task["finished_at"] = datetime.now().isoformat()
|
|
|
|
# Parse test results for better report
|
|
test_results = _parse_pytest_results(logs_all)
|
|
total_pass = sum(1 for r in test_results if r.get("status") == "passed")
|
|
total_fail = sum(1 for r in test_results if r.get("status") == "failed")
|
|
total_skip = sum(1 for r in test_results if r.get("status") == "skipped")
|
|
total = total_pass + total_fail + total_skip
|
|
if total == 0:
|
|
total = 1
|
|
total_pass = 1 if task["status"] == "pass" else 0
|
|
total_fail = 0 if task["status"] == "pass" else 1
|
|
|
|
report = {
|
|
"task_id": tid,
|
|
"task_name": task["name"],
|
|
"product": task.get("product", "test-agent"),
|
|
"module": task.get("module", ""),
|
|
"total_runs": total,
|
|
"pass": total_pass,
|
|
"fail": total_fail,
|
|
"started_at": task["started_at"],
|
|
"finished_at": task["finished_at"],
|
|
"result": "PASS" if task["status"] == "pass" else "FAIL",
|
|
"results": test_results,
|
|
}
|
|
reports_db[tid] = report
|
|
|
|
log_file = REPORTS_DIR / f"{tid}.json"
|
|
# Convert raw string logs to structured format
|
|
structured_logs = []
|
|
for entry in logs_all:
|
|
if isinstance(entry, str):
|
|
structured_logs.append({"ts": "", "level": "INFO", "msg": entry})
|
|
else:
|
|
structured_logs.append(entry)
|
|
log_file.write_text(json.dumps(structured_logs, ensure_ascii=False, indent=2))
|
|
|
|
# Generate HTML dashboard report
|
|
try:
|
|
html_report = REPORTS_DIR / f"{tid}.html"
|
|
subprocess.run(
|
|
[sys.executable, str(PROJECT_ROOT / "scripts" / "generate_report.py"),
|
|
"--output", str(html_report), "--no-history"],
|
|
capture_output=True, timeout=30, cwd=str(PROJECT_ROOT)
|
|
)
|
|
if html_report.exists():
|
|
reports_db[tid]["html_report"] = str(html_report)
|
|
_log("SUCCESS", f"HTML 报告生成: {html_report}")
|
|
except Exception as e:
|
|
logger.warning(f"HTML 报告生成失败: {e}")
|
|
|
|
_save_db()
|
|
_log("DONE", "任务完成")
|
|
|
|
thread = threading.Thread(target=_run, daemon=True)
|
|
thread.start()
|
|
|
|
|
|
def _run_scan(task, _log, logs_all):
|
|
repo_url = task["repo_url"]
|
|
_log("INFO", f"克隆仓库: {repo_url}")
|
|
git_ops = GitOps(WORK_DIR)
|
|
repo_info = git_ops.clone_or_pull(repo_url, task.get("branch", "main"))
|
|
|
|
repo_id = str(uuid.uuid4())[:8]
|
|
repos_db[repo_id] = {
|
|
"id": repo_id,
|
|
"url": repo_url,
|
|
"branch": task.get("branch", "main"),
|
|
"local_path": str(repo_info.local_path),
|
|
"language": repo_info.language,
|
|
"framework": repo_info.framework,
|
|
"file_count": len(repo_info.files),
|
|
"scanned_at": datetime.now().isoformat(),
|
|
}
|
|
task["repo_id"] = repo_id
|
|
|
|
_log("INFO", f"语言: {repo_info.language}, 框架: {repo_info.framework}, 文件: {len(repo_info.files)}")
|
|
_log("SUCCESS", f"仓库扫描完成: {repo_id}")
|
|
|
|
analyzer = CodeAnalyzer(repo_info.local_path)
|
|
analyses = analyzer.analyze_all(repo_info.files)
|
|
_log("INFO", f"代码分析: {len(analyses)} 文件, {sum(len(a.classes) for a in analyses)} 类, {sum(len(a.functions) for a in analyses)} 函数")
|
|
|
|
extractor = DomainExtractor(repo_info.local_path)
|
|
domain_model = extractor.extract_domains(analyses)
|
|
_log("INFO", f"DDD领域: {len(domain_model.bounded_contexts)} 个限界上下文")
|
|
for ctx in domain_model.bounded_contexts:
|
|
_log("INFO", f" - {ctx.name}: {len(ctx.files)} 文件")
|
|
|
|
repos_db[repo_id]["bounded_contexts"] = [ctx.name for ctx in domain_model.bounded_contexts]
|
|
repos_db[repo_id]["ubiquitous_language"] = domain_model.ubiquitous_language
|
|
_save_db()
|
|
|
|
|
|
def _run_analyze(task, _log, logs_all):
|
|
repo_id = task.get("repo_id")
|
|
if not repo_id or repo_id not in repos_db:
|
|
_log("ERROR", "请先扫描仓库")
|
|
return
|
|
repo = repos_db[repo_id]
|
|
local_path = Path(repo["local_path"])
|
|
git_ops = GitOps(WORK_DIR)
|
|
repo_info = git_ops.clone_or_pull(repo["url"], repo.get("branch", "main"))
|
|
|
|
analyzer = CodeAnalyzer(local_path)
|
|
analyses = analyzer.analyze_all(repo_info.files)
|
|
extractor = DomainExtractor(local_path)
|
|
domain_model = extractor.extract_domains(analyses)
|
|
|
|
report_md = extractor.generate_context_report(domain_model)
|
|
report_path = OUTPUT_DIR / f"{repo_id}_domain_report.md"
|
|
report_path.write_text(report_md, encoding="utf-8")
|
|
|
|
repos_db[repo_id]["bounded_contexts"] = [ctx.name for ctx in domain_model.bounded_contexts]
|
|
repos_db[repo_id]["analyzed_at"] = datetime.now().isoformat()
|
|
_save_db()
|
|
_log("SUCCESS", f"DDD 领域分析完成: {report_path}")
|
|
|
|
|
|
def _run_generate(task, _log, logs_all):
|
|
repo_id = task.get("repo_id")
|
|
api_key = task.get("api_key", os.getenv("DEEPSEEK_API_KEY", ""))
|
|
if not api_key:
|
|
_log("ERROR", "请设置 API Key")
|
|
return
|
|
if not repo_id or repo_id not in repos_db:
|
|
_log("ERROR", "请先扫描仓库")
|
|
return
|
|
|
|
repo = repos_db[repo_id]
|
|
local_path = Path(repo["local_path"])
|
|
test_types = task.get("test_types", ["smoke", "unit", "integration"])
|
|
|
|
config = TestAgent()
|
|
config.set_api_key(api_key)
|
|
|
|
git_ops = GitOps(WORK_DIR)
|
|
repo_info = git_ops.clone_or_pull(repo["url"], repo.get("branch", "main"))
|
|
analyzer = CodeAnalyzer(local_path)
|
|
analyses = analyzer.analyze_all(repo_info.files)
|
|
extractor = DomainExtractor(local_path)
|
|
domain_model = extractor.extract_domains(analyses)
|
|
|
|
source_codes = {}
|
|
for a in analyses:
|
|
source_codes[a.path] = git_ops.get_file_content(local_path, a.path)
|
|
|
|
generator = TestGenerator(config.llm_client, config.prompt_builder)
|
|
all_tests = generator.generate_tests_for_all(analyses, source_codes, domain_model, test_types)
|
|
|
|
test_output_dir = OUTPUT_DIR / repo_id
|
|
written = generator.write_tests(all_tests, test_output_dir, domain_model)
|
|
_log("SUCCESS", f"测试生成完成: {len(written)} 个文件 -> {test_output_dir}")
|
|
|
|
|
|
def _run_repo_full(task, _log, logs_all):
|
|
_run_scan(task, _log, logs_all)
|
|
if task["status"] == "fail":
|
|
return
|
|
_run_analyze(task, _log, logs_all)
|
|
if task["status"] == "fail":
|
|
return
|
|
_run_generate(task, _log, logs_all)
|
|
|
|
|
|
def _run_diff(task, _log, logs_all):
|
|
repo_id = task.get("repo_id")
|
|
api_key = task.get("api_key", os.getenv("DEEPSEEK_API_KEY", ""))
|
|
if not api_key or not repo_id or repo_id not in repos_db:
|
|
_log("ERROR", "请先扫描仓库并设置 API Key")
|
|
return
|
|
|
|
repo = repos_db[repo_id]
|
|
local_path = Path(repo["local_path"])
|
|
against = task.get("against", "HEAD~1")
|
|
|
|
git_ops = GitOps(WORK_DIR)
|
|
diff_files = git_ops.get_diff(local_path, against)
|
|
if not diff_files:
|
|
_log("INFO", "没有检测到代码变更")
|
|
return
|
|
|
|
_log("INFO", f"变更文件: {len(diff_files)} 个")
|
|
for f in diff_files:
|
|
_log("INFO", f" - {f}")
|
|
|
|
config = TestAgent()
|
|
config.set_api_key(api_key)
|
|
analyzer = CodeAnalyzer(local_path)
|
|
analyses = analyzer.analyze_all(diff_files)
|
|
extractor = DomainExtractor(local_path)
|
|
domain_model = extractor.extract_domains(analyses)
|
|
|
|
source_codes = {}
|
|
for a in analyses:
|
|
source_codes[a.path] = git_ops.get_file_content(local_path, a.path)
|
|
|
|
generator = TestGenerator(config.llm_client, config.prompt_builder)
|
|
all_tests = generator.generate_tests_for_all(analyses, source_codes, domain_model, ["regression", "unit"])
|
|
test_output_dir = OUTPUT_DIR / f"{repo_id}_diff"
|
|
written = generator.write_tests(all_tests, test_output_dir, domain_model)
|
|
_log("SUCCESS", f"精确回归测试生成完成: {len(written)} 个文件")
|
|
|
|
|
|
def _run_ui_check(task, _log, logs_all):
|
|
frontend_url = task.get("repo_url", "")
|
|
frontend_branch = task.get("branch", "main")
|
|
api_key = task.get("api_key", os.getenv("DEEPSEEK_API_KEY", ""))
|
|
|
|
if not frontend_url:
|
|
_log("ERROR", "请提供前端仓库 URL")
|
|
return
|
|
|
|
_log("INFO", f"克隆前端仓库: {frontend_url}")
|
|
git_ops = GitOps(WORK_DIR)
|
|
fe_info = git_ops.clone_or_pull(frontend_url, frontend_branch)
|
|
_log("INFO", f"前端代码: {fe_info.language}, {len(fe_info.files)} 个文件")
|
|
|
|
_log("INFO", "提取 Robogo 测试脚本中的 UI 选择器...")
|
|
analyzer = UIAnalyzer(PROJECT_ROOT)
|
|
selectors = analyzer.extract_selectors()
|
|
_log("INFO", f"提取到 {len(selectors)} 个 UI 选择器")
|
|
|
|
_log("INFO", "与前端代码比对...")
|
|
changes = analyzer.check_frontend(selectors, fe_info.local_path)
|
|
missing = [c for c in changes if not c.found_in_frontend]
|
|
found = [c for c in changes if c.found_in_frontend]
|
|
_log("INFO", f"匹配: {len(found)}, 未匹配: {len(missing)}")
|
|
|
|
report = analyzer.generate_report(changes)
|
|
report_path = REPORTS_DIR / f"{task['id']}_ui_report.md"
|
|
report_path.write_text(report, encoding="utf-8")
|
|
_log("SUCCESS", f"UI 变更分析报告: {report_path}")
|
|
|
|
if missing:
|
|
_log("WARN", f"发现 {len(missing)} 个可能失效的选择器:")
|
|
for c in missing[:10]:
|
|
_log("WARN", f" - {c.selector.file}:{c.selector.line} [{c.selector.type}] {c.selector.value[:60]}")
|
|
|
|
if api_key:
|
|
_log("INFO", "调用 LLM 分析变更原因...")
|
|
llm_prompt = analyzer.build_llm_prompt(changes, fe_info.local_path)
|
|
if llm_prompt:
|
|
try:
|
|
config = TestAgent()
|
|
config.set_api_key(api_key)
|
|
from agent.llm_client import LLMMessage
|
|
messages = [LLMMessage(role="user", content=llm_prompt)]
|
|
system = "你是一个前端自动化测试专家,精通 Playwright 和前端 UI 测试。请分析选择器失效原因并给出修复建议。"
|
|
resp = config.llm_client.chat(messages, system_prompt=system)
|
|
suggestion_path = REPORTS_DIR / f"{task['id']}_ui_suggestions.md"
|
|
suggestion_path.write_text(resp.content, encoding="utf-8")
|
|
_log("SUCCESS", f"LLM 修复建议: {suggestion_path}")
|
|
except Exception as e:
|
|
_log("ERROR", f"LLM 分析失败: {e}")
|
|
else:
|
|
_log("SUCCESS", "所有 UI 选择器均能在前端代码中找到匹配,无需更新")
|
|
|
|
|
|
def _run_existing_tests(task, _log, logs_all):
|
|
module = task.get("module", "")
|
|
platform = task.get("platform", "dcloud")
|
|
test_marker = task.get("test_marker", "")
|
|
test_file = task.get("test_file", "")
|
|
|
|
if platform == "robogo":
|
|
if module == "robogo":
|
|
script_path = ROBOGO_SCRIPTS_ROOT / "run_all.py"
|
|
elif module == "data_loop":
|
|
script_path = ROBOGO_SCRIPTS_ROOT / "run_data_loop.py"
|
|
elif test_file:
|
|
script_path = ROBOGO_PROJECT_ROOT / test_file
|
|
else:
|
|
script_path = ROBOGO_SCRIPTS_ROOT / "run_all.py"
|
|
|
|
if not script_path.exists():
|
|
_log("ERROR", f"脚本不存在: {script_path}")
|
|
return
|
|
|
|
cmd = [sys.executable, str(script_path)]
|
|
cwd = str(ROBOGO_PROJECT_ROOT)
|
|
else:
|
|
cmd = [sys.executable, "-m", "pytest"]
|
|
if test_file:
|
|
cmd.append(str(EXISTING_TESTS_ROOT / test_file))
|
|
elif module == "e2e":
|
|
cmd.extend([str(EXISTING_TESTS_ROOT), "-m", "e2e"])
|
|
elif test_marker:
|
|
cmd.extend(["-m", test_marker])
|
|
elif module:
|
|
cmd.append(str(EXISTING_TESTS_ROOT / module))
|
|
else:
|
|
cmd.append(str(EXISTING_TESTS_ROOT))
|
|
|
|
cmd.extend(["-v", "--tb=short", "--color=no"])
|
|
if task.get("junit_xml"):
|
|
cmd.extend(["--junitxml", str(REPORTS_DIR / f"{task['id']}.xml")])
|
|
cwd = str(EXISTING_TESTS_ROOT.parent)
|
|
|
|
_log("INFO", f"执行: {' '.join(cmd)}")
|
|
env = os.environ.copy()
|
|
env["PYTEST_RUNNING"] = "1"
|
|
env["HEADLESS"] = task.get("headless", "false")
|
|
|
|
if platform == "robogo":
|
|
env["ROBOGO_ENV"] = task.get("env", "PROD")
|
|
env["ROBOGO_SCOPE"] = task.get("scope", "all")
|
|
env["TEST_USERNAME"] = task.get("account") or os.environ.get("TEST_USERNAME", "placeholder")
|
|
env["TEST_PASSWORD"] = task.get("password") or os.environ.get("TEST_PASSWORD", "placeholder")
|
|
env["AUTH_ACCOUNT"] = task.get("account") or os.environ.get("TEST_USERNAME", "placeholder")
|
|
env["AUTH_PASSWORD"] = task.get("password") or os.environ.get("TEST_PASSWORD", "placeholder")
|
|
|
|
proc = subprocess.Popen(
|
|
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
|
text=True, bufsize=1, env=env,
|
|
cwd=cwd,
|
|
)
|
|
process_pids[task["id"]] = proc.pid
|
|
|
|
for line in proc.stdout:
|
|
line = line.strip()
|
|
if line:
|
|
logs_all.append(line)
|
|
level = "ERROR" if "FAILED" in line or "ERROR" in line else "SUCCESS" if "PASSED" in line else "INFO"
|
|
_log(level, line)
|
|
|
|
proc.wait()
|
|
process_pids.pop(task["id"], None)
|
|
|
|
# Parse pytest results
|
|
test_results = _parse_pytest_results(logs_all)
|
|
if test_results:
|
|
results_path = REPORTS_DIR / f"{task['id']}_results.json"
|
|
results_path.write_text(json.dumps(test_results, ensure_ascii=False, indent=2))
|
|
|
|
if proc.returncode == 0:
|
|
_log("SUCCESS", "全部测试通过")
|
|
else:
|
|
_log("ERROR", f"测试失败 (exit code: {proc.returncode})")
|
|
|
|
|
|
# ==================== API Routes ====================
|
|
|
|
@app.route("/api/health")
|
|
def health():
|
|
return jsonify({
|
|
"status": "ok",
|
|
"version": "1.0.0",
|
|
"tasks_count": len([t for t in tasks_db.values() if not t.get("is_deleted")]),
|
|
"reports_count": len(reports_db),
|
|
"repos_count": len(repos_db),
|
|
})
|
|
|
|
|
|
# --- Repos ---
|
|
|
|
@app.route("/api/repos", methods=["GET"])
|
|
def list_repos():
|
|
return jsonify(list(repos_db.values()))
|
|
|
|
|
|
@app.route("/api/repos", methods=["POST"])
|
|
def create_repo():
|
|
data = request.get_json()
|
|
repo_url = data.get("url")
|
|
if not repo_url:
|
|
return jsonify({"error": "缺少 url"}), 400
|
|
|
|
task_id = str(uuid.uuid4())[:8]
|
|
task = {
|
|
"id": task_id,
|
|
"name": f"扫描仓库: {repo_url}",
|
|
"product": "test-agent",
|
|
"status": "pending",
|
|
"created_at": datetime.now().isoformat(),
|
|
"started_at": None,
|
|
"finished_at": None,
|
|
"task_type": "scan",
|
|
"repo_url": repo_url,
|
|
"branch": data.get("branch", "main"),
|
|
"api_key": data.get("api_key", ""),
|
|
}
|
|
tasks_db[task_id] = task
|
|
_save_db()
|
|
_run_task_process(task)
|
|
return jsonify(task)
|
|
|
|
|
|
@app.route("/api/repos/<repo_id>", methods=["DELETE"])
|
|
def delete_repo(repo_id):
|
|
if repo_id in repos_db:
|
|
del repos_db[repo_id]
|
|
_save_db()
|
|
return jsonify({"ok": True})
|
|
|
|
|
|
@app.route("/api/repos/<repo_id>/analyze", methods=["POST"])
|
|
def analyze_repo(repo_id):
|
|
if repo_id not in repos_db:
|
|
return jsonify({"error": "仓库不存在"}), 404
|
|
|
|
data = request.get_json() or {}
|
|
task_id = str(uuid.uuid4())[:8]
|
|
task = {
|
|
"id": task_id,
|
|
"name": f"DDD分析: {repos_db[repo_id]['url']}",
|
|
"product": "test-agent",
|
|
"status": "pending",
|
|
"created_at": datetime.now().isoformat(),
|
|
"task_type": "analyze",
|
|
"repo_id": repo_id,
|
|
"api_key": data.get("api_key", ""),
|
|
}
|
|
tasks_db[task_id] = task
|
|
_save_db()
|
|
_run_task_process(task)
|
|
return jsonify(task)
|
|
|
|
|
|
@app.route("/api/repos/<repo_id>/generate", methods=["POST"])
|
|
def generate_tests(repo_id):
|
|
if repo_id not in repos_db:
|
|
return jsonify({"error": "仓库不存在"}), 404
|
|
|
|
data = request.get_json() or {}
|
|
task_id = str(uuid.uuid4())[:8]
|
|
task = {
|
|
"id": task_id,
|
|
"name": f"生成测试: {repos_db[repo_id]['url']}",
|
|
"product": "test-agent",
|
|
"status": "pending",
|
|
"created_at": datetime.now().isoformat(),
|
|
"task_type": "generate",
|
|
"repo_id": repo_id,
|
|
"api_key": data.get("api_key", ""),
|
|
"test_types": data.get("test_types", ["smoke", "unit", "integration"]),
|
|
}
|
|
tasks_db[task_id] = task
|
|
_save_db()
|
|
_run_task_process(task)
|
|
return jsonify(task)
|
|
|
|
|
|
@app.route("/api/repos/<repo_id>/diff", methods=["POST"])
|
|
def diff_repo(repo_id):
|
|
if repo_id not in repos_db:
|
|
return jsonify({"error": "仓库不存在"}), 404
|
|
|
|
data = request.get_json() or {}
|
|
task_id = str(uuid.uuid4())[:8]
|
|
task = {
|
|
"id": task_id,
|
|
"name": f"精确回归: {repos_db[repo_id]['url']}",
|
|
"product": "test-agent",
|
|
"status": "pending",
|
|
"created_at": datetime.now().isoformat(),
|
|
"task_type": "diff",
|
|
"repo_id": repo_id,
|
|
"api_key": data.get("api_key", ""),
|
|
"against": data.get("against", "HEAD~1"),
|
|
}
|
|
tasks_db[task_id] = task
|
|
_save_db()
|
|
_run_task_process(task)
|
|
return jsonify(task)
|
|
|
|
|
|
@app.route("/api/repos/<repo_id>/full", methods=["POST"])
|
|
def full_pipeline(repo_id):
|
|
data = request.get_json() or {}
|
|
task_id = str(uuid.uuid4())[:8]
|
|
task = {
|
|
"id": task_id,
|
|
"name": f"全流程: {data.get('url', '已有仓库')}",
|
|
"product": "test-agent",
|
|
"status": "pending",
|
|
"created_at": datetime.now().isoformat(),
|
|
"task_type": "repo_full",
|
|
"repo_id": repo_id,
|
|
"repo_url": data.get("url", repos_db.get(repo_id, {}).get("url", "")),
|
|
"api_key": data.get("api_key", ""),
|
|
"test_types": data.get("test_types", ["smoke", "unit", "integration"]),
|
|
}
|
|
tasks_db[task_id] = task
|
|
_save_db()
|
|
_run_task_process(task)
|
|
return jsonify(task)
|
|
|
|
|
|
def _parse_pytest_results(logs: list) -> list[dict]:
|
|
import re
|
|
results = []
|
|
for line in logs:
|
|
line_str = line if isinstance(line, str) else line.get("msg", "")
|
|
# Parse PASSED/FAILED lines like: tests/data_space/tests/test_xxx.py::TestClass::test_func PASSED [ 50%]
|
|
m = re.match(r'(.+?::[\w:]+)\s+(PASSED|FAILED|SKIPPED|ERROR)\s*\[?\s*(\d+%?)', line_str)
|
|
if m:
|
|
name = m.group(1)
|
|
status = m.group(2).lower()
|
|
results.append({"name": name, "status": status, "duration": "", "module": name.split("::")[0].split("/")[0] if "/" in name else ""})
|
|
# Parse duration from summary line
|
|
m2 = re.search(r'(\d+) passed.*?(\d+) failed.*?(\d+) skipped.*?in\s+([\d.]+)s', line_str)
|
|
if m2:
|
|
results.append({"name": "总计", "status": "summary", "duration": m2.group(4) + "s", "module": "", "pass": int(m2.group(1)), "fail": int(m2.group(2)), "skip": int(m2.group(3))})
|
|
return results
|
|
frontend_url = task.get("repo_url", "")
|
|
frontend_branch = task.get("branch", "main")
|
|
api_key = task.get("api_key", os.getenv("DEEPSEEK_API_KEY", ""))
|
|
|
|
if not frontend_url or not api_key:
|
|
_log("ERROR", "请提供前端仓库 URL 和 API Key")
|
|
return
|
|
|
|
_log("INFO", f"克隆前端仓库: {frontend_url}")
|
|
git_ops = GitOps(WORK_DIR)
|
|
fe_info = git_ops.clone_or_pull(frontend_url, frontend_branch)
|
|
|
|
_log("INFO", "提取 Robogo 选择器并比对...")
|
|
analyzer = UIAnalyzer(PROJECT_ROOT)
|
|
selectors = analyzer.extract_selectors()
|
|
changes = analyzer.check_frontend(selectors, fe_info.local_path)
|
|
missing = [c for c in changes if not c.found_in_frontend]
|
|
|
|
if not missing:
|
|
_log("SUCCESS", "所有选择器均匹配,无需修复")
|
|
return
|
|
|
|
_log("INFO", f"发现 {len(missing)} 个失效选择器,调用 LLM 修复...")
|
|
config = TestAgent()
|
|
config.set_api_key(api_key)
|
|
from agent.llm_client import LLMMessage
|
|
|
|
# 备份原文件
|
|
backup_dir = PROJECT_ROOT / "framework" / "business_backup"
|
|
backup_dir.mkdir(parents=True, exist_ok=True)
|
|
for f in set(c.selector.file for c in missing):
|
|
src = PROJECT_ROOT / f
|
|
if src.exists():
|
|
import shutil
|
|
shutil.copy2(src, backup_dir / src.name)
|
|
|
|
# 分批修复
|
|
total_files = len(set(c.selector.file for c in missing))
|
|
batch_size = 1
|
|
batches = (total_files + batch_size - 1) // batch_size
|
|
all_fixed = {}
|
|
|
|
for batch_idx in range(batches):
|
|
fix_prompt = analyzer.build_fix_prompt(changes, fe_info.local_path, batch_idx, batch_size)
|
|
if not fix_prompt:
|
|
break
|
|
_log("INFO", f" 批次 {batch_idx+1}/{batches}...")
|
|
messages = [LLMMessage(role="user", content=fix_prompt)]
|
|
system = "你是一个 Playwright 自动化测试专家。请输出完整的文件代码,不要省略任何部分。"
|
|
resp = config.llm_client.chat(messages, system_prompt=system)
|
|
fixed = analyzer.apply_fixes(changes, resp.content, PROJECT_ROOT)
|
|
all_fixed.update(fixed)
|
|
for fpath, size in fixed.items():
|
|
_log("INFO", f" ✅ {fpath}: {size} chars")
|
|
|
|
_log("SUCCESS", f"修复完成: {len(all_fixed)} 个文件已更新")
|
|
|
|
# 验证修复:运行受影响的测试
|
|
_log("INFO", "验证修复效果,运行受影响的测试...")
|
|
if all_fixed:
|
|
test_file = list(all_fixed.keys())[0]
|
|
page_name = Path(test_file).stem
|
|
# 找到使用该 page 的场景脚本
|
|
for script_dir in [ROBOGO_SCRIPTS_ROOT]:
|
|
if script_dir.exists():
|
|
for script in script_dir.iterdir():
|
|
if script.suffix == '.py' and not script.name.startswith('_'):
|
|
content = script.read_text(encoding="utf-8", errors="ignore")
|
|
if page_name in content:
|
|
_log("INFO", f" 运行: {script.name}")
|
|
cmd = [sys.executable, str(script)]
|
|
env = os.environ.copy()
|
|
env["TEST_USERNAME"] = task.get("account") or os.environ.get("TEST_USERNAME", "placeholder")
|
|
env["TEST_PASSWORD"] = task.get("password") or os.environ.get("TEST_PASSWORD", "placeholder")
|
|
env["HEADLESS"] = "true"
|
|
env["ROBOGO_ENV"] = "PROD"
|
|
env["ROBOGO_SCOPE"] = "smoke"
|
|
proc = subprocess.Popen(
|
|
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
|
text=True, bufsize=1, env=env, cwd=str(PROJECT_ROOT),
|
|
)
|
|
process_pids[task["id"]] = proc.pid
|
|
for line in proc.stdout:
|
|
line = line.strip()
|
|
if line:
|
|
logs_all.append(line)
|
|
level = "ERROR" if any(k in line for k in ["FAILED", "ERROR", "Traceback"]) else "SUCCESS" if "PASSED" in line else "INFO"
|
|
_log(level, line)
|
|
proc.wait()
|
|
process_pids.pop(task["id"], None)
|
|
_log("SUCCESS" if proc.returncode == 0 else "ERROR", "测试{} (exit={})".format("通过" if proc.returncode == 0 else "失败", proc.returncode))
|
|
break
|
|
|
|
|
|
# --- UI 变更检查 ---
|
|
|
|
@app.route("/api/repos/branches", methods=["GET"])
|
|
def list_branches():
|
|
url = request.args.get("url", "")
|
|
if not url:
|
|
return jsonify({"error": "缺少 url 参数"}), 400
|
|
try:
|
|
import subprocess
|
|
r = subprocess.run(
|
|
["git", "ls-remote", "--heads", url],
|
|
capture_output=True, text=True, timeout=15
|
|
)
|
|
branches = []
|
|
default = "main"
|
|
for line in r.stdout.strip().split('\n'):
|
|
if line and 'refs/heads/' in line:
|
|
parts = line.split('\t')
|
|
commit_hash = parts[0] if len(parts) > 0 else ""
|
|
name = parts[1].replace('refs/heads/', '') if len(parts) > 1 else ""
|
|
if name:
|
|
branches.append({"name": name, "commit": commit_hash[:8]})
|
|
if name in ('main', 'master'):
|
|
default = name
|
|
|
|
if not branches:
|
|
branches = [{"name": n, "commit": ""} for n in ["main", "master", "develop"]]
|
|
default = "main"
|
|
|
|
# Try to find the latest active branch by cloning temporarily
|
|
latest_branch = default
|
|
try:
|
|
import tempfile, shutil
|
|
tmp_dir = tempfile.mkdtemp(prefix="repo_check_")
|
|
r2 = subprocess.run(
|
|
["git", "clone", "--bare", "--single-branch", "--branch", default, "--depth", "1", url, tmp_dir],
|
|
capture_output=True, text=True, timeout=30
|
|
)
|
|
if r2.returncode == 0:
|
|
r3 = subprocess.run(
|
|
["git", "-C", tmp_dir, "for-each-ref", "--sort=-committerdate", "refs/heads/", "--format=%(refname:short)"],
|
|
capture_output=True, text=True, timeout=10
|
|
)
|
|
if r3.stdout.strip():
|
|
latest_branch = r3.stdout.strip().split('\n')[0]
|
|
shutil.rmtree(tmp_dir, ignore_errors=True)
|
|
except Exception:
|
|
pass
|
|
|
|
return jsonify({
|
|
"branches": branches,
|
|
"total": len(branches),
|
|
"default": default,
|
|
"latest": latest_branch,
|
|
})
|
|
except Exception as e:
|
|
return jsonify({"branches": [{"name": n, "commit": ""} for n in ["main", "master", "develop"]], "total": 3, "default": "main", "latest": "main"})
|
|
|
|
|
|
@app.route("/api/repos/compare", methods=["POST"])
|
|
def compare_branches():
|
|
data = request.get_json() or {}
|
|
repo_url = data.get("url", "")
|
|
base_branch = data.get("base", "main")
|
|
compare_branch = data.get("compare", "dev")
|
|
if not repo_url:
|
|
return jsonify({"error": "缺少 url"}), 400
|
|
|
|
try:
|
|
import subprocess, tempfile
|
|
git_ops = GitOps(WORK_DIR)
|
|
repo_info = git_ops.clone_or_pull(repo_url, compare_branch)
|
|
diff_files = git_ops.get_diff(repo_info.local_path, f"origin/{base_branch}..origin/{compare_branch}")
|
|
return jsonify({
|
|
"base": base_branch,
|
|
"compare": compare_branch,
|
|
"changed_files": diff_files,
|
|
"total_changes": len(diff_files),
|
|
})
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
@app.route("/api/repos/check-ui", methods=["POST"])
|
|
def check_ui_changes():
|
|
data = request.get_json() or {}
|
|
frontend_url = data.get("url", "")
|
|
if not frontend_url:
|
|
return jsonify({"error": "缺少前端仓库 URL"}), 400
|
|
|
|
task_id = str(uuid.uuid4())[:8]
|
|
task = {
|
|
"id": task_id,
|
|
"name": f"UI变更检查: {frontend_url}",
|
|
"product": "test-agent",
|
|
"status": "pending",
|
|
"created_at": datetime.now().isoformat(),
|
|
"task_type": "ui_check",
|
|
"repo_url": frontend_url,
|
|
"branch": data.get("branch", "main"),
|
|
"api_key": data.get("api_key", ""),
|
|
}
|
|
tasks_db[task_id] = task
|
|
_save_db()
|
|
_run_task_process(task)
|
|
return jsonify(task)
|
|
|
|
|
|
@app.route("/api/repos/fix-ui", methods=["POST"])
|
|
def fix_ui_changes():
|
|
data = request.get_json() or {}
|
|
frontend_url = data.get("url", "")
|
|
if not frontend_url:
|
|
return jsonify({"error": "缺少前端仓库 URL"}), 400
|
|
|
|
task_id = str(uuid.uuid4())[:8]
|
|
task = {
|
|
"id": task_id,
|
|
"name": f"UI自动修复: {frontend_url}",
|
|
"product": "test-agent",
|
|
"status": "pending",
|
|
"created_at": datetime.now().isoformat(),
|
|
"task_type": "ui_fix",
|
|
"repo_url": frontend_url,
|
|
"branch": data.get("branch", "main"),
|
|
"api_key": data.get("api_key", ""),
|
|
}
|
|
tasks_db[task_id] = task
|
|
_save_db()
|
|
_run_task_process(task)
|
|
return jsonify(task)
|
|
|
|
|
|
# --- Tasks ---
|
|
|
|
@app.route("/api/tasks", methods=["GET"])
|
|
def list_tasks():
|
|
product = request.args.get("product")
|
|
tasks = [t for t in tasks_db.values() if not t.get("is_deleted")]
|
|
if product:
|
|
tasks = [t for t in tasks if t.get("product") == product]
|
|
return jsonify(sorted(tasks, key=lambda t: t.get("created_at", ""), reverse=True))
|
|
|
|
|
|
@app.route("/api/tasks", methods=["POST"])
|
|
def create_task():
|
|
data = request.get_json()
|
|
task_id = str(uuid.uuid4())[:8]
|
|
task = {
|
|
"id": task_id,
|
|
"name": data.get("name", f"Task-{task_id}"),
|
|
"product": data.get("product", "test-agent"),
|
|
"platform": data.get("platform", "dcloud"),
|
|
"status": "pending",
|
|
"created_at": datetime.now().isoformat(),
|
|
"started_at": None,
|
|
"finished_at": None,
|
|
"task_type": data.get("task_type", "run_existing"),
|
|
"module": data.get("module", ""),
|
|
"test_marker": data.get("test_marker", ""),
|
|
"test_file": data.get("test_file", ""),
|
|
"headless": data.get("headless", "false"),
|
|
"repo_id": data.get("repo_id", ""),
|
|
"repo_url": data.get("repo_url", ""),
|
|
"api_key": data.get("api_key", ""),
|
|
"against": data.get("against", "HEAD~1"),
|
|
"test_types": data.get("test_types", ["smoke", "unit", "integration"]),
|
|
"env": data.get("env", "PROD"),
|
|
"scope": data.get("scope", "all"),
|
|
"schedule_type": data.get("schedule_type", "once"),
|
|
"account": data.get("account", ""),
|
|
"password": data.get("password", ""),
|
|
"run_count": data.get("run_count", 1),
|
|
}
|
|
tasks_db[task_id] = task
|
|
_save_db()
|
|
_run_task_process(task)
|
|
return jsonify(task)
|
|
|
|
|
|
@app.route("/api/tasks/<tid>", methods=["DELETE"])
|
|
def delete_task(tid):
|
|
if tid in tasks_db:
|
|
tasks_db[tid]["is_deleted"] = True
|
|
_cleanup_assets(tid)
|
|
_save_db()
|
|
return jsonify({"ok": True})
|
|
|
|
|
|
@app.route("/api/tasks/<tid>/stop", methods=["POST"])
|
|
def stop_task(tid):
|
|
if tid in process_pids:
|
|
try:
|
|
os.killpg(os.getpgid(process_pids[tid]), 9)
|
|
except Exception:
|
|
try:
|
|
os.kill(process_pids[tid], 9)
|
|
except Exception:
|
|
pass
|
|
process_pids.pop(tid, None)
|
|
if tid in tasks_db:
|
|
tasks_db[tid]["status"] = "fail"
|
|
tasks_db[tid]["finished_at"] = datetime.now().isoformat()
|
|
_save_db()
|
|
return jsonify({"ok": True})
|
|
|
|
|
|
@app.route("/api/tasks/<tid>/logs")
|
|
def task_logs(tid):
|
|
q = log_queues.get(tid)
|
|
if not q:
|
|
log_file = REPORTS_DIR / f"{tid}.json"
|
|
if log_file.exists():
|
|
logs = json.loads(log_file.read_text())
|
|
def gen():
|
|
for log in logs:
|
|
yield f"data: {json.dumps(log)}\n\n"
|
|
yield 'data: {"level":"DONE","msg":"","ts":"--:--:--"}\n\n'
|
|
return Response(gen(), mimetype="text/event-stream")
|
|
return Response('data: {"level":"ERROR","msg":"任务日志不存在"}\n\n', mimetype="text/event-stream")
|
|
|
|
def generate():
|
|
while True:
|
|
try:
|
|
msg = q.get(timeout=30)
|
|
yield f"data: {json.dumps(msg)}\n\n"
|
|
if msg.get("level") == "DONE":
|
|
break
|
|
except queue.Empty:
|
|
yield 'data: {"level":"PING","msg":"","ts":"--:--:--"}\n\n'
|
|
|
|
return Response(generate(), mimetype="text/event-stream", headers={
|
|
"Cache-Control": "no-cache",
|
|
"X-Accel-Buffering": "no",
|
|
})
|
|
|
|
|
|
# --- Reports ---
|
|
|
|
@app.route("/api/reports", methods=["GET"])
|
|
def list_reports():
|
|
product = request.args.get("product")
|
|
reports = list(reports_db.values())
|
|
if product:
|
|
reports = [r for r in reports if r.get("product") == product]
|
|
return jsonify(sorted(reports, key=lambda r: r.get("finished_at", ""), reverse=True))
|
|
|
|
|
|
@app.route("/api/reports/<tid>/html", methods=["GET"])
|
|
def get_report_html(tid):
|
|
html_path = REPORTS_DIR / f"{tid}.html"
|
|
if html_path.exists():
|
|
return send_from_directory(str(REPORTS_DIR), f"{tid}.html")
|
|
return jsonify({"error": "HTML report not found"}), 404
|
|
|
|
|
|
@app.route("/api/reports/<tid>", methods=["GET"])
|
|
def get_report(tid):
|
|
report = reports_db.get(tid)
|
|
if not report:
|
|
return jsonify({"error": "报告不存在"}), 404
|
|
|
|
log_file = REPORTS_DIR / f"{tid}.json"
|
|
if log_file.exists():
|
|
raw_logs = json.loads(log_file.read_text())
|
|
report["logs"] = []
|
|
for entry in raw_logs:
|
|
if isinstance(entry, str):
|
|
report["logs"].append({"ts": "", "level": "INFO", "msg": entry})
|
|
else:
|
|
report["logs"].append(entry)
|
|
|
|
result_file = REPORTS_DIR / f"{tid}_results.json"
|
|
if result_file.exists():
|
|
report["results"] = json.loads(result_file.read_text())
|
|
|
|
html_path = REPORTS_DIR / f"{tid}.html"
|
|
if html_path.exists():
|
|
report["html_report"] = f"/api/reports/{tid}/html"
|
|
|
|
screenshot_dir = ARTIFACTS_DIR / "screenshots"
|
|
if screenshot_dir.exists():
|
|
report["screenshots"] = [f.name for f in screenshot_dir.iterdir() if f.name.startswith(tid)]
|
|
|
|
return jsonify(report)
|
|
|
|
|
|
# --- Existing Tests ---
|
|
|
|
def _parse_test_functions(filepath: Path) -> list[dict]:
|
|
import re
|
|
tests = []
|
|
try:
|
|
source = filepath.read_text(encoding="utf-8")
|
|
for match in re.finditer(r'def\s+(test_\w+)\s*\(', source):
|
|
func_name = match.group(1)
|
|
readable = func_name.replace("test_", "").replace("_", " ")
|
|
line_start = source[:match.start()].count("\n")
|
|
doc = ""
|
|
lines = source.split("\n")
|
|
if line_start + 1 < len(lines):
|
|
next_line = lines[line_start + 1].strip()
|
|
if next_line.startswith('"""') or next_line.startswith("'''"):
|
|
doc_match = re.search(r'["\']{3}\s*(.*?)\s*["\']{3}', source[match.end():], re.DOTALL)
|
|
if doc_match:
|
|
doc = doc_match.group(1)
|
|
tests.append({"name": func_name, "readable": readable, "doc": doc})
|
|
except Exception:
|
|
pass
|
|
return tests
|
|
|
|
|
|
@app.route("/api/existing-tests/modules", methods=["GET"])
|
|
def get_existing_modules():
|
|
result = {"dcloud": [], "robogo": []}
|
|
|
|
if EXISTING_TESTS_ROOT.exists():
|
|
e2e_files = []
|
|
for d in sorted(EXISTING_TESTS_ROOT.iterdir()):
|
|
if d.is_dir() and not d.name.startswith("_") and not d.name.startswith("."):
|
|
test_dir = d / "tests"
|
|
test_files = []
|
|
if test_dir.exists():
|
|
for f in sorted(test_dir.iterdir()):
|
|
if f.suffix == ".py" and f.name.startswith("test_"):
|
|
tests = _parse_test_functions(f)
|
|
test_files.append({"name": f.name, "tests": tests, "count": len(tests)})
|
|
if "_e2e" in f.stem:
|
|
e2e_files.append({"name": f"{d.name}/tests/{f.name}", "tests": tests, "count": len(tests)})
|
|
result["dcloud"].append({
|
|
"name": d.name,
|
|
"path": str(d.relative_to(EXISTING_TESTS_ROOT.parent)),
|
|
"test_files": test_files,
|
|
"test_count": sum(tf["count"] for tf in test_files),
|
|
})
|
|
|
|
result["dcloud"].append({
|
|
"name": "e2e",
|
|
"path": "tests/e2e",
|
|
"test_files": e2e_files,
|
|
"test_count": sum(tf["count"] for tf in e2e_files),
|
|
})
|
|
|
|
if ROBOGO_SCRIPTS_ROOT.exists():
|
|
result["robogo"] = [
|
|
{
|
|
"name": "robogo",
|
|
"path": "framework/scripts/run_all.py",
|
|
"label": "Robogo 全量巡检",
|
|
"description": "串行运行全部 13 个场景脚本",
|
|
"test_files": [{"name": "run_all.py", "tests": [], "count": 1}],
|
|
"test_count": 1,
|
|
"section": "product",
|
|
},
|
|
{
|
|
"name": "data_loop",
|
|
"path": "framework/scripts/run_data_loop.py",
|
|
"label": "数据闭环",
|
|
"description": "端到端业务流水线验证",
|
|
"test_files": [{"name": "run_data_loop.py", "tests": [], "count": 1}],
|
|
"test_count": 1,
|
|
"section": "product",
|
|
},
|
|
]
|
|
for f in sorted(ROBOGO_SCRIPTS_ROOT.iterdir()):
|
|
if f.suffix == ".py" and not f.name.startswith("_") and f.name.startswith("_") and not f.name.startswith("__"):
|
|
pass
|
|
elif f.suffix == ".py" and f.name not in ("__init__.py", "run_all.py", "run_data_loop.py"):
|
|
result["robogo"].append({
|
|
"name": f.stem,
|
|
"path": str(f.relative_to(ROBOGO_PROJECT_ROOT)),
|
|
"label": f.stem.replace("_", " ").title(),
|
|
"description": "独立场景脚本",
|
|
"test_files": [{"name": f.name, "tests": [], "count": 1}],
|
|
"test_count": 1,
|
|
"section": "script",
|
|
})
|
|
|
|
return jsonify(result)
|
|
|
|
|
|
@app.route("/api/existing-tests/markers", methods=["GET"])
|
|
def get_existing_markers():
|
|
return jsonify([
|
|
"smoke", "e2e", "api", "ui", "flow", "login",
|
|
"data_space", "dataspace", "data_set", "data_generation",
|
|
"model_workshop", "model_square", "monitor", "cloud_desktop",
|
|
"dev_machine", "device_manage", "mirror_center",
|
|
"material_library", "prompt_library", "lora_finetune", "workbench",
|
|
"regression", "integration", "system",
|
|
])
|
|
|
|
|
|
# --- Dashboard ---
|
|
|
|
@app.route("/api/dashboard/stats", methods=["GET"])
|
|
def dashboard_stats():
|
|
active_tasks = [t for t in tasks_db.values() if not t.get("is_deleted")]
|
|
total_tasks = len(active_tasks)
|
|
pass_count = len([t for t in active_tasks if t["status"] == "pass"])
|
|
fail_count = len([t for t in active_tasks if t["status"] == "fail"])
|
|
running_count = len([t for t in active_tasks if t["status"] == "running"])
|
|
|
|
total_reports = len(reports_db)
|
|
report_pass = len([r for r in reports_db.values() if r.get("result") == "PASS"])
|
|
|
|
return jsonify({
|
|
"tasks": {
|
|
"total": total_tasks,
|
|
"pass": pass_count,
|
|
"fail": fail_count,
|
|
"running": running_count,
|
|
"pass_rate": (pass_count / total_tasks * 100) if total_tasks > 0 else 0,
|
|
},
|
|
"reports": {
|
|
"total": total_reports,
|
|
"pass": report_pass,
|
|
"pass_rate": (report_pass / total_reports * 100) if total_reports > 0 else 0,
|
|
},
|
|
"repos": {
|
|
"total": len(repos_db),
|
|
},
|
|
"existing_tests": {
|
|
"modules": (
|
|
len([d for d in EXISTING_TESTS_ROOT.iterdir() if d.is_dir() and not d.name.startswith("_")]) if EXISTING_TESTS_ROOT.exists() else 0
|
|
) + (
|
|
len([f for f in ROBOGO_SCRIPTS_ROOT.iterdir() if f.suffix == ".py" and not f.name.startswith("_")]) if ROBOGO_SCRIPTS_ROOT.exists() else 0
|
|
),
|
|
},
|
|
})
|
|
|
|
|
|
# --- SPA & Static ---
|
|
|
|
@app.route("/")
|
|
def serve_spa():
|
|
index_path = PROJECT_ROOT / "frontend" / "dist" / "index.html"
|
|
if index_path.exists():
|
|
return send_from_directory(str(PROJECT_ROOT / "frontend" / "dist"), "index.html")
|
|
return jsonify({"status": "ok", "message": "TestAgent Platform API", "frontend": "not built"})
|
|
|
|
|
|
@app.route("/<path:path>")
|
|
def serve_static(path):
|
|
dist_dir = PROJECT_ROOT / "frontend" / "dist"
|
|
static_path = dist_dir / path
|
|
if static_path.exists() and static_path.is_file():
|
|
return send_from_directory(str(dist_dir), path)
|
|
index_path = dist_dir / "index.html"
|
|
if index_path.exists():
|
|
return send_from_directory(str(dist_dir), "index.html")
|
|
return jsonify({"error": "not found"}), 404
|
|
|
|
|
|
@app.route("/artifacts/screenshots/<path:filename>")
|
|
def serve_screenshot(filename):
|
|
return send_from_directory(str(ARTIFACTS_DIR / "screenshots"), filename)
|
|
|
|
|
|
_load_db() |