fix: kobo sync only update finished date when one doesn't already exist (#1614)

* fix: only update finished date on kobo sync when an existing date doesn't exist

* chore: add unit tests to confirm this is the case
This commit is contained in:
CounterClops
2025-11-24 07:26:21 -07:00
committed by GitHub
parent a0a08080ed
commit 1dbf214199
2 changed files with 47 additions and 1 deletions
@@ -185,7 +185,9 @@ public class KoboReadingStateService {
if (progressPercent >= finishedThreshold) {
userProgress.setReadStatus(ReadStatus.READ);
userProgress.setDateFinished(Instant.now());
if (userProgress.getDateFinished() == null) {
userProgress.setDateFinished(Instant.now());
}
} else if (progressPercent >= readingThreshold) {
userProgress.setReadStatus(ReadStatus.READING);
} else {
@@ -216,6 +216,50 @@ class KoboReadingStateServiceTest {
assertNotNull(savedProgress.getDateFinished());
}
@Test
@DisplayName("Should not overwrite existing finished date when syncing completed book")
void testSyncKoboProgressToUserBookProgress_PreserveExistingFinishedDate() {
String entitlementId = "100";
testSettings.setProgressMarkAsFinishedThreshold(99f);
Instant originalFinishedDate = Instant.parse("2025-01-15T10:30:00Z");
UserBookProgressEntity existingProgress = new UserBookProgressEntity();
existingProgress.setUser(testUserEntity);
existingProgress.setBook(testBook);
existingProgress.setKoboProgressPercent(99.5f);
existingProgress.setReadStatus(ReadStatus.READ);
existingProgress.setDateFinished(originalFinishedDate);
KoboReadingState.CurrentBookmark bookmark = KoboReadingState.CurrentBookmark.builder()
.progressPercent(100)
.build();
KoboReadingState readingState = KoboReadingState.builder()
.entitlementId(entitlementId)
.currentBookmark(bookmark)
.build();
KoboReadingStateEntity entity = new KoboReadingStateEntity();
when(mapper.toEntity(any())).thenReturn(entity);
when(mapper.toDto(any(KoboReadingStateEntity.class))).thenReturn(readingState);
when(repository.findByEntitlementId(entitlementId)).thenReturn(Optional.empty());
when(repository.save(any())).thenReturn(entity);
when(bookRepository.findById(100L)).thenReturn(Optional.of(testBook));
when(userRepository.findById(1L)).thenReturn(Optional.of(testUserEntity));
when(progressRepository.findByUserIdAndBookId(1L, 100L)).thenReturn(Optional.of(existingProgress));
ArgumentCaptor<UserBookProgressEntity> progressCaptor = ArgumentCaptor.forClass(UserBookProgressEntity.class);
when(progressRepository.save(progressCaptor.capture())).thenReturn(existingProgress);
service.saveReadingState(List.of(readingState));
UserBookProgressEntity savedProgress = progressCaptor.getValue();
assertEquals(100.0f, savedProgress.getKoboProgressPercent());
assertEquals(ReadStatus.READ, savedProgress.getReadStatus());
assertEquals(originalFinishedDate, savedProgress.getDateFinished(),
"Existing finished date should not be overwritten during sync");
}
@Test
@DisplayName("Should mark book as READING when progress exceeds reading threshold")
void testSyncKoboProgressToUserBookProgress_MarkAsReading() {