Added MCP tools to access info provider features

This commit is contained in:
Jan Böhmer 2026-07-20 22:07:06 +02:00
parent a3fe7ea591
commit 7e53c7ae9a
14 changed files with 708 additions and 0 deletions

View file

@ -0,0 +1,99 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\State\Mcp;
use ApiPlatform\Metadata\Get;
use App\Mcp\DTO\InfoProviderPartDetailsInput;
use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
use App\Services\InfoProviderSystem\DTOtoEntityConverter;
use App\Services\InfoProviderSystem\PartInfoRetriever;
use App\Services\InfoProviderSystem\Providers\InfoProviderInterface;
use App\Services\InfoProviderSystem\ProviderRegistry;
use App\Settings\SystemSettings\LocalizationSettings;
use App\State\Mcp\GetInfoProviderPartDetailsProcessor;
use App\Tests\SettingsTestHelper;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
final class GetInfoProviderPartDetailsProcessorTest extends TestCase
{
private PartInfoRetriever $infoRetriever;
protected function setUp(): void
{
$activeProvider = $this->createMock(InfoProviderInterface::class);
$activeProvider->method('getProviderKey')->willReturn('test1');
$activeProvider->method('isActive')->willReturn(true);
$activeProvider->method('getDetails')->willReturn(
new PartDetailDTO(provider_key: 'test1', provider_id: '42', name: 'Element 42', description: 'desc')
);
$inactiveProvider = $this->createMock(InfoProviderInterface::class);
$inactiveProvider->method('getProviderKey')->willReturn('test2');
$inactiveProvider->method('isActive')->willReturn(false);
$providerRegistry = new ProviderRegistry([$activeProvider, $inactiveProvider]);
$dtoToEntityConverter = new DTOtoEntityConverter(
$this->createMock(EntityManagerInterface::class),
SettingsTestHelper::createSettingsDummy(LocalizationSettings::class)
);
$this->infoRetriever = new PartInfoRetriever(
$providerRegistry,
$dtoToEntityConverter,
new ArrayAdapter(),
debugMode: true
);
}
public function testGetDetails(): void
{
$processor = new GetInfoProviderPartDetailsProcessor($this->infoRetriever);
$result = $processor->process(new InfoProviderPartDetailsInput(provider_key: 'test1', provider_id: '42'), new Get());
$this->assertInstanceOf(PartDetailDTO::class, $result);
$this->assertSame('test1', $result->provider_key);
$this->assertSame('42', $result->provider_id);
}
public function testGetDetailsWithUnknownProviderThrowsBadRequest(): void
{
$processor = new GetInfoProviderPartDetailsProcessor($this->infoRetriever);
$this->expectException(BadRequestHttpException::class);
$processor->process(new InfoProviderPartDetailsInput(provider_key: 'unknown', provider_id: '42'), new Get());
}
public function testGetDetailsWithInactiveProviderThrowsBadRequest(): void
{
$processor = new GetInfoProviderPartDetailsProcessor($this->infoRetriever);
$this->expectException(BadRequestHttpException::class);
$processor->process(new InfoProviderPartDetailsInput(provider_key: 'test2', provider_id: '42'), new Get());
}
}

View file

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\State\Mcp;
use ApiPlatform\Metadata\Get;
use App\Mcp\DTO\ListInfoProvidersInput;
use App\Services\InfoProviderSystem\DTOs\InfoProviderDTO;
use App\Services\InfoProviderSystem\Providers\InfoProviderInterface;
use App\Services\InfoProviderSystem\Providers\ProviderCapabilities;
use App\Services\InfoProviderSystem\ProviderRegistry;
use App\State\Mcp\ListInfoProvidersProcessor;
use PHPUnit\Framework\TestCase;
final class ListInfoProvidersProcessorTest extends TestCase
{
public function testOnlyActiveProvidersAreListed(): void
{
$active = $this->createMock(InfoProviderInterface::class);
$active->method('getProviderKey')->willReturn('active_provider');
$active->method('isActive')->willReturn(true);
$active->method('getProviderInfo')->willReturn([
'name' => 'Active Provider',
'description' => 'A provider that is active',
'url' => 'https://example.com',
]);
$active->method('getCapabilities')->willReturn([ProviderCapabilities::BASIC, ProviderCapabilities::PRICE]);
$disabled = $this->createMock(InfoProviderInterface::class);
$disabled->method('getProviderKey')->willReturn('disabled_provider');
$disabled->method('isActive')->willReturn(false);
$disabled->method('getProviderInfo')->willReturn(['name' => 'Disabled Provider']);
$disabled->method('getCapabilities')->willReturn([]);
$registry = new ProviderRegistry([$active, $disabled]);
$processor = new ListInfoProvidersProcessor($registry);
$result = $processor->process(new ListInfoProvidersInput(), new Get());
$this->assertCount(1, $result);
$this->assertInstanceOf(InfoProviderDTO::class, $result[0]);
$this->assertSame('active_provider', $result[0]->key);
$this->assertSame('Active Provider', $result[0]->name);
$this->assertSame('A provider that is active', $result[0]->description);
$this->assertSame('https://example.com', $result[0]->url);
$this->assertSame(['BASIC', 'PRICE'], $result[0]->capabilities);
}
}

