#!/bin/bash
# 測試所有格式生成

set -e

echo "🧪 測試 go-doc 所有格式生成"
echo "================================"

# 建立測試目錄
TEST_DIR="test_output_verification"
mkdir -p "$TEST_DIR"

echo ""
echo "📝 測試 1: Swagger 2.0 (JSON)"
./bin/go-doc -a example/example.api -d "$TEST_DIR" -f test1_swagger2
if [ -f "$TEST_DIR/test1_swagger2.json" ]; then
    VERSION=$(jq -r '.swagger' "$TEST_DIR/test1_swagger2.json")
    echo "✅ 成功生成 Swagger $VERSION"
else
    echo "❌ 失敗"
    exit 1
fi

echo ""
echo "📝 測試 2: Swagger 2.0 (YAML)"
./bin/go-doc -a example/example.api -d "$TEST_DIR" -f test2_swagger2 -y
if [ -f "$TEST_DIR/test2_swagger2.yaml" ]; then
    echo "✅ 成功生成 YAML 格式"
else
    echo "❌ 失敗"
    exit 1
fi

echo ""
echo "📝 測試 3: OpenAPI 3.0 (JSON)"
./bin/go-doc -a example/example.api -d "$TEST_DIR" -f test3_openapi3 -s openapi3.0
if [ -f "$TEST_DIR/test3_openapi3.json" ]; then
    VERSION=$(jq -r '.openapi' "$TEST_DIR/test3_openapi3.json")
    echo "✅ 成功生成 OpenAPI $VERSION"
else
    echo "❌ 失敗"
    exit 1
fi

echo ""
echo "📝 測試 4: OpenAPI 3.0 (YAML)"
./bin/go-doc -a example/example.api -d "$TEST_DIR" -f test4_openapi3 -s openapi3.0 -y
if [ -f "$TEST_DIR/test4_openapi3.yaml" ]; then
    echo "✅ 成功生成 OpenAPI YAML"
else
    echo "❌ 失敗"
    exit 1
fi

echo ""
echo "📝 測試 5: 中文範例 (Swagger 2.0)"
./bin/go-doc -a example/example_cn.api -d "$TEST_DIR" -f test5_cn_swagger2
if [ -f "$TEST_DIR/test5_cn_swagger2.json" ]; then
    TITLE=$(jq -r '.info.title' "$TEST_DIR/test5_cn_swagger2.json")
    echo "✅ 成功生成中文範例: $TITLE"
else
    echo "❌ 失敗"
    exit 1
fi

echo ""
echo "📝 測試 6: 中文範例 (OpenAPI 3.0)"
./bin/go-doc -a example/example_cn.api -d "$TEST_DIR" -f test6_cn_openapi3 -s openapi3.0
if [ -f "$TEST_DIR/test6_cn_openapi3.json" ]; then
    TITLE=$(jq -r '.info.title' "$TEST_DIR/test6_cn_openapi3.json")
    echo "✅ 成功生成中文 OpenAPI 3.0: $TITLE"
else
    echo "❌ 失敗"
    exit 1
fi

echo ""
echo "📝 測試 7: 錯誤處理（無效的 spec-version）"
if ./bin/go-doc -a example/example.api -d "$TEST_DIR" -s invalid 2>&1 | grep -q "spec-version must be"; then
    echo "✅ 錯誤處理正常"
else
    echo "❌ 錯誤處理失敗"
    exit 1
fi

echo ""
echo "📊 生成檔案統計"
echo "================================"
ls -lh "$TEST_DIR"

echo ""
echo "📈 檔案大小比較"
echo "================================"
echo "Swagger 2.0 vs OpenAPI 3.0:"
S2_SIZE=$(stat -f%z "$TEST_DIR/test1_swagger2.json" 2>/dev/null || stat -c%s "$TEST_DIR/test1_swagger2.json")
O3_SIZE=$(stat -f%z "$TEST_DIR/test3_openapi3.json" 2>/dev/null || stat -c%s "$TEST_DIR/test3_openapi3.json")
echo "  Swagger 2.0:  $S2_SIZE bytes"
echo "  OpenAPI 3.0:  $O3_SIZE bytes"

echo ""
echo "🎉 所有測試通過！"
echo ""
echo "生成的檔案位於: $TEST_DIR/"
