Loheide-eu-mitarbeiterplatt.../tests/Feature/Accessibility/AiProvidersTest.php
Friederich Loheide 268742983a
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m17s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m28s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 6m32s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 6m48s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 8m51s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 8m52s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 7m31s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 52s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 7m51s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m22s
KI-Assistent: OpenAI und Ollama als Anbieter (0.35.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 13:57:37 +00:00

87 lines
3.9 KiB
PHP

<?php
namespace Tests\Feature\Accessibility;
use App\Ai\AiAssistant;
use App\Support\SafeOutbound;
use App\Support\Settings;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Validation\ValidationException;
use Tests\TestCase;
class AiProvidersTest extends TestCase
{
use RefreshDatabase;
private function ai(): AiAssistant
{
return app(AiAssistant::class);
}
public function test_openai_uses_chat_completions_with_bearer_token(): void
{
$this->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' => '<p>Einfach.</p>']]]])]);
$this->assertSame('<p>Einfach.</p>', $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' => "<think>hmm</think>\n<p>Kurz.</p>"]])]);
$this->assertSame('<p>Kurz.</p>', $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'));
}
}