“mitao”
2026-04-14 1f173d482408cbeedb84d261898ebd2efb816547
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package com.tcm.training.admin.app.live.service;
 
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.tcm.training.admin.app.auth.service.AppAuthSessionService;
import com.tcm.training.admin.app.live.support.AppLiveCourseDisplaySupport;
import com.tcm.training.admin.app.live.vo.AppLiveCourseDetailVO;
import com.tcm.training.admin.app.live.vo.AppLiveReserveResultVO;
import com.tcm.training.admin.live.entity.Expert;
import com.tcm.training.admin.live.entity.LiveCourse;
import com.tcm.training.admin.live.entity.LiveReserve;
import com.tcm.training.admin.live.entity.LiveSession;
import com.tcm.training.admin.live.mapper.ExpertMapper;
import com.tcm.training.admin.live.mapper.LiveCourseMapper;
import com.tcm.training.admin.live.mapper.LiveReserveMapper;
import com.tcm.training.admin.live.mapper.LiveSessionMapper;
import com.tcm.training.admin.live.mapper.LiveSessionQuestionMapper;
import com.tcm.training.admin.live.mapper.LiveSessionQuizRecordMapper;
import com.tcm.training.admin.live.mapper.LiveWatchRecordMapper;
import com.tcm.training.admin.points.mapper.UserPointsAccountMapper;
import com.tcm.training.admin.points.service.PointsLedgerAppService;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
 
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
/**
 * APP 养生直播详情服务测试。
 */
@ExtendWith(MockitoExtension.class)
class AppLiveCourseServiceTest {
 
    @BeforeAll
    static void initLambdaCache() {
        MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
        TableInfoHelper.initTableInfo(assistant, LiveCourse.class);
        TableInfoHelper.initTableInfo(assistant, LiveSession.class);
        TableInfoHelper.initTableInfo(assistant, LiveReserve.class);
        TableInfoHelper.initTableInfo(assistant, Expert.class);
    }
 
    @Mock
    private LiveCourseMapper liveCourseMapper;
 
    @Mock
    private LiveSessionMapper liveSessionMapper;
 
    @Mock
    private LiveReserveMapper liveReserveMapper;
 
    @Mock
    private ExpertMapper expertMapper;
 
    @Mock
    private LiveSessionQuestionMapper liveSessionQuestionMapper;
 
    @Mock
    private LiveSessionQuizRecordMapper liveSessionQuizRecordMapper;
 
    @Mock
    private LiveWatchRecordMapper liveWatchRecordMapper;
 
    @Mock
    private UserPointsAccountMapper userPointsAccountMapper;
 
    @Mock
    private PointsLedgerAppService pointsLedgerAppService;
 
    @Mock
    private AppAuthSessionService appAuthSessionService;
 
    private AppLiveCourseService appLiveCourseService;
 
    @Captor
    private ArgumentCaptor<LiveReserve> liveReserveCaptor;
 
    /**
     * 每个用例前重建被测服务。
     */
    @BeforeEach
    void setUp() {
        AppLiveExpertService appLiveExpertService = new AppLiveExpertService(expertMapper);
        // 详情与预约用例不关心答题和观看明细,统一返回空列表避免空指针干扰主断言。
        lenient().when(liveSessionQuestionMapper.selectList(any())).thenReturn(Collections.emptyList());
        lenient().when(liveSessionQuizRecordMapper.selectList(any())).thenReturn(Collections.emptyList());
        lenient().when(liveWatchRecordMapper.selectList(any())).thenReturn(Collections.emptyList());
        appLiveCourseService = new AppLiveCourseService(
                liveCourseMapper,
                liveSessionMapper,
                liveReserveMapper,
                expertMapper,
                liveSessionQuestionMapper,
                liveSessionQuizRecordMapper,
                liveWatchRecordMapper,
                userPointsAccountMapper,
                pointsLedgerAppService,
                appAuthSessionService,
                new AppLiveCourseDisplaySupport(),
                appLiveExpertService
        );
    }
 
