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 @@ -47,8 +47,8 @@ public void endStudySession(LocalDateTime endTime) {
status = StudyStatus.FINISHED;
this.totalMillis = Duration.between(this.startTime, this.endTime).toMillis();
}
public void endMaxFocusStudySession(int maxFocusTime) {
this.endTime = this.startTime.plusMinutes(maxFocusTime);
public void endMaxFocusStudySession(int maxFocusHours) {
this.endTime = this.startTime.plusHours(maxFocusHours);
status = StudyStatus.FINISHED;
this.totalMillis = Duration.between(this.startTime, this.endTime).toMillis();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public StudySession makeStudySession(Long userId){
@Transactional
public List<User> endExpiredMaxFocusSessions() {
int maxFocusHours = studyProperties.getMaxFocusHours();
LocalDateTime cutoffTime = LocalDateTime.now().minusMinutes(maxFocusHours);
LocalDateTime cutoffTime = LocalDateTime.now().minusHours(maxFocusHours);

List<StudySession> expiredSessions = studySessionRepository.findAllByStatusAndStartTimeBefore(
StudyStatus.STARTED, cutoffTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.gpt.geumpumtabackend.global.exception.ExceptionType;
import com.gpt.geumpumtabackend.study.config.StudyProperties;
import com.gpt.geumpumtabackend.study.domain.StudySession;
import com.gpt.geumpumtabackend.study.domain.StudyStatus;
import com.gpt.geumpumtabackend.study.dto.request.StudyEndRequest;
import com.gpt.geumpumtabackend.study.dto.request.StudyStartRequest;
import com.gpt.geumpumtabackend.study.dto.response.StudyStartResponse;
Expand All @@ -20,11 +21,13 @@
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.*;
Expand Down Expand Up @@ -289,6 +292,60 @@ class EndStudySession {
}

// 테스트 데이터 생성 헬퍼 메서드
@Nested
@DisplayName("max focus 계산")
class MaxFocusCalculation {

@Test
@DisplayName("max focus 종료 시간은 hour 단위로 계산된다")
void max_focus_ends_in_hour_units() {
// Given
LocalDateTime startTime = LocalDateTime.of(2024, 1, 1, 9, 0);
User testUser = createTestUser(1L, "test-user", Department.SOFTWARE);
StudySession session = new StudySession();

// When
session.startStudySession(startTime, testUser);
session.endMaxFocusStudySession(3);

// Then
assertThat(session.getEndTime()).isEqualTo(startTime.plusHours(3));
assertThat(session.getTotalMillis()).isEqualTo(10_800_000L);
assertThat(session.getStatus()).isEqualTo(StudyStatus.FINISHED);
}

@Test
@DisplayName("만료 cutoff 는 3시간 기준으로 계산된다")
void expired_cutoff_uses_three_hours() {
// Given
User testUser = createTestUser(1L, "test-user", Department.SOFTWARE);
StudySession expiredSession = new StudySession();
LocalDateTime sessionStartTime = LocalDateTime.now().minusHours(4);
expiredSession.startStudySession(sessionStartTime, testUser);

given(studyProperties.getMaxFocusHours()).willReturn(3);
given(studySessionRepository.findAllByStatusAndStartTimeBefore(eq(StudyStatus.STARTED), any(LocalDateTime.class)))
.willReturn(List.of(expiredSession));

LocalDateTime beforeCall = LocalDateTime.now();

// When
List<User> usersToNotify = studySessionService.endExpiredMaxFocusSessions();

// Then
LocalDateTime afterCall = LocalDateTime.now();
ArgumentCaptor<LocalDateTime> cutoffTimeCaptor = ArgumentCaptor.forClass(LocalDateTime.class);
verify(studySessionRepository).findAllByStatusAndStartTimeBefore(eq(StudyStatus.STARTED), cutoffTimeCaptor.capture());

assertThat(cutoffTimeCaptor.getValue())
.isBetween(beforeCall.minusHours(3), afterCall.minusHours(3));
assertThat(usersToNotify).containsExactly(testUser);
assertThat(expiredSession.getEndTime()).isEqualTo(sessionStartTime.plusHours(3));
assertThat(expiredSession.getTotalMillis()).isEqualTo(10_800_000L);
assertThat(expiredSession.getStatus()).isEqualTo(StudyStatus.FINISHED);
}
}

private User createTestUser(Long id, String name, Department department) {
User user = User.builder()
.name(name)
Expand Down
Loading