139 lines
4.1 KiB
C#
139 lines
4.1 KiB
C#
using DramaLing.Api.Tests.Integration.Fixtures;
|
|
using System.Net;
|
|
using System.Net.Http.Json;
|
|
|
|
namespace DramaLing.Api.Tests.Integration.Controllers;
|
|
|
|
/// <summary>
|
|
/// AIController 整合測試
|
|
/// 測試 AI 分析相關的 API 端點功能
|
|
/// </summary>
|
|
public class AIControllerTests : IntegrationTestBase
|
|
{
|
|
public AIControllerTests(DramaLingWebApplicationFactory factory) : base(factory)
|
|
{
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnalyzeSentence_WithValidAuth_ShouldReturnAnalysis()
|
|
{
|
|
// Arrange
|
|
var client = CreateTestUser1Client();
|
|
var analysisData = new
|
|
{
|
|
text = "Hello, this is a beautiful day for learning English.",
|
|
targetLevel = "A2",
|
|
includeGrammar = true,
|
|
includeVocabulary = true
|
|
};
|
|
|
|
// Act
|
|
var response = await client.PostAsJsonAsync("/api/ai/analyze-sentence", analysisData);
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
content.Should().Contain("success");
|
|
content.Should().NotBeNullOrEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnalyzeSentence_WithoutAuth_ShouldReturn401()
|
|
{
|
|
// Arrange
|
|
var analysisData = new
|
|
{
|
|
text = "Hello, this is a test sentence.",
|
|
targetLevel = "A2"
|
|
};
|
|
|
|
// Act
|
|
var response = await HttpClient.PostAsJsonAsync("/api/ai/analyze-sentence", analysisData);
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnalyzeSentence_WithEmptyText_ShouldReturn400()
|
|
{
|
|
// Arrange
|
|
var client = CreateTestUser1Client();
|
|
var analysisData = new
|
|
{
|
|
text = "", // 空文本
|
|
targetLevel = "A2"
|
|
};
|
|
|
|
// Act
|
|
var response = await client.PostAsJsonAsync("/api/ai/analyze-sentence", analysisData);
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetHealth_ShouldReturnHealthStatus()
|
|
{
|
|
// Arrange
|
|
var client = CreateTestUser1Client();
|
|
|
|
// Act
|
|
var response = await client.GetAsync("/api/ai/health");
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
content.Should().Contain("success");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetStats_WithValidAuth_ShouldReturnStats()
|
|
{
|
|
// Arrange
|
|
var client = CreateTestUser1Client();
|
|
|
|
// Act
|
|
var response = await client.GetAsync("/api/ai/stats");
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
content.Should().Contain("success");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetStats_WithoutAuth_ShouldReturn401()
|
|
{
|
|
// Arrange & Act
|
|
var response = await HttpClient.GetAsync("/api/ai/stats");
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task MockGeminiService_ShouldWorkCorrectly()
|
|
{
|
|
// Arrange
|
|
var client = CreateTestUser1Client();
|
|
var testSentence = new
|
|
{
|
|
text = "The sophisticated algorithm analyzed the beautiful sentence.",
|
|
targetLevel = "B2",
|
|
includeGrammar = true,
|
|
includeVocabulary = true
|
|
};
|
|
|
|
// Act
|
|
var response = await client.PostAsJsonAsync("/api/ai/analyze-sentence", testSentence);
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
|
|
// 驗證 Mock 服務返回預期的回應格式
|
|
content.Should().Contain("success");
|
|
// Mock 服務應該能夠處理這個請求而不需要真實的 Gemini API
|
|
}
|
|
} |