feat(通知): 列表按 场景×自然日×主体用户 聚合成两级,红点按组数亮而明细条数同屏;后管日期面板不再被弹窗压住

V158 给 biz_user_notification 加 subject_user_id(事件主体用户,与收件人 user_id 分家):
一行通知是给某个收件人看的,被通知的那个 uid 原先只活在文案字符串里,没有列就没法按用户成组,
也不肯用正则去解历史文案凑分组键。存量不回填(定案)⇒ 老行 subject_user_id 为 NULL,自成「未标注主体」一组。
写入侧 pushOrRefresh 第 8 参带主体,AUDIENCE_USER 时主体=收件人(故新增 7 参便捷档,68 处调用一行没改);
pushToAdminsAnyOf 传 null 并注明理由:一把密钥被 N 个账号共用,不挑一个 uid 冒充。

新增 GET /api/notifications/groups 与 /api/admin/notifications/groups:Java 侧分组(复用 baseWrapper 一份筛选实现),
分页按组切,同屏返回 total=组数 与 rowCount=明细条数;unreadGroupCount 走
COUNT(DISTINCT scene, DATE(created_at), IFNULL(subject_user_id,-1)) —— IFNULL 不能省,
MySQL 的 COUNT(DISTINCT a,b,c) 会跳过任一表达式为 NULL 的行,不兜就把存量那一组整个数没。
红点是新增 unreadGroupCount 而不动 unreadCount 语义:已装机老客户端读 unreadCount 画徽标,
换语义会让老客户端出现「红点 3、列表 217 条」对不上。

铃铛两级(后管+桌面各一份):聚合行点击只展开、不替用户标已读,已读必须进明细逐条点;
明细一次最多 50 条,超出显示「另有 N 条未列出」;下钻带 scene+day+subjectUserId(主体为空改下发
subjectUnlabelled 旗标走 IS NULL),少一维就会把同场景同天别主体的行混进这一组的明细——桌面端最初就漏了这一维。
死码随两级视图退场:groupNotificationsByDay/NotificationDayGroup 删掉,用例换成 formatDayLabel/notificationGroupKey/formatGroupCounts。

后管日期选择面板 z-index 抬到 3300(面板 3200)之上:Element 弹层只自 2000 起递增,不抬就被面板压住,表现为「日期点不动」。
playwright 加 E2E_PORT+strictPort:实测 5174/5176 被别的项目的 vite 占着,reuseExistingServer 让 e2e 连上别人的应用,
当时 5 条全红是端口归属问题而不是回归。

验证=后端通知面 66/66(-Dtest='Notification*' 63 + AdminNotificationControllerTest 3)BUILD SUCCESS;
后管 node --test 1819/1819、真浏览器 e2e 10/10(含遮挡与「展开只取本组」);
桌面 node --test 834/834、组件挂载 vitest 8/8;两前端 vue-tsc --noEmit rc=0;ops 值班侧 18/18。
记账三处=后管 source-pin 逐文件基线 notification-bell.test.ts 用例 6→7(该文件钉子仍 0,占比不动);
桌面 catch 棘轮 blocks 298→299(新增那支是 toggleGroup 明细失败写进面板 loadError,非静默,silent 84 一格没涨);
桌面 endpoints 台账补 /api/notifications/groups。
变异自证 7 条全 KILLED:红点按条数/组内没读完就减组数/聚合行直接标已读/明细截断不报数/下钻丢主体/
丢 IS NULL 旗标/null 主体当值下发。其中「组内没读完就减组数」第一轮 SURVIVED——用例只钉了徽标,
而徽标在 unreadGroupCount 归零时会退回条数兜底,把这条回归盖住了;改成同时钉「N 组未读」chip 才杀掉。

