#!/usr/bin/env ts-node // tslint:disable:max-line-length // tslint:disable:no-shadowed-variable import test from 'blue-tape' import { MessagePayload, MessageType, } from 'unique-wechaty-puppet' import { MacproMessagePayload, MacproMessageType, } from '../schemas' import { messageRawPayloadParser, } from './message-raw-payload-parser' test('messageRawPayloadParser', async t => { t.test('text', async t => { const MACPRO_MESSAGE_PAYLOAD_TEXT: MacproMessagePayload = { content: '啦', content_type: 1, file_name: '', messageId: '1', my_account: 'wxid_v7j3e9kna9l912', my_account_alias: 'wxid_v7j3e9kna9l912', my_name: '李青青', timestamp: 0, to_account: 'lylezhuifeng', to_name: '球球', type: 2, voice_len: 0, } const EXPECTED_MESSAGE_PAYLOAD_TEXT: MessagePayload = { fromId: 'lylezhuifeng', id: '1', mentionIdList: [], roomId: undefined, text: '啦', timestamp: 0, toId: 'wxid_v7j3e9kna9l912', type: MessageType.Text, } const actualPayload = await messageRawPayloadParser(MACPRO_MESSAGE_PAYLOAD_TEXT) t.deepEqual(actualPayload, EXPECTED_MESSAGE_PAYLOAD_TEXT) }) t.test('voice', async t => { const MACPRO_MESSAGE_PAYLOAD_VOICE: MacproMessagePayload = { content: 'https://wkgj.oss-cn-beijing.aliyuncs.com/files/20190831/6bf0c33d449eeaff40069de530db7aa2.mp3', content_type: 4, file_name: '', messageId: '1', my_account: 'wxid_v7j3e9kna9l912', my_account_alias: 'wxid_v7j3e9kna9l912', my_name: '李青青', timestamp: 0, to_account: 'lylezhuifeng', to_name: '球球', type: 2, voice_len: 1, } const EXPECTED_MESSAGE_PAYLOAD_VOICE: MessagePayload = { fromId: 'lylezhuifeng', id: '1', mentionIdList: [], roomId: undefined, text: 'https://wkgj.oss-cn-beijing.aliyuncs.com/files/20190831/6bf0c33d449eeaff40069de530db7aa2.mp3', timestamp: 0, toId: 'wxid_v7j3e9kna9l912', type: MessageType.Audio, } const actualPayload = await messageRawPayloadParser(MACPRO_MESSAGE_PAYLOAD_VOICE) t.deepEqual(actualPayload, EXPECTED_MESSAGE_PAYLOAD_VOICE) }) test('attachment file with ext .xlsx', async t => { const FILE_MESSAGE_PAYLOAD: MacproMessagePayload = { content: 'https://wkgj.oss-cn-beijing.aliyuncs.com/files/20190831/7f24126d7c269d815f8827f307fc48d0.xlsx', content_type: MacproMessageType.File, file_name: '报价.xlsx', messageId: '1', my_account: 'wxid_v7j3e9kna9l912', my_account_alias: 'wxid_v7j3e9kna9l912', my_name: '李青青', timestamp: 0, to_account: 'lylezhuifeng', to_name: '高原ོ', type: 2, voice_len: 1, } const EXPECTED_PAYLOAD: MessagePayload = { fromId: 'lylezhuifeng', id: '1', mentionIdList: [], roomId: undefined, text: 'https://wkgj.oss-cn-beijing.aliyuncs.com/files/20190831/7f24126d7c269d815f8827f307fc48d0.xlsx', timestamp: 0, toId: 'wxid_v7j3e9kna9l912', type: 1, } const payload = await messageRawPayloadParser(FILE_MESSAGE_PAYLOAD) t.deepEqual(payload, EXPECTED_PAYLOAD, 'should parse share card message peer to peer') }) }) // test('room invitation created by bot', async t => { // const MESSAGE_PAYLOAD: MacproMessagePayload = { // content : '3453262102@chatroom:\n\n\t\n\t\t\n\t\t\n\t\t\n\t\t\tinvite\n\t\t\t\n\t\t\t\n\t\t\t\t\n\t\t\t\n\t\t\n\t\n\n', // data : '', // fromUser : '3453262102@chatroom', // messageId : '4030118997146183783', // messageSource: '', // messageType : 10002, // status : 1, // timestamp : 1528755135, // toUser : 'wxid_5zj4i5htp9ih22', // } // const EXPECTED_PAYLOAD: MessagePayload = { // fromId : undefined, // id : '4030118997146183783', // mentionIdList: undefined, // roomId : '3453262102@chatroom', // text : '\n\t\n\t\t\n\t\t\n\t\t\n\t\t\tinvite\n\t\t\t\n\t\t\t\n\t\t\t\t\n\t\t\t\n\t\t\n\t\n\n', // timestamp : 1528755135, // toId : 'wxid_5zj4i5htp9ih22', // type : 0, // } // const payload = await messageRawPayloadParser(MESSAGE_PAYLOAD) // t.deepEqual(payload, EXPECTED_PAYLOAD, 'should parse room invitation message payload') // }) // test('room ownership transfer message', async t => { // const MESSAGE_PAYLOAD: MacproMessagePayload = { // content : '你已成为新群主', // data : '', // fromUser : '6350854677@chatroom', // messageId : '3798725634572049107', // messageSource: '', // messageType : 10000, // status : 1, // timestamp : 1527689361, // toUser : 'wxid_zj2cahpwzgie12', // } // const EXPECTED_PAYLOAD: MessagePayload = { // fromId : undefined, // id : '3798725634572049107', // mentionIdList: undefined, // roomId : '6350854677@chatroom', // text : '你已成为新群主', // timestamp : 1527689361, // toId : 'wxid_zj2cahpwzgie12', // type : 0, // } // const payload = await messageRawPayloadParser(MESSAGE_PAYLOAD) // t.deepEqual(payload, EXPECTED_PAYLOAD, 'should parse ower transfer message') // }) // test('StatusNotify to roomId', async t => { // const MESSAGE_PAYLOAD: MacproMessagePayload = { // content : '\n\n5367653125@chatroom\n\n', // data : '', // fromUser : 'wxid_5zj4i5htp9ih22', // messageId : '179056144527271247', // messageSource: '', // messageType : 51, // status : 1, // timestamp : 1528920139, // toUser : '5367653125@chatroom', // } // const EXPECTED_PAYLOAD = { // fromId : 'wxid_5zj4i5htp9ih22', // id : '179056144527271247', // mentionIdList: undefined, // roomId : '5367653125@chatroom', // text : '\n\n5367653125@chatroom\n\n', // timestamp : 1528920139, // toId : undefined, // type : 0, // } // const payload = await messageRawPayloadParser(MESSAGE_PAYLOAD) // t.deepEqual(payload, EXPECTED_PAYLOAD, 'should parse status notify message to room id') // }) // test('share card peer to peer', async t => { // const MESSAGE_PAYLOAD: MacproMessagePayload = { // content : '\n\n', // data : '', // fromUser : 'lizhuohuan', // messageId : '5911987709823889005', // messageSource: '', // messageType : 42, // status : 1, // timestamp : 1528959169, // toUser : 'wxid_5zj4i5htp9ih22', // } // const EXPECTED_PAYLOAD: MessagePayload = { // fromId : 'lizhuohuan', // id : '5911987709823889005', // mentionIdList: undefined, // roomId : undefined, // text : '\n\n', // timestamp : 1528959169, // toId : 'wxid_5zj4i5htp9ih22', // type : 3, // } // const payload = await messageRawPayloadParser(MESSAGE_PAYLOAD) // t.deepEqual(payload, EXPECTED_PAYLOAD, 'should parse share card message peer to peer') // }) // test('share card in room', async t => { // const MESSAGE_PAYLOAD: MacproMessagePayload = { // content : 'lizhuohuan:\n\n\n', // data : '', // fromUser : '3453262102@chatroom', // messageId : '7332176666514216982', // messageSource: '\n\t0\n\t3\n\n', // messageType : 42, // status : 1, // timestamp : 1528961383, // toUser : 'wxid_5zj4i5htp9ih22', // } // const EXPECTED_PAYLOAD: MessagePayload = { // fromId : 'lizhuohuan', // id : '7332176666514216982', // mentionIdList: undefined, // roomId : '3453262102@chatroom', // text : '\n\n', // timestamp : 1528961383, // toId : 'wxid_5zj4i5htp9ih22', // type : 3, // } // const payload = await messageRawPayloadParser(MESSAGE_PAYLOAD) // t.deepEqual(payload, EXPECTED_PAYLOAD, 'should parse share card message peer to peer') // }) // test('recalled message in room', async t => { // const MESSAGE_PAYLOAD: MacproMessagePayload = { // content: '\n\t\n\t\tlylezhuifeng\n\t\t1062840803\n\t\t2244146148648143901\n\t\t\n\t\n\n', // data: null, // fromUser: 'lylezhuifeng', // messageId: '1062840804', // messageSource: '', // messageType: 10002, // status: 4, // timestamp: 1553853738, // toUser: 'wxid_zovb9ol86m7l22', // } // const EXPECTED_PAYLOAD: MessagePayload = { // fromId: 'lylezhuifeng', // id: '1062840804', // mentionIdList: undefined, // roomId: undefined, // text: '1062840803', // timestamp: 1553853738, // toId: 'wxid_zovb9ol86m7l22', // type: 11, // } // const payload = await messageRawPayloadParser(MESSAGE_PAYLOAD) // t.deepEqual(payload, EXPECTED_PAYLOAD, 'should parse recalled message in room') // }) // test('Official account sent url', async t => { // const MESSAGE_PAYLOAD: MacproMessagePayload = { // content: '<![CDATA[这是一个测试的图文消息]]>51000000<![CDATA[这是一个测试的图文消息]]>15597078651000000020000000000200001', // data: null, // fromUser: 'gh_87e03c422b73', // messageId: '1006688399', // messageSource: '\n\t3\n\t\n\t\t0\n\t\t\n\t\n\t0\n\t1\n\t1\n\n', // messageType: 49, // status: 3, // timestamp: 1559707890, // toUser: 'wxid_x01jgln69ath22', // } // const EXPECTED_PAYLOAD: MessagePayload = { // filename: '1006688399-to-be-implement.txt', // fromId: 'gh_87e03c422b73', // id: '1006688399', // mentionIdList: undefined, // roomId: undefined, // text: '<![CDATA[这是一个测试的图文消息]]>51000000<![CDATA[这是一个测试的图文消息]]>15597078651000000020000000000200001', // timestamp: 1559707890, // toId: 'wxid_x01jgln69ath22', // type: 12, // } // const payload = await messageRawPayloadParser(MESSAGE_PAYLOAD) // t.deepEqual(payload, EXPECTED_PAYLOAD, 'should parse official account sent url.') // }) // test('Special Official account sent url', async t => { // const MESSAGE_PAYLOAD = { // content: '\n \n <![CDATA[“演员”孙宇晨]]>\n \n \n 5\n 1\n \n 0\n \n \n \n 0\n \n \n \n \n \n \n \n \n \n 0\n 0\n \n \n \n \n 0\n <![CDATA[“演员”孙宇晨]]>\n \n \n \n 1559707142\n \n \n \n 504497991\n \n \n \n \n \n \n \n 0\n 0\n 0\n \n \n 0\n 0\n \n \n \n \n 840787998215241730\n \n \n 2\n 0\n 0\n \n \n \n 0\n <![CDATA[深创投孙东升:专业化是本土创投转型升级的必由之路]]>\n \n \n \n 1559707142\n \n \n \n 504497993\n \n \n \n \n \n \n \n 0\n 0\n 0\n \n \n 0\n 0\n \n \n \n \n 840787998986993664\n \n \n 2\n 0\n 0\n \n \n \n 0\n <![CDATA[松禾资本厉伟:老老实实做生意]]>\n \n \n \n 1559707142\n \n \n \n 504497994\n \n \n \n \n \n \n \n 0\n 0\n 0\n \n \n 0\n 0\n \n \n \n \n 840787999741968385\n \n \n 2\n 0\n 0\n \n \n \n \n \n \n \n \n \n 0\n \n \n \n \n \n \n \n 1\n \n \n \n \n \n \n \n', // data: null, // fromUser: 'wxid_2965349653612', // messageId: '1601417885', // messageSource: '\n\t\n\t\t\n\t\t0\n\t\n\t0\n\t3\n\t0\n\n', // messageType: 49, // status: 3, // timestamp: 1559707752, // toUser: 'wxid_x01jgln69ath22', // } // const EXPECTED_PAYLOAD: MessagePayload = { // filename: '1601417885-to-be-implement.txt', // fromId: 'wxid_2965349653612', // id: '1601417885', // mentionIdList: undefined, // roomId: undefined, // text: '\n \n <![CDATA[“演员”孙宇晨]]>\n \n \n 5\n 1\n \n 0\n \n \n \n 0\n \n \n \n \n \n \n \n \n \n 0\n 0\n \n \n \n \n 0\n <![CDATA[“演员”孙宇晨]]>\n \n \n \n 1559707142\n \n \n \n 504497991\n \n \n \n \n \n \n \n 0\n 0\n 0\n \n \n 0\n 0\n \n \n \n \n 840787998215241730\n \n \n 2\n 0\n 0\n \n \n \n 0\n <![CDATA[深创投孙东升:专业化是本土创投转型升级的必由之路]]>\n \n \n \n 1559707142\n \n \n \n 504497993\n \n \n \n \n \n \n \n 0\n 0\n 0\n \n \n 0\n 0\n \n \n \n \n 840787998986993664\n \n \n 2\n 0\n 0\n \n \n \n 0\n <![CDATA[松禾资本厉伟:老老实实做生意]]>\n \n \n \n 1559707142\n \n \n \n 504497994\n \n \n \n \n \n \n \n 0\n 0\n 0\n \n \n 0\n 0\n \n \n \n \n 840787999741968385\n \n \n 2\n 0\n 0\n \n \n \n \n \n \n \n \n \n 0\n \n \n \n \n \n \n \n 1\n \n \n \n \n \n \n \n', // timestamp: 1559707752, // toId: 'wxid_x01jgln69ath22', // type: 12, // } // const payload = await messageRawPayloadParser(MESSAGE_PAYLOAD) // t.deepEqual(payload, EXPECTED_PAYLOAD, 'should parse official account sent url.') // }) // test('Transfer money message', async t => { // const MESSAGE_PAYLOAD = { // content: '<![CDATA[微信转账]]>20001', // data: null, // fromUser: 'lylezhuifeng', // messageId: '1006688402', // messageSource: '', // messageType: 49, // status: 3, // timestamp: 1559715691, // toUser: 'wxid_x01jgln69ath22', // } // const EXPECTED_PAYLOAD: MessagePayload = { // filename: '1006688402-to-be-implement.txt', // fromId: 'lylezhuifeng', // id: '1006688402', // mentionIdList: undefined, // roomId: undefined, // text: '<![CDATA[微信转账]]>20001', // timestamp: 1559715691, // toId: 'wxid_x01jgln69ath22', // type: 10, // } // const payload = await messageRawPayloadParser(MESSAGE_PAYLOAD) // t.deepEqual(payload, EXPECTED_PAYLOAD, 'should parse transfer money message.') // }) // test('Transfer money confirm message', async t => { // const MESSAGE_PAYLOAD = { // content: '\n\n<![CDATA[微信转账]]>\n\n\n2000\n\n\n\n\n\n\n\n3\n\n\n\n\n\n\n\n\n\n\n\n', // data: null, // fromUser: 'wxid_x01jgln69ath22', // messageId: '1601417905', // messageSource: '', // messageType: 49, // status: 3, // timestamp: 1559715714, // toUser: 'lylezhuifeng', // } // const EXPECTED_PAYLOAD: MessagePayload = { // filename: '1601417905-to-be-implement.txt', // fromId: 'wxid_x01jgln69ath22', // id: '1601417905', // mentionIdList: undefined, // roomId: undefined, // text: '\n\n<![CDATA[微信转账]]>\n\n\n2000\n\n\n\n\n\n\n\n3\n\n\n\n\n\n\n\n\n\n\n\n', // timestamp: 1559715714, // toId: 'lylezhuifeng', // type: 10, // } // const payload = await messageRawPayloadParser(MESSAGE_PAYLOAD) // t.deepEqual(payload, EXPECTED_PAYLOAD, 'should parse transfer money confirm message.') // })