mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
When a product went unavailable or was removed, its page had no price. Self-healing then ran on that page. The fixed selector list matched ".price" on related-product buttons (for example on imusic.au) and saved it as the store's price selector. That broke every product of the store. Before a heal, AiConfigHealer now scrapes another in-stock product of the same store that has a saved price, using the current config and the scrape cache. If that scrape finds a title and a price, the config is not broken, so the heal does not run and the cooldown is not set. heal() and healStoreForUrl() both use the check. The manual "Heal with AI" preview does not. Closes #220
413 lines
16 KiB
PHP
413 lines
16 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Services;
|
|
|
|
use App\Enums\StockStatus;
|
|
use App\Models\Price;
|
|
use App\Models\Store;
|
|
use App\Models\Url;
|
|
use App\Services\AiConfigHealer;
|
|
use App\Services\AiService;
|
|
use App\Services\Helpers\SettingsHelper;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Once;
|
|
use Jez500\WebScraperForLaravel\Facades\WebScraper;
|
|
use Jez500\WebScraperForLaravel\WebScraperFake;
|
|
use Mockery;
|
|
use Tests\TestCase;
|
|
|
|
class AiConfigHealerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
SettingsHelper::$settings = null;
|
|
Cache::flush();
|
|
Once::flush();
|
|
|
|
// The deterministic step may escalate to a browser fetch; return non-structured
|
|
// HTML so heuristics find nothing and the mocked agent path is exercised.
|
|
WebScraper::shouldReceive('make')->andReturn((new WebScraperFake)->setBody($this->html()));
|
|
}
|
|
|
|
private function configureProviders(array $aiOverrides = []): void
|
|
{
|
|
SettingsHelper::setSetting('integrated_services', ['ai' => array_merge([
|
|
'enabled' => true,
|
|
'default_provider_id' => 'p1',
|
|
'providers' => [[
|
|
'id' => 'p1', 'name' => 'Local', 'type' => 'ollama',
|
|
'base_url' => 'http://ai.example:11434', 'model' => 'm',
|
|
]],
|
|
], $aiOverrides)]);
|
|
SettingsHelper::$settings = null;
|
|
Cache::flush();
|
|
Once::flush();
|
|
}
|
|
|
|
private function url(array $settings = []): Url
|
|
{
|
|
$store = Store::factory()->create([
|
|
'scrape_strategy' => [],
|
|
'settings' => array_merge(['scraper_service' => 'http'], $settings),
|
|
]);
|
|
|
|
return Url::factory()->for($store)->create(['url' => 'https://shop.test/widget']);
|
|
}
|
|
|
|
private function html(): string
|
|
{
|
|
return '<html><body><div class="t">Widget</div><span id="pr">$12.99</span></body></html>';
|
|
}
|
|
|
|
private function mockAgent(?array $proposal, string $expectation = 'once'): void
|
|
{
|
|
$this->mock(AiService::class, fn ($m) => $m->shouldReceive('runAgent')->{$expectation}()->andReturn($proposal));
|
|
}
|
|
|
|
public function test_heals_config_applies_selectors_and_recovers_price(): void
|
|
{
|
|
$this->configureProviders();
|
|
$this->mockAgent([
|
|
'is_product' => true,
|
|
'fields' => [
|
|
'title' => ['type' => 'selector', 'value' => '.t'],
|
|
'price' => ['type' => 'selector', 'value' => '#pr'],
|
|
],
|
|
]);
|
|
$url = $this->url();
|
|
|
|
$result = AiConfigHealer::new()->heal($url, ['price' => null, 'body' => $this->html(), 'availability' => null]);
|
|
|
|
$this->assertSame('$12.99', $result['price']);
|
|
$this->assertSame('Widget', $result['title']);
|
|
$this->assertSame('#pr', data_get($url->store->fresh()->scrape_strategy, 'price.value'));
|
|
$this->assertNull($url->store->fresh()->getAiHealFailedAt());
|
|
}
|
|
|
|
public function test_heal_merges_into_existing_strategy_without_dropping_other_fields(): void
|
|
{
|
|
$this->configureProviders();
|
|
$this->mockAgent([
|
|
'is_product' => true,
|
|
'fields' => [
|
|
'title' => ['type' => 'selector', 'value' => '.t'],
|
|
'price' => ['type' => 'selector', 'value' => '#pr'],
|
|
],
|
|
]);
|
|
$store = Store::factory()->create([
|
|
'scrape_strategy' => ['image' => ['type' => 'selector', 'value' => 'img.hero|src']],
|
|
'settings' => ['scraper_service' => 'http'],
|
|
]);
|
|
$url = Url::factory()->for($store)->create(['url' => 'https://shop.test/widget']);
|
|
|
|
AiConfigHealer::new()->heal($url, ['price' => null, 'body' => $this->html(), 'availability' => null]);
|
|
|
|
$strategy = $store->fresh()->scrape_strategy;
|
|
$this->assertSame('img.hero|src', data_get($strategy, 'image.value'));
|
|
$this->assertSame('#pr', data_get($strategy, 'price.value'));
|
|
$this->assertSame('.t', data_get($strategy, 'title.value'));
|
|
}
|
|
|
|
public function test_logs_when_a_heal_is_attempted(): void
|
|
{
|
|
$this->configureProviders();
|
|
$this->mockAgent([
|
|
'is_product' => true,
|
|
'fields' => [
|
|
'title' => ['type' => 'selector', 'value' => '.t'],
|
|
'price' => ['type' => 'selector', 'value' => '#pr'],
|
|
],
|
|
]);
|
|
$url = $this->url();
|
|
|
|
AiConfigHealer::new()->heal($url, ['price' => null, 'body' => $this->html(), 'availability' => null]);
|
|
|
|
$this->assertDatabaseHas('log_messages', [
|
|
'message' => 'AI self-healing started; attempting to repair store scraper config.',
|
|
]);
|
|
}
|
|
|
|
public function test_no_op_when_price_already_present(): void
|
|
{
|
|
$this->configureProviders();
|
|
$this->mockAgent([], 'never');
|
|
|
|
$result = AiConfigHealer::new()->heal($this->url(), ['price' => '5.00', 'body' => $this->html()]);
|
|
|
|
$this->assertSame('5.00', $result['price']);
|
|
}
|
|
|
|
public function test_skips_when_store_opted_out(): void
|
|
{
|
|
$this->configureProviders();
|
|
$this->mockAgent([], 'never');
|
|
|
|
$result = AiConfigHealer::new()->heal(
|
|
$this->url(['ai_self_healing_disabled' => true]),
|
|
['price' => null, 'body' => $this->html()],
|
|
);
|
|
|
|
$this->assertNull($result['price']);
|
|
}
|
|
|
|
public function test_skips_when_feature_disabled_globally(): void
|
|
{
|
|
$this->configureProviders(['feature_providers' => ['healing' => '__disabled__']]);
|
|
$this->mockAgent([], 'never');
|
|
|
|
$result = AiConfigHealer::new()->heal($this->url(), ['price' => null, 'body' => $this->html()]);
|
|
|
|
$this->assertNull($result['price']);
|
|
}
|
|
|
|
public function test_skips_when_within_cooldown(): void
|
|
{
|
|
$this->configureProviders();
|
|
$this->mockAgent([], 'never');
|
|
$url = $this->url();
|
|
$url->store->markAiHealFailed();
|
|
|
|
$result = AiConfigHealer::new()->heal($url, ['price' => null, 'body' => $this->html()]);
|
|
|
|
$this->assertNull($result['price']);
|
|
}
|
|
|
|
public function test_skips_when_out_of_stock(): void
|
|
{
|
|
$this->configureProviders();
|
|
$this->mockAgent([], 'never');
|
|
|
|
$result = AiConfigHealer::new()->heal(
|
|
$this->url(),
|
|
['price' => null, 'body' => $this->html(), 'availability' => 'OutOfStock'],
|
|
);
|
|
|
|
$this->assertNull($result['price']);
|
|
}
|
|
|
|
public function test_skips_when_store_treats_missing_price_as_out_of_stock(): void
|
|
{
|
|
$this->configureProviders();
|
|
$this->mockAgent([], 'never');
|
|
$url = $this->url(['missing_price_out_of_stock' => true]);
|
|
|
|
$result = AiConfigHealer::new()->heal(
|
|
$url,
|
|
['store' => $url->store, 'title' => 'Widget', 'price' => null, 'body' => $this->html()],
|
|
);
|
|
|
|
$this->assertNull($result['price']);
|
|
}
|
|
|
|
/**
|
|
* A product that goes unavailable or is removed has no price. When the same
|
|
* config still works on another product of the store, the page is the problem,
|
|
* not the config, so healing must not rewrite the store (issue #220).
|
|
*/
|
|
public function test_skips_when_config_still_works_on_another_product(): void
|
|
{
|
|
$this->configureProviders();
|
|
$this->mockAgent([], 'never');
|
|
$strategy = $this->workingStrategy();
|
|
$store = Store::factory()->create(['scrape_strategy' => $strategy, 'settings' => ['scraper_service' => 'http']]);
|
|
$this->pricedUrl($store, 'https://shop.test/other');
|
|
$url = Url::factory()->for($store)->create(['url' => 'https://shop.test/removed']);
|
|
|
|
$result = AiConfigHealer::new()->heal($url, ['price' => null, 'title' => 'Removed', 'body' => '<html></html>']);
|
|
|
|
$this->assertNull($result['price']);
|
|
$this->assertSame($strategy, $store->fresh()->scrape_strategy->toArray());
|
|
$this->assertNull($store->fresh()->getAiHealFailedAt());
|
|
$this->assertDatabaseHas('log_messages', [
|
|
'message' => 'AI self-healing skipped; the store config still works on another product.',
|
|
]);
|
|
}
|
|
|
|
public function test_heals_when_config_also_fails_on_another_product(): void
|
|
{
|
|
$this->configureProviders();
|
|
$this->mockAgent([
|
|
'is_product' => true,
|
|
'fields' => [
|
|
'title' => ['type' => 'selector', 'value' => '.t'],
|
|
'price' => ['type' => 'selector', 'value' => '#pr'],
|
|
],
|
|
]);
|
|
$store = Store::factory()->create([
|
|
'scrape_strategy' => [
|
|
'title' => ['type' => 'selector', 'value' => '.t'],
|
|
'price' => ['type' => 'selector', 'value' => '#old-price'],
|
|
],
|
|
'settings' => ['scraper_service' => 'http'],
|
|
]);
|
|
$this->pricedUrl($store, 'https://shop.test/other');
|
|
$url = Url::factory()->for($store)->create(['url' => 'https://shop.test/widget']);
|
|
|
|
$result = AiConfigHealer::new()->heal($url, ['price' => null, 'body' => $this->html(), 'availability' => null]);
|
|
|
|
$this->assertSame('$12.99', $result['price']);
|
|
$this->assertSame('#pr', data_get($store->fresh()->scrape_strategy, 'price.value'));
|
|
}
|
|
|
|
public function test_ignores_unavailable_products_when_checking_the_config(): void
|
|
{
|
|
$this->configureProviders();
|
|
// The config works on the other product, but that product is out of stock,
|
|
// so the check must not use it and the agent must run.
|
|
$this->mockAgent([
|
|
'is_product' => true,
|
|
'fields' => [
|
|
'title' => ['type' => 'selector', 'value' => '.t'],
|
|
'price' => ['type' => 'selector', 'value' => '#pr'],
|
|
],
|
|
]);
|
|
$store = Store::factory()->create(['scrape_strategy' => $this->workingStrategy(), 'settings' => ['scraper_service' => 'http']]);
|
|
$other = $this->pricedUrl($store, 'https://shop.test/other');
|
|
$other->update(['availability' => StockStatus::OutOfStock]);
|
|
$url = Url::factory()->for($store)->create(['url' => 'https://shop.test/widget']);
|
|
|
|
AiConfigHealer::new()->heal($url, ['price' => null, 'body' => $this->html(), 'availability' => null]);
|
|
|
|
$this->assertDatabaseHas('log_messages', [
|
|
'message' => 'AI self-healing started; attempting to repair store scraper config.',
|
|
]);
|
|
}
|
|
|
|
public function test_store_heal_for_a_new_url_skips_when_config_works_on_another_product(): void
|
|
{
|
|
$this->configureProviders();
|
|
$this->mockAgent([], 'never');
|
|
$strategy = $this->workingStrategy();
|
|
$store = Store::factory()->create(['scrape_strategy' => $strategy, 'settings' => ['scraper_service' => 'http']]);
|
|
$this->pricedUrl($store, 'https://shop.test/other');
|
|
|
|
$healed = AiConfigHealer::new()->healStoreForUrl('https://shop.test/removed', $store, '<html></html>');
|
|
|
|
$this->assertNull($healed);
|
|
$this->assertSame($strategy, $store->fresh()->scrape_strategy->toArray());
|
|
$this->assertNull($store->fresh()->getAiHealFailedAt());
|
|
}
|
|
|
|
/**
|
|
* @return array<string, array<string, string>>
|
|
*/
|
|
private function workingStrategy(): array
|
|
{
|
|
return [
|
|
'title' => ['type' => 'selector', 'value' => '.t'],
|
|
'price' => ['type' => 'selector', 'value' => '#pr'],
|
|
];
|
|
}
|
|
|
|
private function pricedUrl(Store $store, string $address): Url
|
|
{
|
|
// ScrapeUrl calls from(), which resolves a new scraper with an empty body.
|
|
// This fake keeps the page body so the check on the other product can read it.
|
|
$fake = new class extends WebScraperFake
|
|
{
|
|
public function from(string $url): self
|
|
{
|
|
$this->setUrl($url);
|
|
|
|
return $this;
|
|
}
|
|
};
|
|
WebScraper::swap(Mockery::mock()->shouldReceive('make')->andReturn($fake->setBody($this->html()))->getMock());
|
|
|
|
$url = Url::factory()->for($store)->create(['url' => $address]);
|
|
Price::factory()->create(['url_id' => $url->getKey(), 'store_id' => $store->getKey(), 'price' => 12.99]);
|
|
|
|
return $url;
|
|
}
|
|
|
|
public function test_marks_failure_and_keeps_config_when_required_fields_do_not_validate(): void
|
|
{
|
|
$this->configureProviders();
|
|
$this->mockAgent([
|
|
'is_product' => true,
|
|
'fields' => [
|
|
'title' => ['type' => 'selector', 'value' => '.does-not-exist'],
|
|
'price' => ['type' => 'selector', 'value' => '#nope'],
|
|
],
|
|
]);
|
|
$url = $this->url();
|
|
|
|
$result = AiConfigHealer::new()->heal($url, ['price' => null, 'body' => $this->html(), 'availability' => null]);
|
|
|
|
$this->assertNull($result['price']);
|
|
$this->assertSame([], $url->store->fresh()->scrape_strategy->toArray());
|
|
$this->assertNotNull($url->store->fresh()->getAiHealFailedAt());
|
|
}
|
|
|
|
public function test_skips_when_store_lock_is_held(): void
|
|
{
|
|
$this->configureProviders();
|
|
$this->mockAgent([], 'never');
|
|
$url = $this->url();
|
|
Cache::lock('ai-heal:store:'.$url->store->getKey(), 120)->get();
|
|
|
|
$result = AiConfigHealer::new()->heal($url, ['price' => null, 'body' => $this->html(), 'availability' => null]);
|
|
|
|
$this->assertNull($result['price']);
|
|
}
|
|
|
|
public function test_skips_and_marks_failure_when_agent_says_not_a_product(): void
|
|
{
|
|
$this->configureProviders();
|
|
$this->mockAgent([
|
|
'is_product' => false,
|
|
'fields' => [
|
|
'title' => ['type' => 'selector', 'value' => '.t'],
|
|
'price' => ['type' => 'selector', 'value' => '#pr'],
|
|
],
|
|
]);
|
|
$url = $this->url();
|
|
|
|
$result = AiConfigHealer::new()->heal($url, ['price' => null, 'body' => $this->html(), 'availability' => null]);
|
|
|
|
$this->assertNull($result['price']);
|
|
$this->assertSame([], $url->store->fresh()->scrape_strategy->toArray());
|
|
$this->assertNotNull($url->store->fresh()->getAiHealFailedAt());
|
|
}
|
|
|
|
public function test_marks_failure_when_agent_returns_null(): void
|
|
{
|
|
$this->configureProviders();
|
|
$this->mockAgent(null);
|
|
$url = $this->url();
|
|
|
|
$result = AiConfigHealer::new()->heal($url, ['price' => null, 'body' => $this->html(), 'availability' => null]);
|
|
|
|
$this->assertNull($result['price']);
|
|
$this->assertSame([], $url->store->fresh()->scrape_strategy->toArray());
|
|
$this->assertNotNull($url->store->fresh()->getAiHealFailedAt());
|
|
}
|
|
|
|
public function test_apply_preview_to_store_merges_fields_in_memory_without_saving(): void
|
|
{
|
|
$store = Store::factory()->create([
|
|
'scrape_strategy' => ['image' => ['type' => 'selector', 'value' => 'img|src']],
|
|
'settings' => ['scraper_service' => 'http'],
|
|
]);
|
|
|
|
AiConfigHealer::new()->applyPreviewToStore($store, [
|
|
'fields' => ['price' => ['type' => 'selector', 'value' => '#pr']],
|
|
'usedBrowser' => true,
|
|
]);
|
|
|
|
// In memory: merged with the existing field, switched to the browser scraper.
|
|
$this->assertSame('#pr', data_get($store->scrape_strategy, 'price.value'));
|
|
$this->assertSame('img|src', data_get($store->scrape_strategy, 'image.value'));
|
|
$this->assertSame('api', data_get($store->settings, 'scraper_service'));
|
|
|
|
// Not persisted: the DB row is unchanged.
|
|
$fresh = $store->fresh();
|
|
$this->assertSame(['image' => ['type' => 'selector', 'value' => 'img|src']], $fresh->scrape_strategy->toArray());
|
|
$this->assertSame('http', data_get($fresh->settings, 'scraper_service'));
|
|
}
|
|
}
|