126 lines
3.6 KiB
PHP
126 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Sysparam;
|
|
use App\Models\User;
|
|
use App\Services\AiChatService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Redis;
|
|
use Mockery\MockInterface;
|
|
use Tests\TestCase;
|
|
|
|
class ChatBotControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
Redis::flushall();
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_chatbot_disabled_by_default()
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$response = $this->actingAs($user)->postJson(route('chatbot.chat'), [
|
|
'message' => 'Hello',
|
|
'room_id' => 1,
|
|
]);
|
|
|
|
$response->assertStatus(403);
|
|
$response->assertJson(['status' => 'error']);
|
|
}
|
|
|
|
public function test_chatbot_can_reply()
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
Sysparam::updateOrCreate(['alias' => 'chatbot_enabled'], ['body' => '1']);
|
|
|
|
User::factory()->create([
|
|
'username' => 'AI小班长',
|
|
'exp_num' => 0,
|
|
'jjb' => 0,
|
|
]);
|
|
|
|
// Mock the AiChatService
|
|
$this->mock(AiChatService::class, function (MockInterface $mock) {
|
|
$mock->shouldReceive('chat')
|
|
->once()
|
|
->andReturn([
|
|
'reply' => 'Hello from AI',
|
|
'provider' => 'test_provider',
|
|
'model' => 'test_model',
|
|
]);
|
|
});
|
|
|
|
$response = $this->actingAs($user)->postJson(route('chatbot.chat'), [
|
|
'message' => 'Hello',
|
|
'room_id' => 1,
|
|
]);
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJson([
|
|
'status' => 'success',
|
|
'reply' => 'Hello from AI',
|
|
'provider' => 'test_provider',
|
|
]);
|
|
}
|
|
|
|
public function test_chatbot_can_give_gold()
|
|
{
|
|
$user = User::factory()->create(['jjb' => 0]);
|
|
|
|
Sysparam::updateOrCreate(['alias' => 'chatbot_enabled'], ['body' => '1']);
|
|
Sysparam::updateOrCreate(['alias' => 'chatbot_max_daily_rewards'], ['body' => '1']);
|
|
Sysparam::updateOrCreate(['alias' => 'chatbot_max_gold'], ['body' => '500']);
|
|
|
|
User::factory()->create([
|
|
'username' => 'AI小班长',
|
|
'exp_num' => 0,
|
|
'jjb' => 1000, // Ensure AI bot has enough gold
|
|
]);
|
|
|
|
// Mock the AiChatService
|
|
$this->mock(AiChatService::class, function (MockInterface $mock) {
|
|
$mock->shouldReceive('chat')
|
|
->once()
|
|
->andReturn([
|
|
'reply' => 'Here is some gold! [ACTION:GIVE_GOLD]',
|
|
'provider' => 'test_provider',
|
|
'model' => 'test_model',
|
|
]);
|
|
});
|
|
|
|
$response = $this->actingAs($user)->postJson(route('chatbot.chat'), [
|
|
'message' => 'Give me gold',
|
|
'room_id' => 1,
|
|
]);
|
|
|
|
$response->assertStatus(200);
|
|
|
|
// User should have received gold
|
|
$user->refresh();
|
|
$this->assertGreaterThan(0, $user->jjb);
|
|
$this->assertLessThanOrEqual(500, $user->jjb);
|
|
}
|
|
|
|
public function test_clear_context()
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$this->mock(AiChatService::class, function (MockInterface $mock) use ($user) {
|
|
$mock->shouldReceive('clearContext')
|
|
->once()
|
|
->with($user->id);
|
|
});
|
|
|
|
$response = $this->actingAs($user)->postJson(route('chatbot.clear'));
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJson(['status' => 'success']);
|
|
}
|
|
}
|