puppet-mock.ts 8.7 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
import * as path  from 'path'
20 21

import {
22 23 24 25 26
  FileBox,
}             from 'file-box'

import {
  MessagePayload,
27 28 29

  // ContactQueryFilter,
  ContactGender,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
30
  ContactType,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
31
  ContactPayload,
32

33 34
  FriendRequestPayload,

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
35
  RoomPayload,
36 37
  // RoomQueryFilter,
}                       from '../puppet/'
38 39 40
import {
  Puppet,
  PuppetOptions,
41
  Receiver,
42
  MessageType,
43
}                       from '../puppet/'
44 45 46

import {
  log,
47
  qrCodeForChatie,
48
}                       from '../config'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
49

50 51 52
export type PuppetFoodType = 'scan' | 'ding'
export type ScanFoodType   = 'scan' | 'login' | 'logout'

53 54 55 56 57
export interface MockContactRawPayload {
  name : string,
}

export interface MockMessageRawPayload {
58
  id   : string,
59 60 61 62 63 64
  from : string,
  to   : string,
  text : string
}

export interface MockRoomRawPayload {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
65 66 67
  topic      : string,
  memberList : string[],
  ownerId    : string,
68 69
}

70 71 72
export class PuppetMock extends Puppet {

  constructor(
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
73
    public options: PuppetOptions,
74
  ) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
75
    super(options)
76 77 78
  }

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

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

85
    this.id = 'logined_user_id'
86 87 88 89 90 91 92 93 94
    // const user = this.Contact.load(this.id)
    this.emit('login', this.id)

    const MOCK_MSG_ID = 'mockid'
    this.cacheMessagePayload.set(MOCK_MSG_ID, {
      id        : MOCK_MSG_ID,
      type      : MessageType.Text,
      text      : 'mock text',
      timestamp : Date.now(),
95 96
      fromId    : 'xxx',
      toId      : 'xxx',
97
    })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
98

99
    setInterval(() => {
100 101
      log.verbose('PuppetMock', `start() setInterval() pretending received a new message: ${MOCK_MSG_ID}`)
      this.emit('message', MOCK_MSG_ID)
102 103
    }, 3000)

104 105 106 107 108 109 110 111 112 113
  }

  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
    }
114

115 116 117 118 119 120 121
    this.state.off('pending')
    // await some tasks...
    this.state.off(true)
  }

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

123
    if (!this.id) {
124
      throw new Error('logout before login?')
125 126
    }

127 128
    this.emit('logout', this.id) // becore we will throw above by logonoff() when this.user===undefined
    this.id = undefined
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
129 130

    // TODO: do the logout job
131 132
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
133 134 135 136 137
  /**
   *
   * Contact
   *
   */
138 139
  public contactAlias(contactId: string)                      : Promise<string>
  public contactAlias(contactId: string, alias: string | null): Promise<void>
140

141 142
  public async contactAlias(contactId: string, alias?: string|null): Promise<void | string> {
    log.verbose('PuppetMock', 'contactAlias(%s, %s)', contactId, alias)
143

144 145 146 147 148 149
    if (typeof alias === 'undefined') {
      return 'mock alias'
    }
    return
  }

150 151
  public async contactList(): Promise<string[]> {
    log.verbose('PuppetMock', 'contactList()')
152

153 154 155
    return []
  }

156 157
  public async contactAvatar(contactId: string): Promise<FileBox> {
    log.verbose('PuppetMock', 'contactAvatar(%s)', contactId)
158

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
159
    const WECHATY_ICON_PNG = path.resolve('../../docs/images/wechaty-icon.png')
160
    return FileBox.fromFile(WECHATY_ICON_PNG)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
161 162
  }

