Files
pricebuddy/tests/Feature/Api/ClientConfigApiTest.php
Jeremy 2f0ae302e1 fix: bound AI healing inside the meta-extraction request
POST /api/meta-extraction is synchronous and user-facing but had no ceiling. The
scrape was bounded at 10s; the healing it can trigger was bounded only by the
provider's own timeout (120s on this instance), so one request could block for
over two minutes. Worse, healing is reached from the auto-create path - a URL
matching no store - which is the first thing a user hits on a new retailer.

Verified against a wedged provider: the same request went from HTTP 200 after
120.1s to HTTP 200 after 0.08s by default, and 24.1s with heal=true.

Healing is now opt-in via a `heal` boolean on the request, defaulting to false.
A caller testing selectors it just typed wants a fast deterministic answer, and
healing may propose a different strategy than the one under test; "work it out
for me" is a separate, explicitly slow action.

Every request also carries a wall-clock budget (config
price_buddy.meta_extraction.budget_seconds, default 25) spanning the scrape, any
browser re-scrape and the AI call, which is handed the time remaining instead of
the provider's timeout. Healing is declined outright below heal_floor_seconds
rather than started and cut off. The agent's own tool fetches draw from the same
budget and throw once it is gone - they run in-process between model calls, so
the model call timeout does not cover them.

Healing never fails the request: every failure mode returns 200 with the
deterministic result and a `healing` object saying what happened
(attempted/applied/reason, reason being disabled|not_needed|timeout|error), so a
client can explain a thin result instead of showing empty fields.

/api/client-config gains limits.meta_extraction_timeout_seconds so clients set
their abort from the instance's real ceiling rather than a hardcoded guess.

The admin UI heal path is unaffected - it calls previewForUrl() without a budget,
which keeps the provider timeout, the right ceiling when a human chose to wait.

BREAKING: API callers that relied on healing running by default must now send
heal: true. The four API tests that exercised healing were updated accordingly.
2026-08-08 12:54:30 +10:00

116 lines
3.6 KiB
PHP

<?php
namespace Tests\Feature\Api;
use App\Enums\ApiAbility;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;
class ClientConfigApiTest extends TestCase
{
use RefreshDatabase;
private User $user;
protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()->create();
}
private function authenticateWith(array $abilities): void
{
Auth::forgetGuards();
$token = $this->user->createToken('test-token', $abilities)->plainTextToken;
$this->withHeaders(['Authorization' => 'Bearer '.$token]);
}
public function test_readable_with_a_minimal_ability_token(): void
{
$this->authenticateWith([ApiAbility::ClientConfigRead->value]);
$this->getJson('/api/client-config')->assertSuccessful();
}
public function test_readable_with_an_all_access_token(): void
{
$this->authenticateWith(['*']);
$this->getJson('/api/client-config')->assertSuccessful();
}
public function test_forbidden_without_the_ability(): void
{
$this->authenticateWith([ApiAbility::UserDetail->value]);
$this->getJson('/api/client-config')->assertForbidden();
}
public function test_unauthenticated_request_is_rejected(): void
{
$this->getJson('/api/client-config')->assertUnauthorized();
}
public function test_response_keys_match_an_exact_expected_set(): void
{
$this->authenticateWith(['*']);
$payload = $this->getJson('/api/client-config')->assertSuccessful()->json();
$this->assertSame(['data'], array_keys($payload));
$this->assertSame(['capabilities', 'limits', 'app_version'], array_keys($payload['data']));
$this->assertSame(
['products_filter_url', 'products_current_url', 'products_sparse_fieldsets', 'stores_filter_domain'],
array_keys($payload['data']['capabilities'])
);
$this->assertSame(['meta_extraction_timeout_seconds'], array_keys($payload['data']['limits']));
}
public function test_meta_extraction_limit_comes_from_config(): void
{
config()->set('price_buddy.meta_extraction.budget_seconds', 40);
$this->authenticateWith(['*']);
$this->getJson('/api/client-config')
->assertSuccessful()
->assertJsonPath('data.limits.meta_extraction_timeout_seconds', 40);
}
public function test_app_version_comes_from_config(): void
{
config()->set('app.version', '9.9.9');
$this->authenticateWith(['*']);
$this->getJson('/api/client-config')
->assertSuccessful()
->assertJsonPath('data.app_version', '9.9.9');
}
public function test_etag_is_stable_and_if_none_match_returns_304(): void
{
$this->authenticateWith(['*']);
$first = $this->getJson('/api/client-config')->assertSuccessful();
$etag = $first->headers->get('ETag');
$this->assertNotEmpty($etag);
$this->assertSame($etag, $this->getJson('/api/client-config')->headers->get('ETag'));
$this->withHeaders(['If-None-Match' => $etag])
->getJson('/api/client-config')
->assertStatus(304);
}
public function test_cache_control_is_private_for_a_day(): void
{
$this->authenticateWith(['*']);
$cacheControl = $this->getJson('/api/client-config')->headers->get('Cache-Control');
$this->assertStringContainsString('private', (string) $cacheControl);
$this->assertStringContainsString('max-age=86400', (string) $cacheControl);
}
}