Loheide-eu-mitarbeiterplatt.../tests/Feature/Ask/AskPortalTest.php
Friederich Loheide 3addc09c38
All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Has been skipped
CI / PHP 8.4 / mariadb:10.6 (pull_request) Has been skipped
CI / PHP 8.3 / mariadb:11.4 (pull_request) Has been skipped
CI / PHP 8.4 / mariadb:11.4 (pull_request) Has been skipped
CI / PHP 8.3 / mysql:8.0 (pull_request) Has been skipped
CI / PHP 8.4 / mysql:8.0 (pull_request) Has been skipped
CI / PHP 8.3 / mysql:8.4 (pull_request) Has been skipped
CI / PHP 8.4 / mysql:8.4 (pull_request) Has been skipped
CI / Browser-Tests (pull_request) Successful in 8m33s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 9m8s
CI / PHP 8.3 / mysql:8.4-1 (pull_request) Successful in 17m55s
CI / Build-Artefakte aktuell (pull_request) Successful in 9m50s
Frag das Portal durchsucht alles, neue Demo-Inhalte (0.66.0)
- Wissensquellen: Termine, Downloads, Formulare, Stellen, Ideen, Umfragen,
  Personen, eigene IT-Tickets/Geräte (OpenNIT, je Anfrage zwischengespeichert)
- Ranking mit IDF, Abfrage je Begriff, Abkürzungen als ganze Wörter,
  LIKE unabhängig von Groß-/Kleinschreibung (JSON-Spalten), Datum im Prompt
- Demo-Inhalte für einen Träger der Eingliederungshilfe (DemoData),
  mehrseitige PDFs, vorhandene Inhalte je Bereich entfernen

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-07 20:01:00 +00:00

