provider = new DeepSeekProvider('test-api-key'); } public function test_builds_request_correctly(): void { $messages = [ ['role' => 'user', 'content' => 'Write a function'] ]; $options = [ 'model' => 'deepseek-coder', 'temperature' => 0.5, 'max_tokens' => 1500 ]; $reflection = new \ReflectionClass($this->provider); $method = $reflection->getMethod('buildRequest'); $method->setAccessible(true); $result = $method->invoke($this->provider, $messages, $options); $this->assertEquals('deepseek-coder', $result['model']); $this->assertEquals(0.5, $result['temperature']); $this->assertEquals(1500, $result['max_tokens']); $this->assertEquals($messages, $result['messages']); $this->assertFalse($result['stream']); } public function test_normalizes_response_correctly(): void { $rawResponse = [ 'id' => 'deepseek-123', 'model' => 'deepseek-coder', 'choices' => [ [ 'message' => [ 'role' => 'assistant', 'content' => 'def hello_world():\n print("Hello, World!")' ], 'finish_reason' => 'stop' ] ], 'usage' => [ 'prompt_tokens' => 8, 'completion_tokens' => 22, 'total_tokens' => 30 ] ]; $normalized = $this->provider->normalizeResponse($rawResponse); $this->assertEquals('deepseek-123', $normalized['id']); $this->assertEquals('deepseek-coder', $normalized['model']); $this->assertStringContainsString('def hello_world()', $normalized['content']); $this->assertEquals('assistant', $normalized['role']); $this->assertEquals('stop', $normalized['finish_reason']); $this->assertEquals(8, $normalized['usage']['prompt_tokens']); $this->assertEquals(22, $normalized['usage']['completion_tokens']); $this->assertEquals(30, $normalized['usage']['total_tokens']); } public function test_calculates_cost_correctly(): void { ModelPricing::updateOrCreate( [ 'provider' => 'deepseek', 'model' => 'deepseek-chat', 'effective_from' => now()->toDateString(), ], [ 'input_price_per_million' => 0.14, 'output_price_per_million' => 0.28, 'is_active' => true, ] ); Cache::flush(); $cost = $this->provider->calculateCost(1000, 500, 'deepseek-chat'); // Expected: (1000/1M * 0.14) + (500/1M * 0.28) = 0.00014 + 0.00014 = 0.00028 $this->assertEquals(0.00028, $cost); } public function test_handles_api_errors(): void { Http::fake([ 'https://api.deepseek.com/*' => 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('deepseek-chat', $models); $this->assertContains('deepseek-coder', $models); $this->assertContains('deepseek-reasoner', $models); } }