Files
portal/app/tests/Unit/External/ExaBalanceProviderTest.php
T
Дмитрий 90da8c1bb2 fix(external): живость = сервер ответил (<500), не только 2xx
На проде self_render (GET к POST-ручке /render) и exa (GET api.exa.ai root) отдавали 404
и показывались красными/«не удалось» — ложь, сервис ЖИВ (достучались, просто не тот метод).
Теперь liveness: status<500 → жив (404/405 = сервер ответил); 5xx/обрыв → мёртв.
Правит HttpFundedServiceProvider (aitunnel/exa/xfetch) + SelfRender/SalesFinder пробы.
Тесты: 20/20 (вкл. новые 404→жив, 5xx→мёртв), Larastan 0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-16 15:29:07 +03:00

55 lines
2.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
use App\Services\External\ExaBalanceProvider;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
uses(TestCase::class);
beforeEach(fn () => config()->set('services.exa.key', 'test-key'));
it('не настроено, когда ключа нет', function () {
config()->set('services.exa.key', '');
expect((new ExaBalanceProvider)->fetch()->ok)->toBeFalse();
});
it('жив без суммы, когда пинг 200', function () {
config()->set('services.exa.balance_url', '');
config()->set('services.exa.base_url', 'https://api.exa.ai');
Http::fake(['api.exa.ai*' => Http::response([], 200)]);
$r = (new ExaBalanceProvider)->fetch();
expect($r->ok)->toBeTrue();
expect($r->balance)->toBeNull();
});
it('деньги при balance_url с числом', function () {
config()->set('services.exa.balance_url', 'https://api.exa.ai/billing/balance');
Http::fake(['api.exa.ai/billing/balance' => Http::response(['balance' => 40.0], 200)]);
expect((new ExaBalanceProvider)->fetch()->balance)->toBe(40.0);
});
it('жив при 404 на пинге (EXA достучались через тоннель — не 2xx, но сервис отвечает)', function () {
config()->set('services.exa.balance_url', '');
config()->set('services.exa.base_url', 'https://api.exa.ai');
Http::fake(['api.exa.ai*' => Http::response('Not Found', 404)]);
$r = (new ExaBalanceProvider)->fetch();
expect($r->ok)->toBeTrue();
expect($r->balance)->toBeNull();
});
it('мёртв при 5xx на пинге', function () {
config()->set('services.exa.balance_url', '');
config()->set('services.exa.base_url', 'https://api.exa.ai');
Http::fake(['api.exa.ai*' => Http::response('err', 503)]);
expect((new ExaBalanceProvider)->fetch()->ok)->toBeFalse();
});
it('с настроенным proxy поток не ломается (EXA за Cloudflare ходит через тоннель)', function () {
config()->set('services.exa.balance_url', '');
config()->set('services.exa.base_url', 'https://api.exa.ai');
config()->set('services.exa.proxy', 'socks5h://127.0.0.1:10808');
Http::fake(['api.exa.ai*' => Http::response([], 200)]);
expect((new ExaBalanceProvider)->fetch()->ok)->toBeTrue();
});