- 将通用 Entities 命名空间拆分为具体业务实体子目录 - 更新所有实体类型引用为具体的 Entity 后缀类型 - 将 ModelType、ModelProvider、GroupMemberRole 等枚举重命名为 Enum 后缀 - 重构 AiModel 相关配置和映射逻辑适配新的实体结构 - 更新测试代码中的实体类型引用和枚举使用 - 调整应用层处理器中的实体创建和查询逻辑 - 修改 appsettings.json 中的基础设施主机配置为动态占位符模式
66 lines
2.5 KiB
C#
66 lines
2.5 KiB
C#
using IM.Application.Chat.Handlers;
|
|
using IM.Application.Chat.Queries;
|
|
using IM.Domain.Common;
|
|
using IM.Domain.Enums;
|
|
using IM.Infrastructure.Persistence;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Moq;
|
|
using Xunit;
|
|
|
|
namespace BuildFlow.IM.Tests.Chat;
|
|
|
|
/// <summary>
|
|
/// GetConversationsQueryHandler 的会话列表断言:未读数展示必须优先使用 Redis 热数据。
|
|
/// </summary>
|
|
public class GetConversationsQueryHandlerTests
|
|
{
|
|
private static readonly Guid UserId = Guid.Parse("22222222-2222-2222-2222-222222222222");
|
|
private static readonly Guid OtherUserId = Guid.Parse("33333333-3333-3333-3333-333333333333");
|
|
private static readonly Guid ConversationId = Guid.Parse("11111111-1111-1111-1111-111111111111");
|
|
|
|
[Fact]
|
|
public async Task Uses_redis_unread_in_conversation_list_when_pg_cold_backup_is_zero()
|
|
{
|
|
await using var db = BuildInMemoryDb();
|
|
db.Conversations.Add(ImTestData.BuildConversation(
|
|
ConversationId,
|
|
ConversationTypeEnum.Private,
|
|
(UserId, 0),
|
|
(OtherUserId, 0)));
|
|
await db.SaveChangesAsync();
|
|
|
|
var userInfoService = new Mock<IUserInfoService>();
|
|
userInfoService
|
|
.Setup(s => s.GetUsersAsync(
|
|
It.Is<IEnumerable<Guid>>(ids => ids.SequenceEqual(new[] { OtherUserId })),
|
|
It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(new Dictionary<string, (string Username, string? Avatar)>
|
|
{
|
|
[OtherUserId.ToString()] = ("Bob", null)
|
|
});
|
|
|
|
var unreadCounter = new Mock<IUnreadCounter>();
|
|
unreadCounter
|
|
.Setup(c => c.GetByUserAsync(
|
|
UserId,
|
|
It.Is<IEnumerable<Guid>>(ids => ids.SequenceEqual(new[] { ConversationId })),
|
|
It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(new Dictionary<Guid, int> { [ConversationId] = 2 });
|
|
|
|
var handler = new GetConversationsQueryHandler(db, userInfoService.Object, unreadCounter.Object);
|
|
|
|
var result = await handler.Handle(new GetConversationsQuery(UserId), CancellationToken.None);
|
|
|
|
var conversation = Assert.Single(result);
|
|
Assert.Equal(2, conversation.UnreadCount);
|
|
}
|
|
|
|
private static ImDbContext BuildInMemoryDb()
|
|
{
|
|
var services = new ServiceCollection();
|
|
services.AddDbContext<ImDbContext>(o => o.UseInMemoryDatabase($"im-conversation-{Guid.NewGuid()}"));
|
|
return services.BuildServiceProvider().GetRequiredService<ImDbContext>();
|
|
}
|
|
}
|