#!/bin/bash
# 測試 respdoc 功能

set -e

echo "測試 @respdoc 多狀態碼回應功能"
echo "================================"

OUTPUT_DIR="example/test_output"
mkdir -p "$OUTPUT_DIR"

echo ""
echo "測試 1: Swagger 2.0 with @respdoc"
./bin/go-doc -a example/example_respdoc.api -d "$OUTPUT_DIR" -f respdoc_swagger2
CODES=$(jq -r '.paths."/v1/query".get.responses | keys | join(", ")' "$OUTPUT_DIR/respdoc_swagger2.json")
echo "OK: $CODES"

echo ""
echo "測試 2: OpenAPI 3.0 with @respdoc"
./bin/go-doc -a example/example_respdoc.api -d "$OUTPUT_DIR" -f respdoc_openapi3 -s openapi3.0
CODES=$(jq -r '.paths."/v1/query".get.responses | keys | join(", ")' "$OUTPUT_DIR/respdoc_openapi3.json")
echo "OK: $CODES"

echo ""
echo "測試 3: 檢查 400 回應的 oneOf（OpenAPI 3.0）"
HAS_ONEOF=$(jq '.paths."/v1/query".get.responses."400".content."application/json".schema | has("oneOf")' "$OUTPUT_DIR/respdoc_openapi3.json")
if [ "$HAS_ONEOF" = "true" ]; then
    TYPES=$(jq -r '.paths."/v1/query".get.responses."400".content."application/json".schema.oneOf | map(."$ref" | split("/") | last) | join(", ")' "$OUTPUT_DIR/respdoc_openapi3.json")
    echo "OK: oneOf types: $TYPES"
else
    echo "FAIL: oneOf not found"
    exit 1
fi

echo ""
echo "測試 4: 檢查所有錯誤類型的 schema"
SCHEMAS=$(jq -r '.components.schemas | keys | map(select(. | contains("Error"))) | join(", ")' "$OUTPUT_DIR/respdoc_openapi3.json")
echo "OK: $SCHEMAS"

echo ""
echo "測試 5: Swagger 2.0 檢查業務錯誤碼描述"
DESC=$(jq -r '.paths."/v1/query".get.responses."400".description' "$OUTPUT_DIR/respdoc_swagger2.json" | head -3)
echo "400 描述:"
echo "$DESC"

echo ""
echo "所有 @respdoc 測試通過"
echo ""
echo "生成的檔案:"
ls -lh "$OUTPUT_DIR"/respdoc_*