View file

@ -0,0 +1,128 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\State\Mcp;
use ApiPlatform\Metadata\Get;
use App\Mcp\DTO\InfoProviderSearchInput;
use App\Services\InfoProviderSystem\DTOs\SearchResultDTO;
use App\Services\InfoProviderSystem\DTOtoEntityConverter;
use App\Services\InfoProviderSystem\PartInfoRetriever;
use App\Services\InfoProviderSystem\Providers\InfoProviderInterface;
use App\Services\InfoProviderSystem\ProviderRegistry;
use App\Settings\InfoProviderSystem\InfoProviderGeneralSettings;
use App\Settings\SystemSettings\LocalizationSettings;
use App\State\Mcp\SearchInfoProvidersProcessor;
use App\Tests\SettingsTestHelper;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
final class SearchInfoProvidersProcessorTest extends TestCase
{
private ProviderRegistry $providerRegistry;
private PartInfoRetriever $infoRetriever;
protected function setUp(): void
{
$providers = [
$this->getMockProvider('test1'),
$this->getMockProvider('test2', active: false),
];
$this->providerRegistry = new ProviderRegistry($providers);
$dtoToEntityConverter = new DTOtoEntityConverter(
$this->createMock(EntityManagerInterface::class),
SettingsTestHelper::createSettingsDummy(LocalizationSettings::class)
);
$this->infoRetriever = new PartInfoRetriever(
$this->providerRegistry,
$dtoToEntityConverter,
new ArrayAdapter(),
debugMode: true
);
}
private function getMockProvider(string $key, bool $active = true): InfoProviderInterface
{
$mock = $this->createMock(InfoProviderInterface::class);
$mock->method('getProviderKey')->willReturn($key);
$mock->method('isActive')->willReturn($active);
$mock->method('searchByKeyword')->willReturn([
new SearchResultDTO(provider_key: $key, provider_id: '1', name: 'Element 1', description: 'desc'),
]);
return $mock;
}
public function testSearchWithExplicitProvider(): void
{
$processor = new SearchInfoProvidersProcessor($this->infoRetriever, $this->providerRegistry, SettingsTestHelper::createSettingsDummy(InfoProviderGeneralSettings::class));
$result = $processor->process(new InfoProviderSearchInput(keyword: 'foo', providers: ['test1']), new Get());
$this->assertCount(1, $result);
$this->assertInstanceOf(SearchResultDTO::class, $result[0]);
$this->assertSame('test1', $result[0]->provider_key);
}
public function testSearchWithUnknownProviderThrowsBadRequest(): void
{
$processor = new SearchInfoProvidersProcessor($this->infoRetriever, $this->providerRegistry, SettingsTestHelper::createSettingsDummy(InfoProviderGeneralSettings::class));
$this->expectException(BadRequestHttpException::class);
$processor->process(new InfoProviderSearchInput(keyword: 'foo', providers: ['unknown']), new Get());
}
public function testSearchWithInactiveProviderThrowsBadRequest(): void
{
$processor = new SearchInfoProvidersProcessor($this->infoRetriever, $this->providerRegistry, SettingsTestHelper::createSettingsDummy(InfoProviderGeneralSettings::class));
$this->expectException(BadRequestHttpException::class);
$processor->process(new InfoProviderSearchInput(keyword: 'foo', providers: ['test2']), new Get());
}
public function testSearchFallsBackToConfiguredDefaultProviders(): void
{
$settings = SettingsTestHelper::createSettingsDummy(InfoProviderGeneralSettings::class);
$settings->defaultSearchProviders = ['test1'];
$processor = new SearchInfoProvidersProcessor($this->infoRetriever, $this->providerRegistry, $settings);
$result = $processor->process(new InfoProviderSearchInput(keyword: 'foo'), new Get());
$this->assertCount(1, $result);
$this->assertSame('test1', $result[0]->provider_key);
}
public function testSearchWithoutProvidersAndNoDefaultsThrowsBadRequest(): void
{
$processor = new SearchInfoProvidersProcessor($this->infoRetriever, $this->providerRegistry, SettingsTestHelper::createSettingsDummy(InfoProviderGeneralSettings::class));
$this->expectException(BadRequestHttpException::class);
$processor->process(new InfoProviderSearchInput(keyword: 'foo'), new Get());
}
}