fix(kobo-sync): use KoboSpan for progress sync (#253)
This commit is contained in:
@@ -73,7 +73,7 @@ class AppBookServiceProgressTest {
|
||||
mockBookWithLibrary(bookId, libraryId);
|
||||
|
||||
UpdateProgressRequest request = new UpdateProgressRequest();
|
||||
request.setFileProgress(new BookFileProgress(1L, "pos", "href", 0.5f, null));
|
||||
request.setFileProgress(new BookFileProgress(1L, "pos", "href", 0.5f, null, null));
|
||||
|
||||
service.updateBookProgress(bookId, request);
|
||||
|
||||
@@ -92,7 +92,7 @@ class AppBookServiceProgressTest {
|
||||
mockBookWithLibrary(bookId, libraryId);
|
||||
|
||||
UpdateProgressRequest request = new UpdateProgressRequest();
|
||||
request.setFileProgress(new BookFileProgress(1L, null, null, 0.75f, null));
|
||||
request.setFileProgress(new BookFileProgress(1L, null, null, 0.75f, null, null));
|
||||
|
||||
service.updateBookProgress(bookId, request);
|
||||
|
||||
@@ -118,7 +118,7 @@ class AppBookServiceProgressTest {
|
||||
mockBookWithLibrary(bookId, libraryId);
|
||||
|
||||
UpdateProgressRequest request = new UpdateProgressRequest();
|
||||
request.setFileProgress(new BookFileProgress(1L, null, null, 0.5f, null));
|
||||
request.setFileProgress(new BookFileProgress(1L, null, null, 0.5f, null, null));
|
||||
|
||||
assertThrows(APIException.class, () -> service.updateBookProgress(bookId, request));
|
||||
verify(bookService, never()).updateReadProgress(any());
|
||||
@@ -130,7 +130,7 @@ class AppBookServiceProgressTest {
|
||||
mockBookWithLibrary(bookId, libraryId);
|
||||
|
||||
UpdateProgressRequest request = new UpdateProgressRequest();
|
||||
request.setFileProgress(new BookFileProgress(1L, null, null, 0.5f, null));
|
||||
request.setFileProgress(new BookFileProgress(1L, null, null, 0.5f, null, null));
|
||||
|
||||
service.updateBookProgress(bookId, request);
|
||||
|
||||
@@ -147,7 +147,7 @@ class AppBookServiceProgressTest {
|
||||
when(bookRepository.findByIdWithBookFiles(bookId)).thenReturn(Optional.empty());
|
||||
|
||||
UpdateProgressRequest request = new UpdateProgressRequest();
|
||||
request.setFileProgress(new BookFileProgress(1L, null, null, 0.5f, null));
|
||||
request.setFileProgress(new BookFileProgress(1L, null, null, 0.5f, null, null));
|
||||
|
||||
assertThrows(APIException.class, () -> service.updateBookProgress(bookId, request));
|
||||
verify(bookService, never()).updateReadProgress(any());
|
||||
|
||||
@@ -25,14 +25,17 @@ import org.booklore.mapper.KoboReadingStateMapper;
|
||||
import org.booklore.model.dto.KoboSyncSettings;
|
||||
import org.booklore.model.dto.kobo.*;
|
||||
import org.booklore.model.entity.KoboReadingStateEntity;
|
||||
import org.booklore.model.entity.UserBookFileProgressEntity;
|
||||
import org.booklore.model.entity.UserBookProgressEntity;
|
||||
import org.booklore.model.enums.KoboReadStatus;
|
||||
import org.booklore.model.enums.ReadStatus;
|
||||
import org.booklore.repository.KoboReadingStateRepository;
|
||||
import org.booklore.repository.UserBookFileProgressRepository;
|
||||
import org.booklore.repository.UserBookProgressRepository;
|
||||
import org.booklore.service.kobo.KoboEntitlementService;
|
||||
import org.booklore.service.kobo.KoboReadingStateBuilder;
|
||||
import org.booklore.service.kobo.KoboSettingsService;
|
||||
import org.booklore.service.kobo.KoboSpanMapService;
|
||||
import org.booklore.service.opds.MagicShelfBookService;
|
||||
import org.booklore.util.kobo.KoboUrlBuilder;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -55,6 +58,7 @@ import java.time.ZoneOffset;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -93,6 +97,9 @@ class KoboEntitlementServiceTest {
|
||||
@Mock
|
||||
private UserBookProgressRepository progressRepository;
|
||||
|
||||
@Mock
|
||||
private UserBookFileProgressRepository fileProgressRepository;
|
||||
|
||||
@Mock
|
||||
private KoboReadingStateRepository readingStateRepository;
|
||||
|
||||
@@ -105,6 +112,9 @@ class KoboEntitlementServiceTest {
|
||||
@Mock
|
||||
private KoboSettingsService koboSettingsService;
|
||||
|
||||
@Mock
|
||||
private KoboSpanMapService koboSpanMapService;
|
||||
|
||||
@InjectMocks
|
||||
private KoboEntitlementService koboEntitlementService;
|
||||
|
||||
@@ -113,7 +123,9 @@ class KoboEntitlementServiceTest {
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
BookLoreUser.UserPermissions permissions = new BookLoreUser.UserPermissions();
|
||||
user = BookLoreUser.builder().permissions(permissions).build();
|
||||
user = BookLoreUser.builder().id(1L).permissions(permissions).build();
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
|
||||
when(koboSpanMapService.getValidMaps(anyMap())).thenReturn(Map.of());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -180,6 +192,28 @@ class KoboEntitlementServiceTest {
|
||||
assertEquals(KoboBookFormat.EPUB3.toString(), result.getDownloadUrls().getFirst().getFormat());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapToKoboMetadata_epubBookShouldReturnKepubFormatEvenWhenToggleIsOff() {
|
||||
long bookId = 1L;
|
||||
BookEntity epubBook = createEpubBookEntity(bookId);
|
||||
String token = "test-token";
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(bookId)))
|
||||
.thenReturn(List.of(epubBook));
|
||||
when(koboCompatibilityService.isBookSupportedForKobo(epubBook))
|
||||
.thenReturn(true);
|
||||
when(koboUrlBuilder.downloadUrl(token, epubBook.getId()))
|
||||
.thenReturn("http://test.com/download/" + epubBook.getId());
|
||||
when(appSettingService.getAppSettings())
|
||||
.thenReturn(createAppSettingsWithKoboSettings());
|
||||
|
||||
KoboBookMetadata result = koboEntitlementService.getMetadataForBook(bookId, token);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.getDownloadUrls().size());
|
||||
assertEquals(KoboBookFormat.KEPUB.toString(), result.getDownloadUrls().getFirst().getFormat());
|
||||
}
|
||||
|
||||
private BookEntity createCbxBookEntity(Long id) {
|
||||
BookEntity book = new BookEntity();
|
||||
book.setId(id);
|
||||
@@ -204,7 +238,6 @@ class KoboEntitlementServiceTest {
|
||||
KoboSettings koboSettings = KoboSettings.builder()
|
||||
.convertCbxToEpub(true)
|
||||
.conversionLimitInMbForCbx(50)
|
||||
.convertToKepub(false)
|
||||
.conversionLimitInMb(50)
|
||||
.build();
|
||||
appSettings.setKoboSettings(koboSettings);
|
||||
@@ -506,6 +539,127 @@ class KoboEntitlementServiceTest {
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should prefer href-only web reader bookmark over existing state when two-way sync is on")
|
||||
void generateNewEntitlements_prefersHrefOnlyWebReaderBookmark() {
|
||||
BookEntity book = createEpubBookEntity(1L);
|
||||
book.setAddedOn(Instant.parse("2025-01-01T00:00:00Z"));
|
||||
book.getPrimaryBookFile().setId(10L);
|
||||
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setBook(book);
|
||||
progress.setEpubProgressHref("OPS/chapter3.xhtml");
|
||||
progress.setEpubProgressPercent(54.6f);
|
||||
|
||||
KoboReadingState.CurrentBookmark existingBookmark = KoboReadingState.CurrentBookmark.builder()
|
||||
.progressPercent(12)
|
||||
.build();
|
||||
KoboReadingState existingState = KoboReadingState.builder()
|
||||
.entitlementId("1")
|
||||
.currentBookmark(existingBookmark)
|
||||
.build();
|
||||
KoboReadingStateEntity existingEntity = new KoboReadingStateEntity();
|
||||
|
||||
KoboReadingState.CurrentBookmark webReaderBookmark = KoboReadingState.CurrentBookmark.builder()
|
||||
.progressPercent(55)
|
||||
.build();
|
||||
|
||||
KoboSyncSettings settings = new KoboSyncSettings();
|
||||
settings.setTwoWayProgressSync(true);
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(1L))).thenReturn(List.of(book));
|
||||
when(koboCompatibilityService.isBookSupportedForKobo(book)).thenReturn(true);
|
||||
when(koboUrlBuilder.downloadUrl("token1", 1L)).thenReturn("http://test.com/download/1");
|
||||
when(appSettingService.getAppSettings()).thenReturn(createAppSettingsWithKoboSettings());
|
||||
when(koboSettingsService.getCurrentUserSettings()).thenReturn(settings);
|
||||
when(progressRepository.findByUserIdAndBookId(user.getId(), 1L)).thenReturn(Optional.of(progress));
|
||||
when(fileProgressRepository.findByUserIdAndBookFileId(user.getId(), 10L)).thenReturn(Optional.empty());
|
||||
when(readingStateRepository.findByEntitlementIdAndUserId("1", user.getId()))
|
||||
.thenReturn(Optional.of(existingEntity));
|
||||
when(readingStateMapper.toDto(existingEntity)).thenReturn(existingState);
|
||||
when(readingStateBuilder.shouldUseWebReaderProgress(progress)).thenReturn(true);
|
||||
when(readingStateBuilder.buildBookmarkFromProgress(eq(progress), isNull(), any(OffsetDateTime.class)))
|
||||
.thenReturn(webReaderBookmark);
|
||||
when(readingStateBuilder.buildStatusInfoFromProgress(eq(progress), anyString()))
|
||||
.thenReturn(KoboReadingState.StatusInfo.builder()
|
||||
.status(KoboReadStatus.READING)
|
||||
.timesStartedReading(1)
|
||||
.build());
|
||||
|
||||
List<NewEntitlement> result = koboEntitlementService.generateNewEntitlements(Set.of(1L), "token1");
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(55, result.getFirst().getNewEntitlement().getReadingState().getCurrentBookmark().getProgressPercent());
|
||||
verify(readingStateBuilder).buildBookmarkFromProgress(eq(progress), isNull(), any(OffsetDateTime.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should keep stored Kobo bookmark when mirrored EPUB progress is not newer")
|
||||
void generateNewEntitlements_preservesStoredKoboBookmarkWhenKoboIsFreshest() {
|
||||
BookEntity book = createEpubBookEntity(1L);
|
||||
book.setAddedOn(Instant.parse("2025-01-01T00:00:00Z"));
|
||||
book.getPrimaryBookFile().setId(10L);
|
||||
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setBook(book);
|
||||
progress.setKoboProgressPercent(55f);
|
||||
progress.setKoboLocation("kobo.12.18");
|
||||
progress.setKoboLocationType("KoboSpan");
|
||||
progress.setKoboLocationSource("OPS/chapter3.xhtml");
|
||||
progress.setKoboProgressReceivedTime(Instant.parse("2025-06-15T12:00:00Z"));
|
||||
progress.setEpubProgressPercent(55f);
|
||||
progress.setLastReadTime(Instant.parse("2025-06-15T12:00:00Z"));
|
||||
progress.setReadStatus(ReadStatus.READING);
|
||||
|
||||
KoboReadingState.CurrentBookmark existingBookmark = KoboReadingState.CurrentBookmark.builder()
|
||||
.progressPercent(55)
|
||||
.contentSourceProgressPercent(23)
|
||||
.location(KoboReadingState.CurrentBookmark.Location.builder()
|
||||
.value("kobo.12.18")
|
||||
.type("KoboSpan")
|
||||
.source("OPS/chapter3.xhtml")
|
||||
.build())
|
||||
.lastModified("2025-06-15T12:00:00Z")
|
||||
.build();
|
||||
KoboReadingState existingState = KoboReadingState.builder()
|
||||
.entitlementId("1")
|
||||
.currentBookmark(existingBookmark)
|
||||
.build();
|
||||
KoboReadingStateEntity existingEntity = new KoboReadingStateEntity();
|
||||
|
||||
KoboSyncSettings settings = new KoboSyncSettings();
|
||||
settings.setTwoWayProgressSync(true);
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(1L))).thenReturn(List.of(book));
|
||||
when(koboCompatibilityService.isBookSupportedForKobo(book)).thenReturn(true);
|
||||
when(koboUrlBuilder.downloadUrl("token1", 1L)).thenReturn("http://test.com/download/1");
|
||||
when(appSettingService.getAppSettings()).thenReturn(createAppSettingsWithKoboSettings());
|
||||
when(koboSettingsService.getCurrentUserSettings()).thenReturn(settings);
|
||||
when(progressRepository.findByUserIdAndBookId(user.getId(), 1L)).thenReturn(Optional.of(progress));
|
||||
when(fileProgressRepository.findByUserIdAndBookFileId(user.getId(), 10L)).thenReturn(Optional.empty());
|
||||
when(readingStateRepository.findByEntitlementIdAndUserId("1", user.getId()))
|
||||
.thenReturn(Optional.of(existingEntity));
|
||||
when(readingStateMapper.toDto(existingEntity)).thenReturn(existingState);
|
||||
when(readingStateBuilder.shouldUseWebReaderProgress(progress)).thenReturn(false);
|
||||
when(readingStateBuilder.buildStatusInfoFromProgress(eq(progress), anyString()))
|
||||
.thenReturn(KoboReadingState.StatusInfo.builder()
|
||||
.status(KoboReadStatus.READING)
|
||||
.timesStartedReading(1)
|
||||
.build());
|
||||
|
||||
List<NewEntitlement> result = koboEntitlementService.generateNewEntitlements(Set.of(1L), "token1");
|
||||
|
||||
assertEquals(1, result.size());
|
||||
KoboReadingState.CurrentBookmark bookmark = result.getFirst().getNewEntitlement().getReadingState().getCurrentBookmark();
|
||||
assertEquals(55, bookmark.getProgressPercent());
|
||||
assertEquals(23, bookmark.getContentSourceProgressPercent());
|
||||
assertNotNull(bookmark.getLocation());
|
||||
assertEquals("kobo.12.18", bookmark.getLocation().getValue());
|
||||
assertEquals("OPS/chapter3.xhtml", bookmark.getLocation().getSource());
|
||||
verify(readingStateBuilder).shouldUseWebReaderProgress(progress);
|
||||
verify(readingStateBuilder, never()).buildBookmarkFromProgress(eq(progress), isNull(), any(OffsetDateTime.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should handle empty book IDs")
|
||||
void generateNewEntitlements_emptyIds() {
|
||||
@@ -563,6 +717,11 @@ class KoboEntitlementServiceTest {
|
||||
void generateChangedReadingStates_fromProgress() {
|
||||
BookEntity book = new BookEntity();
|
||||
book.setId(1L);
|
||||
BookFileEntity primaryFile = new BookFileEntity();
|
||||
primaryFile.setId(10L);
|
||||
primaryFile.setBook(book);
|
||||
primaryFile.setBookType(BookFileType.EPUB);
|
||||
book.setBookFiles(List.of(primaryFile));
|
||||
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setBook(book);
|
||||
@@ -570,12 +729,19 @@ class KoboEntitlementServiceTest {
|
||||
progress.setReadStatus(ReadStatus.READING);
|
||||
|
||||
KoboSyncSettings settings = new KoboSyncSettings();
|
||||
settings.setTwoWayProgressSync(true);
|
||||
when(koboSettingsService.getCurrentUserSettings()).thenReturn(settings);
|
||||
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setBookFile(primaryFile);
|
||||
fileProgress.setContentSourceProgressPercent(20f);
|
||||
when(fileProgressRepository.findByUserIdAndBookFileIdIn(eq(1L), any())).thenReturn(List.of(fileProgress));
|
||||
|
||||
KoboReadingState.CurrentBookmark bookmark = KoboReadingState.CurrentBookmark.builder()
|
||||
.progressPercent(50)
|
||||
.contentSourceProgressPercent(20)
|
||||
.build();
|
||||
when(readingStateBuilder.buildBookmarkFromProgress(eq(progress), any(OffsetDateTime.class)))
|
||||
when(readingStateBuilder.buildBookmarkFromProgress(eq(progress), eq(fileProgress), any(OffsetDateTime.class), anyMap()))
|
||||
.thenReturn(bookmark);
|
||||
when(readingStateBuilder.buildStatusInfoFromProgress(eq(progress), anyString()))
|
||||
.thenReturn(KoboReadingState.StatusInfo.builder()
|
||||
@@ -590,9 +756,68 @@ class KoboEntitlementServiceTest {
|
||||
KoboReadingState state = result.getFirst().getChangedReadingState().getReadingState();
|
||||
assertEquals("1", state.getEntitlementId());
|
||||
assertNotNull(state.getCurrentBookmark());
|
||||
assertEquals(20, state.getCurrentBookmark().getContentSourceProgressPercent());
|
||||
assertNotNull(state.getStatusInfo());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should use synced EPUB file progress when book has multiple files")
|
||||
void generateChangedReadingStates_usesSyncedEpubFileProgress() {
|
||||
BookEntity book = new BookEntity();
|
||||
book.setId(1L);
|
||||
|
||||
BookFileEntity primaryEpub = new BookFileEntity();
|
||||
primaryEpub.setId(10L);
|
||||
primaryEpub.setBook(book);
|
||||
primaryEpub.setBookType(BookFileType.EPUB);
|
||||
|
||||
BookFileEntity alternateAudiobook = new BookFileEntity();
|
||||
alternateAudiobook.setId(20L);
|
||||
alternateAudiobook.setBook(book);
|
||||
alternateAudiobook.setBookType(BookFileType.AUDIOBOOK);
|
||||
|
||||
book.setBookFiles(List.of(primaryEpub, alternateAudiobook));
|
||||
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setBook(book);
|
||||
progress.setEpubProgressPercent(70f);
|
||||
progress.setReadStatus(ReadStatus.READING);
|
||||
|
||||
KoboSyncSettings settings = new KoboSyncSettings();
|
||||
settings.setTwoWayProgressSync(true);
|
||||
when(koboSettingsService.getCurrentUserSettings()).thenReturn(settings);
|
||||
|
||||
UserBookFileProgressEntity epubFileProgress = new UserBookFileProgressEntity();
|
||||
epubFileProgress.setBookFile(primaryEpub);
|
||||
epubFileProgress.setContentSourceProgressPercent(21f);
|
||||
|
||||
UserBookFileProgressEntity audiobookProgress = new UserBookFileProgressEntity();
|
||||
audiobookProgress.setBookFile(alternateAudiobook);
|
||||
audiobookProgress.setContentSourceProgressPercent(99f);
|
||||
|
||||
when(fileProgressRepository.findByUserIdAndBookFileIdIn(eq(1L), any()))
|
||||
.thenReturn(List.of(epubFileProgress, audiobookProgress));
|
||||
|
||||
KoboReadingState.CurrentBookmark bookmark = KoboReadingState.CurrentBookmark.builder()
|
||||
.progressPercent(70)
|
||||
.contentSourceProgressPercent(21)
|
||||
.build();
|
||||
when(readingStateBuilder.buildBookmarkFromProgress(eq(progress), eq(epubFileProgress), any(OffsetDateTime.class), anyMap()))
|
||||
.thenReturn(bookmark);
|
||||
when(readingStateBuilder.buildStatusInfoFromProgress(eq(progress), anyString()))
|
||||
.thenReturn(KoboReadingState.StatusInfo.builder()
|
||||
.status(KoboReadStatus.READING)
|
||||
.timesStartedReading(1)
|
||||
.build());
|
||||
|
||||
List<ChangedReadingState> result = koboEntitlementService.generateChangedReadingStates(List.of(progress));
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(21, result.getFirst().getChangedReadingState().getReadingState()
|
||||
.getCurrentBookmark().getContentSourceProgressPercent());
|
||||
verify(readingStateBuilder).buildBookmarkFromProgress(eq(progress), eq(epubFileProgress), any(OffsetDateTime.class), anyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should use empty bookmark when no progress data exists")
|
||||
void generateChangedReadingStates_emptyBookmark() {
|
||||
@@ -637,7 +862,7 @@ class KoboEntitlementServiceTest {
|
||||
KoboReadingState.CurrentBookmark bookmark = KoboReadingState.CurrentBookmark.builder()
|
||||
.progressPercent(70)
|
||||
.build();
|
||||
when(readingStateBuilder.buildBookmarkFromProgress(eq(progress), any(OffsetDateTime.class)))
|
||||
when(readingStateBuilder.buildBookmarkFromProgress(eq(progress), isNull(), any(OffsetDateTime.class), anyMap()))
|
||||
.thenReturn(bookmark);
|
||||
when(readingStateBuilder.buildStatusInfoFromProgress(eq(progress), anyString()))
|
||||
.thenReturn(KoboReadingState.StatusInfo.builder()
|
||||
@@ -682,4 +907,4 @@ class KoboEntitlementServiceTest {
|
||||
verify(readingStateBuilder).buildEmptyBookmark(any(OffsetDateTime.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,11 @@ package org.booklore.service;
|
||||
import org.booklore.model.dto.KoboSyncSettings;
|
||||
import org.booklore.model.dto.kobo.KoboReadingState;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.UserBookFileProgressEntity;
|
||||
import org.booklore.model.entity.UserBookProgressEntity;
|
||||
import org.booklore.model.enums.KoboReadStatus;
|
||||
import org.booklore.model.enums.ReadStatus;
|
||||
import org.booklore.service.kobo.KoboBookmarkLocationResolver;
|
||||
import org.booklore.service.kobo.KoboReadingStateBuilder;
|
||||
import org.booklore.service.kobo.KoboSettingsService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -27,6 +29,7 @@ import java.time.ZoneOffset;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@DisplayName("KoboReadingStateBuilder Tests")
|
||||
@@ -37,6 +40,9 @@ class KoboReadingStateBuilderTest {
|
||||
@Mock
|
||||
private KoboSettingsService koboSettingsService;
|
||||
|
||||
@Mock
|
||||
private KoboBookmarkLocationResolver bookmarkLocationResolver;
|
||||
|
||||
private KoboReadingStateBuilder builder;
|
||||
|
||||
@BeforeEach
|
||||
@@ -44,7 +50,8 @@ class KoboReadingStateBuilderTest {
|
||||
KoboSyncSettings settings = new KoboSyncSettings();
|
||||
settings.setTwoWayProgressSync(true);
|
||||
when(koboSettingsService.getCurrentUserSettings()).thenReturn(settings);
|
||||
builder = new KoboReadingStateBuilder(koboSettingsService);
|
||||
when(bookmarkLocationResolver.resolve(any(), any(), any())).thenReturn(java.util.Optional.empty());
|
||||
builder = new KoboReadingStateBuilder(koboSettingsService, bookmarkLocationResolver);
|
||||
}
|
||||
|
||||
@Nested
|
||||
@@ -186,19 +193,19 @@ class KoboReadingStateBuilderTest {
|
||||
void buildBookmarkFromProgress_WithLocation() {
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setKoboProgressPercent(75.5f);
|
||||
progress.setKoboLocation("epubcfi(/6/4[chap01ref]!/4/2/1:3)");
|
||||
progress.setKoboLocationType("EpubCfi");
|
||||
progress.setKoboLocationSource("Kobo");
|
||||
progress.setKoboLocation("kobo.12.18");
|
||||
progress.setKoboLocationType("KoboSpan");
|
||||
progress.setKoboLocationSource("OEBPS/chapter3.xhtml");
|
||||
progress.setKoboProgressReceivedTime(Instant.parse("2025-11-26T10:00:00Z"));
|
||||
|
||||
KoboReadingState.CurrentBookmark bookmark = builder.buildBookmarkFromProgress(progress);
|
||||
KoboReadingState.CurrentBookmark bookmark = builder.buildBookmarkFromProgress(progress, (UserBookFileProgressEntity) null);
|
||||
|
||||
assertNotNull(bookmark);
|
||||
assertEquals(76, bookmark.getProgressPercent()); // Rounded
|
||||
assertNotNull(bookmark.getLocation());
|
||||
assertEquals("epubcfi(/6/4[chap01ref]!/4/2/1:3)", bookmark.getLocation().getValue());
|
||||
assertEquals("EpubCfi", bookmark.getLocation().getType());
|
||||
assertEquals("Kobo", bookmark.getLocation().getSource());
|
||||
assertEquals("kobo.12.18", bookmark.getLocation().getValue());
|
||||
assertEquals("KoboSpan", bookmark.getLocation().getType());
|
||||
assertEquals("OEBPS/chapter3.xhtml", bookmark.getLocation().getSource());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -209,13 +216,161 @@ class KoboReadingStateBuilderTest {
|
||||
progress.setKoboLocation(null);
|
||||
progress.setKoboProgressReceivedTime(Instant.now());
|
||||
|
||||
KoboReadingState.CurrentBookmark bookmark = builder.buildBookmarkFromProgress(progress);
|
||||
KoboReadingState.CurrentBookmark bookmark = builder.buildBookmarkFromProgress(progress, (UserBookFileProgressEntity) null);
|
||||
|
||||
assertNotNull(bookmark);
|
||||
assertEquals(50, bookmark.getProgressPercent());
|
||||
assertNull(bookmark.getLocation());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should carry file content progress into Kobo bookmark")
|
||||
void buildBookmarkFromProgress_KoboBookmarkReusesFileContentProgress() {
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setKoboProgressPercent(44f);
|
||||
progress.setKoboProgressReceivedTime(Instant.parse("2025-11-26T10:00:00Z"));
|
||||
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setContentSourceProgressPercent(18.6f);
|
||||
|
||||
KoboReadingState.CurrentBookmark bookmark = builder.buildBookmarkFromProgress(progress, fileProgress);
|
||||
|
||||
assertNotNull(bookmark);
|
||||
assertEquals(44, bookmark.getProgressPercent());
|
||||
assertEquals(19, bookmark.getContentSourceProgressPercent());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should build web reader bookmark without a fallback location when no KoboSpan resolves")
|
||||
void buildBookmarkFromProgress_WebReaderLocation() {
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setEpubProgress("epubcfi(/6/4!/4/2/6/1:1)");
|
||||
progress.setEpubProgressHref("OPS/chapter1.xhtml");
|
||||
progress.setEpubProgressPercent(54.6f);
|
||||
progress.setLastReadTime(Instant.parse("2025-11-26T10:00:00Z"));
|
||||
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setPositionData("epubcfi(/6/8!/4/2/6/1:15)");
|
||||
fileProgress.setPositionHref("OPS/chapter3.xhtml");
|
||||
fileProgress.setContentSourceProgressPercent(18.6f);
|
||||
|
||||
KoboReadingState.CurrentBookmark bookmark = builder.buildBookmarkFromProgress(progress, fileProgress);
|
||||
|
||||
assertNotNull(bookmark);
|
||||
assertEquals(55, bookmark.getProgressPercent());
|
||||
assertEquals(19, bookmark.getContentSourceProgressPercent());
|
||||
assertNull(bookmark.getLocation());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should build web reader bookmark with resolved KoboSpan location")
|
||||
void buildBookmarkFromProgress_WebReaderResolvedKoboSpanLocation() {
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setEpubProgress("epubcfi(/6/4!/4/2/6/1:1)");
|
||||
progress.setEpubProgressHref("OPS/chapter3.xhtml");
|
||||
progress.setEpubProgressPercent(54.6f);
|
||||
progress.setLastReadTime(Instant.parse("2025-11-26T10:00:00Z"));
|
||||
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setPositionData("epubcfi(/6/8!/4/2/6/1:15)");
|
||||
fileProgress.setPositionHref("OPS/chapter3.xhtml");
|
||||
|
||||
when(bookmarkLocationResolver.resolve(progress, fileProgress, null))
|
||||
.thenReturn(java.util.Optional.of(
|
||||
new KoboBookmarkLocationResolver.ResolvedBookmarkLocation(
|
||||
"kobo.12.18",
|
||||
"KoboSpan",
|
||||
"OEBPS/OPS/chapter3.xhtml",
|
||||
18.6f)));
|
||||
|
||||
KoboReadingState.CurrentBookmark bookmark = builder.buildBookmarkFromProgress(progress, fileProgress);
|
||||
|
||||
assertNotNull(bookmark);
|
||||
assertEquals(55, bookmark.getProgressPercent());
|
||||
assertEquals(19, bookmark.getContentSourceProgressPercent());
|
||||
assertNotNull(bookmark.getLocation());
|
||||
assertEquals("kobo.12.18", bookmark.getLocation().getValue());
|
||||
assertEquals("KoboSpan", bookmark.getLocation().getType());
|
||||
assertEquals("OEBPS/OPS/chapter3.xhtml", bookmark.getLocation().getSource());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should prefer web reader bookmark when Kobo bookmark timestamp is older")
|
||||
void buildBookmarkFromProgress_PrefersWebReaderWhenKoboTimestampIsOlder() {
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setEpubProgress("epubcfi(/6/4!/4/2/6/1:1)");
|
||||
progress.setEpubProgressHref("OPS/chapter3.xhtml");
|
||||
progress.setEpubProgressPercent(54.6f);
|
||||
progress.setLastReadTime(Instant.parse("2025-11-26T10:00:00Z"));
|
||||
progress.setKoboProgressPercent(12f);
|
||||
progress.setKoboLocation("kobo.1.1");
|
||||
progress.setKoboLocationType("KoboSpan");
|
||||
progress.setKoboLocationSource("OPS/chapter1.xhtml");
|
||||
progress.setKoboProgressReceivedTime(Instant.parse("2025-11-26T09:00:00Z"));
|
||||
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setPositionData("epubcfi(/6/8!/4/2/6/1:15)");
|
||||
fileProgress.setPositionHref("OPS/chapter3.xhtml");
|
||||
|
||||
KoboReadingState.CurrentBookmark bookmark = builder.buildBookmarkFromProgress(progress, fileProgress);
|
||||
|
||||
assertNotNull(bookmark);
|
||||
assertEquals(55, bookmark.getProgressPercent());
|
||||
assertNull(bookmark.getLocation());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should keep inbound Kobo bookmark when mirrored EPUB progress has the same timestamp")
|
||||
void buildBookmarkFromProgress_PrefersKoboWhenTimestampsAreEqual() {
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setEpubProgressPercent(54.6f);
|
||||
progress.setLastReadTime(Instant.parse("2025-11-26T10:00:00Z"));
|
||||
progress.setKoboProgressPercent(12f);
|
||||
progress.setKoboLocation("kobo.1.1");
|
||||
progress.setKoboLocationType("KoboSpan");
|
||||
progress.setKoboLocationSource("OPS/chapter1.xhtml");
|
||||
progress.setKoboProgressReceivedTime(Instant.parse("2025-11-26T10:00:00Z"));
|
||||
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setPositionHref("OPS/chapter3.xhtml");
|
||||
fileProgress.setContentSourceProgressPercent(18.6f);
|
||||
|
||||
KoboReadingState.CurrentBookmark bookmark = builder.buildBookmarkFromProgress(progress, fileProgress);
|
||||
|
||||
assertNotNull(bookmark);
|
||||
assertEquals(12, bookmark.getProgressPercent());
|
||||
assertEquals(19, bookmark.getContentSourceProgressPercent());
|
||||
assertNotNull(bookmark.getLocation());
|
||||
assertEquals("kobo.1.1", bookmark.getLocation().getValue());
|
||||
assertEquals("KoboSpan", bookmark.getLocation().getType());
|
||||
assertEquals("OPS/chapter1.xhtml", bookmark.getLocation().getSource());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should prefer web reader bookmark when newer progress has href and percent but no CFI")
|
||||
void buildBookmarkFromProgress_UsesWebReaderWithoutCfi() {
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setEpubProgressHref("OPS/chapter3.xhtml");
|
||||
progress.setEpubProgressPercent(54.6f);
|
||||
progress.setLastReadTime(Instant.parse("2025-11-26T10:00:00Z"));
|
||||
progress.setKoboProgressPercent(12f);
|
||||
progress.setKoboLocation("kobo.1.1");
|
||||
progress.setKoboLocationType("KoboSpan");
|
||||
progress.setKoboLocationSource("OPS/chapter1.xhtml");
|
||||
progress.setKoboProgressReceivedTime(Instant.parse("2025-11-26T09:00:00Z"));
|
||||
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setPositionHref("OPS/chapter3.xhtml");
|
||||
fileProgress.setContentSourceProgressPercent(18.6f);
|
||||
|
||||
KoboReadingState.CurrentBookmark bookmark = builder.buildBookmarkFromProgress(progress, fileProgress);
|
||||
|
||||
assertNotNull(bookmark);
|
||||
assertEquals(55, bookmark.getProgressPercent());
|
||||
assertEquals(19, bookmark.getContentSourceProgressPercent());
|
||||
assertNull(bookmark.getLocation());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should use default time when progress received time is null")
|
||||
void buildBookmarkFromProgress_UseDefaultTime() {
|
||||
@@ -224,7 +379,7 @@ class KoboReadingStateBuilderTest {
|
||||
progress.setKoboProgressReceivedTime(null);
|
||||
|
||||
OffsetDateTime defaultTime = OffsetDateTime.parse("2025-11-26T12:00:00Z");
|
||||
KoboReadingState.CurrentBookmark bookmark = builder.buildBookmarkFromProgress(progress, defaultTime);
|
||||
KoboReadingState.CurrentBookmark bookmark = builder.buildBookmarkFromProgress(progress, null, defaultTime);
|
||||
|
||||
assertNotNull(bookmark);
|
||||
assertEquals(defaultTime.toString(), bookmark.getLastModified());
|
||||
@@ -237,7 +392,7 @@ class KoboReadingStateBuilderTest {
|
||||
progress.setKoboProgressPercent(33.7f);
|
||||
progress.setKoboProgressReceivedTime(Instant.now());
|
||||
|
||||
KoboReadingState.CurrentBookmark bookmark = builder.buildBookmarkFromProgress(progress);
|
||||
KoboReadingState.CurrentBookmark bookmark = builder.buildBookmarkFromProgress(progress, (UserBookFileProgressEntity) null);
|
||||
|
||||
assertEquals(34, bookmark.getProgressPercent()); // Rounded up
|
||||
}
|
||||
@@ -256,13 +411,13 @@ class KoboReadingStateBuilderTest {
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setBook(book);
|
||||
progress.setKoboProgressPercent(75f);
|
||||
progress.setKoboLocation("epubcfi(/6/4!)");
|
||||
progress.setKoboLocationType("EpubCfi");
|
||||
progress.setKoboLocationSource("Kobo");
|
||||
progress.setKoboLocation("kobo.1.1");
|
||||
progress.setKoboLocationType("KoboSpan");
|
||||
progress.setKoboLocationSource("OEBPS/CoverImage.xhtml");
|
||||
progress.setKoboProgressReceivedTime(Instant.parse("2025-11-26T10:00:00Z"));
|
||||
progress.setReadStatus(ReadStatus.READING);
|
||||
|
||||
KoboReadingState state = builder.buildReadingStateFromProgress("100", progress);
|
||||
KoboReadingState state = builder.buildReadingStateFromProgress("100", progress, null);
|
||||
|
||||
assertNotNull(state);
|
||||
assertEquals("100", state.getEntitlementId());
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.BookLoreUserEntity;
|
||||
import org.booklore.model.entity.KoboReadingStateEntity;
|
||||
import org.booklore.model.entity.UserBookProgressEntity;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.booklore.model.enums.KoboReadStatus;
|
||||
import org.booklore.model.enums.ReadStatus;
|
||||
import org.booklore.repository.BookRepository;
|
||||
@@ -112,6 +113,15 @@ class KoboReadingStateServiceTest {
|
||||
lenient().when(mapper.cleanString(any())).thenCallRealMethod();
|
||||
}
|
||||
|
||||
private BookFileEntity setPrimaryEpub(Long bookFileId) {
|
||||
BookFileEntity primaryFile = new BookFileEntity();
|
||||
primaryFile.setId(bookFileId);
|
||||
primaryFile.setBook(testBook);
|
||||
primaryFile.setBookType(BookFileType.EPUB);
|
||||
testBook.setBookFiles(List.of(primaryFile));
|
||||
return primaryFile;
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should not overwrite existing finished date when syncing completed book")
|
||||
void testSyncKoboProgressToUserBookProgress_PreserveExistingFinishedDate() {
|
||||
@@ -248,9 +258,9 @@ class KoboReadingStateServiceTest {
|
||||
String entitlementId = "100";
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setKoboProgressPercent(75.5f);
|
||||
progress.setKoboLocation("epubcfi(/6/4[chap01ref]!/4/2/1:3)");
|
||||
progress.setKoboLocationType("EpubCfi");
|
||||
progress.setKoboLocationSource("Kobo");
|
||||
progress.setKoboLocation("kobo.12.18");
|
||||
progress.setKoboLocationType("KoboSpan");
|
||||
progress.setKoboLocationSource("OEBPS/chapter3.xhtml");
|
||||
progress.setKoboProgressReceivedTime(Instant.now());
|
||||
|
||||
KoboReadingState expectedState = KoboReadingState.builder()
|
||||
@@ -258,9 +268,9 @@ class KoboReadingStateServiceTest {
|
||||
.currentBookmark(KoboReadingState.CurrentBookmark.builder()
|
||||
.progressPercent(75)
|
||||
.location(KoboReadingState.CurrentBookmark.Location.builder()
|
||||
.value("epubcfi(/6/4[chap01ref]!/4/2/1:3)")
|
||||
.type("EpubCfi")
|
||||
.source("Kobo")
|
||||
.value("kobo.12.18")
|
||||
.type("KoboSpan")
|
||||
.source("OEBPS/chapter3.xhtml")
|
||||
.build())
|
||||
.build())
|
||||
.build();
|
||||
@@ -268,25 +278,26 @@ class KoboReadingStateServiceTest {
|
||||
when(repository.findByEntitlementIdAndUserId(entitlementId, 1L)).thenReturn(Optional.empty());
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(testUser);
|
||||
when(progressRepository.findByUserIdAndBookId(1L, 100L)).thenReturn(Optional.of(progress));
|
||||
when(readingStateBuilder.buildReadingStateFromProgress(entitlementId, progress)).thenReturn(expectedState);
|
||||
when(bookRepository.findById(100L)).thenReturn(Optional.of(testBook));
|
||||
when(readingStateBuilder.buildReadingStateFromProgress(eq(entitlementId), eq(progress), isNull())).thenReturn(expectedState);
|
||||
|
||||
List<KoboReadingState> result = service.getReadingState(entitlementId);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.size());
|
||||
|
||||
|
||||
KoboReadingState state = result.getFirst();
|
||||
assertEquals(entitlementId, state.getEntitlementId());
|
||||
assertNotNull(state.getCurrentBookmark());
|
||||
assertEquals(75, state.getCurrentBookmark().getProgressPercent());
|
||||
assertNotNull(state.getCurrentBookmark().getLocation());
|
||||
assertEquals("epubcfi(/6/4[chap01ref]!/4/2/1:3)", state.getCurrentBookmark().getLocation().getValue());
|
||||
assertEquals("EpubCfi", state.getCurrentBookmark().getLocation().getType());
|
||||
assertEquals("Kobo", state.getCurrentBookmark().getLocation().getSource());
|
||||
|
||||
assertEquals("kobo.12.18", state.getCurrentBookmark().getLocation().getValue());
|
||||
assertEquals("KoboSpan", state.getCurrentBookmark().getLocation().getType());
|
||||
assertEquals("OEBPS/chapter3.xhtml", state.getCurrentBookmark().getLocation().getSource());
|
||||
|
||||
verify(repository).findByEntitlementIdAndUserId(entitlementId, 1L);
|
||||
verify(progressRepository, atLeastOnce()).findByUserIdAndBookId(1L, 100L);
|
||||
verify(readingStateBuilder).buildReadingStateFromProgress(entitlementId, progress);
|
||||
verify(readingStateBuilder).buildReadingStateFromProgress(eq(entitlementId), eq(progress), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -369,7 +380,7 @@ class KoboReadingStateServiceTest {
|
||||
|
||||
UserBookProgressEntity savedProgress = progressCaptor.getValue();
|
||||
assertNull(savedProgress.getKoboProgressPercent());
|
||||
assertNotNull(savedProgress.getKoboProgressReceivedTime());
|
||||
assertNull(savedProgress.getKoboProgressReceivedTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -556,6 +567,7 @@ class KoboReadingStateServiceTest {
|
||||
void testGetReadingState_overlayWebReaderBookmark() {
|
||||
testSettings.setTwoWayProgressSync(true);
|
||||
String entitlementId = "100";
|
||||
BookFileEntity primaryFile = setPrimaryEpub(10L);
|
||||
|
||||
KoboReadingState existingState = KoboReadingState.builder()
|
||||
.entitlementId(entitlementId)
|
||||
@@ -563,9 +575,9 @@ class KoboReadingStateServiceTest {
|
||||
.progressPercent(30)
|
||||
.contentSourceProgressPercent(45)
|
||||
.location(KoboReadingState.CurrentBookmark.Location.builder()
|
||||
.value("epubcfi(/6/10)")
|
||||
.type("EpubCfi")
|
||||
.source("Kobo")
|
||||
.value("kobo.1.1")
|
||||
.type("KoboSpan")
|
||||
.source("OEBPS/CoverImage.xhtml")
|
||||
.build())
|
||||
.lastModified("2025-01-01T00:00:00Z")
|
||||
.build())
|
||||
@@ -580,21 +592,23 @@ class KoboReadingStateServiceTest {
|
||||
progress.setEpubProgressPercent(65f);
|
||||
progress.setLastReadTime(Instant.parse("2025-06-01T10:00:00Z"));
|
||||
when(progressRepository.findByUserIdAndBookId(1L, 100L)).thenReturn(Optional.of(progress));
|
||||
when(bookRepository.findById(100L)).thenReturn(Optional.of(testBook));
|
||||
when(fileProgressRepository.findByUserIdAndBookFileId(1L, primaryFile.getId())).thenReturn(Optional.empty());
|
||||
|
||||
KoboReadingState.CurrentBookmark webBookmark = KoboReadingState.CurrentBookmark.builder()
|
||||
.progressPercent(65)
|
||||
.lastModified("2025-06-01T10:00:00Z")
|
||||
.build();
|
||||
when(readingStateBuilder.buildBookmarkFromProgress(progress)).thenReturn(webBookmark);
|
||||
when(readingStateBuilder.shouldUseWebReaderProgress(progress)).thenReturn(true);
|
||||
when(readingStateBuilder.buildBookmarkFromProgress(progress, (UserBookFileProgressEntity) null)).thenReturn(webBookmark);
|
||||
|
||||
List<KoboReadingState> result = service.getReadingState(entitlementId);
|
||||
|
||||
assertEquals(1, result.size());
|
||||
KoboReadingState.CurrentBookmark bookmark = result.getFirst().getCurrentBookmark();
|
||||
assertEquals(65, bookmark.getProgressPercent());
|
||||
assertEquals(45, bookmark.getContentSourceProgressPercent());
|
||||
assertNotNull(bookmark.getLocation());
|
||||
assertEquals("epubcfi(/6/10)", bookmark.getLocation().getValue());
|
||||
assertNull(bookmark.getContentSourceProgressPercent());
|
||||
assertNull(bookmark.getLocation());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -619,30 +633,142 @@ class KoboReadingStateServiceTest {
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(30, result.getFirst().getCurrentBookmark().getProgressPercent());
|
||||
verify(readingStateBuilder, never()).buildBookmarkFromProgress(any());
|
||||
verify(readingStateBuilder, never()).buildBookmarkFromProgress(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should cross-populate epub fields from Kobo when two-way sync is ON")
|
||||
@DisplayName("Should overlay web reader location when available")
|
||||
void testGetReadingState_overlayWebReaderBookmark_replacesLocation() {
|
||||
testSettings.setTwoWayProgressSync(true);
|
||||
String entitlementId = "100";
|
||||
BookFileEntity primaryFile = setPrimaryEpub(10L);
|
||||
|
||||
KoboReadingState existingState = KoboReadingState.builder()
|
||||
.entitlementId(entitlementId)
|
||||
.currentBookmark(KoboReadingState.CurrentBookmark.builder()
|
||||
.progressPercent(30)
|
||||
.location(KoboReadingState.CurrentBookmark.Location.builder()
|
||||
.value("kobo.1.1")
|
||||
.type("KoboSpan")
|
||||
.source("old.xhtml")
|
||||
.build())
|
||||
.lastModified("2025-01-01T00:00:00Z")
|
||||
.build())
|
||||
.build();
|
||||
|
||||
KoboReadingStateEntity entity = new KoboReadingStateEntity();
|
||||
when(repository.findByEntitlementIdAndUserId(entitlementId, 1L)).thenReturn(Optional.of(entity));
|
||||
when(mapper.toDto(entity)).thenReturn(existingState);
|
||||
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setEpubProgress("epubcfi(/6/20)");
|
||||
progress.setEpubProgressPercent(65f);
|
||||
progress.setLastReadTime(Instant.parse("2025-06-01T10:00:00Z"));
|
||||
when(progressRepository.findByUserIdAndBookId(1L, 100L)).thenReturn(Optional.of(progress));
|
||||
when(bookRepository.findById(100L)).thenReturn(Optional.of(testBook));
|
||||
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setContentSourceProgressPercent(22f);
|
||||
when(fileProgressRepository.findByUserIdAndBookFileId(1L, primaryFile.getId())).thenReturn(Optional.of(fileProgress));
|
||||
|
||||
KoboReadingState.CurrentBookmark webBookmark = KoboReadingState.CurrentBookmark.builder()
|
||||
.progressPercent(65)
|
||||
.contentSourceProgressPercent(22)
|
||||
.location(KoboReadingState.CurrentBookmark.Location.builder()
|
||||
.value("kobo.12.18")
|
||||
.type("KoboSpan")
|
||||
.source("new.xhtml")
|
||||
.build())
|
||||
.lastModified("2025-06-01T10:00:00Z")
|
||||
.build();
|
||||
when(readingStateBuilder.shouldUseWebReaderProgress(progress)).thenReturn(true);
|
||||
when(readingStateBuilder.buildBookmarkFromProgress(progress, fileProgress)).thenReturn(webBookmark);
|
||||
|
||||
List<KoboReadingState> result = service.getReadingState(entitlementId);
|
||||
|
||||
assertEquals(1, result.size());
|
||||
KoboReadingState.CurrentBookmark bookmark = result.getFirst().getCurrentBookmark();
|
||||
assertEquals(65, bookmark.getProgressPercent());
|
||||
assertEquals(22, bookmark.getContentSourceProgressPercent());
|
||||
assertNotNull(bookmark.getLocation());
|
||||
assertEquals("kobo.12.18", bookmark.getLocation().getValue());
|
||||
assertEquals("new.xhtml", bookmark.getLocation().getSource());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should keep stored Kobo bookmark when mirrored EPUB progress is not newer")
|
||||
void testGetReadingState_preservesStoredKoboBookmarkWhenKoboIsFreshest() {
|
||||
testSettings.setTwoWayProgressSync(true);
|
||||
String entitlementId = "100";
|
||||
BookFileEntity primaryFile = setPrimaryEpub(10L);
|
||||
|
||||
KoboReadingState.CurrentBookmark existingBookmark = KoboReadingState.CurrentBookmark.builder()
|
||||
.progressPercent(55)
|
||||
.contentSourceProgressPercent(23)
|
||||
.location(KoboReadingState.CurrentBookmark.Location.builder()
|
||||
.value("kobo.12.18")
|
||||
.type("KoboSpan")
|
||||
.source("OEBPS/chapter3.xhtml")
|
||||
.build())
|
||||
.lastModified("2025-06-15T12:00:00Z")
|
||||
.build();
|
||||
|
||||
KoboReadingState existingState = KoboReadingState.builder()
|
||||
.entitlementId(entitlementId)
|
||||
.currentBookmark(existingBookmark)
|
||||
.build();
|
||||
|
||||
KoboReadingStateEntity entity = new KoboReadingStateEntity();
|
||||
when(repository.findByEntitlementIdAndUserId(entitlementId, 1L)).thenReturn(Optional.of(entity));
|
||||
when(mapper.toDto(entity)).thenReturn(existingState);
|
||||
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setKoboProgressPercent(55f);
|
||||
progress.setKoboLocation("kobo.12.18");
|
||||
progress.setKoboLocationType("KoboSpan");
|
||||
progress.setKoboLocationSource("OEBPS/chapter3.xhtml");
|
||||
progress.setKoboProgressReceivedTime(Instant.parse("2025-06-15T12:00:00Z"));
|
||||
progress.setEpubProgressPercent(55f);
|
||||
progress.setLastReadTime(Instant.parse("2025-06-15T12:00:00Z"));
|
||||
when(progressRepository.findByUserIdAndBookId(1L, 100L)).thenReturn(Optional.of(progress));
|
||||
when(bookRepository.findById(100L)).thenReturn(Optional.of(testBook));
|
||||
when(fileProgressRepository.findByUserIdAndBookFileId(1L, primaryFile.getId())).thenReturn(Optional.empty());
|
||||
when(readingStateBuilder.shouldUseWebReaderProgress(progress)).thenReturn(false);
|
||||
|
||||
List<KoboReadingState> result = service.getReadingState(entitlementId);
|
||||
|
||||
assertEquals(1, result.size());
|
||||
KoboReadingState.CurrentBookmark bookmark = result.getFirst().getCurrentBookmark();
|
||||
assertEquals(55, bookmark.getProgressPercent());
|
||||
assertEquals(23, bookmark.getContentSourceProgressPercent());
|
||||
assertNotNull(bookmark.getLocation());
|
||||
assertEquals("kobo.12.18", bookmark.getLocation().getValue());
|
||||
assertEquals("OEBPS/chapter3.xhtml", bookmark.getLocation().getSource());
|
||||
verify(readingStateBuilder).shouldUseWebReaderProgress(progress);
|
||||
verify(readingStateBuilder, never()).buildBookmarkFromProgress(eq(progress), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should cross-populate chapter-aware EPUB fields from Kobo when two-way sync is ON")
|
||||
void testSyncKoboProgress_crossPopulateEpubFields() {
|
||||
testSettings.setTwoWayProgressSync(true);
|
||||
String entitlementId = "100";
|
||||
|
||||
BookFileEntity primaryFile = BookFileEntity.builder().id(10L).build();
|
||||
testBook.setBookFiles(List.of(primaryFile));
|
||||
BookFileEntity primaryFile = setPrimaryEpub(10L);
|
||||
|
||||
UserBookProgressEntity existingProgress = new UserBookProgressEntity();
|
||||
existingProgress.setUser(testUserEntity);
|
||||
existingProgress.setBook(testBook);
|
||||
|
||||
KoboReadingState.CurrentBookmark.Location location = KoboReadingState.CurrentBookmark.Location.builder()
|
||||
.value("epubcfi(/6/8)")
|
||||
.type("EpubCfi")
|
||||
.source("chapter3.xhtml")
|
||||
.value("kobo.12.18")
|
||||
.type("KoboSpan")
|
||||
.source("OEBPS/chapter3.xhtml")
|
||||
.build();
|
||||
|
||||
KoboReadingState.CurrentBookmark bookmark = KoboReadingState.CurrentBookmark.builder()
|
||||
.progressPercent(55)
|
||||
.contentSourceProgressPercent(23)
|
||||
.location(location)
|
||||
.lastModified("2025-06-15T12:00:00Z")
|
||||
.build();
|
||||
@@ -660,17 +786,30 @@ class KoboReadingStateServiceTest {
|
||||
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));
|
||||
when(fileProgressRepository.findByUserIdAndBookFileId(1L, 10L)).thenReturn(Optional.empty());
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setUser(testUserEntity);
|
||||
fileProgress.setBookFile(primaryFile);
|
||||
when(fileProgressRepository.findByUserIdAndBookFileId(1L, 10L)).thenReturn(Optional.of(fileProgress));
|
||||
|
||||
ArgumentCaptor<UserBookProgressEntity> captor = ArgumentCaptor.forClass(UserBookProgressEntity.class);
|
||||
when(progressRepository.save(captor.capture())).thenReturn(existingProgress);
|
||||
ArgumentCaptor<UserBookFileProgressEntity> fileProgressCaptor =
|
||||
ArgumentCaptor.forClass(UserBookFileProgressEntity.class);
|
||||
|
||||
service.saveReadingState(List.of(readingState));
|
||||
|
||||
UserBookProgressEntity saved = captor.getValue();
|
||||
assertEquals(55f, saved.getEpubProgressPercent());
|
||||
assertEquals("chapter3.xhtml", saved.getEpubProgressHref());
|
||||
assertNull(saved.getEpubProgress());
|
||||
assertEquals("OEBPS/chapter3.xhtml", saved.getEpubProgressHref());
|
||||
assertEquals(Instant.parse("2025-06-15T12:00:00Z"), saved.getKoboProgressReceivedTime());
|
||||
assertEquals(Instant.parse("2025-06-15T12:00:00Z"), saved.getLastReadTime());
|
||||
verify(fileProgressRepository).save(fileProgressCaptor.capture());
|
||||
UserBookFileProgressEntity savedFileProgress = fileProgressCaptor.getValue();
|
||||
assertNull(savedFileProgress.getPositionData());
|
||||
assertEquals("OEBPS/chapter3.xhtml", savedFileProgress.getPositionHref());
|
||||
assertEquals(23f, savedFileProgress.getContentSourceProgressPercent());
|
||||
assertEquals(Instant.parse("2025-06-15T12:00:00Z"), savedFileProgress.getLastReadTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -711,14 +850,135 @@ class KoboReadingStateServiceTest {
|
||||
assertEquals("epubcfi(/6/4)", saved.getEpubProgress());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should clear stale EPUB CFI but keep chapter source when Kobo sends newer KoboSpan")
|
||||
void testSyncKoboProgress_clearEpubCfiWhenKoboSendsKoboSpan() {
|
||||
testSettings.setTwoWayProgressSync(true);
|
||||
String entitlementId = "100";
|
||||
|
||||
BookFileEntity primaryFile = setPrimaryEpub(10L);
|
||||
|
||||
UserBookProgressEntity existingProgress = new UserBookProgressEntity();
|
||||
existingProgress.setUser(testUserEntity);
|
||||
existingProgress.setBook(testBook);
|
||||
existingProgress.setEpubProgressPercent(55f);
|
||||
existingProgress.setEpubProgress("epubcfi(/6/8!/4/2/6/1:15)");
|
||||
existingProgress.setEpubProgressHref("OPS/chapter3.xhtml");
|
||||
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setUser(testUserEntity);
|
||||
fileProgress.setBookFile(primaryFile);
|
||||
fileProgress.setPositionData("epubcfi(/6/8!/4/2/6/1:15)");
|
||||
fileProgress.setPositionHref("OPS/chapter3.xhtml");
|
||||
|
||||
KoboReadingState.CurrentBookmark.Location location = KoboReadingState.CurrentBookmark.Location.builder()
|
||||
.value("kobo.12.18")
|
||||
.type("KoboSpan")
|
||||
.source("OEBPS/OPS/chapter3.xhtml")
|
||||
.build();
|
||||
|
||||
KoboReadingState.CurrentBookmark bookmark = KoboReadingState.CurrentBookmark.builder()
|
||||
.progressPercent(55)
|
||||
.contentSourceProgressPercent(23)
|
||||
.location(location)
|
||||
.lastModified("2025-06-15T12:00:00Z")
|
||||
.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.findByEntitlementIdAndUserId(entitlementId, 1L)).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));
|
||||
when(fileProgressRepository.findByUserIdAndBookFileId(1L, primaryFile.getId())).thenReturn(Optional.of(fileProgress));
|
||||
|
||||
ArgumentCaptor<UserBookProgressEntity> progressCaptor = ArgumentCaptor.forClass(UserBookProgressEntity.class);
|
||||
when(progressRepository.save(progressCaptor.capture())).thenReturn(existingProgress);
|
||||
ArgumentCaptor<UserBookFileProgressEntity> fileProgressCaptor =
|
||||
ArgumentCaptor.forClass(UserBookFileProgressEntity.class);
|
||||
|
||||
service.saveReadingState(List.of(readingState));
|
||||
|
||||
UserBookProgressEntity savedProgress = progressCaptor.getValue();
|
||||
assertNull(savedProgress.getEpubProgress());
|
||||
assertEquals("OEBPS/OPS/chapter3.xhtml", savedProgress.getEpubProgressHref());
|
||||
|
||||
verify(fileProgressRepository).save(fileProgressCaptor.capture());
|
||||
UserBookFileProgressEntity savedFileProgress = fileProgressCaptor.getValue();
|
||||
assertNull(savedFileProgress.getPositionData());
|
||||
assertEquals("OEBPS/OPS/chapter3.xhtml", savedFileProgress.getPositionHref());
|
||||
assertEquals(23f, savedFileProgress.getContentSourceProgressPercent());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should clear stale chapter progress when Kobo bookmark omits content source progress")
|
||||
void testSyncKoboProgress_clearStaleContentSourceProgressPercent() {
|
||||
testSettings.setTwoWayProgressSync(true);
|
||||
String entitlementId = "100";
|
||||
|
||||
BookFileEntity primaryFile = setPrimaryEpub(10L);
|
||||
|
||||
UserBookProgressEntity existingProgress = new UserBookProgressEntity();
|
||||
existingProgress.setUser(testUserEntity);
|
||||
existingProgress.setBook(testBook);
|
||||
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setUser(testUserEntity);
|
||||
fileProgress.setBookFile(primaryFile);
|
||||
fileProgress.setContentSourceProgressPercent(23f);
|
||||
fileProgress.setPositionHref("OPS/chapter3.xhtml");
|
||||
|
||||
KoboReadingState.CurrentBookmark.Location location = KoboReadingState.CurrentBookmark.Location.builder()
|
||||
.value("kobo.12.18")
|
||||
.type("KoboSpan")
|
||||
.source("OEBPS/OPS/chapter3.xhtml")
|
||||
.build();
|
||||
|
||||
KoboReadingState.CurrentBookmark bookmark = KoboReadingState.CurrentBookmark.builder()
|
||||
.progressPercent(55)
|
||||
.location(location)
|
||||
.lastModified("2025-06-15T12:00:00Z")
|
||||
.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.findByEntitlementIdAndUserId(entitlementId, 1L)).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));
|
||||
when(progressRepository.save(any())).thenReturn(existingProgress);
|
||||
when(fileProgressRepository.findByUserIdAndBookFileId(1L, primaryFile.getId())).thenReturn(Optional.of(fileProgress));
|
||||
|
||||
ArgumentCaptor<UserBookFileProgressEntity> fileProgressCaptor =
|
||||
ArgumentCaptor.forClass(UserBookFileProgressEntity.class);
|
||||
|
||||
service.saveReadingState(List.of(readingState));
|
||||
|
||||
verify(fileProgressRepository).save(fileProgressCaptor.capture());
|
||||
assertNull(fileProgressCaptor.getValue().getContentSourceProgressPercent());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should not cross-populate when web reader has newer progress")
|
||||
void testSyncKoboProgress_skipCrossPopulateWhenWebReaderNewer() {
|
||||
testSettings.setTwoWayProgressSync(true);
|
||||
String entitlementId = "100";
|
||||
|
||||
BookFileEntity primaryFile = BookFileEntity.builder().id(10L).build();
|
||||
testBook.setBookFiles(List.of(primaryFile));
|
||||
BookFileEntity primaryFile = setPrimaryEpub(10L);
|
||||
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setLastReadTime(Instant.now().plusSeconds(3600));
|
||||
@@ -728,6 +988,7 @@ class KoboReadingStateServiceTest {
|
||||
existingProgress.setBook(testBook);
|
||||
existingProgress.setEpubProgressPercent(80f);
|
||||
existingProgress.setEpubProgress("epubcfi(/6/20)");
|
||||
existingProgress.setReadStatus(ReadStatus.READING);
|
||||
|
||||
KoboReadingState readingState = KoboReadingState.builder()
|
||||
.entitlementId(entitlementId)
|
||||
@@ -755,6 +1016,9 @@ class KoboReadingStateServiceTest {
|
||||
UserBookProgressEntity saved = captor.getValue();
|
||||
assertEquals(80f, saved.getEpubProgressPercent());
|
||||
assertEquals("epubcfi(/6/20)", saved.getEpubProgress());
|
||||
assertEquals(ReadStatus.READING, saved.getReadStatus());
|
||||
assertEquals(Instant.parse("2025-01-01T00:00:00Z"), saved.getKoboProgressReceivedTime());
|
||||
verify(hardcoverSyncService, never()).syncProgressToHardcover(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -778,18 +1042,24 @@ class KoboReadingStateServiceTest {
|
||||
|
||||
when(repository.findByEntitlementIdAndUserId(entitlementId, 1L)).thenReturn(Optional.empty());
|
||||
when(progressRepository.findByUserIdAndBookId(1L, 100L)).thenReturn(Optional.of(progress));
|
||||
when(readingStateBuilder.buildReadingStateFromProgress(entitlementId, progress)).thenReturn(expectedState);
|
||||
BookFileEntity primaryFile = setPrimaryEpub(10L);
|
||||
when(bookRepository.findById(100L)).thenReturn(Optional.of(testBook));
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setContentSourceProgressPercent(35f);
|
||||
when(fileProgressRepository.findByUserIdAndBookFileId(1L, primaryFile.getId())).thenReturn(Optional.of(fileProgress));
|
||||
when(readingStateBuilder.buildReadingStateFromProgress(entitlementId, progress, fileProgress)).thenReturn(expectedState);
|
||||
|
||||
KoboReadingState.CurrentBookmark webBookmark = KoboReadingState.CurrentBookmark.builder()
|
||||
.progressPercent(60)
|
||||
.lastModified("2025-06-01T10:00:00Z")
|
||||
.build();
|
||||
when(readingStateBuilder.buildBookmarkFromProgress(progress)).thenReturn(webBookmark);
|
||||
when(readingStateBuilder.shouldUseWebReaderProgress(progress)).thenReturn(true);
|
||||
when(readingStateBuilder.buildBookmarkFromProgress(progress, fileProgress)).thenReturn(webBookmark);
|
||||
|
||||
List<KoboReadingState> result = service.getReadingState(entitlementId);
|
||||
|
||||
assertEquals(1, result.size());
|
||||
verify(readingStateBuilder).buildReadingStateFromProgress(entitlementId, progress);
|
||||
verify(readingStateBuilder).buildReadingStateFromProgress(entitlementId, progress, fileProgress);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -809,7 +1079,7 @@ class KoboReadingStateServiceTest {
|
||||
List<KoboReadingState> result = service.getReadingState(entitlementId);
|
||||
|
||||
assertTrue(result.isEmpty());
|
||||
verify(readingStateBuilder, never()).buildReadingStateFromProgress(any(), any());
|
||||
verify(readingStateBuilder, never()).buildReadingStateFromProgress(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -818,9 +1088,9 @@ class KoboReadingStateServiceTest {
|
||||
String entitlementId = "100";
|
||||
|
||||
KoboReadingState.CurrentBookmark.Location location = KoboReadingState.CurrentBookmark.Location.builder()
|
||||
.value("epubcfi(/6/4[chap01ref]!/4/2/1:3)")
|
||||
.type("EpubCfi")
|
||||
.source("Kobo")
|
||||
.value("kobo.12.18")
|
||||
.type("KoboSpan")
|
||||
.source("OEBPS/chapter3.xhtml")
|
||||
.build();
|
||||
|
||||
KoboReadingState readingState = KoboReadingState.builder()
|
||||
@@ -846,9 +1116,9 @@ class KoboReadingStateServiceTest {
|
||||
service.saveReadingState(List.of(readingState));
|
||||
|
||||
UserBookProgressEntity saved = captor.getValue();
|
||||
assertEquals("epubcfi(/6/4[chap01ref]!/4/2/1:3)", saved.getKoboLocation());
|
||||
assertEquals("EpubCfi", saved.getKoboLocationType());
|
||||
assertEquals("Kobo", saved.getKoboLocationSource());
|
||||
assertEquals("kobo.12.18", saved.getKoboLocation());
|
||||
assertEquals("KoboSpan", saved.getKoboLocationType());
|
||||
assertEquals("OEBPS/chapter3.xhtml", saved.getKoboLocationSource());
|
||||
assertEquals(42f, saved.getKoboProgressPercent());
|
||||
}
|
||||
|
||||
|
||||
+242
@@ -0,0 +1,242 @@
|
||||
package org.booklore.service.kobo;
|
||||
|
||||
import org.booklore.model.dto.kobo.KoboSpanPositionMap;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.BookFileEntity;
|
||||
import org.booklore.model.entity.LibraryPathEntity;
|
||||
import org.booklore.model.entity.UserBookFileProgressEntity;
|
||||
import org.booklore.model.entity.UserBookProgressEntity;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.booklore.util.koreader.EpubCfiService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class KoboBookmarkLocationResolverTest {
|
||||
|
||||
@Mock
|
||||
private KoboSpanMapService koboSpanMapService;
|
||||
|
||||
@Mock
|
||||
private EpubCfiService epubCfiService;
|
||||
|
||||
private KoboBookmarkLocationResolver resolver;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
resolver = new KoboBookmarkLocationResolver(koboSpanMapService, epubCfiService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolve_UsesFirstKoboSpanWhenOnlyHrefIsAvailable() {
|
||||
BookFileEntity bookFile = createBookFile();
|
||||
when(koboSpanMapService.getValidMap(bookFile)).thenReturn(Optional.of(singleChapterMap()));
|
||||
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setBookFile(bookFile);
|
||||
fileProgress.setPositionHref("chapter1.xhtml");
|
||||
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setEpubProgressHref("chapter1.xhtml");
|
||||
|
||||
Optional<KoboBookmarkLocationResolver.ResolvedBookmarkLocation> result =
|
||||
resolver.resolve(progress, fileProgress);
|
||||
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals("kobo.1.1", result.get().value());
|
||||
assertEquals("KoboSpan", result.get().type());
|
||||
assertEquals("OPS/chapter1.xhtml", result.get().source());
|
||||
assertNull(result.get().contentSourceProgressPercent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolve_UsesStoredContentSourceProgressPercentToSelectNearestKoboSpan() {
|
||||
BookFileEntity bookFile = createBookFile();
|
||||
when(koboSpanMapService.getValidMap(bookFile)).thenReturn(Optional.of(singleChapterMap()));
|
||||
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setBookFile(bookFile);
|
||||
fileProgress.setPositionHref("chapter1.xhtml");
|
||||
fileProgress.setContentSourceProgressPercent(80f);
|
||||
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setEpubProgressHref("chapter1.xhtml");
|
||||
|
||||
Optional<KoboBookmarkLocationResolver.ResolvedBookmarkLocation> result =
|
||||
resolver.resolve(progress, fileProgress);
|
||||
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals("kobo.1.2", result.get().value());
|
||||
assertEquals("KoboSpan", result.get().type());
|
||||
assertEquals("OPS/chapter1.xhtml", result.get().source());
|
||||
assertEquals(80f, result.get().contentSourceProgressPercent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolve_FallsBackToPrimaryEpubWhenFileProgressIsMissing() {
|
||||
BookFileEntity bookFile = createBookFile();
|
||||
when(koboSpanMapService.getValidMap(bookFile)).thenReturn(Optional.of(singleChapterMap()));
|
||||
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setBook(bookFile.getBook());
|
||||
progress.setEpubProgressHref("chapter1.xhtml");
|
||||
|
||||
Optional<KoboBookmarkLocationResolver.ResolvedBookmarkLocation> result =
|
||||
resolver.resolve(progress, null);
|
||||
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals("kobo.1.1", result.get().value());
|
||||
assertEquals("OPS/chapter1.xhtml", result.get().source());
|
||||
assertNull(result.get().contentSourceProgressPercent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolve_UsesGlobalProgressWhenHrefIsMissing() {
|
||||
BookFileEntity bookFile = createBookFile();
|
||||
when(koboSpanMapService.getValidMap(bookFile)).thenReturn(Optional.of(twoChapterMap()));
|
||||
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setBookFile(bookFile);
|
||||
fileProgress.setProgressPercent(75f);
|
||||
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setEpubProgressPercent(75f);
|
||||
|
||||
Optional<KoboBookmarkLocationResolver.ResolvedBookmarkLocation> result =
|
||||
resolver.resolve(progress, fileProgress);
|
||||
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals("kobo.2.2", result.get().value());
|
||||
assertEquals("OPS/chapter2.xhtml", result.get().source());
|
||||
assertEquals(50f, result.get().contentSourceProgressPercent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolve_UsesCfiDerivedChapterPositionWhenStoredChapterProgressIsMissing() {
|
||||
BookFileEntity bookFile = createBookFile();
|
||||
when(koboSpanMapService.getValidMap(bookFile)).thenReturn(Optional.of(singleChapterMap()));
|
||||
when(epubCfiService.resolveCfiLocation(Path.of("/library/book.epub"), "epubcfi(/6/2!/4/2/2:15)"))
|
||||
.thenReturn(Optional.of(new EpubCfiService.CfiLocation("chapter1.xhtml", 80f)));
|
||||
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setBookFile(bookFile);
|
||||
fileProgress.setPositionData("epubcfi(/6/2!/4/2/2:15)");
|
||||
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setEpubProgress("epubcfi(/6/2!/4/2/2:15)");
|
||||
|
||||
Optional<KoboBookmarkLocationResolver.ResolvedBookmarkLocation> result =
|
||||
resolver.resolve(progress, fileProgress);
|
||||
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals("kobo.1.2", result.get().value());
|
||||
assertEquals("OPS/chapter1.xhtml", result.get().source());
|
||||
assertEquals(80f, result.get().contentSourceProgressPercent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolve_PrefersExactHrefMatchOverSuffixMatch() {
|
||||
BookFileEntity bookFile = createBookFile();
|
||||
when(koboSpanMapService.getValidMap(bookFile)).thenReturn(Optional.of(new KoboSpanPositionMap(List.of(
|
||||
new KoboSpanPositionMap.Chapter(
|
||||
"OPS/chapter3.xhtml",
|
||||
"OPS/chapter3.xhtml",
|
||||
0,
|
||||
0f,
|
||||
0.5f,
|
||||
List.of(new KoboSpanPositionMap.Span("exact-span", 0.2f))),
|
||||
new KoboSpanPositionMap.Chapter(
|
||||
"OEBPS/OPS/chapter3.xhtml",
|
||||
"OEBPS/OPS/chapter3.xhtml",
|
||||
1,
|
||||
0.5f,
|
||||
1f,
|
||||
List.of(new KoboSpanPositionMap.Span("suffix-span", 0.2f)))
|
||||
))));
|
||||
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setBookFile(bookFile);
|
||||
fileProgress.setPositionHref("OPS/chapter3.xhtml");
|
||||
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
progress.setEpubProgressHref("OPS/chapter3.xhtml");
|
||||
|
||||
Optional<KoboBookmarkLocationResolver.ResolvedBookmarkLocation> result =
|
||||
resolver.resolve(progress, fileProgress);
|
||||
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals("exact-span", result.get().value());
|
||||
assertEquals("OPS/chapter3.xhtml", result.get().source());
|
||||
}
|
||||
|
||||
private BookFileEntity createBookFile() {
|
||||
BookEntity book = new BookEntity();
|
||||
LibraryPathEntity libraryPath = new LibraryPathEntity();
|
||||
libraryPath.setPath("/library");
|
||||
book.setLibraryPath(libraryPath);
|
||||
|
||||
BookFileEntity bookFile = new BookFileEntity();
|
||||
bookFile.setId(10L);
|
||||
bookFile.setBook(book);
|
||||
bookFile.setBookType(BookFileType.EPUB);
|
||||
bookFile.setCurrentHash("hash-123");
|
||||
bookFile.setFileName("book.epub");
|
||||
bookFile.setFileSubPath("");
|
||||
book.setBookFiles(List.of(bookFile));
|
||||
|
||||
return bookFile;
|
||||
}
|
||||
|
||||
private KoboSpanPositionMap singleChapterMap() {
|
||||
return new KoboSpanPositionMap(List.of(
|
||||
new KoboSpanPositionMap.Chapter(
|
||||
"OPS/chapter1.xhtml",
|
||||
"OPS/chapter1.xhtml",
|
||||
0,
|
||||
0f,
|
||||
1f,
|
||||
List.of(
|
||||
new KoboSpanPositionMap.Span("kobo.1.1", 0.2f),
|
||||
new KoboSpanPositionMap.Span("kobo.1.2", 0.85f)
|
||||
))
|
||||
));
|
||||
}
|
||||
|
||||
private KoboSpanPositionMap twoChapterMap() {
|
||||
return new KoboSpanPositionMap(List.of(
|
||||
new KoboSpanPositionMap.Chapter(
|
||||
"OPS/chapter1.xhtml",
|
||||
"OPS/chapter1.xhtml",
|
||||
0,
|
||||
0f,
|
||||
0.5f,
|
||||
List.of(
|
||||
new KoboSpanPositionMap.Span("kobo.1.1", 0.1f),
|
||||
new KoboSpanPositionMap.Span("kobo.1.2", 0.9f)
|
||||
)),
|
||||
new KoboSpanPositionMap.Chapter(
|
||||
"OPS/chapter2.xhtml",
|
||||
"OPS/chapter2.xhtml",
|
||||
1,
|
||||
0.5f,
|
||||
1f,
|
||||
List.of(
|
||||
new KoboSpanPositionMap.Span("kobo.2.1", 0.1f),
|
||||
new KoboSpanPositionMap.Span("kobo.2.2", 0.5f),
|
||||
new KoboSpanPositionMap.Span("kobo.2.3", 0.9f)
|
||||
))
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,6 @@ class KoboCompatibilityServiceTest {
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
koboSettings = KoboSettings.builder()
|
||||
.convertToKepub(false)
|
||||
.conversionLimitInMb(100)
|
||||
.convertCbxToEpub(false)
|
||||
.conversionLimitInMbForCbx(50)
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.booklore.service.kobo;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
class KoboEpubUtilsTest {
|
||||
|
||||
@Test
|
||||
void normalizeHref_returnsNullForNullHref() {
|
||||
assertNull(KoboEpubUtils.normalizeHref(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void decodeHrefPath_preservesMalformedPathWhenUriParsingFails() {
|
||||
assertEquals("chapter%zz.xhtml", KoboEpubUtils.decodeHrefPath("chapter%zz.xhtml"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeHref_trimsLeadingSlashFromDecodedHref() {
|
||||
assertEquals("OPS/chapter 1.xhtml", KoboEpubUtils.normalizeHref("/OPS/chapter%201.xhtml"));
|
||||
}
|
||||
}
|
||||
@@ -230,6 +230,21 @@ class KoboLibrarySyncServiceTest {
|
||||
assertTrue(needsProgressSync(progress));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should detect href-only web reader progress when toggle ON and lastReadTime after sent")
|
||||
void needsProgressSync_webReaderHrefOnly() {
|
||||
testSettings.setTwoWayProgressSync(true);
|
||||
|
||||
UserBookProgressEntity progress = createProgress(1L);
|
||||
progress.setEpubProgressHref("OPS/chapter3.xhtml");
|
||||
progress.setEpubProgressPercent(65f);
|
||||
progress.setLastReadTime(Instant.now());
|
||||
progress.setKoboProgressSentTime(Instant.now().minusSeconds(60));
|
||||
progress.setKoboProgressReceivedTime(Instant.now().minusSeconds(120));
|
||||
|
||||
assertTrue(needsProgressSync(progress));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should not sync web reader progress when toggle OFF")
|
||||
void needsProgressSync_toggleOff() {
|
||||
@@ -286,7 +301,7 @@ class KoboLibrarySyncServiceTest {
|
||||
if (needsKoboProgressSync(progress)) return true;
|
||||
|
||||
if (testSettings.isTwoWayProgressSync()
|
||||
&& progress.getEpubProgress() != null && progress.getEpubProgressPercent() != null) {
|
||||
&& progress.getEpubProgressPercent() != null) {
|
||||
Instant sentTime = progress.getKoboProgressSentTime();
|
||||
Instant lastReadTime = progress.getLastReadTime();
|
||||
if (lastReadTime != null && (sentTime == null || lastReadTime.isAfter(sentTime))) {
|
||||
|
||||
+193
@@ -0,0 +1,193 @@
|
||||
package org.booklore.service.kobo;
|
||||
|
||||
import org.booklore.model.dto.kobo.KoboSpanPositionMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class KoboSpanMapExtractionServiceTest {
|
||||
|
||||
private final KoboSpanMapExtractionService service = new KoboSpanMapExtractionService();
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
void extractFromKepub_ExtractsSpineEntriesAndProgressBoundaries() throws Exception {
|
||||
File kepubFile = createKepub(
|
||||
"OPS/package.opf",
|
||||
"""
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<package xmlns="http://www.idpf.org/2007/opf" version="3.0">
|
||||
<manifest>
|
||||
<item id="chap1" href="chapter1.xhtml" media-type="application/xhtml+xml"/>
|
||||
<item id="chap2" href="chapter2.xhtml" media-type="application/xhtml+xml"/>
|
||||
</manifest>
|
||||
<spine>
|
||||
<itemref idref="chap1"/>
|
||||
<itemref idref="chap2"/>
|
||||
</spine>
|
||||
</package>
|
||||
""",
|
||||
new ZipResource("OPS/chapter1.xhtml", chapterHtml("kobo.1.1", "kobo.1.2")),
|
||||
new ZipResource("OPS/chapter2.xhtml", chapterHtml("kobo.2.1", "kobo.2.2"))
|
||||
);
|
||||
|
||||
KoboSpanPositionMap result = service.extractFromKepub(kepubFile);
|
||||
|
||||
assertEquals(2, result.chapters().size());
|
||||
assertEquals("OPS/chapter1.xhtml", result.chapters().get(0).sourceHref());
|
||||
assertEquals("OPS/chapter2.xhtml", result.chapters().get(1).sourceHref());
|
||||
assertEquals("kobo.1.1", result.chapters().get(0).spans().get(0).id());
|
||||
assertEquals("kobo.2.2", result.chapters().get(1).spans().get(1).id());
|
||||
assertEquals(0f, result.chapters().get(0).globalStartProgress(), 0.0001f);
|
||||
assertTrue(result.chapters().get(0).globalEndProgress() > 0f);
|
||||
assertEquals(1f, result.chapters().get(1).globalEndProgress(), 0.0001f);
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractFromKepub_ResolvesEncodedManifestHrefsToActualEntries() throws Exception {
|
||||
File kepubFile = createKepub(
|
||||
"OPS/package.opf",
|
||||
"""
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<package xmlns="http://www.idpf.org/2007/opf" version="3.0">
|
||||
<manifest>
|
||||
<item id="chap1" href="Text/chapter%202.xhtml" media-type="application/xhtml+xml"/>
|
||||
</manifest>
|
||||
<spine>
|
||||
<itemref idref="chap1"/>
|
||||
</spine>
|
||||
</package>
|
||||
""",
|
||||
new ZipResource("OPS/Text/chapter 2.xhtml", chapterHtml("kobo.2.1", "kobo.2.2"))
|
||||
);
|
||||
|
||||
KoboSpanPositionMap result = service.extractFromKepub(kepubFile);
|
||||
|
||||
assertEquals(1, result.chapters().size());
|
||||
assertEquals("OPS/Text/chapter 2.xhtml", result.chapters().getFirst().sourceHref());
|
||||
assertEquals("OPS/Text/chapter 2.xhtml", result.chapters().getFirst().normalizedHref());
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractFromKepub_PreservesPlusSignsInManifestHrefs() throws Exception {
|
||||
File kepubFile = createKepub(
|
||||
"OPS/package.opf",
|
||||
"""
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<package xmlns="http://www.idpf.org/2007/opf" version="3.0">
|
||||
<manifest>
|
||||
<item id="chap1" href="Text/chapter%2B1.xhtml" media-type="application/xhtml+xml"/>
|
||||
</manifest>
|
||||
<spine>
|
||||
<itemref idref="chap1"/>
|
||||
</spine>
|
||||
</package>
|
||||
""",
|
||||
new ZipResource("OPS/Text/chapter+1.xhtml", chapterHtml("kobo.1.1", "kobo.1.2"))
|
||||
);
|
||||
|
||||
KoboSpanPositionMap result = service.extractFromKepub(kepubFile);
|
||||
|
||||
assertEquals(1, result.chapters().size());
|
||||
assertEquals("OPS/Text/chapter+1.xhtml", result.chapters().getFirst().sourceHref());
|
||||
assertEquals("OPS/Text/chapter+1.xhtml", result.chapters().getFirst().normalizedHref());
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractFromKepub_KeepsSpanlessChaptersInProgressModel() throws Exception {
|
||||
File kepubFile = createKepub(
|
||||
"OPS/package.opf",
|
||||
"""
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<package xmlns="http://www.idpf.org/2007/opf" version="3.0">
|
||||
<manifest>
|
||||
<item id="front" href="front.xhtml" media-type="application/xhtml+xml"/>
|
||||
<item id="chap1" href="chapter1.xhtml" media-type="application/xhtml+xml"/>
|
||||
<item id="chap2" href="chapter2.xhtml" media-type="application/xhtml+xml"/>
|
||||
</manifest>
|
||||
<spine>
|
||||
<itemref idref="front"/>
|
||||
<itemref idref="chap1"/>
|
||||
<itemref idref="chap2"/>
|
||||
</spine>
|
||||
</package>
|
||||
""",
|
||||
new ZipResource("OPS/front.xhtml", chapterHtmlWithoutSpans()),
|
||||
new ZipResource("OPS/chapter1.xhtml", chapterHtml("kobo.1.1", "kobo.1.2")),
|
||||
new ZipResource("OPS/chapter2.xhtml", chapterHtml("kobo.2.1", "kobo.2.2"))
|
||||
);
|
||||
|
||||
KoboSpanPositionMap result = service.extractFromKepub(kepubFile);
|
||||
|
||||
assertEquals(3, result.chapters().size());
|
||||
assertTrue(result.chapters().getFirst().spans().isEmpty());
|
||||
assertTrue(result.chapters().get(1).globalStartProgress() > 0f);
|
||||
}
|
||||
|
||||
private File createKepub(String opfPath, String opfContent, ZipResource... resources) throws IOException {
|
||||
File kepubFile = tempDir.resolve("test.kepub.epub").toFile();
|
||||
try (ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(kepubFile))) {
|
||||
outputStream.putNextEntry(new ZipEntry("META-INF/container.xml"));
|
||||
outputStream.write(containerXml(opfPath).getBytes(StandardCharsets.UTF_8));
|
||||
outputStream.closeEntry();
|
||||
|
||||
outputStream.putNextEntry(new ZipEntry(opfPath));
|
||||
outputStream.write(opfContent.getBytes(StandardCharsets.UTF_8));
|
||||
outputStream.closeEntry();
|
||||
|
||||
for (ZipResource resource : resources) {
|
||||
outputStream.putNextEntry(new ZipEntry(resource.path()));
|
||||
outputStream.write(resource.content().getBytes(StandardCharsets.UTF_8));
|
||||
outputStream.closeEntry();
|
||||
}
|
||||
}
|
||||
return kepubFile;
|
||||
}
|
||||
|
||||
private String containerXml(String opfPath) {
|
||||
return """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
|
||||
<rootfiles>
|
||||
<rootfile full-path="%s" media-type="application/oebps-package+xml"/>
|
||||
</rootfiles>
|
||||
</container>
|
||||
""".formatted(opfPath);
|
||||
}
|
||||
|
||||
private String chapterHtml(String firstSpanId, String secondSpanId) {
|
||||
return """
|
||||
<html>
|
||||
<body>
|
||||
<p><span class="koboSpan" id="%s"></span>First paragraph with enough text to move the marker.</p>
|
||||
<p><span class="koboSpan" id="%s"></span>Second paragraph with even more text to shift the marker later.</p>
|
||||
</body>
|
||||
</html>
|
||||
""".formatted(firstSpanId, secondSpanId);
|
||||
}
|
||||
|
||||
private String chapterHtmlWithoutSpans() {
|
||||
return """
|
||||
<html>
|
||||
<body>
|
||||
<p>Front matter without Kobo span markers but with enough text to count in the book length.</p>
|
||||
</body>
|
||||
</html>
|
||||
""";
|
||||
}
|
||||
|
||||
private record ZipResource(String path, String content) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package org.booklore.service.kobo;
|
||||
|
||||
import org.booklore.model.dto.kobo.KoboSpanPositionMap;
|
||||
import org.booklore.model.entity.BookFileEntity;
|
||||
import org.booklore.model.entity.KoboSpanMapEntity;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.booklore.repository.KoboSpanMapRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.io.File;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class KoboSpanMapServiceTest {
|
||||
|
||||
@Mock
|
||||
private KoboSpanMapRepository koboSpanMapRepository;
|
||||
|
||||
@Mock
|
||||
private KoboSpanMapExtractionService koboSpanMapExtractionService;
|
||||
|
||||
private KoboSpanMapService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new KoboSpanMapService(koboSpanMapRepository, koboSpanMapExtractionService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void computeAndStoreIfNeeded_SkipsWhenExistingHashMatches() throws Exception {
|
||||
BookFileEntity bookFile = createBookFile("hash-1");
|
||||
KoboSpanMapEntity existing = KoboSpanMapEntity.builder()
|
||||
.bookFile(bookFile)
|
||||
.fileHash("hash-1")
|
||||
.spanMap(spanMap())
|
||||
.createdAt(Instant.now())
|
||||
.build();
|
||||
when(koboSpanMapRepository.findByBookFileId(bookFile.getId())).thenReturn(Optional.of(existing));
|
||||
|
||||
service.computeAndStoreIfNeeded(bookFile, new File("ignored.kepub.epub"));
|
||||
|
||||
verify(koboSpanMapExtractionService, never()).extractFromKepub(any());
|
||||
verify(koboSpanMapRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void computeAndStoreIfNeeded_ExtractsAndSavesWhenHashChanges() throws Exception {
|
||||
BookFileEntity bookFile = createBookFile("hash-2");
|
||||
KoboSpanMapEntity existing = KoboSpanMapEntity.builder()
|
||||
.id(99L)
|
||||
.bookFile(bookFile)
|
||||
.fileHash("old-hash")
|
||||
.spanMap(spanMap())
|
||||
.createdAt(Instant.parse("2024-01-01T00:00:00Z"))
|
||||
.build();
|
||||
KoboSpanPositionMap newMap = new KoboSpanPositionMap(List.of(
|
||||
new KoboSpanPositionMap.Chapter("OPS/chapter2.xhtml", "OPS/chapter2.xhtml", 0, 0f, 1f,
|
||||
List.of(new KoboSpanPositionMap.Span("kobo.2.1", 0.5f)))
|
||||
));
|
||||
|
||||
when(koboSpanMapRepository.findByBookFileId(bookFile.getId())).thenReturn(Optional.of(existing));
|
||||
when(koboSpanMapExtractionService.extractFromKepub(any())).thenReturn(newMap);
|
||||
|
||||
service.computeAndStoreIfNeeded(bookFile, tempKepubFile());
|
||||
|
||||
ArgumentCaptor<KoboSpanMapEntity> captor = ArgumentCaptor.forClass(KoboSpanMapEntity.class);
|
||||
verify(koboSpanMapRepository).save(captor.capture());
|
||||
assertEquals(99L, captor.getValue().getId());
|
||||
assertEquals("hash-2", captor.getValue().getFileHash());
|
||||
assertEquals(newMap, captor.getValue().getSpanMap());
|
||||
assertEquals(Instant.parse("2024-01-01T00:00:00Z"), captor.getValue().getCreatedAt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getValidMap_ReturnsOnlyWhenHashMatches() {
|
||||
BookFileEntity bookFile = createBookFile("hash-3");
|
||||
KoboSpanMapEntity stored = KoboSpanMapEntity.builder()
|
||||
.bookFile(bookFile)
|
||||
.fileHash("hash-3")
|
||||
.spanMap(spanMap())
|
||||
.createdAt(Instant.now())
|
||||
.build();
|
||||
when(koboSpanMapRepository.findByBookFileId(bookFile.getId())).thenReturn(Optional.of(stored));
|
||||
|
||||
Optional<KoboSpanPositionMap> validMap = service.getValidMap(bookFile);
|
||||
|
||||
assertTrue(validMap.isPresent());
|
||||
assertEquals(spanMap(), validMap.get());
|
||||
|
||||
stored.setFileHash("stale-hash");
|
||||
Optional<KoboSpanPositionMap> staleMap = service.getValidMap(bookFile);
|
||||
|
||||
assertFalse(staleMap.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getValidMaps_SkipsEntriesWithNullSpanMap() {
|
||||
BookFileEntity bookFile = createBookFile("hash-4");
|
||||
KoboSpanMapEntity stored = KoboSpanMapEntity.builder()
|
||||
.bookFile(bookFile)
|
||||
.fileHash("hash-4")
|
||||
.spanMap(null)
|
||||
.createdAt(Instant.now())
|
||||
.build();
|
||||
when(koboSpanMapRepository.findByBookFileIdIn(bookFilesById(bookFile).keySet())).thenReturn(List.of(stored));
|
||||
|
||||
Map<Long, KoboSpanPositionMap> validMaps = service.getValidMaps(bookFilesById(bookFile));
|
||||
|
||||
assertTrue(validMaps.isEmpty());
|
||||
}
|
||||
|
||||
private BookFileEntity createBookFile(String hash) {
|
||||
BookFileEntity bookFile = new BookFileEntity();
|
||||
bookFile.setId(42L);
|
||||
bookFile.setBookType(BookFileType.EPUB);
|
||||
bookFile.setCurrentHash(hash);
|
||||
return bookFile;
|
||||
}
|
||||
|
||||
private KoboSpanPositionMap spanMap() {
|
||||
return new KoboSpanPositionMap(List.of(
|
||||
new KoboSpanPositionMap.Chapter("OPS/chapter1.xhtml", "OPS/chapter1.xhtml", 0, 0f, 1f,
|
||||
List.of(new KoboSpanPositionMap.Span("kobo.1.1", 0.2f)))
|
||||
));
|
||||
}
|
||||
|
||||
private Map<Long, BookFileEntity> bookFilesById(BookFileEntity bookFile) {
|
||||
return Map.of(bookFile.getId(), bookFile);
|
||||
}
|
||||
|
||||
private File tempKepubFile() throws Exception {
|
||||
File file = File.createTempFile("kobo-span-map", ".kepub.epub");
|
||||
file.deleteOnExit();
|
||||
return file;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import org.booklore.model.dto.Book;
|
||||
import org.booklore.model.dto.BookLoreUser;
|
||||
import org.booklore.model.dto.progress.EpubProgress;
|
||||
import org.booklore.model.dto.progress.PdfProgress;
|
||||
import org.booklore.model.dto.request.BookFileProgress;
|
||||
import org.booklore.model.dto.request.ReadProgressRequest;
|
||||
import org.booklore.model.dto.response.BookStatusUpdateResponse;
|
||||
import org.booklore.model.entity.*;
|
||||
@@ -153,12 +154,16 @@ class ReadingProgressServiceTest {
|
||||
UserBookFileProgressEntity fileProgress = new UserBookFileProgressEntity();
|
||||
fileProgress.setBookFile(bookFile);
|
||||
fileProgress.setPositionData("new-cfi");
|
||||
fileProgress.setPositionHref("OPS/chapter3.xhtml");
|
||||
fileProgress.setContentSourceProgressPercent(18.6f);
|
||||
fileProgress.setProgressPercent(50.0f);
|
||||
fileProgress.setLastReadTime(Instant.now());
|
||||
|
||||
readingProgressService.enrichBookWithProgress(book, progress, fileProgress);
|
||||
|
||||
assertEquals("new-cfi", book.getEpubProgress().getCfi());
|
||||
assertEquals("OPS/chapter3.xhtml", book.getEpubProgress().getHref());
|
||||
assertEquals(18.6f, book.getEpubProgress().getContentSourceProgressPercent());
|
||||
assertEquals(50.0f, book.getEpubProgress().getPercentage());
|
||||
}
|
||||
|
||||
@@ -200,6 +205,49 @@ class ReadingProgressServiceTest {
|
||||
assertEquals(100f, progress.getEpubProgressPercent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateReadProgress_fileProgress_shouldSaveContentSourceProgress() {
|
||||
long bookId = 1L;
|
||||
BookEntity book = new BookEntity();
|
||||
book.setId(bookId);
|
||||
BookFileEntity primaryFile = new BookFileEntity();
|
||||
primaryFile.setId(1L);
|
||||
primaryFile.setBook(book);
|
||||
primaryFile.setBookType(BookFileType.EPUB);
|
||||
book.setBookFiles(List.of(primaryFile));
|
||||
|
||||
BookLoreUser user = mock(BookLoreUser.class);
|
||||
when(user.getId()).thenReturn(2L);
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
|
||||
when(bookRepository.findByIdWithBookFiles(bookId)).thenReturn(Optional.of(book));
|
||||
when(bookFileRepository.findById(1L)).thenReturn(Optional.of(primaryFile));
|
||||
|
||||
BookLoreUserEntity userEntity = new BookLoreUserEntity();
|
||||
userEntity.setId(2L);
|
||||
when(userRepository.findById(2L)).thenReturn(Optional.of(userEntity));
|
||||
|
||||
UserBookProgressEntity progress = new UserBookProgressEntity();
|
||||
when(userBookProgressRepository.findByUserIdAndBookId(2L, bookId)).thenReturn(Optional.of(progress));
|
||||
when(userBookFileProgressRepository.findByUserIdAndBookFileId(2L, 1L)).thenReturn(Optional.empty());
|
||||
|
||||
ReadProgressRequest req = new ReadProgressRequest();
|
||||
req.setBookId(bookId);
|
||||
req.setFileProgress(new BookFileProgress(
|
||||
1L,
|
||||
"epubcfi(/6/8!/4/2/6/1:15)",
|
||||
"OPS/chapter3.xhtml",
|
||||
55f,
|
||||
"epubcfi(/6/8!/4/2/6/1:10)",
|
||||
18.6f));
|
||||
|
||||
readingProgressService.updateReadProgress(req);
|
||||
|
||||
verify(userBookFileProgressRepository).save(argThat(savedFileProgress ->
|
||||
savedFileProgress.getContentSourceProgressPercent() != null
|
||||
&& savedFileProgress.getContentSourceProgressPercent().equals(18.6f)
|
||||
&& "OPS/chapter3.xhtml".equals(savedFileProgress.getPositionHref())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateReadProgress_pdf_shouldSaveProgress() {
|
||||
long bookId = 1L;
|
||||
|
||||
@@ -182,6 +182,18 @@ class EpubCfiServiceTest {
|
||||
assertNotNull(result);
|
||||
assertNotNull(result.getXpointer());
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertCfiToXPointer_roundTripsNestedParagraphLocation() {
|
||||
String originalXPointer = "/body/DocFragment[1]/body/div[1]/p[2]/text().10";
|
||||
String cfi = service.convertXPointerToCfi(testEpubFile, originalXPointer);
|
||||
|
||||
XPointerResult result = service.convertCfiToXPointer(testEpubFile, cfi);
|
||||
|
||||
assertNotNull(result);
|
||||
assertNotNull(result.getXpointer());
|
||||
assertTrue(result.getXpointer().contains("/p[2]/text().10"), result.getXpointer());
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@@ -241,6 +253,31 @@ class EpubCfiServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ResolveCfiLocationTests {
|
||||
|
||||
@Test
|
||||
void resolveCfiLocation_textOffsetsReturnMatchingHrefAndIncreasingProgress() {
|
||||
String earlyCfi = service.convertXPointerToCfi(testEpubFile, "/body/DocFragment[1]/body/div[1]/p[1]/text().0");
|
||||
String laterCfi = service.convertXPointerToCfi(testEpubFile, "/body/DocFragment[1]/body/div[1]/p[2]/text().10");
|
||||
|
||||
EpubCfiService.CfiLocation earlyLocation = service.resolveCfiLocation(testEpubFile, earlyCfi).orElseThrow();
|
||||
EpubCfiService.CfiLocation laterLocation = service.resolveCfiLocation(testEpubFile, laterCfi).orElseThrow();
|
||||
|
||||
assertEquals("chapter1.xhtml", earlyLocation.href());
|
||||
assertEquals("chapter1.xhtml", laterLocation.href());
|
||||
assertNotNull(earlyLocation.contentSourceProgressPercent());
|
||||
assertNotNull(laterLocation.contentSourceProgressPercent());
|
||||
assertTrue(earlyLocation.contentSourceProgressPercent() > 0f);
|
||||
assertTrue(earlyLocation.contentSourceProgressPercent() < laterLocation.contentSourceProgressPercent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveCfiLocation_invalidCfiReturnsEmpty() {
|
||||
assertTrue(service.resolveCfiLocation(testEpubFile, "invalid").isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ValidationTests {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user