rob-revisited/ide/tests/BootstrapAssistantTest.php

66 lines
2.4 KiB
PHP

<?php
namespace JumPHP\Ide\Tests;
use JumPHP\Ide\Async\Deferred;
use JumPHP\Ide\Bootstrap\Assistant;
use PHPUnit\Framework\TestCase;
final class BootstrapAssistantTest extends TestCase
{
/** @var list<list<string>> */
private array $commands = [];
private function assistant(string $stdout, int $code = 0): Assistant
{
return new Assistant(function (array $command) use ($stdout, $code): Deferred {
$this->commands[] = $command;
return Deferred::resolved(['code' => $code, 'stdout' => $stdout, 'stderr' => '']);
});
}
private static function settled(Deferred $d): mixed
{
if ($d->error()) {
throw $d->error();
}
return $d->result();
}
public function testClaudeIsAskedWithoutToolsAndItsTextComesBack(): void
{
$answer = self::settled($this->assistant((string)json_encode(['is_error' => false, 'result' => '<div class="row"></div>']))
->ask('Make a row', ['provider' => 'claude', 'model' => 'haiku']));
self::assertSame('<div class="row"></div>', $answer);
$command = $this->commands[0];
self::assertSame('claude', $command[0]);
self::assertContains('--tools', $command);
self::assertSame('haiku', $command[array_search('--model', $command, true) + 1]);
}
public function testClaudeErrorsAreReported(): void
{
$this->expectException(\RuntimeException::class);
self::settled($this->assistant((string)json_encode(['is_error' => true, 'result' => 'rate limited']))->ask('x'));
}
public function testOpenCodeGetsTheModelAndItsOutputIsTheAnswer(): void
{
$answer = self::settled($this->assistant('<p>hi</p>')->ask('Make a paragraph', ['provider' => 'opencode', 'model' => 'x/y']));
self::assertSame('<p>hi</p>', $answer);
self::assertSame(['opencode', 'run'], array_slice($this->commands[0], 0, 2));
self::assertSame(['-m', 'x/y'], array_slice($this->commands[0], -2));
}
public function testAnEmptyPromptAndAnUnknownProviderAreRefused(): void
{
foreach ([['', 'claude'], ['x', 'nobody']] as [$prompt, $provider]) {
try {
self::settled($this->assistant('')->ask($prompt, ['provider' => $provider]));
self::fail('refused');
} catch (\InvalidArgumentException) {
self::assertSame([], $this->commands);
}
}
}
}