Files
fewo-stommer/tests/Feature/TenantSettingsTest.php
T
LarsandClaude Opus 5.5 4170e3bd29 v0.11 U6: risikobasierte KI-Extraktion von Gast- und Portalnachrichten
- Kleines Modell (claude-haiku-5-5) extrahiert Fakten mit Belegstelle und
  Konfidenz; programmatische Prüfung (Gültigkeit, Belegstelle im Original,
  Widerspruch zum gespeicherten Wert oder in der Nachricht, Plausibilität,
  Kritikalität nach Feld und Phase). Widerspruch oder fehlgeschlagene
  Prüfung → Prüfmodell (claude-opus-5-5, serverseitiger Fallback bei
  Ablehnung) unabhängig von der Konfidenz; solche Fakten nie automatisch.
- Nur geprüfte, bisher fehlende Angaben gehen an den Fakteneingang (Quelle
  „llm“); Zeitraum/Personen nur als Änderungswunsch (M06); Rest als
  Vorschlag im Posteingang („Vorschläge übernehmen“).
- Versionierte Prompts mit Platzhaltern, JSON-Schema mit eigener
  Validierung und einer Wiederholung; Ausfall/Ablehnung → sicherer
  Rückfall zur Vermieterin. HTTP-Client, kein SDK (laut Plan).
- Dienst „KI: Gästekommunikation“ verdrahtet, standardmäßig aus;
  Schlüssel je Mandant oder FEWO_AI_API_KEY; Verbrauch in service_usages.
- messages:extract alle fünf Minuten; ai:evaluate misst Fehlwerte und
  übersehene Angaben getrennt (M05).

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

