fix(library-processing): re-fetch library entity after clearing entity manager during rescan (#2507)

* fix(library-processing): re-fetch library entity after clearing entity manager during rescan

Ensure the latest library entity state is retrieved after clearing the entity manager in rescanLibrary. Add test to verify fresh entity is used for subsequent processing.

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* test(library-processing): update rescan test to verify grouping service usage and remove obsolete assertions

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

---------

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
Balázs Szücs
2026-01-27 16:38:30 -07:00
committed by GitHub
parent 257c016f3f
commit 878bc44138
2 changed files with 61 additions and 5 deletions
@@ -22,11 +22,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -511,4 +507,61 @@ class LibraryProcessingServiceTest {
verify(bookDeletionService, never()).processDeletedLibraryFiles(any(), any());
}
@Test
void rescanLibrary_shouldRefetchLibraryAfterEntityManagerClear(@TempDir Path tempDir) throws IOException {
long libraryId = 1L;
Path accessiblePath = tempDir.resolve("accessible");
Files.createDirectory(accessiblePath);
LibraryPathEntity pathEntity = new LibraryPathEntity();
pathEntity.setId(10L);
pathEntity.setPath(accessiblePath.toString());
BookEntity existingBook = new BookEntity();
existingBook.setId(1L);
existingBook.setLibraryPath(pathEntity);
BookFileEntity existingBookFile = new BookFileEntity();
existingBookFile.setBook(existingBook);
existingBook.setBookFiles(new ArrayList<>(List.of(existingBookFile)));
existingBook.getPrimaryBookFile().setFileSubPath("");
existingBook.getPrimaryBookFile().setFileName("book (with parens).epub");
// First call returns entity with one book, second call returns fresh entity with same book
LibraryEntity libraryEntity = new LibraryEntity();
libraryEntity.setId(libraryId);
libraryEntity.setName("Test Library");
libraryEntity.setLibraryPaths(new ArrayList<>(List.of(pathEntity)));
libraryEntity.setBookEntities(new ArrayList<>(List.of(existingBook)));
LibraryEntity freshLibraryEntity = new LibraryEntity();
freshLibraryEntity.setId(libraryId);
freshLibraryEntity.setName("Test Library");
freshLibraryEntity.setLibraryPaths(new ArrayList<>(List.of(pathEntity)));
freshLibraryEntity.setBookEntities(new ArrayList<>(List.of(existingBook)));
LibraryFile fileOnDisk = LibraryFile.builder()
.libraryEntity(libraryEntity)
.libraryPathEntity(pathEntity)
.fileSubPath("")
.fileName("book (with parens).epub")
.build();
when(libraryRepository.findById(libraryId))
.thenReturn(Optional.of(libraryEntity))
.thenReturn(Optional.of(freshLibraryEntity)); // Second call returns fresh entity
when(libraryFileHelper.getLibraryFiles(any(LibraryEntity.class))).thenReturn(List.of(fileOnDisk));
when(bookAdditionalFileRepository.findByLibraryId(libraryId)).thenReturn(Collections.emptyList());
when(bookGroupingService.groupForRescan(anyList(), any(LibraryEntity.class)))
.thenReturn(new BookGroupingService.GroupingResult(Collections.emptyMap(), Collections.emptyMap()));
RescanLibraryContext context = RescanLibraryContext.builder().libraryId(libraryId).build();
libraryProcessingService.rescanLibrary(context);
verify(libraryRepository, times(2)).findById(libraryId);
verify(bookGroupingService).groupForRescan(eq(Collections.emptyList()), any(LibraryEntity.class));
}
}