import mongoose from 'mongoose'; import { BadRequestException } from '@nestjs/common'; jest.mock('../auth/jwt.service', () => ({ JwtService: class JwtService {} })); import { AuctionGateway } from './auction.gateway'; function setup() { const appendChatMessage = jest.fn(); const gateway = new AuctionGateway( { appendChatMessage } as never, {} as never, {} as never, {} as never, {} as never, {} as never, {} as never, ); const broadcast = jest.fn(); const to = jest.fn().mockReturnValue({ emit: broadcast }); (gateway as unknown as { server: unknown }).server = { to, }; const socket = { data: { matchId: new mongoose.Types.ObjectId().toString(), user: { _id: new mongoose.Types.ObjectId(), role: 'admin', riotAccounts: [], kakaoTalkNickname: '운영자', }, }, emit: jest.fn(), }; return { gateway, appendChatMessage, broadcast, socket, to }; } describe('Auction chat delivery', () => { const payload = { message: '준비해 주세요', clientMessageId: '0589e8a4-bccd-4ccd-a3ae-28f117401e79', }; it('acknowledges only after saving and assigns the operator role from the socket user', async () => { const { gateway, appendChatMessage, broadcast, socket } = setup(); const message = { id: 'saved', ...payload, sentAt: new Date() }; let finish!: (result: unknown) => void; appendChatMessage.mockReturnValue( new Promise((resolve) => { finish = resolve; }), ); const ack = jest.fn(); const pending = gateway.handleChatMessage(socket as never, payload, ack); expect(ack).not.toHaveBeenCalled(); expect(broadcast).not.toHaveBeenCalled(); finish({ message, created: true }); await pending; expect(appendChatMessage).toHaveBeenCalledWith( expect.objectContaining({ clientMessageId: payload.clientMessageId, senderRole: 'operator', displayName: '운영자', }), ); expect(ack).toHaveBeenCalledWith({ ok: true, message: expect.objectContaining({ id: 'saved' }), }); expect(broadcast).toHaveBeenCalledTimes(1); }); it('acknowledges a retry without broadcasting a duplicate', async () => { const { gateway, appendChatMessage, broadcast, socket } = setup(); appendChatMessage.mockResolvedValue({ message: { id: 'saved' }, created: false, }); const ack = jest.fn(); await gateway.handleChatMessage(socket as never, payload, ack); expect(ack).toHaveBeenCalledWith({ ok: true, message: { id: 'saved' } }); expect(broadcast).not.toHaveBeenCalled(); }); it('returns rejection to the requesting composer without reporting success', async () => { const { gateway, appendChatMessage, broadcast, socket } = setup(); appendChatMessage.mockRejectedValue( new BadRequestException('Chat message is required.'), ); const ack = jest.fn(); await gateway.handleChatMessage(socket as never, payload, ack); expect(ack).toHaveBeenCalledWith( expect.objectContaining({ ok: false, message: 'Chat message is required.', }), ); expect(broadcast).not.toHaveBeenCalled(); }); }); describe('Auction live reactions', () => { it('broadcasts to the joined room with the server-resolved name, without saving a chat message', () => { const { gateway, appendChatMessage, broadcast, socket, to } = setup(); const ack = jest.fn(); gateway.handleChatReaction(socket as never, { kind: 'clap' }, ack); expect(to).toHaveBeenCalledWith(socket.data.matchId); expect(broadcast).toHaveBeenCalledWith( 'chat-reaction', expect.objectContaining({ id: expect.any(String), kind: 'clap', displayName: '운영자', userId: socket.data.user._id.toString(), }), ); expect(ack).toHaveBeenCalledWith({ ok: true, reaction: broadcast.mock.calls[0][1], }); expect(appendChatMessage).not.toHaveBeenCalled(); }); it('rejects unjoined clients and arbitrary reaction values', () => { const { gateway, broadcast, socket } = setup(); const ack = jest.fn(); gateway.handleChatReaction({ data: {} } as never, { kind: 'heart' }, ack); expect(ack).toHaveBeenLastCalledWith({ ok: false, code: 'NOT_JOINED' }); for (const kind of ['__proto__', 'not-supported', 1, null]) { gateway.handleChatReaction(socket as never, { kind }, ack); expect(ack).toHaveBeenLastCalledWith({ ok: false, code: 'INVALID_REACTION', }); } expect(broadcast).not.toHaveBeenCalled(); }); it('throttles bursts, then accepts the next reaction after the interval', () => { const { gateway, broadcast, socket } = setup(); const clock = jest.spyOn(Date, 'now').mockReturnValue(1000); const ack = jest.fn(); try { gateway.handleChatReaction(socket as never, { kind: 'heart' }, ack); gateway.handleChatReaction(socket as never, { kind: 'heart' }, ack); expect(ack).toHaveBeenLastCalledWith({ ok: false, code: 'REACTION_RATE_LIMITED', }); expect(broadcast).toHaveBeenCalledTimes(1); clock.mockReturnValue(1400); gateway.handleChatReaction(socket as never, { kind: 'celebrate' }, ack); expect(broadcast).toHaveBeenCalledTimes(2); } finally { clock.mockRestore(); } }); });