    /**
     * 未开始场次且用户已预约时,应回显预约态详情。
     */
    @Test
    void detailShouldReturnReservedActionWhenUpcomingSessionReserved() {
        LiveCourse course = buildCourse(10L, 8L);
        LiveSession nextSession = buildSession(101L, 10L, LocalDateTime.now().plusDays(1),
                AppLiveCourseDisplaySupport.SESSION_STATUS_NOT_STARTED);
        nextSession.setBaseReserveCount(10);
        nextSession.setRealReserveCount(2);
        nextSession.setBaseWatchCount(3);
        nextSession.setRealWatchCount(4);
 
        when(liveCourseMapper.selectOne(any())).thenReturn(course);
        when(liveSessionMapper.selectList(any())).thenReturn(Collections.singletonList(nextSession));
        when(expertMapper.selectOne(any())).thenReturn(buildExpert(8L));
        when(appAuthSessionService.isLogin()).thenReturn(true);
        when(appAuthSessionService.getLoginUserId()).thenReturn(66L);
        when(liveReserveMapper.selectCount(any())).thenReturn(1L);
 
        AppLiveCourseDetailVO detailVO = appLiveCourseService.detail(10L);
 
        assertEquals("NOT_STARTED", detailVO.getDisplayStatus());
        assertEquals("RESERVED", detailVO.getActionStatus());
        assertEquals("已预约", detailVO.getActionText());
        assertEquals(Long.valueOf(101L), detailVO.getActionSessionId());
        assertNotNull(detailVO.getCountdownTime());
        assertEquals(Long.valueOf(12L), detailVO.getReserveCount());
        assertEquals(Long.valueOf(7L), detailVO.getWatchCount());
        assertEquals("张雨泽", detailVO.getExpert().getName());
    }
 
    /**
     * 直播结束后五分钟内仍应保持立即查看按钮。
     */
    @Test
    void detailShouldKeepWatchableDuringEndedCooldown() {
        LiveCourse course = buildCourse(20L, 9L);
        LiveSession endedSession = buildSession(201L, 20L, LocalDateTime.now().minusHours(1),
                AppLiveCourseDisplaySupport.SESSION_STATUS_ENDED);
        endedSession.setActualEndTime(LocalDateTime.now().minusMinutes(3));
        endedSession.setVideoUrl("https://cdn.example/live.m3u8");
        LiveSession nextSession = buildSession(202L, 20L, LocalDateTime.now().plusDays(2),
                AppLiveCourseDisplaySupport.SESSION_STATUS_NOT_STARTED);
 
        when(liveCourseMapper.selectOne(any())).thenReturn(course);
        when(liveSessionMapper.selectList(any())).thenReturn(Arrays.asList(endedSession, nextSession));
        when(expertMapper.selectOne(any())).thenReturn(buildExpert(9L));
        when(appAuthSessionService.isLogin()).thenReturn(false);
        when(liveReserveMapper.selectCount(any())).thenReturn(0L);
 
        AppLiveCourseDetailVO detailVO = appLiveCourseService.detail(20L);
 
        assertEquals("ENDED", detailVO.getDisplayStatus());
        assertEquals("WATCHABLE", detailVO.getActionStatus());
        assertEquals("立即查看", detailVO.getActionText());
        assertEquals(Long.valueOf(201L), detailVO.getActionSessionId());
        assertNull(detailVO.getCountdownTime());
        assertEquals("https://cdn.example/live.m3u8", detailVO.getVideoUrl());
    }
 
    /**
     * 历史残留的直播中场次不应覆盖时间线上最近一场已结束场次的展示态。
     */
    @Test
    void detailShouldIgnoreHistoricalLivingSessionWhenLatestStartedSessionEnded() {
        LiveCourse course = buildCourse(21L, 9L);
        LiveSession staleLivingSession = buildSession(210L, 21L, LocalDateTime.now().minusDays(1),
                AppLiveCourseDisplaySupport.SESSION_STATUS_LIVING);
        staleLivingSession.setVideoUrl("https://cdn.example/stale.m3u8");
        LiveSession latestEndedSession = buildSession(211L, 21L, LocalDateTime.now().minusHours(1),
                AppLiveCourseDisplaySupport.SESSION_STATUS_ENDED);
        latestEndedSession.setActualEndTime(LocalDateTime.now().minusMinutes(2));
        latestEndedSession.setVideoUrl("https://cdn.example/latest-ended.m3u8");
 
        when(liveCourseMapper.selectOne(any())).thenReturn(course);
        when(liveSessionMapper.selectList(any())).thenReturn(Arrays.asList(staleLivingSession, latestEndedSession));
        when(expertMapper.selectOne(any())).thenReturn(buildExpert(9L));
        when(appAuthSessionService.isLogin()).thenReturn(false);
        when(liveReserveMapper.selectCount(any())).thenReturn(0L);
 
        AppLiveCourseDetailVO detailVO = appLiveCourseService.detail(21L);
 
        assertEquals("ENDED", detailVO.getDisplayStatus());
        assertEquals("WATCHABLE", detailVO.getActionStatus());
        assertEquals(Long.valueOf(211L), detailVO.getActionSessionId());
        assertEquals("https://cdn.example/latest-ended.m3u8", detailVO.getVideoUrl());
    }
 
