Урок 10

Тестирование AI

AI SDK предоставляет fake-агенты для тестов без реальных API-вызовов.

Fake агента

use App\Ai\Agents\SupportAgent;

SupportAgent::fake([
    'Ответ на первый запрос',
    'Ответ на второй запрос',
]);

$response = (new SupportAgent)->prompt('Привет');

$this->assertSame('Ответ на первый запрос', (string) $response);

Fake structured output

SalesCoach::fake([
    ['feedback' => 'Хорошо', 'score' => 8],
]);

$response = (new SalesCoach)->prompt('Анализ...');

$this->assertSame(8, $response['score']);

SDK генерирует совместимые fake-данные.

Fake tools

LookupOrder::fake([
    'Заказ #1: доставлен',
]);

Assertions

SupportAgent::assertPrompted('Привет');
SupportAgent::assertNotPrompted('Пока');

Fake embeddings

use Laravel\Ai\Facades\Embeddings;

Embeddings::fake();

$embedding = Embeddings::for('текст')->generate();

$this->assertCount(1536, $embedding);

Fake images

use Laravel\Ai\Facades\Image;

Image::fake();

$image = Image::of('кот')->generate();

$this->assertTrue($image->exists());

Почему это важно

  • Тесты не тратят деньги
  • Быстрые
  • Не зависят от сети
  • Покрытие AI-логики

Итоги

  • Agent::fake() для ответов
  • Fake structured output
  • Fake tools, embeddings, images
  • Assertions для проверки
  • Тесты без реальных вызовов