ai()->save(['enabled' => true, 'provider' => 'openai', 'base_url' => null, 'model' => 'gpt-test', 'api_key' => 'sk-openai']); Http::fake(['https://api.openai.com/v1/chat/completions' => Http::response(['choices' => [['message' => ['content' => '

Einfach.

']]]])]); $this->assertSame('

Einfach.

', $this->ai()->easyLanguage('Titel', 'Ein schwieriger Text.')); Http::assertSent(fn ($r) => $r->hasHeader('Authorization', 'Bearer sk-openai') && $r['model'] === 'gpt-test' && $r['messages'][0]['role'] === 'system'); } public function test_ollama_runs_without_key_on_internal_server_and_strips_thinking(): void { $this->ai()->save(['enabled' => true, 'provider' => 'ollama', 'base_url' => 'http://10.0.0.5:11434/', 'model' => 'llama3.1:8b', 'api_key' => null]); $this->assertTrue($this->ai()->available()); $this->assertFalse($this->ai()->isExternal()); Http::fake(['http://10.0.0.5:11434/api/chat' => Http::response(['message' => ['content' => "hmm\n

Kurz.

"]])]); $this->assertSame('

Kurz.

', $this->ai()->easyLanguage('Titel', 'Text')); Http::assertSent(fn ($r) => $r['stream'] === false && $r['model'] === 'llama3.1:8b' && ! $r->hasHeader('Authorization')); } public function test_missing_ollama_model_gives_a_helpful_message(): void { $this->ai()->save(['enabled' => true, 'provider' => 'ollama', 'base_url' => 'http://ki-server:11434', 'model' => 'mistral', 'api_key' => null]); Http::fake(['*' => Http::response(['error' => 'model "mistral" not found'], 404)]); $this->expectExceptionMessage('ollama pull mistral'); $this->ai()->test(); } public function test_unsafe_addresses_are_rejected(): void { SafeOutbound::fakeResolve(['intern.example' => '192.168.1.10']); foreach ([ ['openai', 'http://api.example.com/v1'], ['openai', 'https://intern.example/v1'], ['ollama', 'http://169.254.169.254'], ['ollama', 'file:///etc/passwd'], ] as [$provider, $url]) { try { $this->ai()->save(['enabled' => true, 'provider' => $provider, 'base_url' => $url, 'model' => 'x', 'api_key' => 'k']); $this->fail("{$provider} {$url} hätte abgelehnt werden müssen"); } catch (ValidationException $e) { $this->assertArrayHasKey('data.base_url', $e->errors()); } } } public function test_stored_key_is_not_sent_to_a_new_destination(): void { $this->ai()->save(['enabled' => true, 'provider' => 'anthropic', 'base_url' => null, 'model' => null, 'api_key' => 'sk-ant']); SafeOutbound::fakeResolve(['api.fremd.example' => '93.184.216.34']); try { $this->ai()->save(['enabled' => true, 'provider' => 'openai', 'base_url' => 'https://api.fremd.example/v1', 'model' => 'm', 'api_key' => null]); $this->fail('Schlüssel hätte neu eingegeben werden müssen'); } catch (ValidationException $e) { $this->assertArrayHasKey('data.api_key', $e->errors()); } // Nur Modell ändern: Schlüssel bleibt $this->ai()->save(['enabled' => true, 'provider' => 'anthropic', 'base_url' => null, 'model' => 'claude-anderes', 'api_key' => null]); $this->assertNotNull(app(Settings::class)->get('ai.api_key')); } }