Files
pricebuddy/tests/Unit/Enums/StockStatusSchemaOrgTest.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

63 lines
3.1 KiB
PHP

<?php
namespace Tests\Unit\Enums;
use App\Enums\StockStatus;
use PHPUnit\Framework\TestCase;
class StockStatusSchemaOrgTest extends TestCase
{
public function test_maps_item_availability_urls_to_stock_status(): void
{
$map = [
'https://schema.org/InStock' => StockStatus::InStock,
'https://schema.org/OnlineOnly' => StockStatus::InStock,
'https://schema.org/InStoreOnly' => StockStatus::InStock,
'https://schema.org/LimitedAvailability' => StockStatus::InStock,
'https://schema.org/OutOfStock' => StockStatus::OutOfStock,
'https://schema.org/SoldOut' => StockStatus::OutOfStock,
'https://schema.org/Reserved' => StockStatus::OutOfStock,
'https://schema.org/PreOrder' => StockStatus::PreOrder,
'https://schema.org/PreSale' => StockStatus::PreOrder,
'https://schema.org/BackOrder' => StockStatus::BackOrder,
'https://schema.org/MadeToOrder' => StockStatus::SpecialOrder,
'https://schema.org/Discontinued' => StockStatus::Discontinued,
];
foreach ($map as $url => $expected) {
$this->assertSame($expected, StockStatus::fromSchemaOrgAvailability($url), $url);
$this->assertSame($expected, StockStatus::fromSchemaOrgAvailability(substr($url, strlen('https://schema.org/'))));
}
}
public function test_empty_availability_is_in_stock_and_unknown_is_out_of_stock(): void
{
$this->assertSame(StockStatus::InStock, StockStatus::fromSchemaOrgAvailability(null));
$this->assertSame(StockStatus::InStock, StockStatus::fromSchemaOrgAvailability(''));
$this->assertSame(StockStatus::OutOfStock, StockStatus::fromSchemaOrgAvailability('https://schema.org/Nonsense'));
}
public function test_resolve_availability_uses_schema_org_mapping_and_ignores_match_config(): void
{
$strategy = ['type' => 'schema_org', 'value' => null];
$this->assertSame(StockStatus::InStock, StockStatus::resolveAvailability('https://schema.org/InStock', $strategy));
$this->assertSame(StockStatus::OutOfStock, StockStatus::resolveAvailability('https://schema.org/OutOfStock', $strategy));
$strategyWithMatch = ['type' => 'schema_org', 'match' => ['out_of_stock' => ['type' => 'match', 'value' => 'InStock']]];
$this->assertSame(StockStatus::InStock, StockStatus::resolveAvailability('https://schema.org/InStock', $strategyWithMatch));
}
public function test_resolve_availability_delegates_to_match_for_non_schema_org(): void
{
$strategy = ['type' => 'selector', 'match' => ['out_of_stock' => ['type' => 'match', 'value' => 'Sold out']]];
$this->assertSame(StockStatus::OutOfStock, StockStatus::resolveAvailability('Sold out', $strategy));
$this->assertSame(StockStatus::InStock, StockStatus::resolveAvailability(null, $strategy));
$this->assertSame(
StockStatus::matchFromScrapedValue('Sold out', $strategy['match']),
StockStatus::resolveAvailability('Sold out', $strategy),
);
}
}