shouldReceive('getMessagesByMessageID') ->once() ->with([''], false) ->andReturn('BINARY-DATA'); $service = new UsenetDownloadService($this->makeConfig(), $nntp); $result = $service->download(DownloadKind::Compressed, [''], 'alt.binaries', 55, 'archive.rar'); $this->assertTrue($result['success']); $this->assertSame('BINARY-DATA', $result['data']); } #[Test] public function it_marks_group_unavailable_errors_explicitly(): void { $error = new class { public function getMessage(): string { return 'No such news group'; } }; $nntp = Mockery::mock(NNTPService::class); $nntp->shouldReceive('getMessagesByMessageID') ->once() ->with([''], false) ->andReturn($error); $service = new UsenetDownloadService($this->makeConfig(), $nntp); $result = $service->download(DownloadKind::Sample, ['']); $this->assertFalse($result['success']); $this->assertTrue($result['groupUnavailable']); $this->assertStringContainsString('Group unavailable', (string) $result['error']); } #[Test] public function it_reuses_exact_successful_requests_only_within_the_current_release(): void { $nntp = Mockery::mock(NNTPService::class); $nntp->shouldReceive('getMessagesByMessageID') ->twice() ->with([''], false) ->andReturn('SHARED-DATA'); $service = new UsenetDownloadService($this->makeConfig(), $nntp); $service->beginReleaseScope(); $first = $service->download(DownloadKind::Compressed, [''], 'alt.binaries.test', 10); $second = $service->download(DownloadKind::Jpg, [''], 'alt.binaries.test', 10); $firstReleaseMetrics = $service->finishReleaseScope(); $this->assertSame($first, $second); $this->assertSame(2, $firstReleaseMetrics->logicalRequests); $this->assertSame(1, $firstReleaseMetrics->networkRequests); $this->assertSame(1, $firstReleaseMetrics->cacheHits); $this->assertSame(strlen('SHARED-DATA'), $firstReleaseMetrics->bytesDownloaded); $this->assertSame(strlen('SHARED-DATA'), $firstReleaseMetrics->bytesReused); $service->beginReleaseScope(); $service->download(DownloadKind::Compressed, [''], 'alt.binaries.test', 11); $secondReleaseMetrics = $service->finishReleaseScope(); $this->assertSame(1, $secondReleaseMetrics->logicalRequests); $this->assertSame(1, $secondReleaseMetrics->networkRequests); $this->assertSame(0, $secondReleaseMetrics->cacheHits); } #[Test] public function it_does_not_cache_failed_downloads(): void { $nntp = Mockery::mock(NNTPService::class); $nntp->shouldReceive('getMessagesByMessageID') ->twice() ->with([''], false) ->andReturn(''); $service = new UsenetDownloadService($this->makeConfig(), $nntp); $service->beginReleaseScope(); $service->download(DownloadKind::Compressed, ['']); $service->download(DownloadKind::Compressed, ['']); $metrics = $service->finishReleaseScope(); $this->assertSame(2, $metrics->networkRequests); $this->assertSame(0, $metrics->cacheHits); } #[Test] public function it_does_not_reuse_successful_downloads_outside_a_release_scope(): void { $nntp = Mockery::mock(NNTPService::class); $nntp->shouldReceive('getMessagesByMessageID') ->twice() ->with([''], false) ->andReturn('SHARED-DATA'); $service = new UsenetDownloadService($this->makeConfig(), $nntp); $first = $service->download(DownloadKind::Compressed, [''], 'alt.binaries.test', 10); $second = $service->download(DownloadKind::Jpg, [''], 'alt.binaries.test', 10); $this->assertSame('SHARED-DATA', $first['data']); $this->assertSame('SHARED-DATA', $second['data']); } #[Test] public function it_retries_an_exact_request_after_a_partial_provider_failure(): void { $nntp = Mockery::mock(NNTPService::class); $nntp->shouldReceive('getMessagesByMessageID') ->twice() ->with([''], false) ->andReturn('', 'RECOVERED-DATA'); $service = new UsenetDownloadService($this->makeConfig(), $nntp); $service->beginReleaseScope(); $failed = $service->download(DownloadKind::Compressed, ['']); $recovered = $service->download(DownloadKind::Compressed, ['']); $metrics = $service->finishReleaseScope(); $this->assertFalse($failed['success']); $this->assertTrue($recovered['success']); $this->assertSame('RECOVERED-DATA', $recovered['data']); $this->assertSame(2, $metrics->networkRequests); $this->assertSame(0, $metrics->cacheHits); } #[Test] public function it_checks_minimum_download_sizes(): void { $service = new UsenetDownloadService($this->makeConfig(), Mockery::mock(NNTPService::class)); $this->assertFalse($service->meetsMinimumSize(str_repeat('a', 40))); $this->assertTrue($service->meetsMinimumSize(str_repeat('a', 41))); } }