Files
pricebuddy/tests/Feature/Services/SchemaOrgAvailabilityTest.php
Jeremy e56eac068c feat: schema.org availability inference and deterministic-first self-healing
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
2026-06-09 15:07:40 +10:00

72 lines
2.3 KiB
PHP

<?php
namespace Tests\Feature\Services;
use App\Enums\StockStatus;
use App\Models\Store;
use App\Services\ScrapeUrl;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class SchemaOrgAvailabilityTest extends TestCase
{
use RefreshDatabase;
private function jsonLdPage(string $availabilityLabel): string
{
$json = json_encode([
'@context' => 'https://schema.org/',
'@type' => 'Product',
'name' => 'Widget',
'offers' => [
'@type' => 'Offer',
'priceCurrency' => 'USD',
'price' => '19.99',
'availability' => 'https://schema.org/'.$availabilityLabel,
],
]);
return "<html><head><script type=\"application/ld+json\">{$json}</script></head><body></body></html>";
}
private function schemaOrgStore(): Store
{
return Store::factory()->create([
'domains' => [['domain' => 'example.com']],
'scrape_strategy' => [
'title' => ['type' => 'schema_org', 'value' => null],
'price' => ['type' => 'schema_org', 'value' => null],
'availability' => ['type' => 'schema_org', 'value' => null],
],
'settings' => ['scraper_service' => 'http'],
]);
}
public function test_schema_org_out_of_stock_resolves_without_match_config(): void
{
$store = $this->schemaOrgStore();
Http::fake(['*' => Http::response($this->jsonLdPage('OutOfStock'))]);
$scrape = ScrapeUrl::new('https://example.com/p')->scrape();
$this->assertSame(StockStatus::OutOfStock, StockStatus::resolveAvailability(
data_get($scrape, 'availability'),
data_get($store, 'scrape_strategy.availability'),
));
}
public function test_schema_org_in_stock_resolves_in_stock_without_match_config(): void
{
$store = $this->schemaOrgStore();
Http::fake(['*' => Http::response($this->jsonLdPage('InStock'))]);
$scrape = ScrapeUrl::new('https://example.com/p')->scrape();
$this->assertSame(StockStatus::InStock, StockStatus::resolveAvailability(
data_get($scrape, 'availability'),
data_get($store, 'scrape_strategy.availability'),
));
}
}