fix: 统一学习会话结算并修正结束幂等
This commit is contained in:
+101
-3
@@ -3,6 +3,7 @@ package com.guo.learningprogresstracker.service.impl;
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.guo.learningprogresstracker.entity.StudyReportsEntity;
|
||||
import com.guo.learningprogresstracker.entity.StudySessionsEntity;
|
||||
import com.guo.learningprogresstracker.entity.TaskEntity;
|
||||
import com.guo.learningprogresstracker.enums.StudySessionStateEnum;
|
||||
import com.guo.learningprogresstracker.exception.ErrorParameterException;
|
||||
import com.guo.learningprogresstracker.mapper.StudyReportFragmentsMapper;
|
||||
@@ -25,6 +26,7 @@ import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@@ -47,10 +49,18 @@ class StudySessionsServiceImplTest {
|
||||
@Mock
|
||||
private StudyExpectationsService studyExpectationsService;
|
||||
|
||||
@Mock
|
||||
private TasksServiceImpl tasksServiceImpl;
|
||||
|
||||
@Mock
|
||||
private AiServiceClient aiServiceClient;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
// stub updateById,避免调用真实的 getBaseMapper()
|
||||
doReturn(true).when(studySessionsService).updateById(any());
|
||||
// stub 条件更新(结束会话抢占),默认视为抢占成功
|
||||
doReturn(true).when(studySessionsService).update(any(StudySessionsEntity.class), any(Wrapper.class));
|
||||
// stub removeById,避免调用真实的 getBaseMapper()
|
||||
doReturn(true).when(studySessionsService).removeById(any(Serializable.class));
|
||||
}
|
||||
@@ -207,16 +217,17 @@ class StudySessionsServiceImplTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* 场景10:确认 updateById 被调用(会话状态持久化)
|
||||
* 场景10:确认会话状态被持久化(条件更新:仅当仍未结束时写入)
|
||||
*/
|
||||
@Test
|
||||
void endedStudySession_shouldCallUpdateById() throws Exception {
|
||||
void endedStudySession_shouldCallUpdateWithNotEndedCondition() throws Exception {
|
||||
StudySessionsEntity session = createSession(StudySessionStateEnum.ONGOING.name(), 5);
|
||||
session.setId(10);
|
||||
mockGetOneOpt(session);
|
||||
|
||||
studySessionsService.endedStudySession("SESSION_TEST", "内容");
|
||||
|
||||
verify(studySessionsService).updateById(session);
|
||||
verify(studySessionsService).update(eq(session), any(Wrapper.class));
|
||||
}
|
||||
|
||||
// ============ 误操作结束(零数据关闭) ============
|
||||
@@ -313,4 +324,91 @@ class StudySessionsServiceImplTest {
|
||||
studySessionsService.abortStudySession("NOT_EXIST", StudySessionsServiceImpl.MISOPERATION_CONFIRMATION));
|
||||
}
|
||||
|
||||
// ============ 会话-01:结算路径统一 ============
|
||||
|
||||
/**
|
||||
* 场景17:ONGOING 且上一段开始于 3 小时前 → 结束时应截断为 25 分钟
|
||||
*/
|
||||
@Test
|
||||
void endedStudySession_ongoingThreeHoursGap_shouldTruncateToOnePomodoro() throws Exception {
|
||||
StudySessionsEntity session = createSession(StudySessionStateEnum.ONGOING.name(), 180);
|
||||
session.setId(20);
|
||||
mockGetOneOpt(session);
|
||||
|
||||
String message = studySessionsService.endedStudySession("SESSION_TEST", "内容");
|
||||
|
||||
assertNull(message, "截断后 25 分钟 ≥ 10 分钟,不应提示时间不足");
|
||||
assertEquals(25 * 60, session.getEffectiveTime(), "3 小时无人操作只应计入 25 分钟");
|
||||
verify(studyReportsMapper).insert(any(StudyReportsEntity.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 场景18:ONGOING 且上一段开始于 3 小时前 → 暂停时同样应截断为 25 分钟
|
||||
*/
|
||||
@Test
|
||||
void pauseStudySession_ongoingThreeHoursGap_shouldTruncateToOnePomodoro() throws Exception {
|
||||
StudySessionsEntity session = createSession(StudySessionStateEnum.ONGOING.name(), 180);
|
||||
mockGetOneOpt(session);
|
||||
|
||||
studySessionsService.pauseStudySession("SESSION_TEST", null);
|
||||
|
||||
assertEquals(StudySessionStateEnum.PAUSED.name(), session.getSessionState());
|
||||
assertEquals(25 * 60, session.getEffectiveTime(), "暂停路径也应按遗忘规则截断");
|
||||
}
|
||||
|
||||
// ============ 会话-02:暂停会话不再弹出超时提示 ============
|
||||
|
||||
/**
|
||||
* 场景19:PAUSED 且 end_time 已存在、last_start_time 很旧 → 不返回 systemMessage
|
||||
*/
|
||||
@Test
|
||||
void startOrContinueStudySession_pausedStaleSession_shouldNotReturnSystemMessage() throws Exception {
|
||||
StudySessionsEntity session = createSession(StudySessionStateEnum.PAUSED.name(), 180);
|
||||
session.setEndTime(session.getLastStartTime().plusMinutes(25));
|
||||
session.setEffectiveTime(25 * 60);
|
||||
mockGetOneOpt(session);
|
||||
|
||||
TaskEntity task = new TaskEntity();
|
||||
task.setTaskNum("TASK_TEST");
|
||||
task.setTaskName("测试任务");
|
||||
doReturn(Optional.of(task)).when(tasksServiceImpl).getOneOpt(any(Wrapper.class));
|
||||
|
||||
var response = studySessionsService.startOrContinueStudySession("TASK_TEST");
|
||||
|
||||
assertNull(response.getSystemMessage(), "已暂停的会话不应再提示超时");
|
||||
}
|
||||
|
||||
// ============ 会话-03:结束会话幂等 ============
|
||||
|
||||
/**
|
||||
* 场景20:重复结束已 ENDED 的会话 → 不重复生成学习报告
|
||||
*/
|
||||
@Test
|
||||
void endedStudySession_alreadyEnded_shouldNotInsertReport() throws Exception {
|
||||
StudySessionsEntity session = createSession(StudySessionStateEnum.ENDED.name(), 5);
|
||||
session.setEffectiveTime(5 * 60);
|
||||
mockGetOneOpt(session);
|
||||
|
||||
String message = studySessionsService.endedStudySession("SESSION_TEST", "内容");
|
||||
|
||||
assertNull(message);
|
||||
verify(studyReportsMapper, never()).insert(any(StudyReportsEntity.class));
|
||||
verify(studySessionsService, never()).update(any(StudySessionsEntity.class), any(Wrapper.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 场景21:并发结束抢占失败(其他请求已先结束)→ 不重复生成学习报告
|
||||
*/
|
||||
@Test
|
||||
void endedStudySession_concurrentClaimLost_shouldNotInsertReport() throws Exception {
|
||||
StudySessionsEntity session = createSession(StudySessionStateEnum.ONGOING.name(), 15);
|
||||
session.setId(30);
|
||||
mockGetOneOpt(session);
|
||||
doReturn(false).when(studySessionsService).update(any(StudySessionsEntity.class), any(Wrapper.class));
|
||||
|
||||
String message = studySessionsService.endedStudySession("SESSION_TEST", "内容");
|
||||
|
||||
assertNull(message);
|
||||
verify(studyReportsMapper, never()).insert(any(StudyReportsEntity.class));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user