feat(hardcover): sync Kobo reading progress to Hardcover (#1926)

* feat(hardcover): sync Kobo reading progress to Hardcover

- Add HardcoverSyncService to sync reading progress asynchronously
- Store hardcover_book_id in book_metadata for faster lookups
- Integrate with KoboReadingStateService to trigger sync on progress updates
- Add database migration for hardcover_book_id column

* test(hardcover): add unit tests for HardcoverSyncService

* test(hardcover): add HardcoverSyncService mock to unit tests for improved coverage

* refactor(hardcover): update syncProgressToHardcover method to use book ID

- Changed syncProgressToHardcover to accept book ID instead of BookEntity.
- Integrated BookRepository to fetch BookEntity within the method.
- Updated related unit tests to reflect the new method signature and ensure proper functionality.

* feat(hardcover): enhance HardcoverSyncService with edition lookup and logging

- Added a method to find an edition by ISBN, improving the accuracy of edition information.
- Enhanced logging for progress calculations, search responses, and reading progress updates for better traceability.
- Updated the handling of default edition IDs to ensure correct page counts are used when available.

* feat(metadata): add hardcoverBookId and its locking mechanism

- Introduced hardcoverBookId and hardcoverBookIdLocked fields to MetadataClearFlags, BookMetadata, and BookMetadataEntity.
- Updated BookMetadataUpdater to handle locking for hardcoverBookId.
- Enhanced MetadataChangeDetector to compare changes for hardcoverBookId, ensuring proper metadata management.

* feat(metadata): add hardcoverBookId input and display in metadata dialogs

- Introduced hardcoverBookId input field in the metadata editor with locking mechanism.
- Updated metadata restore dialog to display hardcoverBookId when available.
- Enhanced user interface for better metadata management and visibility.

* feat(metadata): integrate hardcoverBookId across components and forms

- Added hardcoverBookId and hardcoverBookIdLocked fields to relevant models and interfaces.
- Updated metadata editor, picker, and review components to include hardcoverBookId input and locking functionality.
- Enhanced form controls and metadata handling to support the new hardcoverBookId feature for improved user experience.

* feat(migration): add hardcover_book_id_locked column to book_metadata table

- Introduced a new column hardcover_book_id_locked with a default value of FALSE to the book_metadata table for enhanced metadata management.

* fix(metadata): realign html with develop

---------

Co-authored-by: akiraslingshot <akiraslingshot@gmail.com>
This commit is contained in:
Giancarlo Perrone
2025-12-19 10:56:40 -07:00
committed by GitHub
co-authored by akiraslingshot
parent f869ac0ac4
commit 2da01e7a2e
22 changed files with 1124 additions and 2 deletions
@@ -15,6 +15,7 @@ import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.repository.KoboReadingStateRepository;
import com.adityachandel.booklore.repository.UserBookProgressRepository;
import com.adityachandel.booklore.repository.UserRepository;
import com.adityachandel.booklore.service.hardcover.HardcoverSyncService;
import com.adityachandel.booklore.service.kobo.KoboReadingStateBuilder;
import com.adityachandel.booklore.service.kobo.KoboReadingStateService;
import com.adityachandel.booklore.service.kobo.KoboSettingsService;
@@ -64,6 +65,9 @@ class KoboReadingStateServiceTest {
@Mock
private KoboReadingStateBuilder readingStateBuilder;
@Mock
private HardcoverSyncService hardcoverSyncService;
@InjectMocks
private KoboReadingStateService service;
@@ -14,6 +14,7 @@ import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.repository.KoboReadingStateRepository;
import com.adityachandel.booklore.repository.UserBookProgressRepository;
import com.adityachandel.booklore.repository.UserRepository;
import com.adityachandel.booklore.service.hardcover.HardcoverSyncService;
import com.adityachandel.booklore.service.kobo.KoboReadingStateBuilder;
import com.adityachandel.booklore.service.kobo.KoboReadingStateService;
import com.adityachandel.booklore.service.kobo.KoboSettingsService;
@@ -58,6 +59,9 @@ class KoboStatusSyncProtectionTest {
@Mock
private KoboReadingStateBuilder readingStateBuilder;
@Mock
private HardcoverSyncService hardcoverSyncService;
@InjectMocks
private KoboReadingStateService service;
@@ -0,0 +1,439 @@
package com.adityachandel.booklore.service.hardcover;
import com.adityachandel.booklore.model.dto.settings.AppSettings;
import com.adityachandel.booklore.model.dto.settings.MetadataProviderSettings;
import com.adityachandel.booklore.model.entity.BookEntity;
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.service.appsettings.AppSettingService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.web.client.RestClient;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class HardcoverSyncServiceTest {
@Mock
private AppSettingService appSettingService;
@Mock
private BookRepository bookRepository;
@Mock
private RestClient restClient;
@Mock
private RestClient.RequestBodyUriSpec requestBodyUriSpec;
@Mock
private RestClient.RequestBodySpec requestBodySpec;
@Mock
private RestClient.ResponseSpec responseSpec;
private HardcoverSyncService service;
private BookEntity testBook;
private BookMetadataEntity testMetadata;
private AppSettings appSettings;
private MetadataProviderSettings.Hardcover hardcoverSettings;
private static final Long TEST_BOOK_ID = 100L;
@BeforeEach
void setUp() throws Exception {
// Create service with mocked dependencies
service = new HardcoverSyncService(appSettingService, bookRepository);
// Inject our mocked restClient using reflection
Field restClientField = HardcoverSyncService.class.getDeclaredField("restClient");
restClientField.setAccessible(true);
restClientField.set(service, restClient);
testBook = new BookEntity();
testBook.setId(TEST_BOOK_ID);
testMetadata = new BookMetadataEntity();
testMetadata.setIsbn13("9781234567890");
testMetadata.setPageCount(300);
testBook.setMetadata(testMetadata);
appSettings = new AppSettings();
MetadataProviderSettings metadataSettings = new MetadataProviderSettings();
hardcoverSettings = new MetadataProviderSettings.Hardcover();
hardcoverSettings.setEnabled(true);
hardcoverSettings.setApiKey("test-api-key");
metadataSettings.setHardcover(hardcoverSettings);
appSettings.setMetadataProviderSettings(metadataSettings);
when(appSettingService.getAppSettings()).thenReturn(appSettings);
when(bookRepository.findById(TEST_BOOK_ID)).thenReturn(Optional.of(testBook));
// Setup RestClient mock chain - handles multiple calls
when(restClient.post()).thenReturn(requestBodyUriSpec);
when(requestBodyUriSpec.uri(anyString())).thenReturn(requestBodySpec);
when(requestBodySpec.header(anyString(), anyString())).thenReturn(requestBodySpec);
when(requestBodySpec.body(any())).thenReturn(requestBodySpec);
when(requestBodySpec.retrieve()).thenReturn(responseSpec);
}
// === Tests for skipping sync (no API calls should be made) ===
@Test
@DisplayName("Should skip sync when Hardcover is not enabled")
void syncProgressToHardcover_whenHardcoverDisabled_shouldSkip() {
hardcoverSettings.setEnabled(false);
service.syncProgressToHardcover(TEST_BOOK_ID, 50.0f);
verify(restClient, never()).post();
}
@Test
@DisplayName("Should skip sync when API key is missing")
void syncProgressToHardcover_whenApiKeyMissing_shouldSkip() {
hardcoverSettings.setApiKey(null);
service.syncProgressToHardcover(TEST_BOOK_ID, 50.0f);
verify(restClient, never()).post();
}
@Test
@DisplayName("Should skip sync when API key is blank")
void syncProgressToHardcover_whenApiKeyBlank_shouldSkip() {
hardcoverSettings.setApiKey(" ");
service.syncProgressToHardcover(TEST_BOOK_ID, 50.0f);
verify(restClient, never()).post();
}
@Test
@DisplayName("Should skip sync when progress is null")
void syncProgressToHardcover_whenProgressNull_shouldSkip() {
service.syncProgressToHardcover(TEST_BOOK_ID, null);
verify(restClient, never()).post();
}
@Test
@DisplayName("Should skip sync when book not found")
void syncProgressToHardcover_whenBookNotFound_shouldSkip() {
when(bookRepository.findById(TEST_BOOK_ID)).thenReturn(Optional.empty());
service.syncProgressToHardcover(TEST_BOOK_ID, 50.0f);
verify(restClient, never()).post();
}
@Test
@DisplayName("Should skip sync when book has no metadata")
void syncProgressToHardcover_whenNoMetadata_shouldSkip() {
testBook.setMetadata(null);
service.syncProgressToHardcover(TEST_BOOK_ID, 50.0f);
verify(restClient, never()).post();
}
@Test
@DisplayName("Should skip sync when no ISBN available")
void syncProgressToHardcover_whenNoIsbn_shouldSkip() {
testMetadata.setIsbn13(null);
testMetadata.setIsbn10(null);
service.syncProgressToHardcover(TEST_BOOK_ID, 50.0f);
verify(restClient, never()).post();
}
// === Tests for successful sync (API calls should be made) ===
@Test
@DisplayName("Should use stored hardcoverBookId when available")
void syncProgressToHardcover_withStoredBookId_shouldUseStoredId() {
testMetadata.setHardcoverBookId(12345);
testMetadata.setPageCount(300);
// Mock successful responses for the chain
when(responseSpec.body(Map.class))
.thenReturn(createInsertUserBookResponse(5001, null))
.thenReturn(createEmptyUserBookReadsResponse())
.thenReturn(createInsertUserBookReadResponse());
service.syncProgressToHardcover(TEST_BOOK_ID, 50.0f);
// Verify API was called at least once (using stored ID, no search needed)
verify(restClient, atLeastOnce()).post();
}
@Test
@DisplayName("Should search by ISBN when hardcoverBookId is not stored")
void syncProgressToHardcover_withoutStoredBookId_shouldSearchByIsbn() {
// Mock successful responses for the chain
when(responseSpec.body(Map.class))
.thenReturn(createSearchResponse(12345, 300))
.thenReturn(createInsertUserBookResponse(5001, null))
.thenReturn(createEmptyUserBookReadsResponse())
.thenReturn(createInsertUserBookReadResponse());
service.syncProgressToHardcover(TEST_BOOK_ID, 50.0f);
// Verify API was called at least once
verify(restClient, atLeastOnce()).post();
}
@Test
@DisplayName("Should skip further processing when book not found on Hardcover")
void syncProgressToHardcover_whenBookNotFoundOnHardcover_shouldSkipAfterSearch() {
// Mock: search returns empty results
when(responseSpec.body(Map.class)).thenReturn(createEmptySearchResponse());
service.syncProgressToHardcover(TEST_BOOK_ID, 50.0f);
// Should call search only
verify(restClient, times(1)).post();
}
@Test
@DisplayName("Should set status to READ when progress >= 99%")
void syncProgressToHardcover_whenProgress99Percent_shouldMakeApiCalls() {
testMetadata.setHardcoverBookId(12345);
testMetadata.setPageCount(300);
when(responseSpec.body(Map.class))
.thenReturn(createInsertUserBookResponse(5001, null))
.thenReturn(createEmptyUserBookReadsResponse())
.thenReturn(createInsertUserBookReadResponse());
service.syncProgressToHardcover(TEST_BOOK_ID, 99.0f);
verify(restClient, atLeastOnce()).post();
}
@Test
@DisplayName("Should set status to CURRENTLY_READING when progress < 99%")
void syncProgressToHardcover_whenProgressLessThan99_shouldMakeApiCalls() {
testMetadata.setHardcoverBookId(12345);
testMetadata.setPageCount(300);
when(responseSpec.body(Map.class))
.thenReturn(createInsertUserBookResponse(5001, null))
.thenReturn(createEmptyUserBookReadsResponse())
.thenReturn(createInsertUserBookReadResponse());
service.syncProgressToHardcover(TEST_BOOK_ID, 50.0f);
verify(restClient, atLeastOnce()).post();
}
@Test
@DisplayName("Should handle existing user_book gracefully")
void syncProgressToHardcover_whenUserBookExists_shouldFindExisting() {
testMetadata.setHardcoverBookId(12345);
// Mock: insert_user_book returns error, then find existing, then create progress
when(responseSpec.body(Map.class))
.thenReturn(createInsertUserBookResponse(null, "Book already exists"))
.thenReturn(createFindUserBookResponse(5001))
.thenReturn(createEmptyUserBookReadsResponse())
.thenReturn(createInsertUserBookReadResponse());
service.syncProgressToHardcover(TEST_BOOK_ID, 50.0f);
verify(restClient, atLeastOnce()).post();
}
@Test
@DisplayName("Should update existing reading progress")
void syncProgressToHardcover_whenProgressExists_shouldUpdate() {
testMetadata.setHardcoverBookId(12345);
// Mock: insert_user_book -> find existing read -> update read
when(responseSpec.body(Map.class))
.thenReturn(createInsertUserBookResponse(5001, null))
.thenReturn(createFindUserBookReadResponse(6001))
.thenReturn(createUpdateUserBookReadResponse());
service.syncProgressToHardcover(TEST_BOOK_ID, 50.0f);
verify(restClient, atLeastOnce()).post();
}
@Test
@DisplayName("Should use ISBN10 when ISBN13 is missing")
void syncProgressToHardcover_whenIsbn13Missing_shouldUseIsbn10() {
testMetadata.setIsbn13(null);
testMetadata.setIsbn10("1234567890");
when(responseSpec.body(Map.class))
.thenReturn(createSearchResponse(12345, 300))
.thenReturn(createInsertUserBookResponse(5001, null))
.thenReturn(createEmptyUserBookReadsResponse())
.thenReturn(createInsertUserBookReadResponse());
service.syncProgressToHardcover(TEST_BOOK_ID, 50.0f);
verify(restClient, atLeastOnce()).post();
}
// === Tests for error handling ===
@Test
@DisplayName("Should handle API errors gracefully")
void syncProgressToHardcover_whenApiError_shouldNotThrow() {
testMetadata.setHardcoverBookId(12345);
when(responseSpec.body(Map.class)).thenReturn(Map.of("errors", List.of(Map.of("message", "Unauthorized"))));
assertDoesNotThrow(() -> service.syncProgressToHardcover(TEST_BOOK_ID, 50.0f));
}
@Test
@DisplayName("Should handle null response gracefully")
void syncProgressToHardcover_whenResponseNull_shouldNotThrow() {
testMetadata.setHardcoverBookId(12345);
when(responseSpec.body(Map.class)).thenReturn(null);
assertDoesNotThrow(() -> service.syncProgressToHardcover(TEST_BOOK_ID, 50.0f));
}
// === Helper methods to create mock responses ===
private Map<String, Object> createSearchResponse(Integer bookId, Integer pages) {
Map<String, Object> response = new HashMap<>();
Map<String, Object> data = new HashMap<>();
Map<String, Object> search = new HashMap<>();
Map<String, Object> results = new HashMap<>();
Map<String, Object> hit = new HashMap<>();
Map<String, Object> document = new HashMap<>();
document.put("id", bookId.toString());
document.put("pages", pages);
hit.put("document", document);
results.put("hits", List.of(hit));
search.put("results", results);
data.put("search", search);
response.put("data", data);
return response;
}
private Map<String, Object> createEmptySearchResponse() {
Map<String, Object> response = new HashMap<>();
Map<String, Object> data = new HashMap<>();
Map<String, Object> search = new HashMap<>();
Map<String, Object> results = new HashMap<>();
results.put("hits", List.of());
search.put("results", results);
data.put("search", search);
response.put("data", data);
return response;
}
private Map<String, Object> createInsertUserBookResponse(Integer userBookId, String error) {
Map<String, Object> response = new HashMap<>();
Map<String, Object> data = new HashMap<>();
Map<String, Object> insertResult = new HashMap<>();
if (userBookId != null) {
Map<String, Object> userBook = new HashMap<>();
userBook.put("id", userBookId);
insertResult.put("user_book", userBook);
}
if (error != null) {
insertResult.put("error", error);
}
data.put("insert_user_book", insertResult);
response.put("data", data);
return response;
}
private Map<String, Object> createFindUserBookResponse(Integer userBookId) {
Map<String, Object> response = new HashMap<>();
Map<String, Object> data = new HashMap<>();
Map<String, Object> me = new HashMap<>();
Map<String, Object> userBook = new HashMap<>();
userBook.put("id", userBookId);
me.put("user_books", List.of(userBook));
data.put("me", me);
response.put("data", data);
return response;
}
private Map<String, Object> createInsertUserBookReadResponse() {
Map<String, Object> response = new HashMap<>();
Map<String, Object> data = new HashMap<>();
Map<String, Object> insertResult = new HashMap<>();
Map<String, Object> userBookRead = new HashMap<>();
userBookRead.put("id", 6001);
insertResult.put("user_book_read", userBookRead);
data.put("insert_user_book_read", insertResult);
response.put("data", data);
return response;
}
private Map<String, Object> createFindUserBookReadResponse(Integer readId) {
Map<String, Object> response = new HashMap<>();
Map<String, Object> data = new HashMap<>();
Map<String, Object> read = new HashMap<>();
read.put("id", readId);
data.put("user_book_reads", List.of(read));
response.put("data", data);
return response;
}
private Map<String, Object> createEmptyUserBookReadsResponse() {
Map<String, Object> response = new HashMap<>();
Map<String, Object> data = new HashMap<>();
data.put("user_book_reads", List.of());
response.put("data", data);
return response;
}
private Map<String, Object> createUpdateUserBookReadResponse() {
Map<String, Object> response = new HashMap<>();
Map<String, Object> data = new HashMap<>();
Map<String, Object> updateResult = new HashMap<>();
Map<String, Object> userBookRead = new HashMap<>();
userBookRead.put("id", 6001);
userBookRead.put("progress", 50);
updateResult.put("user_book_read", userBookRead);
data.put("update_user_book_read", updateResult);
response.put("data", data);
return response;
}
}