2026-05-17 22:43:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Tests\Unit\Infrastructure\AI\Agent;
|
|
|
|
|
|
|
|
|
|
use App\Domain\Article\ArticleType;
|
|
|
|
|
use App\Domain\Article\AttributeDefinition;
|
|
|
|
|
use App\Domain\Article\AttributeType;
|
2026-05-18 16:57:01 +00:00
|
|
|
use App\Domain\AI\PromptTemplate;
|
|
|
|
|
use App\Domain\AI\Repository\PromptTemplateRepositoryInterface;
|
2026-05-17 22:43:47 +00:00
|
|
|
use App\Infrastructure\AI\Agent\JsonCodingAgent;
|
|
|
|
|
use App\Infrastructure\AI\OllamaClientInterface;
|
2026-05-18 16:57:01 +00:00
|
|
|
use App\Infrastructure\AI\PromptTemplateService;
|
2026-05-17 22:43:47 +00:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
|
|
final class JsonCodingAgentTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
private OllamaClientInterface&MockObject $ollama;
|
|
|
|
|
private JsonCodingAgent $agent;
|
|
|
|
|
private ArticleType $type;
|
|
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
$this->ollama = $this->createMock(OllamaClientInterface::class);
|
2026-05-18 16:57:01 +00:00
|
|
|
|
|
|
|
|
$repo = $this->createMock(PromptTemplateRepositoryInterface::class);
|
|
|
|
|
$repo->method('findByKey')->willReturn(new PromptTemplate('json_coding', '{{schema}}{{missingHint}}{{specsText}}'));
|
|
|
|
|
$prompts = new PromptTemplateService($repo);
|
|
|
|
|
|
|
|
|
|
$this->agent = new JsonCodingAgent($this->ollama, $prompts, 'llama3.2');
|
2026-05-17 22:43:47 +00:00
|
|
|
$this->type = new ArticleType('Notebook');
|
|
|
|
|
$ramDef = new AttributeDefinition('RAM', AttributeType::String);
|
|
|
|
|
$this->type->addAttributeDefinition($ramDef);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testReturnsParsedAttributes(): void
|
|
|
|
|
{
|
|
|
|
|
$first = $this->type->getAttributeDefinitions()->first();
|
|
|
|
|
\assert($first instanceof AttributeDefinition);
|
|
|
|
|
$defId = $first->getId()->toRfc4122();
|
|
|
|
|
$this->ollama->method('generate')
|
|
|
|
|
->willReturn('```json'."\n".'{"'.$defId.'": "16 GB"}'."\n".'```');
|
|
|
|
|
|
|
|
|
|
$result = $this->agent->encode($this->type, 'Dell Latitude 5520: 16 GB RAM, Intel i7');
|
|
|
|
|
|
|
|
|
|
self::assertCount(1, $result);
|
|
|
|
|
self::assertSame('16 GB', array_values($result)[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testExtractsJsonFromMarkdownFences(): void
|
|
|
|
|
{
|
|
|
|
|
$first = $this->type->getAttributeDefinitions()->first();
|
|
|
|
|
\assert($first instanceof AttributeDefinition);
|
|
|
|
|
$defId = $first->getId()->toRfc4122();
|
|
|
|
|
$this->ollama->method('generate')
|
|
|
|
|
->willReturn("Here is the JSON:\n```json\n{\"{$defId}\": \"16 GB\"}\n```\nDone.");
|
|
|
|
|
|
|
|
|
|
$result = $this->agent->encode($this->type, 'Specs text');
|
|
|
|
|
|
|
|
|
|
self::assertArrayHasKey($defId, $result);
|
|
|
|
|
}
|
|
|
|
|
}
|