mirror of https://github.com/flightphp/core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
377 lines
12 KiB
377 lines
12 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace tests\commands;
|
|
|
|
use Ahc\Cli\Application;
|
|
use flight\commands\AiGenerateInstructionsCommand;
|
|
use PHPUnit\Framework\TestCase;
|
|
use tests\classes\NoExitInteractor;
|
|
|
|
class AiGenerateInstructionsCommandTest extends TestCase
|
|
{
|
|
protected static $in;
|
|
protected static $ou;
|
|
protected $baseDir;
|
|
|
|
public function setUp(): void
|
|
{
|
|
self::$in = __DIR__ . DIRECTORY_SEPARATOR . 'input.test' . uniqid('', true) . '.txt';
|
|
self::$ou = __DIR__ . DIRECTORY_SEPARATOR . 'output.test' . uniqid('', true) . '.txt';
|
|
file_put_contents(self::$in, '');
|
|
file_put_contents(self::$ou, '');
|
|
|
|
$this->baseDir = sys_get_temp_dir()
|
|
. DIRECTORY_SEPARATOR
|
|
. 'flightphp-test-basedir-'
|
|
. uniqid('', true)
|
|
. DIRECTORY_SEPARATOR;
|
|
|
|
if (!is_dir($this->baseDir)) {
|
|
mkdir($this->baseDir, 0777, true);
|
|
}
|
|
}
|
|
|
|
public function tearDown(): void
|
|
{
|
|
if (file_exists(self::$in)) {
|
|
unlink(self::$in);
|
|
}
|
|
if (file_exists(self::$ou)) {
|
|
unlink(self::$ou);
|
|
}
|
|
$this->recursiveRmdir($this->baseDir);
|
|
}
|
|
|
|
protected function recursiveRmdir($dir)
|
|
{
|
|
if (!is_dir($dir)) {
|
|
return;
|
|
}
|
|
$files = array_diff(scandir($dir), ['.', '..']);
|
|
foreach ($files as $file) {
|
|
(is_dir("$dir/$file")) ? $this->recursiveRmdir("$dir/$file") : unlink("$dir/$file");
|
|
}
|
|
return rmdir($dir);
|
|
}
|
|
|
|
protected function newApp($command): Application
|
|
{
|
|
$app = new Application('test', '0.0.1', function ($exitCode) {
|
|
return $exitCode;
|
|
});
|
|
$app->io(new NoExitInteractor(self::$in, self::$ou));
|
|
$app->add($command);
|
|
return $app;
|
|
}
|
|
|
|
protected function setInput(array $lines): void
|
|
{
|
|
file_put_contents(self::$in, implode("\n", $lines) . "\n");
|
|
}
|
|
|
|
/**
|
|
* Default interactive answers (templating default is twig).
|
|
*
|
|
* @return array<int,string>
|
|
*/
|
|
protected function defaultAnswers(): array
|
|
{
|
|
return [
|
|
'desc',
|
|
'mysql',
|
|
'twig',
|
|
'y',
|
|
'y',
|
|
'flight/lib',
|
|
'Docker',
|
|
'2',
|
|
'y',
|
|
'context info',
|
|
];
|
|
}
|
|
|
|
protected function setProjectRoot($command, $path)
|
|
{
|
|
$reflection = new \ReflectionClass(get_class($command));
|
|
$property = null;
|
|
$currentClass = $reflection;
|
|
while ($currentClass && !$property) {
|
|
try {
|
|
$property = $currentClass->getProperty('projectRoot');
|
|
} catch (\ReflectionException $e) {
|
|
$currentClass = $currentClass->getParentClass();
|
|
}
|
|
}
|
|
if ($property) {
|
|
// only setAccessible if php 8 or php 7.4
|
|
if (PHP_VERSION_ID < 80100) {
|
|
$property->setAccessible(true);
|
|
}
|
|
$property->setValue($command, $path);
|
|
}
|
|
}
|
|
|
|
public function testFailsIfAiConfigMissing()
|
|
{
|
|
$this->setInput([
|
|
'desc',
|
|
'none',
|
|
'twig',
|
|
'y',
|
|
'y',
|
|
'none',
|
|
'Docker',
|
|
'1',
|
|
'n',
|
|
'no'
|
|
]);
|
|
// Provide 'runway' with dummy data to avoid deprecated configFile logic
|
|
$cmd = $this->getMockBuilder(AiGenerateInstructionsCommand::class)
|
|
->setConstructorArgs([['runway' => ['dummy' => true]]])
|
|
->onlyMethods(['callLlmApi'])
|
|
->getMock();
|
|
$this->setProjectRoot($cmd, $this->baseDir);
|
|
$app = $this->newApp($cmd);
|
|
$result = $app->handle([
|
|
'runway',
|
|
'ai:generate-instructions',
|
|
]);
|
|
$this->assertSame(1, $result);
|
|
$this->assertStringContainsString('Missing AI configuration', file_get_contents(self::$ou));
|
|
}
|
|
|
|
public function testWritesInstructionsToAgentsMdOnly()
|
|
{
|
|
$creds = [
|
|
'api_key' => 'key',
|
|
'model' => 'gpt-4o',
|
|
'base_url' => 'https://api.openai.com',
|
|
];
|
|
$this->setInput($this->defaultAnswers());
|
|
$mockInstructions = "# Project Instructions\n\nUse MySQL, Twig, Docker.";
|
|
$cmd = $this->getMockBuilder(AiGenerateInstructionsCommand::class)
|
|
->setConstructorArgs([
|
|
[
|
|
'runway' => ['ai' => $creds]
|
|
]
|
|
])
|
|
->onlyMethods(['callLlmApi'])
|
|
->getMock();
|
|
$this->setProjectRoot($cmd, $this->baseDir);
|
|
$cmd->expects($this->once())
|
|
->method('callLlmApi')
|
|
->willReturn(json_encode([
|
|
'choices' => [
|
|
['message' => ['content' => $mockInstructions]]
|
|
]
|
|
]));
|
|
$app = $this->newApp($cmd);
|
|
$result = $app->handle([
|
|
'runway',
|
|
'ai:generate-instructions',
|
|
]);
|
|
$this->assertSame(0, $result);
|
|
$this->assertFileExists($this->baseDir . 'AGENTS.md');
|
|
$this->assertSame($mockInstructions, file_get_contents($this->baseDir . 'AGENTS.md'));
|
|
$this->assertFileDoesNotExist($this->baseDir . '.github/copilot-instructions.md');
|
|
$this->assertFileDoesNotExist($this->baseDir . '.cursor/rules/project-overview.mdc');
|
|
$this->assertFileDoesNotExist($this->baseDir . '.gemini/GEMINI.md');
|
|
$this->assertFileDoesNotExist($this->baseDir . '.windsurfrules');
|
|
$this->assertStringContainsString('Updating AGENTS.md', file_get_contents(self::$ou));
|
|
}
|
|
|
|
public function testUsesExistingAgentsMdAsContext()
|
|
{
|
|
$creds = [
|
|
'api_key' => 'key',
|
|
'model' => 'gpt-4o',
|
|
'base_url' => 'https://api.openai.com',
|
|
];
|
|
$existing = "# Existing AGENTS\n\nKeep this context.";
|
|
file_put_contents($this->baseDir . 'AGENTS.md', $existing);
|
|
$this->setInput($this->defaultAnswers());
|
|
|
|
$cmd = $this->getMockBuilder(AiGenerateInstructionsCommand::class)
|
|
->setConstructorArgs([
|
|
[
|
|
'runway' => ['ai' => $creds]
|
|
]
|
|
])
|
|
->onlyMethods(['callLlmApi'])
|
|
->getMock();
|
|
$this->setProjectRoot($cmd, $this->baseDir);
|
|
$cmd->expects($this->once())
|
|
->method('callLlmApi')
|
|
->with(
|
|
$this->anything(),
|
|
$this->anything(),
|
|
$this->callback(function ($jsonData) use ($existing) {
|
|
$data = json_decode($jsonData, true);
|
|
$userContent = $data['messages'][1]['content'] ?? '';
|
|
return strpos($userContent, $existing) !== false;
|
|
}),
|
|
$this->anything()
|
|
)
|
|
->willReturn(json_encode([
|
|
'choices' => [
|
|
['message' => ['content' => "# Updated\n\nDone."]]
|
|
]
|
|
]));
|
|
$app = $this->newApp($cmd);
|
|
$result = $app->handle([
|
|
'runway',
|
|
'ai:generate-instructions',
|
|
]);
|
|
$this->assertSame(0, $result);
|
|
}
|
|
|
|
public function testFallsBackToLegacyCopilotInstructionsForContext()
|
|
{
|
|
$creds = [
|
|
'api_key' => 'key',
|
|
'model' => 'gpt-4o',
|
|
'base_url' => 'https://api.openai.com',
|
|
];
|
|
$legacy = "# Legacy copilot instructions\n\nOld layout.";
|
|
mkdir($this->baseDir . '.github', 0777, true);
|
|
file_put_contents($this->baseDir . '.github/copilot-instructions.md', $legacy);
|
|
$this->setInput($this->defaultAnswers());
|
|
|
|
$cmd = $this->getMockBuilder(AiGenerateInstructionsCommand::class)
|
|
->setConstructorArgs([
|
|
[
|
|
'runway' => ['ai' => $creds]
|
|
]
|
|
])
|
|
->onlyMethods(['callLlmApi'])
|
|
->getMock();
|
|
$this->setProjectRoot($cmd, $this->baseDir);
|
|
$cmd->expects($this->once())
|
|
->method('callLlmApi')
|
|
->with(
|
|
$this->anything(),
|
|
$this->anything(),
|
|
$this->callback(function ($jsonData) use ($legacy) {
|
|
$data = json_decode($jsonData, true);
|
|
$userContent = $data['messages'][1]['content'] ?? '';
|
|
return strpos($userContent, $legacy) !== false;
|
|
}),
|
|
$this->anything()
|
|
)
|
|
->willReturn(json_encode([
|
|
'choices' => [
|
|
['message' => ['content' => "# New AGENTS.md\n\nMigrated."]]
|
|
]
|
|
]));
|
|
$app = $this->newApp($cmd);
|
|
$result = $app->handle([
|
|
'runway',
|
|
'ai:generate-instructions',
|
|
]);
|
|
$this->assertSame(0, $result);
|
|
$this->assertFileExists($this->baseDir . 'AGENTS.md');
|
|
// Legacy file is left alone; only AGENTS.md is written
|
|
$this->assertSame($legacy, file_get_contents($this->baseDir . '.github/copilot-instructions.md'));
|
|
}
|
|
|
|
public function testNoInstructionsReturnedFromLlm()
|
|
{
|
|
$creds = [
|
|
'api_key' => 'key',
|
|
'model' => 'gpt-4o',
|
|
'base_url' => 'https://api.openai.com',
|
|
];
|
|
$this->setInput($this->defaultAnswers());
|
|
$cmd = $this->getMockBuilder(AiGenerateInstructionsCommand::class)
|
|
->setConstructorArgs([
|
|
[
|
|
'runway' => ['ai' => $creds]
|
|
]
|
|
])
|
|
->onlyMethods(['callLlmApi'])
|
|
->getMock();
|
|
$this->setProjectRoot($cmd, $this->baseDir);
|
|
$cmd->expects($this->once())
|
|
->method('callLlmApi')
|
|
->willReturn(json_encode([
|
|
'choices' => [
|
|
['message' => ['content' => '']]
|
|
]
|
|
]));
|
|
$app = $this->newApp($cmd);
|
|
$result = $app->handle([
|
|
'runway',
|
|
'ai:generate-instructions',
|
|
]);
|
|
$this->assertSame(1, $result);
|
|
}
|
|
|
|
public function testLlmApiCallFails()
|
|
{
|
|
$creds = [
|
|
'api_key' => 'key',
|
|
'model' => 'gpt-4o',
|
|
'base_url' => 'https://api.openai.com',
|
|
];
|
|
$this->setInput($this->defaultAnswers());
|
|
$cmd = $this->getMockBuilder(AiGenerateInstructionsCommand::class)
|
|
->setConstructorArgs([
|
|
[
|
|
'runway' => ['ai' => $creds]
|
|
]
|
|
])
|
|
->onlyMethods(['callLlmApi'])
|
|
->getMock();
|
|
$this->setProjectRoot($cmd, $this->baseDir);
|
|
$cmd->expects($this->once())
|
|
->method('callLlmApi')
|
|
->willReturn(false);
|
|
$app = $this->newApp($cmd);
|
|
$result = $app->handle([
|
|
'runway',
|
|
'ai:generate-instructions',
|
|
]);
|
|
$this->assertSame(1, $result);
|
|
}
|
|
|
|
public function testUsesDeprecatedConfigFile()
|
|
{
|
|
$creds = [
|
|
'ai' => [
|
|
'api_key' => 'key',
|
|
'model' => 'gpt-4o',
|
|
'base_url' => 'https://api.openai.com',
|
|
]
|
|
];
|
|
$configFile = $this->baseDir . 'old-config.json';
|
|
file_put_contents($configFile, json_encode($creds));
|
|
$this->setInput($this->defaultAnswers());
|
|
$mockInstructions = "# Project Instructions\n\nUse MySQL, Twig, Docker.";
|
|
// runway key is MISSING from config to trigger deprecated logic
|
|
$cmd = $this->getMockBuilder(AiGenerateInstructionsCommand::class)
|
|
->setConstructorArgs([[]])
|
|
->onlyMethods(['callLlmApi'])
|
|
->getMock();
|
|
$this->setProjectRoot($cmd, $this->baseDir);
|
|
$cmd->expects($this->once())
|
|
->method('callLlmApi')
|
|
->willReturn(json_encode([
|
|
'choices' => [
|
|
['message' => ['content' => $mockInstructions]]
|
|
]
|
|
]));
|
|
$app = $this->newApp($cmd);
|
|
$result = $app->handle([
|
|
'runway',
|
|
'ai:generate-instructions',
|
|
'--config-file=' . $configFile
|
|
]);
|
|
$this->assertSame(0, $result);
|
|
$this->assertStringContainsString('The --config-file option is deprecated', file_get_contents(self::$ou));
|
|
$this->assertFileExists($this->baseDir . 'AGENTS.md');
|
|
$this->assertFileDoesNotExist($this->baseDir . '.github/copilot-instructions.md');
|
|
}
|
|
}
|