#!/usr/bin/env ts-node // tslint:disable:no-shadowed-variable import test from 'blue-tape' import { FileBox, } from 'file-box' import { MemoryCard, } from 'memory-card' import { ContactPayload, ContactQueryFilter, ContactPayloadFilterFunction, ContactType, ContactGender, // ContactPayloadFilterFactory, } from '../puppet/schemas/contact' import { FriendRequestPayload, } from '../puppet/schemas/friend-request' import { MessagePayload, } from '../puppet/schemas/message' import { RoomMemberPayload, RoomPayload, RoomPayloadFilterFunction, RoomQueryFilter, } from '../puppet/schemas/room' import { Receiver, } from '../puppet/schemas/puppet' import { Puppet, } from '../puppet/puppet' class PuppetTest extends Puppet { public async start() : Promise { return {} as any } public async stop() : Promise { return {} as any } public async ding(data?: any) : Promise { return {data} as any } public async logout(): Promise { return {} as any } /** * * Contact * */ public async contactAlias(contactId: string) : Promise public async contactAlias(contactId: string, alias: string | null) : Promise public async contactAlias(contactId: string, alias?: string|null) : Promise { return {contactId, alias} as any } public async contactAvatar(contactId: string) : Promise public async contactAvatar(contactId: string, file: FileBox) : Promise public async contactAvatar(contactId: string, file?: FileBox) : Promise { return {contactId, file} as any } public async contactList() : Promise { return {} as any } public async contactQrCode(contactId: string) : Promise { return {contactId} as any } public async contactRawPayload(id: string) : Promise { return {id} as any } public async contactRawPayloadParser(rawPayload: any) : Promise { return {rawPayload} as any } /** * * FriendRequest * */ public async friendRequestRawPayload(id: string) : Promise { return {id} as any } public async friendRequestRawPayloadParser(rawPayload: any) : Promise { return rawPayload } public async friendRequestSend(contactId: string, hello?: string) : Promise { return {contactId, hello} as any } public async friendRequestAccept(contactId: string, ticket: string) : Promise { return {contactId, ticket} as any } /** * * Message * */ public async messageFile(messageId: string) : Promise { return {messageId} as any } public async messageForward(to: Receiver, messageId: string) : Promise { return {to, messageId} as any } public async messageSendText(to: Receiver, text: string) : Promise { return {to, text} as any } public async messageSendFile(to: Receiver, file: FileBox) : Promise { return {to, file} as any } public async messageRawPayload(id: string) : Promise { return {id} as any } public async messageRawPayloadParser(rawPayload: any) : Promise { return {rawPayload} as any } /** * * Room * */ public async roomAnnounce(roomId: string) : Promise public async roomAnnounce(roomId: string, text: string) : Promise public async roomAnnounce(roomId: string, text?: string) : Promise { return {roomId, text} as any } public async roomAdd(roomId: string, contactId: string) : Promise { return {roomId, contactId} as any } public async roomAvatar(roomId: string) : Promise { return {roomId} as any } public async roomCreate(contactIdList: string[], topic?: string) : Promise { return {contactIdList, topic} as any } public async roomDel(roomId: string, contactId: string) : Promise { return {roomId, contactId} as any } public async roomQuit(roomId: string) : Promise { return {roomId} as any } public async roomQrCode(roomId: string) : Promise { return {roomId} as any } public async roomTopic(roomId: string) : Promise public async roomTopic(roomId: string, topic: string) : Promise public async roomTopic(roomId: string, topic?: string) : Promise { return {roomId, topic} as any } public async roomList() : Promise { return {} as any } public async roomMemberList(roomId: string) : Promise { return {roomId} as any } public async roomRawPayload(id: string) : Promise { return {id} as any } public async roomRawPayloadParser(rawPayload: any) : Promise { return {rawPayload} as any } public async roomMemberRawPayload(roomId: string, contactId: string) : Promise { return {roomId, contactId} as any} public async roomMemberRawPayloadParser(rawPayload: any) : Promise { return rawPayload as any} /** * expose to public for internal methods: */ public roomQueryFilterFactory( query: RoomQueryFilter, ): RoomPayloadFilterFunction { return super.roomQueryFilterFactory(query) } public contactQueryFilterFactory( query: ContactQueryFilter, ): ContactPayloadFilterFunction { return super.contactQueryFilterFactory(query) } } test('contactQueryFilterFunction()', async t => { const TEXT_REGEX = 'query by regex' const TEXT_TEXT = 'query by text' const PAYLOAD_LIST: ContactPayload[] = [ { id : 'id1', gender : ContactGender.Unknown, type : ContactType.Personal, name : TEXT_REGEX, alias : TEXT_TEXT, }, { id : 'id2', gender : ContactGender.Unknown, type : ContactType.Personal, name : TEXT_TEXT, alias : TEXT_REGEX, }, { id : 'id3', gender : ContactGender.Unknown, type : ContactType.Personal, name : TEXT_REGEX, alias : TEXT_TEXT, }, { id : 'id4', gender : ContactGender.Unknown, type : ContactType.Personal, name : TEXT_TEXT, alias : TEXT_REGEX, }, ] const REGEX_VALUE = new RegExp(TEXT_REGEX) const TEXT_VALUE = TEXT_TEXT const puppet = new PuppetTest({ memory: new MemoryCard }) t.test('filter name by regex', async t => { const QUERY = { name: REGEX_VALUE } const ID_LIST = ['id1', 'id3'] const func = puppet.contactQueryFilterFactory(QUERY) const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id) t.deepEqual(idList, ID_LIST, 'should filter the query to id list') }) t.test('filter name by text', async t => { const QUERY = { name: TEXT_VALUE } const ID_LIST = ['id2', 'id4'] const func = puppet.contactQueryFilterFactory(QUERY) const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id) t.deepEqual(idList, ID_LIST, 'should filter query to id list') }) t.test('filter alias by regex', async t => { const QUERY = { alias: REGEX_VALUE } const ID_LIST = ['id2', 'id4'] const func = puppet.contactQueryFilterFactory(QUERY) const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id) t.deepEqual(idList, ID_LIST, 'should filter query to id list') }) t.test('filter alias by text', async t => { const QUERY = { alias: TEXT_VALUE } const ID_LIST = ['id1', 'id3'] const func = puppet.contactQueryFilterFactory(QUERY) const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id) t.deepEqual(idList, ID_LIST, 'should filter query to id list') }) t.test('throw if filter key unknown', async t => { t.throws(() => puppet.contactQueryFilterFactory({ xxxx: 'test' } as any), 'should throw') }) t.test('throw if filter key are more than one', async t => { t.throws(() => puppet.contactQueryFilterFactory({ name: 'test', alias: 'test', }), 'should throw') }) }) test('roomQueryFilterFunction()', async t => { const TEXT_REGEX = 'query by regex' const TEXT_TEXT = 'query by text' const PAYLOAD_LIST: RoomPayload[] = [ { id : 'id1', topic : TEXT_TEXT, }, { id : 'id2', topic : TEXT_REGEX, }, { id : 'id3', topic : TEXT_TEXT, }, { id : 'id4', topic : TEXT_REGEX, }, ] const REGEX_VALUE = new RegExp(TEXT_REGEX) const TEXT_VALUE = TEXT_TEXT const puppet = new PuppetTest({ memory: new MemoryCard() }) t.test('filter name by regex', async t => { const QUERY = { topic: REGEX_VALUE } const ID_LIST = ['id2', 'id4'] const func = puppet.roomQueryFilterFactory(QUERY) const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id) t.deepEqual(idList, ID_LIST, 'should filter the query to id list') }) t.test('filter name by text', async t => { const QUERY = { topic: TEXT_VALUE } const ID_LIST = ['id1', 'id3'] const func = puppet.roomQueryFilterFactory(QUERY) const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id) t.deepEqual(idList, ID_LIST, 'should filter query to id list') }) t.test('throw if filter key unknown', async t => { t.throws(() => puppet.roomQueryFilterFactory({ xxx: 'test' } as any), 'should throw') }) t.test('throw if filter key are more than one', async t => { t.throws(() => puppet.roomQueryFilterFactory({ topic: 'test', alias: 'test', } as any), 'should throw') }) })