218 lines
12 KiB
PHP
Raw Permalink 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\Ask;
use App\Ai\AiAssistant;
use App\Ask\AskPortal;
use App\Ask\Keywords;
use App\Models\PortalQuestion;
use App\Models\User;
use App\Pagebuilder\Content;
use App\Support\Permissions\Audiences;
use App\Support\Settings;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Http;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\Events\Models\Event;
use Inhaus\Plugins\Glossar\Models\GlossaryEntry;
use Inhaus\Plugins\News\Models\NewsPost;
use Inhaus\Plugins\Pages\Models\Page;
use Tests\TestCase;
class AskPortalTest extends TestCase
{
use RefreshDatabase;
private User $user;
protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()->create(['department' => 'Werkstatt']);
app(Settings::class)->setMany([
'ai.enabled' => true,
'ai.api_key' => Crypt::encryptString('sk-test'),
'ai.model' => 'claude-sonnet-5',
'ask.enabled' => true,
]);
}
private function page(string $title, string $html): Page
{
$page = Page::query()->create(['title' => $title]);
$content = ['blocks' => [['id' => 'blk'.substr(md5($title), 0, 5), 'type' => 'text', 'data' => ['html' => $html]]]];
$page->publish($title, app(Content::class)->sanitize($content), User::factory()->admin()->create());
return $page->fresh();
}
private function fakeAi(string $reply): void
{
Http::fake([AiAssistant::ENDPOINT => Http::response(['content' => [['type' => 'text', 'text' => $reply]]])]);
}
public function test_keywords_drop_filler_words_and_endings(): void
{
$this->assertSame(['beantrag', 'urlaub'], Keywords::from('Wie beantrage ich meinen Urlaub?'));
$this->assertSame(['mav-wahl', 'mav', 'wahl', '2026'], Keywords::from('Wann ist die MAV-Wahl 2026?'));
$this->assertSame([], Keywords::from('Wie geht das?'));
}
public function test_answer_with_sources_only_from_visible_content(): void
{
$vacation = $this->page('Urlaub beantragen', '<p>Urlaub beantragen Sie bis zum 15. des Vormonats über Vivendi PEP. Die Bereichsleitung genehmigt.</p>');
$care = $this->page('Urlaub in der Pflege', '<p>Geheim: Urlaub in der Pflege nur nach Dienstplanrunde.</p>');
Audiences::sync('page', $care->id, ['department' => ['Pflege']]);
GlossaryEntry::query()->create(['term' => 'Vivendi PEP', 'description' => '<p>Dienstplanung und Urlaubsanträge</p>']);
NewsPost::factory()->create(['title' => 'Kantine im August geschlossen', 'body' => '<p>Nichts zum Thema.</p>']);
$this->fakeAi("Urlaub beantragen Sie bis zum 15. des Vormonats in Vivendi PEP [1].\n- Genehmigt wird von der Bereichsleitung [1].");
$result = $this->actingAs($this->user)->postJson('/frag-das-portal', ['question' => 'Wie beantrage ich Urlaub?'])->assertOk()->json();
$this->assertTrue($result['found']);
$this->assertStringContainsString('Vivendi PEP [1]', $result['answer']);
$this->assertSame([$vacation->url()], array_column($result['sources'], 'url'), 'Nur zitierte Quellen');
Http::assertSent(function (Request $request) {
$prompt = $request['messages'][0]['content'];
return str_contains($prompt, 'bis zum 15. des Vormonats')
&& str_contains($prompt, 'Frage: Wie beantrage ich Urlaub?')
&& ! str_contains($prompt, 'Geheim') // Seite nur für die Pflege
&& ! str_contains($prompt, 'Kantine') // kein Begriff passt
&& str_contains($request['system'], 'NICHTS_GEFUNDEN');
});
// Ohne Personenbezug gespeichert
$logged = PortalQuestion::query()->sole();
$this->assertSame('Wie beantrage ich Urlaub?', $logged->question);
$this->assertTrue($logged->answered);
$this->assertSame(['id', 'question', 'answered', 'source_count', 'asked_on'], array_keys($logged->getAttributes()));
}
public function test_events_people_and_short_abbreviations(): void
{
$event = Event::query()->create([
'title' => 'Sommerfest der Werkstatt', 'description' => 'Grillen und Musik', 'location' => 'Werkstatt am Kanal',
'starts_at' => now()->addDays(10)->setTime(14, 0), 'ends_at' => now()->addDays(10)->setTime(18, 0), 'is_published' => true,
]);
Event::query()->create([
'title' => 'Sommerfest 2024', 'description' => 'Längst vorbei', 'starts_at' => now()->subYears(2), 'is_published' => true,
]);
$admin = User::factory()->create(['first_name' => 'Jens', 'last_name' => 'Hüsing', 'job_title' => 'IT-Administrator', 'department' => 'IT', 'phone' => '05482 230']);
$this->page('Arbeiten mit Kolleginnen', '<p>Wir arbeiten mit Freude miteinander.</p>');
$this->fakeAi('Am '.now()->addDays(10)->format('d.m.').' um 14 Uhr [1].');
$this->actingAs($this->user)->postJson('/frag-das-portal', ['question' => 'Wann ist das Sommerfest?'])->assertOk()->assertJsonPath('sources.0.url', '/termine/'.$event->id);
Http::assertSent(fn (Request $r) => str_contains($r['messages'][0]['content'], 'Werkstatt am Kanal')
&& str_contains($r['messages'][0]['content'], 'Heute ist ')
&& ! str_contains($r['messages'][0]['content'], 'Längst vorbei'));
$this->assertSame(['it', 'zuständig'], Keywords::from('Wer ist für die IT zuständig?'));
$this->fakeAi('Jens Hüsing [1].');
$result = $this->actingAs($this->user)->postJson('/frag-das-portal', ['question' => 'Wer ist für die IT zuständig?'])->json();
$this->assertSame([route('people.show', $admin)], array_column($result['sources'], 'url'));
// „IT“ trifft kein „mit“ oder „arbeiten“
Http::assertSent(fn (Request $r) => str_contains($r['messages'][0]['content'], '05482 230') && ! str_contains($r['messages'][0]['content'], 'miteinander'));
}
public function test_own_it_tickets_are_a_source_for_the_asker_only(): void
{
app(\Inhaus\Plugins\ItService\OpenNit\Settings::class)->save(['url' => 'https://opennit.example.com/api/ex', 'token' => 'tok', 'secret' => 'geheim', 'auto_reserve' => 'default', 'intro' => null]);
Http::fake([
'*/my-tickets*' => Http::response(['tickets' => [['id' => 42, 'number' => 'T-1042', 'subject' => 'Drucker im Büro druckt nicht', 'status' => 'open', 'created_at' => '2026-10-01']]]),
'*/tickets/42*' => Http::response(['ticket' => ['id' => 42, 'subject' => 'Drucker im Büro druckt nicht', 'status' => 'open', 'description' => '<p>Papierstau</p>'], 'replies' => [['message' => 'Toner ist bestellt.', 'is_staff' => true]]]),
'*/my-assets*' => Http::response(['assets' => []]),
AiAssistant::ENDPOINT => Http::response(['content' => [['type' => 'text', 'text' => 'Der Toner ist bestellt [1].']]]),
]);
$result = $this->actingAs($this->user)->postJson('/frag-das-portal', ['question' => 'Was ist mit meinem Drucker-Ticket?'])->assertOk()->json();
$this->assertSame('Mein Ticket: Drucker im Büro druckt nicht', $result['sources'][0]['title']);
Http::assertSent(fn (Request $r) => str_contains($r->url(), 'my-tickets') && str_contains($r->url(), urlencode($this->user->email)));
Http::assertSent(fn (Request $r) => $r->url() === AiAssistant::ENDPOINT && str_contains($r['messages'][0]['content'], 'Toner ist bestellt'));
}
public function test_nothing_found_does_not_call_the_ai(): void
{
Http::fake();
$this->page('Speiseplan', '<p>Montag Nudeln</p>');
$this->actingAs($this->user)->postJson('/frag-das-portal', ['question' => 'Wie funktioniert die Altersvorsorge?'])
->assertOk()->assertJson(['found' => false, 'answer' => AskPortal::NOTHING, 'sources' => []]);
Http::assertNothingSent();
$this->assertFalse(PortalQuestion::query()->sole()->answered);
}
public function test_ai_saying_nothing_fits_offers_suggestions(): void
{
$page = $this->page('Fortbildung', '<p>Fortbildungen stehen im Bildungskatalog.</p>');
$this->fakeAi('NICHTS_GEFUNDEN');
$result = $this->actingAs($this->user)->postJson('/frag-das-portal', ['question' => 'Wer zahlt die Fahrtkosten zur Fortbildung?'])->json();
$this->assertFalse($result['found']);
$this->assertSame(AskPortal::NOTHING, $result['answer']);
$this->assertSame([$page->url()], array_column($result['suggestions'], 'url'));
}
public function test_off_or_without_ai_the_feature_is_hidden(): void
{
app(Settings::class)->set('ask.enabled', false);
$this->actingAs($this->user)->get('/frag-das-portal')->assertNotFound();
$this->actingAs($this->user)->postJson('/frag-das-portal', ['question' => 'Urlaub?'])->assertNotFound();
app(Settings::class)->setMany(['ask.enabled' => true, 'ai.enabled' => false]);
$this->actingAs($this->user)->get('/frag-das-portal')->assertNotFound();
app(Settings::class)->set('ai.enabled', true);
$this->actingAs($this->user)->get('/frag-das-portal?frage=Urlaub')->assertOk()->assertInertia(fn (Assert $page) => $page
->component('Ask/Index')
->where('question', 'Urlaub')
->where('provider', AiAssistant::PROVIDERS['anthropic'])
->where('ask.bubble', true));
}
public function test_bubble_can_be_switched_off(): void
{
$this->actingAs($this->user)->get('/')->assertInertia(fn (Assert $page) => $page->where('ask.bubble', true));
app(Settings::class)->set('ask.bubble', false);
$this->actingAs($this->user)->get('/')->assertInertia(fn (Assert $page) => $page->where('ask.bubble', false));
app(Settings::class)->set('ask.enabled', false);
$this->actingAs($this->user)->get('/')->assertInertia(fn (Assert $page) => $page->where('ask', false));
}
public function test_ai_failure_is_a_friendly_error(): void
{
$this->page('Urlaub', '<p>Urlaub beantragen</p>');
Http::fake([AiAssistant::ENDPOINT => Http::response(['error' => ['message' => 'overloaded']], 529)]);
$this->actingAs($this->user)->postJson('/frag-das-portal', ['question' => 'Urlaub beantragen?'])
->assertStatus(503)->assertJson(['message' => 'Die Antwort konnte gerade nicht erstellt werden. Bitte gleich noch einmal versuchen.']);
}
public function test_excerpt_prefers_passages_with_the_terms(): void
{
$text = str_repeat('Allgemeines zur Einrichtung. ', 80).'Der Urlaub wird über Vivendi beantragt. '.str_repeat('Weiteres. ', 80);
$excerpt = AskPortal::excerpt($text, ['urlaub', 'vivendi'], 300);
$this->assertStringContainsString('Der Urlaub wird über Vivendi beantragt.', $excerpt);
$this->assertLessThanOrEqual(300, mb_strlen($excerpt));
}
public function test_admin_page_shows_unanswered_questions(): void
{
PortalQuestion::query()->create(['question' => 'Wo parke ich?', 'answered' => false, 'source_count' => 0, 'asked_on' => now()->toDateString()]);
PortalQuestion::query()->create(['question' => 'wo parke ich', 'answered' => false, 'source_count' => 0, 'asked_on' => now()->toDateString()]);
$this->actingAs(User::factory()->admin()->create())->get('/admin/frag-das-portal')
->assertOk()->assertSee('Ohne Antwort')->assertSee('2×');
$this->actingAs($this->user)->get('/admin/frag-das-portal')->assertForbidden();
}
}