新增聊天室状态与功能快捷菜单

This commit is contained in:
2026-04-24 21:17:44 +08:00
parent d7ec42a025
commit 0f0bfef2a8
18 changed files with 1361 additions and 124 deletions
+58
View File
@@ -149,6 +149,38 @@ class ChatControllerTest extends TestCase
$this->assertSame($position?->department?->name, $authorizedPayload['department_name'] ?? null);
}
/**
* 测试聊天室 Presence 频道会返回仍在有效期内的当日状态载荷。
*/
public function test_room_presence_channel_returns_active_daily_status_payload(): void
{
$room = Room::create([
'room_name' => 'dsbadge',
'door_open' => true,
]);
$user = User::factory()->create([
'daily_status_key' => 'working_hard',
'daily_status_expires_at' => now()->addHours(3),
]);
$channelCallback = Broadcast::driver()->getChannels()->get('room.{roomId}');
$this->assertIsCallable($channelCallback);
$this->actingAs($user)->get(route('chat.room', $room->id));
$authorizedPayload = $channelCallback($user, (string) $room->id);
$this->assertIsArray($authorizedPayload);
$this->assertSame('working_hard', $authorizedPayload['daily_status_key'] ?? null);
$this->assertSame('搬砖', $authorizedPayload['daily_status_label'] ?? null);
$this->assertSame('🧱', $authorizedPayload['daily_status_icon'] ?? null);
$this->assertSame('工作学习', $authorizedPayload['daily_status_group'] ?? null);
$this->assertSame(
$user->fresh()->daily_status_expires_at?->toIso8601String(),
$authorizedPayload['daily_status_expires_at'] ?? null
);
}
/**
* 测试主干默认聊天室页面不会渲染虚拟形象挂载点和配置。
*/
@@ -186,6 +218,32 @@ class ChatControllerTest extends TestCase
$response->assertSee('toggleBlockedSystemSender');
}
/**
* 测试聊天室输入区会渲染状态入口,并移除旧的直接清屏按钮绑定。
*/
public function test_room_view_renders_daily_status_button_and_hides_direct_local_clear_button(): void
{
$room = Room::create(['room_name' => 'statusmenu']);
$user = User::factory()->create();
$response = $this->actingAs($user)->get(route('chat.room', $room->id));
$response->assertOk();
$response->assertSee('toggleFeatureMenu(event)', false);
$response->assertSee('功能');
$response->assertSee('设个状态', false);
$response->assertSee('runFeatureShortcut(\'shop\')', false);
$response->assertSee('runFeatureShortcut(\'vip\')', false);
$response->assertSee('runFeatureShortcut(\'game\')', false);
$response->assertSee('runFeatureShortcut(\'avatar\')', false);
$response->assertSee('runFeatureShortcut(\'bank\')', false);
$response->assertSee('runFeatureShortcut(\'marriage\')', false);
$response->assertSee('runFeatureShortcut(\'friend\')', false);
$response->assertSee('runFeatureShortcut(\'settings\')', false);
$response->assertSee('本地清屏', false);
$response->assertDontSee('onclick="localClearScreen()"', false);
}
/**
* 测试无聊天室权限的职务用户看不到顶部管理按钮。
*/
+111
View File
@@ -10,6 +10,8 @@ namespace Tests\Feature;
use App\Models\Room;
use App\Models\Sysparam;
use App\Models\User;
use App\Services\ChatUserPresenceService;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
@@ -31,6 +33,9 @@ class UserControllerTest extends TestCase
{
parent::setUp();
// 清空 Redis 在线状态,避免当日状态与房间在线缓存互相污染。
Redis::flushall();
Sysparam::updateOrCreate(['alias' => 'superlevel'], ['body' => '100']);
Sysparam::updateOrCreate(['alias' => 'level_kick'], ['body' => '15']);
Sysparam::updateOrCreate(['alias' => 'level_mute'], ['body' => '15']);
@@ -163,6 +168,95 @@ class UserControllerTest extends TestCase
], $user->chat_preferences);
}
/**
* 测试合法聊天室当日状态可以保存,并在当天结束后自动失效。
*/
public function test_can_update_daily_status_until_end_of_day(): void
{
Carbon::setTestNow('2026-04-24 10:36:00');
try {
$user = User::factory()->create([
'username' => 'status-user',
]);
$room = $this->enterRoomForUser($user);
$response = $this->actingAs($user)->putJson(route('user.update_daily_status'), [
'room_id' => $room->id,
'action' => 'set',
'status_key' => 'working_hard',
]);
$expectedExpiry = now()->endOfDay();
$response->assertOk()
->assertJsonPath('status', 'success')
->assertJsonPath('data.status.key', 'working_hard')
->assertJsonPath('data.status.label', '搬砖')
->assertJsonPath('data.status.group', '工作学习');
$user->refresh();
$this->assertSame('working_hard', $user->daily_status_key);
$this->assertSame(
$expectedExpiry->toDateTimeString(),
$user->daily_status_expires_at?->toDateTimeString()
);
// 时间推进到次日,验证在线名单服务不再返回已过期状态。
Carbon::setTestNow($expectedExpiry->copy()->addSecond());
$this->assertNull(app(ChatUserPresenceService::class)->currentDailyStatus($user->fresh()));
} finally {
Carbon::setTestNow();
}
}
/**
* 测试非法聊天室当日状态键会返回 422 校验错误。
*/
public function test_invalid_daily_status_key_returns_validation_error(): void
{
$user = User::factory()->create();
$room = Room::create([
'room_name' => 'dsinvalid',
'room_owner' => 'someone',
]);
$response = $this->actingAs($user)->putJson(route('user.update_daily_status'), [
'room_id' => $room->id,
'action' => 'set',
'status_key' => 'not-a-real-status',
]);
$response->assertStatus(422)
->assertJsonValidationErrors('status_key');
}
/**
* 测试清除聊天室当日状态后会把状态字段置空。
*/
public function test_clear_daily_status_resets_status_fields(): void
{
$user = User::factory()->create([
'daily_status_key' => 'working_hard',
'daily_status_expires_at' => now()->endOfDay(),
]);
$room = $this->enterRoomForUser($user);
$response = $this->actingAs($user)->putJson(route('user.update_daily_status'), [
'room_id' => $room->id,
'action' => 'clear',
]);
$response->assertOk()
->assertJsonPath('status', 'success')
->assertJsonPath('message', '状态已清除。')
->assertJsonPath('data.status', null);
$user->refresh();
$this->assertNull($user->daily_status_key);
$this->assertNull($user->daily_status_expires_at);
}
public function test_can_change_password()
{
$user = User::factory()->create([
@@ -304,4 +398,21 @@ class UserControllerTest extends TestCase
$target->refresh();
$this->assertEquals(-1, $target->user_level);
}
/**
* 让指定用户先进入聊天室,满足“仅在线用户可设置状态”的前置条件。
*/
private function enterRoomForUser(User $user): Room
{
$room = Room::create([
'room_name' => 'dsr'.$user->id,
'room_owner' => 'someone',
]);
$this->actingAs($user)
->get(route('chat.room', $room->id))
->assertOk();
return $room;
}
}