liyong
10 小时以前 7a23c450f3ac85de7dca1b908de273ff636ce218
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
package cn.iocoder.yudao.module.im.service.message;
 
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
import cn.iocoder.yudao.module.im.dal.dataobject.message.ImChannelMessageDO;
import cn.iocoder.yudao.module.im.dal.mysql.message.ImChannelMessageMapper;
import cn.iocoder.yudao.module.im.service.channel.ImChannelMaterialService;
import cn.iocoder.yudao.module.im.service.conversation.ImConversationReadService;
import cn.iocoder.yudao.module.im.service.websocket.ImWebSocketService;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
 
import java.time.LocalDateTime;
import java.util.List;
 
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
 
/**
 * {@link ImChannelMessageServiceImpl} 的单元测试
 *
 * @author 芋道源码
 */
public class ImChannelMessageServiceImplTest extends BaseMockitoUnitTest {
 
    @InjectMocks
    private ImChannelMessageServiceImpl channelMessageService;
 
    @Mock
    private ImChannelMessageMapper channelMessageMapper;
    @Mock
    private ImChannelMaterialService channelMaterialService;
    @Mock
    private ImWebSocketService webSocketService;
    @Mock
    private ImConversationReadService conversationReadService;
 
    @Test
    public void testReadChannelMessages_messageNotExists() {
        // 准备:messageId 不存在(伪造 / 未来 id)
        when(channelMessageMapper.selectById(999L)).thenReturn(null);
 
        // 调用
        channelMessageService.readChannelMessages(1L, 10L, 999L);
 
        // 断言:不推进读位置
        verify(conversationReadService, never()).updateConversationReadPosition(anyLong(), anyInt(), anyLong(), anyLong());
    }
 
    @Test
    public void testReadChannelMessages_wrongChannel() {
        // 准备:消息属于别的频道
        ImChannelMessageDO message = ImChannelMessageDO.builder()
                .id(100L).channelId(20L).sendTime(LocalDateTime.now()).build();
        when(channelMessageMapper.selectById(100L)).thenReturn(message);
 
        // 调用:声称读的是频道 10
        channelMessageService.readChannelMessages(1L, 10L, 100L);
 
        // 断言:不推进读位置
        verify(conversationReadService, never()).updateConversationReadPosition(anyLong(), anyInt(), anyLong(), anyLong());
    }
 
    @Test
    public void testReadChannelMessages_notVisible() {
        // 准备:定向消息,接收人不含当前用户
        ImChannelMessageDO message = ImChannelMessageDO.builder()
                .id(100L).channelId(10L).receiverUserIds(List.of(2L, 3L)).sendTime(LocalDateTime.now()).build();
        when(channelMessageMapper.selectById(100L)).thenReturn(message);
 
        // 调用
        channelMessageService.readChannelMessages(1L, 10L, 100L);
 
        // 断言:不推进读位置
        verify(conversationReadService, never()).updateConversationReadPosition(anyLong(), anyInt(), anyLong(), anyLong());
    }
 
    @Test
    public void testReadChannelMessages_notAdvanced() {
        // 准备:消息真实可见,但读位置未前进(updateConversationReadPosition 返回 false)
        ImChannelMessageDO message = ImChannelMessageDO.builder()
                .id(100L).channelId(10L).sendTime(LocalDateTime.now()).build();
        when(channelMessageMapper.selectById(100L)).thenReturn(message);
        when(conversationReadService.updateConversationReadPosition(anyLong(), anyInt(), anyLong(), anyLong()))
                .thenReturn(false);
 
        // 调用
        channelMessageService.readChannelMessages(1L, 10L, 100L);
 
        // 断言:读位置未前进 → 不推 READ 事件
        verify(webSocketService, never()).sendNotificationAsync(anyLong(), anyInt(), anyInt(), any());
    }
 
}