已知盲区(下次补,别当已验):①桌面两级只在 happy-dom 跑过真组件,布局与层级没在真浏览器点过
(桌面日期弹层的层级修复本就在 HEAD 里,不是这批引入);②后管 e2e 夹具没有 subjectUserId=null 的行,
「存量自成一组」在后管 UI 层没被点过,只有后端 WHERE 与桌面组件各一条用例钉着。
This commit is contained in:
2026-10-13 08:34:18 +08:00
parent 1e3bcc9688
commit 5d2a1c6974
31 changed files with 1938 additions and 344 deletions
@@ -11,6 +11,7 @@ import org.mockito.ArgumentCaptor;
import java.time.LocalDate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -35,7 +36,8 @@ class AdminNotificationControllerTest {
controller.summary(request);
controller.page(request, 1L, 20L, false, "余额", NotificationService.CATEGORY_CONFIG_ERROR,
LocalDate.of(2026, 9, 10), LocalDate.of(2026, 9, 13));
LocalDate.of(2026, 9, 10), LocalDate.of(2026, 9, 13),
"task_partial", LocalDate.of(2026, 9, 12), 1160L, false);
verify(notificationService).summary(24L, NotificationService.AUDIENCE_ADMIN);
ArgumentCaptor<NotificationPageQuery> queryCaptor = ArgumentCaptor.forClass(NotificationPageQuery.class);
@@ -48,6 +50,22 @@ class AdminNotificationControllerTest {
assertThat(query.getCategory()).isEqualTo(NotificationService.CATEGORY_CONFIG_ERROR);
assertThat(query.getStartDate()).isEqualTo(LocalDate.of(2026, 9, 10));
assertThat(query.getEndDate()).isEqualTo(LocalDate.of(2026, 9, 13));
// 聚合组展开的三个条件必须原样落到查询对象上,丢了任何一个,展开就会多拿别的组的明细
assertThat(query.getScene()).isEqualTo("task_partial");
assertThat(query.getDay()).isEqualTo(LocalDate.of(2026, 9, 12));
assertThat(query.getSubjectUserId()).isEqualTo(1160L);
assertThat(query.getSubjectUnlabelled()).isFalse();
}
/** 聚合视图与单条列表读的是同一份"谁的副本",这里守 groups 这条新出口别绕过管理员维度。 */
@Test
void groupsUseOperatorIdAndAdminAudience() {
when(adminAuthSupport.requireConsoleOperator(request)).thenReturn(admin(24L));
controller.groups(request, 1L, 20L, false, null, null, null, null);
verify(notificationService).groups(eq(24L), eq(NotificationService.AUDIENCE_ADMIN),
argThat(query -> query.getPage().equals(1L) && query.getPageSize().equals(20L)));
}
@Test
@@ -34,7 +34,8 @@ class NotificationControllerTest {
controller.summary(request);
controller.page(request, 2L, 20L, true, "欠费", NotificationService.CATEGORY_TASK_ERROR,
LocalDate.of(2026, 9, 1), LocalDate.of(2026, 9, 13));
LocalDate.of(2026, 9, 1), LocalDate.of(2026, 9, 13),
"task_failed", LocalDate.of(2026, 9, 5));
verify(notificationService).summary(1095L, NotificationService.AUDIENCE_USER);
ArgumentCaptor<NotificationPageQuery> queryCaptor = ArgumentCaptor.forClass(NotificationPageQuery.class);
@@ -47,6 +48,10 @@ class NotificationControllerTest {
assertThat(query.getCategory()).isEqualTo(NotificationService.CATEGORY_TASK_ERROR);
assertThat(query.getStartDate()).isEqualTo(LocalDate.of(2026, 9, 1));
assertThat(query.getEndDate()).isEqualTo(LocalDate.of(2026, 9, 13));
assertThat(query.getScene()).isEqualTo("task_failed");
assertThat(query.getDay()).isEqualTo(LocalDate.of(2026, 9, 5));
// 桌面端的主体用户就是本人,接口不暴露 subjectUserId,展开只靠 场景+自然日
assertThat(query.getSubjectUserId()).isNull();
}
@Test
@@ -14,6 +14,7 @@ import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
@@ -61,8 +62,7 @@ class NotificationDispatchServiceTest {
when(adminAuthSupport.currentRole(superAdmin)).thenReturn("super_admin");
when(adminAuthSupport.currentRole(admin)).thenReturn("admin");
when(userDataScopeSupport.resolveVisibleUserIds(2L)).thenReturn(List.of(2L));
when(notificationService.pushOrRefresh(anyLong(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString()))
.thenReturn(true);
stubPushOrRefresh();
NotificationDispatchService.AdminAudience audience = service.prepareAdminAudience();
int pushed = service.pushToAdmins(audience, NotificationService.SCENE_TASK_FAILED,
@@ -70,9 +70,10 @@ class NotificationDispatchServiceTest {
// 主管乙不可见用户 20 → 只有超管收到
assertThat(pushed).isEqualTo(1);
// 第 8 个参数是事件主体用户:管理员行必须带上它,聚合才有"按用户折"的键
org.mockito.Mockito.verify(notificationService).pushOrRefresh(eq(1L), eq(NotificationService.AUDIENCE_ADMIN),
eq(NotificationService.SCENE_TASK_FAILED), eq(NotificationService.LEVEL_WARNING),
eq("标题"), eq("内容"), eq("task_failed_admin:20:PRICE_TRACK:20260913:1"));
eq("标题"), eq("内容"), eq("task_failed_admin:20:PRICE_TRACK:20260913:1"), eq(20L));
}
@Test
@@ -86,8 +87,7 @@ class NotificationDispatchServiceTest {
when(adminAuthSupport.currentRole(outOfScope)).thenReturn("admin");
when(userDataScopeSupport.resolveVisibleUserIds(2L)).thenReturn(List.of(2L, 20L));
when(userDataScopeSupport.resolveVisibleUserIds(3L)).thenReturn(List.of(3L, 30L));
when(notificationService.pushOrRefresh(anyLong(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString()))
.thenReturn(true);
stubPushOrRefresh();
int pushed = service.pushToAdmins(NotificationService.SCENE_TASK_FAILED,
NotificationService.LEVEL_WARNING, "标题", "内容",
@@ -97,13 +97,13 @@ class NotificationDispatchServiceTest {
assertThat(pushed).isEqualTo(2);
verify(notificationService).pushOrRefresh(eq(1L), eq(NotificationService.AUDIENCE_ADMIN),
eq(NotificationService.SCENE_TASK_FAILED), eq(NotificationService.LEVEL_WARNING),
eq("标题"), eq("内容"), eq("task_failed_admin:20:PRICE_TRACK:20260913:1"));
eq("标题"), eq("内容"), eq("task_failed_admin:20:PRICE_TRACK:20260913:1"), eq(20L));
verify(notificationService).pushOrRefresh(eq(2L), eq(NotificationService.AUDIENCE_ADMIN),
eq(NotificationService.SCENE_TASK_FAILED), eq(NotificationService.LEVEL_WARNING),
eq("标题"), eq("内容"), eq("task_failed_admin:20:PRICE_TRACK:20260913:2"));
eq("标题"), eq("内容"), eq("task_failed_admin:20:PRICE_TRACK:20260913:2"), eq(20L));
// 非本组主管一条都不落
verify(notificationService, never()).pushOrRefresh(eq(3L), anyString(), anyString(),
anyString(), anyString(), anyString(), anyString());
anyString(), anyString(), anyString(), anyString(), any());
}
/**
@@ -121,8 +121,7 @@ class NotificationDispatchServiceTest {
when(adminAuthSupport.currentRole(outOfScope)).thenReturn("admin");
when(userDataScopeSupport.resolveVisibleUserIds(2L)).thenReturn(List.of(2L, 20L));
when(userDataScopeSupport.resolveVisibleUserIds(3L)).thenReturn(List.of(3L, 30L));
when(notificationService.pushOrRefresh(anyLong(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString()))
.thenReturn(true);
stubPushOrRefresh();
int pushed = service.pushToAdminsAnyOf(service.prepareAdminAudience(),
NotificationService.SCENE_SECRET_BALANCE, NotificationService.LEVEL_ERROR,
@@ -131,12 +130,14 @@ class NotificationDispatchServiceTest {
// 3 个受影响账号 × 2 个可见管理员 = 只落 2 条;非本组主管一条都不落
assertThat(pushed).isEqualTo(2);
verify(notificationService, times(2)).pushOrRefresh(anyLong(), anyString(), anyString(),
anyString(), anyString(), anyString(), anyString());
anyString(), anyString(), anyString(), anyString(), any());
// 合并告警的主体不唯一(一把密钥被 N 个账号共用)⇒ 留 null,聚合时自成一组,
// 不挑一个 uid 冒充"这件事关于谁"
verify(notificationService).pushOrRefresh(eq(2L), eq(NotificationService.AUDIENCE_ADMIN),
eq(NotificationService.SCENE_SECRET_BALANCE), eq(NotificationService.LEVEL_ERROR),
eq("标题"), eq("内容"), eq("secret_balance_admin:group:hash:20261002:2"));
eq("标题"), eq("内容"), eq("secret_balance_admin:group:hash:20261002:2"), isNull());
verify(notificationService, never()).pushOrRefresh(eq(3L), anyString(), anyString(),
anyString(), anyString(), anyString(), anyString());
anyString(), anyString(), anyString(), anyString(), any());
}
@Test
@@ -149,8 +150,7 @@ class NotificationDispatchServiceTest {
when(adminAuthSupport.currentRole(adminA)).thenReturn("admin");
when(adminAuthSupport.currentRole(adminB)).thenReturn("admin");
when(userDataScopeSupport.resolveVisibleUserIds(anyLong())).thenReturn(List.of());
when(notificationService.pushOrRefresh(anyLong(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString()))
.thenReturn(true);
stubPushOrRefresh();
// 服务探测失败这类全局事件(subjectUserId=null)按设计推给所有管理员,不受分组限制
int pushed = service.pushToAdmins(NotificationService.SCENE_SERVICE_DOWN,
@@ -170,8 +170,7 @@ class NotificationDispatchServiceTest {
AdminUserEntity superAdmin = admin(1L, "超管甲");
when(adminUserMapper.selectList(any())).thenReturn(List.of(superAdmin));
when(adminAuthSupport.currentRole(superAdmin)).thenReturn("super_admin");
when(notificationService.pushOrRefresh(anyLong(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString()))
.thenReturn(true);
stubPushOrRefresh();
service.pushPartialResult(20L, 3301L, "BRAND", "BRAND-3301", "12 个品牌查询失败");
@@ -180,13 +179,12 @@ class NotificationDispatchServiceTest {
contains("任务部分结果缺失"), contains("12 个品牌查询失败"), eq("task_partial:3301"));
verify(notificationService).pushOrRefresh(eq(1L), eq(NotificationService.AUDIENCE_ADMIN),
eq(NotificationService.SCENE_TASK_PARTIAL), eq(NotificationService.LEVEL_WARNING),
contains("用户"), anyString(), eq("task_partial_admin:3301:1"));
contains("用户"), anyString(), eq("task_partial_admin:3301:1"), eq(20L));
}
@Test
void pushToUserUsesUserAudience() {
when(notificationService.pushOrRefresh(anyLong(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString()))
.thenReturn(true);
stubPushOrRefresh();
boolean pushed = service.pushToUser(7L, NotificationService.SCENE_SECRET_BALANCE,
NotificationService.LEVEL_ERROR, "标题", "内容", "secret_balance:7:proxy:20260913");
@@ -208,6 +206,18 @@ class NotificationDispatchServiceTest {
assertThat(service.displayNameOf(null)).isEqualTo("未知用户");
}
/**
* 管理员行走 8 参重载(带事件主体用户)、桌面端行走 7 参便捷档(主体就是收件人)。
* 两条都桩:只桩一条的话,"生产码换了一条出口"会表现成 pushed=0 的假失败,
* 看着像分发没跑,其实是桩没接上。
*/
private void stubPushOrRefresh() {
when(notificationService.pushOrRefresh(anyLong(), anyString(), anyString(), anyString(),
anyString(), anyString(), anyString())).thenReturn(true);
when(notificationService.pushOrRefresh(anyLong(), anyString(), anyString(), anyString(),
anyString(), anyString(), anyString(), any())).thenReturn(true);
}
private AdminUserEntity admin(Long id, String username) {
AdminUserEntity user = new AdminUserEntity();
user.setId(id);
@@ -0,0 +1,261 @@
package com.nanri.aiimage.modules.notification.service;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.nanri.aiimage.modules.notification.mapper.UserNotificationMapper;
import com.nanri.aiimage.modules.notification.model.dto.NotificationPageQuery;
import com.nanri.aiimage.modules.notification.model.entity.UserNotificationEntity;
import com.nanri.aiimage.modules.notification.model.vo.NotificationGroupPageVo;
import com.nanri.aiimage.modules.notification.model.vo.NotificationGroupVo;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* 通知聚合视图:把「场景 × 自然日 × 事件主体用户」折成一行。
*
* <p>这组用例钉的是"折叠折在哪、折掉了什么",不是"折得好不好看":
* 分组键的三个维度各自都要能**把一组拆成两组**(少一个维度就会把不同用户或不同天
* 混成一条,那是比刷屏更糟的错——故障被藏起来了);而 total(组数)与 rowCount(明细条数)
* 必须同时给出且不相等,否则界面上的「共 N 条」会被读成"只有 N 条通知"。
*/
class NotificationGroupTest {
@BeforeAll
static void initTableInfo() {
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
UserNotificationEntity.class);
}
private final UserNotificationMapper mapper = mock(UserNotificationMapper.class);
private final NotificationService service = new NotificationService(mapper);
private static UserNotificationEntity row(long id, String scene, Long subject, LocalDate day,
String level, LocalDateTime readAt) {
UserNotificationEntity entity = new UserNotificationEntity();
entity.setId(id);
entity.setScene(scene);
entity.setSubjectUserId(subject);
entity.setLevel(level);
entity.setCreatedAt(day.atTime(9, 30).plusMinutes(id % 50));
entity.setReadAt(readAt);
return entity;
}
private static UserNotificationEntity headline(long id, String title, String content) {
UserNotificationEntity entity = new UserNotificationEntity();
entity.setId(id);
entity.setTitle(title);
entity.setContent(content);
return entity;
}
/** 只喂分组要用的列,明细文案靠 selectBatchIds 回表取——这条桩同时钉住"组头只回表取本页组"。 */
private void stub(List<UserNotificationEntity> rows, List<UserNotificationEntity> heads) {
when(mapper.selectList(any())).thenReturn(rows);
when(mapper.selectBatchIds(any())).thenReturn(heads);
// 行数桩成非 0:page() 在 total==0 时直接短路、根本不调 selectList,
// 那样下面两个"筛选条件进没进 WHERE"的断言会抓不到 wrapper 而假失败。
when(mapper.selectCount(any())).thenReturn(1L);
when(mapper.selectMaps(any())).thenReturn(List.of(Map.of("group_count", 0)));
}
private NotificationGroupPageVo groups() {
return service.groups(24L, NotificationService.AUDIENCE_ADMIN, new NotificationPageQuery());
}
@Test
@SuppressWarnings("unchecked")
void sameSceneDaySubjectFoldsIntoOneGroupWithBothCounts() {
stub(List.of(
row(5, "task_partial", 1160L, LocalDate.of(2026, 10, 13), "warning", null),
row(4, "task_partial", 1160L, LocalDate.of(2026, 10, 13), "warning", null),
row(3, "task_partial", 1160L, LocalDate.of(2026, 10, 13), "warning",
LocalDateTime.of(2026, 10, 13, 10, 0))),
List.of(headline(5, "用户采集数据任务部分结果缺失", "用户 小魏11(uid=1160)任务 A:6 行")));
NotificationGroupPageVo vo = groups();
assertThat(vo.getGroups()).hasSize(1);
NotificationGroupVo group = vo.getGroups().get(0);
assertThat(group.getRowCount()).isEqualTo(3L);
assertThat(group.getUnreadCount()).isEqualTo(2L);
assertThat(group.getLatestId()).isEqualTo(5L);
assertThat(group.getTitle()).isEqualTo("用户采集数据任务部分结果缺失");
// 组数与明细数必须同时给出且不相等:只报一个,界面上就是"少了一堆通知"
assertThat(vo.getTotal()).isEqualTo(1L);
assertThat(vo.getRowCount()).isEqualTo(3L);
}
@Test
void differentSubjectUsersStayInSeparateGroups() {
stub(List.of(
row(9, "task_partial", 1196L, LocalDate.of(2026, 10, 12), "warning", null),
row(8, "task_partial", 1160L, LocalDate.of(2026, 10, 12), "warning", null)),
List.of(headline(9, "t", "c"), headline(8, "t", "c")));
NotificationGroupPageVo vo = groups();
assertThat(vo.getGroups()).hasSize(2);
assertThat(vo.getGroups()).extracting(NotificationGroupVo::getSubjectUserId).containsExactly(1196L, 1160L);
assertThat(vo.getTotal()).isEqualTo(2L);
assertThat(vo.getRowCount()).isEqualTo(2L);
}
@Test
void differentDaysAndScenesEachBreakTheGroup() {
stub(List.of(
row(7, "task_failed", 1160L, LocalDate.of(2026, 10, 12), "warning", null),
row(6, "task_partial", 1160L, LocalDate.of(2026, 10, 12), "warning", null),
row(5, "task_partial", 1160L, LocalDate.of(2026, 10, 11), "warning", null)),
List.of(headline(7, "t", "c"), headline(6, "t", "c"), headline(5, "t", "c")));
NotificationGroupPageVo vo = groups();
assertThat(vo.getGroups()).hasSize(3);
assertThat(vo.getGroups()).extracting(NotificationGroupVo::getDay)
.containsExactly(LocalDate.of(2026, 10, 12), LocalDate.of(2026, 10, 12), LocalDate.of(2026, 10, 11));
assertThat(vo.getGroups()).extracting(NotificationGroupVo::getScene)
.containsExactly("task_failed", "task_partial", "task_partial");
}
/** 存量行没有主体用户列(V158 不回填):它们按"未区分用户"自成一组,而不是按文案里的 uid 拆开。 */
@Test
void unlabelledLegacyRowsFormOneNullSubjectGroup() {
stub(List.of(
row(4, "task_partial", null, LocalDate.of(2026, 10, 9), "warning", null),
row(3, "task_partial", null, LocalDate.of(2026, 10, 9), "warning", null)),
List.of(headline(4, "t", "c")));
NotificationGroupPageVo vo = groups();
assertThat(vo.getGroups()).hasSize(1);
assertThat(vo.getGroups().get(0).getSubjectUserId()).isNull();
assertThat(vo.getGroups().get(0).getRowCount()).isEqualTo(2L);
}
/** 一组里混进 error 不能被后写入的 warning 盖掉——级别是"这组有多严重"的唯一凭据。 */
@Test
void groupLevelTakesTheMostSevereRowNotTheLatest() {
stub(List.of(
row(6, "service_down", null, LocalDate.of(2026, 10, 13), "warning", null),
row(5, "service_down", null, LocalDate.of(2026, 10, 13), "error", null)),
List.of(headline(6, "t", "c")));
assertThat(groups().getGroups().get(0).getLevel()).isEqualTo("error");
}
@Test
void pagingCutsGroupsNotRows() {
stub(List.of(
row(9, "task_partial", 1160L, LocalDate.of(2026, 10, 13), "warning", null),
row(8, "task_partial", 1196L, LocalDate.of(2026, 10, 13), "warning", null),
row(7, "task_partial", 1160L, LocalDate.of(2026, 10, 13), "warning", null)),
List.of(headline(9, "t", "c"), headline(8, "t", "c")));
NotificationPageQuery query = new NotificationPageQuery();
query.setPage(2L);
query.setPageSize(1L);
NotificationGroupPageVo vo = service.groups(24L, NotificationService.AUDIENCE_ADMIN, query);
assertThat(vo.getTotal()).isEqualTo(2L);
assertThat(vo.getRowCount()).isEqualTo(3L);
assertThat(vo.getGroups()).hasSize(1);
assertThat(vo.getGroups().get(0).getSubjectUserId()).isEqualTo(1196L);
}
/**
* 补录/时钟回拨会让 id 与 created_at 倒挂:新的一天可能带着更小的 id。
* 排序必须按天,否则最该看到的一组被排到最后一页——分页之后就彻底看不见了。
*/
@Test
void groupOrderFollowsDayNotId() {
stub(List.of(
row(99, "task_failed", 1160L, LocalDate.of(2026, 10, 11), "warning", null),
row(1, "task_failed", 1160L, LocalDate.of(2026, 10, 13), "warning", null)),
List.of(headline(99, "t", "c"), headline(1, "t", "c")));
assertThat(groups().getGroups()).extracting(NotificationGroupVo::getDay)
.containsExactly(LocalDate.of(2026, 10, 13), LocalDate.of(2026, 10, 11));
}
/** 展开明细的三个条件必须进 WHERE:丢任何一个,展开就会把别的组也倒进来。 */
@Test
@SuppressWarnings("unchecked")
void drillDownFiltersReachTheWhereClause() {
stub(List.of(), List.of());
NotificationPageQuery query = new NotificationPageQuery();
query.setScene("task_partial");
query.setDay(LocalDate.of(2026, 9, 12));
query.setSubjectUserId(1160L);
service.page(24L, NotificationService.AUDIENCE_ADMIN, query);
ArgumentCaptor<LambdaQueryWrapper<UserNotificationEntity>> captor = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
verify(mapper, org.mockito.Mockito.atLeastOnce()).selectList(captor.capture());
LambdaQueryWrapper<UserNotificationEntity> wrapper = captor.getValue();
// MP 3.5.7 要先求值一次 getSqlSegment(),参数才会铺进 paramNameValuePairs
String sql = wrapper.getSqlSegment();
assertThat(sql).contains("scene").contains("subject_user_id").contains("created_at");
assertThat(wrapper.getParamNameValuePairs().values()).contains("task_partial", 1160L);
}
/** 「未区分用户」那一组展开时按 IS NULL 过滤;不这么做它会连带把已标注的行一起倒出来。 */
@Test
@SuppressWarnings("unchecked")
void unlabelledGroupExpandsWithIsNull() {
stub(List.of(), List.of());
NotificationPageQuery query = new NotificationPageQuery();
query.setScene("task_partial");
query.setSubjectUnlabelled(true);
service.page(24L, NotificationService.AUDIENCE_ADMIN, query);
ArgumentCaptor<LambdaQueryWrapper<UserNotificationEntity>> captor = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
verify(mapper, org.mockito.Mockito.atLeastOnce()).selectList(captor.capture());
assertThat(captor.getValue().getSqlSegment()).contains("subject_user_id IS NULL");
}
/**
* 未读组数走 COUNT(DISTINCT 场景, 自然日, IFNULL(主体用户,-1))。
*
* <p>这条只证"IFNULL 写了",证不了 MySQL 那半边语义——
* {@code COUNT(DISTINCT a,b,c)} 会**跳过任一表达式为 NULL 的行**,少了 IFNULL
* 存量未标注的行整个不计、未读组数少报。那半边要靠上线后一条现网只读 SQL 复量
* (见 V158 第 5 节的验证语句)。
*/
@Test
void unreadGroupCountGuardsNullSubjectWithIfnull() {
when(mapper.selectMaps(any())).thenReturn(List.of(Map.of("group_count", 7)));
assertThat(service.unreadGroupCount(24L, NotificationService.AUDIENCE_ADMIN)).isEqualTo(7L);
ArgumentCaptor<com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<UserNotificationEntity>> captor =
ArgumentCaptor.forClass(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper.class);
verify(mapper).selectMaps(captor.capture());
assertThat(captor.getValue().getSqlSelect())
.contains("COUNT(DISTINCT scene, DATE(created_at), IFNULL(subject_user_id, -1))");
}
/** 读不到值 ≠ 0:桩成空结果时必须能区分(否则"没数"会被报成"没有未读")。 */
@Test
void unreadGroupCountDistinguishesNoReadingFromZero() {
when(mapper.selectMaps(any())).thenReturn(List.of());
assertThat(service.unreadGroupCount(24L, NotificationService.AUDIENCE_ADMIN)).isZero();
when(mapper.selectMaps(any())).thenReturn(List.of(Map.of("group_count", 0)));
assertThat(service.unreadGroupCount(24L, NotificationService.AUDIENCE_ADMIN)).isZero();
}
}