@@ -447,6 +447,120 @@ describe('CollectionHandler', () => {
447447 expect ( collectionsService . saveCollection ) . not . toHaveBeenCalled ( ) ;
448448 } ) ;
449449
450+ it ( 'credits cached sizeBytes to handledMediaSizeBytes for delete-style actions' , async ( ) => {
451+ const collection = createCollection ( {
452+ arrAction : ServarrAction . DELETE ,
453+ type : 'episode' ,
454+ } ) ;
455+ const collectionMedia = createCollectionMedia ( collection ) ;
456+ collectionMedia . sizeBytes = 1_500_000_000 as any ;
457+
458+ mediaServer . getLibraries . mockResolvedValue (
459+ createMediaLibraries ( {
460+ id : collection . libraryId . toString ( ) ,
461+ type : 'show' ,
462+ } ) ,
463+ ) ;
464+
465+ await collectionHandler . handleMedia ( collection , collectionMedia ) ;
466+
467+ expect ( collectionsService . resolveItemSize ) . not . toHaveBeenCalled ( ) ;
468+ expect ( collectionsService . saveCollection ) . toHaveBeenCalledWith (
469+ expect . objectContaining ( {
470+ handledMediaAmount : 1 ,
471+ handledMediaSizeBytes : 1_500_000_000 ,
472+ } ) ,
473+ ) ;
474+ } ) ;
475+
476+ it ( 'falls back to media-server lookup when sizeBytes is null on a delete-style action' , async ( ) => {
477+ const collection = createCollection ( {
478+ arrAction : ServarrAction . DELETE ,
479+ type : 'episode' ,
480+ } ) ;
481+ const collectionMedia = createCollectionMedia ( collection ) ;
482+ collectionMedia . sizeBytes = null as any ;
483+
484+ mediaServer . getLibraries . mockResolvedValue (
485+ createMediaLibraries ( {
486+ id : collection . libraryId . toString ( ) ,
487+ type : 'show' ,
488+ } ) ,
489+ ) ;
490+ collectionsService . resolveItemSize . mockResolvedValue ( 2_000_000_000 ) ;
491+
492+ await collectionHandler . handleMedia ( collection , collectionMedia ) ;
493+
494+ expect ( collectionsService . resolveItemSize ) . toHaveBeenCalledWith (
495+ mediaServer ,
496+ collectionMedia . mediaServerId ,
497+ ) ;
498+ expect (
499+ collectionsService . resolveItemSize . mock . invocationCallOrder [ 0 ] ,
500+ ) . toBeLessThan ( mediaServer . deleteFromDisk . mock . invocationCallOrder [ 0 ] ) ;
501+ expect ( collectionsService . saveCollection ) . toHaveBeenCalledWith (
502+ expect . objectContaining ( {
503+ handledMediaAmount : 1 ,
504+ handledMediaSizeBytes : 2_000_000_000 ,
505+ } ) ,
506+ ) ;
507+ } ) ;
508+
509+ it ( 'does not look up size for unmonitor actions' , async ( ) => {
510+ const collection = createCollection ( {
511+ arrAction : ServarrAction . UNMONITOR ,
512+ sonarrSettingsId : 1 ,
513+ type : 'show' ,
514+ } ) ;
515+ const collectionMedia = createCollectionMedia ( collection ) ;
516+ collectionMedia . sizeBytes = null as any ;
517+
518+ mediaServer . getLibraries . mockResolvedValue (
519+ createMediaLibraries ( {
520+ id : collection . libraryId . toString ( ) ,
521+ type : 'show' ,
522+ } ) ,
523+ ) ;
524+ sonarrActionHandler . handleAction . mockResolvedValue ( true ) ;
525+
526+ await collectionHandler . handleMedia ( collection , collectionMedia ) ;
527+
528+ expect ( collectionsService . resolveItemSize ) . not . toHaveBeenCalled ( ) ;
529+ expect ( collectionsService . saveCollection ) . toHaveBeenCalledWith (
530+ expect . objectContaining ( {
531+ handledMediaAmount : 1 ,
532+ handledMediaSizeBytes : 0 ,
533+ } ) ,
534+ ) ;
535+ } ) ;
536+
537+ it ( 'skips byte credit when the lookup also fails to resolve a size' , async ( ) => {
538+ const collection = createCollection ( {
539+ arrAction : ServarrAction . DELETE ,
540+ type : 'episode' ,
541+ } ) ;
542+ const collectionMedia = createCollectionMedia ( collection ) ;
543+ collectionMedia . sizeBytes = null as any ;
544+
545+ mediaServer . getLibraries . mockResolvedValue (
546+ createMediaLibraries ( {
547+ id : collection . libraryId . toString ( ) ,
548+ type : 'show' ,
549+ } ) ,
550+ ) ;
551+ collectionsService . resolveItemSize . mockResolvedValue ( null ) ;
552+
553+ await collectionHandler . handleMedia ( collection , collectionMedia ) ;
554+
555+ expect ( collectionsService . resolveItemSize ) . toHaveBeenCalled ( ) ;
556+ expect ( collectionsService . saveCollection ) . toHaveBeenCalledWith (
557+ expect . objectContaining ( {
558+ handledMediaAmount : 1 ,
559+ handledMediaSizeBytes : 0 ,
560+ } ) ,
561+ ) ;
562+ } ) ;
563+
450564 it ( 'should not call SeerrApiService if Seerr is not configured' , async ( ) => {
451565 const collection = createCollection ( {
452566 arrAction : ServarrAction . DELETE ,
0 commit comments