提交 75a4c706 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

clean PuppetMock

上级 9774604b
/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2018 Huan LI <zixia@zixia.net>
*
* 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<void>
public async say(message: MockMessage): Promise<void>
public async say(
textOrMessage : string | MockMessage,
): Promise<void> {
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<void>
public alias(empty: null) : Promise<void>
public alias(newAlias?: string|null): Promise<void> | 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<NodeJS.ReadableStream> {
log.verbose('MockContact', 'avatar()')
return this.puppet.contactAvatar(this)
}
public isReady(): boolean {
return this.payload !== undefined
}
/**
* @deprecated use sync() instead
*/
public async refresh(): Promise<void> {
log.verbose('MockContact', 'refresh() DEPRECATED use sync instead')
return this.sync()
}
public async sync(): Promise<void> {
log.verbose('MockContact', 'sync()')
this.payload = undefined
await this.ready()
}
public async ready(): Promise<void> {
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
/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2018 Huan LI <zixia@zixia.net>
*
* 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
......@@ -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<path.ParsedPath>
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<void>
public async say(text: string, mention?: Contact | Contact[]): Promise<void>
public async say(message: MockMessage): Promise<void>
public async say(
textOrMessage: string | MockMessage,
mention?: MockContact | MockContact[],
mention?: Contact | Contact[],
): Promise<void> {
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<void> {
public async forward(to: Room | Contact): Promise<void> {
/**
* 1. Text message
*/
......
/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2018 Huan LI <zixia@zixia.net>
*
* 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<void> {
return
}
public async ready(): Promise<void> {
return
}
public say(message: MockMessage) : Promise<void>
public say(text: string) : Promise<void>
public say(text: string, replyTo: MockContact) : Promise<void>
public say(text: string, replyTo: MockContact[]) : Promise<void>
public say(text: never, ...args: never[]) : Promise<never>
public async say(textOrMessage: string | MockMessage, replyTo?: MockContact|MockContact[]): Promise<void> {
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<void> {
log.verbose('MockRoom', 'add(%s)', contact)
await this.puppet.roomAdd(this, contact)
}
public async del(contact: MockContact): Promise<void> {
log.verbose('MockRoom', 'del(%s)', contact.name())
await this.puppet.roomDel(this, contact)
}
public async quit(): Promise<void> {
return
}
public topic() : string
public async topic(newTopic: string): Promise<void>
public topic(newTopic?: string): string | Promise<void> {
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<Room> {
log.verbose('MockRoom', 'create(%s, %s)', contactList.join(','), topic)
const room = await this.puppet.roomCreate(contactList, topic)
return room
}
public async refresh(): Promise<void> {
return
}
public owner(): MockContact | null {
log.info('MockRoom', 'owner()')
return null
}
}
export default MockRoom
......@@ -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<void> {
public async forward(message: MockMessage, sendTo: Contact | Room): Promise<void> {
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<string>
public contactAlias(contact: MockContact, alias: string | null): Promise<void>
public contactAlias(contact: Contact) : Promise<string>
public contactAlias(contact: Contact, alias: string | null): Promise<void>
public async contactAlias(contact: MockContact, alias?: string|null): Promise<void | string> {
public async contactAlias(contact: Contact, alias?: string|null): Promise<void | string> {
if (typeof alias === 'undefined') {
return 'mock alias'
}
return
}
public async contactFindAll(query: ContactQueryFilter): Promise<MockContact[]> {
public async contactFindAll(query: ContactQueryFilter): Promise<Contact[]> {
return []
}
public async contactAvatar(contact: MockContact): Promise<NodeJS.ReadableStream> {
public async contactAvatar(contact: Contact): Promise<NodeJS.ReadableStream> {
const WECHATY_ICON_PNG = path.resolve('../../docs/images/wechaty-icon.png')
return fs.createReadStream(WECHATY_ICON_PNG)
}
public async contactPayload(contact: MockContact): Promise<ContactPayload> {
public async contactPayload(contact: Contact): Promise<ContactPayload> {
return {
gender: Gender.UNKNOWN,
type: ContactType.UNKNOWN,
......@@ -197,7 +198,7 @@ export class PuppetMock extends Puppet {
}
public async roomPayload(room: MockRoom): Promise<RoomPayload> {
public async roomPayload(room: Room): Promise<RoomPayload> {
return {
topic : 'mock topic',
memberList : [],
......@@ -209,45 +210,49 @@ export class PuppetMock extends Puppet {
public async roomFindAll(
query: RoomQueryFilter = { topic: /.*/ },
): Promise<MockRoom[]> {
): Promise<Room[]> {
return []
}
public async roomDel(
room: MockRoom,
contact: MockContact,
room: Room,
contact: Contact,
): Promise<void> {
//
}
public async roomAdd(
room: MockRoom,
contact: MockContact,
room: Room,
contact: Contact,
): Promise<void> {
//
}
public async roomTopic(room: MockRoom, topic?: string): Promise<void | string> {
public async roomTopic(room: Room, topic?: string): Promise<void | string> {
if (typeof topic === 'undefined') {
return 'mock room topic'
}
return
}
public async roomCreate(contactList: MockContact[], topic: string): Promise<MockRoom> {
public async roomCreate(contactList: Contact[], topic: string): Promise<Room> {
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<void> {
public async roomQuit(room: Room): Promise<void> {
//
}
public async friendRequestSend(contact: Contact, hello: string): Promise<void> {
//
}
public async friendRequestAccept(contact: MockContact, ticket: string): Promise<void> {
public async friendRequestAccept(contact: Contact, ticket: string): Promise<void> {
//
}
......
......@@ -240,31 +240,24 @@ export class Contact extends PuppetAccessory implements Sayable {
public async say(textOrMessage: string | Message): Promise<void> {
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)
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册