bridge.spec.ts 5.8 KB
Newer Older
1
#!/usr/bin/env ts-node
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
2 3 4
/**
 *   Wechaty - https://github.com/chatie/wechaty
 *
5
 *   @copyright 2016-2017 Huan LI <zixia@zixia.net>
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 *   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.
 *
 */
20
import * as test  from 'blue-tape'
21 22
// tslint:disable:no-shadowed-variable

23
// import * as sinon from 'sinon'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
24
// const sinonTest   = require('sinon-test')(sinon)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
25

26 27 28 29
import {
  launch,
}                 from 'puppeteer'

30
import Profile    from '../profile'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
31

32
import Bridge     from './bridge'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
33

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
34
test('PuppetWebBridge', async t => {
35 36
  const profile = new Profile()
  const bridge = new Bridge({ profile })
37 38 39 40 41 42 43
  try {
    await bridge.init()
    await bridge.quit()
    t.pass('Bridge instnace')
  } catch (e) {
    t.fail('Bridge instance: ' + e)
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
44
})
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
test('preHtmlToXml()', async t => {
  const BLOCKED_HTML_ZH = [
    '<pre style="word-wrap: break-word; white-space: pre-wrap;">',
      '&lt;error&gt;',
        '&lt;ret&gt;1203&lt;/ret&gt;',
        '&lt;message&gt;当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。你可以通过Windows微信、Mac微信或者手机客户端微信登录。&lt;/message&gt;',
      '&lt;/error&gt;',
    '</pre>',
  ].join('')

  const BLOCKED_XML_ZH = [
    '<error>',
      '<ret>1203</ret>',
      '<message>当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。你可以通过Windows微信、Mac微信或者手机客户端微信登录。</message>',
    '</error>',
  ].join('')

  const profile = new Profile()
  const bridge = new Bridge({ profile })

  const xml = bridge.preHtmlToXml(BLOCKED_HTML_ZH)
  t.equal(xml, BLOCKED_XML_ZH, 'should parse html to xml')
})

70
test('testBlockedMessage()', async t => {
71 72 73 74 75 76 77 78 79
  const BLOCKED_HTML_ZH = [
    '<pre style="word-wrap: break-word; white-space: pre-wrap;">',
      '&lt;error&gt;',
        '&lt;ret&gt;1203&lt;/ret&gt;',
        '&lt;message&gt;当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。你可以通过手机客户端或者windows微信登录。&lt;/message&gt;',
      '&lt;/error&gt;',
    '</pre>',
  ].join('')

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  const BLOCKED_XML_ZH = `
    <error>
     <ret>1203</ret>
     <message>当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。你可以通过手机客户端或者windows微信登录。</message>
    </error>
  `
  const BLOCKED_TEXT_ZH = [
    '当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。',
    '你可以通过手机客户端或者windows微信登录。',
  ].join('')
   // tslint:disable:max-line-length
  const BLOCKED_XML_EN = `
    <error>
     <ret>1203</ret>
     <message>For account security, newly registered WeChat accounts are unable to log in to Web WeChat. To use WeChat on a computer, use Windows WeChat or Mac WeChat at http://wechat.com</message>
    </error>
96
  `
97 98 99 100 101 102 103 104 105
  const BLOCKED_TEXT_EN = [
    'For account security, newly registered WeChat accounts are unable to log in to Web WeChat.',
    ' To use WeChat on a computer, use Windows WeChat or Mac WeChat at http://wechat.com',
  ].join('')

  test('not blocked', async t => {
    const profile = new Profile()
    const bridge = new Bridge({ profile })

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
106 107
    const msg = await bridge.testBlockedMessage('this is not xml')
    t.equal(msg, false, 'should return false when no block message')
108 109
  })

110 111 112 113 114 115 116 117
  test('html', async t => {
    const profile = new Profile()
    const bridge = new Bridge({ profile })

    const msg = await bridge.testBlockedMessage(BLOCKED_HTML_ZH)
    t.equal(msg, BLOCKED_TEXT_ZH, 'should get zh blocked message')
  })

118 119 120 121
  test('zh', async t => {
    const profile = new Profile()
    const bridge = new Bridge({ profile })

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
122 123
    const msg = await bridge.testBlockedMessage(BLOCKED_XML_ZH)
    t.equal(msg, BLOCKED_TEXT_ZH, 'should get zh blocked message')
124 125 126 127 128 129
  })

  test('en', async t => {
    const profile = new Profile()
    const bridge = new Bridge({ profile })

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
130 131
    const msg = await bridge.testBlockedMessage(BLOCKED_XML_EN)
    t.equal(msg, BLOCKED_TEXT_EN, 'should get en blocked message')
132 133
  })
})
134 135 136 137 138 139 140 141 142 143 144 145 146

test('clickSwitchAccount()', async t => {
  const SWITCH_ACCOUNT_HTML = `
    <div class="association show" ng-class="{show: isAssociationLogin &amp;&amp; !isBrokenNetwork}">
    <img class="img" mm-src="" alt="" src="//res.wx.qq.com/a/wx_fed/webwx/res/static/img/2KriyDK.png">
    <p ng-show="isWaitingAsConfirm" class="waiting_confirm ng-hide">Confirm login on mobile WeChat</p>
    <a href="javascript:;" ng-show="!isWaitingAsConfirm" ng-click="associationLogin()" class="button button_primary">Log in</a>
    <a href="javascript:;" ng-click="qrcodeLogin()" class="button button_default">Switch Account</a>
    </div>
  `
  const profile = new Profile()
  const bridge = new Bridge({ profile} )

147
  test('switch account needed', async t => {
148
    const browser = await launch()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
149
    const page    = await browser.newPage()
150 151 152 153 154 155 156 157 158 159

    await page.setContent(SWITCH_ACCOUNT_HTML)
    const clicked = await bridge.clickSwitchAccount(page)

    await page.close()
    await browser.close()

    t.equal(clicked, true, 'should click the switch account button')
  })

160
  test('switch account not needed', async t => {
161
    const browser = await launch()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
162
    const page    = await browser.newPage()
163 164 165 166 167 168 169 170 171 172 173

    await page.setContent('<h1>ok</h1>')
    const clicked = await bridge.clickSwitchAccount(page)

    await page.close()
    await browser.close()

    t.equal(clicked, false, 'should no button found')
  })

})