优化ai小班长

This commit is contained in:
2026-04-12 22:25:18 +08:00
parent f8d5a3b250
commit ef407a8c6e
7 changed files with 608 additions and 91 deletions
+35 -5
View File
@@ -1,5 +1,12 @@
<?php
/**
* 文件功能:AI小班长聊天控制器测试
*
* 覆盖 AI 开关、普通回复、金币福利以及上下文清理逻辑,
* 同时验证金币福利发放时不会自动动用 AI 的银行存款。
*/
namespace Tests\Feature;
use App\Models\Sysparam;
@@ -10,17 +17,26 @@ use Illuminate\Support\Facades\Redis;
use Mockery\MockInterface;
use Tests\TestCase;
/**
* 验证 AI小班长对话与金币福利接口行为。
*/
class ChatBotControllerTest extends TestCase
{
use RefreshDatabase;
/**
* 每个测试结束后清空 Redis,避免每日奖励计数彼此污染。
*/
protected function tearDown(): void
{
Redis::flushall();
parent::tearDown();
}
public function test_chatbot_disabled_by_default()
/**
* 默认情况下,AI 功能关闭时应直接拒绝聊天请求。
*/
public function test_chatbot_disabled_by_default(): void
{
$user = User::factory()->create();
@@ -33,7 +49,10 @@ class ChatBotControllerTest extends TestCase
$response->assertJson(['status' => 'error']);
}
public function test_chatbot_can_reply()
/**
* AI 功能开启后,控制器会返回模型生成的正常回复。
*/
public function test_chatbot_can_reply(): void
{
$user = User::factory()->create();
@@ -69,7 +88,10 @@ class ChatBotControllerTest extends TestCase
]);
}
public function test_chatbot_can_give_gold()
/**
* 发放金币福利时,只使用 AI 当前手上金币,不会为了凑金额自动动用银行存款。
*/
public function test_chatbot_can_give_gold(): void
{
$user = User::factory()->create(['jjb' => 0]);
@@ -80,7 +102,8 @@ class ChatBotControllerTest extends TestCase
User::factory()->create([
'username' => 'AI小班长',
'exp_num' => 0,
'jjb' => 1000, // Ensure AI bot has enough gold
'jjb' => 1000000,
'bank_jjb' => 1000,
]);
// Mock the AiChatService
@@ -105,9 +128,16 @@ class ChatBotControllerTest extends TestCase
$user->refresh();
$this->assertGreaterThan(0, $user->jjb);
$this->assertLessThanOrEqual(500, $user->jjb);
$aiUser = User::query()->where('username', 'AI小班长')->firstOrFail();
$this->assertLessThan(1000000, (int) $aiUser->jjb);
$this->assertSame(1000, (int) $aiUser->bank_jjb);
}
public function test_clear_context()
/**
* 用户主动清空上下文时,应调用 AI 服务清理其上下文缓存。
*/
public function test_clear_context(): void
{
$user = User::factory()->create();