using BuildFlow.Application.AiModel.Commands;
using BuildFlow.Application.AiModel.Handlers;
using BuildFlow.Domain.Entities.AiModel;
using BuildFlow.Domain.Enums;
using BuildFlow.Infrastructure.Persistence;
using BuildFlow.Shared.Domain.Errors;
using BuildFlow.Shared.Domain.Exceptions;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Xunit;
namespace BuildFlow.Rag.Tests;
///
/// 删除 AI 模型处理器测试(软删除/硬移除)。成功路径 + 不存在异常分支。
///
public class DeleteAiModelCommandHandlerTests
{
[Fact]
public async Task Handle_ExistingModel_RemovesModel()
{
await using var db = CreateDb();
var modelId = Guid.NewGuid();
db.AiModels.Add(new AiModelEntity
{
Id = modelId,
Name = "Qwen",
Provider = ModelProviderEnum.LMStudio,
ModelId = "qwen2.5",
BaseUrl = "http://localhost:1234/v1",
ApiKey = "lm-studio",
ModelType = ModelTypeEnum.Chat,
CreatedBy = "test",
UpdatedBy = "test",
OperatorIP = "127.0.0.1",
});
await db.SaveChangesAsync();
var handler = new DeleteAiModelCommandHandler(db);
await handler.Handle(new DeleteAiModelCommand(modelId), CancellationToken.None);
(await db.AiModels.CountAsync()).Should().Be(0);
}
[Fact]
public async Task Handle_NotFound_ThrowsNotFound()
{
await using var db = CreateDb();
var handler = new DeleteAiModelCommandHandler(db);
var act = async () => await handler.Handle(new DeleteAiModelCommand(Guid.NewGuid()), CancellationToken.None);
var ex = await act.Should().ThrowAsync();
ex.Which.Code.Should().Be(ErrorCode.NotFound);
}
private static RagDbContext CreateDb()
{
var options = new DbContextOptionsBuilder()
.UseInMemoryDatabase($"rag-delete-aimodel-{Guid.NewGuid():N}")
.Options;
var db = new RagDbContext(options);
db.SavingChanges += (_, _) => RagTestDb.FillAudit(db);
return db;
}
}