|
6 | 6 | import com.ject.vs.chat.domain.ChatRoomUnreadRepository; |
7 | 7 | import com.ject.vs.chat.exception.ChatForbiddenException; |
8 | 8 | import com.ject.vs.chat.exception.InvalidMessageException; |
| 9 | +import com.ject.vs.chat.port.in.dto.ChatListItemResult; |
9 | 10 | import com.ject.vs.chat.port.in.dto.MarkAsReadCommand; |
10 | 11 | import com.ject.vs.chat.port.in.dto.MessagePageResult; |
11 | 12 | import com.ject.vs.chat.port.in.dto.MessageResult; |
12 | 13 | import com.ject.vs.chat.port.in.dto.SendMessageCommand; |
| 14 | +import com.ject.vs.vote.domain.VoteStatus; |
13 | 15 | import com.ject.vs.user.domain.ImageColor; |
14 | 16 | import com.ject.vs.user.domain.User; |
15 | 17 | import com.ject.vs.user.port.in.UserQueryUseCase; |
|
27 | 29 | import org.mockito.junit.jupiter.MockitoExtension; |
28 | 30 | import org.springframework.data.domain.PageRequest; |
29 | 31 |
|
| 32 | +import java.time.Instant; |
30 | 33 | import java.util.List; |
31 | 34 | import java.util.Optional; |
32 | 35 |
|
@@ -139,6 +142,57 @@ class markAsRead { |
139 | 142 | } |
140 | 143 | } |
141 | 144 |
|
| 145 | + @Nested |
| 146 | + class getChatList { |
| 147 | + |
| 148 | + @Test |
| 149 | + void 최근_메시지_시간_기준_내림차순으로_정렬한다() { |
| 150 | + // given |
| 151 | + Long userId = 1L; |
| 152 | + given(voteParticipationQueryUseCase.findAllVoteIdsByUserId(userId)).willReturn(List.of(10L, 20L, 30L)); |
| 153 | + given(voteQueryUseCase.findAllVoteIdsByStatus(List.of(10L, 20L, 30L), VoteStatus.ONGOING)) |
| 154 | + .willReturn(List.of(10L, 20L, 30L)); |
| 155 | + |
| 156 | + Instant endAt = Instant.parse("2026-12-31T00:00:00Z"); |
| 157 | + given(voteQueryUseCase.getVoteChatSummary(10L)) |
| 158 | + .willReturn(new VoteQueryUseCase.VoteChatSummary(10L, "t10", null, VoteStatus.ONGOING, endAt, "A", "B")); |
| 159 | + given(voteQueryUseCase.getVoteChatSummary(20L)) |
| 160 | + .willReturn(new VoteQueryUseCase.VoteChatSummary(20L, "t20", null, VoteStatus.ONGOING, endAt, "A", "B")); |
| 161 | + given(voteQueryUseCase.getVoteChatSummary(30L)) |
| 162 | + .willReturn(new VoteQueryUseCase.VoteChatSummary(30L, "t30", null, VoteStatus.ONGOING, endAt, "A", "B")); |
| 163 | + |
| 164 | + given(voteParticipationQueryUseCase.countParticipantsByVoteId(any())).willReturn(1L); |
| 165 | + given(chatRoomUnreadRepository.findByIdUserIdAndIdVoteId(eq(userId), any())).willReturn(Optional.empty()); |
| 166 | + given(chatMessageRepository.countByVoteId(any())).willReturn(0L); |
| 167 | + |
| 168 | + ChatMessage oldMsg = mock(ChatMessage.class); |
| 169 | + ChatMessage midMsg = mock(ChatMessage.class); |
| 170 | + ChatMessage newMsg = mock(ChatMessage.class); |
| 171 | + |
| 172 | + given(oldMsg.getContent()).willReturn("old"); |
| 173 | + given(midMsg.getContent()).willReturn("mid"); |
| 174 | + given(newMsg.getContent()).willReturn("new"); |
| 175 | + given(oldMsg.getCreatedAt()).willReturn(Instant.parse("2026-01-01T00:00:00Z")); |
| 176 | + given(midMsg.getCreatedAt()).willReturn(Instant.parse("2026-01-02T00:00:00Z")); |
| 177 | + given(newMsg.getCreatedAt()).willReturn(Instant.parse("2026-01-03T00:00:00Z")); |
| 178 | + |
| 179 | + given(chatMessageRepository.findFirstByVoteIdOrderByIdDesc(10L)).willReturn(Optional.of(oldMsg)); |
| 180 | + given(chatMessageRepository.findFirstByVoteIdOrderByIdDesc(20L)).willReturn(Optional.of(midMsg)); |
| 181 | + given(chatMessageRepository.findFirstByVoteIdOrderByIdDesc(30L)).willReturn(Optional.of(newMsg)); |
| 182 | + |
| 183 | + // when |
| 184 | + List<ChatListItemResult> result = chatService.getChatList(userId, VoteStatus.ONGOING); |
| 185 | + |
| 186 | + // then |
| 187 | + assertThat(result).extracting(ChatListItemResult::voteId).containsExactly(30L, 20L, 10L); |
| 188 | + assertThat(result).extracting(ChatListItemResult::lastMessageAt) |
| 189 | + .containsExactly( |
| 190 | + Instant.parse("2026-01-03T00:00:00Z"), |
| 191 | + Instant.parse("2026-01-02T00:00:00Z"), |
| 192 | + Instant.parse("2026-01-01T00:00:00Z")); |
| 193 | + } |
| 194 | + } |
| 195 | + |
142 | 196 | @Nested |
143 | 197 | class getMessages { |
144 | 198 |
|
|
0 commit comments