#!/bin/bash
# 知识库 API 测试脚本

API_BASE="http://localhost:8001"
TOKEN="a21c99620a8ef61d69563afe05ccce89"
DATASET_ID="3c3c671205c911f1a37efedd444ada7f"

echo "=========================================="
echo "知识库 API 测试"
echo "=========================================="

# 1. 获取数据集列表
echo ""
echo "1. 获取数据集列表"
echo "GET /api/v1/knowledge-base/datasets"
curl --silent --request GET \
  "$API_BASE/api/v1/knowledge-base/datasets" \
  --header "authorization: Bearer $TOKEN" \
  --header 'content-type: application/json' | python3 -m json.tool
echo ""

# 2. 获取数据集详情
echo "2. 获取数据集详情"
echo "GET /api/v1/knowledge-base/datasets/{dataset_id}"
curl --silent --request GET \
  "$API_BASE/api/v1/knowledge-base/datasets/$DATASET_ID" \
  --header "authorization: Bearer $TOKEN" | python3 -m json.tool
echo ""

# 3. 创建数据集
echo "3. 创建数据集"
echo "POST /api/v1/knowledge-base/datasets"
curl --silent --request POST \
  "$API_BASE/api/v1/knowledge-base/datasets" \
  --header "authorization: Bearer $TOKEN" \
  --header 'content-type: application/json' \
  --data '{
    "name": "API测试知识库",
    "description": "通过API创建的测试知识库",
    "chunk_method": "naive"
  }' | python3 -m json.tool
echo ""

# 4. 获取文件列表
echo "4. 获取文件列表"
echo "GET /api/v1/knowledge-base/datasets/{dataset_id}/files"
curl --silent --request GET \
  "$API_BASE/api/v1/knowledge-base/datasets/$DATASET_ID/files" \
  --header "authorization: Bearer $TOKEN" | python3 -m json.tool
echo ""

# 5. 上传文件
echo "5. 上传文件"
echo "POST /api/v1/knowledge-base/datasets/{dataset_id}/files"
echo "测试文档内容，用于文件上传测试。" > /tmp/test_doc.txt
curl --silent --request POST \
  "$API_BASE/api/v1/knowledge-base/datasets/$DATASET_ID/files" \
  --header "authorization: Bearer $TOKEN" \
  -F "file=@/tmp/test_doc.txt" | python3 -m json.tool
echo ""

# 6. 获取切片列表
echo "6. 获取切片列表"
echo "GET /api/v1/knowledge-base/datasets/{dataset_id}/chunks"
curl --silent --request GET \
  "$API_BASE/api/v1/knowledge-base/datasets/$DATASET_ID/chunks" \
  --header "authorization: Bearer $TOKEN" | python3 -m json.tool
echo ""

# 7. 更新数据集
echo "7. 更新数据集"
echo "PATCH /api/v1/knowledge-base/datasets/{dataset_id}"
curl --silent --request PATCH \
  "$API_BASE/api/v1/knowledge-base/datasets/$DATASET_ID" \
  --header "authorization: Bearer $TOKEN" \
  --header 'content-type: application/json' \
  --data '{
    "name": "更新后的知识库名称",
    "description": "更新后的描述"
  }' | python3 -m json.tool
echo ""

echo "=========================================="
echo "测试完成"
echo "=========================================="
