55 lines
2.5 KiB
PHP
55 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Services\External\ProxyMarketProbe;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Tests\TestCase;
|
|
|
|
uses(TestCase::class);
|
|
|
|
beforeEach(function () {
|
|
config()->set('services.proxy_market.check_url', 'https://vm.local/proxy-check');
|
|
config()->set('services.proxy_market.amber_days', 7);
|
|
config()->set('services.self_render.key', 'SECRET');
|
|
});
|
|
|
|
it('серый, когда check_url или срок не заданы', function () {
|
|
config()->set('services.proxy_market.check_url', '');
|
|
config()->set('services.proxy_market.expires_at', '2026-08-04');
|
|
expect((new ProxyMarketProbe)->check()->light)->toBe('grey');
|
|
|
|
config()->set('services.proxy_market.check_url', 'https://vm.local/proxy-check');
|
|
config()->set('services.proxy_market.expires_at', '');
|
|
expect((new ProxyMarketProbe)->check()->light)->toBe('grey');
|
|
});
|
|
|
|
it('зелёный: прокси жив и срок далёк (в детали — IP)', function () {
|
|
config()->set('services.proxy_market.expires_at', now()->addDays(30)->format('Y-m-d'));
|
|
Http::fake(['vm.local/proxy-check' => Http::response(['ok' => true, 'ip' => '178.9.9.9'], 200)]);
|
|
$r = (new ProxyMarketProbe)->check();
|
|
expect($r->light)->toBe('green');
|
|
expect($r->detail)->toContain('178.9.9.9');
|
|
});
|
|
|
|
it('амбер: до конца срока меньше порога', function () {
|
|
config()->set('services.proxy_market.expires_at', now()->addDays(3)->format('Y-m-d'));
|
|
Http::fake(['vm.local/proxy-check' => Http::response(['ok' => true, 'ip' => '178.9.9.9'], 200)]);
|
|
expect((new ProxyMarketProbe)->check()->light)->toBe('amber');
|
|
});
|
|
|
|
it('красный: срок вышел, даже если прокси жив', function () {
|
|
config()->set('services.proxy_market.expires_at', now()->subDay()->format('Y-m-d'));
|
|
Http::fake(['vm.local/proxy-check' => Http::response(['ok' => true, 'ip' => '178.9.9.9'], 200)]);
|
|
expect((new ProxyMarketProbe)->check()->light)->toBe('red');
|
|
});
|
|
|
|
it('красный: прокси не отвечает (ok:false и сетевой сбой)', function () {
|
|
config()->set('services.proxy_market.expires_at', now()->addDays(30)->format('Y-m-d'));
|
|
Http::fake(['vm.local/proxy-check' => Http::response(['ok' => false, 'error' => 'timeout'], 200)]);
|
|
expect((new ProxyMarketProbe)->check()->light)->toBe('red');
|
|
|
|
Http::fake(fn () => throw new RuntimeException('conn refused'));
|
|
expect((new ProxyMarketProbe)->check()->light)->toBe('red');
|
|
});
|