Files
famous-ly4-ev/app-modules/ai-agent/tests/Feature/ViewEvChatHistoryTest.php
T
Nyan Lin Paing b8d31e3dc4 Add bnfexpress signed admin client and AI Agent Filament UI
- Modules\Shared\Bnfexpress\BnfexpressAdminClient: HMAC-signed HTTP client
  for bnfexpress's admin API (EV FAQs, agent instructions, chat history),
  with a bnfexpress:smoke-test command and full unit coverage.
- New ai-agent module: Filament pages to manage EV FAQs, publish/roll back
  agent instruction versions, and browse EV chat history + transcripts.
- New manage_ai_agent permission (super_admin/admin).
- Recorded .ai/rules for the client's auth scheme and non-Resource
  Filament page/table testing gotchas.
2026-08-30 23:52:21 +07:00

89 lines
3.2 KiB
PHP

<?php
use App\Models\User;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Modules\AiAgent\Filament\Pages\ViewEvChatHistory;
use Spatie\Permission\Models\Permission;
beforeEach(function () {
config([
'services.bnfexpress.ai_api_url' => 'https://bnfexpress.test',
'services.bnfexpress.client_id' => 'ev_admin',
'services.bnfexpress.client_secret' => 'test-secret',
]);
Permission::findOrCreate('manage_ai_agent', 'web');
$this->admin = User::factory()->create()->givePermissionTo(['manage_ai_agent']);
$this->actingAs($this->admin);
});
test('a user without manage_ai_agent cannot access it', function () {
$this->actingAs(User::factory()->create());
expect(ViewEvChatHistory::canAccess())->toBeFalse();
});
test('it lists sessions from the client', function () {
Http::fake(['bnfexpress.test/admin/ev/history*' => Http::response([
'total' => 1,
'limit' => 25,
'offset' => 0,
'sessions' => [
['session_id' => 'sess-1', 'user_id' => 'user-1', 'title' => 'Charging question', 'last_message' => 'Thanks!', 'updated_at' => now()->toIso8601String()],
],
])]);
Livewire::test(ViewEvChatHistory::class)
->assertOk()
->loadTable()
->assertSee('Charging question');
});
test('viewing a transcript shows its messages', function () {
Http::fake([
'bnfexpress.test/admin/ev/history/user-1/sess-1' => Http::response([
'session_id' => 'sess-1',
'user_id' => 'user-1',
'messages' => [
['author' => 'user', 'text' => 'How do I charge my EV?', 'timestamp' => now()->timestamp],
['author' => 'bnfexpress_ev_agent', 'text' => 'Plug it in at a station.', 'timestamp' => now()->timestamp],
],
]),
'bnfexpress.test/admin/ev/history*' => Http::response([
'total' => 1,
'limit' => 25,
'offset' => 0,
'sessions' => [
['session_id' => 'sess-1', 'user_id' => 'user-1', 'title' => 'Charging question', 'last_message' => 'Thanks!', 'updated_at' => now()->toIso8601String()],
],
]),
]);
Livewire::test(ViewEvChatHistory::class)
->loadTable()
->mountTableAction('view', 'user-1:sess-1')
->assertMountedActionModalSee('Plug it in at a station.');
});
test('a failed transcript fetch surfaces the gateway detail message', function () {
Http::fake([
'bnfexpress.test/admin/ev/history/user-1/sess-1' => Http::response(['detail' => 'Session not found.'], 404),
'bnfexpress.test/admin/ev/history*' => Http::response([
'total' => 1,
'limit' => 25,
'offset' => 0,
'sessions' => [
['session_id' => 'sess-1', 'user_id' => 'user-1', 'title' => 'Charging question', 'last_message' => 'Thanks!', 'updated_at' => now()->toIso8601String()],
],
]),
]);
Livewire::test(ViewEvChatHistory::class)
->loadTable()
->mountTableAction('view', 'user-1:sess-1')
->assertMountedActionModalSee('Session not found.')
->assertNotified('Could not load transcript');
});