feat(bot): YandexGptClient — completion Lite, таймаут 8с, null при беде
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Services\Bot\YandexGptClient;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
uses(Tests\TestCase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
config()->set('services.yandexgpt', [
|
||||
'api_key' => 'test-key', 'folder_id' => 'b1gtest', 'model' => 'yandexgpt-lite/latest',
|
||||
'endpoint' => 'https://llm.api.cloud.yandex.net/foundationModels/v1/completion',
|
||||
'timeout_seconds' => 8,
|
||||
]);
|
||||
});
|
||||
|
||||
it('шлёт правильный запрос и возвращает текст ответа', function () {
|
||||
Http::fake([
|
||||
'llm.api.cloud.yandex.net/*' => Http::response([
|
||||
'result' => ['alternatives' => [['message' => ['role' => 'assistant', 'text' => 'Проект — это…']]]],
|
||||
]),
|
||||
]);
|
||||
|
||||
$text = app(YandexGptClient::class)->complete('системный наказ', 'что такое проект?');
|
||||
|
||||
expect($text)->toBe('Проект — это…');
|
||||
Http::assertSent(function ($request) {
|
||||
return $request->hasHeader('Authorization', 'Api-Key test-key')
|
||||
&& $request['modelUri'] === 'gpt://b1gtest/yandexgpt-lite/latest'
|
||||
&& $request['messages'][0]['role'] === 'system'
|
||||
&& $request['messages'][1]['role'] === 'user'
|
||||
&& $request['completionOptions']['temperature'] === 0.2;
|
||||
});
|
||||
});
|
||||
|
||||
it('пустой api_key → null (бот не настроен, не исключение)', function () {
|
||||
config()->set('services.yandexgpt.api_key', '');
|
||||
|
||||
expect(app(YandexGptClient::class)->complete('s', 'u'))->toBeNull();
|
||||
});
|
||||
|
||||
it('ошибка API → null (эскалация решается выше)', function () {
|
||||
Http::fake(['llm.api.cloud.yandex.net/*' => Http::response('err', 500)]);
|
||||
|
||||
expect(app(YandexGptClient::class)->complete('s', 'u'))->toBeNull();
|
||||
});
|
||||
Reference in New Issue
Block a user