SuperSeller3000/tests/Unit/Infrastructure/AI/Agent/OllamaVisionAgentTest.php
Simon Kuehn a79791a972
Some checks are pending
CI / test (push) Waiting to run
style: apply CS Fixer formatting across codebase
Consistent brace style, spacing, and method expansion throughout
domain, infrastructure, and test files.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-19 10:56:37 +00:00

82 lines
3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Unit\Infrastructure\AI\Agent;
use App\Domain\AI\PromptTemplate;
use App\Domain\AI\Repository\PromptTemplateRepositoryInterface;
use App\Infrastructure\AI\Agent\OllamaVisionAgent;
use App\Infrastructure\AI\OllamaClientInterface;
use App\Infrastructure\AI\PromptTemplateService;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
final class OllamaVisionAgentTest extends TestCase
{
private OllamaClientInterface&MockObject $ollama;
private OllamaVisionAgent $agent;
protected function setUp(): void
{
$this->ollama = $this->createMock(OllamaClientInterface::class);
$repo = $this->createMock(PromptTemplateRepositoryInterface::class);
$repo->method('findByKey')->willReturn(new PromptTemplate('vision_analyze', 'prompt'));
$prompts = new PromptTemplateService($repo);
$this->agent = new OllamaVisionAgent($this->ollama, $prompts, 'llava');
}
public function testParsesAllFields(): void
{
$this->ollama->method('generateWithImage')->willReturn(
"MANUFACTURER: Lenovo\nMODEL_NAME: ThinkBook 14 G6 IRL\nMODEL_NUMBER: 21KG00NQGE\nSERIAL: PNV09SJZ"
);
$result = $this->agent->analyze('/tmp/photo.jpg');
$this->assertSame('Lenovo', $result['manufacturer']);
$this->assertSame('ThinkBook 14 G6 IRL', $result['modelName']);
$this->assertSame('21KG00NQGE', $result['modelNumber']);
$this->assertSame('PNV09SJZ', $result['serial']);
}
public function testStripsSerialBleedIntoModelNumberLeading(): void
{
// LLM outputs "SERIAL: xyz" as the entire value for MODEL_NUMBER
$this->ollama->method('generateWithImage')->willReturn(
"MANUFACTURER: ELPIDA\nMODEL_NAME: 2GB 2Rx8 PC3-10600S-9-10-F1\nMODEL_NUMBER: SERIAL: 1005NK677594\nSERIAL: 1005NK677594"
);
$result = $this->agent->analyze('/tmp/photo.jpg');
$this->assertSame('', $result['modelNumber']);
$this->assertSame('1005NK677594', $result['serial']);
}
public function testStripsSerialBleedIntoModelNameMidValue(): void
{
// LLM appends serial after the model name
$this->ollama->method('generateWithImage')->willReturn(
"MANUFACTURER: Lenovo\nMODEL_NAME: ThinkBook 14 G6 IRL SERIAL: PNV09SJZ\nMODEL_NUMBER: 21KG00NQGE\nSERIAL: PNV09SJZ"
);
$result = $this->agent->analyze('/tmp/photo.jpg');
$this->assertSame('ThinkBook 14 G6 IRL', $result['modelName']);
$this->assertSame('PNV09SJZ', $result['serial']);
}
public function testReturnsEmptyStringsWhenFieldsMissing(): void
{
$this->ollama->method('generateWithImage')->willReturn('I cannot read the nameplate.');
$result = $this->agent->analyze('/tmp/photo.jpg');
$this->assertSame('', $result['manufacturer']);
$this->assertSame('', $result['modelName']);
$this->assertSame('', $result['modelNumber']);
$this->assertSame('', $result['serial']);
}
}