optimizations

This commit is contained in:
2026-03-28 19:15:39 +01:00
parent 0b25d9570a
commit cab4fbd83e
509 changed files with 1016804 additions and 1605 deletions

View File

@@ -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';
});
});