mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
Add AiConfigHealer to bootstrap or repair a store config via a tool-using structured agent (fetch/css/regex tools, HealingContext, shared StrategyExtractor). - Run healing before the one-shot enhancer; AI-bootstrap a store config when product create would otherwise fail; StoreUrl validation defers to self-healing when enabled - Per-feature AI provider resolution (AiFeature enum) and settings - Per-store self-healing opt-out plus cooldown; log heal attempts and unusable proposals - Heal with AI action with non-persisting preview/apply/discard in the store test modal; prompt switches to browser scraping when blocked
62 lines
1.9 KiB
PHP
62 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Models;
|
|
|
|
use App\Models\Product;
|
|
use App\Models\Store;
|
|
use App\Models\Url;
|
|
use App\Models\User;
|
|
use App\Services\AiConfigHealer;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
use Tests\Traits\ScraperTrait;
|
|
|
|
class UrlCreateAiFallbackTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
use ScraperTrait;
|
|
|
|
const URL = 'https://newdomain.test/product/1';
|
|
|
|
public function test_ai_builds_a_store_when_create_would_otherwise_fail(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$this->actingAs($user);
|
|
$product = Product::factory()->create();
|
|
|
|
// No store exists for newdomain.test, so the initial scrape finds nothing.
|
|
// The (mocked) healer creates a working store; the re-scrape then succeeds.
|
|
$this->mock(AiConfigHealer::class, fn ($m) => $m->shouldReceive('healStoreForUrl')
|
|
->once()
|
|
->andReturnUsing(fn ($url, $store, $html) => Store::factory()->create([
|
|
'domains' => [['domain' => 'newdomain.test']],
|
|
])));
|
|
|
|
$this->mockScrape(50, 'Healed Widget');
|
|
|
|
$urlModel = Url::createFromUrl(self::URL, productId: $product->id, createStore: false);
|
|
|
|
$this->assertInstanceOf(Url::class, $urlModel);
|
|
$this->assertSame($product->id, $urlModel->product_id);
|
|
$this->assertSame('newdomain.test', parse_url($urlModel->url, PHP_URL_HOST));
|
|
}
|
|
|
|
public function test_returns_false_and_creates_nothing_when_ai_cannot_heal(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$this->actingAs($user);
|
|
|
|
$this->mock(AiConfigHealer::class, fn ($m) => $m->shouldReceive('healStoreForUrl')
|
|
->once()
|
|
->andReturnNull());
|
|
|
|
$this->mockScrape('', '');
|
|
|
|
$result = Url::createFromUrl(self::URL, createStore: false);
|
|
|
|
$this->assertFalse($result);
|
|
$this->assertDatabaseCount('stores', 0);
|
|
$this->assertDatabaseCount('urls', 0);
|
|
}
|
|
}
|