192 lines
8.2 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\Enums\TenantServiceKey;
use App\Filament\Operator\Tenants\Pages\CreateTenant;
use App\Filament\Operator\Tenants\Pages\EditTenant;
use App\Models\Tenant;
use App\Models\User;
use App\Support\Content;
use App\Support\Settings;
use App\Tenancy\TenantConfig;
use App\Tenancy\TenantContext;
use App\Tenancy\TenantFeatures;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
/**
* Phase 3: operating data and texts belong to the landlord, not to the installation.
*/
class TenantSettingsTest extends TestCase
{
use RefreshDatabase;
public function test_each_landlord_has_its_own_imprint_bank_and_operating_mode(): void
{
config(['fewo.mail_redirect_to' => null]);
$first = Tenant::factory()->create([
'bank_iban' => 'DE02120300000000202051',
'operating_mode' => Settings::MODE_LIVE,
'owner_name' => 'Brigitte Beispiel',
]);
$second = Tenant::factory()->create([
'bank_iban' => 'DE02500105170137075030',
'operating_mode' => Settings::MODE_TEST,
'test_recipient' => 'test@zweiter.example',
'owner_name' => 'Zweiter Vermieter',
]);
$context = app(TenantContext::class);
$context->set($first);
Content::save(['legal.imprint' => 'Impressum des ersten Vermieters']);
$this->assertSame('Impressum des ersten Vermieters', Content::get('legal.imprint'));
$this->assertSame('DE02120300000000202051', config('fewo.bank.iban'));
$this->assertSame('Brigitte Beispiel', config('fewo.owner.name'));
$this->assertTrue(Settings::isLive());
$this->assertNull(Settings::mailRedirect());
$context->set($second);
$this->assertNull(Content::get('legal.imprint'));
Content::save(['legal.imprint' => 'Impressum des zweiten Vermieters']);
$this->assertSame('DE02500105170137075030', config('fewo.bank.iban'));
$this->assertSame('Zweiter Vermieter', config('fewo.owner.name'));
$this->assertFalse(Settings::isLive());
$this->assertSame('test@zweiter.example', Settings::mailRedirect());
$context->set($first);
$this->assertSame('Impressum des ersten Vermieters', Content::get('legal.imprint'));
}
public function test_without_a_landlord_the_configuration_of_the_installation_applies(): void
{
config(['fewo.owner.name' => 'Aus der Konfiguration']);
$first = Tenant::factory()->create(['owner_name' => 'Brigitte Beispiel']);
Tenant::factory()->create();
$context = app(TenantContext::class);
$context->set($first);
$this->assertSame('Brigitte Beispiel', config('fewo.owner.name'));
$context->forget();
$this->assertSame('Aus der Konfiguration', config('fewo.owner.name'));
$this->assertNull(Settings::tenant());
}
public function test_only_services_the_application_really_asks_about_are_offered(): void
{
$this->assertSame(
[TenantServiceKey::PortalSync, TenantServiceKey::PortalMail, TenantServiceKey::AiGuestMail],
TenantServiceKey::wired(),
);
// v0.11: the guest message extraction sends data to a provider – only after the operator switched it on.
$this->assertFalse(TenantServiceKey::AiGuestMail->enabledByDefault());
foreach (TenantServiceKey::cases() as $service) {
if (! $service->isWired()) {
$this->assertFalse($service->enabledByDefault(), $service->value.' darf nicht vorgegeben an sein, solange nichts danach fragt.');
}
}
}
public function test_a_landlord_without_a_decision_keeps_the_portal_services(): void
{
$tenant = Tenant::factory()->create();
$this->assertSame(0, $tenant->services()->count());
$this->assertTrue(app(TenantFeatures::class)->enabled(TenantServiceKey::PortalSync, $tenant));
$this->assertFalse(app(TenantFeatures::class)->enabled(TenantServiceKey::AiOffers, $tenant));
}
public function test_the_operator_creates_a_landlord_in_the_operator_panel(): void
{
$existing = Tenant::factory()->create();
app(TenantContext::class)->set($existing);
$operator = User::factory()->create();
$operator->forceFill(['is_operator' => true])->saveQuietly();
$this->actingAs($operator);
Filament::setCurrentPanel('operator');
$this->get('/betreiber/tenants')->assertOk()->assertSee('Neuen Mandanten anlegen');
$this->get('/betreiber/tenants/create')->assertOk();
Livewire::test(CreateTenant::class)
->fillForm([
'name' => 'Ferienhaus Düne',
'slug' => 'duene',
'is_active' => true,
'websites' => [['name' => 'Ferienhaus Düne', 'hosts' => ['duene.example']]],
'services.'.TenantServiceKey::PortalSync->value.'.is_enabled' => true,
'services.'.TenantServiceKey::PortalMail->value.'.is_enabled' => false,
])
->call('create')
->assertHasNoErrors();
$created = Tenant::query()->where('slug', 'duene')->sole();
$this->assertSame('Ferienhaus Düne', $created->name);
$this->assertTrue(app(TenantFeatures::class)->enabled(TenantServiceKey::PortalSync, $created));
$this->assertFalse(app(TenantFeatures::class)->enabled(TenantServiceKey::PortalMail, $created));
// A new landlord must not inherit anything from the one the operator happened to be looking at.
$this->assertNull($created->contact_email);
$this->assertSame(Settings::MODE_TEST, $created->operating_mode);
}
public function test_the_operator_switches_a_service_on_the_tenant_form(): void
{
$tenant = Tenant::factory()->create();
app(TenantContext::class)->set($tenant);
$operator = User::factory()->create();
$operator->forceFill(['is_operator' => true])->saveQuietly();
$this->actingAs($operator);
Filament::setCurrentPanel('operator');
Livewire::test(EditTenant::class, ['record' => $tenant->getRouteKey()])
->fillForm([
'services.'.TenantServiceKey::PortalSync->value.'.is_enabled' => false,
'services.'.TenantServiceKey::PortalMail->value.'.is_enabled' => true,
])
->call('save')
->assertHasNoErrors();
$this->assertFalse($tenant->services()->where('service', TenantServiceKey::PortalSync->value)->sole()->is_enabled);
$this->assertTrue($tenant->services()->where('service', TenantServiceKey::PortalMail->value)->sole()->is_enabled);
$this->assertSame(0, $tenant->services()->where('service', TenantServiceKey::AiOffers->value)->count());
$this->assertFalse(app(TenantFeatures::class)->enabled(TenantServiceKey::PortalSync, $tenant));
}
/**
* The data a new landlord starts with comes from the installation, never from the landlord that happens
* to be current – otherwise the migration would copy one landlord's address into the next one.
*/
public function test_the_defaults_of_a_new_landlord_come_from_the_configuration_not_from_the_current_one(): void
{
config(['fewo.owner.name' => 'Aus der Konfiguration', 'fewo.owner.email' => 'konfiguration@example.com']);
$current = Tenant::factory()->create(['owner_name' => 'Erster Vermieter', 'contact_email' => 'erster@example.com']);
app(TenantContext::class)->set($current);
$this->assertSame('Erster Vermieter', config('fewo.owner.name'));
$next = Tenant::factory()->create();
$this->assertSame('Aus der Konfiguration', $next->owner_name);
$this->assertSame('konfiguration@example.com', $next->contact_email);
}
public function test_the_landlord_of_the_context_decides_the_sender(): void
{
$tenant = Tenant::factory()->create(['sender_email' => 'versand@vermieter.example', 'sender_name' => 'Vermieter']);
app(TenantContext::class)->set($tenant);
app(TenantConfig::class)->refresh();
$this->assertSame(['versand@vermieter.example', 'Vermieter'], [config('mail.from.address'), config('mail.from.name')]);
}
}