@ -9,12 +9,14 @@ use flight\commands\AiGenerateInstructionsCommand;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestCase;
use tests\classes\NoExitInteractor;
use tests\classes\NoExitInteractor;
class AiGenerateInstructionsCommandTest extends TestCase {
class AiGenerateInstructionsCommandTest extends TestCase
{
protected static $in;
protected static $in;
protected static $ou;
protected static $ou;
protected $baseDir;
protected $baseDir;
public function setUp(): void {
public function setUp(): void
{
self::$in = __DIR__ . DIRECTORY_SEPARATOR . 'input.test' . uniqid('', true) . '.txt';
self::$in = __DIR__ . DIRECTORY_SEPARATOR . 'input.test' . uniqid('', true) . '.txt';
self::$ou = __DIR__ . DIRECTORY_SEPARATOR . 'output.test' . uniqid('', true) . '.txt';
self::$ou = __DIR__ . DIRECTORY_SEPARATOR . 'output.test' . uniqid('', true) . '.txt';
file_put_contents(self::$in, '');
file_put_contents(self::$in, '');
@ -25,7 +27,8 @@ class AiGenerateInstructionsCommandTest extends TestCase {
}
}
}
}
public function tearDown(): void {
public function tearDown(): void
{
if (file_exists(self::$in)) {
if (file_exists(self::$in)) {
unlink(self::$in);
unlink(self::$in);
}
}
@ -35,7 +38,8 @@ class AiGenerateInstructionsCommandTest extends TestCase {
$this->recursiveRmdir($this->baseDir);
$this->recursiveRmdir($this->baseDir);
}
}
protected function recursiveRmdir($dir) {
protected function recursiveRmdir($dir)
{
if (!is_dir($dir)) {
if (!is_dir($dir)) {
return;
return;
}
}
@ -46,7 +50,8 @@ class AiGenerateInstructionsCommandTest extends TestCase {
return rmdir($dir);
return rmdir($dir);
}
}
protected function newApp($command): Application {
protected function newApp($command): Application
{
$app = new Application('test', '0.0.1', function ($exitCode) {
$app = new Application('test', '0.0.1', function ($exitCode) {
return $exitCode;
return $exitCode;
});
});
@ -55,11 +60,13 @@ class AiGenerateInstructionsCommandTest extends TestCase {
return $app;
return $app;
}
}
protected function setInput(array $lines): void {
protected function setInput(array $lines): void
{
file_put_contents(self::$in, implode("\n", $lines) . "\n");
file_put_contents(self::$in, implode("\n", $lines) . "\n");
}
}
protected function setProjectRoot($command, $path) {
protected function setProjectRoot($command, $path)
{
$reflection = new \ReflectionClass(get_class($command));
$reflection = new \ReflectionClass(get_class($command));
$property = null;
$property = null;
$currentClass = $reflection;
$currentClass = $reflection;
@ -79,7 +86,8 @@ class AiGenerateInstructionsCommandTest extends TestCase {
}
}
}
}
public function testFailsIfAiConfigMissing() {
public function testFailsIfAiConfigMissing()
{
$this->setInput([
$this->setInput([
'desc',
'desc',
'none',
'none',
@ -107,7 +115,8 @@ class AiGenerateInstructionsCommandTest extends TestCase {
$this->assertStringContainsString('Missing AI configuration', file_get_contents(self::$ou));
$this->assertStringContainsString('Missing AI configuration', file_get_contents(self::$ou));
}
}
public function testWritesInstructionsToFiles() {
public function testWritesInstructionsToFiles()
{
$creds = [
$creds = [
'api_key' => 'key',
'api_key' => 'key',
'model' => 'gpt-4o',
'model' => 'gpt-4o',
@ -154,7 +163,8 @@ class AiGenerateInstructionsCommandTest extends TestCase {
$this->assertFileExists($this->baseDir . '.windsurfrules');
$this->assertFileExists($this->baseDir . '.windsurfrules');
}
}
public function testNoInstructionsReturnedFromLlm() {
public function testNoInstructionsReturnedFromLlm()
{
$creds = [
$creds = [
'api_key' => 'key',
'api_key' => 'key',
'model' => 'gpt-4o',
'model' => 'gpt-4o',
@ -196,7 +206,8 @@ class AiGenerateInstructionsCommandTest extends TestCase {
$this->assertSame(1, $result);
$this->assertSame(1, $result);
}
}
public function testLlmApiCallFails() {
public function testLlmApiCallFails()
{
$creds = [
$creds = [
'api_key' => 'key',
'api_key' => 'key',
'model' => 'gpt-4o',
'model' => 'gpt-4o',
@ -234,7 +245,8 @@ class AiGenerateInstructionsCommandTest extends TestCase {
$this->assertSame(1, $result);
$this->assertSame(1, $result);
}
}
public function testUsesDeprecatedConfigFile() {
public function testUsesDeprecatedConfigFile()
{
$creds = [
$creds = [
'ai' => [
'ai' => [
'api_key' => 'key',
'api_key' => 'key',