mirror of
https://github.com/NNTmux/newznab-tmux.git
synced 2026-08-29 02:01:33 +00:00
Refactor additional postprocessing
This commit is contained in:
@@ -4,6 +4,7 @@ namespace Tests\Unit\AdditionalProcessing;
|
||||
|
||||
use App\Models\Release;
|
||||
use App\Models\ReleaseFile;
|
||||
use App\Services\AdditionalProcessing\AdditionalWorkPlanner;
|
||||
use App\Services\AdditionalProcessing\ArchiveExtractionService;
|
||||
use App\Services\AdditionalProcessing\ConsoleOutputService;
|
||||
use App\Services\AdditionalProcessing\MediaExtractionService;
|
||||
@@ -59,10 +60,13 @@ class ReleaseFilesArchiveFallbackTest extends TestCase
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_extracts_jpg_candidates_from_an_archive_file_list(): void
|
||||
public function it_extracts_webp_candidates_from_an_archive_file_list(): void
|
||||
{
|
||||
$archive = Mockery::mock(ArchiveExtractionService::class);
|
||||
$archive->shouldReceive('extractSpecificFile')->once()->andReturn('JPEG-DATA');
|
||||
$archive->shouldReceive('extractSpecificFiles')
|
||||
->once()
|
||||
->with('ARCHIVE', ['cover.webp'], Mockery::type('string'))
|
||||
->andReturn(['cover.webp' => 'WEBP-DATA']);
|
||||
|
||||
$media = Mockery::mock(MediaExtractionService::class);
|
||||
$media->shouldReceive('isValidImage')->once()->andReturn(true);
|
||||
@@ -72,7 +76,6 @@ class ReleaseFilesArchiveFallbackTest extends TestCase
|
||||
$output->shouldReceive('echoJpgSaved')->once()->andReturnNull();
|
||||
|
||||
$fallback = new ReleaseFilesArchiveFallback(
|
||||
$this->makeConfig(['processJPGSample' => true]),
|
||||
$archive,
|
||||
$media,
|
||||
Mockery::mock(UsenetDownloadService::class),
|
||||
@@ -81,7 +84,7 @@ class ReleaseFilesArchiveFallbackTest extends TestCase
|
||||
);
|
||||
|
||||
$context = $this->makeContext();
|
||||
$fallback->processJpgFromArchiveFileList('ARCHIVE', [['name' => 'cover.jpg', 'size' => 99]], $context);
|
||||
$fallback->processJpgFromArchiveFileList('ARCHIVE', [['name' => 'cover.webp', 'size' => 99]], $context);
|
||||
|
||||
$this->assertTrue($context->foundJPGSample);
|
||||
}
|
||||
@@ -96,7 +99,10 @@ class ReleaseFilesArchiveFallbackTest extends TestCase
|
||||
]);
|
||||
|
||||
$archive = Mockery::mock(ArchiveExtractionService::class);
|
||||
$archive->shouldReceive('extractSpecificFile')->once()->andReturn('NFO DATA');
|
||||
$archive->shouldReceive('extractSpecificFiles')
|
||||
->once()
|
||||
->with('ARCHIVE', ['release.nfo'], Mockery::type('string'))
|
||||
->andReturn(['release.nfo' => 'NFO DATA']);
|
||||
|
||||
$download = Mockery::mock(UsenetDownloadService::class);
|
||||
$download->shouldReceive('download')->once()->andReturn([
|
||||
@@ -118,8 +124,8 @@ class ReleaseFilesArchiveFallbackTest extends TestCase
|
||||
$output = Mockery::mock(ConsoleOutputService::class);
|
||||
$output->shouldReceive('echoNfoFound')->once()->andReturnNull();
|
||||
|
||||
$config = $this->makeConfig();
|
||||
$fallback = new ReleaseFilesArchiveFallback(
|
||||
$this->makeConfig(),
|
||||
$archive,
|
||||
Mockery::mock(MediaExtractionService::class),
|
||||
$download,
|
||||
@@ -131,12 +137,133 @@ class ReleaseFilesArchiveFallbackTest extends TestCase
|
||||
$context->releaseGroupName = 'alt.binaries.test';
|
||||
$context->releaseHasNoNFO = true;
|
||||
$context->nzbContents = [['title' => 'archive.part01.rar', 'segments' => ['<1>', '<2>']]];
|
||||
$context->workPlan = (new AdditionalWorkPlanner($config))->plan(
|
||||
$context->nzbContents,
|
||||
$context->releaseGroupName,
|
||||
);
|
||||
|
||||
$fallback->processNfoFromReleaseFiles($context);
|
||||
|
||||
$this->assertFalse($context->releaseHasNoNFO);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_recovers_an_nfo_from_an_already_downloaded_archive_without_another_download(): void
|
||||
{
|
||||
$files = [['name' => 'release.nfo', 'size' => 200]];
|
||||
|
||||
$archive = Mockery::mock(ArchiveExtractionService::class);
|
||||
$archive->shouldReceive('sortFilesWithNfoPriority')->once()->with($files)->andReturn($files);
|
||||
$archive->shouldReceive('isNfoFile')->once()->with('release.nfo')->andReturn(true);
|
||||
$archive->shouldReceive('extractSpecificFiles')
|
||||
->once()
|
||||
->with('ARCHIVE', ['release.nfo'], Mockery::type('string'))
|
||||
->andReturn(['release.nfo' => 'NFO DATA']);
|
||||
|
||||
$download = Mockery::mock(UsenetDownloadService::class);
|
||||
$download->shouldNotReceive('download');
|
||||
$download->shouldReceive('getNNTP')->once()->andReturn(Mockery::mock(NNTPService::class));
|
||||
|
||||
$manager = Mockery::mock(ReleaseFileManager::class);
|
||||
$manager->shouldReceive('processNfoFile')->once()->andReturnUsing(
|
||||
function (string $path, ReleaseProcessingContext $context): bool {
|
||||
$this->assertSame('NFO DATA', file_get_contents($path));
|
||||
$context->releaseHasNoNFO = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
);
|
||||
|
||||
$output = Mockery::mock(ConsoleOutputService::class);
|
||||
$output->shouldReceive('echoNfoFound')->once()->andReturnNull();
|
||||
|
||||
$fallback = new ReleaseFilesArchiveFallback(
|
||||
$archive,
|
||||
Mockery::mock(MediaExtractionService::class),
|
||||
$download,
|
||||
$manager,
|
||||
$output
|
||||
);
|
||||
|
||||
$context = $this->makeContext();
|
||||
$context->releaseHasNoNFO = true;
|
||||
$this->assertTrue($context->rememberDownloadedArchive('release.part01.rar', 'ARCHIVE', $files));
|
||||
|
||||
$fallback->processNfoFromDownloadedArchives($context);
|
||||
|
||||
$this->assertFalse($context->releaseHasNoNFO);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_extracts_archive_candidates_in_one_batch(): void
|
||||
{
|
||||
$files = [
|
||||
['name' => 'first.nfo', 'size' => 200],
|
||||
['name' => 'second.nfo', 'size' => 200],
|
||||
];
|
||||
|
||||
$archive = Mockery::mock(ArchiveExtractionService::class);
|
||||
$archive->shouldReceive('sortFilesWithNfoPriority')->once()->with($files)->andReturn($files);
|
||||
$archive->shouldReceive('isNfoFile')->twice()->andReturn(true);
|
||||
$archive->shouldNotReceive('extractSpecificFile');
|
||||
$archive->shouldReceive('extractSpecificFiles')
|
||||
->once()
|
||||
->with('ARCHIVE', ['first.nfo', 'second.nfo'], Mockery::type('string'))
|
||||
->andReturn([
|
||||
'first.nfo' => '',
|
||||
'second.nfo' => 'NFO DATA',
|
||||
]);
|
||||
|
||||
$download = Mockery::mock(UsenetDownloadService::class);
|
||||
$download->shouldNotReceive('download');
|
||||
$download->shouldReceive('getNNTP')->once()->andReturn(Mockery::mock(NNTPService::class));
|
||||
|
||||
$manager = Mockery::mock(ReleaseFileManager::class);
|
||||
$manager->shouldReceive('processNfoFile')->once()->andReturnUsing(
|
||||
function (string $path, ReleaseProcessingContext $context): bool {
|
||||
$this->assertSame('NFO DATA', file_get_contents($path));
|
||||
$context->releaseHasNoNFO = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
);
|
||||
|
||||
$output = Mockery::mock(ConsoleOutputService::class);
|
||||
$output->shouldReceive('echoNfoFound')->once()->andReturnNull();
|
||||
|
||||
$fallback = new ReleaseFilesArchiveFallback(
|
||||
$archive,
|
||||
Mockery::mock(MediaExtractionService::class),
|
||||
$download,
|
||||
$manager,
|
||||
$output,
|
||||
);
|
||||
|
||||
$context = $this->makeContext();
|
||||
$context->releaseHasNoNFO = true;
|
||||
$this->assertTrue($context->rememberDownloadedArchive('release.part01.rar', 'ARCHIVE', $files));
|
||||
|
||||
$fallback->processNfoFromDownloadedArchives($context);
|
||||
|
||||
$this->assertFalse($context->releaseHasNoNFO);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_bounds_and_releases_retained_archive_payloads(): void
|
||||
{
|
||||
$context = $this->makeContext();
|
||||
$files = [['name' => 'release.nfo', 'size' => 10]];
|
||||
|
||||
$this->assertTrue($context->rememberDownloadedArchive('first.rar', 'FIRST', $files));
|
||||
$this->assertTrue($context->rememberDownloadedArchive('second.rar', 'SECOND', $files));
|
||||
$this->assertFalse($context->rememberDownloadedArchive('third.rar', 'THIRD', $files));
|
||||
$this->assertCount(2, $context->downloadedArchives);
|
||||
|
||||
$context->releaseDownloadedArchives();
|
||||
|
||||
$this->assertSame([], $context->downloadedArchives);
|
||||
}
|
||||
|
||||
private function makeContext(int $releaseId = 12): ReleaseProcessingContext
|
||||
{
|
||||
$context = new ReleaseProcessingContext(new Release([
|
||||
|
||||
Reference in New Issue
Block a user