setMany(['ai.enabled' => true, 'ai.api_key' => Crypt::encryptString('sk-test'), 'ai.model' => 'claude-sonnet-5']); } public function test_editors_add_easy_language_and_readers_see_it(): void { $admin = User::factory()->admin()->create(); $reader = User::factory()->create(); $post = NewsPost::factory()->create(['published_at' => now()->subHour()]); $this->actingAs($reader)->get("/neuigkeiten/{$post->slug}")->assertInertia(fn (Assert $page) => $page->where('easy', null)); $this->actingAs($reader)->put("/leichte-sprache/news/{$post->id}", ['html' => '
Hallo
'])->assertForbidden(); $this->actingAs($admin)->put("/leichte-sprache/news/{$post->id}", ['html' => 'Wir feiern.
']) ->assertRedirect()->assertSessionHas('status', 'Leichte Sprache gespeichert.'); $this->actingAs($reader)->get("/neuigkeiten/{$post->slug}")->assertInertia(fn (Assert $page) => $page ->where('easy.html', fn ($html) => str_contains($html, 'Wir feiern.') && ! str_contains($html, 'script')) ->where('easy.can_edit', false)); $this->actingAs($admin)->put("/leichte-sprache/news/{$post->id}", ['html' => ''])->assertSessionHas('status', 'Leichte Sprache entfernt.'); $this->actingAs($reader)->get("/neuigkeiten/{$post->slug}")->assertInertia(fn (Assert $page) => $page->where('easy', null)); $this->actingAs($admin)->put('/leichte-sprache/unbekannt/1', ['html' => '
x
'])->assertForbidden(); } public function test_ai_suggestion_only_sends_public_text(): void { $this->enableAi(); $admin = User::factory()->admin()->create(); $page = Page::query()->create(['title' => 'Urlaub']); $page->forceFill(['published_at' => now(), 'content' => ['blocks' => [ ['id' => 'a1b2', 'type' => 'text', 'data' => ['html' => 'Urlaub beantragen Sie im Portal.
']], ['id' => 'c3d4', 'type' => 'text', 'data' => ['html' => 'Geheime Regel für Führungskräfte.
'], 'visibility' => ['role' => ['1']]], ]]])->save(); Http::fake([AiAssistant::ENDPOINT => Http::response(['content' => [['type' => 'text', 'text' => "```html\nSie wollen Urlaub?
\n```"]]])]); $this->actingAs($admin)->get('/seiten/urlaub')->assertInertia(fn (Assert $p) => $p->where('easy.ai', true)->where('easy.can_edit', true)); $this->actingAs($admin)->postJson("/leichte-sprache/pages/{$page->id}/vorschlag") ->assertOk()->assertJsonPath('html', 'Sie wollen Urlaub?
'); Http::assertSent(fn ($request) => $request->hasHeader('x-api-key', 'sk-test') && $request['model'] === 'claude-sonnet-5' && str_contains($request['messages'][0]['content'], 'Urlaub beantragen') && ! str_contains($request['messages'][0]['content'], 'Geheime Regel')); } public function test_suggestions_need_a_configured_assistant_and_report_errors(): void { $admin = User::factory()->admin()->create(); $post = NewsPost::factory()->create(['published_at' => now()->subHour()]); $this->actingAs($admin)->postJson("/leichte-sprache/news/{$post->id}/vorschlag")->assertUnprocessable()->assertJsonPath('message', 'Der KI-Assistent ist nicht eingerichtet.'); $this->enableAi(); Http::fake([AiAssistant::ENDPOINT => Http::response(['error' => ['message' => 'invalid x-api-key']], 401)]); $this->actingAs($admin)->postJson("/leichte-sprache/news/{$post->id}/vorschlag")->assertUnprocessable()->assertJsonPath('message', 'Der API-Schlüssel wurde abgelehnt.'); } public function test_ai_settings_page_keeps_the_key_secret(): void { $admin = User::factory()->admin()->create(); $this->actingAs($admin)->get('/admin/ki-assistent')->assertOk()->assertSee('Datenschutz'); $this->actingAs(User::factory()->create())->get('/admin/ki-assistent')->assertForbidden(); app(AiAssistant::class)->save(['enabled' => true, 'model' => null, 'api_key' => 'sk-geheim']); $this->assertNotSame('sk-geheim', app(Settings::class)->get('ai.api_key')); $this->assertTrue(app(AiAssistant::class)->available()); $this->assertSame(AiAssistant::DEFAULT_MODEL, app(AiAssistant::class)->model()); $this->actingAs($admin)->get('/admin/ki-assistent')->assertDontSee('sk-geheim'); } }