@echo off
REM ====================================
REM 项目创建功能测试脚本 (Windows版本)
REM 功能：测试创建项目时集成流水分析平台
REM 作者：Claude Code
REM 日期：2026-03-04
REM ====================================

setlocal enabledelayedexpansion

REM 配置
set BASE_URL=http://localhost:8080
set USERNAME=admin
set PASSWORD=admin123
set TOKEN=

REM 颜色设置（Windows 10+）
set "GREEN=[92m"
set "RED=[91m"
set "YELLOW=[93m"
set "NC=[0m"

REM 计数器
set PASS_COUNT=0
set FAIL_COUNT=0

REM 日志函数
goto :main

:log_info
echo %GREEN%[INFO]%NC% %~1
goto :eof

:log_error
echo %RED%[ERROR]%NC% %~1
goto :eof

:log_warning
echo %YELLOW%[WARNING]%NC% %~1
goto :eof

REM 检查curl是否存在
:check_curl
where curl >nul 2>&1
if %ERRORLEVEL% neq 0 (
    call :log_error "curl 未安装，请先安装 curl 或使用 Git Bash 运行 test-project-creation.sh"
    exit /b 1
)
exit /b 0

REM 检查后端服务
:check_backend_service
call :log_info "检查后端服务状态..."
curl -s --connect-timeout 5 "%BASE_URL%/actuator/health" >nul 2>&1
if %ERRORLEVEL% equ 0 (
    call :log_info "✓ 后端服务运行正常"
    exit /b 0
) else (
    call :log_error "✗ 后端服务未运行，请先启动后端服务"
    call :log_info "启动命令: cd ruoyi-admin && mvn spring-boot:run"
    exit /b 1
)

REM 获取访问令牌
:get_token
call :log_info "获取访问令牌..."
for /f "delims=" %%i in ('curl -s -X POST "%BASE_URL%/login/test?username=%USERNAME%&password=%PASSWORD%"') do set TOKEN_RESPONSE=%%i

REM 提取token（简单解析）
for /f "tokens=2 delims=:," %%a in ('echo %TOKEN_RESPONSE% ^| findstr /r "token"') do (
    set TOKEN=%%a
    set TOKEN=!TOKEN:"=!
    goto :token_extracted
)

:token_extracted
if "%TOKEN%"=="" (
    call :log_error "获取令牌失败：无法从响应中提取 token"
    call :log_info "响应内容: %TOKEN_RESPONSE%"
    exit /b 1
)

call :log_info "✓ 成功获取令牌"
exit /b 0

REM 测试场景1：创建项目成功
:test_create_project_success
call :log_info "=========================================="
call :log_info "测试场景1：创建项目成功"
call :log_info "=========================================="

REM 生成时间戳
for /f "tokens=1-6 delims=/:. " %%a in ("%date% %time%") do (
    set TIMESTAMP=%%a%%b%%c_%%d%%e%%f
)
set PROJECT_NAME=集成测试项目_%TIMESTAMP%

REM 准备JSON数据（需要转义）
set REQUEST_DATA={"projectName":"%PROJECT_NAME%","description":"测试集成流水分析平台","configType":"default"}

call :log_info "请求数据: %REQUEST_DATA%"

REM 发送请求并保存响应到文件
curl -s -X POST "%BASE_URL%/ccdi/project" ^
    -H "Content-Type: application/json" ^
    -H "Authorization: Bearer %TOKEN%" ^
    -d "%REQUEST_DATA%" > response.json

type response.json
echo.

REM 检查是否成功
findstr /c:"code":200 response.json >nul
if %ERRORLEVEL% equ 0 (
    call :log_info "✓ 项目创建成功"

    REM 检查lsfxProjectId
    findstr /c:"lsfxProjectId" response.json >nul
    if %ERRORLEVEL% equ 0 (
        call :log_info "✓ 流水分析平台项目ID存在"
        set /a PASS_COUNT+=1
    ) else (
        call :log_error "✗ 流水分析平台项目ID为空"
        set /a FAIL_COUNT+=1
    )
) else (
    call :log_error "✗ 项目创建失败"
    set /a FAIL_COUNT+=1
)

del response.json
exit /b 0

REM 测试场景2：创建项目失败（项目名称为空）
:test_create_project_empty_name
call :log_info "=========================================="
call :log_info "测试场景2：创建项目失败（项目名称为空）"
call :log_info "=========================================="

set REQUEST_DATA={"projectName":"","description":"测试异常场景","configType":"default"}

call :log_info "请求数据: %REQUEST_DATA%"

curl -s -X POST "%BASE_URL%/ccdi/project" ^
    -H "Content-Type: application/json" ^
    -H "Authorization: Bearer %TOKEN%" ^
    -d "%REQUEST_DATA%" > response.json

REM 检查是否失败
findstr /c:"code":200 response.json >nul
if %ERRORLEVEL% neq 0 (
    call :log_info "✓ 正确拒绝了空项目名称"
    set /a PASS_COUNT+=1
) else (
    call :log_error "✗ 未正确验证项目名称"
    set /a FAIL_COUNT+=1
)

del response.json
exit /b 0

REM 测试场景3：查询项目列表
:test_query_project_list
call :log_info "=========================================="
call :log_info "测试场景3：查询项目列表"
call :log_info "=========================================="

curl -s -X GET "%BASE_URL%/ccdi/project/list?pageNum=1&pageSize=10" ^
    -H "Authorization: Bearer %TOKEN%" > response.json

REM 检查是否成功
findstr /c:"code":200 response.json >nul
if %ERRORLEVEL% equ 0 (
    call :log_info "✓ 查询项目列表成功"

    REM 检查lsfxProjectId
    findstr /c:"lsfxProjectId" response.json >nul
    if %ERRORLEVEL% equ 0 (
        call :log_info "✓ 项目列表包含 lsfxProjectId 字段"
    ) else (
        call :log_warning "! 项目列表可能缺少 lsfxProjectId 字段"
    )
    set /a PASS_COUNT+=1
) else (
    call :log_error "✗ 查询项目列表失败"
    set /a FAIL_COUNT+=1
)

del response.json
exit /b 0

REM 主函数
:main
call :log_info "=========================================="
call :log_info "开始执行项目创建功能测试"
call :log_info "=========================================="

REM 检查curl
call :check_curl || exit /b 1

REM 检查后端服务
call :check_backend_service || exit /b 1

REM 获取令牌
call :get_token || exit /b 1

REM 执行测试
call :test_create_project_success
call :test_create_project_empty_name
call :test_query_project_list

REM 输出测试结果
call :log_info "=========================================="
call :log_info "测试结果汇总"
call :log_info "=========================================="
call :log_info "通过: %PASS_COUNT%"
call :log_error "失败: %FAIL_COUNT%"
call :log_info "总计: %PASS_COUNT%"

if %FAIL_COUNT% equ 0 (
    call :log_info "✓ 所有测试通过！"
    exit /b 0
) else (
    call :log_error "✗ 存在失败的测试"
    exit /b 1
)
