optimizations
This commit is contained in:
110
tests/Feature/Vision/AiArtworkSearchApiTest.php
Normal file
110
tests/Feature/Vision/AiArtworkSearchApiTest.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\Artwork;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use function Pest\Laravel\actingAs;
|
||||
use function Pest\Laravel\getJson;
|
||||
use function Pest\Laravel\postJson;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function (): void {
|
||||
config()->set('vision.vector_gateway.enabled', true);
|
||||
config()->set('vision.vector_gateway.base_url', 'https://vision.klevze.net');
|
||||
config()->set('vision.vector_gateway.api_key', 'test-key');
|
||||
config()->set('vision.vector_gateway.search_endpoint', '/vectors/search');
|
||||
config()->set('cdn.files_url', 'https://files.skinbase.org');
|
||||
config()->set('app.url', 'https://skinbase.test');
|
||||
Storage::fake('public');
|
||||
});
|
||||
|
||||
it('returns AI similar artworks for a public artwork', function (): void {
|
||||
$source = Artwork::factory()->create([
|
||||
'title' => 'Source artwork',
|
||||
'hash' => 'aabbcc112233',
|
||||
'thumb_ext' => 'webp',
|
||||
'is_public' => true,
|
||||
'is_approved' => true,
|
||||
'published_at' => now()->subHour(),
|
||||
]);
|
||||
|
||||
$match = Artwork::factory()->create([
|
||||
'title' => 'AI match',
|
||||
'hash' => 'ddeeff445566',
|
||||
'thumb_ext' => 'webp',
|
||||
'is_public' => true,
|
||||
'is_approved' => true,
|
||||
'published_at' => now()->subHour(),
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://vision.klevze.net/vectors/search' => Http::response([
|
||||
'results' => [
|
||||
['id' => $source->id, 'score' => 1.0],
|
||||
['id' => $match->id, 'score' => 0.91234],
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$response = getJson('/api/art/' . $source->id . '/similar-ai');
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonPath('data.0.id', $match->id)
|
||||
->assertJsonPath('data.0.source', 'vector_gateway')
|
||||
->assertJsonPath('meta.artwork_id', $source->id)
|
||||
->assertJsonCount(1, 'data');
|
||||
});
|
||||
|
||||
it('returns 404 for missing similar-ai source artwork', function (): void {
|
||||
getJson('/api/art/999999/similar-ai')
|
||||
->assertStatus(404)
|
||||
->assertJsonPath('error', 'Artwork not found');
|
||||
});
|
||||
|
||||
it('searches by uploaded image through the vector gateway', function (): void {
|
||||
$user = User::factory()->create();
|
||||
$match = Artwork::factory()->create([
|
||||
'title' => 'Reverse image match',
|
||||
'hash' => '112233445566',
|
||||
'thumb_ext' => 'webp',
|
||||
'is_public' => true,
|
||||
'is_approved' => true,
|
||||
'published_at' => now()->subHour(),
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://vision.klevze.net/vectors/search' => Http::response([
|
||||
'results' => [
|
||||
['id' => $match->id, 'score' => 0.88765],
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
actingAs($user);
|
||||
|
||||
$response = postJson('/api/search/image', [
|
||||
'image' => UploadedFile::fake()->image('query.png', 640, 480),
|
||||
'limit' => 12,
|
||||
]);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonPath('data.0.id', $match->id)
|
||||
->assertJsonPath('data.0.source', 'vector_gateway')
|
||||
->assertJsonPath('meta.limit', 12);
|
||||
|
||||
Http::assertSent(function ($request): bool {
|
||||
$payload = json_decode($request->body(), true);
|
||||
|
||||
return $request->url() === 'https://vision.klevze.net/vectors/search'
|
||||
&& $request->hasHeader('X-API-Key', 'test-key')
|
||||
&& is_array($payload)
|
||||
&& str_contains((string) ($payload['url'] ?? ''), '/storage/ai-search/tmp/')
|
||||
&& ($payload['limit'] ?? null) === 12;
|
||||
});
|
||||
});
|
||||
@@ -3,8 +3,10 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\Artwork;
|
||||
use App\Models\ArtworkEmbedding;
|
||||
use App\Models\Category;
|
||||
use App\Models\ContentType;
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use function Pest\Laravel\artisan;
|
||||
@@ -43,6 +45,8 @@ it('indexes artworks into the vector gateway with artwork metadata', function ()
|
||||
'thumb_ext' => 'webp',
|
||||
]);
|
||||
$artwork->categories()->attach($category->id);
|
||||
$tag = Tag::query()->create(['name' => 'Skyline', 'slug' => 'skyline']);
|
||||
$artwork->tags()->attach($tag->id, ['source' => 'ai', 'confidence' => 0.88]);
|
||||
|
||||
Http::fake([
|
||||
'https://vision.klevze.net/vectors/upsert' => Http::response(['ok' => true], 200),
|
||||
@@ -51,6 +55,9 @@ it('indexes artworks into the vector gateway with artwork metadata', function ()
|
||||
artisan('artworks:vectors-index', ['--limit' => 1])
|
||||
->assertSuccessful();
|
||||
|
||||
$artwork->refresh();
|
||||
expect($artwork->last_vector_indexed_at)->not->toBeNull();
|
||||
|
||||
Http::assertSent(function ($request) use ($artwork): bool {
|
||||
if ($request->url() !== 'https://vision.klevze.net/vectors/upsert') {
|
||||
return false;
|
||||
@@ -63,7 +70,8 @@ it('indexes artworks into the vector gateway with artwork metadata', function ()
|
||||
&& ($payload['id'] ?? null) === (string) $artwork->id
|
||||
&& ($payload['url'] ?? null) === 'https://files.skinbase.org/md/aa/bb/aabbcc112233.webp'
|
||||
&& ($payload['metadata']['content_type'] ?? null) === 'Photography'
|
||||
&& ($payload['metadata']['category'] ?? null) === 'Abstract';
|
||||
&& ($payload['metadata']['category'] ?? null) === 'Abstract'
|
||||
&& ($payload['metadata']['tags'] ?? null) === ['skyline'];
|
||||
});
|
||||
});
|
||||
|
||||
@@ -120,3 +128,78 @@ it('searches similar artworks through the vector gateway', function (): void {
|
||||
]])
|
||||
->assertSuccessful();
|
||||
});
|
||||
|
||||
it('can re-upsert only artworks that already have local embeddings', function (): void {
|
||||
$contentType = ContentType::query()->create([
|
||||
'name' => 'Photography',
|
||||
'slug' => 'photography',
|
||||
'description' => '',
|
||||
]);
|
||||
|
||||
$category = Category::query()->create([
|
||||
'content_type_id' => $contentType->id,
|
||||
'parent_id' => null,
|
||||
'name' => 'Portraits',
|
||||
'slug' => 'portraits',
|
||||
'description' => '',
|
||||
'is_active' => true,
|
||||
'sort_order' => 0,
|
||||
]);
|
||||
|
||||
$embeddedArtwork = Artwork::factory()->create([
|
||||
'hash' => '112233445566',
|
||||
'thumb_ext' => 'webp',
|
||||
'is_public' => true,
|
||||
'is_approved' => true,
|
||||
'published_at' => now()->subDay(),
|
||||
]);
|
||||
$embeddedArtwork->categories()->attach($category->id);
|
||||
|
||||
ArtworkEmbedding::query()->create([
|
||||
'artwork_id' => $embeddedArtwork->id,
|
||||
'model' => 'clip',
|
||||
'model_version' => 'v1',
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'dim' => 2,
|
||||
'embedding_json' => json_encode([0.6, 0.8], JSON_THROW_ON_ERROR),
|
||||
'source_hash' => '112233445566',
|
||||
'is_normalized' => true,
|
||||
'generated_at' => now(),
|
||||
'meta' => ['source' => 'clip'],
|
||||
]);
|
||||
|
||||
$nonEmbeddedArtwork = Artwork::factory()->create([
|
||||
'hash' => 'aabbccddeeff',
|
||||
'thumb_ext' => 'webp',
|
||||
'is_public' => true,
|
||||
'is_approved' => true,
|
||||
'published_at' => now()->subDay(),
|
||||
]);
|
||||
$nonEmbeddedArtwork->categories()->attach($category->id);
|
||||
|
||||
Http::fake([
|
||||
'https://vision.klevze.net/vectors/upsert' => Http::response(['ok' => true], 200),
|
||||
]);
|
||||
|
||||
artisan('artworks:vectors-index', ['--embedded-only' => true, '--limit' => 10])
|
||||
->assertSuccessful();
|
||||
|
||||
$embeddedArtwork->refresh();
|
||||
$nonEmbeddedArtwork->refresh();
|
||||
|
||||
expect($embeddedArtwork->last_vector_indexed_at)->not->toBeNull()
|
||||
->and($nonEmbeddedArtwork->last_vector_indexed_at)->toBeNull();
|
||||
|
||||
Http::assertSentCount(1);
|
||||
Http::assertSent(function ($request) use ($embeddedArtwork): bool {
|
||||
if ($request->url() !== 'https://vision.klevze.net/vectors/upsert') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$payload = json_decode($request->body(), true);
|
||||
|
||||
return is_array($payload)
|
||||
&& ($payload['id'] ?? null) === (string) $embeddedArtwork->id
|
||||
&& ($payload['url'] ?? null) === 'https://files.skinbase.org/md/11/22/112233445566.webp';
|
||||
});
|
||||
});
|
||||
|
||||
153
tests/Feature/Vision/ArtworkVectorRepairQueueTest.php
Normal file
153
tests/Feature/Vision/ArtworkVectorRepairQueueTest.php
Normal file
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Jobs\BackfillArtworkVectorIndexJob;
|
||||
use App\Jobs\SyncArtworkVectorIndexJob;
|
||||
use App\Models\Artwork;
|
||||
use App\Models\ArtworkEmbedding;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use function Pest\Laravel\artisan;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
function jobProperty(object $job, string $property): mixed
|
||||
{
|
||||
$reflection = new ReflectionProperty($job, $property);
|
||||
$reflection->setAccessible(true);
|
||||
|
||||
return $reflection->getValue($job);
|
||||
}
|
||||
|
||||
it('queues a resumable vector repair run from the command', function (): void {
|
||||
Queue::fake();
|
||||
|
||||
artisan('artworks:vectors-repair', [
|
||||
'--after-id' => 25,
|
||||
'--batch' => 50,
|
||||
'--public-only' => true,
|
||||
'--stale-hours' => 24,
|
||||
])->assertSuccessful();
|
||||
|
||||
Queue::assertPushed(BackfillArtworkVectorIndexJob::class, function (BackfillArtworkVectorIndexJob $job): bool {
|
||||
return jobProperty($job, 'afterId') === 25
|
||||
&& jobProperty($job, 'batchSize') === 50
|
||||
&& jobProperty($job, 'publicOnly') === true
|
||||
&& jobProperty($job, 'staleHours') === 24;
|
||||
});
|
||||
});
|
||||
|
||||
it('fans out queued vector repair only for artworks with local embeddings', function (): void {
|
||||
Queue::fake();
|
||||
|
||||
$embeddedArtwork = Artwork::factory()->create([
|
||||
'hash' => '1122334455667788',
|
||||
'is_public' => true,
|
||||
'is_approved' => true,
|
||||
'published_at' => now()->subDay(),
|
||||
]);
|
||||
ArtworkEmbedding::query()->create([
|
||||
'artwork_id' => $embeddedArtwork->id,
|
||||
'model' => 'clip',
|
||||
'model_version' => 'v1',
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'dim' => 2,
|
||||
'embedding_json' => json_encode([0.6, 0.8], JSON_THROW_ON_ERROR),
|
||||
'source_hash' => '1122334455667788',
|
||||
'is_normalized' => true,
|
||||
'generated_at' => now(),
|
||||
'meta' => ['source' => 'clip'],
|
||||
]);
|
||||
|
||||
Artwork::factory()->create([
|
||||
'hash' => 'aabbccddeeff0011',
|
||||
'is_public' => true,
|
||||
'is_approved' => true,
|
||||
'published_at' => now()->subDay(),
|
||||
]);
|
||||
|
||||
(new BackfillArtworkVectorIndexJob(0, 100, true))->handle();
|
||||
|
||||
Queue::assertPushed(SyncArtworkVectorIndexJob::class, function (SyncArtworkVectorIndexJob $job) use ($embeddedArtwork): bool {
|
||||
return jobProperty($job, 'artworkId') === $embeddedArtwork->id;
|
||||
});
|
||||
Queue::assertNotPushed(BackfillArtworkVectorIndexJob::class);
|
||||
});
|
||||
|
||||
it('can target only stale or never-indexed artworks during queued repair', function (): void {
|
||||
Queue::fake();
|
||||
|
||||
$staleArtwork = Artwork::factory()->create([
|
||||
'hash' => '1111222233334444',
|
||||
'is_public' => true,
|
||||
'is_approved' => true,
|
||||
'published_at' => now()->subDay(),
|
||||
'last_vector_indexed_at' => now()->subHours(48),
|
||||
]);
|
||||
ArtworkEmbedding::query()->create([
|
||||
'artwork_id' => $staleArtwork->id,
|
||||
'model' => 'clip',
|
||||
'model_version' => 'v1',
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'dim' => 2,
|
||||
'embedding_json' => json_encode([0.6, 0.8], JSON_THROW_ON_ERROR),
|
||||
'source_hash' => '1111222233334444',
|
||||
'is_normalized' => true,
|
||||
'generated_at' => now(),
|
||||
'meta' => ['source' => 'clip'],
|
||||
]);
|
||||
|
||||
$missingArtwork = Artwork::factory()->create([
|
||||
'hash' => '5555666677778888',
|
||||
'is_public' => true,
|
||||
'is_approved' => true,
|
||||
'published_at' => now()->subDay(),
|
||||
'last_vector_indexed_at' => null,
|
||||
]);
|
||||
ArtworkEmbedding::query()->create([
|
||||
'artwork_id' => $missingArtwork->id,
|
||||
'model' => 'clip',
|
||||
'model_version' => 'v1',
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'dim' => 2,
|
||||
'embedding_json' => json_encode([0.6, 0.8], JSON_THROW_ON_ERROR),
|
||||
'source_hash' => '5555666677778888',
|
||||
'is_normalized' => true,
|
||||
'generated_at' => now(),
|
||||
'meta' => ['source' => 'clip'],
|
||||
]);
|
||||
|
||||
$freshArtwork = Artwork::factory()->create([
|
||||
'hash' => '9999aaaabbbbcccc',
|
||||
'is_public' => true,
|
||||
'is_approved' => true,
|
||||
'published_at' => now()->subDay(),
|
||||
'last_vector_indexed_at' => now()->subHours(2),
|
||||
]);
|
||||
ArtworkEmbedding::query()->create([
|
||||
'artwork_id' => $freshArtwork->id,
|
||||
'model' => 'clip',
|
||||
'model_version' => 'v1',
|
||||
'algo_version' => 'clip-cosine-v1',
|
||||
'dim' => 2,
|
||||
'embedding_json' => json_encode([0.6, 0.8], JSON_THROW_ON_ERROR),
|
||||
'source_hash' => '9999aaaabbbbcccc',
|
||||
'is_normalized' => true,
|
||||
'generated_at' => now(),
|
||||
'meta' => ['source' => 'clip'],
|
||||
]);
|
||||
|
||||
(new BackfillArtworkVectorIndexJob(0, 100, true, 24))->handle();
|
||||
|
||||
Queue::assertPushed(SyncArtworkVectorIndexJob::class, function (SyncArtworkVectorIndexJob $job) use ($staleArtwork): bool {
|
||||
return jobProperty($job, 'artworkId') === $staleArtwork->id;
|
||||
});
|
||||
Queue::assertPushed(SyncArtworkVectorIndexJob::class, function (SyncArtworkVectorIndexJob $job) use ($missingArtwork): bool {
|
||||
return jobProperty($job, 'artworkId') === $missingArtwork->id;
|
||||
});
|
||||
Queue::assertNotPushed(SyncArtworkVectorIndexJob::class, function (SyncArtworkVectorIndexJob $job) use ($freshArtwork): bool {
|
||||
return jobProperty($job, 'artworkId') === $freshArtwork->id;
|
||||
});
|
||||
Queue::assertNotPushed(BackfillArtworkVectorIndexJob::class);
|
||||
});
|
||||
141
tests/Feature/Vision/GenerateArtworkEmbeddingJobTest.php
Normal file
141
tests/Feature/Vision/GenerateArtworkEmbeddingJobTest.php
Normal file
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Jobs\GenerateArtworkEmbeddingJob;
|
||||
use App\Models\Artwork;
|
||||
use App\Models\ArtworkEmbedding;
|
||||
use App\Models\Category;
|
||||
use App\Models\ContentType;
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
it('persists a normalized embedding and upserts the artwork to the vector gateway', function () {
|
||||
config()->set('recommendations.embedding.enabled', true);
|
||||
config()->set('recommendations.embedding.endpoint', '/embed');
|
||||
config()->set('recommendations.embedding.min_dim', 2);
|
||||
config()->set('vision.clip.base_url', 'https://clip.local');
|
||||
config()->set('vision.vector_gateway.enabled', true);
|
||||
config()->set('vision.vector_gateway.base_url', 'https://vision.local');
|
||||
config()->set('vision.vector_gateway.api_key', 'test-key');
|
||||
config()->set('vision.vector_gateway.upsert_endpoint', '/vectors/upsert');
|
||||
config()->set('cdn.files_url', 'https://files.local');
|
||||
|
||||
Http::fake([
|
||||
'https://clip.local/embed' => Http::response([
|
||||
'embedding' => [3.0, 4.0],
|
||||
], 200),
|
||||
'https://vision.local/vectors/upsert' => Http::response([
|
||||
'status' => 'ok',
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$contentType = ContentType::query()->create([
|
||||
'name' => 'Wallpapers',
|
||||
'slug' => 'wallpapers',
|
||||
'description' => '',
|
||||
]);
|
||||
|
||||
$category = Category::query()->create([
|
||||
'content_type_id' => $contentType->id,
|
||||
'parent_id' => null,
|
||||
'name' => 'Abstract',
|
||||
'slug' => 'abstract',
|
||||
'description' => '',
|
||||
'image' => null,
|
||||
'is_active' => true,
|
||||
'sort_order' => 10,
|
||||
]);
|
||||
|
||||
$artwork = Artwork::factory()->create([
|
||||
'hash' => 'aabbccddeeff1122',
|
||||
'thumb_ext' => 'webp',
|
||||
'is_public' => true,
|
||||
'is_approved' => true,
|
||||
'published_at' => now()->subDay(),
|
||||
]);
|
||||
$artwork->categories()->attach($category->id);
|
||||
$tag = Tag::query()->create(['name' => 'Neon', 'slug' => 'neon']);
|
||||
$artwork->tags()->attach($tag->id, ['source' => 'ai', 'confidence' => 0.91]);
|
||||
|
||||
$job = new GenerateArtworkEmbeddingJob($artwork->id, 'aabbccddeeff1122');
|
||||
$job->handle(
|
||||
app(\App\Services\Vision\ArtworkEmbeddingClient::class),
|
||||
app(\App\Services\Vision\ArtworkVisionImageUrl::class),
|
||||
app(\App\Services\Vision\ArtworkVectorIndexService::class),
|
||||
);
|
||||
|
||||
$embedding = ArtworkEmbedding::query()->where('artwork_id', $artwork->id)->first();
|
||||
$artwork->refresh();
|
||||
|
||||
expect($embedding)->not->toBeNull()
|
||||
->and($embedding?->dim)->toBe(2)
|
||||
->and($embedding?->is_normalized)->toBeTrue()
|
||||
->and($artwork->last_vector_indexed_at)->not->toBeNull();
|
||||
|
||||
$vector = json_decode((string) $embedding?->embedding_json, true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
expect(round((float) $vector[0], 4))->toBe(0.6)
|
||||
->and(round((float) $vector[1], 4))->toBe(0.8);
|
||||
|
||||
Http::assertSent(function (\Illuminate\Http\Client\Request $request) use ($artwork): bool {
|
||||
if ($request->url() !== 'https://vision.local/vectors/upsert') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = $request->data();
|
||||
|
||||
return ($data['id'] ?? null) === (string) $artwork->id
|
||||
&& ($data['url'] ?? null) === 'https://files.local/md/aa/bb/aabbccddeeff1122.webp'
|
||||
&& ($data['metadata']['content_type'] ?? null) === 'Wallpapers'
|
||||
&& ($data['metadata']['category'] ?? null) === 'Abstract'
|
||||
&& ($data['metadata']['tags'] ?? null) === ['neon']
|
||||
&& ($data['metadata']['user_id'] ?? null) === (string) $artwork->user_id;
|
||||
});
|
||||
});
|
||||
|
||||
it('keeps the local embedding when vector upsert fails', function () {
|
||||
config()->set('recommendations.embedding.enabled', true);
|
||||
config()->set('recommendations.embedding.endpoint', '/embed');
|
||||
config()->set('recommendations.embedding.min_dim', 2);
|
||||
config()->set('vision.clip.base_url', 'https://clip.local');
|
||||
config()->set('vision.vector_gateway.enabled', true);
|
||||
config()->set('vision.vector_gateway.base_url', 'https://vision.local');
|
||||
config()->set('vision.vector_gateway.api_key', 'test-key');
|
||||
config()->set('vision.vector_gateway.upsert_endpoint', '/vectors/upsert');
|
||||
config()->set('cdn.files_url', 'https://files.local');
|
||||
|
||||
Http::fake([
|
||||
'https://clip.local/embed' => Http::response([
|
||||
'embedding' => [1.0, 2.0, 2.0],
|
||||
], 200),
|
||||
'https://vision.local/vectors/upsert' => Http::response([
|
||||
'message' => 'gateway error',
|
||||
], 500),
|
||||
]);
|
||||
|
||||
$artwork = Artwork::factory()->create([
|
||||
'hash' => '1122334455667788',
|
||||
'thumb_ext' => 'webp',
|
||||
'is_public' => true,
|
||||
'is_approved' => true,
|
||||
'published_at' => now()->subDay(),
|
||||
]);
|
||||
|
||||
$job = new GenerateArtworkEmbeddingJob($artwork->id, '1122334455667788');
|
||||
$job->handle(
|
||||
app(\App\Services\Vision\ArtworkEmbeddingClient::class),
|
||||
app(\App\Services\Vision\ArtworkVisionImageUrl::class),
|
||||
app(\App\Services\Vision\ArtworkVectorIndexService::class),
|
||||
);
|
||||
|
||||
$artwork->refresh();
|
||||
|
||||
expect(ArtworkEmbedding::query()->where('artwork_id', $artwork->id)->exists())->toBeTrue()
|
||||
->and($artwork->last_vector_indexed_at)->toBeNull();
|
||||
|
||||
Http::assertSentCount(2);
|
||||
});
|
||||
82
tests/Feature/Vision/UploadVisionSuggestApiTest.php
Normal file
82
tests/Feature/Vision/UploadVisionSuggestApiTest.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\Artwork;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use function Pest\Laravel\actingAs;
|
||||
use function Pest\Laravel\postJson;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
it('returns normalized synchronous vision tag suggestions for the artwork owner', function (): void {
|
||||
config()->set('vision.enabled', true);
|
||||
config()->set('vision.gateway.base_url', 'https://vision.local');
|
||||
config()->set('cdn.files_url', 'https://files.local');
|
||||
|
||||
$user = User::factory()->create();
|
||||
$artwork = Artwork::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'hash' => 'aabbcc112233',
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://vision.local/analyze/all' => Http::response([
|
||||
'clip' => [
|
||||
['tag' => 'Neon City', 'confidence' => 0.91],
|
||||
['tag' => 'Night Sky', 'confidence' => 0.77],
|
||||
],
|
||||
'yolo' => [
|
||||
['label' => 'car', 'confidence' => 0.65],
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
actingAs($user);
|
||||
|
||||
$response = postJson('/api/uploads/' . $artwork->id . '/vision-suggest?limit=10');
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonPath('vision_enabled', true)
|
||||
->assertJsonPath('source', 'gateway_sync')
|
||||
->assertJsonPath('tags.0.slug', 'neon-city')
|
||||
->assertJsonPath('tags.0.source', 'clip')
|
||||
->assertJsonPath('tags.1.slug', 'night-sky')
|
||||
->assertJsonPath('tags.2.slug', 'car');
|
||||
});
|
||||
|
||||
it('returns 404 when a non-owner requests upload vision suggestions', function (): void {
|
||||
config()->set('vision.enabled', true);
|
||||
config()->set('vision.gateway.base_url', 'https://vision.local');
|
||||
config()->set('cdn.files_url', 'https://files.local');
|
||||
|
||||
$owner = User::factory()->create();
|
||||
$viewer = User::factory()->create();
|
||||
$artwork = Artwork::factory()->create([
|
||||
'user_id' => $owner->id,
|
||||
'hash' => 'aabbcc112233',
|
||||
]);
|
||||
|
||||
actingAs($viewer);
|
||||
|
||||
postJson('/api/uploads/' . $artwork->id . '/vision-suggest')
|
||||
->assertStatus(404);
|
||||
});
|
||||
|
||||
it('returns disabled payload when vision suggestions are turned off', function (): void {
|
||||
config()->set('vision.enabled', false);
|
||||
|
||||
$user = User::factory()->create();
|
||||
$artwork = Artwork::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
actingAs($user);
|
||||
|
||||
postJson('/api/uploads/' . $artwork->id . '/vision-suggest')
|
||||
->assertOk()
|
||||
->assertJsonPath('vision_enabled', false)
|
||||
->assertJsonPath('tags', []);
|
||||
});
|
||||
Reference in New Issue
Block a user