mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
Infer availability from schema.org StockStatus via a type-aware resolveAvailability, hiding the match-values UI when the type is schema.org and labelling inferred results in the test modal. - AutoCreateStore::detect uses schema.org availability - Try deterministic heuristics before escalating to the AI agent, with browser escalation when needed - Match schema.org Product @type case-insensitively (and array types) - Suppress intermediate scrape error toasts during create/self-heal
285 lines
9.6 KiB
PHP
Executable File
285 lines
9.6 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Tests\Unit\Services;
|
|
|
|
use App\Enums\ScraperService;
|
|
use App\Models\Store;
|
|
use App\Services\AutoCreateStore;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Tests\TestCase;
|
|
|
|
class AutoCreateStoreTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected string $testUrl = 'http://example.com?product=1';
|
|
|
|
protected string $html = '';
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
Store::all()->each(fn ($store) => $store->delete());
|
|
}
|
|
|
|
public function test_get_store_attributes()
|
|
{
|
|
$this->fakeResponse('basic-meta');
|
|
$autoCreateStore = new AutoCreateStore($this->testUrl, $this->html);
|
|
$attributes = $autoCreateStore->getStoreAttributes();
|
|
|
|
$this->assertSame('Example.com', data_get($attributes, 'name'));
|
|
$this->assertSame(ScraperService::Http->value, data_get($attributes, 'settings.scraper_service'));
|
|
|
|
$this->assertCount(2, $attributes['domains']);
|
|
$this->assertSame('example.com', data_get($attributes, 'domains.0.domain'));
|
|
|
|
$this->assertCount(3, $attributes['scrape_strategy']);
|
|
$this->assertSame('meta[property="og:title"]|content', data_get($attributes, 'scrape_strategy.title.value'));
|
|
$this->assertArrayHasKey('price', $attributes['scrape_strategy']);
|
|
$this->assertArrayHasKey('image', $attributes['scrape_strategy']);
|
|
}
|
|
|
|
public function test_rule_parse_basic_meta()
|
|
{
|
|
$this->fakeResponse('basic-meta');
|
|
$autoCreateStore = new AutoCreateStore($this->testUrl, $this->html);
|
|
|
|
$this->assertEquals([
|
|
'title' => [
|
|
'type' => 'selector',
|
|
'value' => 'meta[property="og:title"]|content',
|
|
'data' => 'My product',
|
|
],
|
|
'price' => [
|
|
'type' => 'selector',
|
|
'value' => 'meta[property="product:price:amount"]|content',
|
|
'data' => '35.00',
|
|
],
|
|
'image' => [
|
|
'type' => 'selector',
|
|
'value' => 'meta[property="og:image"]|content',
|
|
'data' => 'http://localhost/my-image.jpg',
|
|
],
|
|
], $autoCreateStore->strategyParse());
|
|
}
|
|
|
|
public function test_rule_parse_basic_meta_secure_image()
|
|
{
|
|
$this->fakeResponse('basic-meta-secure-image');
|
|
$autoCreateStore = new AutoCreateStore($this->testUrl, $this->html);
|
|
|
|
$this->assertEquals([
|
|
'title' => [
|
|
'type' => 'selector',
|
|
'value' => 'meta[property="og:title"]|content',
|
|
'data' => 'My product',
|
|
],
|
|
'price' => [
|
|
'type' => 'selector',
|
|
'value' => 'meta[property="product:price:amount"]|content',
|
|
'data' => '35.00',
|
|
],
|
|
'image' => [
|
|
'type' => 'selector',
|
|
'value' => 'meta[property="og:image:secure_url"]|content',
|
|
'data' => 'http://localhost/my-image.jpg',
|
|
],
|
|
], $autoCreateStore->strategyParse());
|
|
}
|
|
|
|
public function test_rule_parse_unstructured_selector_1()
|
|
{
|
|
$this->fakeResponse('unstructured-selector-1');
|
|
$autoCreateStore = new AutoCreateStore($this->testUrl, $this->html);
|
|
|
|
$this->assertEquals([
|
|
'title' => [
|
|
'type' => 'selector',
|
|
'value' => 'h1',
|
|
'data' => 'My product',
|
|
],
|
|
'price' => [
|
|
'type' => 'selector',
|
|
'value' => '[class^="price"]',
|
|
'data' => '35.00',
|
|
],
|
|
'image' => [
|
|
'type' => 'regex',
|
|
'value' => '~\"hiRes\":\"(.+?)\"~',
|
|
'data' => 'http://localhost/my-image.jpg',
|
|
],
|
|
], $autoCreateStore->strategyParse());
|
|
}
|
|
|
|
public function test_rule_parse_unstructured_regex_1()
|
|
{
|
|
$this->fakeResponse('unstructured-regex-1');
|
|
$autoCreateStore = new AutoCreateStore($this->testUrl, $this->html);
|
|
|
|
$this->assertEquals([
|
|
'title' => [
|
|
'type' => 'selector',
|
|
'value' => 'h1',
|
|
'data' => 'My product',
|
|
],
|
|
'price' => [
|
|
'type' => 'regex',
|
|
'value' => '~>\$(\d+(\.\d{2})?)<~',
|
|
'data' => '35.00',
|
|
],
|
|
'image' => [
|
|
'type' => 'selector',
|
|
'value' => 'meta[property="og:image"]|content',
|
|
'data' => 'http://localhost/my-image.jpg',
|
|
],
|
|
], $autoCreateStore->strategyParse());
|
|
}
|
|
|
|
public function test_rule_parse_unstructured_regex_2()
|
|
{
|
|
$this->fakeResponse('unstructured-regex-2');
|
|
$autoCreateStore = new AutoCreateStore($this->testUrl, $this->html);
|
|
|
|
$this->assertEquals([
|
|
'title' => [
|
|
'type' => 'selector',
|
|
'value' => 'h1',
|
|
'data' => 'My product',
|
|
],
|
|
'price' => [
|
|
'type' => 'regex',
|
|
'value' => '~\$(\d+(\.\d{2})?)~',
|
|
'data' => '35.00',
|
|
],
|
|
'image' => [
|
|
'type' => 'selector',
|
|
'value' => 'meta[property="og:image"]|content',
|
|
'data' => 'http://localhost/my-image.jpg',
|
|
],
|
|
], $autoCreateStore->strategyParse());
|
|
}
|
|
|
|
public function test_rule_parse_unstructured_regex_3()
|
|
{
|
|
$this->fakeResponse('unstructured-regex-3');
|
|
$autoCreateStore = new AutoCreateStore($this->testUrl, $this->html);
|
|
|
|
$this->assertEquals([
|
|
'title' => [
|
|
'type' => 'selector',
|
|
'value' => 'h1',
|
|
'data' => 'My product',
|
|
],
|
|
'price' => [
|
|
'type' => 'regex',
|
|
'value' => '~\$(\d+(\.\d{2})?)~',
|
|
'data' => '35.00',
|
|
],
|
|
'image' => [
|
|
'type' => 'selector',
|
|
'value' => 'meta[property="og:image"]|content',
|
|
'data' => 'http://localhost/my-image.jpg',
|
|
],
|
|
], $autoCreateStore->strategyParse());
|
|
}
|
|
|
|
public function test_rule_parse_unstructured_store_amazon()
|
|
{
|
|
$this->fakeResponse('unstructured-store-amazon');
|
|
$autoCreateStore = new AutoCreateStore($this->testUrl, $this->html);
|
|
|
|
$this->assertEquals([
|
|
'title' => [
|
|
'type' => 'selector',
|
|
'value' => 'title',
|
|
'data' => 'My amazon product',
|
|
],
|
|
'price' => [
|
|
'type' => 'selector',
|
|
'value' => '.a-price .a-offscreen',
|
|
'data' => '35.00',
|
|
],
|
|
'image' => [
|
|
'type' => 'regex',
|
|
'value' => '~\"hiRes\":\"(.+?)\"~',
|
|
'data' => 'http://localhost/my-image.jpg',
|
|
],
|
|
], $autoCreateStore->strategyParse());
|
|
}
|
|
|
|
public function test_rule_parse_schema_org()
|
|
{
|
|
$this->fakeResponse('schema-org');
|
|
$autoCreateStore = new AutoCreateStore($this->testUrl, $this->html);
|
|
|
|
$this->assertEquals([
|
|
'title' => [
|
|
'type' => 'schema_org',
|
|
'value' => null,
|
|
'data' => 'Schema Product',
|
|
],
|
|
'price' => [
|
|
'type' => 'schema_org',
|
|
'value' => null,
|
|
'data' => '45.00',
|
|
],
|
|
'image' => [
|
|
'type' => 'schema_org',
|
|
'value' => null,
|
|
'data' => 'https://example.com/schema-image.jpg',
|
|
],
|
|
], $autoCreateStore->strategyParse());
|
|
}
|
|
|
|
public function test_detect_returns_schema_org_strategy_including_availability(): void
|
|
{
|
|
$json = json_encode([
|
|
'@context' => 'https://schema.org/',
|
|
'@type' => 'Product',
|
|
'name' => 'Widget',
|
|
'image' => 'https://x.test/w.png',
|
|
'offers' => [
|
|
'@type' => 'Offer',
|
|
'priceCurrency' => 'USD',
|
|
'price' => 48.95,
|
|
'availability' => 'https://schema.org/InStock',
|
|
],
|
|
]);
|
|
$html = "<html><head><script type=\"application/ld+json\">{$json}</script></head><body></body></html>";
|
|
|
|
$detected = (new AutoCreateStore('https://shop.test/p', $html))->detect();
|
|
|
|
$this->assertSame('schema_org', data_get($detected, 'fields.title.type'));
|
|
$this->assertSame('schema_org', data_get($detected, 'fields.price.type'));
|
|
$this->assertSame('schema_org', data_get($detected, 'fields.image.type'));
|
|
$this->assertSame('schema_org', data_get($detected, 'fields.availability.type'));
|
|
$this->assertSame('Widget', data_get($detected, 'extracted.title'));
|
|
$this->assertNotEmpty(data_get($detected, 'extracted.price'));
|
|
$this->assertSame('https://schema.org/InStock', data_get($detected, 'extracted.availability'));
|
|
}
|
|
|
|
public function test_detect_returns_null_when_title_or_price_missing(): void
|
|
{
|
|
$detected = (new AutoCreateStore('https://shop.test/p', '<html><body><div>nothing</div></body></html>'))->detect();
|
|
|
|
$this->assertNull($detected);
|
|
}
|
|
|
|
protected function getHtml(string $name): string
|
|
{
|
|
return file_get_contents(__DIR__.'/../../Fixtures/AutoCreateStore/'.$name.'.html');
|
|
}
|
|
|
|
protected function fakeResponse(string $name, string $domain = 'example.com*'): void
|
|
{
|
|
$this->html = $this->getHtml($name);
|
|
|
|
Http::fake([
|
|
$domain => Http::response($this->html),
|
|
]);
|
|
}
|
|
}
|