182 lines
7.7 KiB
Java
182 lines
7.7 KiB
Java
package com.guo.learningprogresstracker.service.impl;
|
|
|
|
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
|
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
|
import com.guo.learningprogresstracker.dto.TaskReviewDuration;
|
|
import com.guo.learningprogresstracker.entity.ReviewRecallRecordEntity;
|
|
import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity;
|
|
import com.guo.learningprogresstracker.entity.StudyReportsEntity;
|
|
import com.guo.learningprogresstracker.entity.StudySessionsEntity;
|
|
import com.guo.learningprogresstracker.entity.TaskEntity;
|
|
import com.guo.learningprogresstracker.mapper.ReviewRecallRecordMapper;
|
|
import com.guo.learningprogresstracker.mapper.StudyReportFragmentsMapper;
|
|
import com.guo.learningprogresstracker.mapper.StudyReportsMapper;
|
|
import com.guo.learningprogresstracker.mapper.StudySessionsMapper;
|
|
import com.guo.learningprogresstracker.mapper.TasksMapper;
|
|
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
|
import org.junit.jupiter.api.BeforeAll;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
import org.mockito.InjectMocks;
|
|
import org.mockito.Mock;
|
|
import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.util.List;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
@ExtendWith(MockitoExtension.class)
|
|
class ReviewServiceImplTest {
|
|
|
|
@BeforeAll
|
|
static void initTableInfo() {
|
|
MybatisConfiguration configuration = new MybatisConfiguration();
|
|
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(configuration, ""), StudyReportsEntity.class);
|
|
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(configuration, ""), StudyReportFragmentsEntity.class);
|
|
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(configuration, ""), StudySessionsEntity.class);
|
|
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(configuration, ""), TaskEntity.class);
|
|
}
|
|
|
|
@InjectMocks
|
|
private ReviewServiceImpl reviewService;
|
|
|
|
@Mock
|
|
private StudyReportsMapper studyReportsMapper;
|
|
|
|
@Mock
|
|
private StudyReportFragmentsMapper studyReportFragmentsMapper;
|
|
|
|
@Mock
|
|
private StudySessionsMapper studySessionsMapper;
|
|
|
|
@Mock
|
|
private TasksMapper tasksMapper;
|
|
|
|
@Mock
|
|
private ReviewRecallRecordMapper reviewRecallRecordMapper;
|
|
|
|
@Test
|
|
void getReviewFeed_smartMode_prefersLowRecallRatioContent() {
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
|
StudyReportFragmentsEntity oldLowMastery = new StudyReportFragmentsEntity();
|
|
oldLowMastery.setId(1);
|
|
oldLowMastery.setSessionNum("S_LOW");
|
|
oldLowMastery.setContent("很久没复习且掌握度低");
|
|
oldLowMastery.setCreatedTime(now.minusDays(60));
|
|
|
|
StudyReportFragmentsEntity freshHighMastery = new StudyReportFragmentsEntity();
|
|
freshHighMastery.setId(2);
|
|
freshHighMastery.setSessionNum("S_HIGH");
|
|
freshHighMastery.setContent("刚学完且掌握度高");
|
|
freshHighMastery.setCreatedTime(now);
|
|
|
|
StudySessionsEntity sessionLow = new StudySessionsEntity();
|
|
sessionLow.setSessionNum("S_LOW");
|
|
sessionLow.setTaskNum("T_LOW");
|
|
StudySessionsEntity sessionHigh = new StudySessionsEntity();
|
|
sessionHigh.setSessionNum("S_HIGH");
|
|
sessionHigh.setTaskNum("T_HIGH");
|
|
|
|
TaskEntity taskLow = new TaskEntity();
|
|
taskLow.setTaskNum("T_LOW");
|
|
taskLow.setTaskName("低掌握任务");
|
|
TaskEntity taskHigh = new TaskEntity();
|
|
taskHigh.setTaskNum("T_HIGH");
|
|
taskHigh.setTaskName("高掌握任务");
|
|
|
|
ReviewRecallRecordEntity highRatioRecord = new ReviewRecallRecordEntity();
|
|
highRatioRecord.setTaskNum("T_HIGH");
|
|
highRatioRecord.setRecallRatio(1.0);
|
|
highRatioRecord.setCreatedTime(now);
|
|
|
|
when(studyReportFragmentsMapper.selectList(any())).thenReturn(List.of(oldLowMastery, freshHighMastery));
|
|
when(studySessionsMapper.selectList(any())).thenReturn(List.of(sessionLow, sessionHigh));
|
|
when(tasksMapper.selectList(any())).thenReturn(List.of(taskLow, taskHigh));
|
|
when(reviewRecallRecordMapper.selectList(any())).thenReturn(List.of(highRatioRecord));
|
|
|
|
List<ReviewFeedItem> items = reviewService.getReviewFeed(1, "smart");
|
|
|
|
// 采样是概率性的,多次运行统计低掌握内容被选中的频率应显著更高
|
|
int lowMasteryWins = 0;
|
|
for (int i = 0; i < 200; i++) {
|
|
List<ReviewFeedItem> sample = reviewService.getReviewFeed(1, "smart");
|
|
if (!sample.isEmpty() && "T_LOW".equals(sample.get(0).getTaskNum())) {
|
|
lowMasteryWins++;
|
|
}
|
|
}
|
|
assertEquals(1, items.size());
|
|
// 低掌握内容权重约为高掌握内容的 5 倍以上,200 次中至少应赢 120 次
|
|
org.junit.jupiter.api.Assertions.assertTrue(lowMasteryWins > 120,
|
|
"低掌握内容被选中次数应显著更多,实际: " + lowMasteryWins + "/200");
|
|
}
|
|
|
|
@Test
|
|
void getReviewFeed_shouldReturnFragmentsOnly() {
|
|
StudyReportFragmentsEntity fragment = new StudyReportFragmentsEntity();
|
|
fragment.setId(1);
|
|
fragment.setSessionNum("S1");
|
|
fragment.setContent("fragment");
|
|
fragment.setCreatedTime(LocalDateTime.now().minusMinutes(1));
|
|
|
|
StudySessionsEntity session = new StudySessionsEntity();
|
|
session.setSessionNum("S1");
|
|
session.setTaskNum("T1");
|
|
|
|
TaskEntity task = new TaskEntity();
|
|
task.setTaskNum("T1");
|
|
task.setTaskName("Task");
|
|
|
|
when(studyReportFragmentsMapper.selectList(any())).thenReturn(List.of(fragment));
|
|
when(studySessionsMapper.selectList(any())).thenReturn(List.of(session));
|
|
when(tasksMapper.selectList(any())).thenReturn(List.of(task));
|
|
|
|
List<ReviewFeedItem> items = reviewService.getReviewFeed(1, "recent");
|
|
|
|
assertEquals(1, items.size());
|
|
assertEquals("FRAGMENT", items.get(0).getSourceType());
|
|
assertEquals("Task", items.get(0).getTaskName());
|
|
}
|
|
|
|
@Test
|
|
void getReviewTaskStats_fillsReviewDurationFromAggregate() {
|
|
TaskEntity task = new TaskEntity();
|
|
task.setTaskNum("T1");
|
|
task.setTaskName("任务一");
|
|
|
|
StudySessionsEntity session = new StudySessionsEntity();
|
|
session.setSessionNum("S1");
|
|
session.setTaskNum("T1");
|
|
session.setEffectiveTime(600);
|
|
session.setEffectivenessRatio(0.8);
|
|
session.setStartTime(LocalDateTime.now());
|
|
|
|
when(tasksMapper.selectList(any())).thenReturn(List.of(task));
|
|
when(studySessionsMapper.selectList(any())).thenReturn(List.of(session));
|
|
when(studyReportsMapper.selectList(any())).thenReturn(List.of());
|
|
when(studyReportFragmentsMapper.selectList(any())).thenReturn(List.of());
|
|
// 聚合结果里出现了不在任务列表中的 taskNum,应被忽略
|
|
when(reviewRecallRecordMapper.sumReviewDurationByTask(any())).thenReturn(List.of(
|
|
reviewDuration("T1", 420L),
|
|
reviewDuration("T_UNKNOWN", 100L)));
|
|
|
|
List<ReviewTaskStats> stats = reviewService.getReviewTaskStats();
|
|
|
|
assertEquals(1, stats.size());
|
|
assertEquals(600, stats.get(0).getEffectiveTime());
|
|
assertEquals(420L, stats.get(0).getReviewDurationSeconds());
|
|
}
|
|
|
|
private static TaskReviewDuration reviewDuration(String taskNum, Long seconds) {
|
|
TaskReviewDuration row = new TaskReviewDuration();
|
|
row.setTaskNum(taskNum);
|
|
row.setReviewDurationSeconds(seconds);
|
|
return row;
|
|
}
|
|
}
|