|
|
@@ -0,0 +1,241 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
+import com.etotem.cfc.entity.PointsLog;
|
|
|
+import com.etotem.cfc.entity.Task;
|
|
|
+import com.etotem.cfc.entity.TaskExecution;
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
+import com.etotem.cfc.mapper.PointsLogMapper;
|
|
|
+import com.etotem.cfc.mapper.TaskExecutionMapper;
|
|
|
+import com.etotem.cfc.mapper.TaskMapper;
|
|
|
+import com.etotem.cfc.service.CfPathwayEarnService;
|
|
|
+import com.etotem.cfc.service.EnergyService;
|
|
|
+import com.etotem.cfc.service.NotificationDispatchService;
|
|
|
+import com.etotem.cfc.service.StreakService;
|
|
|
+import com.etotem.cfc.service.TaskReminderConfigService;
|
|
|
+import com.etotem.cfc.service.TaskService;
|
|
|
+import com.etotem.cfc.task.RepeatTaskGenerator;
|
|
|
+import com.etotem.cfc.config.DatabaseInitializer;
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+import static org.mockito.ArgumentMatchers.*;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 每小时任务「完成后串行生成下一条」契约测试。
|
|
|
+ *
|
|
|
+ * <p>需求:hourly 任务同时只显示一条;完成(或提交待审核)后,按完成时刻 +1 小时生成下一条,
|
|
|
+ * 下一条 earliest_start = 上条完成 +1h(配合 completeTask 时间窗守卫实现「没到时间不能完成」),
|
|
|
+ * 并透传 action_type(如 exercise)。</p>
|
|
|
+ *
|
|
|
+ * <p><b>RED 预期</b>:现状 completeTask 完成后不生成 hourly 下一条,前 3 条断言失败;
|
|
|
+ * GREEN 阶段在 completeTask 两条完成路径加生成逻辑后转绿。</p>
|
|
|
+ */
|
|
|
+class HourlyTaskChainContractTest {
|
|
|
+
|
|
|
+ private TaskService service;
|
|
|
+ private TaskMapper taskMapper;
|
|
|
+ private FamilyMemberMapper familyMemberMapper;
|
|
|
+ private PointsLogMapper pointsLogMapper;
|
|
|
+ private TaskExecutionMapper taskExecutionMapper;
|
|
|
+ private StreakService streakService;
|
|
|
+
|
|
|
+ private static final long HOUR = 3_600_000L;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() throws Exception {
|
|
|
+ service = new TaskService();
|
|
|
+ taskMapper = mock(TaskMapper.class);
|
|
|
+ familyMemberMapper = mock(FamilyMemberMapper.class);
|
|
|
+ pointsLogMapper = mock(PointsLogMapper.class);
|
|
|
+ taskExecutionMapper = mock(TaskExecutionMapper.class);
|
|
|
+ streakService = mock(StreakService.class);
|
|
|
+
|
|
|
+ inject(service, "taskMapper", taskMapper);
|
|
|
+ inject(service, "familyMemberMapper", familyMemberMapper);
|
|
|
+ inject(service, "pointsLogMapper", pointsLogMapper);
|
|
|
+ inject(service, "taskExecutionMapper", taskExecutionMapper);
|
|
|
+ inject(service, "streakService", streakService);
|
|
|
+ inject(service, "energyService", mock(EnergyService.class));
|
|
|
+ inject(service, "cfPathwayEarnService", mock(CfPathwayEarnService.class));
|
|
|
+ TaskReminderConfigService cfg = mock(TaskReminderConfigService.class);
|
|
|
+ inject(service, "taskReminderConfigService", cfg);
|
|
|
+ inject(service, "notificationDispatchService", mock(NotificationDispatchService.class));
|
|
|
+
|
|
|
+ when(cfg.loadAllConfigs()).thenReturn(new HashMap<>());
|
|
|
+ when(streakService.checkAndAwardMilestone(anyLong(), anyInt())).thenReturn(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("hourly-1:免审核 hourly 实例完成 → 生成下一条,earliest_start=完成+1h,透传action_type")
|
|
|
+ void hourly实例完成_生成下一条() {
|
|
|
+ Date now = new Date();
|
|
|
+ Task template = hourlyTemplate(100L, "exercise");
|
|
|
+ Task instance = hourlyInstance(200L, 100L, "exercise");
|
|
|
+ when(taskMapper.selectById(200L)).thenReturn(instance);
|
|
|
+ when(taskMapper.selectById(100L)).thenReturn(template);
|
|
|
+ when(familyMemberMapper.selectById(21L)).thenReturn(member(21L, 50));
|
|
|
+ when(taskMapper.selectList(any())).thenReturn(java.util.Collections.emptyList()); // 幂等查无重复
|
|
|
+
|
|
|
+ service.completeTask(200L, 21L, null, "眼部放松完成", "text");
|
|
|
+
|
|
|
+ ArgumentCaptor<Task> cap = ArgumentCaptor.forClass(Task.class);
|
|
|
+ verify(taskMapper, atLeastOnce()).insert(cap.capture());
|
|
|
+ Task next = cap.getValue();
|
|
|
+ assertEquals("pending", next.getStatus(), "下一条应待完成");
|
|
|
+ assertEquals(Long.valueOf(100L), next.getTemplateId(), "下一条应指向模板");
|
|
|
+ assertEquals("exercise", next.getActionType(), "下一条应透传 action_type=exercise");
|
|
|
+ assertEquals(Integer.valueOf(0), next.getIsTemplate(), "下一条是实例非模板");
|
|
|
+ assertNotNull(next.getEarliestStart(), "下一条必须有 earliest_start");
|
|
|
+ long diff = next.getEarliestStart().getTime() - now.getTime();
|
|
|
+ assertTrue(diff >= HOUR - 5000 && diff <= HOUR + 5000,
|
|
|
+ "earliest_start 应为完成时刻+1h,实际差 " + diff + "ms");
|
|
|
+ assertNotNull(next.getDeadline(), "下一条必须有 deadline");
|
|
|
+ assertTrue(next.getDeadline().after(next.getEarliestStart()), "deadline 应晚于 earliest_start");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("hourly-2:需审核 hourly 实例提交待审核 → 也生成下一条(决策2:提交即生成)")
|
|
|
+ void hourly实例提交待审核_也生成下一条() {
|
|
|
+ Date now = new Date();
|
|
|
+ Task template = hourlyTemplate(101L, "exercise");
|
|
|
+ Task instance = hourlyInstance(201L, 101L, "exercise");
|
|
|
+ instance.setNeedReview(1);
|
|
|
+ when(taskMapper.selectById(201L)).thenReturn(instance);
|
|
|
+ when(taskMapper.selectById(101L)).thenReturn(template);
|
|
|
+ when(familyMemberMapper.selectById(21L)).thenReturn(member(21L, 50));
|
|
|
+ when(taskMapper.selectList(any())).thenReturn(java.util.Collections.emptyList());
|
|
|
+
|
|
|
+ Map<String, Object> result = service.completeTask(201L, 21L, null, "眼部放松", "text");
|
|
|
+
|
|
|
+ assertEquals(Boolean.TRUE, result.get("needReview"), "应进入待审核");
|
|
|
+ ArgumentCaptor<Task> cap = ArgumentCaptor.forClass(Task.class);
|
|
|
+ verify(taskMapper, atLeastOnce()).insert(cap.capture());
|
|
|
+ Task next = cap.getValue();
|
|
|
+ assertEquals("pending", next.getStatus(), "提交待审核也应生成下一条");
|
|
|
+ assertEquals(Long.valueOf(101L), next.getTemplateId());
|
|
|
+ assertNotNull(next.getEarliestStart());
|
|
|
+ long diff = next.getEarliestStart().getTime() - now.getTime();
|
|
|
+ assertTrue(diff >= HOUR - 5000 && diff <= HOUR + 5000, "earliest_start 应为提交时刻+1h");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("hourly-3:非 hourly 实例完成 → 不生成下一条(不误伤普通/每日任务)")
|
|
|
+ void 非hourly实例完成_不生成下一条() {
|
|
|
+ Task instance = hourlyInstance(202L, null, null); // templateId=null
|
|
|
+ instance.setCategory("阅读任务");
|
|
|
+ when(taskMapper.selectById(202L)).thenReturn(instance);
|
|
|
+ when(familyMemberMapper.selectById(21L)).thenReturn(member(21L, 50));
|
|
|
+
|
|
|
+ service.completeTask(202L, 21L, null, "读书", "text");
|
|
|
+
|
|
|
+ verify(taskMapper, never()).insert(any(Task.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("hourly-4:下一条 earliest_start 前完成 → 400 未到开始时间(时间窗守卫生效)")
|
|
|
+ void 下一条未到时间完成_被守卫拒绝() {
|
|
|
+ Date futureStart = new Date(System.currentTimeMillis() + HOUR);
|
|
|
+ Task instance = hourlyInstance(203L, 100L, "exercise");
|
|
|
+ instance.setEarliestStart(futureStart);
|
|
|
+ when(taskMapper.selectById(203L)).thenReturn(instance);
|
|
|
+
|
|
|
+ IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
|
|
|
+ () -> service.completeTask(203L, 21L, null, null, null));
|
|
|
+
|
|
|
+ assertTrue(ex.getMessage().contains("未到开始时间"), "应提示未到开始时间,实际: " + ex.getMessage());
|
|
|
+ verify(taskMapper, never()).insert(any(Task.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("hourly-5:配置开关关闭(默认)→ 调度器不自动生成 hourly 实例")
|
|
|
+ void 配置开关关闭_调度器不自动生成hourly() throws Exception {
|
|
|
+ // 绕过迁移完成守卫
|
|
|
+ ReflectionTestUtils.setField(DatabaseInitializer.class, "migrationsComplete", true);
|
|
|
+ RepeatTaskGenerator generator = new RepeatTaskGenerator();
|
|
|
+ TaskMapper genMapper = mock(TaskMapper.class);
|
|
|
+ ReflectionTestUtils.setField(generator, "taskMapper", genMapper);
|
|
|
+ ReflectionTestUtils.setField(generator, "hourlyAutoGenerate", false);
|
|
|
+
|
|
|
+ // 调度器查询模板时,hourly 模板应被跳过(开关关闭),不查 hourly 分支
|
|
|
+ when(genMapper.selectList(any())).thenReturn(java.util.Collections.emptyList());
|
|
|
+
|
|
|
+ generator.generateDailyTasks();
|
|
|
+
|
|
|
+ // 开关关闭时不应有任何 insert(hourly 分支整段跳过)
|
|
|
+ verify(genMapper, never()).insert(any(Task.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== helpers ====================
|
|
|
+
|
|
|
+ private Task hourlyTemplate(Long id, String actionType) {
|
|
|
+ Task t = new Task();
|
|
|
+ t.setId(id);
|
|
|
+ t.setFamilyId(72L);
|
|
|
+ t.setFamilyMemberId(21L);
|
|
|
+ t.setCreatorId(12L);
|
|
|
+ t.setExecutorType("child");
|
|
|
+ t.setExecutorId(21L);
|
|
|
+ t.setMemberId(21L);
|
|
|
+ t.setTitle("眼部放松");
|
|
|
+ t.setPoints(2);
|
|
|
+ t.setCategory("运动任务");
|
|
|
+ t.setNeedReview(0);
|
|
|
+ t.setFrequency("hourly");
|
|
|
+ t.setIsTemplate(1);
|
|
|
+ t.setActionType(actionType);
|
|
|
+ t.setDuration(60);
|
|
|
+ t.setDeadline(new Date(System.currentTimeMillis() + 7 * 86400000L));
|
|
|
+ t.setCreatedAt(new Date());
|
|
|
+ return t;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Task hourlyInstance(Long id, Long templateId, String actionType) {
|
|
|
+ Task t = new Task();
|
|
|
+ t.setId(id);
|
|
|
+ t.setFamilyId(72L);
|
|
|
+ t.setFamilyMemberId(21L);
|
|
|
+ t.setCreatorId(12L);
|
|
|
+ t.setExecutorType("child");
|
|
|
+ t.setExecutorId(21L);
|
|
|
+ t.setMemberId(21L);
|
|
|
+ t.setTitle("眼部放松");
|
|
|
+ t.setPoints(2);
|
|
|
+ t.setCategory("运动任务");
|
|
|
+ t.setNeedReview(0);
|
|
|
+ t.setStatus("pending");
|
|
|
+ t.setTemplateId(templateId);
|
|
|
+ t.setIsTemplate(0);
|
|
|
+ t.setActionType(actionType);
|
|
|
+ t.setDuration(60);
|
|
|
+ t.setCreatedAt(new Date());
|
|
|
+ return t;
|
|
|
+ }
|
|
|
+
|
|
|
+ private FamilyMember member(Long id, Integer points) {
|
|
|
+ FamilyMember m = new FamilyMember();
|
|
|
+ m.setId(id);
|
|
|
+ m.setUserId(12L);
|
|
|
+ m.setTotalPoints(points);
|
|
|
+ m.setPenaltyEnabled(0);
|
|
|
+ m.setStreakDays(0);
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void inject(Object target, String field, Object value) throws Exception {
|
|
|
+ Field f = target.getClass().getDeclaredField(field);
|
|
|
+ f.setAccessible(true);
|
|
|
+ f.set(target, value);
|
|
|
+ }
|
|
|
+}
|