-
Notifications
You must be signed in to change notification settings - Fork 24
Add unit tests for OpenAiProvider #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
noruzzamans
wants to merge
7
commits into
WordPress:trunk
Choose a base branch
from
noruzzamans:test/openai-provider-unit-tests
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0b70d95
Add unit tests for OpenAiProvider
noruzzamans e64baf9
Update tests/unit/Provider/OpenAiProviderTest.php
noruzzamans ac01a0a
Update tests/unit/Provider/OpenAiProviderTest.php
noruzzamans ed699c3
Update tests/unit/Provider/OpenAiProviderTest.php
noruzzamans 15e27ea
Add unit tests for capability skipping and multi-capability precedence
noruzzamans 2344a36
Assert embedding fallback on older client and restore speech generati…
noruzzamans a086e2a
Merge branch 'trunk' into test/openai-provider-unit-tests
noruzzamans File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,275 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace WordPress\OpenAiAiProvider\Tests\unit\Provider; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use WordPress\AiClient\AiClient; | ||
| use WordPress\AiClient\Common\Exception\RuntimeException; | ||
| use WordPress\AiClient\Providers\ApiBasedImplementation\ListModelsApiBasedProviderAvailability; | ||
| use WordPress\AiClient\Providers\DTO\ProviderMetadata; | ||
| use WordPress\AiClient\Providers\Models\Contracts\ModelInterface; | ||
| use WordPress\AiClient\Providers\Models\DTO\ModelMetadata; | ||
| use WordPress\AiClient\Providers\Models\EmbeddingGeneration\Contracts\EmbeddingGenerationModelInterface; | ||
| use WordPress\AiClient\Providers\Models\Enums\CapabilityEnum; | ||
| use WordPress\OpenAiAiProvider\Metadata\OpenAiModelMetadataDirectory; | ||
| use WordPress\OpenAiAiProvider\Models\OpenAiEmbeddingGenerationModel; | ||
| use WordPress\OpenAiAiProvider\Models\OpenAiImageGenerationModel; | ||
| use WordPress\OpenAiAiProvider\Models\OpenAiTextGenerationModel; | ||
| use WordPress\OpenAiAiProvider\Models\OpenAiTextToSpeechConversionModel; | ||
| use WordPress\OpenAiAiProvider\Provider\OpenAiProvider; | ||
|
|
||
| /** | ||
| * @covers \WordPress\OpenAiAiProvider\Provider\OpenAiProvider | ||
| */ | ||
| class OpenAiProviderTest extends TestCase | ||
| { | ||
| /** | ||
| * Tests URL resolution and endpoint concatenation with base URL. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testBaseUrlAndUrlConstruction(): void | ||
| { | ||
| $this->assertSame('https://api.openai.com/v1', OpenAiProvider::url()); | ||
| $this->assertSame('https://api.openai.com/v1/models', OpenAiProvider::url('models')); | ||
| $this->assertSame('https://api.openai.com/v1/responses', OpenAiProvider::url('/responses')); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that provider metadata is constructed with correct properties. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testProviderMetadata(): void | ||
| { | ||
| $metadata = OpenAiProvider::metadata(); | ||
|
|
||
| $this->assertInstanceOf(ProviderMetadata::class, $metadata); | ||
| $this->assertSame('openai', $metadata->getId()); | ||
| $this->assertSame('OpenAI', $metadata->getName()); | ||
| $this->assertTrue($metadata->getType()->isCloud()); | ||
| $this->assertSame('https://platform.openai.com/api-keys', $metadata->getCredentialsUrl()); | ||
| $this->assertTrue($metadata->getAuthenticationMethod()->isApiKey()); | ||
|
|
||
| if (version_compare(AiClient::VERSION, '1.2.0', '>=')) { | ||
| $description = $metadata->getDescription(); | ||
| $this->assertNotNull($description); | ||
| $this->assertStringContainsString('GPT and Dall-E', $description); | ||
| } | ||
|
|
||
| if (version_compare(AiClient::VERSION, '1.3.0', '>=')) { | ||
| $logoPath = $metadata->getLogoPath(); | ||
| $this->assertNotNull($logoPath); | ||
| $this->assertStringEndsWith('assets/images/openai.svg', $logoPath); | ||
| $this->assertFileExists($logoPath); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Tests that modelMetadataDirectory returns the directory instance and caches it. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testModelMetadataDirectory(): void | ||
| { | ||
| $directory1 = OpenAiProvider::modelMetadataDirectory(); | ||
| $directory2 = OpenAiProvider::modelMetadataDirectory(); | ||
|
|
||
| $this->assertInstanceOf(OpenAiModelMetadataDirectory::class, $directory1); | ||
| $this->assertSame($directory1, $directory2); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that availability returns the availability checker instance and caches it. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testProviderAvailability(): void | ||
| { | ||
| $availability1 = OpenAiProvider::availability(); | ||
| $availability2 = OpenAiProvider::availability(); | ||
|
|
||
| $this->assertInstanceOf(ListModelsApiBasedProviderAvailability::class, $availability1); | ||
| $this->assertSame($availability1, $availability2); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that createModel creates an OpenAiTextGenerationModel for text-generation capability. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testCreateModelWithTextGenerationCapability(): void | ||
| { | ||
| $modelMetadata = new ModelMetadata( | ||
| 'gpt-4o', | ||
| 'gpt-4o', | ||
| [CapabilityEnum::textGeneration(), CapabilityEnum::chatHistory()], | ||
| [] | ||
| ); | ||
|
|
||
| $model = $this->createModelViaProvider($modelMetadata); | ||
|
|
||
| $this->assertInstanceOf(OpenAiTextGenerationModel::class, $model); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that createModel creates an OpenAiImageGenerationModel for image-generation capability. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testCreateModelWithImageGenerationCapability(): void | ||
| { | ||
| $modelMetadata = new ModelMetadata( | ||
| 'dall-e-3', | ||
| 'dall-e-3', | ||
| [CapabilityEnum::imageGeneration()], | ||
| [] | ||
| ); | ||
|
|
||
| $model = $this->createModelViaProvider($modelMetadata); | ||
|
|
||
| $this->assertInstanceOf(OpenAiImageGenerationModel::class, $model); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that createModel creates an OpenAiEmbeddingGenerationModel for embedding capability. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testCreateModelWithEmbeddingGenerationCapability(): void | ||
| { | ||
| if (!interface_exists(EmbeddingGenerationModelInterface::class)) { | ||
| $this->markTestSkipped('Embedding generation requires PHP AI Client 1.4.0 or later.'); | ||
| } | ||
|
|
||
| $modelMetadata = new ModelMetadata( | ||
| 'text-embedding-3-small', | ||
| 'text-embedding-3-small', | ||
| [CapabilityEnum::embeddingGeneration()], | ||
| [] | ||
| ); | ||
|
|
||
| $model = $this->createModelViaProvider($modelMetadata); | ||
|
|
||
| $this->assertInstanceOf(OpenAiEmbeddingGenerationModel::class, $model); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that createModel creates an OpenAiTextToSpeechConversionModel for text-to-speech capability. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testCreateModelWithTextToSpeechConversionCapability(): void | ||
| { | ||
| $modelMetadata = new ModelMetadata( | ||
| 'tts-1', | ||
| 'tts-1', | ||
| [CapabilityEnum::textToSpeechConversion()], | ||
| [] | ||
| ); | ||
|
|
||
| $model = $this->createModelViaProvider($modelMetadata); | ||
|
|
||
| $this->assertInstanceOf(OpenAiTextToSpeechConversionModel::class, $model); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that createModel skips unhandled capabilities before reaching a supported one. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testCreateModelSkipsUnhandledCapabilities(): void | ||
| { | ||
| $modelMetadata = new ModelMetadata( | ||
| 'gpt-4o', | ||
| 'gpt-4o', | ||
| [CapabilityEnum::chatHistory(), CapabilityEnum::textGeneration()], | ||
| [] | ||
| ); | ||
|
|
||
| $model = $this->createModelViaProvider($modelMetadata); | ||
|
|
||
| $this->assertInstanceOf(OpenAiTextGenerationModel::class, $model); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that createModel instantiates the first matching capability when multiple supported capabilities exist. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testCreateModelSelectsFirstMatchingCapability(): void | ||
| { | ||
| $modelMetadata = new ModelMetadata( | ||
| 'hybrid-model', | ||
| 'hybrid-model', | ||
| [CapabilityEnum::imageGeneration(), CapabilityEnum::textGeneration()], | ||
| [] | ||
| ); | ||
|
|
||
| $model = $this->createModelViaProvider($modelMetadata); | ||
|
|
||
| $this->assertInstanceOf(OpenAiImageGenerationModel::class, $model); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that createModel throws RuntimeException when unsupported capabilities are provided. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testCreateModelThrowsRuntimeExceptionForUnsupportedCapabilities(): void | ||
| { | ||
| $modelMetadata = new ModelMetadata( | ||
| 'unsupported-model', | ||
| 'unsupported-model', | ||
| [CapabilityEnum::chatHistory()], | ||
|
saarnilauri marked this conversation as resolved.
Outdated
|
||
| [] | ||
| ); | ||
|
|
||
| $this->expectException(RuntimeException::class); | ||
| $this->expectExceptionMessage('Unsupported model capabilities: chat_history'); | ||
|
|
||
| $this->createModelViaProvider($modelMetadata); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that createModel throws RuntimeException when capabilities list is empty. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testCreateModelThrowsRuntimeExceptionForEmptyCapabilities(): void | ||
| { | ||
| $modelMetadata = new ModelMetadata( | ||
| 'empty-model', | ||
| 'empty-model', | ||
| [], | ||
| [] | ||
| ); | ||
|
|
||
| $this->expectException(RuntimeException::class); | ||
| $this->expectExceptionMessage('Unsupported model capabilities: '); | ||
|
|
||
| $this->createModelViaProvider($modelMetadata); | ||
| } | ||
|
|
||
| /** | ||
| * Helper to invoke protected static createModel on OpenAiProvider. | ||
| * | ||
| * @param ModelMetadata $modelMetadata The model metadata. | ||
| * @return ModelInterface The instantiated model. | ||
| */ | ||
| private function createModelViaProvider(ModelMetadata $modelMetadata): ModelInterface | ||
| { | ||
| $provider = new class extends OpenAiProvider { | ||
| public static function exposeCreateModel( | ||
| ModelMetadata $modelMetadata, | ||
| ProviderMetadata $providerMetadata | ||
| ): ModelInterface { | ||
| return static::createModel($modelMetadata, $providerMetadata); | ||
| } | ||
| }; | ||
|
|
||
| return $provider::exposeCreateModel($modelMetadata, OpenAiProvider::metadata()); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.