refactor: убрать дубли, развести слои, разбить тесты

- Support/FileWalker: один обход каталога вместо двух копий
- Support/Bytes: форматирование размера вместо трёх копий
- Support/UrlGuard: SSRF-проверки вынесены из HttpRequestTool
- Support/AtomicWriter: запись вынесена из FileSandbox
- HttpRequest: normalizeUrl/resolveRedirect — одна реализация URL-логики
- AiClient поверх HttpTransport::request: один транспорт, одна TLS-логика
  (было две копии cURL-настроек), подсказка про AI_CAINFO переехала в HttpTransport
- Http переименован в HttpTransport: транспорт/значение/инструмент больше
  не называются одинаково
- Agent: удалён мёртвый withDefaults — ядро больше не зависит от tools
- tests: раннер + support + cases по темам (agent, files, http, shell,
  logging, tools, architecture)
- архитектурные тесты: src не импортирует Tools, tools не создают AiClient
  и не вызывают cURL напрямую
- 100 тестов
This commit is contained in:
rsgrinko committed 2026-11-12 23:13:45 +03:00
1 parent 74bb23c159
commit 76b3be7115
28 files changed
+2022 -1749

No files matched your search

+409
View File
@@ -0,0 +1,409 @@
<?php
declare(strict_types=1);
// Тесты: agent. Запускаются из tests/run.php.
use AiSdk\Agent;
use AiSdk\ChatClient;
use AiSdk\ChatResult;
use AiSdk\HttpTransport;
use AiSdk\HttpRequest;
use AiSdk\LoggingClient;
use AiSdk\Tool;
use AiSdk\ToolRegistry;
use AiSdk\Tools\CalculatorTool;
use AiSdk\Tools\ConvertUnitsTool;
use AiSdk\Tools\CsvQueryTool;
use AiSdk\Tools\DateCalcTool;
use AiSdk\Tools\FileSandbox;
use AiSdk\Tools\FindFilesTool;
use AiSdk\Tools\JsonQueryTool;
use AiSdk\Tools\FinishTool;
use AiSdk\Tools\HashTool;
use AiSdk\Tools\HttpRequestTool;
use AiSdk\Tools\ListDirTool;
use AiSdk\Tools\ReadFileTool;
use AiSdk\Tools\ReplaceInFileTool;
use AiSdk\Tools\SearchFilesTool;
use AiSdk\Tools\ShellTool;
use AiSdk\Tools\TimeTool;
use AiSdk\Tools\WriteFileTool;
use AiSdk\Tools\UuidTool;
// ---------------------------------------------------------------------------
// Агент
// ---------------------------------------------------------------------------
test('агент: вызывает инструмент и отдаёт финальный ответ', function (): void {
$client = new FakeClient([
toolCall('calculator', ['expression' => '2+2'], 'call_42'),
textResult('Получилось 4.'),
]);
$agent = new Agent($client, (new ToolRegistry())->add(new CalculatorTool()), 'ты тестовый', 3);
$answer = $agent->ask('Сколько будет 2+2?');
assertSame('Получилось 4.', $answer);
assertSame(2, count($client->requests), 'должно быть два запроса к модели');
// Второй запрос обязан содержать результат инструмента.
$messages = $client->requests[1]['messages'];
$last = $messages[count($messages) - 1];
assertSame('tool', $last['role']);
assertSame('call_42', $last['tool_call_id']);
assertSame('2+2 = 4', $last['content']);
// И сами схемы инструментов ушли модели.
assertTrue($client->requests[0]['tools'] !== [], 'схемы инструментов не отправлены');
// Трассировка для отладки.
assertSame(1, count($agent->trace()));
assertSame('calculator', $agent->trace()[0]['name']);
});
test('агент: ошибка инструмента уходит модели, а не в exception', function (): void {
$client = new FakeClient([
toolCall('nope', []),
textResult('Не смог посчитать.'),
]);
$agent = new Agent($client, new ToolRegistry(), 'ты тестовый', 3);
$answer = $agent->ask('вопрос');
assertSame('Не смог посчитать.', $answer);
$messages = $client->requests[1]['messages'];
$last = $messages[count($messages) - 1];
assertContains('неизвестный инструмент', $last['content']);
});
test('агент: после лимита раундов просит ответ без инструментов', function (): void {
$client = new FakeClient([
toolCall('calculator', ['expression' => '1+1'], 'c1'),
toolCall('calculator', ['expression' => '1+1'], 'c2'),
toolCall('calculator', ['expression' => '1+1'], 'c3'),
textResult('Два.'),
]);
$agent = new Agent($client, (new ToolRegistry())->add(new CalculatorTool()), 'ты тестовый', 2);
assertSame('Два.', $agent->ask('сколько?'));
// 1 стартовый + 2 раунда + 1 финальный.
assertSame(4, count($client->requests));
$lastRequest = $client->requests[3];
assertSame([], $lastRequest['tools'], 'финальный запрос должен идти без инструментов');
});
test('агент: помнит историю между вопросами', function (): void {
$client = new FakeClient([
textResult('Первый ответ.'),
textResult('Второй ответ.'),
]);
$agent = new Agent($client, new ToolRegistry(), 'ты тестовый', 3);
$agent->ask('первый вопрос');
$agent->ask('второй вопрос');
$messages = $client->requests[1]['messages'];
$roles = array_column($messages, 'role');
assertSame(['system', 'user', 'assistant', 'user'], $roles);
assertSame('Первый ответ.', $messages[2]['content']);
});
test('агент: системный промпт можно сменить на ходу', function (): void {
$client = new FakeClient([textResult('первый'), textResult('второй')]);
$agent = new Agent($client, new ToolRegistry(), 'промпт №1', 3);
$agent->ask('вопрос');
$agent->setSystemPrompt('промпт №2');
$agent->ask('ещё вопрос');
assertSame('промпт №2', $agent->systemPrompt());
// System-сообщение остаётся первым и в истории, и в запросе к модели.
$messages = $client->requests[1]['messages'];
assertSame('system', $messages[0]['role']);
assertSame('промпт №2', $messages[0]['content']);
// История диалога при этом не потерялась.
assertSame(4, count($messages), 'system + 2 user + assistant');
});
test('агент: без явного промпта берётся встроенный', function (): void {
$agent = new Agent(new FakeClient([textResult('ок')]), new ToolRegistry());
assertTrue($agent->systemPrompt() !== '', 'промпт по умолчанию пустой');
});
test('агент: tool_choice не навязывается на повторных раундах', function (): void {
$client = new FakeClient([
toolCall('calculator', ['expression' => '2+2'], 'c1'),
textResult('4'),
]);
$agent = new Agent(
$client,
(new ToolRegistry())->add(new CalculatorTool()),
'тест',
3,
'required',
);
$agent->ask('посчитай');
assertSame('required', $client->requests[0]['options']['tool_choice'] ?? null);
assertTrue(
!isset($client->requests[1]['options']['tool_choice']),
'во втором раунде tool_choice должен вернуться к auto, иначе модель зациклится'
);
});
// ---------------------------------------------------------------------------
// Настройки агента
// ---------------------------------------------------------------------------
test('настройки: tool_choice уходит в клиент', function (): void {
$client = new FakeClient([textResult('ок')]);
$agent = new Agent($client, new ToolRegistry(), 'тест', 3, 'required');
$agent->ask('вопрос');
assertSame('required', $client->requests[0]['options']['tool_choice'] ?? null);
});
test('настройки: tool_choice по умолчанию не навязывается', function (): void {
$client = new FakeClient([textResult('ок')]);
$agent = new Agent($client, new ToolRegistry(), 'тест', 3);
$agent->ask('вопрос');
assertTrue(!isset($client->requests[0]['options']['tool_choice']), 'auto не должен уходить явно');
});
test('настройки: parallel_tool_calls=false отправляется только когда выключено', function (): void {
$off = new FakeClient([textResult('ок')]);
(new Agent($off, new ToolRegistry(), 'тест', 3, 'auto', false))->ask('вопрос');
assertSame(false, $off->requests[0]['options']['parallel_tool_calls']);
$on = new FakeClient([textResult('ок')]);
(new Agent($on, new ToolRegistry(), 'тест', 3, 'auto', true))->ask('вопрос');
assertTrue(!isset($on->requests[0]['options']['parallel_tool_calls']), 'true — это значение по умолчанию');
});
test('настройки: хук может запретить вызов инструмента', function (): void {
$client = new FakeClient([
toolCall('calculator', ['expression' => '2+2'], 'c1'),
textResult('Не смог: действие запрещено.'),
]);
$agent = new Agent(
$client,
(new ToolRegistry())->add(new CalculatorTool()),
'тест',
3,
'auto',
true,
40,
static fn (string $name, array $args): bool => false,
);
$agent->ask('посчитай');
assertTrue($agent->trace()[0]['blocked'], 'вызов должен быть помечен как отклонённый');
assertContains('отклонён оператором', $agent->trace()[0]['output']);
$messages = $client->requests[1]['messages'];
$last = $messages[count($messages) - 1];
assertContains('отклонён оператором', $last['content']);
});
test('настройки: хук пропускает разрешённый вызов', function (): void {
$client = new FakeClient([
toolCall('calculator', ['expression' => '2+2'], 'c1'),
textResult('4'),
]);
$seen = null;
$agent = new Agent(
$client,
(new ToolRegistry())->add(new CalculatorTool()),
'тест',
3,
'auto',
true,
40,
static function (string $name, array $args) use (&$seen): bool {
$seen = [$name, $args];
return true;
},
);
$agent->ask('посчитай');
assertSame('calculator', $seen[0]);
assertSame('2+2', $seen[1]['expression']);
assertSame(false, $agent->trace()[0]['blocked']);
assertSame('2+2 = 4', $agent->trace()[0]['output']);
});
test('настройки: historyLimit обрезает историю целыми ходами', function (): void {
$client = new FakeClient([
textResult('a1'),
textResult('a2'),
textResult('a3'),
textResult('a4'),
]);
$agent = new Agent($client, new ToolRegistry(), 'тест', 3, 'auto', true, 4);
$agent->ask('вопрос 1');
$agent->ask('вопрос 2');
$agent->ask('вопрос 3');
$agent->ask('вопрос 4');
$messages = $agent->messages();
assertSame('system', $messages[0]['role'], 'system должен оставаться первым');
assertTrue(($messages[1]['role'] ?? '') !== 'tool', 'история не должна начинаться с tool');
// Обрезка идёт Перед запросом: лимит 4 + system + финальный ответ.
assertSame(6, count($messages), 'без лимита было бы 9');
$last = $messages[count($messages) - 1];
$question = $messages[count($messages) - 2];
assertSame('user', $question['role']);
assertSame('вопрос 4', $question['content'], 'текущий вопрос должен сохраниться');
assertSame('assistant', $last['role']);
});
test('настройки: historyLimit=0 отключает обрезку', function (): void {
$client = new FakeClient([textResult('a'), textResult('b')]);
$agent = new Agent($client, new ToolRegistry(), 'тест', 3, 'auto', true, 0);
$agent->ask('раз');
$agent->ask('два');
assertSame(5, count($agent->messages()), 'system + 2 вопроса + 2 ответа');
});
test('агент: reset очищает историю и трассировку', function (): void {
$client = new FakeClient([textResult('ответ')]);
$agent = new Agent($client, new ToolRegistry(), 'ты тестовый', 3);
$agent->ask('вопрос');
$agent->reset();
assertSame([], $agent->messages());
assertSame([], $agent->trace());
});
// ---------------------------------------------------------------------------
// Завершение через finish
// ---------------------------------------------------------------------------
test('finish: возвращает result и не делает лишний запрос', function (): void {
$client = new FakeClient([
toolCalls([['name' => 'finish', 'arguments' => ['result' => 'готово'], 'id' => 'f1']]),
]);
$agent = new Agent(
$client,
(new ToolRegistry())->add(new FinishTool()),
'ты тестовый',
3,
);
assertSame('готово', $agent->ask('сделай'));
assertSame(1, count($client->requests), 'после finish второго запроса быть не должно');
assertSame('finish', $agent->trace()[0]['name']);
$last = $agent->messages()[count($agent->messages()) - 1];
assertSame('assistant', $last['role']);
assertSame('готово', $last['content']);
});
test('finish: остальные вызовы из того же ответа получают ответ', function (): void {
$client = new FakeClient([
toolCalls([
['name' => 'calculator', 'arguments' => ['expression' => '2+2'], 'id' => 'c1'],
['name' => 'finish', 'arguments' => ['result' => '4'], 'id' => 'f1'],
]),
]);
$agent = new Agent(
$client,
(new ToolRegistry())->add(new CalculatorTool(), new FinishTool()),
'ты тестовый',
3,
);
assertSame('4', $agent->ask('посчитай'));
// История должна остаться валидной: у каждого tool_call_id есть tool-ответ.
$messages = $agent->messages();
$answered = [];
foreach ($messages as $message) {
if (($message['role'] ?? '') === 'tool') {
$answered[] = $message['tool_call_id'];
}
}
assertSame(['c1', 'f1'], $answered);
assertSame('2+2 = 4', $messages[3]['content'], 'калькулятор должен выполниться до finish');
});
test('finish: без result возвращает понятную заглушку', function (): void {
$client = new FakeClient([
toolCalls([['name' => 'finish', 'arguments' => [], 'id' => 'f1']]),
]);
$agent = new Agent($client, (new ToolRegistry())->add(new FinishTool()), 'тест', 3);
assertContains('без результата', $agent->ask('сделай'));
});
test('finish: незарегистрированный инструмент не перехватывается', function (): void {
$client = new FakeClient([
toolCall('finish', ['result' => 'x'], 'f1'),
textResult('продолжил'),
]);
$agent = new Agent($client, new ToolRegistry(), 'тест', 3);
assertSame('продолжил', $agent->ask('сделай'));
assertContains('неизвестный инструмент', $agent->trace()[0]['output']);
});
test('finish: можно отключить перехват через finishTool', function (): void {
$client = new FakeClient([
toolCall('finish', ['result' => 'x'], 'f1'),
textResult('обычный ответ'),
]);
$agent = new Agent(
client: $client,
tools: (new ToolRegistry())->add(new FinishTool()),
systemPrompt: 'тест',
maxRounds: 3,
finishTool: '',
);
assertSame('обычный ответ', $agent->ask('сделай'));
});
+38
View File
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
// Тесты: архитектурные границы между слоями.
test('архитектура: ядро не зависит от инструментов', function (): void {
foreach (glob(dirname(__DIR__, 2) . '/src/*.php') ?: [] as $file) {
$code = (string) file_get_contents($file);
assertTrue(
preg_match('/^use AiSdk\\\\Tools\\\\/m', $code) !== 1,
'ядро импортирует инструменты: ' . basename($file)
);
}
});
test('архитектура: инструменты не создают клиента модели', function (): void {
foreach (glob(dirname(__DIR__, 2) . '/tools/*.php') ?: [] as $file) {
$code = (string) file_get_contents($file);
assertTrue(
str_contains($code, 'AiClient') === false,
'инструмент зависит от AiClient: ' . basename($file)
);
}
});
test('архитектура: инструменты не работают с cURL напрямую', function (): void {
foreach (glob(dirname(__DIR__, 2) . '/tools/*.php') ?: [] as $file) {
$code = (string) file_get_contents($file);
assertTrue(
str_contains($code, 'curl_') === false,
'инструмент вызывает cURL напрямую: ' . basename($file)
);
}
});
+383
View File
@@ -0,0 +1,383 @@
<?php
declare(strict_types=1);
// Тесты: files. Запускаются из tests/run.php.
use AiSdk\Agent;
use AiSdk\ChatClient;
use AiSdk\ChatResult;
use AiSdk\HttpTransport;
use AiSdk\HttpRequest;
use AiSdk\LoggingClient;
use AiSdk\Tool;
use AiSdk\ToolRegistry;
use AiSdk\Tools\CalculatorTool;
use AiSdk\Tools\ConvertUnitsTool;
use AiSdk\Tools\CsvQueryTool;
use AiSdk\Tools\DateCalcTool;
use AiSdk\Tools\FileSandbox;
use AiSdk\Tools\FindFilesTool;
use AiSdk\Tools\JsonQueryTool;
use AiSdk\Tools\FinishTool;
use AiSdk\Tools\HashTool;
use AiSdk\Tools\HttpRequestTool;
use AiSdk\Tools\ListDirTool;
use AiSdk\Tools\ReadFileTool;
use AiSdk\Tools\ReplaceInFileTool;
use AiSdk\Tools\SearchFilesTool;
use AiSdk\Tools\ShellTool;
use AiSdk\Tools\TimeTool;
use AiSdk\Tools\WriteFileTool;
use AiSdk\Tools\UuidTool;
// ---------------------------------------------------------------------------
// Файлы
// ---------------------------------------------------------------------------
test('read_file: читает файл внутри корня', function (): void {
$dir = testDir();
file_put_contents($dir . '/note.txt', "первая\nвторая\nтретья");
$result = (new ReadFileTool(new FileSandbox($dir)))->handle(['path' => 'note.txt']);
assertContains('строки 1–3 из 3', $result);
assertContains('вторая', $result);
});
test('read_file: offset и limit в строках', function (): void {
$dir = testDir();
file_put_contents($dir . '/note.txt', "1\n2\n3\n4\n5");
$result = (new ReadFileTool(new FileSandbox($dir)))
->handle(['path' => 'note.txt', 'offset' => 1, 'limit' => 2]);
assertContains('строки 2–3 из 5', $result);
assertContains("2\n3", $result);
});
test('read_file: побег через .. запрещён', function (): void {
$dir = testDir();
file_put_contents(dirname($dir) . '/outside.txt', 'секрет');
$result = (new ReadFileTool(new FileSandbox($dir)))->handle(['path' => '../outside.txt']);
assertContains('не должен содержать', $result);
});
test('read_file: абсолютный путь запрещён', function (): void {
$dir = testDir();
$result = (new ReadFileTool(new FileSandbox($dir)))->handle(['path' => '/etc/passwd']);
assertContains('Абсолютные пути запрещены', $result);
});
test('read_file: бинарный файл не читается', function (): void {
$dir = testDir();
file_put_contents($dir . '/bin.dat', "\x00\x01\x02\xFF");
$result = (new ReadFileTool(new FileSandbox($dir)))->handle(['path' => 'bin.dat']);
assertContains('бинарный', $result);
});
test('list_dir: показывает каталоги и файлы', function (): void {
$dir = testDir();
mkdir($dir . '/sub');
file_put_contents($dir . '/a.txt', 'hello');
$result = (new ListDirTool(new FileSandbox($dir)))->handle([]);
assertContains('sub/', $result);
assertContains('a.txt', $result);
});
test('list_dir: выход за корень запрещён', function (): void {
$dir = testDir();
$result = (new ListDirTool(new FileSandbox($dir)))->handle(['path' => '..']);
assertContains('не должен содержать', $result);
});
// ---------------------------------------------------------------------------
// CSV
// ---------------------------------------------------------------------------
test('csv_query: фильтр по колонке', function (): void {
$dir = testDir();
file_put_contents(
$dir . '/people.csv',
"name,city,age\nИван,Тула,30\nМария,Москва,25\nПётр,Тула,41\n"
);
$result = (new CsvQueryTool(new FileSandbox($dir)))->handle([
'path' => 'people.csv',
'where' => 'city=Тула',
]);
assertContains('Иван', $result);
assertContains('Пётр', $result);
assertTrue(!str_contains($result, 'Мария'), 'строка из другого города попала в выборку');
assertContains('2 из 3 строк', $result);
});
test('csv_query: вхождение подстроки и лимит', function (): void {
$dir = testDir();
file_put_contents($dir . '/people.csv', "name,city\nИван,Тула\nИгорь,Тула\nПётр,Тула\n");
$result = (new CsvQueryTool(new FileSandbox($dir)))->handle([
'path' => 'people.csv',
'where' => 'name~и',
'columns' => 'name',
'limit' => 2,
]);
assertContains('2 из 3 строк', $result);
assertTrue(!str_contains($result, 'city'), 'лишняя колонка в выборке');
});
test('csv_query: неизвестная колонка — понятная ошибка', function (): void {
$dir = testDir();
file_put_contents($dir . '/people.csv', "name,city\nИван,Тула\n");
$result = (new CsvQueryTool(new FileSandbox($dir)))->handle([
'path' => 'people.csv',
'where' => 'город=Тула',
]);
assertContains('Нет колонки', $result);
assertContains('name, city', $result);
});
// ---------------------------------------------------------------------------
// Запись и правка файлов
// ---------------------------------------------------------------------------
test('write_file: создаёт файл', function (): void {
$dir = testDir();
$result = (new WriteFileTool(new FileSandbox($dir)))->handle([
'path' => 'note.txt',
'content' => 'привет',
]);
assertContains('Создан', $result);
assertSame('привет', file_get_contents($dir . '/note.txt'));
});
test('write_file: перезаписывает файл', function (): void {
$dir = testDir();
file_put_contents($dir . '/note.txt', 'старое');
$result = (new WriteFileTool(new FileSandbox($dir)))->handle([
'path' => 'note.txt',
'content' => 'новое',
]);
assertContains('Перезаписан', $result);
assertSame('новое', file_get_contents($dir . '/note.txt'));
});
test('write_file: создаёт каталоги с create_dirs', function (): void {
$dir = testDir();
$result = (new WriteFileTool(new FileSandbox($dir)))->handle([
'path' => 'a/b/c.txt',
'content' => 'x',
'create_dirs' => true,
]);
assertContains('Создан', $result);
assertTrue(is_file($dir . '/a/b/c.txt'), 'файл не создан');
});
test('write_file: без create_dirs каталог обязателен', function (): void {
$dir = testDir();
$result = (new WriteFileTool(new FileSandbox($dir)))->handle([
'path' => 'nope/x.txt',
'content' => 'x',
]);
assertContains('Каталог не существует', $result);
});
test('write_file: запись вне песочницы запрещена', function (): void {
$dir = testDir();
$result = (new WriteFileTool(new FileSandbox($dir)))->handle([
'path' => '../evil.txt',
'content' => 'x',
]);
assertContains('не должен содержать', $result);
});
test('replace_in_file: заменяет единственное совпадение', function (): void {
$dir = testDir();
file_put_contents($dir . '/code.txt', "class A\nclass B\n");
$result = (new ReplaceInFileTool(new FileSandbox($dir)))->handle([
'path' => 'code.txt',
'search' => 'class A',
'replace' => 'final class A',
]);
assertContains('Заменено вхождений: 1', $result);
$content = (string) file_get_contents($dir . '/code.txt');
assertContains('final class A', $content);
assertContains('class B', $content);
});
test('replace_in_file: несколько совпадений требуют all', function (): void {
$dir = testDir();
file_put_contents($dir . '/code.txt', "x\nx\n");
$result = (new ReplaceInFileTool(new FileSandbox($dir)))->handle([
'path' => 'code.txt',
'search' => 'x',
'replace' => 'y',
]);
assertContains('Найдено совпадений: 2', $result);
});
test('replace_in_file: не найдено — понятная ошибка', function (): void {
$dir = testDir();
file_put_contents($dir . '/code.txt', 'abc');
$result = (new ReplaceInFileTool(new FileSandbox($dir)))->handle([
'path' => 'code.txt',
'search' => 'zzz',
'replace' => 'y',
]);
assertContains('не найден', $result);
});
// ---------------------------------------------------------------------------
// Поиск по файлам
// ---------------------------------------------------------------------------
test('search_files: находит строки с номерами', function (): void {
$dir = testDir();
file_put_contents($dir . '/a.php', "<?php\nclass Foo {}\n");
file_put_contents($dir . '/b.txt', "class Bar\n");
$result = (new SearchFilesTool(new FileSandbox($dir)))->handle(['pattern' => 'class \w+']);
assertContains('a.php:2:', $result);
assertContains('b.txt:1:', $result);
});
test('search_files: glob ограничивает файлы', function (): void {
$dir = testDir();
file_put_contents($dir . '/a.php', "class Foo\n");
file_put_contents($dir . '/b.txt', "class Bar\n");
$result = (new SearchFilesTool(new FileSandbox($dir)))->handle([
'pattern' => 'class',
'glob' => '*.php',
]);
assertContains('a.php', $result);
assertTrue(!str_contains($result, 'b.txt'), 'лишний файл в выдаче');
});
test('search_files: служебные каталоги пропускаются', function (): void {
$dir = testDir();
mkdir($dir . '/vendor', 0777, true);
file_put_contents($dir . '/vendor/lib.php', "class Vendor\n");
file_put_contents($dir . '/app.php', "class App\n");
$result = (new SearchFilesTool(new FileSandbox($dir)))->handle(['pattern' => 'class']);
assertContains('app.php', $result);
assertTrue(!str_contains($result, 'vendor'), 'vendor не должен сканироваться');
});
test('search_files: некорректная регулярка', function (): void {
$dir = testDir();
$result = (new SearchFilesTool(new FileSandbox($dir)))->handle(['pattern' => '([']);
assertContains('Некорректное регулярное выражение', $result);
});
test('find_files: поиск по маске', function (): void {
$dir = testDir();
file_put_contents($dir . '/a.php', 'x');
file_put_contents($dir . '/b.txt', 'x');
$result = (new FindFilesTool(new FileSandbox($dir)))->handle(['pattern' => '*.php']);
assertContains('a.php', $result);
assertTrue(!str_contains($result, 'b.txt'), 'лишний файл в выдаче');
});
// ---------------------------------------------------------------------------
// Песочница: ограниченный и свободный режимы
// ---------------------------------------------------------------------------
test('sandbox: по умолчанию ограничена', function (): void {
$dir = testDir();
assertTrue((new FileSandbox($dir))->isRestricted());
assertTrue(!FileSandbox::unrestricted($dir)->isRestricted());
});
test('sandbox: свободный режим читает вне корня', function (): void {
$dir = testDir();
$outside = str_replace('\\', '/', dirname($dir) . '/outside.txt');
file_put_contents($outside, 'снаружи');
$result = (new ReadFileTool(FileSandbox::unrestricted($dir)))->handle(['path' => $outside]);
assertContains('снаружи', $result);
});
test('sandbox: свободный режим пишет вне корня', function (): void {
$dir = testDir();
$outside = str_replace('\\', '/', dirname($dir) . '/written.txt');
$result = (new WriteFileTool(FileSandbox::unrestricted($dir)))->handle([
'path' => $outside,
'content' => 'записано',
]);
assertContains('Создан', $result);
assertSame('записано', file_get_contents($outside));
});
test('sandbox: политика задаётся для каждого инструмента отдельно', function (): void {
$dir = testDir();
$outside = str_replace('\\', '/', dirname($dir) . '/mixed.txt');
file_put_contents($outside, 'данные');
// Читать можно везде...
$read = new ReadFileTool(FileSandbox::unrestricted($dir));
assertContains('данные', $read->handle(['path' => $outside]));
// ...а писать — только в корень.
$write = new WriteFileTool(new FileSandbox($dir));
assertContains('запрещены', $write->handle(['path' => $outside, 'content' => 'x']));
});
test('sandbox: ограниченный режим по-прежнему блокирует выход', function (): void {
$dir = testDir();
$outside = dirname($dir) . '/blocked.txt';
file_put_contents($outside, 'секрет');
$result = (new ReadFileTool(new FileSandbox($dir)))->handle(['path' => '../blocked.txt']);
assertContains('не должен содержать', $result);
});
+144
View File
@@ -0,0 +1,144 @@
<?php
declare(strict_types=1);
// Тесты: http. Запускаются из tests/run.php.
use AiSdk\Agent;
use AiSdk\ChatClient;
use AiSdk\ChatResult;
use AiSdk\HttpTransport;
use AiSdk\HttpRequest;
use AiSdk\LoggingClient;
use AiSdk\Tool;
use AiSdk\ToolRegistry;
use AiSdk\Tools\CalculatorTool;
use AiSdk\Tools\ConvertUnitsTool;
use AiSdk\Tools\CsvQueryTool;
use AiSdk\Tools\DateCalcTool;
use AiSdk\Tools\FileSandbox;
use AiSdk\Tools\FindFilesTool;
use AiSdk\Tools\JsonQueryTool;
use AiSdk\Tools\FinishTool;
use AiSdk\Tools\HashTool;
use AiSdk\Tools\HttpRequestTool;
use AiSdk\Tools\ListDirTool;
use AiSdk\Tools\ReadFileTool;
use AiSdk\Tools\ReplaceInFileTool;
use AiSdk\Tools\SearchFilesTool;
use AiSdk\Tools\ShellTool;
use AiSdk\Tools\TimeTool;
use AiSdk\Tools\WriteFileTool;
use AiSdk\Tools\UuidTool;
// ---------------------------------------------------------------------------
// http_request: сборка запроса (без сети)
// ---------------------------------------------------------------------------
test('http_request: query добавляется к URL', function (): void {
$request = HttpRequest::build('get', 'https://example.com/search', ['q' => 'кот', 'page' => 2]);
assertSame('GET', $request->method);
assertContains('q=%D0%BA%D0%BE%D1%82', $request->url);
assertContains('page=2', $request->url);
});
test('http_request: JSON-тело и Content-Type', function (): void {
$request = HttpRequest::build('POST', 'https://example.com/orders', body: ['sku' => 'A-1']);
assertSame('{"sku":"A-1"}', $request->body);
assertSame('application/json', $request->headers['Content-Type']);
assertSame('POST', $request->method);
});
test('http_request: form-тело', function (): void {
$request = HttpRequest::build(
'POST',
'https://example.com/login',
body: ['u' => 'a', 'p' => 'b'],
contentType: 'form',
);
assertSame('u=a&p=b', $request->body);
assertSame('application/x-www-form-urlencoded', $request->headers['Content-Type']);
});
test('http_request: Basic-авторизация', function (): void {
$request = HttpRequest::build(
'GET',
'https://example.com/me',
auth: ['user' => 'bot', 'pass' => 'secret'],
);
assertSame('Basic ' . base64_encode('bot:secret'), $request->headers['Authorization']);
});
test('http_request: Bearer-авторизация', function (): void {
$request = HttpRequest::build(
'GET',
'https://example.com/me',
auth: ['type' => 'bearer', 'token' => 'abc'],
);
assertSame('Bearer abc', $request->headers['Authorization']);
});
test('http_request: запрещённые заголовки отбрасываются', function (): void {
$request = HttpRequest::build(
'GET',
'https://example.com',
headers: ['Host' => 'evil', 'X-Ok' => '1'],
);
assertTrue(!isset($request->headers['Host']), 'Host должен отбрасываться');
assertSame('1', $request->headers['X-Ok']);
});
test('http_request: неизвестный метод отвергается', function (): void {
assertThrows(static fn () => HttpRequest::build('TRACE', 'https://example.com'));
});
test('http_request: без схемы подставляется https', function (): void {
assertSame('https://example.com/x', HttpRequest::build('GET', 'example.com/x')->url);
});
test('http: заголовки превращаются в строки cURL', function (): void {
// Регрессия: ассоциативный массив, отданный в cURL напрямую,
// молча терял и Content-Type, и Authorization.
assertSame(['X-Ok: 1'], HttpTransport::headerLines(['X-Ok' => '1']));
assertSame(['Accept: */*'], HttpTransport::headerLines([]));
assertSame(['Accept: */*'], HttpTransport::headerLines(['Accept: */*']));
});
// ---------------------------------------------------------------------------
// http_request: защита от SSRF (без сети)
// ---------------------------------------------------------------------------
test('http_request: localhost запрещён', function (): void {
assertContains('локальному адресу', (new HttpRequestTool())->handle(['url' => 'http://localhost/x']));
});
test('http_request: приватные адреса запрещены', function (): void {
$tool = new HttpRequestTool();
assertContains('приватному адресу', $tool->handle(['url' => 'http://127.0.0.1/x']));
assertContains('приватному адресу', $tool->handle(['url' => 'http://10.0.0.5/x']));
assertContains('приватному адресу', $tool->handle(['url' => 'http://192.168.1.10/x']));
});
test('http_request: только http и https', function (): void {
assertContains('только http и https', (new HttpRequestTool())->handle(['url' => 'ftp://example.com/x']));
});
test('http_request: allowPrivate снимает блокировку внутренней сети', function (): void {
// Порт закрыт: проверяем именно то, что SSRF-отказ больше не выдаётся.
$result = (new HttpRequestTool(allowPrivate: true))->handle(['url' => 'http://127.0.0.1:9/x']);
assertTrue(
!str_contains($result, 'приватному адресу'),
'с allowPrivate адрес не должен блокироваться: ' . $result
);
});
+119
View File
@@ -0,0 +1,119 @@
<?php
declare(strict_types=1);
// Тесты: logging. Запускаются из tests/run.php.
use AiSdk\Agent;
use AiSdk\ChatClient;
use AiSdk\ChatResult;
use AiSdk\HttpTransport;
use AiSdk\HttpRequest;
use AiSdk\LoggingClient;
use AiSdk\Tool;
use AiSdk\ToolRegistry;
use AiSdk\Tools\CalculatorTool;
use AiSdk\Tools\ConvertUnitsTool;
use AiSdk\Tools\CsvQueryTool;
use AiSdk\Tools\DateCalcTool;
use AiSdk\Tools\FileSandbox;
use AiSdk\Tools\FindFilesTool;
use AiSdk\Tools\JsonQueryTool;
use AiSdk\Tools\FinishTool;
use AiSdk\Tools\HashTool;
use AiSdk\Tools\HttpRequestTool;
use AiSdk\Tools\ListDirTool;
use AiSdk\Tools\ReadFileTool;
use AiSdk\Tools\ReplaceInFileTool;
use AiSdk\Tools\SearchFilesTool;
use AiSdk\Tools\ShellTool;
use AiSdk\Tools\TimeTool;
use AiSdk\Tools\WriteFileTool;
use AiSdk\Tools\UuidTool;
// ---------------------------------------------------------------------------
// Логирование
// ---------------------------------------------------------------------------
test('logging: прокидывает ответ и пишет строки', function (): void {
$lines = [];
$client = new LoggingClient(
new FakeClient([textResult('привет')]),
sink: static function (string $line) use (&$lines): void {
$lines[] = $line;
},
);
$result = $client->chat([['role' => 'user', 'content' => 'вопрос']]);
assertSame('привет', $result->text);
assertSame(2, count($lines), 'ожидались две строки: запрос и ответ');
assertContains('LLM →', $lines[0]);
assertContains('LLM ←', $lines[1]);
assertContains('текст 6 симв.', $lines[1]);
});
test('logging: ошибка пишется и пробрасывается', function (): void {
$broken = new class implements ChatClient {
public function chat(array $messages, array $tools = [], array $options = []): ChatResult
{
throw new RuntimeException('сеть упала');
}
};
$lines = [];
$client = new LoggingClient($broken, sink: static function (string $line) use (&$lines): void {
$lines[] = $line;
});
assertThrows(static fn () => $client->chat([['role' => 'user', 'content' => 'x']]));
$log = implode("\n", $lines);
assertContains('LLM ✗', $log);
assertContains('сеть упала', $log);
});
test('logging: verbose печатает сообщения и инструменты', function (): void {
$lines = [];
$client = new LoggingClient(
new FakeClient([textResult('ок')]),
sink: static function (string $line) use (&$lines): void {
$lines[] = $line;
},
verbose: true,
);
$tools = (new ToolRegistry())->add(new TimeTool())->definitions();
$client->chat([['role' => 'user', 'content' => 'который час']], $tools);
$log = implode("\n", $lines);
assertContains('[0] user: который час', $log);
assertContains('инструменты: get_time', $log);
assertContains('ответ: ок', $log);
});
test('logging: в агенте логирует каждый раунд', function (): void {
$lines = [];
$client = new LoggingClient(
new FakeClient([
toolCall('calculator', ['expression' => '2+2'], 'c1'),
textResult('4'),
]),
sink: static function (string $line) use (&$lines): void {
$lines[] = $line;
},
);
$agent = new Agent($client, (new ToolRegistry())->add(new CalculatorTool()), 'тест', 3);
assertSame('4', $agent->ask('посчитай'));
$log = implode("\n", $lines);
assertContains('tool_calls: calculator', $log);
assertSame(4, count($lines), 'два раунда — по две строки');
});
+82
View File
@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
// Тесты: shell. Запускаются из tests/run.php.
use AiSdk\Agent;
use AiSdk\ChatClient;
use AiSdk\ChatResult;
use AiSdk\HttpTransport;
use AiSdk\HttpRequest;
use AiSdk\LoggingClient;
use AiSdk\Tool;
use AiSdk\ToolRegistry;
use AiSdk\Tools\CalculatorTool;
use AiSdk\Tools\ConvertUnitsTool;
use AiSdk\Tools\CsvQueryTool;
use AiSdk\Tools\DateCalcTool;
use AiSdk\Tools\FileSandbox;
use AiSdk\Tools\FindFilesTool;
use AiSdk\Tools\JsonQueryTool;
use AiSdk\Tools\FinishTool;
use AiSdk\Tools\HashTool;
use AiSdk\Tools\HttpRequestTool;
use AiSdk\Tools\ListDirTool;
use AiSdk\Tools\ReadFileTool;
use AiSdk\Tools\ReplaceInFileTool;
use AiSdk\Tools\SearchFilesTool;
use AiSdk\Tools\ShellTool;
use AiSdk\Tools\TimeTool;
use AiSdk\Tools\WriteFileTool;
use AiSdk\Tools\UuidTool;
// ---------------------------------------------------------------------------
// run_command: оболочка
// ---------------------------------------------------------------------------
test('shell: выполняет команду и возвращает код', function (): void {
$result = (new ShellTool(tmpDir: __DIR__ . '/tmp'))->handle(['command' => 'echo hello']);
assertContains('hello', $result);
assertContains('exit: 0', $result);
});
test('shell: возвращает ненулевой код выхода', function (): void {
assertContains('exit: 3', (new ShellTool(tmpDir: __DIR__ . '/tmp'))->handle(['command' => 'exit 3']));
});
test('shell: таймаут прерывает команду', function (): void {
$command = DIRECTORY_SEPARATOR === '\\' ? 'ping -n 5 127.0.0.1 >NUL' : 'sleep 5';
$started = microtime(true);
$result = (new ShellTool(timeout: 0.5, tmpDir: __DIR__ . '/tmp'))->handle(['command' => $command]);
$elapsed = microtime(true) - $started;
assertContains('таймаут', $result);
assertTrue($elapsed < 3.0, 'таймаут не сработал: ' . round($elapsed, 2) . ' с');
});
test('shell: allowlist запрещает лишнее', function (): void {
$result = (new ShellTool(allow: ['echo'], tmpDir: __DIR__ . '/tmp'))->handle(['command' => 'whoami']);
assertContains('не разрешена', $result);
});
test('shell: allowlist пропускает разрешённое', function (): void {
$result = (new ShellTool(allow: ['echo'], tmpDir: __DIR__ . '/tmp'))->handle(['command' => 'echo ok']);
assertContains('ok', $result);
});
test('shell: длинный вывод обрезается', function (): void {
$result = (new ShellTool(maxOutput: 100, tmpDir: __DIR__ . '/tmp'))->handle(['command' => 'echo ' . str_repeat('x', 500)]);
assertContains('обрезано', $result);
});
test('shell: пустая команда не запускается', function (): void {
assertContains('Пустая команда', (new ShellTool(tmpDir: __DIR__ . '/tmp'))->handle(['command' => ' ']));
});
+264
View File
@@ -0,0 +1,264 @@
<?php
declare(strict_types=1);
// Тесты: tools. Запускаются из tests/run.php.
use AiSdk\Agent;
use AiSdk\ChatClient;
use AiSdk\ChatResult;
use AiSdk\HttpTransport;
use AiSdk\HttpRequest;
use AiSdk\LoggingClient;
use AiSdk\Tool;
use AiSdk\ToolRegistry;
use AiSdk\Tools\CalculatorTool;
use AiSdk\Tools\ConvertUnitsTool;
use AiSdk\Tools\CsvQueryTool;
use AiSdk\Tools\DateCalcTool;
use AiSdk\Tools\FileSandbox;
use AiSdk\Tools\FindFilesTool;
use AiSdk\Tools\JsonQueryTool;
use AiSdk\Tools\FinishTool;
use AiSdk\Tools\HashTool;
use AiSdk\Tools\HttpRequestTool;
use AiSdk\Tools\ListDirTool;
use AiSdk\Tools\ReadFileTool;
use AiSdk\Tools\ReplaceInFileTool;
use AiSdk\Tools\SearchFilesTool;
use AiSdk\Tools\ShellTool;
use AiSdk\Tools\TimeTool;
use AiSdk\Tools\WriteFileTool;
use AiSdk\Tools\UuidTool;
// ---------------------------------------------------------------------------
// Инструменты
// ---------------------------------------------------------------------------
test('calculator: приоритет операций', function (): void {
assertSame('2+2*2 = 6', (new CalculatorTool())->handle(['expression' => '2+2*2']));
});
test('calculator: скобки и степень', function (): void {
assertSame('(1+2)^2 = 9', (new CalculatorTool())->handle(['expression' => '(1+2)^2']));
});
test('calculator: деление даёт дробь', function (): void {
assertSame('10/4 = 2.5', (new CalculatorTool())->handle(['expression' => '10/4']));
});
test('calculator: деление на ноль — понятная ошибка', function (): void {
assertContains('деление на ноль', (new CalculatorTool())->handle(['expression' => '1/0']));
});
test('calculator: буквы не пропускаются', function (): void {
assertContains('Разрешены только', (new CalculatorTool())->handle(['expression' => '2+a']));
});
test('time: возвращает дату в нужном поясе', function (): void {
$result = (new TimeTool())->handle(['timezone' => 'Europe/Moscow']);
assertTrue(
preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \S+ \((\p{L}{2})\)$/u', $result) === 1,
'неожиданный формат: ' . $result
);
});
test('time: неизвестный пояс — понятная ошибка', function (): void {
assertContains('Неизвестный часовой пояс', (new TimeTool())->handle(['timezone' => 'Mars/Olympus']));
});
// ---------------------------------------------------------------------------
// Реестр инструментов
// ---------------------------------------------------------------------------
test('реестр: схемы в формате OpenAI tools', function (): void {
$definitions = (new ToolRegistry())->add(new TimeTool(), new CalculatorTool())->definitions();
assertSame(2, count($definitions));
assertSame('function', $definitions[0]['type']);
assertSame('get_time', $definitions[0]['function']['name']);
assertSame('object', $definitions[0]['function']['parameters']['type']);
});
test('реестр: неизвестный инструмент не бросает исключение', function (): void {
$result = (new ToolRegistry())->execute('nope', []);
assertContains('неизвестный инструмент', $result);
});
test('реестр: исключение инструмента превращается в текст', function (): void {
$broken = new class implements Tool {
public function name(): string
{
return 'broken';
}
public function description(): string
{
return 'всегда падает';
}
public function parameters(): array
{
return ['type' => 'object', 'properties' => []];
}
public function handle(array $args): string
{
throw new RuntimeException('сломалось');
}
};
$result = (new ToolRegistry())->add($broken)->execute('broken', []);
assertContains('Ошибка инструмента "broken"', $result);
assertContains('сломалось', $result);
});
test('реестр: длинный результат обрезается', function (): void {
$verbose = new class implements Tool {
public function name(): string
{
return 'verbose';
}
public function description(): string
{
return 'много текста';
}
public function parameters(): array
{
return ['type' => 'object', 'properties' => []];
}
public function handle(array $args): string
{
return str_repeat('x', 100);
}
};
$result = (new ToolRegistry(10))->add($verbose)->execute('verbose', []);
assertSame(11, mb_strlen($result), 'ожидалось 10 символов и многоточие');
});
// ---------------------------------------------------------------------------
// Единицы и даты
// ---------------------------------------------------------------------------
test('convert_units: километры в мили', function (): void {
assertSame('100 km = 62.137119 mi', (new ConvertUnitsTool())->handle([
'value' => 100,
'from' => 'km',
'to' => 'mi',
]));
});
test('convert_units: температура', function (): void {
$tool = new ConvertUnitsTool();
assertSame('0 c = 32 f', $tool->handle(['value' => 0, 'from' => 'c', 'to' => 'f']));
assertSame('100 c = 373.15 k', $tool->handle(['value' => 100, 'from' => 'c', 'to' => 'k']));
});
test('convert_units: русские названия единиц', function (): void {
// Ответ приходит в канонических кодах, независимо от ввода.
assertSame('5 km = 5000 m', (new ConvertUnitsTool())->handle([
'value' => 5,
'from' => 'км',
'to' => 'м',
]));
});
test('convert_units: неизвестная единица', function (): void {
$result = (new ConvertUnitsTool())->handle(['value' => 1, 'from' => 'parsec', 'to' => 'm']);
assertContains('Не знаю единицы', $result);
});
test('date_calc: разница между датами', function (): void {
$result = (new DateCalcTool())->handle(['from' => '2026-01-01', 'to' => '2026-02-01']);
assertContains('31 день', $result);
});
test('date_calc: дата без времени — это полночь, а не «сейчас»', function (): void {
$result = (new DateCalcTool())->handle(['from' => '2026-01-01', 'modifier' => '+1 day']);
assertContains('2026-01-01 00:00:00', $result);
assertContains('2026-01-02 00:00:00', $result);
});
test('date_calc: прибавить интервал', function (): void {
$result = (new DateCalcTool())->handle(['from' => '2026-01-01', 'modifier' => '+30 days']);
assertContains('2026-01-31', $result);
});
test('date_calc: без параметров — подсказка', function (): void {
assertContains('Нужен либо to', (new DateCalcTool())->handle([]));
});
test('date_calc: плохой часовой пояс', function (): void {
assertContains('Неизвестный часовой пояс', (new DateCalcTool())->handle([
'from' => 'now',
'timezone' => 'Mars/Olympus',
]));
});
// ---------------------------------------------------------------------------
// UUID и хеши
// ---------------------------------------------------------------------------
test('uuid: формат v4 и уникальность', function (): void {
$tool = new UuidTool();
$first = $tool->handle([]);
$second = $tool->handle([]);
assertTrue(
preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/', $first) === 1,
'не UUID v4: ' . $first
);
assertTrue($first !== $second, 'два вызова вернули одинаковый UUID');
});
test('hash_text: известные значения', function (): void {
$tool = new HashTool();
assertSame('sha256: ' . hash('sha256', 'привет'), $tool->handle(['text' => 'привет']));
assertSame('md5: ' . md5('x'), $tool->handle(['text' => 'x', 'algo' => 'md5']));
assertContains('Неизвестный алгоритм', $tool->handle(['text' => 'x', 'algo' => 'crc32']));
});
// ---------------------------------------------------------------------------
// JSON
// ---------------------------------------------------------------------------
test('json_query: вложенный путь', function (): void {
$result = (new JsonQueryTool())->handle([
'json' => '{"data":{"items":[{"id":42,"name":"x"}]}}',
'path' => 'data.items[0].id',
]);
assertContains('42', $result);
});
test('json_query: нет ключа — сообщение', function (): void {
$result = (new JsonQueryTool())->handle(['json' => '{"a":1}', 'path' => 'b']);
assertContains('не найден', $result);
});
test('json_query: битый JSON', function (): void {
$result = (new JsonQueryTool())->handle(['json' => '{oops', 'path' => 'a']);
assertContains('Некорректный JSON', $result);
});