Core Features: - Multi-provider support (OpenAI, Anthropic, DeepSeek, Gemini, Mistral) - Provider service architecture with abstract base class - Dynamic model discovery from provider APIs - Encrypted per-user provider credentials storage Admin Interface: - Complete admin panel with Livewire components - User management with CRUD operations - API key management with testing capabilities - Budget system with limits and reset schedules - Usage logs with filtering and CSV export - Model pricing management with cost calculator - Dashboard with Chart.js visualizations Database Schema: - MariaDB migrations for all tables - User provider credentials (encrypted) - LLM request logging - Budget tracking and rate limiting - Model pricing configuration API Implementation: - OpenAI-compatible endpoints - Budget checking middleware - Rate limit enforcement - Request logging jobs - Cost calculation service Testing: - Unit tests for all provider services - Provider factory tests - Cost calculator tests Documentation: - Admin user seeder - Model pricing seeder - Configuration files
129 lines
4.1 KiB
PHP
129 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Services;
|
|
|
|
use Tests\TestCase;
|
|
use App\Services\LLM\Providers\MistralProvider;
|
|
use App\Models\ModelPricing;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
class MistralProviderTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private MistralProvider $provider;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->provider = new MistralProvider('test-api-key');
|
|
}
|
|
|
|
public function test_builds_request_correctly(): void
|
|
{
|
|
$messages = [
|
|
['role' => 'user', 'content' => 'Hello']
|
|
];
|
|
|
|
$options = [
|
|
'model' => 'mistral-small-latest',
|
|
'temperature' => 0.8,
|
|
'max_tokens' => 1000
|
|
];
|
|
|
|
$reflection = new \ReflectionClass($this->provider);
|
|
$method = $reflection->getMethod('buildRequest');
|
|
$method->setAccessible(true);
|
|
|
|
$result = $method->invoke($this->provider, $messages, $options);
|
|
|
|
$this->assertEquals('mistral-small-latest', $result['model']);
|
|
$this->assertEquals(0.8, $result['temperature']);
|
|
$this->assertEquals(1000, $result['max_tokens']);
|
|
$this->assertEquals($messages, $result['messages']);
|
|
$this->assertArrayNotHasKey('stream', $result); // stream=false is filtered out
|
|
}
|
|
|
|
public function test_normalizes_response_correctly(): void
|
|
{
|
|
$rawResponse = [
|
|
'id' => 'cmpl-123',
|
|
'model' => 'mistral-small-latest',
|
|
'choices' => [
|
|
[
|
|
'message' => [
|
|
'role' => 'assistant',
|
|
'content' => 'Bonjour! Comment puis-je vous aider?'
|
|
],
|
|
'finish_reason' => 'stop'
|
|
]
|
|
],
|
|
'usage' => [
|
|
'prompt_tokens' => 12,
|
|
'completion_tokens' => 18,
|
|
'total_tokens' => 30
|
|
]
|
|
];
|
|
|
|
$normalized = $this->provider->normalizeResponse($rawResponse);
|
|
|
|
$this->assertEquals('cmpl-123', $normalized['id']);
|
|
$this->assertEquals('mistral-small-latest', $normalized['model']);
|
|
$this->assertEquals('Bonjour! Comment puis-je vous aider?', $normalized['content']);
|
|
$this->assertEquals('assistant', $normalized['role']);
|
|
$this->assertEquals('stop', $normalized['finish_reason']);
|
|
$this->assertEquals(12, $normalized['usage']['prompt_tokens']);
|
|
$this->assertEquals(18, $normalized['usage']['completion_tokens']);
|
|
$this->assertEquals(30, $normalized['usage']['total_tokens']);
|
|
}
|
|
|
|
public function test_calculates_cost_correctly(): void
|
|
{
|
|
ModelPricing::updateOrCreate(
|
|
[
|
|
'provider' => 'mistral',
|
|
'model' => 'mistral-small-latest',
|
|
'effective_from' => now()->toDateString(),
|
|
],
|
|
[
|
|
'input_price_per_million' => 0.20,
|
|
'output_price_per_million' => 0.60,
|
|
'is_active' => true,
|
|
]
|
|
);
|
|
|
|
Cache::flush();
|
|
|
|
$cost = $this->provider->calculateCost(1000, 500, 'mistral-small-latest');
|
|
|
|
// Expected: (1000/1M * 0.20) + (500/1M * 0.60) = 0.0002 + 0.0003 = 0.0005
|
|
$this->assertEquals(0.0005, $cost);
|
|
}
|
|
|
|
public function test_handles_api_errors(): void
|
|
{
|
|
Http::fake([
|
|
'https://api.mistral.ai/*' => Http::response(['error' => 'Invalid API key'], 401)
|
|
]);
|
|
|
|
$this->expectException(\App\Exceptions\ProviderException::class);
|
|
$this->expectExceptionMessage('Invalid API key');
|
|
|
|
$this->provider->chatCompletion([
|
|
['role' => 'user', 'content' => 'test']
|
|
]);
|
|
}
|
|
|
|
public function test_get_supported_models(): void
|
|
{
|
|
$models = $this->provider->getSupportedModels();
|
|
|
|
$this->assertIsArray($models);
|
|
$this->assertContains('mistral-large-latest', $models);
|
|
$this->assertContains('mistral-small-latest', $models);
|
|
$this->assertContains('open-mixtral-8x7b', $models);
|
|
}
|
|
}
|