Files
fewo-stommer/tests/Feature/AiAccessTest.php
T
LarsandClaude Opus 5.5 6862dec201
Deploy FeWo mit PipelineManager / deploy (push) Successful in 5m11s
v0.11 U6: Bring your own key für die KI-Auswertung (Anthropic, OpenRouter)
- Jeder Mandant verbindet unter „System › KI-Anbindung“ seinen eigenen
  Zugang: Anbieter (Anthropic oder OpenRouter), API-Schlüssel, Modell für
  die erste Auswertung und für die Prüfung – keine Modellkennung im Code,
  kein zentraler Betreiberschlüssel (FEWO_AI_API_KEY entfällt).
- Gespeichert im bestehenden TenantService (ai_guest_mail): Schlüssel
  verschlüsselt, nie wieder angezeigt, nie in Meldungen; Anbieter/Modelle
  in settings, Prüfergebnis in check_status/check_message/checked_at.
- Feste Endpunkte je Anbieter (AiProvider), nicht konfigurierbar.
  OpenRouter mit response_format json_schema (strict) und
  provider.require_parameters.
- „Verbindung prüfen“ testet beide Modelle. Ein abgelehnter Zugang
  (401/402/403) schaltet die Auswertung für den Mandanten ab, meldet es im
  Postfach der Verwaltung; Nachrichten bleiben zur Prüfung im Posteingang.
- Zweistufige Extraktion und ihre Sicherheitsregeln unverändert.
- Zielarchitektur §3.8, Umsetzungsplan U6, Bericht, Drehbuch, Status.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 14:19:13 +01:00

