room.spec.ts 4.6 KB
Newer Older
1 2
#!/usr/bin/env ts-node
/**
3
 *   Wechaty - https://github.com/wechaty/wechaty
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *   @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

import test  from 'blue-tape'
import sinon from 'sinon'

26 27 28
import {
  ContactPayload,
  RoomMemberPayload,
29
  RoomPayload,
30
}                       from 'wechaty-puppet'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
31
import { PuppetMock }   from 'wechaty-puppet-mock'
32

33
import { Wechaty }      from '../wechaty'
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

test('findAll()', async t => {
  const EXPECTED_ROOM_ID      = 'test-id'
  const EXPECTED_ROOM_TOPIC   = 'test-topic'
  const EXPECTED_ROOM_ID_LIST = [EXPECTED_ROOM_ID]

  const sandbox = sinon.createSandbox()

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

  await wechaty.start()

  sandbox.stub(puppet, 'roomSearch').resolves(EXPECTED_ROOM_ID_LIST)
  sandbox.stub(puppet, 'roomPayload').callsFake(async () => {
49
    await new Promise(resolve => setImmediate(resolve))
50 51
    return {
      topic: EXPECTED_ROOM_TOPIC,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
52
    } as RoomPayload
53 54 55 56 57 58 59 60
  })

  const roomList = await wechaty.Room.findAll()
  t.equal(roomList.length, 1, 'should find 1 room')
  t.equal(await roomList[0].topic(), EXPECTED_ROOM_TOPIC, 'should get topic from payload')

  await wechaty.stop()
})
61

62
test('say()', async () => {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

  const sandbox = sinon.createSandbox()
  const callback = sinon.spy()

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

  await wechaty.start()

  const EXPECTED_ROOM_ID = 'roomId'
  const EXPECTED_ROOM_TOPIC = 'test-topic'
  const EXPECTED_CONTACT_1_ID = 'contact1'
  const EXPECTED_CONTACT_1_ALIAS = 'little1'
  const EXPECTED_CONTACT_2_ID = 'contact2'
  const EXPECTED_CONTACT_2_ALIAS = 'big2'
  const CONTACT_MAP: { [contactId: string]: string } = {}
  CONTACT_MAP[EXPECTED_CONTACT_1_ID] = EXPECTED_CONTACT_1_ALIAS
  CONTACT_MAP[EXPECTED_CONTACT_2_ID] = EXPECTED_CONTACT_2_ALIAS

  sandbox.stub(puppet, 'roomMemberPayload').callsFake(async (_, contactId) => {
83
    await new Promise(resolve => setImmediate(resolve))
84 85 86 87 88 89
    return {
      id: contactId,
      roomAlias: CONTACT_MAP[contactId],
    } as RoomMemberPayload
  })
  sandbox.stub(puppet, 'roomPayload').callsFake(async () => {
90
    await new Promise(resolve => setImmediate(resolve))
91 92 93 94 95
    return {
      topic: EXPECTED_ROOM_TOPIC,
    } as RoomPayload
  })
  sandbox.stub(puppet, 'contactPayload').callsFake(async (contactId) => {
96
    await new Promise(resolve => setImmediate(resolve))
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    return {
      id: contactId,
    } as ContactPayload
  })
  // sandbox.spy(puppet, 'messageSendText')
  sandbox.stub(puppet, 'messageSendText').callsFake(callback)

  const room = wechaty.Room.load(EXPECTED_ROOM_ID)
  const contact1 = wechaty.Contact.load(EXPECTED_CONTACT_1_ID)
  const contact2 = wechaty.Contact.load(EXPECTED_CONTACT_2_ID)
  await contact1.sync()
  await contact2.sync()
  await room.sync()

  test('say with Tagged Template', async t => {
    callback.resetHistory()
    await room.say`To be ${contact1} or not to be ${contact2}`

    t.deepEqual(callback.getCall(0).args, [
      { contactId: EXPECTED_CONTACT_1_ID, roomId: EXPECTED_ROOM_ID },
      'To be @little1 or not to be @big2',
      [EXPECTED_CONTACT_1_ID, EXPECTED_CONTACT_2_ID],
    ], 'Tagged Template say should be matched')
  })

  test('say with regular mention contact', async t => {
    callback.resetHistory()
    await room.say('Yo', contact1)

    t.deepEqual(callback.getCall(0).args, [
      { contactId: EXPECTED_CONTACT_1_ID, roomId: EXPECTED_ROOM_ID },
      '@little1 Yo',
      [EXPECTED_CONTACT_1_ID],
    ], 'Single mention should work with old ways')
  })

  test('say with multiple mention contact', async t => {
    callback.resetHistory()
    await room.say('hey buddies, let\'s party', contact1, contact2)

    t.deepEqual(callback.getCall(0).args, [
      { contactId: EXPECTED_CONTACT_1_ID, roomId: EXPECTED_ROOM_ID },
      '@little1 @big2 hey buddies, let\'s party',
      [EXPECTED_CONTACT_1_ID, EXPECTED_CONTACT_2_ID],
    ], 'Multiple mention should work with new way')
  })

  await wechaty.stop()
})