puppet-mock.ts 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/**
 *   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.
 *
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
19 20
import * as fs    from 'fs'
import * as path  from 'path'
21 22 23

import {
  ContactQueryFilter,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
24 25
  Gender,
  ContactType,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
26
  ContactPayload,
27 28 29 30

  Puppet,
  PuppetOptions,

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
31
  RoomPayload,
32
  RoomQueryFilter,
33
}                     from '../puppet/'
34 35 36

import {
  log,
37
}                   from '../config'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
38 39
// import Profile      from '../profile'
// import Wechaty      from '../wechaty'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
40 41

import {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
42 43 44 45 46
  Contact,
}                             from '../puppet/contact'
import { FriendRequest }  from '../puppet/friend-request'
import { Room }           from '../puppet/room'

47
import { MockMessage }        from './mock-message'
48 49 50 51 52 53

export type PuppetFoodType = 'scan' | 'ding'
export type ScanFoodType   = 'scan' | 'login' | 'logout'

export class PuppetMock extends Puppet {

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
54
  private user?: Contact
55

56
  constructor(
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
57
    public options: PuppetOptions,
58
  ) {
59
    super(
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
60
      options,
61
      {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
62 63
        Contact:        Contact,
        FriendRequest:  FriendRequest,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
64
        Message:        MockMessage,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
65
        Room:           Room,
66 67
      },
    )
68 69 70 71 72 73 74 75 76 77 78 79
  }

  public toString() {
    return `PuppetMock<${this.options.profile.name}>`
  }

  public ding(data?: any): Promise<string> {
    return data
  }

  public async start(): Promise<void> {
    log.verbose('PuppetMock', `start() with ${this.options.profile}`)
80

81 82 83
    this.state.on('pending')
    // await some tasks...
    this.state.on(true)
84

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
85 86
    const from = Contact.load('xxx_from')
    const to = Contact.load('xxx_to')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
87 88 89 90 91
    const msg = new MockMessage()

    msg.from(from)
    msg.to(to)
    msg.text('mock hello')
92

93
    this.user = to
94 95 96
    this.emit('login', to)

    setInterval(() => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
97 98
      log.verbose('PuppetMock', `start() setInterval() pretending received a new message: ${msg}`)
      this.emit('message', msg)
99 100
    }, 3000)

101 102 103 104 105 106 107 108 109 110
  }

  public async stop(): Promise<void> {
    log.verbose('PuppetMock', 'quit()')

    if (this.state.off()) {
      log.warn('PuppetMock', 'quit() is called on a OFF puppet. await ready(off) and return.')
      await this.state.ready('off')
      return
    }
111

112 113 114 115 116 117
    this.state.off('pending')
    // await some tasks...
    this.state.off(true)
  }

  public logonoff(): boolean {
118
    if (this.user) {
119 120 121 122
      return true
    } else {
      return false
    }
123 124
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
125
  public userSelf(): Contact {
126
    log.verbose('PuppetMock', 'self()')
127 128 129 130 131

    if (!this.user) {
      throw new Error('not logged in, no userSelf yet.')
    }

132
    return this.user
133 134
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
135
  public async forward(message: MockMessage, sendTo: Contact | Room): Promise<void> {
136 137 138 139 140 141 142 143
    log.silly('PuppetMock', 'forward() to: %s, message: %s)',
                            sendTo, message.filename(),
                            // patchData.ToUserName,
                            // patchData.MMActualContent,
              )
  }

  public async send(message: MockMessage): Promise<void> {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
144 145
    log.verbose('PuppetMock', 'send(%s)', message)
    // TODO
146 147 148 149 150 151 152
  }

  public async say(text: string): Promise<void> {
    if (!this.logonoff()) {
      throw new Error('can not say before login')
    }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
153 154
    const msg = new MockMessage()
    msg.puppet = this
155

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
156 157 158 159 160
    msg.from(this.userSelf())
    msg.to(this.userSelf())
    msg.text(text)

    await this.send(msg)
161 162 163 164
  }

  public async logout(): Promise<void> {
    log.verbose('PuppetMock', 'logout()')
165

166 167
    if (!this.logonoff()) {
      throw new Error('logout before login?')
168 169
    }

170
    this.emit('logout', this.user!) // becore we will throw above by logonoff() when this.user===undefined
171 172 173
    this.user = undefined
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
174 175
  public contactAlias(contact: Contact)                      : Promise<string>
  public contactAlias(contact: Contact, alias: string | null): Promise<void>
176

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
177
  public async contactAlias(contact: Contact, alias?: string|null): Promise<void | string> {
178 179 180 181 182 183
    if (typeof alias === 'undefined') {
      return 'mock alias'
    }
    return
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
184
  public async contactFindAll(query: ContactQueryFilter): Promise<Contact[]> {
185 186 187
    return []
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
188
  public async contactAvatar(contact: Contact): Promise<NodeJS.ReadableStream> {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
189 190 191 192
    const WECHATY_ICON_PNG = path.resolve('../../docs/images/wechaty-icon.png')
    return fs.createReadStream(WECHATY_ICON_PNG)
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
193
  public async contactPayload(contact: Contact): Promise<ContactPayload> {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
194 195 196 197 198 199 200
    return {
      gender: Gender.UNKNOWN,
      type:   ContactType.UNKNOWN,
    }

  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
201
  public async roomPayload(room: Room): Promise<RoomPayload> {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
202 203 204 205 206 207 208 209 210
    return {
      topic          : 'mock topic',
      memberList     : [],
      nameMap        : {} as any,
      roomAliasMap   : {} as any,
      contactAliasMap: {} as any,
    }
  }

211 212
  public async roomFindAll(
    query: RoomQueryFilter = { topic: /.*/ },
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
213
  ): Promise<Room[]> {
214 215 216 217
    return []
  }

  public async roomDel(
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
218 219
    room: Room,
    contact: Contact,
220 221 222 223 224
  ): Promise<void> {
    //
  }

  public async roomAdd(
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
225 226
    room: Room,
    contact: Contact,
227 228 229 230
  ): Promise<void> {
    //
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
231
  public async roomTopic(room: Room, topic?: string): Promise<void | string> {
232 233 234 235 236 237
    if (typeof topic === 'undefined') {
      return 'mock room topic'
    }
    return
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
238
  public async roomCreate(contactList: Contact[], topic: string): Promise<Room> {
239 240 241
    if (!contactList || ! contactList.map) {
      throw new Error('contactList not found')
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
242
    const r = Room.load('mock room id') as Room
243 244 245 246
    r.puppet = this
    return r
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
247 248 249 250 251
  public async roomQuit(room: Room): Promise<void> {
    //
  }

  public async friendRequestSend(contact: Contact, hello: string): Promise<void> {
252 253 254
    //
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
255
  public async friendRequestAccept(contact: Contact, ticket: string): Promise<void> {
256 257 258 259 260 261
    //
  }

}

export default PuppetMock