puppet-mock.ts 8.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
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

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

import {
  log,
45
}                       from '../config'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
46

47 48 49
export type PuppetFoodType = 'scan' | 'ding'
export type ScanFoodType   = 'scan' | 'login' | 'logout'

50 51 52 53 54
export interface MockContactRawPayload {
  name : string,
}

export interface MockMessageRawPayload {
55
  id   : string,
56 57 58 59 60 61
  from : string,
  to   : string,
  text : string
}

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

67 68 69
export class PuppetMock extends Puppet {

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

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

78 79 80
    this.state.on('pending')
    // await some tasks...
    this.state.on(true)
81

82
    this.id = 'logined_user_id'
83 84 85 86 87 88 89 90 91 92
    // 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(),
    })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
93

94
    setInterval(() => {
95 96
      log.verbose('PuppetMock', `start() setInterval() pretending received a new message: ${MOCK_MSG_ID}`)
      this.emit('message', MOCK_MSG_ID)
97 98
    }, 3000)

99 100 101 102 103 104 105 106 107 108
  }

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

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

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

118
    if (!this.id) {
119
      throw new Error('logout before login?')
120 121
    }

122 123
    this.emit('logout', this.id) // becore we will throw above by logonoff() when this.user===undefined
    this.id = undefined
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
124 125

    // TODO: do the logout job
126 127
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
128 129 130 131 132
  /**
   *
   * Contact
   *
   */
133 134
  public contactAlias(contactId: string)                      : Promise<string>
  public contactAlias(contactId: string, alias: string | null): Promise<void>
135

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

139 140 141 142 143 144
    if (typeof alias === 'undefined') {
      return 'mock alias'
    }
    return
  }

145 146
  public async contactList(): Promise<string[]> {
    log.verbose('PuppetMock', 'contactList()')
147

148 149 150
    return []
  }

151 152
  public async contactAvatar(contactId: string): Promise<FileBox> {
    log.verbose('PuppetMock', 'contactAvatar(%s)', contactId)
153

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
154
    const WECHATY_ICON_PNG = path.resolve('../../docs/images/wechaty-icon.png')
155
    return FileBox.fromLocal(WECHATY_ICON_PNG)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
156 157
  }

158 159 160 161 162 163 164 165 166 167
  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)
168

169
    const payload: ContactPayload = {
170 171 172
      id     : 'id',
      gender : ContactGender.Unknown,
      type   : ContactType.Unknown,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
173
    }
174
    return payload
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
175 176
  }

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

189 190 191
  public async messageRawPayload(id: string): Promise<MockMessageRawPayload> {
    log.verbose('PuppetMock', 'messageRawPayload(%s)', id)
    const rawPayload: MockMessageRawPayload = {
192
      id   : 'id',
193 194 195 196 197 198 199 200 201
      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 (李卓桓) 已提交
202
    const payload: MessagePayload = {
203
      id        : rawPayload.id,
204
      timestamp : Date.now(),
205
      fromId    : 'xxx',
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
206
      text      : 'mock message text',
207
      toId      : this.selfId(),
208
      type      : MessageType.Text,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
209 210 211 212
    }
    return payload
  }

213 214 215 216 217 218 219 220 221 222 223 224
  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 (李卓桓) 已提交
225 226
  }

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

  /**
   *
   * Room
   *
   */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
242 243 244
  public async roomRawPayload(
    id: string,
  ): Promise<MockRoomRawPayload> {
245 246 247
    log.verbose('PuppetMock', 'roomRawPayload(%s)', id)

    const rawPayload: MockRoomRawPayload = {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
248
      ownerId      : 'mock_room_owner_id',
249 250 251 252 253
      topic      : 'mock topic',
      memberList : [],
    }
    return rawPayload
  }
254

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
255 256 257
  public async roomRawPayloadParser(
    rawPayload: MockRoomRawPayload,
  ): Promise<RoomPayload> {
258 259 260
    log.verbose('PuppetMock', 'roomRawPayloadParser(%s)', rawPayload)

    const payload: RoomPayload = {
261 262 263 264
      id           : 'id',
      topic        : 'mock topic',
      memberIdList : [],
      aliasDict    : {},
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
265
    }
266 267

    return payload
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
268 269
  }

270 271
  public async roomList(): Promise<string[]> {
    log.verbose('PuppetMock', 'roomList()')
272

273 274 275 276
    return []
  }

  public async roomDel(
277 278
    roomId    : string,
    contactId : string,
279
  ): Promise<void> {
280
    log.verbose('PuppetMock', 'roomDel(%s, %s)', roomId, contactId)
281 282 283
  }

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

290 291 292 293 294
  public async roomTopic(
    roomId: string,
    topic?: string,
  ): Promise<void | string> {
    log.verbose('PuppetMock', 'roomTopic(%s, %s)', roomId, topic)
295

296 297 298 299 300 301
    if (typeof topic === 'undefined') {
      return 'mock room topic'
    }
    return
  }

302 303 304 305 306
  public async roomCreate(
    contactIdList : string[],
    topic         : string,
  ): Promise<string> {
    log.verbose('PuppetMock', 'roomCreate(%s, %s)', contactIdList, topic)
307

308
    return 'mock_room_id'
309 310
  }

311 312
  public async roomQuit(roomId: string): Promise<void> {
    log.verbose('PuppetMock', 'roomQuit(%s)', roomId)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
313 314
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
315 316 317 318 319
  /**
   *
   * FriendRequest
   *
   */
320 321 322 323 324
  public async friendRequestSend(
    contactId : string,
    hello     : string,
  ): Promise<void> {
    log.verbose('PuppetMock', 'friendRequestSend(%s, %s)', contactId, hello)
325 326
  }

327 328 329 330 331
  public async friendRequestAccept(
    contactId : string,
    ticket    : string,
  ): Promise<void> {
    log.verbose('PuppetMock', 'friendRequestAccept(%s, %s)', contactId, ticket)
332 333
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
334 335 336 337
  public ding(data?: any): Promise<string> {
    return data
  }

338 339 340
}

export default PuppetMock