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); } }