184 lines
9.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Tests\Feature;
use App\Actions\CreateReservation;
use App\Actions\ExtractMessageFacts;
use App\Actions\ReceiveInboundMessage;
use App\Channels\Mail\PortalMail;
use App\Communication\Extraction\AiAccess;
use App\Enums\TenantServiceKey;
use App\Filament\Pages\AiConnection;
use App\Models\InboundMessage;
use App\Models\InboxMessage;
use App\Models\Property;
use App\Models\TenantService;
use App\Models\User;
use App\Pricing\StayRequest;
use App\Support\Settings;
use App\Tenancy\TenantFeatures;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\SiteSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Livewire\Livewire;
use Tests\TestCase;
/**
* v0.11, bring your own key for the extraction: each landlord configures provider (Anthropic or OpenRouter), key and
* both models himself; there is no operator key, no built-in model and no free endpoint. The key is stored encrypted,
* never shown again and never logged; a rejected access switches the extraction off and leaves messages to the
* landlord.
*/
class AiAccessTest extends TestCase
{
use RefreshDatabase;
private const EXTRACTION = ['facts' => [], 'change_requests' => [], 'intent' => 'answer', 'uncertainties' => []];
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed([PropertySeeder::class, SiteSeeder::class]);
Mail::fake();
TenantService::query()->create(['tenant_id' => Settings::tenant()->id, 'service' => TenantServiceKey::AiGuestMail->value, 'is_enabled' => true]);
app(TenantFeatures::class)->forget();
}
public function test_without_the_landlords_own_complete_access_nothing_is_sent(): void
{
Http::fake();
$this->assertSame('disabled', app(ExtractMessageFacts::class)->handle($this->message()));
$this->access(['provider' => 'openrouter', 'small_model' => 'vendor/small', 'review_model' => ''], key: 'sk-or-1');
$this->assertSame('disabled', app(ExtractMessageFacts::class)->handle($this->message('<2@x>', 'zwei@example.org', '2026-11-20', '2026-11-23')), 'both models are required');
Http::assertNothingSent();
}
public function test_openrouter_is_called_at_its_fixed_endpoint_with_the_landlords_key_and_a_strict_schema(): void
{
$this->access(['provider' => 'openrouter', 'small_model' => 'vendor/small', 'review_model' => 'vendor/large'], key: 'sk-or-v1-geheim');
Http::fake(['https://openrouter.ai/api/v1/chat/completions' => Http::response(['choices' => [['finish_reason' => 'stop', 'message' => ['content' => json_encode(self::EXTRACTION)]]], 'model' => 'vendor/small'])]);
$this->assertSame('done', app(ExtractMessageFacts::class)->handle($this->message()));
Http::assertSent(function (Request $request): bool {
$body = $request->data();
return $request->url() === 'https://openrouter.ai/api/v1/chat/completions'
&& $request->header('Authorization')[0] === 'Bearer sk-or-v1-geheim'
&& $body['model'] === 'vendor/small'
&& $body['response_format']['type'] === 'json_schema'
&& $body['response_format']['json_schema']['strict'] === true
&& $body['provider']['require_parameters'] === true
&& $body['messages'][0]['role'] === 'system';
});
}
public function test_a_rejected_key_switches_the_extraction_off_and_tells_the_landlord(): void
{
$this->access(['provider' => 'openrouter', 'small_model' => 'vendor/small', 'review_model' => 'vendor/large'], key: 'sk-or-v1-abgelaufen');
Http::fake(['*' => Http::response(['error' => ['code' => 401, 'message' => 'Invalid key sk-or-v1-abgelaufen']], 401)]);
$first = $this->message();
$this->assertSame('failed', app(ExtractMessageFacts::class)->handle($first));
Http::assertSentCount(1);
$service = AiAccess::service(Settings::tenant());
$this->assertSame(AiAccess::INVALID, $service->check_status);
$this->assertStringNotContainsString('abgelaufen', (string) $service->check_message, 'the key is never stored in a message');
$this->assertStringNotContainsString('abgelaufen', (string) $first->fresh()->extraction['error']);
$this->assertSame(1, InboxMessage::query()->where('kind', 'ai_access_rejected')->count());
$this->assertSame('disabled', app(ExtractMessageFacts::class)->handle($this->message('<2@x>', 'zwei@example.org', '2026-11-20', '2026-11-23')));
Http::assertSentCount(1);
}
public function test_the_landlord_configures_the_access_the_key_is_encrypted_and_never_shown(): void
{
$this->actingAs(User::factory()->create());
Livewire::test(AiConnection::class)
->fillForm(['provider' => 'anthropic', 'api_key' => 'sk-ant-geheim', 'small_model' => 'claude-haiku-5-5', 'review_model' => 'claude-opus-5-5'])
->call('save')->assertHasNoFormErrors();
$service = AiAccess::service(Settings::tenant());
$this->assertSame('sk-ant-geheim', $service->api_key);
$this->assertStringNotContainsString('sk-ant-geheim', (string) DB::table('tenant_services')->where('id', $service->id)->value('api_key'), 'encrypted at rest');
$this->assertArrayNotHasKey('api_key', $service->toArray());
Livewire::test(AiConnection::class)
->assertDontSee('sk-ant-geheim')
->assertSee('gespeichert – leer lassen')
->fillForm(['provider' => 'anthropic', 'api_key' => '', 'small_model' => 'claude-haiku-5-5', 'review_model' => 'claude-sonnet-5-5'])
->call('save');
$this->assertSame(['sk-ant-geheim', 'claude-sonnet-5-5'], [$service->fresh()->api_key, $service->fresh()->settings['review_model']]);
}
public function test_the_check_tries_both_models_and_records_the_result(): void
{
$this->actingAs(User::factory()->create());
$this->access(['provider' => 'anthropic', 'small_model' => 'claude-haiku-5-5', 'review_model' => 'claude-opus-5-5'], key: 'sk-ant-1');
$broken = false;
Http::fake(['https://api.anthropic.com/v1/messages' => function () use (&$broken) {
return $broken
? Http::response(['error' => ['type' => 'not_found_error', 'message' => 'model: claude-falsch']], 404)
: Http::response(['content' => [['type' => 'text', 'text' => '{"ok":true}']], 'stop_reason' => 'end_turn', 'model' => 'x']);
}]);
Livewire::test(AiConnection::class)->call('check')->assertNotified('Verbindung in Ordnung');
Http::assertSentCount(2);
$this->assertSame(AiAccess::OK, AiAccess::service(Settings::tenant())->check_status);
$broken = true;
AiAccess::service(Settings::tenant())->update(['settings' => ['provider' => 'anthropic', 'small_model' => 'claude-falsch', 'review_model' => 'claude-opus-5-5']]);
Livewire::test(AiConnection::class)->call('check')->assertNotified('Prüfung fehlgeschlagen');
$this->assertSame(AiAccess::INVALID, AiAccess::service(Settings::tenant())->check_status);
}
public function test_the_page_exists_only_when_the_operator_switched_the_service_on(): void
{
$this->actingAs(User::factory()->create());
TenantService::query()->update(['is_enabled' => false]);
app(TenantFeatures::class)->forget();
$this->assertFalse(AiConnection::canAccess());
}
public function test_endpoints_cannot_be_redirected_by_configuration(): void
{
config(['fewo.ai.endpoint' => 'https://evil.example/v1']);
$this->access(['provider' => 'anthropic', 'small_model' => 'claude-haiku-5-5', 'review_model' => 'claude-opus-5-5'], key: 'sk-ant-1');
Http::fake(['*' => Http::response(['content' => [['type' => 'text', 'text' => json_encode(self::EXTRACTION)]], 'stop_reason' => 'end_turn'])]);
app(ExtractMessageFacts::class)->handle($this->message());
Http::assertSent(fn (Request $request): bool => $request->url() === 'https://api.anthropic.com/v1/messages');
Http::assertNotSent(fn (Request $request): bool => str_contains($request->url(), 'evil.example'));
}
/**
* @param array<string, string> $settings
*/
private function access(array $settings, string $key): void
{
TenantService::query()->update(['settings' => json_encode($settings), 'api_key' => encrypt($key, false)]);
}
private function message(string $id = '<1@x>', string $email = 'gesa@example.org', string $arrival = '2026-11-07', string $departure = '2026-11-10'): InboundMessage
{
$booking = app(CreateReservation::class)->handle(Property::sole(), new StayRequest(CarbonImmutable::parse($arrival), CarbonImmutable::parse($departure), 2), ['first_name' => 'Gesa', 'last_name' => 'Gast', 'email' => $email]);
return app(ReceiveInboundMessage::class)->handle(new PortalMail($id, $email, 'Re: '.$booking->reference, 'Strandweg 1, 18225 Kühlungsborn', CarbonImmutable::now()))->fresh();
}
}