Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.devpick.domain.point.entity.PointAction;
import com.devpick.domain.report.entity.History;
import com.devpick.global.util.MarkdownPreviewUtils;

import java.time.Instant;
import java.time.ZoneOffset;
Expand Down Expand Up @@ -39,7 +40,7 @@ public static HistoryItemResponse of(History history) {
AnswerInfo answerInfo = history.getAnswer() != null
? new AnswerInfo(
history.getAnswer().getId(),
truncate(history.getAnswer().getContent(), 100))
truncate(MarkdownPreviewUtils.stripForPreview(history.getAnswer().getContent()), 100))
: null;

CommentInfo commentInfo = history.getComment() != null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.devpick.domain.report.service;

import com.devpick.domain.community.entity.Answer;
import com.devpick.domain.community.entity.Post;
import com.devpick.domain.content.entity.Content;
import com.devpick.domain.report.dto.ActivityPageResponse;
Expand Down Expand Up @@ -295,6 +296,84 @@
verify(historyRepository, never()).findHistoryIdsByActionTypes(any(), any(), any());
}

// ============================================================
// answer preview — markdown stripping (DP-368)
// ============================================================

@Test
@DisplayName("답변 미리보기 - 마크다운 문법 제거 후 반환")
void historyAnswerInfo_withMarkdown_stripsMarkdown() {
Answer answer = mock(Answer.class);
UUID answerId = UUID.randomUUID();
given(answer.getId()).willReturn(answerId);
given(answer.getContent()).willReturn("**핵심 요약**: `useEffect`는 렌더링 후 실행됩니다.");

History history = History.builder()
.user(user).actionType("answer_written").answer(answer).build();
UUID historyId = UUID.randomUUID();
ReflectionTestUtils.setField(history, "id", historyId);

Page<UUID> idPage = new PageImpl<>(List.of(historyId), pageable, 1);
given(userRepository.findByIdAndIsActiveTrue(userId)).willReturn(Optional.of(user));
given(historyRepository.findHistoryIdsExcludingContentLiked(eq(userId), any(Pageable.class)))
.willReturn(idPage);
given(historyRepository.findHistoriesWithAssociationsByIds(List.of(historyId)))
.willReturn(List.of(history));

HistoryPageResponse response = historyService.getHistory(userId, null, null, null, pageable);

String preview = response.items().get(0).answer().preview();
assertThat(preview).doesNotContain("**").doesNotContain("`");

Check warning on line 326 in src/test/java/com/devpick/domain/report/service/HistoryServiceTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Join these multiple assertions subject to one assertion chain.

See more on https://sonarcloud.io/project/issues?id=Devpick-Org_devpick-backend&issues=AZ29KkKDRkrKfjGKI_j5&open=AZ29KkKDRkrKfjGKI_j5&pullRequest=129
assertThat(preview).contains("핵심 요약").contains("useEffect");
}

@Test
@DisplayName("답변 미리보기 - 마크다운 제거 후 100자 초과 시 잘림 처리")
void historyAnswerInfo_withLongContentAfterStrip_truncated() {
Answer answer = mock(Answer.class);
given(answer.getId()).willReturn(UUID.randomUUID());
String longContent = "a".repeat(120);
given(answer.getContent()).willReturn(longContent);

History history = History.builder()
.user(user).actionType("answer_written").answer(answer).build();
UUID historyId = UUID.randomUUID();
ReflectionTestUtils.setField(history, "id", historyId);

Page<UUID> idPage = new PageImpl<>(List.of(historyId), pageable, 1);
given(userRepository.findByIdAndIsActiveTrue(userId)).willReturn(Optional.of(user));
given(historyRepository.findHistoryIdsExcludingContentLiked(eq(userId), any(Pageable.class)))
.willReturn(idPage);
given(historyRepository.findHistoriesWithAssociationsByIds(List.of(historyId)))
.willReturn(List.of(history));

HistoryPageResponse response = historyService.getHistory(userId, null, null, null, pageable);

String preview = response.items().get(0).answer().preview();
assertThat(preview).endsWith("...");

Check warning on line 353 in src/test/java/com/devpick/domain/report/service/HistoryServiceTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Join these multiple assertions subject to one assertion chain.

See more on https://sonarcloud.io/project/issues?id=Devpick-Org_devpick-backend&issues=AZ29KkKDRkrKfjGKI_j6&open=AZ29KkKDRkrKfjGKI_j6&pullRequest=129
assertThat(preview).hasSize(103); // 100 chars + "..."
}

@Test
@DisplayName("답변 미리보기 - answer null이면 answerInfo null 반환")
void historyAnswerInfo_whenAnswerNull_returnsNullAnswerInfo() {
History history = History.builder()
.user(user).actionType("question_created").answer(null).build();
UUID historyId = UUID.randomUUID();
ReflectionTestUtils.setField(history, "id", historyId);

Page<UUID> idPage = new PageImpl<>(List.of(historyId), pageable, 1);
given(userRepository.findByIdAndIsActiveTrue(userId)).willReturn(Optional.of(user));
given(historyRepository.findHistoryIdsExcludingContentLiked(eq(userId), any(Pageable.class)))
.willReturn(idPage);
given(historyRepository.findHistoriesWithAssociationsByIds(List.of(historyId)))
.willReturn(List.of(history));

HistoryPageResponse response = historyService.getHistory(userId, null, null, null, pageable);

assertThat(response.items().get(0).answer()).isNull();
}

@Test
@DisplayName("활동 히스토리 조회 - 정상 반환")
void getActivityHistory_success() {
Expand Down
Loading