SuperSeller3000/tests/Unit/Infrastructure/AI/Agent/JsonCodingAgentTest.php
Simon Kuehn 376171303e fix: vision agent serial-bleed regex + fix broken agent unit tests
OllamaVisionAgent.extractField() now handles field labels at the start
of a value (e.g. MODEL_NUMBER: "SERIAL: 1005NK677594" -> "") not just
mid-value bleed. Both agent test files updated to mock
PromptTemplateRepositoryInterface and construct a real
PromptTemplateService, since the service is final and unmockable.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-18 16:57:01 +00:00

64 lines
2.3 KiB
PHP

<?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;
use App\Domain\AI\PromptTemplate;
use App\Domain\AI\Repository\PromptTemplateRepositoryInterface;
use App\Infrastructure\AI\Agent\JsonCodingAgent;
use App\Infrastructure\AI\OllamaClientInterface;
use App\Infrastructure\AI\PromptTemplateService;
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);
$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');
$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);
}
}