163 164 165 166 167 168 169 170 171 172
  public async contactRawPayload(id: string): Promise<MockContactRawPayload> {
    log.verbose('PuppetMock', 'contactRawPayload(%s)', id)
    const rawPayload: MockContactRawPayload = {
      name : 'mock name',
    }
    return rawPayload
  }

  public async contactRawPayloadParser(rawPayload: MockContactRawPayload): Promise<ContactPayload> {
    log.verbose('PuppetMock', 'contactRawPayloadParser(%s)', rawPayload)
173

174
    const payload: ContactPayload = {
175 176 177
      id     : 'id',
      gender : ContactGender.Unknown,
      type   : ContactType.Unknown,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
178
    }
179
    return payload
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
180 181
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
182 183 184 185 186
  /**
   *
   * Message
   *
   */
187
  public async messageFile(id: string): Promise<FileBox> {
188
    return FileBox.fromBase64(
189
      'cRH9qeL3XyVnaXJkppBuH20tf5JlcG9uFX1lL2IvdHRRRS9kMMQxOPLKNYIzQQ==',
190
      'mock-file' + id + '.txt',
191 192 193
    )
  }

194 195 196
  public async messageRawPayload(id: string): Promise<MockMessageRawPayload> {
    log.verbose('PuppetMock', 'messageRawPayload(%s)', id)
    const rawPayload: MockMessageRawPayload = {
197
      id   : 'id',
198 199 200 201 202 203 204 205 206
      from : 'from_id',
      text : 'mock message text',
      to   : 'to_id',
    }
    return rawPayload
  }

  public async messageRawPayloadParser(rawPayload: MockMessageRawPayload): Promise<MessagePayload> {
    log.verbose('PuppetMock', 'messagePayload(%s)', rawPayload)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
207
    const payload: MessagePayload = {
208
      id        : rawPayload.id,
209
      timestamp : Date.now(),
210
      fromId    : 'xxx',
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
211
      text      : 'mock message text',
212
      toId      : this.selfId(),
213
      type      : MessageType.Text,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
214 215 216 217
    }
    return payload
  }

218 219 220 221 222 223 224 225 226 227 228 229
  public async messageSendText(
    receiver : Receiver,
    text     : string,
  ): Promise<void> {
    log.verbose('PuppetMock', 'messageSend(%s, %s)', receiver, text)
  }

  public async messageSendFile(
    receiver : Receiver,
    file     : FileBox,
  ): Promise<void> {
    log.verbose('PuppetMock', 'messageSend(%s, %s)', receiver, file)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
230 231
  }

232 233 234 235
  public async messageForward(
    receiver  : Receiver,
    messageId : string,
  ): Promise<void> {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
236
    log.verbose('PuppetMock', 'messageForward(%s, %s)',
237 238
                              receiver,
                              messageId,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
239 240 241 242 243 244 245 246
              )
  }

  /**
   *
   * Room
   *
   */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
247 248 249
  public async roomRawPayload(
    id: string,
  ): Promise<MockRoomRawPayload> {
250 251 252
    log.verbose('PuppetMock', 'roomRawPayload(%s)', id)

    const rawPayload: MockRoomRawPayload = {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
253
      ownerId      : 'mock_room_owner_id',
254 255 256 257 258
      topic      : 'mock topic',
      memberList : [],
    }
    return rawPayload
  }
259

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
260 261 262
  public async roomRawPayloadParser(
    rawPayload: MockRoomRawPayload,
  ): Promise<RoomPayload> {
263 264 265
    log.verbose('PuppetMock', 'roomRawPayloadParser(%s)', rawPayload)

    const payload: RoomPayload = {
266 267 268 269
      id           : 'id',
      topic        : 'mock topic',
      memberIdList : [],
      aliasDict    : {},
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
270
    }
271 272

    return payload
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
273 274
  }

275 276
  public async roomList(): Promise<string[]> {
    log.verbose('PuppetMock', 'roomList()')
277

278 279 280 281
    return []
  }

  public async roomDel(
282 283
    roomId    : string,
    contactId : string,
284
  ): Promise<void> {
285
    log.verbose('PuppetMock', 'roomDel(%s, %s)', roomId, contactId)
286 287
  }

288 289 290 291 292 293 294 295 296 297 298 299
  public async roomAvatar(roomId: string): Promise<FileBox> {
    log.verbose('PuppetMock', 'roomAvatar(%s)', roomId)

    const payload = await this.roomPayload(roomId)

    if (payload.avatar) {
      return FileBox.fromUrl(payload.avatar)
    }
    log.warn('PuppetMock', 'roomAvatar() avatar not found, use the chatie default.')
    return qrCodeForChatie()
  }

300
  public async roomAdd(
301 302
    roomId    : string,
    contactId : string,
303
  ): Promise<void> {
304
    log.verbose('PuppetMock', 'roomAdd(%s, %s)', roomId, contactId)
305 306
  }

307 308 309 310 311
  public async roomTopic(
    roomId: string,
    topic?: string,
  ): Promise<void | string> {
    log.verbose('PuppetMock', 'roomTopic(%s, %s)', roomId, topic)
312

313 314 315 316 317 318
    if (typeof topic === 'undefined') {
      return 'mock room topic'
    }
    return
  }

319 320 321 322 323
  public async roomCreate(
    contactIdList : string[],
    topic         : string,
  ): Promise<string> {
    log.verbose('PuppetMock', 'roomCreate(%s, %s)', contactIdList, topic)
324

325
    return 'mock_room_id'
326 327
  }

328 329
  public async roomQuit(roomId: string): Promise<void> {
    log.verbose('PuppetMock', 'roomQuit(%s)', roomId)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
330 331
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
332 333 334 335 336
  /**
   *
   * FriendRequest
   *
   */
337 338 339 340 341 342 343
  public async friendRequestRawPayload(id: string)            : Promise<any> {
    return {id} as any
  }
  public async friendRequestRawPayloadParser(rawPayload: any) : Promise<FriendRequestPayload> {
    return rawPayload
  }

344 345 346 347 348
  public async friendRequestSend(
    contactId : string,
    hello     : string,
  ): Promise<void> {
    log.verbose('PuppetMock', 'friendRequestSend(%s, %s)', contactId, hello)
349 350
  }

351 352 353 354 355
  public async friendRequestAccept(
    contactId : string,
    ticket    : string,
  ): Promise<void> {
    log.verbose('PuppetMock', 'friendRequestAccept(%s, %s)', contactId, ticket)
356 357
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
358 359 360 361
  public ding(data?: any): Promise<string> {
    return data
  }

362 363 364
}

export default PuppetMock