firer.spec.ts 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/**
 *
 * Wechaty: Wechat for Bot. Connecting ChatBots
 *
 * Class PuppetWeb Firer
 *
 * Process the Message to find which event to FIRE
 *
 * Licenst: ISC
 * https://github.com/wechaty/wechaty
 *
 */
import { test }   from 'ava'

15
import Firer      from './firer'
16 17 18 19 20 21

test('Firer smoking test', t => {

  t.true(true, 'should be true')
})

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
22
test('Firer.parseFriendConfirm', t => {
23 24 25 26 27 28 29 30 31 32 33
  const contentList = [
    [
        'You have added 李卓桓 as your WeChat contact. Start chatting!'
      , '李卓桓'
    ]
    , [
        '你已添加了李卓桓,现在可以开始聊天了。'
      , '李卓桓'
    ]
  ]
  let result: boolean
34

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
35
  contentList.forEach(([content]) => {
36
    result = Firer.parseFriendConfirm(content)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
37
    t.true(result, 'should be truthy for confirm msg: ' + content)
38
  })
39

40
  result = Firer.parseFriendConfirm('fsdfsdfasdfasdfadsa')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
41
  t.false(result, 'should be falsy for other msg')
42 43
})

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
44
test('Firer.parseRoomJoin', t => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
45
  const contentList: [string, string, string[]][] = [
46 47 48
    [
      `You've invited "李卓桓" to the group chat`
      , `You've`
49
      , [`李卓桓`]
50 51 52 53
    ]
    , [
      `You've invited "李卓桓.PreAngel、Bruce LEE" to the group chat`
      , `You've`
54
      , [`李卓桓.PreAngel`, `Bruce LEE`]
55 56 57 58
    ]
    , [
      `"李卓桓.PreAngel" invited "Bruce LEE" to the group chat`
      , `李卓桓.PreAngel`
59 60 61 62 63 64
      , [`Bruce LEE`]
    ]
    , [
      `"凌" invited "庆次、小桔妹" to the group chat`
      , `凌`
      , ['庆次', '小桔妹']
65
    ]
66 67 68 69 70
    , [
      `你邀请"李佳芮"加入了群聊`
      , ''
      , ['李佳芮']
    ]
71 72 73
  ]

  let result
74
  contentList.forEach(([content, inviter, inviteeList]) => {
75
    result = Firer.parseRoomJoin(content)
76
    t.truthy(result, 'should check room join message right for ' + content)
77
    t.deepEqual(result[0], inviteeList, 'should get inviteeList right')
78 79 80
    t.is(result[1], inviter, 'should get inviter right')
  })

81
  t.throws(() => {
82
    Firer.parseRoomJoin('fsadfsadfsdfsdfs')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
83
  }, Error, 'should throws if message is not expected')
84 85
})

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
86
test('Firer.parseRoomLeave', t => {
87 88 89 90 91 92 93 94 95
  const contentList = [
    [
        `You removed "Bruce LEE" from the group chat`
      , `Bruce LEE`
    ]
    , [
      '你将"李佳芮"移出了群聊'
      , '李佳芮'
    ]
96 97
  ]

98 99
  let result
  contentList.forEach(([content, leaver]) => {
100
    result = Firer.parseRoomLeave(content)
101 102 103
    t.truthy(result, 'should get leaver for leave message: ' + content)
    t.is(result, leaver, 'should get leaver name right')
  })
104

105
  t.throws(() => {
106
    Firer.parseRoomLeave('fafdsfsdfafa')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
107
  }, Error, 'should throw if message is not expected')
108
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
109

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
110
test('Firer.parseRoomTopic', t => {
111 112 113 114 115 116 117 118 119 120 121
  const contentList = [
    [
        `"李卓桓.PreAngel" changed the group name to "ding"`
      , `李卓桓.PreAngel`
      , `ding`
    ]
    , [
      '"李佳芮"修改群名为“dong”'
      , '李佳芮'
      , 'dong'
    ]
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
122 123
  ]

124 125
  let result
  contentList.forEach(([content, changer, topic]) => {
126
    result = Firer.parseRoomTopic(content)
127
    t.truthy(result, 'should check topic right for content: ' + content)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
128
    t.is(topic  , result[0], 'should get right topic')
129 130 131 132
    t.is(changer, result[1], 'should get right changer')
  })

  t.throws(() => {
133
    Firer.parseRoomTopic('fafdsfsdfafa')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
134
  }, Error, 'should throw if message is not expected')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
135 136

})