message.spec.ts 3.4 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1 2
#!/usr/bin/env ts-node
/**
3
 *   Wechaty - https://github.com/wechaty/wechaty
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 *   @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.
 *
 */

// tslint:disable:no-shadowed-variable
// tslint:disable:max-classes-per-file

Y
Yuan Gao 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
import test  from 'blue-tape'
import sinon from 'sinon'

import {
  ContactPayload,
  MessagePayload,
  MessageType,
  RoomPayload,
}                       from 'wechaty-puppet'
import { PuppetMock }   from 'wechaty-puppet-mock'

import { Wechaty }      from '../wechaty'

test('recalled()', async t => {

  const EXPECTED_RECALL_MESSAGE_ID = '1'
  const EXPECTED_RECALLED_MESSAGE_ID = '2'
  const EXPECTED_MESSAGE_TIMESTAMP = new Date().getTime()
  const EXPECTED_ROOM_TOPIC = 'topic'
  const EXPECTED_ROOM_ID = 'room1'
  const EXPECTED_FROM_CONTACT_ID = 'contact1'
  const EXPECTED_TO_CONTACT_ID = 'contact1'

  const sandbox = sinon.createSandbox()

  const puppet = new PuppetMock()
  const wechaty = new Wechaty({ puppet })

  await wechaty.start()

  sandbox.stub(puppet, 'messagePayload').callsFake(async (id) => {
55
    await new Promise(resolve => setImmediate(resolve))
Y
Yuan Gao 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    if (id === EXPECTED_RECALL_MESSAGE_ID) {
      return {
        id: EXPECTED_RECALL_MESSAGE_ID,
        text: EXPECTED_RECALLED_MESSAGE_ID,
        timestamp: EXPECTED_MESSAGE_TIMESTAMP,
        type: MessageType.Recalled,
      } as MessagePayload
    } else {
      return {
        fromId: EXPECTED_FROM_CONTACT_ID,
        id: EXPECTED_RECALLED_MESSAGE_ID,
        roomId: EXPECTED_ROOM_ID,
        text: '',
        timestamp: EXPECTED_MESSAGE_TIMESTAMP,
        toId: EXPECTED_TO_CONTACT_ID,
        type: MessageType.Text,
      } as MessagePayload
    }
  })
  sandbox.stub(puppet, 'roomPayload').callsFake(async () => {
76
    await new Promise(resolve => setImmediate(resolve))
Y
Yuan Gao 已提交
77 78 79 80 81 82
    return {
      topic: EXPECTED_ROOM_TOPIC,
    } as RoomPayload
  })

  sandbox.stub(puppet, 'roomMemberList').callsFake(async () => {
83
    await new Promise((resolve) => setImmediate(resolve))
Y
Yuan Gao 已提交
84 85 86 87
    return [ EXPECTED_FROM_CONTACT_ID, EXPECTED_TO_CONTACT_ID ]
  })

  sandbox.stub(puppet, 'contactPayload').callsFake(async (id: string) => {
88
    await new Promise(resolve => setImmediate(resolve))
Y
Yuan Gao 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    return {
      id,
      name: id,
    } as ContactPayload
  })

  const message = wechaty.Message.load(EXPECTED_RECALL_MESSAGE_ID)
  await message.ready()
  const recalledMessage = await message.toRecalled()
  t.assert(recalledMessage, 'recalled message should exist.')
  t.equal(recalledMessage!.id, EXPECTED_RECALLED_MESSAGE_ID, 'Recalled message should have the right id.')
  t.equal(recalledMessage!.from()!.id, EXPECTED_FROM_CONTACT_ID, 'Recalled message should have the right from contact id.')
  t.equal(recalledMessage!.to()!.id, EXPECTED_TO_CONTACT_ID, 'Recalled message should have the right to contact id.')
  t.equal(recalledMessage!.room()!.id, EXPECTED_ROOM_ID, 'Recalled message should have the right room id.')

  await wechaty.stop()
})