    /**
     * 首次预约时应写入直播预约记录。
     */
    @Test
    void reserveShouldInsertRecordWhenNoActiveRecordExists() {
        LiveCourse course = buildCourse(30L, 10L);
        LiveSession nextSession = buildSession(301L, 30L, LocalDateTime.now().plusHours(3),
                AppLiveCourseDisplaySupport.SESSION_STATUS_NOT_STARTED);
 
        when(appAuthSessionService.requireLoginUserId()).thenReturn(88L);
        when(liveCourseMapper.selectOne(any())).thenReturn(course);
        when(liveSessionMapper.selectOne(any())).thenReturn(nextSession);
        when(liveReserveMapper.selectOne(any())).thenReturn(null);
        when(liveSessionMapper.selectList(any())).thenReturn(Collections.singletonList(nextSession));
        when(liveReserveMapper.selectCount(any())).thenReturn(1L);
 
        AppLiveReserveResultVO resultVO = appLiveCourseService.reserve(30L, 301L);
 
        verify(liveReserveMapper).insert(liveReserveCaptor.capture());
        assertEquals(Long.valueOf(30L), liveReserveCaptor.getValue().getLiveCourseId());
        assertEquals(Long.valueOf(301L), liveReserveCaptor.getValue().getSessionId());
        assertEquals(Long.valueOf(88L), liveReserveCaptor.getValue().getUserId());
        assertEquals(Integer.valueOf(0), liveReserveCaptor.getValue().getIsCancel());
        assertEquals(Boolean.TRUE, resultVO.getReserved());
        assertEquals(Long.valueOf(1L), resultVO.getReserveCount());
    }
 
    /**
     * 已取消的预约记录再次预约时应恢复有效状态。
     */
    @Test
    void reserveShouldRestoreCancelledRecordWhenExists() {
        LiveCourse course = buildCourse(40L, 11L);
        LiveSession nextSession = buildSession(401L, 40L, LocalDateTime.now().plusHours(5),
                AppLiveCourseDisplaySupport.SESSION_STATUS_NOT_STARTED);
        LiveReserve cancelledReserve = new LiveReserve();
        cancelledReserve.setId(501L);
        cancelledReserve.setLiveCourseId(40L);
        cancelledReserve.setSessionId(401L);
        cancelledReserve.setUserId(99L);
        cancelledReserve.setIsCancel(1);
 
        when(appAuthSessionService.requireLoginUserId()).thenReturn(99L);
        when(liveCourseMapper.selectOne(any())).thenReturn(course);
        when(liveSessionMapper.selectOne(any())).thenReturn(nextSession);
        when(liveReserveMapper.selectOne(any())).thenReturn(cancelledReserve);
        when(liveSessionMapper.selectList(any())).thenReturn(Collections.singletonList(nextSession));
        when(liveReserveMapper.selectCount(any())).thenReturn(2L);
 
        AppLiveReserveResultVO resultVO = appLiveCourseService.reserve(40L, 401L);
 
        verify(liveReserveMapper).updateById(liveReserveCaptor.capture());
        assertEquals(Integer.valueOf(0), liveReserveCaptor.getValue().getIsCancel());
        assertNotNull(liveReserveCaptor.getValue().getReserveTime());
        assertEquals(Long.valueOf(2L), resultVO.getReserveCount());
    }
 
    /**
     * 构造启用中的直播课程测试数据。
     */
    private LiveCourse buildCourse(Long courseId, Long expertId) {
        LiveCourse course = new LiveCourse();
        course.setId(courseId);
        course.setExpertId(expertId);
        course.setTitle("中医营养师直播课");
        course.setCoverImg("https://cdn.example/cover.png");
        course.setIntro("课程详情");
        course.setTargetUsers("适用人群");
        course.setQuizPoints(3);
        course.setStatus(1);
        return course;
    }
 
    /**
     * 构造直播场次测试数据。
     */
    private LiveSession buildSession(Long sessionId, Long courseId, LocalDateTime startTime, Integer liveStatus) {
        LiveSession session = new LiveSession();
        session.setId(sessionId);
        session.setLiveCourseId(courseId);
        session.setStartTime(startTime);
        session.setLiveStatus(liveStatus);
        return session;
    }
 
    /**
     * 构造启用中的专家测试数据。
     */
    private Expert buildExpert(Long expertId) {
        Expert expert = new Expert();
        expert.setId(expertId);
        expert.setName("张雨泽");
        expert.setAvatar("https://cdn.example/avatar.png");
        expert.setSpecialty("营养调理");
        expert.setIntro("专家介绍");
        expert.setStatus(1);
        return expert;
    }
}