import { DakaoSubscriber } from './dakao.subscriber'; import { DakaoInboundEvent, DakaoSegment } from './dakao.types'; import { CommandParseError } from './parsers/command.parser'; function makeSubscriber(): DakaoSubscriber { return new DakaoSubscriber( {} as never, {} as never, {} as never, {} as never, {} as never, {} as never, {} as never, {} as never, {} as never, {} as never, {} as never, {} as never, {} as never, ); } const event = { projectId: '111111111111111111111111', chatroomId: 'chatroom-1', nickname: '프리리', userId: '12345', origin: { chatId: 'chat-1', logId: 'log-1', chatLog: { logId: 'log-1', chatId: 'chat-1', authorId: '12345', type: 1, sendAt: 0, message: '!내전', }, }, } satisfies DakaoInboundEvent; describe('DakaoSubscriber', () => { it('routes data transfer with the current sender ID and publishes its reply', async () => { const subscriber = makeSubscriber(); const segments = [{ kind: 'text', text: ' 데이터 이관 완료' }]; const handle = jest.fn().mockResolvedValue(segments); const sendMessage = jest.fn(); const markProcessed = jest.fn(); Object.assign(subscriber, { dakaoUserService: { resolveCommunityIdByDakaoProjectId: async () => event.projectId, }, dataTransferHandler: { handle }, publisher: { sendMessage }, inbox: { claim: async () => ({ status: 'claimed' }), markProcessed }, }); await ( subscriber as unknown as { handleEvent: (input: DakaoInboundEvent) => Promise; } ).handleEvent({ ...event, origin: { ...event.origin, chatLog: { ...event.origin.chatLog, message: '!내전 데이터이관 예전 닉', }, }, }); expect(handle).toHaveBeenCalledWith({ communityId: event.projectId, senderUserId: '12345', senderNickname: '프리리', previousNickname: '예전 닉', }); expect(sendMessage).toHaveBeenCalledWith({ chatroomId: 'chatroom-1', segments, correlationId: 'log-1', }); expect(markProcessed).toHaveBeenCalled(); }); it('renders usage help with star emoticon command lines', () => { const subscriber = makeSubscriber() as unknown as { parseErrorReply: ( event: DakaoInboundEvent, senderNickname: string, err: CommandParseError, ) => DakaoSegment[]; }; const segments = subscriber.parseErrorReply( event, '프리리', new CommandParseError('usage'), ); expect(segments).toEqual([ { kind: 'mention', userId: '12345', displayText: '프리리' }, { kind: 'text', text: ' 내전 명령어 사용법\n' }, { kind: 'emoticon', id: '1200281_035', altText: '스타' }, { kind: 'text', text: ' !내전 일정\u3000\n' }, { kind: 'emoticon', id: '1200281_035', altText: '스타' }, { kind: 'text', text: ' !내전 신청 코드 소환사이름#태그 포지션1,포지션2\u3000\n', }, { kind: 'emoticon', id: '1200281_035', altText: '스타' }, { kind: 'text', text: ' !내전 취소 코드\u3000\n' }, { kind: 'emoticon', id: '1200281_035', altText: '스타' }, { kind: 'text', text: ' !내전 계정등록 소환사이름#태그\u3000\n' }, { kind: 'emoticon', id: '1200281_035', altText: '스타' }, { kind: 'text', text: ' !내전 선호포지션등록 포지션1,포지션2\u3000\n', }, { kind: 'emoticon', id: '1200281_035', altText: '스타' }, { kind: 'text', text: ' !내전 대표계정변경 소환사이름#태그\u3000\n', }, { kind: 'emoticon', id: '1200281_035', altText: '스타' }, { kind: 'text', text: ' !내전 데이터이관 이전카카오톡채팅방닉네임' }, ]); }); it('keeps the unknown command header before star emoticon usage lines', () => { const subscriber = makeSubscriber() as unknown as { parseErrorReply: ( event: DakaoInboundEvent, senderNickname: string, err: CommandParseError, ) => DakaoSegment[]; }; const segments = subscriber.parseErrorReply( event, '프리리', new CommandParseError('unknown_command', '폭파'), ); expect(segments.slice(0, 2)).toEqual([ { kind: 'mention', userId: '12345', displayText: '프리리' }, { kind: 'text', text: ' 알 수 없는 명령어입니다.\n' }, ]); expect(segments).toContainEqual({ kind: 'emoticon', id: '1200281_035', altText: '스타', }); }); });