Add MonitorAlbum component tests: 21 cases covering all flow diagrams (bufconn + testcontainers + hand-rolled mocks)

This commit is contained in:
Alexander
2026-05-09 21:31:09 +02:00
parent 6f31698006
commit 31ec3f9826
23 changed files with 2166 additions and 4 deletions
+4
View File
@@ -11,6 +11,10 @@ import (
"homelab.lan/music-agregator/internal/config"
)
type Searcher interface {
Search(query string, limit int32, indexer string) (*SearchResponse, error)
}
type IndexerService struct {
indexer Indexer
}
+4
View File
@@ -29,6 +29,10 @@ func NewMusicAgregatorServer(cfg config.Config, riverClient *river.Client[pgx.Tx
}, nil
}
func NewMusicAgregatorServerWithService(service *MusicAgregatorService) *MusicAgregatorServer {
return &MusicAgregatorServer{service: service}
}
func (s *MusicAgregatorServer) GetArtists(ctx context.Context, req *pb.GetArtistsRequest) (*pb.GetArtistsResponse, error) {
return s.service.GetArtists(ctx, req)
}
+25 -4
View File
@@ -34,9 +34,9 @@ type parsedItem struct {
type MusicAgregatorService struct {
config config.Config
metadata *metadata.MetadataService
indexer *indexer.IndexerService
indexer indexer.Searcher
torrentClient torrent.TorrentClient
magnetResolver *torrentParser.MagnetResolver
magnetResolver torrentParser.Resolver
riverClient *river.Client[pgx.Tx]
torrents *database.TorrentRepository
downloads *database.DownloadRepository
@@ -83,9 +83,30 @@ func NewMusicAgregatorService(cfg config.Config, riverClient *river.Client[pgx.T
}, nil
}
func NewMusicAgregatorServiceWithDeps(
metadata *metadata.MetadataService,
searcher indexer.Searcher,
torrentClient torrent.TorrentClient,
magnetResolver torrentParser.Resolver,
riverClient *river.Client[pgx.Tx],
db *database.DB,
) *MusicAgregatorService {
return &MusicAgregatorService{
metadata: metadata,
indexer: searcher,
torrentClient: torrentClient,
magnetResolver: magnetResolver,
riverClient: riverClient,
torrents: database.NewTorrentRepository(db.Pool),
downloads: database.NewDownloadRepository(db.Pool),
artists: database.NewArtistRepository(db.Pool),
downloadFiles: database.NewDownloadFileRepository(db.Pool),
}
}
func (s *MusicAgregatorService) Close() {
if s.magnetResolver != nil {
s.magnetResolver.Close()
if closer, ok := s.magnetResolver.(interface{ Close() }); ok {
closer.Close()
}
}
+4
View File
@@ -11,6 +11,10 @@ import (
"github.com/rs/zerolog/log"
)
type Resolver interface {
Resolve(magnetURI string) ([]byte, error)
}
type MagnetResolver struct {
client *torrent.Client
timeout time.Duration
+4
View File
@@ -106,6 +106,10 @@ func (w *PollDownloadWorker) onCompleted(ctx context.Context, args PollDownloadA
}
func (w *PollDownloadWorker) reschedule(ctx context.Context, args PollDownloadArgs) error {
if w.RiverClient == nil {
log.Warn().Str("download_id", args.DownloadID).Msg("no river client, cannot reschedule poll_download")
return nil
}
_, err := w.RiverClient.Insert(ctx, args, &river.InsertOpts{
ScheduledAt: time.Now().Add(args.CheckInterval),
})