# 测试知识库API脚本

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  测试知识库API" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# 测试1: 创建知识库
Write-Host "测试1: 创建知识库..." -ForegroundColor Yellow
$createBody = @{
    name = "Test Knowledge Base"
    description = "This is a test knowledge base"
} | ConvertTo-Json

try {
    $result1 = Invoke-RestMethod -Uri "http://localhost:3001/api/v1/knowledge-bases" `
        -Method Post `
        -Body $createBody `
        -ContentType "application/json"
    
    Write-Host "✓ 创建成功" -ForegroundColor Green
    Write-Host "  知识库ID: $($result1.data.id)" -ForegroundColor Gray
    Write-Host "  名称: $($result1.data.name)" -ForegroundColor Gray
    $kbId = $result1.data.id
} catch {
    Write-Host "✗ 创建失败: $_" -ForegroundColor Red
    exit 1
}

Write-Host ""

# 测试2: 获取知识库列表
Write-Host "测试2: 获取知识库列表..." -ForegroundColor Yellow
try {
    $result2 = Invoke-RestMethod -Uri "http://localhost:3001/api/v1/knowledge-bases" `
        -Method Get
    
    Write-Host "✓ 获取成功" -ForegroundColor Green
    Write-Host "  知识库数量: $($result2.data.Count)" -ForegroundColor Gray
} catch {
    Write-Host "✗ 获取失败: $_" -ForegroundColor Red
}

Write-Host ""

# 测试3: 获取知识库详情
Write-Host "测试3: 获取知识库详情..." -ForegroundColor Yellow
try {
    $result3 = Invoke-RestMethod -Uri "http://localhost:3001/api/v1/knowledge-bases/$kbId" `
        -Method Get
    
    Write-Host "✓ 获取成功" -ForegroundColor Green
    Write-Host "  名称: $($result3.data.name)" -ForegroundColor Gray
    Write-Host "  文档数: $($result3.data._count.documents)" -ForegroundColor Gray
} catch {
    Write-Host "✗ 获取失败: $_" -ForegroundColor Red
}

Write-Host ""

# 测试4: 更新知识库
Write-Host "测试4: 更新知识库..." -ForegroundColor Yellow
$updateBody = @{
    name = "Updated Test KB"
    description = "Updated description"
} | ConvertTo-Json

try {
    $result4 = Invoke-RestMethod -Uri "http://localhost:3001/api/v1/knowledge-bases/$kbId" `
        -Method Put `
        -Body $updateBody `
        -ContentType "application/json"
    
    Write-Host "✓ 更新成功" -ForegroundColor Green
    Write-Host "  新名称: $($result4.data.name)" -ForegroundColor Gray
} catch {
    Write-Host "✗ 更新失败: $_" -ForegroundColor Red
}

Write-Host ""

# 测试5: 检索知识库
Write-Host "测试5: 检索知识库..." -ForegroundColor Yellow
try {
    $result5 = Invoke-RestMethod -Uri "http://localhost:3001/api/v1/knowledge-bases/$kbId/search?query=test" `
        -Method Get
    
    Write-Host "✓ 检索成功" -ForegroundColor Green
    Write-Host "  结果数: $($result5.data.records.Count)" -ForegroundColor Gray
} catch {
    Write-Host "✗ 检索失败: $_" -ForegroundColor Red
}

Write-Host ""

# 测试6: 删除知识库
Write-Host "测试6: 删除知识库..." -ForegroundColor Yellow
try {
    $result6 = Invoke-RestMethod -Uri "http://localhost:3001/api/v1/knowledge-bases/$kbId" `
        -Method Delete
    
    Write-Host "✓ 删除成功" -ForegroundColor Green
} catch {
    Write-Host "✗ 删除失败: $_" -ForegroundColor Red
}

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  测试完成!" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan


