|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
| 4 | + * |
| 5 | + * Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics) |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Affero General Public License as published |
| 9 | + * by the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +/** |
| 22 | + * Tests for App\Services\AI\AIPlatformRegistry |
| 23 | + */ |
| 24 | +declare(strict_types=1); |
| 25 | + |
| 26 | +namespace App\Tests\Services\AI; |
| 27 | + |
| 28 | +use App\Services\AI\AIPlatformRegistry; |
| 29 | +use App\Services\AI\AIPlatforms; |
| 30 | +use App\Services\AI\AIPlatformSettingsInterface; |
| 31 | +use Jbtronics\SettingsBundle\Manager\SettingsManagerInterface; |
| 32 | +use PHPUnit\Framework\TestCase; |
| 33 | +use Symfony\AI\Platform\PlatformInterface; |
| 34 | + |
| 35 | +class AIPlatformRegistryTest extends TestCase |
| 36 | +{ |
| 37 | + public function testRegistersEnabledPlatformsAndReturnsPlatform(): void |
| 38 | + { |
| 39 | + // Create a platform mock and expose it under the service tag name (openrouter) |
| 40 | + $platformMock = $this->createMock(PlatformInterface::class); |
| 41 | + |
| 42 | + // Settings for OpenRouter -> enabled |
| 43 | + $openRouterSettings = $this->createMock(AIPlatformSettingsInterface::class); |
| 44 | + $openRouterSettings->method('isAIPlatformEnabled')->willReturn(true); |
| 45 | + |
| 46 | + // Settings for LMStudio -> disabled |
| 47 | + $lmSettings = $this->createMock(AIPlatformSettingsInterface::class); |
| 48 | + $lmSettings->method('isAIPlatformEnabled')->willReturn(false); |
| 49 | + |
| 50 | + // Settings manager should return the corresponding settings object depending on the requested class name |
| 51 | + $settingsManager = $this->createMock(SettingsManagerInterface::class); |
| 52 | + $settingsManager->method('get')->willReturnMap([ |
| 53 | + [AIPlatforms::OPENROUTER->toSettingsClass(), $openRouterSettings], |
| 54 | + [AIPlatforms::LMSTUDIO->toSettingsClass(), $lmSettings], |
| 55 | + ]); |
| 56 | + |
| 57 | + $platforms = new \ArrayIterator([ |
| 58 | + AIPlatforms::OPENROUTER->toServiceTagName() => $platformMock, |
| 59 | + ]); |
| 60 | + |
| 61 | + $registry = new AIPlatformRegistry($settingsManager, $platforms); |
| 62 | + |
| 63 | + // OPENROUTER should be enabled and retrievable |
| 64 | + $this->assertTrue($registry->isEnabled(AIPlatforms::OPENROUTER)); |
| 65 | + $this->assertSame($platformMock, $registry->getPlatform(AIPlatforms::OPENROUTER)); |
| 66 | + |
| 67 | + // LMSTUDIO is either not registered or disabled -> should not be enabled |
| 68 | + $this->assertFalse($registry->isEnabled(AIPlatforms::LMSTUDIO)); |
| 69 | + $this->expectException(\InvalidArgumentException::class); |
| 70 | + $registry->getPlatform(AIPlatforms::LMSTUDIO); |
| 71 | + } |
| 72 | + |
| 73 | + public function testGetEnabledPlatformsReturnsIndexedArray(): void |
| 74 | + { |
| 75 | + $platformMock = $this->createMock(PlatformInterface::class); |
| 76 | + |
| 77 | + $openRouterSettings = $this->createMock(AIPlatformSettingsInterface::class); |
| 78 | + $openRouterSettings->method('isAIPlatformEnabled')->willReturn(true); |
| 79 | + |
| 80 | + $settingsManager = $this->createMock(SettingsManagerInterface::class); |
| 81 | + $settingsManager->method('get')->willReturnMap([ |
| 82 | + [AIPlatforms::OPENROUTER->toSettingsClass(), $openRouterSettings], |
| 83 | + [AIPlatforms::LMSTUDIO->toSettingsClass(), $this->createMock(AIPlatformSettingsInterface::class)], |
| 84 | + ]); |
| 85 | + |
| 86 | + $platforms = new \ArrayIterator([ |
| 87 | + AIPlatforms::OPENROUTER->toServiceTagName() => $platformMock, |
| 88 | + // lmstudio not registered |
| 89 | + ]); |
| 90 | + |
| 91 | + $registry = new AIPlatformRegistry($settingsManager, $platforms); |
| 92 | + |
| 93 | + $enabled = $registry->getEnabledPlatforms(); |
| 94 | + |
| 95 | + $this->assertArrayHasKey(AIPlatforms::OPENROUTER->value, $enabled); |
| 96 | + $this->assertSame($platformMock, $enabled[AIPlatforms::OPENROUTER->value]); |
| 97 | + } |
| 98 | +} |
| 99 | + |
0 commit comments