xiangning 6c110b579a refactor(dependencies): 移除未使用的域实体引用
- 从文件模块的多部分上传处理器中移除未使用的实体引用
- 从即时通讯模块的群组成员处理器中移除未使用的实体引用
- 从RAG模块的AI模型配置中移除未使用的实体引用
- 从监控模块的告警规则配置中移除未使用的实体引用
- 从平台模块的角色权限处理器中移除未使用的实体引用
- 从脚本模块的执行日志实体引用中进行清理
- 从多个基础设施配置类中移除未使用的依赖项
- 优化测试文件中的导入声明,移除不必要的引用
2026-07-19 14:09:47 +08:00

95 lines
4.9 KiB
C#

using BuildFlow.Platform.Domain.Entities.Notification;
using BuildFlow.Platform.Application.Notifications.Commands;
using BuildFlow.Platform.Application.Notifications.Handlers;
using BuildFlow.Platform.Application.Notifications.Queries;
using BuildFlow.Platform.Infrastructure.Persistence;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using NSubstitute;
using Xunit;
using INotificationDispatcher = BuildFlow.Platform.Application.Notifications.INotificationDispatcher;
namespace BuildFlow.Platform.Tests.Notifications;
/// <summary>
/// [SOP-PLATFORM-02] 通知系统全 Handler 覆盖测试。
/// 覆盖:Publish(Fanout) / GetNotifications(分页) / GetUnreadCount / MarkRead / MarkAllRead。
/// 飞书矩阵: platform/Notifications 子域(5 Handler 零覆盖→全补)
/// </summary>
public class NotificationHandlerTests
{
private static PlatformDbContext CreateDb([System.Runtime.CompilerServices.CallerMemberName] string name = "")
=> PlatformTestDb.CreateDbContext(name);
private static NotificationEntity NewNotification(Guid recipient, bool read = false, string type = "system")
=> PlatformTestDb.WithAudit(new NotificationEntity
{
RecipientUserId = recipient,
Type = type,
Title = "测试通知",
Content = "内容",
Source = "system",
IsRead = read,
ReadAt = read ? DateTime.UtcNow : null
});
[Fact]
public async Task Publish_WithRecipientUserIds_FanoutAndDispatch()
{
var db = CreateDb();
var dispatcher = Substitute.For<INotificationDispatcher>();
var u1 = Guid.NewGuid();
var u2 = Guid.NewGuid();
var handler = new PublishNotificationCommandHandler(db, dispatcher);
var count = await handler.Handle(new PublishNotificationCommand(
"task-arrived", "标题", "内容", "workflow", null, "workflow-instance",
new[] { u1, u2 }, null), CancellationToken.None);
count.Should().Be(2);
(await db.Notifications.IgnoreQueryFilters().ToListAsync())
.Should().HaveCount(2);
await dispatcher.Received(1).DispatchAsync(
Arg.Is<IReadOnlyList<NotificationEntity>>(l => l.Count == 2),
Arg.Any<CancellationToken>());
}
public async Task Publish_NoRecipients_ReturnsZeroAndNoDispatch()
"system", "t", "c", "system", null, null, null, null), CancellationToken.None);
count.Should().Be(0);
await dispatcher.DidNotReceiveWithAnyArgs().DispatchAsync(default!, default);
public async Task GetNotifications_Paginated_ReturnsUserOnly()
var me = Guid.NewGuid();
var other = Guid.NewGuid();
db.Notifications.AddRange(NewNotification(me), NewNotification(me), NewNotification(other));
await db.SaveChangesAsync();
var handler = new GetNotificationsQueryHandler(db);
var result = await handler.Handle(
new GetNotificationsQuery(me, false, 1, 10), CancellationToken.None);
result.Items.Should().HaveCount(2);
result.Items.Should().OnlyContain(n => n.RecipientUserId == me);
result.Total.Should().Be(2);
public async Task GetNotifications_UnreadOnly_FiltersRead()
db.Notifications.AddRange(NewNotification(me, read: true), NewNotification(me, read: false));
new GetNotificationsQuery(me, true, 1, 10), CancellationToken.None);
result.Items.Should().HaveCount(1);
result.Items[0].IsRead.Should().BeFalse();
public async Task GetUnreadCount_ReturnsOnlyUnread()
db.Notifications.AddRange(
NewNotification(me, read: false),
NewNotification(me, read: true));
var handler = new GetUnreadNotificationCountQueryHandler(db);
var count = await handler.Handle(new GetUnreadNotificationCountQuery(me), CancellationToken.None);
public async Task MarkRead_ExistingUnread_MarksAndReturnsTrue()
var n = NewNotification(me, read: false);
db.Notifications.Add(n);
var handler = new MarkNotificationReadCommandHandler(db);
var ok = await handler.Handle(new MarkNotificationReadCommand(n.Id, me), CancellationToken.None);
ok.Should().BeTrue();
(await db.Notifications.FindAsync(n.Id))!.IsRead.Should().BeTrue();
public async Task MarkRead_NonExistent_ReturnsFalse()
var ok = await handler.Handle(
new MarkNotificationReadCommand(Guid.NewGuid(), Guid.NewGuid()), CancellationToken.None);
ok.Should().BeFalse();
public async Task MarkAllRead_MarksAllForUser()
NewNotification(other, read: false));
var handler = new MarkAllNotificationsReadCommandHandler(db);
var affected = await handler.Handle(new MarkAllNotificationsReadCommand(me), CancellationToken.None);
affected.Should().Be(2);
(await db.Notifications.CountAsync(n => n.RecipientUserId == me && !n.IsRead)).Should().Be(0);
}