From 75a4c7066704286e3173e65de946ffafd494a438 Mon Sep 17 00:00:00 2001 From: Huan LI Date: Fri, 11 May 2018 03:33:39 +0800 Subject: [PATCH] clean PuppetMock --- src/puppet-mock/mock-contact.ts | 219 ------------------------- src/puppet-mock/mock-friend-request.ts | 24 --- src/puppet-mock/mock-message.ts | 38 ++--- src/puppet-mock/mock-room.ts | 157 ------------------ src/puppet-mock/puppet-mock.ts | 63 +++---- src/puppet/contact.ts | 31 ++-- 6 files changed, 65 insertions(+), 467 deletions(-) delete mode 100644 src/puppet-mock/mock-contact.ts delete mode 100644 src/puppet-mock/mock-friend-request.ts delete mode 100644 src/puppet-mock/mock-room.ts diff --git a/src/puppet-mock/mock-contact.ts b/src/puppet-mock/mock-contact.ts deleted file mode 100644 index 3dd5abf5..00000000 --- a/src/puppet-mock/mock-contact.ts +++ /dev/null @@ -1,219 +0,0 @@ -/** - * Wechaty - https://github.com/chatie/wechaty - * - * @copyright 2016-2018 Huan LI - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * @ignore - */ - -import { - Sayable, - log, -} from '../config' -import { - Contact, - ContactType, - Gender, - Message, -} from '../puppet/' - -import { MockMessage } from './mock-message' - -export class MockContact extends Contact implements Sayable { - - constructor( - public readonly id: string, - ) { - super(id) - log.silly('MockContact', `constructor(${id})`) - } - - public toString(): string { - return `MockContact<${this.id}>` - } - - public async say(text: string) : Promise - public async say(message: MockMessage): Promise - - public async say( - textOrMessage : string | MockMessage, - ): Promise { - log.verbose('MockContact', 'say(%s)', textOrMessage) - - let msg - - if (textOrMessage instanceof Message) { - msg = textOrMessage - } else { - msg = new MockMessage() - msg.text(textOrMessage) - } - - msg.from(this.puppet.userSelf()) - msg.to(this) - - await this.puppet.send(msg) - } - - public name(): string { - if (!this.payload) { - throw new Error('no payload') - } - return this.payload.name || '' - } - - public alias() : string | null - public alias(newAlias: string): Promise - public alias(empty: null) : Promise - - public alias(newAlias?: string|null): Promise | string | null { - log.verbose('MockContact', 'alias(%s)', newAlias) - if (newAlias === undefined) { - return this.payload && this.payload.alias || null - } - - return this.puppet.contactAlias(this, newAlias) - } - - /** - * @deprecated - */ - public stranger(): boolean | null { - if (!this.payload) { - return null - } - - return this.payload.friend === undefined - ? null - : !(this.payload.friend) - } - - public friend(): boolean | null { - if (!this.payload) { - return null - } - - return this.payload.friend === undefined - ? null - : this.payload.friend - } - - /** - * @deprecated - */ - public personal(): boolean { - if (this.type() === ContactType.UNKNOWN) { - throw new Error('unknown type') - } - return this.type() === Contact.Type.PERSONAL - } - - /** - * @deprecated - */ - public official(): boolean { - if (this.type() === ContactType.UNKNOWN) { - throw new Error('unknown type') - } - return this.type() === Contact.Type.OFFICIAL - } - - public type(): ContactType { - if (!this.payload) { - return ContactType.UNKNOWN - } - return this.payload.type - } - - public star(): boolean | null { - if (!this.payload) { - return null - } - return this.payload.star === undefined - ? null - : this.payload.star - } - - public gender(): Gender { - if (!this.payload) { - return Gender.UNKNOWN - } - return this.payload.gender - } - - public province(): string | null { - if (!this.payload) { - return null - } - return this.payload.province === undefined - ? null - : this.payload.province - } - - public city(): string | null { - if (!this.payload) { - return null - } - return this.payload.city === undefined - ? null - : this.payload.city - } - - public async avatar(): Promise { - log.verbose('MockContact', 'avatar()') - - return this.puppet.contactAvatar(this) - } - - public isReady(): boolean { - return this.payload !== undefined - } - - /** - * @deprecated use sync() instead - */ - public async refresh(): Promise { - log.verbose('MockContact', 'refresh() DEPRECATED use sync instead') - return this.sync() - } - - public async sync(): Promise { - log.verbose('MockContact', 'sync()') - this.payload = undefined - await this.ready() - } - - public async ready(): Promise { - log.verbose('MockContact', 'ready()') - if (!this.payload) { - this.payload = await this.puppet.contactPayload(this) - } - } - - public self(): boolean { - const userSelf = this.puppet.userSelf() - return userSelf.id === this.id - } - - public weixin(): string | null { - if (!this.payload) { - throw new Error('no payload') - } - return this.payload.weixin || null - } - -} - -export default MockContact diff --git a/src/puppet-mock/mock-friend-request.ts b/src/puppet-mock/mock-friend-request.ts deleted file mode 100644 index 652cbdea..00000000 --- a/src/puppet-mock/mock-friend-request.ts +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Wechaty - https://github.com/chatie/wechaty - * - * @copyright 2016-2018 Huan LI - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * @ignore - */ - -import { FriendRequest } from '../puppet/' - -export class MockFriendRequest extends FriendRequest {} - -export default MockFriendRequest diff --git a/src/puppet-mock/mock-message.ts b/src/puppet-mock/mock-message.ts index 39906123..8a844fab 100644 --- a/src/puppet-mock/mock-message.ts +++ b/src/puppet-mock/mock-message.ts @@ -24,10 +24,10 @@ import { import { log, } from '../config' -import { Message } from '../puppet/' +import { Message } from '../puppet/message' -import { MockContact } from './mock-contact' -import { MockRoom } from './mock-room' +import { Contact } from '../puppet/contact' +import { Room } from '../puppet/room' import { WebMsgType, @@ -38,9 +38,9 @@ export type ParsedPath = Partial export interface MockMessagePayload { text: string, - from: MockContact, - to?: MockContact, - room?: MockRoom, + from: Contact, + to?: Contact, + room?: Room, type: WebMsgType, } @@ -63,10 +63,10 @@ export class MockMessage extends Message { this.payload = {} as MockMessagePayload } - public from(contact: MockContact) : void - public from() : MockContact + public from(contact: Contact) : void + public from() : Contact - public from(contact?: MockContact): void | MockContact { + public from(contact?: Contact): void | Contact { if (contact) { this.payload.from = contact return @@ -75,10 +75,10 @@ export class MockMessage extends Message { return this.payload.from } - public to(contact: MockContact) : void - public to() : MockContact | null // if no `to` there must be a `room` + public to(contact: Contact) : void + public to() : Contact | null // if no `to` there must be a `room` - public to(contact?: MockContact): void | MockContact | null { + public to(contact?: Contact): void | Contact | null { if (contact) { this.payload.to = contact return @@ -87,10 +87,10 @@ export class MockMessage extends Message { return this.payload.to || null } - public room(room: MockRoom) : void - public room() : null | MockRoom + public room(room: Room) : void + public room() : null | Room - public room(room?: MockRoom): void | null | MockRoom { + public room(room?: Room): void | null | Room { if (room) { this.payload.room = room return @@ -108,12 +108,12 @@ export class MockMessage extends Message { return this.payload.text || '' } - public async say(text: string, mention?: MockContact | MockContact[]): Promise + public async say(text: string, mention?: Contact | Contact[]): Promise public async say(message: MockMessage): Promise public async say( textOrMessage: string | MockMessage, - mention?: MockContact | MockContact[], + mention?: Contact | Contact[], ): Promise { log.verbose('MockMessage', 'say(%s, %s)', textOrMessage, mention) @@ -148,7 +148,7 @@ export class MockMessage extends Message { return fromId === userId } - public mentioned(): MockContact[] { + public mentioned(): Contact[] { return [] } @@ -180,7 +180,7 @@ export class MockMessage extends Message { return 'text/plain' } - public async forward(to: MockRoom | MockContact): Promise { + public async forward(to: Room | Contact): Promise { /** * 1. Text message */ diff --git a/src/puppet-mock/mock-room.ts b/src/puppet-mock/mock-room.ts deleted file mode 100644 index 3574be9d..00000000 --- a/src/puppet-mock/mock-room.ts +++ /dev/null @@ -1,157 +0,0 @@ -/** - * Wechaty - https://github.com/chatie/wechaty - * - * @copyright 2016-2018 Huan LI - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * @ignore - */ -import { - log, -} from '../config' -import { - Room, - RoomMemberQueryFilter, -} from '../puppet/' - -import { MockMessage } from './mock-message' -import { MockContact } from './mock-contact' - -export class MockRoom extends Room { - - constructor( - public id: string, - ) { - super(id) - log.silly('MockRoom', `constructor(${id})`) - } - - public isReady(): boolean { - return true - } - - public async sync(): Promise { - return - } - - public async ready(): Promise { - return - } - - public say(message: MockMessage) : Promise - public say(text: string) : Promise - public say(text: string, replyTo: MockContact) : Promise - public say(text: string, replyTo: MockContact[]) : Promise - public say(text: never, ...args: never[]) : Promise - - public async say(textOrMessage: string | MockMessage, replyTo?: MockContact|MockContact[]): Promise { - log.verbose('MockRoom', 'say(%s, %s)', - textOrMessage, - Array.isArray(replyTo) - ? replyTo.map(c => c.name()).join(', ') - : replyTo ? replyTo.name() : '', - ) - - let m - m = new MockMessage() - m.puppet = this.puppet - m.room(this) - - await this.puppet.send(m) - } - - public async add(contact: MockContact): Promise { - log.verbose('MockRoom', 'add(%s)', contact) - await this.puppet.roomAdd(this, contact) - } - - public async del(contact: MockContact): Promise { - log.verbose('MockRoom', 'del(%s)', contact.name()) - await this.puppet.roomDel(this, contact) - } - - public async quit(): Promise { - return - } - - public topic() : string - public async topic(newTopic: string): Promise - - public topic(newTopic?: string): string | Promise { - log.verbose('MockRoom', 'topic(%s)', newTopic ? newTopic : '') - - if (typeof newTopic === 'undefined') { - return 'mock topic' - } - - this.puppet.roomTopic(this, newTopic) - .catch(e => { - log.warn('MockRoom', 'topic(newTopic=%s) exception: %s', - newTopic, e && e.message || e, - ) - }) - - return Promise.resolve() - } - - public alias(contact: MockContact): string | null { - return this.roomAlias(contact) - } - - public roomAlias(contact: MockContact): string | null { - return 'mock room alias' - } - - public has(contact: MockContact): boolean { - return false - } - - public memberAll(filter: RoomMemberQueryFilter) : MockContact[] - public memberAll(name: string) : MockContact[] - - public memberAll(queryArg: RoomMemberQueryFilter | string): MockContact[] { - return [] - } - - public member(name: string) : MockContact | null - public member(filter: RoomMemberQueryFilter): MockContact | null - - public member(queryArg: RoomMemberQueryFilter | string): MockContact | null { - log.verbose('MockRoom', 'member(%s)', JSON.stringify(queryArg)) - return null - } - - public memberList(): MockContact[] { - log.verbose('MockRoom', 'memberList') - return [] - } - - public static async create(contactList: MockContact[], topic?: string): Promise { - log.verbose('MockRoom', 'create(%s, %s)', contactList.join(','), topic) - const room = await this.puppet.roomCreate(contactList, topic) - return room - } - - public async refresh(): Promise { - return - } - - public owner(): MockContact | null { - log.info('MockRoom', 'owner()') - return null - } - -} - -export default MockRoom diff --git a/src/puppet-mock/puppet-mock.ts b/src/puppet-mock/puppet-mock.ts index f800e368..f75907cb 100644 --- a/src/puppet-mock/puppet-mock.ts +++ b/src/puppet-mock/puppet-mock.ts @@ -39,18 +39,19 @@ import { // import Wechaty from '../wechaty' import { - MockContact, -} from './mock-contact' -import { MockFriendRequest } from './mock-friend-request' + Contact, +} from '../puppet/contact' +import { FriendRequest } from '../puppet/friend-request' +import { Room } from '../puppet/room' + import { MockMessage } from './mock-message' -import { MockRoom } from './mock-room' export type PuppetFoodType = 'scan' | 'ding' export type ScanFoodType = 'scan' | 'login' | 'logout' export class PuppetMock extends Puppet { - private user?: MockContact + private user?: Contact constructor( public options: PuppetOptions, @@ -58,10 +59,10 @@ export class PuppetMock extends Puppet { super( options, { - Contact: MockContact, - FriendRequest: MockFriendRequest, + Contact: Contact, + FriendRequest: FriendRequest, Message: MockMessage, - Room: MockRoom, + Room: Room, }, ) } @@ -81,8 +82,8 @@ export class PuppetMock extends Puppet { // await some tasks... this.state.on(true) - const from = MockContact.load('xxx_from') - const to = MockContact.load('xxx_to') + const from = Contact.load('xxx_from') + const to = Contact.load('xxx_to') const msg = new MockMessage() msg.from(from) @@ -121,7 +122,7 @@ export class PuppetMock extends Puppet { } } - public userSelf(): MockContact { + public userSelf(): Contact { log.verbose('PuppetMock', 'self()') if (!this.user) { @@ -131,7 +132,7 @@ export class PuppetMock extends Puppet { return this.user } - public async forward(message: MockMessage, sendTo: MockContact | MockRoom): Promise { + public async forward(message: MockMessage, sendTo: Contact | Room): Promise { log.silly('PuppetMock', 'forward() to: %s, message: %s)', sendTo, message.filename(), // patchData.ToUserName, @@ -170,26 +171,26 @@ export class PuppetMock extends Puppet { this.user = undefined } - public contactAlias(contact: MockContact) : Promise - public contactAlias(contact: MockContact, alias: string | null): Promise + public contactAlias(contact: Contact) : Promise + public contactAlias(contact: Contact, alias: string | null): Promise - public async contactAlias(contact: MockContact, alias?: string|null): Promise { + public async contactAlias(contact: Contact, alias?: string|null): Promise { if (typeof alias === 'undefined') { return 'mock alias' } return } - public async contactFindAll(query: ContactQueryFilter): Promise { + public async contactFindAll(query: ContactQueryFilter): Promise { return [] } - public async contactAvatar(contact: MockContact): Promise { + public async contactAvatar(contact: Contact): Promise { const WECHATY_ICON_PNG = path.resolve('../../docs/images/wechaty-icon.png') return fs.createReadStream(WECHATY_ICON_PNG) } - public async contactPayload(contact: MockContact): Promise { + public async contactPayload(contact: Contact): Promise { return { gender: Gender.UNKNOWN, type: ContactType.UNKNOWN, @@ -197,7 +198,7 @@ export class PuppetMock extends Puppet { } - public async roomPayload(room: MockRoom): Promise { + public async roomPayload(room: Room): Promise { return { topic : 'mock topic', memberList : [], @@ -209,45 +210,49 @@ export class PuppetMock extends Puppet { public async roomFindAll( query: RoomQueryFilter = { topic: /.*/ }, - ): Promise { + ): Promise { return [] } public async roomDel( - room: MockRoom, - contact: MockContact, + room: Room, + contact: Contact, ): Promise { // } public async roomAdd( - room: MockRoom, - contact: MockContact, + room: Room, + contact: Contact, ): Promise { // } - public async roomTopic(room: MockRoom, topic?: string): Promise { + public async roomTopic(room: Room, topic?: string): Promise { if (typeof topic === 'undefined') { return 'mock room topic' } return } - public async roomCreate(contactList: MockContact[], topic: string): Promise { + public async roomCreate(contactList: Contact[], topic: string): Promise { if (!contactList || ! contactList.map) { throw new Error('contactList not found') } - const r = MockRoom.load('mock room id') as MockRoom + const r = Room.load('mock room id') as Room r.puppet = this return r } - public async friendRequestSend(contact: MockContact, hello: string): Promise { + public async roomQuit(room: Room): Promise { + // + } + + public async friendRequestSend(contact: Contact, hello: string): Promise { // } - public async friendRequestAccept(contact: MockContact, ticket: string): Promise { + public async friendRequestAccept(contact: Contact, ticket: string): Promise { // } diff --git a/src/puppet/contact.ts b/src/puppet/contact.ts index afc5e46c..75fff3d0 100644 --- a/src/puppet/contact.ts +++ b/src/puppet/contact.ts @@ -240,31 +240,24 @@ export class Contact extends PuppetAccessory implements Sayable { public async say(textOrMessage: string | Message): Promise { log.verbose('Contact', 'say(%s)', textOrMessage) - const user = this.puppet.userSelf() as Contact - - if (!user) { - throw new Error('no user') - } - - let m - if (typeof textOrMessage === 'string') { - m = new PuppeteerMessage() - m.puppet = this.puppet - m.text(textOrMessage) - } else if (textOrMessage instanceof Message) { - m = textOrMessage + let msg + if (textOrMessage instanceof Message) { + msg = textOrMessage } else { - throw new Error('not support args') + msg = new PuppeteerMessage() + msg.puppet = this.puppet + msg.text(textOrMessage) } - m.from(user) - m.to(this) + + msg.from(this.puppet.userSelf()) + msg.to(this) log.silly('Contact', 'say() from: %s to: %s content: %s', - user, + this.puppet.userSelf(), this, - textOrMessage, + msg, ) - await this.puppet.send(m) + await this.puppet.send(msg) } /** -- GitLab