2026-07-02 21:07:41 +03:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
2026-07-13 07:04:38 +03:00
|
|
|
|
use App\Jobs\Bot\ProcessChatMessageJob;
|
2026-07-02 21:07:41 +03:00
|
|
|
|
use App\Models\BotDialog;
|
|
|
|
|
|
use App\Models\KnowledgeChunk;
|
|
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
|
|
|
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
|
|
config()->set('services.yandexgpt', [
|
|
|
|
|
|
'api_key' => 'k', 'folder_id' => 'f', 'model' => 'yandexgpt-lite/latest',
|
|
|
|
|
|
'endpoint' => 'https://llm.api.cloud.yandex.net/foundationModels/v1/completion',
|
|
|
|
|
|
'timeout_seconds' => 8,
|
|
|
|
|
|
]);
|
|
|
|
|
|
KnowledgeChunk::create([
|
|
|
|
|
|
'source_path' => 'help/p.md', 'title' => 'Что такое проект', 'tour' => null,
|
|
|
|
|
|
'topics' => 'создать проект', 'chunk_index' => 0,
|
|
|
|
|
|
'content' => 'Проект — это заявка на поток клиентов.',
|
|
|
|
|
|
]);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-07-13 07:04:38 +03:00
|
|
|
|
/**
|
|
|
|
|
|
* Мимикрирует то, что делает ChatController: пишет реплику клиента в журнал и
|
|
|
|
|
|
* запускает джобу с её номером, чтобы история не включала текущий вопрос дважды.
|
|
|
|
|
|
*/
|
|
|
|
|
|
function chatMessage(string $chatId, string $text): void
|
|
|
|
|
|
{
|
|
|
|
|
|
$row = BotDialog::create([
|
|
|
|
|
|
'chat_id' => $chatId,
|
|
|
|
|
|
'direction' => 'in',
|
|
|
|
|
|
'message' => $text,
|
|
|
|
|
|
'created_at' => now(),
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
(new ProcessChatMessageJob($chatId, (int) $row->id, $text))->handle();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
it('happy path: ответ пишется в журнал (in+out) с latency', function () {
|
2026-07-02 21:07:41 +03:00
|
|
|
|
Http::fake([
|
|
|
|
|
|
'llm.api.cloud.yandex.net/*' => Http::response([
|
|
|
|
|
|
'result' => ['alternatives' => [['message' => ['role' => 'assistant', 'text' => 'Проект — это…']]]],
|
|
|
|
|
|
]),
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
2026-07-13 07:04:38 +03:00
|
|
|
|
chatMessage('chat-1', 'что такое проект?');
|
2026-07-02 21:07:41 +03:00
|
|
|
|
|
|
|
|
|
|
$out = BotDialog::where('direction', 'out')->firstOrFail();
|
|
|
|
|
|
expect(BotDialog::where('direction', 'in')->count())->toBe(1)
|
|
|
|
|
|
->and($out->latency_ms)->toBeGreaterThanOrEqual(0)
|
|
|
|
|
|
->and($out->escalated)->toBeFalse()
|
|
|
|
|
|
->and($out->matched_chunks)->not->toBeNull();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-07-13 11:48:39 +03:00
|
|
|
|
it('личный вопрос без карточки фактов: приглашение в кабинет в чате, журнал escalated=false', function () {
|
|
|
|
|
|
// Правило изменено 13.07.2026: ProcessChatMessageJob карточку фактов ClientFacts пока
|
|
|
|
|
|
// не передаёт (отдельная задача) — с точки зрения BotAnswerService это гость, и на
|
|
|
|
|
|
// личный вопрос он получает приглашение войти в кабинет, а не эскалацию к специалисту
|
|
|
|
|
|
// (см. BotContactCaptureTest, PersonalAnswersTest).
|
2026-07-13 07:04:38 +03:00
|
|
|
|
chatMessage('chat-2', 'какой у меня баланс?');
|
2026-07-02 21:07:41 +03:00
|
|
|
|
|
2026-07-13 07:04:38 +03:00
|
|
|
|
$out = BotDialog::where('direction', 'out')->firstOrFail();
|
2026-07-13 11:48:39 +03:00
|
|
|
|
expect($out->message)->toContain('личном кабинете')
|
|
|
|
|
|
->and($out->escalated)->toBeFalse();
|
2026-07-02 21:07:41 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-07-12 10:15:09 +03:00
|
|
|
|
it('второе сообщение чата уходит в LLM вместе с историей первого', function () {
|
|
|
|
|
|
// Живой урок 12.07.2026: «а сам руками я могу?» без памяти разговора — бессмыслица.
|
|
|
|
|
|
Http::fake([
|
|
|
|
|
|
'llm.api.cloud.yandex.net/*' => Http::response([
|
|
|
|
|
|
'result' => ['alternatives' => [['message' => ['role' => 'assistant', 'text' => 'Конечно.']]]],
|
|
|
|
|
|
]),
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
2026-07-13 07:04:38 +03:00
|
|
|
|
chatMessage('chat-h', 'что такое проект?');
|
|
|
|
|
|
chatMessage('chat-h', 'а расскажи про проект подробнее');
|
2026-07-12 10:15:09 +03:00
|
|
|
|
|
|
|
|
|
|
$llmCalls = collect(Http::recorded())
|
|
|
|
|
|
->filter(fn ($pair) => str_contains($pair[0]->url(), 'llm.api.cloud.yandex.net'));
|
|
|
|
|
|
$lastMessages = collect($llmCalls->last()[0]['messages']);
|
|
|
|
|
|
|
|
|
|
|
|
expect($llmCalls)->toHaveCount(2)
|
|
|
|
|
|
->and($lastMessages->pluck('text'))->toContain('что такое проект?')
|
|
|
|
|
|
->and($lastMessages->pluck('role'))->toContain('assistant');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('история — только своего чата, чужие диалоги в LLM не утекают', function () {
|
|
|
|
|
|
Http::fake([
|
|
|
|
|
|
'llm.api.cloud.yandex.net/*' => Http::response([
|
|
|
|
|
|
'result' => ['alternatives' => [['message' => ['role' => 'assistant', 'text' => 'Ок.']]]],
|
|
|
|
|
|
]),
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
2026-07-13 07:04:38 +03:00
|
|
|
|
chatMessage('chat-alien', 'что такое проект?');
|
|
|
|
|
|
chatMessage('chat-mine', 'расскажи про проект');
|
2026-07-12 10:15:09 +03:00
|
|
|
|
|
|
|
|
|
|
$last = collect(collect(Http::recorded())
|
|
|
|
|
|
->filter(fn ($pair) => str_contains($pair[0]->url(), 'llm.api.cloud.yandex.net'))
|
|
|
|
|
|
->last()[0]['messages']);
|
|
|
|
|
|
|
|
|
|
|
|
expect($last->pluck('text'))->not->toContain('что такое проект?');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-07-02 21:07:41 +03:00
|
|
|
|
it('джоба объявлена с queue=bot и timeout ≤ 12 сек', function () {
|
2026-07-13 07:04:38 +03:00
|
|
|
|
$job = new ProcessChatMessageJob('c', 1, 'q');
|
2026-07-02 21:07:41 +03:00
|
|
|
|
|
|
|
|
|
|
expect($job->queue)->toBe('bot')
|
|
|
|
|
|
->and($job->timeout)->toBeLessThanOrEqual(12);
|
|
|
|
|
|
});
|