赠送金币改为私聊通知

This commit is contained in:
2026-04-14 21:53:36 +08:00
parent 762caac938
commit df29da7440
2 changed files with 72 additions and 5 deletions
+67
View File
@@ -274,6 +274,73 @@ class ChatControllerTest extends TestCase
$this->assertEquals(0, Redis::hexists("room:{$room->id}:users", $user->username));
}
/**
* 测试双击名片赠金币会写入私聊消息,且只有赠送双方能看到历史记录。
*/
public function test_gift_gold_creates_secret_message_visible_only_to_participants(): void
{
$room = Room::create(['room_name' => 'gift-gold']);
$sender = User::factory()->create(['jjb' => 500]);
$receiver = User::factory()->create(['jjb' => 100]);
$outsider = User::factory()->create();
$response = $this->actingAs($sender)->postJson(route('gift.gold'), [
'to_user' => $receiver->username,
'room_id' => $room->id,
'amount' => 88,
]);
$response->assertOk();
$response->assertJsonFragment([
'status' => 'success',
'message' => "赠送成功!已向 {$receiver->username} 赠送 88 金币。",
]);
// 余额应同步完成转移,确保消息不是“通知成功但金额未变”。
$this->assertSame(412, $sender->fresh()->jjb);
$this->assertSame(188, $receiver->fresh()->jjb);
$messages = Redis::lrange("room:{$room->id}:messages", 0, -1);
$giftMessage = collect($messages)
->map(fn (string $item) => json_decode($item, true))
->first(fn (array $item) => ($item['from_user'] ?? null) === $sender->username
&& ($item['to_user'] ?? null) === $receiver->username
&& ($item['content'] ?? null) === '赠送了你 88 金币!💝');
$this->assertNotNull($giftMessage);
$this->assertTrue((bool) ($giftMessage['is_secret'] ?? false));
// 赠送方查看房间历史时,应保留这条私聊通知。
$senderHistory = $this->actingAs($sender)
->get(route('chat.room', $room->id))
->viewData('historyMessages');
$this->assertTrue(collect($senderHistory)->contains(
fn (array $item) => ($item['from_user'] ?? null) === $sender->username
&& ($item['to_user'] ?? null) === $receiver->username
&& ($item['content'] ?? null) === '赠送了你 88 金币!💝'
));
// 接收方查看房间历史时,也应看到这条私聊通知。
$receiverHistory = $this->actingAs($receiver)
->get(route('chat.room', $room->id))
->viewData('historyMessages');
$this->assertTrue(collect($receiverHistory)->contains(
fn (array $item) => ($item['from_user'] ?? null) === $sender->username
&& ($item['to_user'] ?? null) === $receiver->username
&& ($item['content'] ?? null) === '赠送了你 88 金币!💝'
));
// 旁观者不应在历史消息里看到别人的赠金币私聊。
$outsiderHistory = $this->actingAs($outsider)
->get(route('chat.room', $room->id))
->viewData('historyMessages');
$this->assertFalse(collect($outsiderHistory)->contains(
fn (array $item) => ($item['from_user'] ?? null) === $sender->username
&& ($item['to_user'] ?? null) === $receiver->username
&& ($item['content'] ?? null) === '赠送了你 88 金币!💝'
));
}
/**
* 测试会员用户首次进房时会把专属欢迎主题写入历史消息。
*/