mirror of
https://github.com/navidrome/navidrome.git
synced 2025-08-13 14:01:14 +03:00
* feat(database): add user_library table and library access methods Signed-off-by: Deluan <deluan@navidrome.org> # Conflicts: # tests/mock_library_repo.go * feat(database): enhance user retrieval with library associations Signed-off-by: Deluan <deluan@navidrome.org> * feat(api): implement library management and user-library association endpoints Signed-off-by: Deluan <deluan@navidrome.org> * feat(api): restrict access to library and config endpoints to admin users Signed-off-by: Deluan <deluan@navidrome.org> * refactor(library): implement library management service and update API routes Signed-off-by: Deluan <deluan@navidrome.org> * feat(database): add library filtering to album, folder, and media file queries Signed-off-by: Deluan <deluan@navidrome.org> * refactor library service to use REST repository pattern and remove CRUD operations Signed-off-by: Deluan <deluan@navidrome.org> * add total_duration column to library and update user_library table Signed-off-by: Deluan <deluan@navidrome.org> * fix migration file name Signed-off-by: Deluan <deluan@navidrome.org> * feat(library): add library management features including create, edit, delete, and list functionalities - WIP Signed-off-by: Deluan <deluan@navidrome.org> * feat(library): enhance library validation and management with path checks and normalization - WIP Signed-off-by: Deluan <deluan@navidrome.org> * feat(library): improve library path validation and error handling - WIP Signed-off-by: Deluan <deluan@navidrome.org> * use utils/formatBytes Signed-off-by: Deluan <deluan@navidrome.org> * simplify DeleteLibraryButton.jsx Signed-off-by: Deluan <deluan@navidrome.org> * feat(library): enhance validation messages and error handling for library paths Signed-off-by: Deluan <deluan@navidrome.org> * lint Signed-off-by: Deluan <deluan@navidrome.org> * test(scanner): add tests for multi-library scanning and validation Signed-off-by: Deluan <deluan@navidrome.org> * test(scanner): improve handling of filesystem errors and ensure warnings are returned Signed-off-by: Deluan <deluan@navidrome.org> * feat(controller): add function to retrieve the most recent scan time across all libraries Signed-off-by: Deluan <deluan@navidrome.org> * feat(library): add additional fields and restructure LibraryEdit component for enhanced statistics display Signed-off-by: Deluan <deluan@navidrome.org> * feat(library): enhance LibraryCreate and LibraryEdit components with additional props and styling Signed-off-by: Deluan <deluan@navidrome.org> * feat(mediafile): add LibraryName field and update queries to include library name Signed-off-by: Deluan <deluan@navidrome.org> * feat(missingfiles): add library filter and display in MissingFilesList component Signed-off-by: Deluan <deluan@navidrome.org> * feat(library): implement scanner interface for triggering library scans on create/update Signed-off-by: Deluan <deluan@navidrome.org> # Conflicts: # cmd/wire_gen.go # cmd/wire_injectors.go # Conflicts: # cmd/wire_gen.go # Conflicts: # cmd/wire_gen.go # cmd/wire_injectors.go * feat(library): trigger scan after successful library deletion to clean up orphaned data Signed-off-by: Deluan <deluan@navidrome.org> * rename migration file for user library table to maintain versioning order Signed-off-by: Deluan <deluan@navidrome.org> * refactor: move scan triggering logic into a helper method for clarity Signed-off-by: Deluan <deluan@navidrome.org> * feat(library): add library path and name fields to album and mediafile models Signed-off-by: Deluan <deluan@navidrome.org> * feat(library): add/remove watchers on demand, not only when server starts Signed-off-by: Deluan <deluan@navidrome.org> * refactor(scanner): streamline library handling by using state-libraries for consistency Signed-off-by: Deluan <deluan@navidrome.org> * fix: track processed libraries by updating state with scan timestamps Signed-off-by: Deluan <deluan@navidrome.org> * prepend libraryID for track and album PIDs Signed-off-by: Deluan <deluan@navidrome.org> * feat(repository): apply library filtering in CountAll methods for albums, folders, and media files Signed-off-by: Deluan <deluan@navidrome.org> * feat(user): add library selection for user creation and editing Signed-off-by: Deluan <deluan@navidrome.org> * feat(library): implement library selection functionality with reducer and UI component Signed-off-by: Deluan <deluan@navidrome.org> # Conflicts: # .github/copilot-instructions.md # Conflicts: # .gitignore * feat(library): add tests for LibrarySelector and library selection hooks Signed-off-by: Deluan <deluan@navidrome.org> * test: add unit tests for file utility functions Signed-off-by: Deluan <deluan@navidrome.org> * feat(library): add library ID filtering for album resources Signed-off-by: Deluan <deluan@navidrome.org> * feat(library): streamline library ID filtering in repositories and update resource filtering logic Signed-off-by: Deluan <deluan@navidrome.org> * fix(repository): add table name handling in filter functions for SQL queries Signed-off-by: Deluan <deluan@navidrome.org> * feat(library): add refresh functionality on LibrarySelector close Signed-off-by: Deluan <deluan@navidrome.org> * feat(artist): add library ID filtering for artists in repository and update resource filtering logic Signed-off-by: Deluan <deluan@navidrome.org> # Conflicts: # persistence/artist_repository.go * Add library_id field support for smart playlists - Add library_id field to smart playlist criteria system - Supports Is and IsNot operators for filtering by library ID - Includes comprehensive test coverage for single values and lists - Enables creation of library-specific smart playlists * feat(subsonic): implement user-specific library access in GetMusicFolders Signed-off-by: Deluan <deluan@navidrome.org> * feat(library): enhance LibrarySelectionField to extract library IDs from record Signed-off-by: Deluan <deluan@navidrome.org> * feat(subsonic): update GetIndexes and GetArtists method to support library ID filtering Signed-off-by: Deluan <deluan@navidrome.org> * fix: ensure LibrarySelector dropdown refreshes on button close Added refresh() call when closing the dropdown via button click to maintain consistency with the ClickAwayListener behavior. This ensures the UI updates properly regardless of how the dropdown is closed, fixing an inconsistent refresh behavior between different closing methods. The fix tracks the previous open state and calls refresh() only when the dropdown was open and is being closed by the button click. * refactor: simplify getUserAccessibleLibraries function and update related tests Signed-off-by: Deluan <deluan@navidrome.org> * feat: enhance selectedMusicFolderIds function to handle valid music folder IDs and improve fallback logic Signed-off-by: Deluan <deluan@navidrome.org> * refactor: change ArtistRepository.GetIndex to accept multiple library IDs Updated the GetIndex method signature to accept a slice of library IDs instead of a single ID, enabling support for filtering artists across multiple libraries simultaneously. Changes include: - Modified ArtistRepository interface in model/artist.go - Updated implementation in persistence/artist_repository.go with improved library filtering logic - Refactored Subsonic API browsing.go to use new selectedMusicFolderIds helper - Added comprehensive test coverage for multiple library scenarios - Updated mock repository implementation for testing This change improves flexibility for multi-library operations while maintaining backward compatibility through the selectedMusicFolderIds helper function. * feat: add library access validation to selectedMusicFolderIds Enhanced the selectedMusicFolderIds function to validate musicFolderId parameters against the user's accessible libraries. Invalid library IDs (those the user doesn't have access to) are now silently filtered out, improving security by preventing users from accessing libraries they don't have permission for. Changes include: - Added validation logic to check musicFolderId parameters against user's accessible libraries - Added slices package import for efficient validation - Enhanced function documentation to clarify validation behavior - Added comprehensive test cases covering validation scenarios - Maintains backward compatibility with existing behavior * feat: implement multi-library support for GetAlbumList and GetAlbumList2 endpoints - Enhanced selectedMusicFolderIds helper to validate and filter library IDs - Added ApplyLibraryFilter function in filter/filters.go for library filtering - Updated getAlbumList to support musicFolderId parameter filtering - Added comprehensive tests for multi-library functionality - Supports single and multiple musicFolderId values - Falls back to all accessible libraries when no musicFolderId provided - Validates library access permissions for user security * feat: implement multi-library support for GetRandomSongs, GetSongsByGenre, GetStarred, and GetStarred2 - Added multi-library filtering to GetRandomSongs endpoint using musicFolderId parameter - Added multi-library filtering to GetSongsByGenre endpoint using musicFolderId parameter - Enhanced GetStarred and GetStarred2 to filter artists, albums, and songs by library - Added Options field to MockMediaFileRepo and MockArtistRepo for test compatibility - Added comprehensive Ginkgo/Gomega tests for all new multi-library functionality - All tests verify proper SQL filter generation and library access validation - Supports single/multiple musicFolderId values with fallback to all accessible libraries * refactor: optimize starred items queries with parallel execution and fix test isolation Refactored starred items functionality by extracting common logic into getStarredItems() method that executes artist, album, and media file queries in parallel for better performance. This eliminates code duplication between GetStarred and GetStarred2 methods while improving response times through concurrent database queries using run.Parallel(). Also fixed test isolation issues by adding missing auth.Init(ds) call in album lists test setup. This resolves nil pointer dereference errors in GetStarred and GetStarred2 tests when run independently. * fix: add ApplyArtistLibraryFilter to filter artists by associated music folders Signed-off-by: Deluan <deluan@navidrome.org> * feat: add library access methods to User model Signed-off-by: Deluan <deluan@navidrome.org> * feat: implement library access filtering for artist queries based on user permissions Signed-off-by: Deluan <deluan@navidrome.org> * feat: enhance artist library filtering based on user permissions and optimize library ID retrieval Signed-off-by: Deluan <deluan@navidrome.org> * fix: return error when any musicFolderId is invalid or inaccessible Changed behavior from silently filtering invalid library IDs to returning ErrorDataNotFound (code 70) when any provided musicFolderId parameter is invalid or the user doesn't have access to it. The error message includes the specific library number for better debugging. This affects album/song list endpoints (getAlbumList, getRandomSongs, getSongsByGenre, getStarred) to provide consistent error handling across all Subsonic API endpoints. Updated corresponding tests to expect errors instead of silent filtering. * feat: add musicFolderId parameter support to Search2 and Search3 endpoints Implemented musicFolderId parameter support for Subsonic API Search2 and Search3 endpoints, completing multi-library functionality across all Subsonic endpoints. Key changes: - Added musicFolderId parameter handling to Search2 and Search3 endpoints - Updated search logic to filter results by specified library or all accessible libraries when parameter not provided - Added proper error handling for invalid/inaccessible musicFolderId values - Refactored SearchableRepository interface to support library filtering with variadic QueryOptions - Updated repository implementations (Album, Artist, MediaFile) to handle library filtering in search operations - Added comprehensive test coverage with robust assertions verifying library filtering works correctly - Enhanced mock repositories to capture QueryOptions for test validation Signed-off-by: Deluan <deluan@navidrome.org> * feat: refresh LibraryList on scan end Signed-off-by: Deluan <deluan@navidrome.org> * fix: allow editing name of main library Signed-off-by: Deluan <deluan@navidrome.org> * refactor: implement SendBroadcastMessage method for event broadcasting Signed-off-by: Deluan <deluan@navidrome.org> * feat: add event broadcasting for library creation, update, and deletion Signed-off-by: Deluan <deluan@navidrome.org> * feat: add useRefreshOnEvents hook for custom refresh logic on event changes Signed-off-by: Deluan <deluan@navidrome.org> * feat: enhance library management with refresh event broadcasting Signed-off-by: Deluan <deluan@navidrome.org> * feat: replace AddUserLibrary and RemoveUserLibrary with SetUserLibraries for better library management Signed-off-by: Deluan <deluan@navidrome.org> * chore: remove commented-out genre repository code from persistence tests * feat: enhance library selection with master checkbox functionality Added a master checkbox to the SelectLibraryInput component, allowing users to select or deselect all libraries at once. This improves user experience by simplifying the selection process when multiple libraries are available. Additionally, updated translations in the en.json file to include a new message for selecting all libraries, ensuring consistency in user interface messaging. Signed-off-by: Deluan <deluan@navidrome.org> * feat: add default library assignment for new users Introduced a new column `default_new_users` in the library table to facilitate automatic assignment of default libraries to new regular users. When a new user is created, they will now be assigned to libraries marked as default, enhancing user experience by ensuring they have immediate access to essential resources. Additionally, updated the user repository logic to handle this new functionality and modified the user creation validation to reflect that library selection is optional for non-admin users. Signed-off-by: Deluan <deluan@navidrome.org> * fix: correct updated_at assignment in library repository Signed-off-by: Deluan <deluan@navidrome.org> * fix: improve cache buffering logic Refactored the cache buffering logic to ensure thread safety when checking the buffer length Signed-off-by: Deluan <deluan@navidrome.org> * fix formating Signed-off-by: Deluan <deluan@navidrome.org> * feat: implement per-library artist statistics with automatic aggregation Implemented comprehensive multi-library support for artist statistics that automatically aggregates stats from user-accessible libraries. This fundamental change moves artist statistics from global scope to per-library granularity while maintaining backward compatibility and transparent operation. Key changes include: - Migrated artist statistics from global artist.stats to per-library library_artist.stats - Added automatic library filtering and aggregation in existing Get/GetAll methods - Updated role-based filtering to work with per-library statistics storage - Enhanced statistics calculation to process and store stats per library - Implemented user permission-aware aggregation that respects library access control - Added comprehensive test coverage for library filtering and restricted user access - Created helper functions to ensure proper library associations in tests This enables users to see statistics that accurately reflect only the content from libraries they have access to, providing proper multi-tenant behavior while maintaining the existing API surface and UI functionality. Signed-off-by: Deluan <deluan@navidrome.org> * feat: add multi-library support with per-library tag statistics - WIP Signed-off-by: Deluan <deluan@navidrome.org> * refactor: genre and tag repositories. add comprehensive tests Signed-off-by: Deluan <deluan@navidrome.org> * feat: add multi-library support to tag repository system Implemented comprehensive library filtering for tag repositories to support the multi-library feature. This change ensures that users only see tags from libraries they have access to, while admin users can see all tags. Key changes: - Enhanced TagRepository.Add() method to accept libraryID parameter for proper library association - Updated baseTagRepository to implement library-aware queries with proper joins - Added library_tag table integration for per-library tag statistics - Implemented user permission-based filtering through user_library associations - Added comprehensive test coverage for library filtering scenarios - Updated UI data provider to include tag filtering by selected libraries - Modified scanner to pass library ID when adding tags during folder processing The implementation maintains backward compatibility while providing proper isolation between libraries for tag-based operations like genres and other metadata tags. * refactor: simplify artist repository library filtering Removed conditional admin logic from applyLibraryFilterToArtistQuery method and unified the library filtering approach to match the tag repository pattern. The method now always uses the same SQL join structure regardless of user role, with admin access handled automatically through user_library associations. Added artistLibraryIdFilter function to properly qualify library_id column references and prevent SQL ambiguity errors when multiple tables contain library_id columns. This ensures the filter targets library_artist.library_id specifically rather than causing ambiguous column name conflicts. * fix: resolve LibrarySelectionField validation error for non-admin users Fixed validation error 'At least one library must be selected for non-admin users' that appeared even when libraries were selected. The issue was caused by a data format mismatch between backend and frontend. The backend sends user data with libraries as an array of objects, but the LibrarySelectionField component expects libraryIds as an array of IDs. Added data transformation in the data provider's getOne method to automatically convert libraries array to libraryIds format when fetching user records. Also extracted validation logic into a separate userValidation module for better code organization and added comprehensive test coverage to prevent similar issues. * refactor: remove unused library access functions and related tests Signed-off-by: Deluan <deluan@navidrome.org> * refactor: rename search_test.go to searching_test.go for consistency Signed-off-by: Deluan <deluan@navidrome.org> * fix: add user context to scrobble buffer getParticipants call Added user context handling to scrobbleBufferRepository.Next method to resolve SQL error 'no such column: library_artist.library_id' when processing scrobble entries in multi-library environments. The artist repository now requires user context for proper library filtering, so we fetch the user and temporarily inject it into the context before calling getParticipants. This ensures background scrobbling operations work correctly with multi-library support. * feat: add cross-library move detection for scanner Implemented cross-library move detection for the scanner phase 2 to properly handle files moved between libraries. This prevents users from losing play counts, ratings, and other metadata when moving files across library boundaries. Changes include: - Added MediaFileRepository methods for two-tier matching: FindRecentFilesByMBZTrackID (primary) and FindRecentFilesByProperties (fallback) - Extended scanner phase 2 pipeline with processCrossLibraryMoves stage that processes files unmatched within their library - Implemented findCrossLibraryMatch with MusicBrainz Release Track ID priority and intrinsic properties fallback - Updated producer logic to handle missing tracks without matches, ensuring cross-library processing - Updated tests to reflect new producer behavior and cross-library functionality The implementation uses existing moveMatched function for unified move operations, automatically preserving all user data through database foreign key relationships. Cross-library moves are detected using the same Equals() and IsEquivalent() matching logic as within-library moves for consistency. Signed-off-by: Deluan <deluan@navidrome.org> * feat: add album annotation reassignment for cross-library moves Implemented album annotation reassignment functionality for the scanner's missing tracks phase. When tracks move between libraries and change album IDs, the system now properly reassigns album annotations (starred status, ratings) from the old album to the new album. This prevents loss of user annotations when tracks are moved across library boundaries. The implementation includes: - Thread-safe annotation reassignment using mutex protection - Duplicate reassignment prevention through processed album tracking - Graceful error handling that doesn't fail the entire move operation - Comprehensive test coverage for various scenarios including error conditions This enhancement ensures data integrity and user experience continuity during cross-library media file movements. * fix: address PR review comments for multi-library support Fixed several issues identified in PR review: - Removed unnecessary artist stats initialization check since the map is already initialized in PostScan() - Improved code clarity in user repository by extracting isNewUser variable to avoid checking count == 0 twice - Fixed library selection logic to properly handle initial library state and prevent overriding user selections These changes address code quality and logic issues identified during the multi-library support PR review. * feat: add automatic playlist statistics refreshing Implemented automatic playlist statistics (duration, size, song count) refreshing when tracks are modified. Added new refreshStats() method to recalculate statistics from playlist tracks, and SetTracks() method to update tracks and refresh statistics atomically. Modified all track manipulation methods (RemoveTracks, AddTracks, AddMediaFiles) to automatically refresh statistics. Updated playlist repository to use the new SetTracks method for consistent statistics handling. * refactor: rename AddTracks to AddMediaFilesByID for clarity Renamed the AddTracks method to AddMediaFilesByID throughout the codebase to better reflect its purpose of adding media files to a playlist by their IDs. This change improves code readability and makes the method name more descriptive of its actual functionality. Updated all references in playlist model, tests, core playlist logic, and Subsonic API handlers to use the new method name. * refactor: consolidate user context access in persistence layer Removed duplicate helper functions userId() and isAdmin() from sql_base_repository.go and consolidated all user context access to use loggedUser(r.ctx).ID and loggedUser(r.ctx).IsAdmin consistently across the persistence layer. This change eliminates code duplication and provides a single, consistent pattern for accessing user context information in repository methods. All functionality remains unchanged - this is purely a code cleanup refactoring. * refactor: eliminate MockLibraryService duplication using embedded struct - Replace 235-line MockLibraryService with 40-line embedded struct pattern - Enhance MockLibraryRepo with service-layer methods (192→310 lines) - Maintain full compatibility with existing tests - All 72 nativeapi specs pass with proper error handling Signed-off-by: Deluan <deluan@navidrome.org> * refactor: cleanup Signed-off-by: Deluan <deluan@navidrome.org> --------- Signed-off-by: Deluan <deluan@navidrome.org>
832 lines
32 KiB
Go
832 lines
32 KiB
Go
package scanner_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"path/filepath"
|
|
"testing/fstest"
|
|
"time"
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/conf/configtest"
|
|
"github.com/navidrome/navidrome/consts"
|
|
"github.com/navidrome/navidrome/core"
|
|
"github.com/navidrome/navidrome/core/artwork"
|
|
"github.com/navidrome/navidrome/core/metrics"
|
|
"github.com/navidrome/navidrome/core/storage/storagetest"
|
|
"github.com/navidrome/navidrome/db"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/model/request"
|
|
"github.com/navidrome/navidrome/persistence"
|
|
"github.com/navidrome/navidrome/scanner"
|
|
"github.com/navidrome/navidrome/server/events"
|
|
"github.com/navidrome/navidrome/tests"
|
|
"github.com/navidrome/navidrome/utils/slice"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("Scanner - Multi-Library", Ordered, func() {
|
|
var ctx context.Context
|
|
var lib1, lib2 model.Library
|
|
var ds *tests.MockDataStore
|
|
var s scanner.Scanner
|
|
|
|
createFS := func(path string, files fstest.MapFS) storagetest.FakeFS {
|
|
fs := storagetest.FakeFS{}
|
|
fs.SetFiles(files)
|
|
storagetest.Register(path, &fs)
|
|
return fs
|
|
}
|
|
|
|
BeforeAll(func() {
|
|
ctx = request.WithUser(GinkgoT().Context(), model.User{ID: "123", IsAdmin: true})
|
|
tmpDir := GinkgoT().TempDir()
|
|
conf.Server.DbPath = filepath.Join(tmpDir, "test-scanner-multilibrary.db?_journal_mode=WAL")
|
|
log.Warn("Using DB at " + conf.Server.DbPath)
|
|
db.Db().SetMaxOpenConns(1)
|
|
})
|
|
|
|
BeforeEach(func() {
|
|
DeferCleanup(configtest.SetupConfig())
|
|
conf.Server.DevExternalScanner = false
|
|
|
|
db.Init(ctx)
|
|
DeferCleanup(func() {
|
|
Expect(tests.ClearDB()).To(Succeed())
|
|
})
|
|
|
|
ds = &tests.MockDataStore{RealDS: persistence.New(db.Db())}
|
|
|
|
// Create the admin user in the database to match the context
|
|
adminUser := model.User{
|
|
ID: "123",
|
|
UserName: "admin",
|
|
Name: "Admin User",
|
|
IsAdmin: true,
|
|
NewPassword: "password",
|
|
}
|
|
Expect(ds.User(ctx).Put(&adminUser)).To(Succeed())
|
|
|
|
s = scanner.New(ctx, ds, artwork.NoopCacheWarmer(), events.NoopBroker(),
|
|
core.NewPlaylists(ds), metrics.NewNoopInstance())
|
|
|
|
// Create two test libraries (let DB auto-assign IDs)
|
|
lib1 = model.Library{Name: "Rock Collection", Path: "rock:///music"}
|
|
lib2 = model.Library{Name: "Jazz Collection", Path: "jazz:///music"}
|
|
Expect(ds.Library(ctx).Put(&lib1)).To(Succeed())
|
|
Expect(ds.Library(ctx).Put(&lib2)).To(Succeed())
|
|
})
|
|
|
|
runScanner := func(ctx context.Context, fullScan bool) error {
|
|
_, err := s.ScanAll(ctx, fullScan)
|
|
return err
|
|
}
|
|
|
|
Context("Two Libraries with Different Content", func() {
|
|
BeforeEach(func() {
|
|
// Rock library content
|
|
beatles := template(_t{"albumartist": "The Beatles", "album": "Abbey Road", "year": 1969, "genre": "Rock"})
|
|
zeppelin := template(_t{"albumartist": "Led Zeppelin", "album": "IV", "year": 1971, "genre": "Rock"})
|
|
|
|
_ = createFS("rock", fstest.MapFS{
|
|
"The Beatles/Abbey Road/01 - Come Together.mp3": beatles(track(1, "Come Together")),
|
|
"The Beatles/Abbey Road/02 - Something.mp3": beatles(track(2, "Something")),
|
|
"Led Zeppelin/IV/01 - Black Dog.mp3": zeppelin(track(1, "Black Dog")),
|
|
"Led Zeppelin/IV/02 - Rock and Roll.mp3": zeppelin(track(2, "Rock and Roll")),
|
|
})
|
|
|
|
// Jazz library content
|
|
miles := template(_t{"albumartist": "Miles Davis", "album": "Kind of Blue", "year": 1959, "genre": "Jazz"})
|
|
coltrane := template(_t{"albumartist": "John Coltrane", "album": "Giant Steps", "year": 1960, "genre": "Jazz"})
|
|
|
|
_ = createFS("jazz", fstest.MapFS{
|
|
"Miles Davis/Kind of Blue/01 - So What.mp3": miles(track(1, "So What")),
|
|
"Miles Davis/Kind of Blue/02 - Freddie Freeloader.mp3": miles(track(2, "Freddie Freeloader")),
|
|
"John Coltrane/Giant Steps/01 - Giant Steps.mp3": coltrane(track(1, "Giant Steps")),
|
|
"John Coltrane/Giant Steps/02 - Cousin Mary.mp3": coltrane(track(2, "Cousin Mary")),
|
|
})
|
|
})
|
|
|
|
When("scanning both libraries", func() {
|
|
It("should import files with correct library_id", func() {
|
|
Expect(runScanner(ctx, true)).To(Succeed())
|
|
|
|
// Check Rock library media files
|
|
rockFiles, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib1.ID},
|
|
Sort: "title",
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(rockFiles).To(HaveLen(4))
|
|
|
|
rockTitles := slice.Map(rockFiles, func(f model.MediaFile) string { return f.Title })
|
|
Expect(rockTitles).To(ContainElements("Come Together", "Something", "Black Dog", "Rock and Roll"))
|
|
|
|
// Verify all rock files have correct library_id
|
|
for _, mf := range rockFiles {
|
|
Expect(mf.LibraryID).To(Equal(lib1.ID), "Rock file %s should have library_id %d", mf.Title, lib1.ID)
|
|
}
|
|
|
|
// Check Jazz library media files
|
|
jazzFiles, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib2.ID},
|
|
Sort: "title",
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzFiles).To(HaveLen(4))
|
|
|
|
jazzTitles := slice.Map(jazzFiles, func(f model.MediaFile) string { return f.Title })
|
|
Expect(jazzTitles).To(ContainElements("So What", "Freddie Freeloader", "Giant Steps", "Cousin Mary"))
|
|
|
|
// Verify all jazz files have correct library_id
|
|
for _, mf := range jazzFiles {
|
|
Expect(mf.LibraryID).To(Equal(lib2.ID), "Jazz file %s should have library_id %d", mf.Title, lib2.ID)
|
|
}
|
|
})
|
|
|
|
It("should create albums with correct library_id", func() {
|
|
Expect(runScanner(ctx, true)).To(Succeed())
|
|
|
|
// Check Rock library albums
|
|
rockAlbums, err := ds.Album(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib1.ID},
|
|
Sort: "name",
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(rockAlbums).To(HaveLen(2))
|
|
Expect(rockAlbums[0].Name).To(Equal("Abbey Road"))
|
|
Expect(rockAlbums[0].LibraryID).To(Equal(lib1.ID))
|
|
Expect(rockAlbums[0].SongCount).To(Equal(2))
|
|
Expect(rockAlbums[1].Name).To(Equal("IV"))
|
|
Expect(rockAlbums[1].LibraryID).To(Equal(lib1.ID))
|
|
Expect(rockAlbums[1].SongCount).To(Equal(2))
|
|
|
|
// Check Jazz library albums
|
|
jazzAlbums, err := ds.Album(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib2.ID},
|
|
Sort: "name",
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzAlbums).To(HaveLen(2))
|
|
Expect(jazzAlbums[0].Name).To(Equal("Giant Steps"))
|
|
Expect(jazzAlbums[0].LibraryID).To(Equal(lib2.ID))
|
|
Expect(jazzAlbums[0].SongCount).To(Equal(2))
|
|
Expect(jazzAlbums[1].Name).To(Equal("Kind of Blue"))
|
|
Expect(jazzAlbums[1].LibraryID).To(Equal(lib2.ID))
|
|
Expect(jazzAlbums[1].SongCount).To(Equal(2))
|
|
})
|
|
|
|
It("should create folders with correct library_id", func() {
|
|
Expect(runScanner(ctx, true)).To(Succeed())
|
|
|
|
// Check Rock library folders
|
|
rockFolders, err := ds.Folder(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib1.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(rockFolders).To(HaveLen(5)) // ., The Beatles, Led Zeppelin, Abbey Road, IV
|
|
|
|
for _, folder := range rockFolders {
|
|
Expect(folder.LibraryID).To(Equal(lib1.ID), "Rock folder %s should have library_id %d", folder.Name, lib1.ID)
|
|
}
|
|
|
|
// Check Jazz library folders
|
|
jazzFolders, err := ds.Folder(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib2.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzFolders).To(HaveLen(5)) // ., Miles Davis, John Coltrane, Kind of Blue, Giant Steps
|
|
|
|
for _, folder := range jazzFolders {
|
|
Expect(folder.LibraryID).To(Equal(lib2.ID), "Jazz folder %s should have library_id %d", folder.Name, lib2.ID)
|
|
}
|
|
})
|
|
|
|
It("should create library-artist associations correctly", func() {
|
|
Expect(runScanner(ctx, true)).To(Succeed())
|
|
|
|
// Check library-artist associations
|
|
|
|
// Get all artists and check library associations
|
|
allArtists, err := ds.Artist(ctx).GetAll()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
rockArtistNames := []string{}
|
|
jazzArtistNames := []string{}
|
|
|
|
for _, artist := range allArtists {
|
|
// Check if artist is associated with rock library
|
|
var count int64
|
|
err := db.Db().QueryRow(
|
|
"SELECT COUNT(*) FROM library_artist WHERE library_id = ? AND artist_id = ?",
|
|
lib1.ID, artist.ID,
|
|
).Scan(&count)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
if count > 0 {
|
|
rockArtistNames = append(rockArtistNames, artist.Name)
|
|
}
|
|
|
|
// Check if artist is associated with jazz library
|
|
err = db.Db().QueryRow(
|
|
"SELECT COUNT(*) FROM library_artist WHERE library_id = ? AND artist_id = ?",
|
|
lib2.ID, artist.ID,
|
|
).Scan(&count)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
if count > 0 {
|
|
jazzArtistNames = append(jazzArtistNames, artist.Name)
|
|
}
|
|
}
|
|
|
|
Expect(rockArtistNames).To(ContainElements("The Beatles", "Led Zeppelin"))
|
|
Expect(jazzArtistNames).To(ContainElements("Miles Davis", "John Coltrane"))
|
|
|
|
// Artists should not be shared between libraries (except [Unknown Artist])
|
|
for _, name := range rockArtistNames {
|
|
if name != "[Unknown Artist]" {
|
|
Expect(jazzArtistNames).ToNot(ContainElement(name))
|
|
}
|
|
}
|
|
})
|
|
|
|
It("should update library statistics correctly", func() {
|
|
Expect(runScanner(ctx, true)).To(Succeed())
|
|
|
|
// Check Rock library stats
|
|
rockLib, err := ds.Library(ctx).Get(lib1.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(rockLib.TotalSongs).To(Equal(4))
|
|
Expect(rockLib.TotalAlbums).To(Equal(2))
|
|
|
|
Expect(rockLib.TotalArtists).To(Equal(3)) // The Beatles, Led Zeppelin, [Unknown Artist]
|
|
Expect(rockLib.TotalFolders).To(Equal(2)) // Abbey Road, IV (only folders with audio files)
|
|
|
|
// Check Jazz library stats
|
|
jazzLib, err := ds.Library(ctx).Get(lib2.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzLib.TotalSongs).To(Equal(4))
|
|
Expect(jazzLib.TotalAlbums).To(Equal(2))
|
|
Expect(jazzLib.TotalArtists).To(Equal(3)) // Miles Davis, John Coltrane, [Unknown Artist]
|
|
Expect(jazzLib.TotalFolders).To(Equal(2)) // Kind of Blue, Giant Steps (only folders with audio files)
|
|
})
|
|
})
|
|
|
|
When("libraries have different content", func() {
|
|
It("should maintain separate statistics per library", func() {
|
|
Expect(runScanner(ctx, true)).To(Succeed())
|
|
|
|
// Verify rock library stats
|
|
rockLib, err := ds.Library(ctx).Get(lib1.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(rockLib.TotalSongs).To(Equal(4))
|
|
Expect(rockLib.TotalAlbums).To(Equal(2))
|
|
|
|
// Verify jazz library stats
|
|
jazzLib, err := ds.Library(ctx).Get(lib2.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzLib.TotalSongs).To(Equal(4))
|
|
Expect(jazzLib.TotalAlbums).To(Equal(2))
|
|
|
|
// Verify that libraries don't interfere with each other
|
|
rockFiles, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib1.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(rockFiles).To(HaveLen(4))
|
|
|
|
jazzFiles, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib2.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzFiles).To(HaveLen(4))
|
|
})
|
|
})
|
|
|
|
When("verifying library isolation", func() {
|
|
It("should keep library data completely separate", func() {
|
|
Expect(runScanner(ctx, true)).To(Succeed())
|
|
|
|
// Verify that rock library only contains rock content
|
|
rockAlbums, err := ds.Album(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib1.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
rockAlbumNames := slice.Map(rockAlbums, func(a model.Album) string { return a.Name })
|
|
Expect(rockAlbumNames).To(ContainElements("Abbey Road", "IV"))
|
|
Expect(rockAlbumNames).ToNot(ContainElements("Kind of Blue", "Giant Steps"))
|
|
|
|
// Verify that jazz library only contains jazz content
|
|
jazzAlbums, err := ds.Album(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib2.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
jazzAlbumNames := slice.Map(jazzAlbums, func(a model.Album) string { return a.Name })
|
|
Expect(jazzAlbumNames).To(ContainElements("Kind of Blue", "Giant Steps"))
|
|
Expect(jazzAlbumNames).ToNot(ContainElements("Abbey Road", "IV"))
|
|
})
|
|
})
|
|
|
|
When("same artist appears in different libraries", func() {
|
|
It("should associate artist with both libraries correctly", func() {
|
|
// Create libraries with Jeff Beck albums in both
|
|
jeffRock := template(_t{"albumartist": "Jeff Beck", "album": "Truth", "year": 1968, "genre": "Rock"})
|
|
jeffJazz := template(_t{"albumartist": "Jeff Beck", "album": "Blow by Blow", "year": 1975, "genre": "Jazz"})
|
|
beatles := template(_t{"albumartist": "The Beatles", "album": "Abbey Road", "year": 1969, "genre": "Rock"})
|
|
miles := template(_t{"albumartist": "Miles Davis", "album": "Kind of Blue", "year": 1959, "genre": "Jazz"})
|
|
|
|
// Create rock library with Jeff Beck's Truth album
|
|
_ = createFS("rock", fstest.MapFS{
|
|
"The Beatles/Abbey Road/01 - Come Together.mp3": beatles(track(1, "Come Together")),
|
|
"The Beatles/Abbey Road/02 - Something.mp3": beatles(track(2, "Something")),
|
|
"Jeff Beck/Truth/01 - Beck's Bolero.mp3": jeffRock(track(1, "Beck's Bolero")),
|
|
"Jeff Beck/Truth/02 - Ol' Man River.mp3": jeffRock(track(2, "Ol' Man River")),
|
|
})
|
|
|
|
// Create jazz library with Jeff Beck's Blow by Blow album
|
|
_ = createFS("jazz", fstest.MapFS{
|
|
"Miles Davis/Kind of Blue/01 - So What.mp3": miles(track(1, "So What")),
|
|
"Miles Davis/Kind of Blue/02 - Freddie Freeloader.mp3": miles(track(2, "Freddie Freeloader")),
|
|
"Jeff Beck/Blow by Blow/01 - You Know What I Mean.mp3": jeffJazz(track(1, "You Know What I Mean")),
|
|
"Jeff Beck/Blow by Blow/02 - She's a Woman.mp3": jeffJazz(track(2, "She's a Woman")),
|
|
})
|
|
|
|
Expect(runScanner(ctx, true)).To(Succeed())
|
|
|
|
// Jeff Beck should be associated with both libraries
|
|
var rockCount, jazzCount int64
|
|
|
|
// Get Jeff Beck artist ID
|
|
jeffArtists, err := ds.Artist(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"name": "Jeff Beck"},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jeffArtists).To(HaveLen(1))
|
|
jeffID := jeffArtists[0].ID
|
|
|
|
// Check rock library association
|
|
err = db.Db().QueryRow(
|
|
"SELECT COUNT(*) FROM library_artist WHERE library_id = ? AND artist_id = ?",
|
|
lib1.ID, jeffID,
|
|
).Scan(&rockCount)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(rockCount).To(Equal(int64(1)))
|
|
|
|
// Check jazz library association
|
|
err = db.Db().QueryRow(
|
|
"SELECT COUNT(*) FROM library_artist WHERE library_id = ? AND artist_id = ?",
|
|
lib2.ID, jeffID,
|
|
).Scan(&jazzCount)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzCount).To(Equal(int64(1)))
|
|
|
|
// Verify Jeff Beck albums are in correct libraries
|
|
rockAlbums, err := ds.Album(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib1.ID, "album_artist": "Jeff Beck"},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(rockAlbums).To(HaveLen(1))
|
|
Expect(rockAlbums[0].Name).To(Equal("Truth"))
|
|
|
|
jazzAlbums, err := ds.Album(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib2.ID, "album_artist": "Jeff Beck"},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzAlbums).To(HaveLen(1))
|
|
Expect(jazzAlbums[0].Name).To(Equal("Blow by Blow"))
|
|
})
|
|
})
|
|
})
|
|
|
|
Context("Incremental Scan Behavior", func() {
|
|
BeforeEach(func() {
|
|
// Start with minimal content in both libraries
|
|
rock := template(_t{"albumartist": "Queen", "album": "News of the World", "year": 1977, "genre": "Rock"})
|
|
jazz := template(_t{"albumartist": "Bill Evans", "album": "Waltz for Debby", "year": 1961, "genre": "Jazz"})
|
|
|
|
createFS("rock", fstest.MapFS{
|
|
"Queen/News of the World/01 - We Will Rock You.mp3": rock(track(1, "We Will Rock You")),
|
|
})
|
|
|
|
createFS("jazz", fstest.MapFS{
|
|
"Bill Evans/Waltz for Debby/01 - My Foolish Heart.mp3": jazz(track(1, "My Foolish Heart")),
|
|
})
|
|
})
|
|
|
|
It("should handle incremental scans per library correctly", func() {
|
|
// Initial full scan
|
|
Expect(runScanner(ctx, true)).To(Succeed())
|
|
|
|
// Verify initial state
|
|
rockFiles, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib1.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(rockFiles).To(HaveLen(1))
|
|
|
|
jazzFiles, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib2.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzFiles).To(HaveLen(1))
|
|
|
|
// Incremental scan should not duplicate existing files
|
|
Expect(runScanner(ctx, false)).To(Succeed())
|
|
|
|
// Verify counts remain the same
|
|
rockFiles, err = ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib1.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(rockFiles).To(HaveLen(1))
|
|
|
|
jazzFiles, err = ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib2.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzFiles).To(HaveLen(1))
|
|
})
|
|
})
|
|
|
|
Context("Missing Files Handling", func() {
|
|
var rockFS storagetest.FakeFS
|
|
|
|
BeforeEach(func() {
|
|
rock := template(_t{"albumartist": "AC/DC", "album": "Back in Black", "year": 1980, "genre": "Rock"})
|
|
|
|
rockFS = createFS("rock", fstest.MapFS{
|
|
"AC-DC/Back in Black/01 - Hells Bells.mp3": rock(track(1, "Hells Bells")),
|
|
"AC-DC/Back in Black/02 - Shoot to Thrill.mp3": rock(track(2, "Shoot to Thrill")),
|
|
})
|
|
|
|
createFS("jazz", fstest.MapFS{
|
|
"Herbie Hancock/Head Hunters/01 - Chameleon.mp3": template(_t{
|
|
"albumartist": "Herbie Hancock", "album": "Head Hunters", "year": 1973, "genre": "Jazz",
|
|
})(track(1, "Chameleon")),
|
|
})
|
|
})
|
|
|
|
It("should mark missing files correctly per library", func() {
|
|
// Initial scan
|
|
Expect(runScanner(ctx, true)).To(Succeed())
|
|
|
|
// Remove one file from rock library only
|
|
rockFS.Remove("AC-DC/Back in Black/02 - Shoot to Thrill.mp3")
|
|
|
|
// Rescan
|
|
Expect(runScanner(ctx, false)).To(Succeed())
|
|
|
|
// Check that only the rock library file is marked as missing
|
|
missingRockFiles, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.And{
|
|
squirrel.Eq{"library_id": lib1.ID},
|
|
squirrel.Eq{"missing": true},
|
|
},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(missingRockFiles).To(HaveLen(1))
|
|
Expect(missingRockFiles[0].Title).To(Equal("Shoot to Thrill"))
|
|
|
|
// Check that jazz library files are not affected
|
|
missingJazzFiles, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.And{
|
|
squirrel.Eq{"library_id": lib2.ID},
|
|
squirrel.Eq{"missing": true},
|
|
},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(missingJazzFiles).To(HaveLen(0))
|
|
|
|
// Verify non-missing files
|
|
presentRockFiles, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.And{
|
|
squirrel.Eq{"library_id": lib1.ID},
|
|
squirrel.Eq{"missing": false},
|
|
},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(presentRockFiles).To(HaveLen(1))
|
|
Expect(presentRockFiles[0].Title).To(Equal("Hells Bells"))
|
|
})
|
|
})
|
|
|
|
Context("Error Handling - Multi-Library", func() {
|
|
Context("Filesystem errors affecting one library", func() {
|
|
var rockFS storagetest.FakeFS
|
|
|
|
BeforeEach(func() {
|
|
// Set up content for both libraries
|
|
rock := template(_t{"albumartist": "AC/DC", "album": "Back in Black", "year": 1980, "genre": "Rock"})
|
|
jazz := template(_t{"albumartist": "Miles Davis", "album": "Kind of Blue", "year": 1959, "genre": "Jazz"})
|
|
|
|
rockFS = createFS("rock", fstest.MapFS{
|
|
"AC-DC/Back in Black/01 - Hells Bells.mp3": rock(track(1, "Hells Bells")),
|
|
"AC-DC/Back in Black/02 - Shoot to Thrill.mp3": rock(track(2, "Shoot to Thrill")),
|
|
})
|
|
|
|
createFS("jazz", fstest.MapFS{
|
|
"Miles Davis/Kind of Blue/01 - So What.mp3": jazz(track(1, "So What")),
|
|
"Miles Davis/Kind of Blue/02 - Freddie Freeloader.mp3": jazz(track(2, "Freddie Freeloader")),
|
|
})
|
|
})
|
|
|
|
It("should not affect scanning of other libraries", func() {
|
|
// Inject filesystem read error in rock library only
|
|
rockFS.SetError("AC-DC/Back in Black/01 - Hells Bells.mp3", errors.New("filesystem read error"))
|
|
|
|
// Scan should succeed overall and return warnings
|
|
warnings, err := s.ScanAll(ctx, true)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(warnings).ToNot(BeEmpty(), "Should have warnings for filesystem errors")
|
|
|
|
// Jazz library should have been scanned successfully
|
|
jazzFiles, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib2.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzFiles).To(HaveLen(2))
|
|
Expect(jazzFiles[0].Title).To(BeElementOf("So What", "Freddie Freeloader"))
|
|
Expect(jazzFiles[1].Title).To(BeElementOf("So What", "Freddie Freeloader"))
|
|
|
|
// Rock library may have partial content (depending on scanner implementation)
|
|
rockFiles, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib1.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
// No specific expectation - some files may have been imported despite errors
|
|
_ = rockFiles
|
|
|
|
// Verify jazz library stats are correct
|
|
jazzLib, err := ds.Library(ctx).Get(lib2.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzLib.TotalSongs).To(Equal(2))
|
|
|
|
// Error should be empty (warnings don't count as scan errors)
|
|
lastError, err := ds.Property(ctx).DefaultGet(consts.LastScanErrorKey, "unset")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(lastError).To(BeEmpty())
|
|
})
|
|
|
|
It("should continue with warnings for affected library", func() {
|
|
// Inject read errors on multiple files in rock library
|
|
rockFS.SetError("AC-DC/Back in Black/01 - Hells Bells.mp3", errors.New("read error 1"))
|
|
rockFS.SetError("AC-DC/Back in Black/02 - Shoot to Thrill.mp3", errors.New("read error 2"))
|
|
|
|
// Scan should complete with warnings for multiple filesystem errors
|
|
warnings, err := s.ScanAll(ctx, true)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(warnings).ToNot(BeEmpty(), "Should have warnings for multiple filesystem errors")
|
|
|
|
// Jazz library should be completely unaffected
|
|
jazzFiles, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib2.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzFiles).To(HaveLen(2))
|
|
|
|
// Jazz library statistics should be accurate
|
|
jazzLib, err := ds.Library(ctx).Get(lib2.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzLib.TotalSongs).To(Equal(2))
|
|
Expect(jazzLib.TotalAlbums).To(Equal(1))
|
|
|
|
// Error should be empty (warnings don't count as scan errors)
|
|
lastError, err := ds.Property(ctx).DefaultGet(consts.LastScanErrorKey, "unset")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(lastError).To(BeEmpty())
|
|
})
|
|
})
|
|
|
|
Context("Database errors during multi-library scanning", func() {
|
|
BeforeEach(func() {
|
|
// Set up content for both libraries
|
|
rock := template(_t{"albumartist": "Queen", "album": "News of the World", "year": 1977, "genre": "Rock"})
|
|
jazz := template(_t{"albumartist": "Bill Evans", "album": "Waltz for Debby", "year": 1961, "genre": "Jazz"})
|
|
|
|
createFS("rock", fstest.MapFS{
|
|
"Queen/News of the World/01 - We Will Rock You.mp3": rock(track(1, "We Will Rock You")),
|
|
})
|
|
|
|
createFS("jazz", fstest.MapFS{
|
|
"Bill Evans/Waltz for Debby/01 - My Foolish Heart.mp3": jazz(track(1, "My Foolish Heart")),
|
|
})
|
|
})
|
|
|
|
It("should propagate database errors and stop scanning", func() {
|
|
// Install mock repo that injects DB error
|
|
mfRepo := &mockMediaFileRepo{
|
|
MediaFileRepository: ds.RealDS.MediaFile(ctx),
|
|
GetMissingAndMatchingError: errors.New("database connection failed"),
|
|
}
|
|
ds.MockedMediaFile = mfRepo
|
|
|
|
// Scan should return the database error
|
|
Expect(runScanner(ctx, false)).To(MatchError(ContainSubstring("database connection failed")))
|
|
|
|
// Error should be recorded in scanner properties
|
|
lastError, err := ds.Property(ctx).DefaultGet(consts.LastScanErrorKey, "")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(lastError).To(ContainSubstring("database connection failed"))
|
|
})
|
|
|
|
It("should preserve error information in scanner properties", func() {
|
|
// Install mock repo that injects DB error
|
|
mfRepo := &mockMediaFileRepo{
|
|
MediaFileRepository: ds.RealDS.MediaFile(ctx),
|
|
GetMissingAndMatchingError: errors.New("critical database error"),
|
|
}
|
|
ds.MockedMediaFile = mfRepo
|
|
|
|
// Attempt scan (should fail)
|
|
Expect(runScanner(ctx, false)).To(HaveOccurred())
|
|
|
|
// Check that error is recorded in scanner properties
|
|
lastError, err := ds.Property(ctx).DefaultGet(consts.LastScanErrorKey, "")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(lastError).To(ContainSubstring("critical database error"))
|
|
|
|
// Scan type should still be recorded
|
|
scanType, _ := ds.Property(ctx).DefaultGet(consts.LastScanTypeKey, "")
|
|
Expect(scanType).To(BeElementOf("incremental", "quick"))
|
|
})
|
|
})
|
|
|
|
Context("Mixed error scenarios", func() {
|
|
var rockFS storagetest.FakeFS
|
|
|
|
BeforeEach(func() {
|
|
// Set up rock library with filesystem that can error
|
|
rock := template(_t{"albumartist": "Metallica", "album": "Master of Puppets", "year": 1986, "genre": "Metal"})
|
|
rockFS = createFS("rock", fstest.MapFS{
|
|
"Metallica/Master of Puppets/01 - Battery.mp3": rock(track(1, "Battery")),
|
|
"Metallica/Master of Puppets/02 - Master of Puppets.mp3": rock(track(2, "Master of Puppets")),
|
|
})
|
|
|
|
// Set up jazz library normally
|
|
jazz := template(_t{"albumartist": "Herbie Hancock", "album": "Head Hunters", "year": 1973, "genre": "Jazz"})
|
|
createFS("jazz", fstest.MapFS{
|
|
"Herbie Hancock/Head Hunters/01 - Chameleon.mp3": jazz(track(1, "Chameleon")),
|
|
})
|
|
})
|
|
|
|
It("should handle filesystem errors in one library while other succeeds", func() {
|
|
// Inject filesystem error in rock library
|
|
rockFS.SetError("Metallica/Master of Puppets/01 - Battery.mp3", errors.New("disk read error"))
|
|
|
|
// Scan should complete with warnings (not hard error)
|
|
warnings, err := s.ScanAll(ctx, true)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(warnings).ToNot(BeEmpty(), "Should have warnings for filesystem error")
|
|
|
|
// Jazz library should scan completely successfully
|
|
jazzFiles, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib2.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzFiles).To(HaveLen(1))
|
|
Expect(jazzFiles[0].Title).To(Equal("Chameleon"))
|
|
|
|
// Jazz library statistics should be accurate
|
|
jazzLib, err := ds.Library(ctx).Get(lib2.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzLib.TotalSongs).To(Equal(1))
|
|
Expect(jazzLib.TotalAlbums).To(Equal(1))
|
|
|
|
// Rock library may have partial content (depending on scanner implementation)
|
|
rockFiles, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib1.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
// No specific expectation - some files may have been imported despite errors
|
|
_ = rockFiles
|
|
|
|
// Error should be empty (warnings don't count as scan errors)
|
|
lastError, err := ds.Property(ctx).DefaultGet(consts.LastScanErrorKey, "unset")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(lastError).To(BeEmpty())
|
|
})
|
|
|
|
It("should handle partial failures gracefully", func() {
|
|
// Create a scenario where rock has filesystem issues and jazz has normal content
|
|
rockFS.SetError("Metallica/Master of Puppets/01 - Battery.mp3", errors.New("file corruption"))
|
|
|
|
// Do an initial scan with filesystem error
|
|
warnings, err := s.ScanAll(ctx, true)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(warnings).ToNot(BeEmpty(), "Should have warnings for file corruption")
|
|
|
|
// Verify that the working parts completed successfully
|
|
jazzFiles, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib2.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzFiles).To(HaveLen(1))
|
|
|
|
// Scanner properties should reflect successful completion despite warnings
|
|
scanType, _ := ds.Property(ctx).DefaultGet(consts.LastScanTypeKey, "")
|
|
Expect(scanType).To(Equal("full"))
|
|
|
|
// Start time should be recorded
|
|
startTimeStr, _ := ds.Property(ctx).DefaultGet(consts.LastScanStartTimeKey, "")
|
|
Expect(startTimeStr).ToNot(BeEmpty())
|
|
|
|
// Error should be empty (warnings don't count as scan errors)
|
|
lastError, err := ds.Property(ctx).DefaultGet(consts.LastScanErrorKey, "unset")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(lastError).To(BeEmpty())
|
|
})
|
|
})
|
|
|
|
Context("Error recovery in multi-library context", func() {
|
|
It("should recover from previous library-specific errors", func() {
|
|
// Set up initial content
|
|
rock := template(_t{"albumartist": "Iron Maiden", "album": "The Number of the Beast", "year": 1982, "genre": "Metal"})
|
|
jazz := template(_t{"albumartist": "John Coltrane", "album": "Giant Steps", "year": 1960, "genre": "Jazz"})
|
|
|
|
rockFS := createFS("rock", fstest.MapFS{
|
|
"Iron Maiden/The Number of the Beast/01 - Invaders.mp3": rock(track(1, "Invaders")),
|
|
})
|
|
|
|
createFS("jazz", fstest.MapFS{
|
|
"John Coltrane/Giant Steps/01 - Giant Steps.mp3": jazz(track(1, "Giant Steps")),
|
|
})
|
|
|
|
// First scan with filesystem error in rock
|
|
rockFS.SetError("Iron Maiden/The Number of the Beast/01 - Invaders.mp3", errors.New("temporary disk error"))
|
|
warnings, err := s.ScanAll(ctx, true)
|
|
Expect(err).ToNot(HaveOccurred()) // Should succeed with warnings
|
|
Expect(warnings).ToNot(BeEmpty(), "Should have warnings for temporary disk error")
|
|
|
|
// Clear the error and add more content - recreate the filesystem completely
|
|
rockFS.ClearError("Iron Maiden/The Number of the Beast/01 - Invaders.mp3")
|
|
|
|
// Create a new filesystem with both files
|
|
createFS("rock", fstest.MapFS{
|
|
"Iron Maiden/The Number of the Beast/01 - Invaders.mp3": rock(track(1, "Invaders")),
|
|
"Iron Maiden/The Number of the Beast/02 - Children of the Damned.mp3": rock(track(2, "Children of the Damned")),
|
|
})
|
|
|
|
// Second scan should recover and import all rock content
|
|
warnings, err = s.ScanAll(ctx, true)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(warnings).ToNot(BeEmpty(), "Should have warnings for temporary disk error")
|
|
|
|
// Verify both libraries now have content (at least jazz should work)
|
|
rockFiles, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib1.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
// The scanner should recover and import both rock files
|
|
Expect(len(rockFiles)).To(Equal(2))
|
|
|
|
jazzFiles, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.Eq{"library_id": lib2.ID},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzFiles).To(HaveLen(1))
|
|
|
|
// Both libraries should have correct content counts
|
|
rockLib, err := ds.Library(ctx).Get(lib1.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(rockLib.TotalSongs).To(Equal(2))
|
|
|
|
jazzLib, err := ds.Library(ctx).Get(lib2.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(jazzLib.TotalSongs).To(Equal(1))
|
|
|
|
// Error should be empty (successful recovery)
|
|
lastError, err := ds.Property(ctx).DefaultGet(consts.LastScanErrorKey, "unset")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(lastError).To(BeEmpty())
|
|
})
|
|
})
|
|
})
|
|
|
|
Context("Scanner Properties", func() {
|
|
It("should persist last scan type, start time and error properties", func() {
|
|
// trivial FS setup
|
|
rock := template(_t{"albumartist": "AC/DC", "album": "Back in Black", "year": 1980, "genre": "Rock"})
|
|
_ = createFS("rock", fstest.MapFS{
|
|
"AC-DC/Back in Black/01 - Hells Bells.mp3": rock(track(1, "Hells Bells")),
|
|
})
|
|
|
|
// Run a full scan
|
|
Expect(runScanner(ctx, true)).To(Succeed())
|
|
|
|
// Validate properties
|
|
scanType, _ := ds.Property(ctx).DefaultGet(consts.LastScanTypeKey, "")
|
|
Expect(scanType).To(Equal("full"))
|
|
|
|
startTimeStr, _ := ds.Property(ctx).DefaultGet(consts.LastScanStartTimeKey, "")
|
|
Expect(startTimeStr).ToNot(BeEmpty())
|
|
_, err := time.Parse(time.RFC3339, startTimeStr)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
lastError, err := ds.Property(ctx).DefaultGet(consts.LastScanErrorKey, "unset")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(lastError).To(BeEmpty())
|
|
})
|
|
})
|
|
})
|