Files
SkinbaseNova/tests/Feature/Artworks/ThumbnailServiceM17CTest.php
T
klevze b9beb8f5c5 Speed legacy photo serving and skip tracking on download routes.
Tighten thumbnail/legacy photo handling and exclude download/photo traffic from session visitor tracking so those hot paths stay cheap.
2026-08-29 12:25:37 +02:00

89 lines
3.0 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\Artwork;
use App\Services\ThumbnailService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
beforeEach(function (): void {
config([
'cdn.files_url' => 'https://cdn.example.test',
'uploads.object_storage.prefix' => 'artworks',
]);
});
it('resolves an existing artwork id to the same CDN URL as fromHash', function (): void {
$hash = 'aabbccddeeff00112233445566778899';
$artwork = Artwork::factory()->create([
'hash' => $hash,
'thumb_ext' => 'webp',
]);
$expected = ThumbnailService::fromHash($hash, 'webp', 'md');
expect(ThumbnailService::url(null, (int) $artwork->id, null, 6))->toBe($expected);
expect($expected)->toBe('https://cdn.example.test/artworks/md/aa/bb/aabbccddeeff00112233445566778899.webp');
});
it('returns the empty-string fallback for a missing artwork id', function (): void {
expect(ThumbnailService::url(null, 99999999, null, 6))->toBe('');
});
it('falls back to Storage::url when a file path is supplied and id lookup misses', function (): void {
$path = 'uploads/artworks/fallback.jpg';
expect(ThumbnailService::url($path, 99999999, null, 6))->toBe(Storage::url($path));
});
it('builds a CDN URL from a direct hash and extension without querying artworks', function (): void {
DB::flushQueryLog();
DB::enableQueryLog();
$url = ThumbnailService::url('11223344556677889900aabbccddeeff', null, 'webp', 'xl');
expect($url)->toBe('https://cdn.example.test/artworks/xl/11/22/11223344556677889900aabbccddeeff.webp');
$legacyIdQueries = collect(DB::getQueryLog())
->filter(fn (array $query): bool => str_contains(strtolower((string) $query['query']), 'legacy_id'));
expect($legacyIdQueries)->toBeEmpty();
DB::disableQueryLog();
});
it('maps numeric size 4 to the thumb/sm CDN path', function (): void {
$hash = 'deadbeefcafebabe0123456789abcdef';
$artwork = Artwork::factory()->create([
'hash' => $hash,
'thumb_ext' => 'jpg',
]);
$url = ThumbnailService::url(null, (int) $artwork->id, null, 4);
$expected = ThumbnailService::fromHash($hash, 'jpg', 'thumb');
expect($url)->toBe($expected);
expect($url)->toBe('https://cdn.example.test/artworks/sm/de/ad/deadbeefcafebabe0123456789abcdef.jpg');
});
it('does not query artworks.legacy_id when resolving a thumbnail by artwork id', function (): void {
$artwork = Artwork::factory()->create([
'hash' => '00112233445566778899aabbccddeeff',
'thumb_ext' => 'webp',
]);
DB::flushQueryLog();
DB::enableQueryLog();
ThumbnailService::url(null, (int) $artwork->id, null, 'md');
$queries = collect(DB::getQueryLog())->map(fn (array $query): string => strtolower((string) $query['query']));
expect($queries)->not->toBeEmpty();
expect($queries->filter(fn (string $sql): bool => str_contains($sql, 'legacy_id')))->toBeEmpty();
expect($queries->filter(fn (string $sql): bool => str_contains($sql, 'artworks')))->not->toBeEmpty();
DB::disableQueryLog();
});