提交 c33a6542 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

switching to ava...

上级 6e5811d3
......@@ -11,9 +11,13 @@ const IoBot = require('./src/io-bot')
const log = require('./src/npmlog-env')
const Config = require('./src/config')
const UtilLib = require('./src/util-lib')
Object.assign(Wechaty, {
Message
default: Wechaty
, Wechaty
, Message
, Contact
, Room
......@@ -26,6 +30,7 @@ Object.assign(Wechaty, {
, log // for convenionce use npmlog with environment variable LEVEL
, Config
, UtilLib
})
module.exports = Wechaty
......@@ -14,7 +14,8 @@
"pretest": "npm run lint",
"start": "node bin/io-bot",
"demo": "node example/ding-dong-bot.js",
"test": "cross-env LC_ALL=C TAP_TIMEOUT=600 WECHATY_LOG=silly tape \"test/**/*.spec.js\"",
"test": "cross-env LC_ALL=C WECHATY_LOG=silly ava --timeout=10m \"test/**/*.spec.js\"",
"oldtest": "cross-env LC_ALL=C TAP_TIMEOUT=600 WECHATY_LOG=silly tape \"test/**/*.spec.js\"",
"devtest": "cross-env LC_ALL=C TAP_TIMEOUT=600 tape"
},
"repository": {
......@@ -68,6 +69,7 @@
},
"devDependencies": {
"apiai": "^2.0.7",
"ava": "^0.16.0",
"cookie-parser": "^1.4.3",
"cross-env": "^2.0.0",
"eslint": "^3.3.1",
......
......@@ -95,13 +95,14 @@ class Contact {
Contact.init = function() { Contact.pool = {} }
Contact.init()
Contact.load = function(id) {
if (!id) { return null }
if (id in Contact.pool) {
return Contact.pool[id]
if (!(id in Contact.pool)) {
Contact.pool[id] = new Contact(id)
}
return Contact.pool[id] = new Contact(id)
return Contact.pool[id]
}
// Contact.search = function(options) {
// if (options.name) {
......@@ -114,6 +115,9 @@ Contact.load = function(id) {
// return []
// }
Contact.attach = function(puppet) { Contact.puppet = puppet }
Contact.attach = function(puppet) {
log.verbose('Contact', 'attach() to %s', puppet && puppet.constructor.name)
Contact.puppet = puppet
}
module.exports = Contact
......@@ -164,7 +164,10 @@ Object.keys(Message.Type).forEach(k => {
Message.Type[v] = k // Message.Type[1] = 'TEXT'
})
Message.attach = function(puppet) { Message.puppet = puppet }
Message.attach = function(puppet) {
log.verbose('Message', 'attach() to %s', puppet && puppet.constructor.name)
Message.puppet = puppet
}
module.exports = Message
......
......@@ -313,4 +313,12 @@ class PuppetWeb extends Puppet {
}
}
Object.assign(PuppetWeb, {
default: PuppetWeb
, PuppetWeb
, Server
, Browser
, Bridge
})
module.exports = PuppetWeb
......@@ -47,7 +47,8 @@ class Puppet extends EventEmitter {
getContact(id) { // for unit testing
log.verbose('Puppet', `Interface method getContact(${id})`)
return Promise.resolve({UserName: 'WeChaty', NickName: 'Puppet'})
throw new Error('Absolute Interface Method should never to be called')
// return Promise.resolve({UserName: 'WeChaty', NickName: 'Puppet'})
}
// () { throw new Error('To Be Implemented') }
......
const test = require('tape')
const Contact = require('../src/contact')
const Puppet = require('../src/puppet')
const log = require('../src/npmlog-env')
import { test } from 'ava'
import {
Contact
, Puppet
, log
} from '../'
// const test = require('tape')
// const Contact = require('../src/contact')
// const Puppet = require('../src/puppet')
// const log = require('../src/npmlog-env')
Contact.attach(new Puppet())
test('Contact smoke testing', t => {
test('Contact smoke testing', async t => {
const UserName = '@0bb3e4dd746fdbd4a80546aef66f4085'
const NickName = 'Nick Name Test'
......@@ -25,15 +30,15 @@ test('Contact smoke testing', t => {
const c = new Contact(UserName)
t.equal(c.id, UserName, 'id/UserName right')
c.ready(mockContactGetter)
.then(r => {
t.equal(r.get('id') , UserName, 'UserName set')
t.equal(r.get('name') , NickName, 'NickName set')
t.is(c.id, UserName, 'id/UserName right')
const r = await c.ready(mockContactGetter)
// .then(r => {
t.is(r.get('id') , UserName, 'UserName set')
t.is(r.get('name') , NickName, 'NickName set')
const s = r.toString()
t.equal(typeof s, 'string', 'toString()')
})
.catch(e => t.fail('ready() rejected: ' + e))
.then(_ => t.end()) // test end
const s = r.toString()
t.is(typeof s, 'string', 'toString()')
// })
// .catch(e => t.fail('ready() rejected: ' + e))
// .then(_ => t.end()) // test end
})
import { test } from 'ava'
test('Electron smoke testing', async t => {
t.true(true, 'test')
})
const test = require('tape')
const Message = require('../src/message')
const Contact = require('../src/contact')
const Puppet = require('../src/puppet')
const log = require('../src/npmlog-env')
import { test } from 'ava'
import {
Contact
, Message
, Puppet
, log
} from '../'
// const test = require('tape')
// const Message = require('../src/message')
// const Contact = require('../src/contact')
// const Puppet = require('../src/puppet')
// const log = require('../src/npmlog-env')
const puppet = new Puppet()
Contact.attach(puppet)
Message.attach(puppet)
test('Message constructor parser test', t => {
Contact.attach(new Puppet())
Message.attach(new Puppet())
// Contact.attach(new Puppet())
// Message.attach(new Puppet())
const rawData = JSON.parse('{"MsgId":"179242112323992762","FromUserName":"@0bb3e4dd746fdbd4a80546aef66f4085","ToUserName":"@16d20edf23a3bf3bc71bb4140e91619f3ff33b4e33f7fcd25e65c1b02c7861ab","MsgType":1,"Content":"test123","Status":3,"ImgStatus":1,"CreateTime":1461652670,"VoiceLength":0,"PlayLength":0,"FileName":"","FileSize":"","MediaId":"","Url":"","AppMsgType":0,"StatusNotifyCode":0,"StatusNotifyUserName":"","RecommendInfo":{"UserName":"","NickName":"","QQNum":0,"Province":"","City":"","Content":"","Signature":"","Alias":"","Scene":0,"VerifyFlag":0,"AttrStatus":0,"Sex":0,"Ticket":"","OpCode":0},"ForwardFlag":0,"AppInfo":{"AppID":"","Type":0},"HasProductId":0,"Ticket":"","ImgHeight":0,"ImgWidth":0,"SubMsgType":0,"NewMsgId":179242112323992770,"MMPeerUserName":"@0bb3e4dd746fdbd4a80546aef66f4085","MMDigest":"test123","MMIsSend":false,"MMIsChatRoom":false,"MMUnread":true,"LocalID":"179242112323992762","ClientMsgId":"179242112323992762","MMActualContent":"test123","MMActualSender":"@0bb3e4dd746fdbd4a80546aef66f4085","MMDigestTime":"14:37","MMDisplayTime":1461652670,"MMTime":"14:37"}')
......@@ -17,32 +28,37 @@ test('Message constructor parser test', t => {
}
const m = new Message(rawData)
t.equal(m.id , EXPECTED.id , 'id right')
t.equal(m.get('from') , EXPECTED.from , 'from right')
t.is(m.id , EXPECTED.id , 'id right')
t.is(m.get('from') , EXPECTED.from , 'from right')
const s = m.toString()
t.equal(typeof s, 'string', 'toString()')
t.is(typeof s, 'string', 'toString()')
t.end()
// t.end()
})
test('Message ready() promise testing', t => {
Contact.attach(new Puppet())
Message.attach(new Puppet())
test('Message ready() promise testing', async t => {
// must different with other rawData, because Contact class with load() will cache the result. or use Contact.resetPool()
const rawData = JSON.parse('{"MsgId":"3009511950433684462","FromUserName":"@0748ee480711bf20af91c298a0d7dcc77c30a680c1004157386b81cf13474823","ToUserName":"@b58f91e0c5c9e841e290d862ddb63c14","MsgType":1,"Content":"哈哈","Status":3,"ImgStatus":1,"CreateTime":1462887888,"VoiceLength":0,"PlayLength":0,"FileName":"","FileSize":"","MediaId":"","Url":"","AppMsgType":0,"StatusNotifyCode":0,"StatusNotifyUserName":"","RecommendInfo":{"UserName":"","NickName":"","QQNum":0,"Province":"","City":"","Content":"","Signature":"","Alias":"","Scene":0,"VerifyFlag":0,"AttrStatus":0,"Sex":0,"Ticket":"","OpCode":0},"ForwardFlag":0,"AppInfo":{"AppID":"","Type":0},"HasProductId":0,"Ticket":"","ImgHeight":0,"ImgWidth":0,"SubMsgType":0,"NewMsgId":3009511950433684500,"MMPeerUserName":"@0748ee480711bf20af91c298a0d7dcc77c30a680c1004157386b81cf13474823","MMDigest":"哈哈","MMIsSend":false,"MMIsChatRoom":false,"MMUnread":false,"LocalID":"3009511950433684462","ClientMsgId":"3009511950433684462","MMActualContent":"哈哈","MMActualSender":"@0748ee480711bf20af91c298a0d7dcc77c30a680c1004157386b81cf13474823","MMDigestTime":"21:44","MMDisplayTime":1462887888,"MMTime":"21:44","_h":104,"_index":0,"_offsetTop":0,"$$hashKey":"098"}')
const expectedFromUserName = '@0748ee480711bf20af91c298a0d7dcc77c30a680c1004157386b81cf13474823'
const expectedToUserName = '@b58f91e0c5c9e841e290d862ddb63c14'
const expectedFromNickName = 'From Nick Name Test'
const expectedToNickName = 'To Nick Name Test'
const expectedFromNickName = 'From Nick Name@Test'
const expectedToNickName = 'To Nick Name@Test'
const expectedMsgId = '3009511950433684462'
// const puppet = new Puppet()
puppet.getContact = mockGetContact
// const puppet = new Puppet()
// Contact.attach(puppet)
// Message.attach(puppet)
Contact.init()
// Mock
Contact.puppet.getContact = function (id) {
function mockGetContact(id) {
log.silly('TestMessage', `mocked getContact(${id})`)
return new Promise((resolve,reject) => {
let obj = {}
......@@ -61,36 +77,34 @@ test('Message ready() promise testing', t => {
break
default:
log.error('TestMessage', `mocked getContact(${id}) unknown`)
t.fail('mocked getContact(${id}) unknown')
break
}
log.silly('TestMessage', 'setTimeout mocked getContact')
setTimeout(r => {
log.silly('TestMessage', 'mocked getContact resolved')
return resolve(obj)
}, 200)
}, 100)
})
}
const m = new Message(rawData)
t.equal(m.id, expectedMsgId, 'id/MsgId right')
t.is(m.id, expectedMsgId, 'id/MsgId right')
const p = m.ready()
p.then(r => {
const fc = Contact.load(m.get('from'))
const tc = Contact.load(m.get('to'))
t.equal(fc.get('id') , expectedFromUserName, 'contact ready for FromUserName')
t.equal(fc.get('name') , expectedFromNickName, 'contact ready for FromNickName')
t.equal(tc.get('id') , expectedToUserName , 'contact ready for ToUserName')
t.equal(tc.get('name') , expectedToNickName , 'contact ready for ToNickName')
})
.catch(e => t.fail('m.ready() rejected: ' + e))
.then(_ => t.end()) // test end
await m.ready()
const fc = Contact.load(m.get('from'))
const tc = Contact.load(m.get('to'))
t.is(fc.get('id') , expectedFromUserName, 'contact ready for FromUserName')
t.is(fc.get('name') , expectedFromNickName, 'contact ready for FromNickName')
t.is(tc.get('id') , expectedToUserName , 'contact ready for ToUserName')
t.is(tc.get('name') , expectedToNickName , 'contact ready for ToNickName')
})
test('TBW: Message static method', t => {
Contact.attach(new Puppet())
Message.attach(new Puppet())
// Contact.attach(new Puppet())
// Message.attach(new Puppet())
const m = Message.find({
id: 'xxx'
......@@ -98,7 +112,7 @@ test('TBW: Message static method', t => {
limit: 1
})
t.ok(m.get('id'), 'Message.find')
t.truthy(m.get('id'), 'Message.find')
const ms = Message.findAll({
from: 'yyy'
......@@ -106,7 +120,7 @@ test('TBW: Message static method', t => {
limit: 2
})
t.equal(ms.length, 2, 'Message.findAll with limit 2')
t.is(ms.length, 2, 'Message.findAll with limit 2')
t.end()
// t.end()
})
'use strict'
import { test } from 'ava'
import { log } from '../'
const sinon = require('sinon')
const test = require('tape')
import { spy } from 'sinon'
const log = require('../src/npmlog-env')
// 'use strict'
// const sinon = require('sinon')
// const test = require('tape')
// const log = require('../src/npmlog-env')
test('Node.js function params destructuring behaviour test', function(t) {
const DEFAULT_N = 1
const DEFAULT_S = 't'
const spy = sinon.spy()
const paramSpy = spy()
function paramTest({
n = DEFAULT_N
, s = DEFAULT_S
} = {}) {
spy(n, s)
paramSpy(n, s)
}
spy.reset()
paramSpy.reset()
paramTest()
t.deepEqual(spy.args[0], [DEFAULT_N, DEFAULT_S], 'should be equal to default args')
t.deepEqual(paramSpy.args[0], [DEFAULT_N, DEFAULT_S], 'should be equal to default args')
spy.reset()
paramSpy.reset()
paramTest({n: 42})
t.deepEqual(spy.args[0], [42, DEFAULT_S], 'should be equal to default s args')
t.deepEqual(paramSpy.args[0], [42, DEFAULT_S], 'should be equal to default s args')
spy.reset()
paramSpy.reset()
paramTest({s: 'life'})
t.deepEqual(spy.args[0], [DEFAULT_N, 'life'], 'should be equal to default n args')
t.deepEqual(paramSpy.args[0], [DEFAULT_N, 'life'], 'should be equal to default n args')
t.end()
// t.end()
})
'use strict'
import WebDriver from 'selenium-webdriver'
import express from 'express'
import * as http from 'http'
import * as url from 'url'
const WebDriver = require('selenium-webdriver')
const Express = require('express')
const http = require('http')
const url = require('url')
const co = require('co')
import { test } from 'ava'
const test = require('tape')
import { UtilLib, log } from '../'
const UtilLib = require('../src/util-lib')
const log = require('../src/npmlog-env')
// use strict'
false && test('Phantomjs smoking test', t => {
// const WebDriver = require('selenium-webdriver')
// const Express = require('express')
// const http = require('http')
// const url = require('url')
// const co = require('co')
// const test = require('tape')
// const UtilLib = require('../src/util-lib')
// const log = require('../src/npmlog-env')
test('Phantomjs smoking test', t => {
const phantomjsExe = require('phantomjs-prebuilt').path
const phantomjsArgs = [
......@@ -37,17 +46,17 @@ false && test('Phantomjs smoking test', t => {
driver.get('https://wx.qq.com')
t.end()
// t.end()
})
test('Phantomjs http header', t => {
co(function* () {
const port = yield UtilLib.getPort(8080)
const app = new Express()
test('Phantomjs http header', async t => {
// co(function* () {
const port = await UtilLib.getPort(8080)
// console.log(express)
const app = express()
app.use((req, res, done) => {
//console.log(req.headers)
t.equal(req.headers.referer, 'https://wx.qq.com/')
t.is(req.headers.referer, 'https://wx.qq.com/')
done()
})
......@@ -60,10 +69,10 @@ test('Phantomjs http header', t => {
options.headers = {
Accept: 'image/webp,image/*,*/*;q=0.8'
, 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'
, Referer: 'https://wx.qq.com/'
, 'Accept-Encoding': 'gzip, deflate, sdch'
, 'Accept-Language': 'zh-CN,zh;q=0.8'
, 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'
, Referer: 'https://wx.qq.com/'
, 'Accept-Encoding': 'gzip, deflate, sdch'
, 'Accept-Language': 'zh-CN,zh;q=0.8'
}
options.agent = http.globalAgent
......@@ -80,9 +89,9 @@ test('Phantomjs http header', t => {
})
req.end()
}).catch(e => {
t.fail(e)
}).then(_ => {
t.end()
})
// }).catch(e => {
// t.fail(e)
// }).then(_ => {
// t.end()
// })
})
const test = require('tape')
const Wechaty = require('../')
const Puppet = Wechaty.Puppet
import { test } from 'ava'
import { Wechaty, Puppet } from '../'
// const test = require('tape')
// const Wechaty = require('../')
// const Puppet = Wechaty.Puppet
test('Puppet smoking test', t => {
const p = new Puppet()
t.equal(p.readyState(), 'disconnected', 'should be disconnected state after instanciate')
t.is(p.readyState(), 'disconnected', 'should be disconnected state after instanciate')
p.readyState('connecting')
t.equal(p.readyState(), 'connecting', 'should be connecting state after set')
t.is(p.readyState(), 'connecting', 'should be connecting state after set')
t.end()
// t.end()
})
const test = require('tape')
const Message = require('../src/message')
const Room = require('../src/room')
const Contact = require('../src/contact')
const Puppet = require('../src/puppet')
const log = require('../src/npmlog-env')
import { test } from 'ava'
import {
Message
, Room
, Contact
, Puppet
, log
} from '../'
Room.attach(new Puppet())
test('Room smoke testing', t => {
Room.attach(new Puppet())
// const test = require('tape')
// const Message = require('../src/message')
// const Room = require('../src/room')
// const Contact = require('../src/contact')
// const Puppet = require('../src/puppet')
// const log = require('../src/npmlog-env')
test('Room smoke testing', async t => {
const UserName = '@0bb3e4dd746fdbd4a80546aef66f4085'
const NickName = 'Nick Name Test'
......@@ -27,20 +37,22 @@ test('Room smoke testing', t => {
})
}
const g = new Room(UserName)
const r = new Room(UserName)
t.is(r.id, UserName, 'id/UserName right')
await r.ready(mockContactGetter)
t.equal(g.id, UserName, 'id/UserName right')
g.ready(mockContactGetter)
.then(r => {
t.equal(r.get('id') , UserName, 'UserName set')
t.equal(r.get('name') , NickName, 'NickName set')
t.equal(r.get('encryId') , EncryChatRoomId, 'EncryChatRoomId set')
// .then(r => {
t.is(r.get('id') , UserName, 'UserName set')
t.is(r.get('name') , NickName, 'NickName set')
t.is(r.get('encryId') , EncryChatRoomId, 'EncryChatRoomId set')
const s = r.toString()
t.equal(typeof s, 'string', 'toString()')
})
.catch(e => t.fail('ready() rejected: ' + e))
.then(_ => t.end()) // test end
t.is(typeof s, 'string', 'toString()')
// })
// .catch(e => t.fail('ready() rejected: ' + e))
// .then(_ => t.end()) // test end
})
/*
const rawData = JSON.parse('{"MsgId":"1120003476579027592","FromUserName":"@@4aa0ae1e1ebc568b613fa43ce93b478df0339f73340d87083822c2016d2e53d9","ToUserName":"@94e4b0db79ccc844d7bb4a2b1efac3ff","MsgType":1,"Content":"@9ad4ba13fac52c55d323521b67f7cc39:<br/>[Strong]","Status":3,"ImgStatus":1,"CreateTime":1462889712,"VoiceLength":0,"PlayLength":0,"FileName":"","FileSize":"","MediaId":"","Url":"","AppMsgType":0,"StatusNotifyCode":0,"StatusNotifyUserName":"","RecommendInfo":{"UserName":"","NickName":"","QQNum":0,"Province":"","City":"","Content":"","Signature":"","Alias":"","Scene":0,"VerifyFlag":0,"AttrStatus":0,"Sex":0,"Ticket":"","OpCode":0},"ForwardFlag":0,"AppInfo":{"AppID":"","Type":0},"HasProductId":0,"Ticket":"","ImgHeight":0,"ImgWidth":0,"SubMsgType":0,"NewMsgId":1120003476579027600,"MMPeerUserName":"@@4aa0ae1e1ebc568b613fa43ce93b478df0339f73340d87083822c2016d2e53d9","MMDigest":"感恩的心 ","MMIsSend":false,"MMIsChatRoom":true,"MMUnread":false,"LocalID":"1120003476579027592","ClientMsgId":"1120003476579027592","MMActualContent":"HTML-CODE","MMActualSender":"@9ad4ba13fac52c55d323521b67f7cc39","MMDigestTime":"22:15","MMDisplayTime":1462889712,"MMTime":"22:15","_h":126,"_index":0,"_offsetTop":0,"$$hashKey":"0QK", "MemberList": [{"Uin":0,"UserName":"@94e4b0db79ccc844d7bb4a2b1efac3ff","NickName":"李卓桓","AttrStatus":37996631,"PYInitial":"","PYQuanPin":"","RemarkPYInitial":"","RemarkPYQuanPin":"","MemberStatus":0,"DisplayName":"北京阿布","KeyWord":"liz"},{"Uin":0,"UserName":"@34887973779b7dd827366a31772cd83df223e6f71d9a79e44fe619aafe2901a4","NickName":"Tiger","AttrStatus":4292711,"PYInitial":"","PYQuanPin":"","RemarkPYInitial":"","RemarkPYQuanPin":"","MemberStatus":0,"DisplayName":"DisplayNameTiger","KeyWord":"","HeadImgUrl":"/cgi-bin/mmwebwx-bin/webwxgeticon?seq=0&username=@34887973779b7dd827366a31772cd83df223e6f71d9a79e44fe619aafe2901a4&skey=@crypt_f9cec94b_8517b7f9fec85f5a78a804c4f45f5536&chatroomid=@7b3dcd218431d79045cda3493c3179ae"}]}')
......@@ -51,26 +63,24 @@ test('Room smoke testing', t => {
}
const g = new Room(rawData)
t.equal(g.id , EXPECTED.id , 'id right')
t.equal(g.from.id , EXPECTED.from , 'from right')
t.is(g.id , EXPECTED.id , 'id right')
t.is(g.from.id , EXPECTED.from , 'from right')
const s = g.toString()
t.equal(typeof s, 'string', 'toString()')
t.is(typeof s, 'string', 'toString()')
t.end()
})
*/
test('TBW: Room static method', t => {
Room.attach(new Puppet())
const r = Room.find({
id: 'xxx'
}, {
limit: 1
})
t.ok(r.id, 'Room.find')
t.truthy(r.id, 'Room.find')
const rs = Room.findAll({
from: 'yyy'
......@@ -78,7 +88,7 @@ test('TBW: Room static method', t => {
limit: 2
})
t.equal(rs.length, 2, 'Room.findAll with limit 2')
t.is(rs.length, 2, 'Room.findAll with limit 2')
t.end()
// t.end()
})
'use strict'
import { test } from 'ava'
import {
UtilLib
, log
} from '../'
const co = require('co')
const test = require('tape')
const Express = require('express')
const http = require('http')
import express from 'express'
import http from 'http'
const log = require('../src/npmlog-env')
const UtilLib = require('../src/util-lib')
// 'use strict'
// const co = require('co')
// const test = require('tape')
// const Express = require('express')
// const http = require('http')
// const log = require('../src/npmlog-env')
// const UtilLib = require('../src/util-lib')
test('Html smoking test', t => {
const HTML_BEFORE_STRIP = 'Outer<html>Inner</html>'
......@@ -28,28 +37,28 @@ test('Html smoking test', t => {
const PLAIN_AFTER = '&&&[流汗]'
const strippedHtml = UtilLib.stripHtml(HTML_BEFORE_STRIP)
t.equal(strippedHtml, HTML_AFTER_STRIP, 'should strip html as expected')
t.is(strippedHtml, HTML_AFTER_STRIP, 'should strip html as expected')
const unescapedHtml = UtilLib.unescapeHtml(HTML_BEFORE_UNESCAPE)
t.equal(unescapedHtml, HTML_AFTER_UNESCAPE, 'should unescape html as expected')
t.is(unescapedHtml, HTML_AFTER_UNESCAPE, 'should unescape html as expected')
for (let i=0; i<EMOJI_BEFORE_DIGEST.length; i++) {
const emojiDigest = UtilLib.digestEmoji(EMOJI_BEFORE_DIGEST[i])
t.equal(emojiDigest, EMOJI_AFTER_DIGEST[i], 'should digest emoji string ' + i + ' as expected')
t.is(emojiDigest, EMOJI_AFTER_DIGEST[i], 'should digest emoji string ' + i + ' as expected')
}
const plainText = UtilLib.plainText(PLAIN_BEFORE)
t.equal(plainText, PLAIN_AFTER, 'should convert plain text as expected')
t.is(plainText, PLAIN_AFTER, 'should convert plain text as expected')
t.end()
// t.end()
})
test('Media download smoking test', t => {
const app = new require('express')()
const app = express()
app.use(require('cookie-parser')())
app.get('/ding', function(req, res) {
// console.log(req.cookies)
t.ok(req.cookies, 'should has cookies in req')
t.equal(req.cookies.life, '42', 'should has a cookie named life value 42')
t.truthy(req.cookies, 'should has cookies in req')
t.is(req.cookies.life, '42', 'should has a cookie named life value 42')
res.end('dong')
})
......@@ -64,9 +73,9 @@ test('Media download smoking test', t => {
.then(s => {
s.on('data', (chunk) => {
// console.log(`BODY: ${chunk}`)
t.equal(chunk.toString(), 'dong', 'should success download dong from downloadStream()')
t.is(chunk.toString(), 'dong', 'should success download dong from downloadStream()')
server.close()
t.end()
// t.end()
})
})
.catch(e => {
......@@ -74,23 +83,23 @@ test('Media download smoking test', t => {
})
})
test('getPort', t => {
test('getPort', async t => {
const PORT = 8788
co(function* () {
let port = yield UtilLib.getPort(PORT)
t.equal(port, PORT, 'should equal exactly PORT when it is available')
// co(function* () {
let port = await UtilLib.getPort(PORT)
t.is(port, PORT, 'should equal exactly PORT when it is available')
const app = new Express()
const app = express()
const server = app.listen(PORT)
port = yield UtilLib.getPort(PORT)
port = await UtilLib.getPort(PORT)
server.close()
t.equal(port, PORT+1, 'should bigger then PORT when it is not availble')
t.is(port, PORT+1, 'should bigger then PORT when it is not availble')
}).catch(e => {
t.fail(e)
}).then(_ => {
t.end()
})
// }).catch(e => {
// t.fail(e)
// }).then(_ => {
// t.end()
// })
})
'use strict'
import { test } from 'ava'
const path = require('path')
const co = require('co')
const test = require('tape')
import path from 'path'
const log = require('../src/npmlog-env')
import { Browser, By } from 'selenium-webdriver'
const WebDriver = require('selenium-webdriver')
const Browser = WebDriver.Browser
const By = WebDriver.By
import {
PuppetWeb
, log
} from '../'
const PuppetWebBrowser = require('../src/puppet-web/browser')
const PuppetWebBridge = require('../src/puppet-web/bridge')
// 'use strict'
test('WebDriver process create & quit test', t => {
co(function* () {
const b = new PuppetWebBrowser()
t.ok(b, 'should instanciate a browser')
// const path = require('path')
// const co = require('co')
// const test = require('tape')
yield b.init()
// const log = require('../src/npmlog-env')
// const WebDriver = require('selenium-webdriver')
// const Browser = WebDriver.Browser
// const By = WebDriver.By
// const PuppetWebBrowser = require('../src/puppet-web/browser')
// const PuppetWebBridge = require('../src/puppet-web/bridge')
test('WebDriver process create & quit test', async t => {
// co(function* () {
const b = new PuppetWeb.Browser()
t.truthy(b, 'should instanciate a browser')
await b.init()
t.pass('should be inited successful')
yield b.open()
await b.open()
t.pass('should open successful')
let pids = yield b.getBrowserPids()
t.ok(pids.length > 0, 'should exist browser process after b.open()')
let pids = await b.getBrowserPids()
t.truthy(pids.length > 0, 'should exist browser process after b.open()')
// console.log(b.driver.getSession())
yield b.quit()
await b.quit()
t.pass('quited')
pids = yield b.getBrowserPids()
t.equal(pids.length, 0, 'no driver process after quit')
})
.catch(e => t.fail(e))
.then(_ => t.end())
pids = await b.getBrowserPids()
t.is(pids.length, 0, 'no driver process after quit')
// })
// .catch(e => t.fail(e))
// .then(_ => t.end())
return
// return
})
test('WebDriver smoke testing', function(t) {
const wb = new PuppetWebBrowser()
t.ok(wb, 'Browser instnace')
test('WebDriver smoke testing', async t => {
const wb = new PuppetWeb.Browser()
t.truthy(wb, 'Browser instnace')
const mockPuppet = {browser: wb}
const bridge = new PuppetWebBridge({puppet: mockPuppet})
t.ok(bridge, 'Bridge instnace')
const bridge = new PuppetWeb.Bridge({puppet: mockPuppet})
t.truthy(bridge, 'Bridge instnace')
var driver // for help function `execute`
co(function* () {
const m = (yield wb.getBrowserPids()).length
t.equal(m, 0, 'should has no browser process before get()')
// co(function* () {
const m = (await wb.getBrowserPids()).length
t.is(m, 0, 'should has no browser process before get()')
driver = yield wb.initDriver()
t.ok(driver, 'should init driver success')
driver = await wb.initDriver()
t.truthy(driver, 'should init driver success')
const injectio = bridge.getInjectio()
t.ok(injectio.length > 10, 'should got injectio script')
t.truthy(injectio.length > 10, 'should got injectio script')
// XXX: if get rid of this dummy,
// driver.get() will fail due to cant start phantomjs process
yield Promise.resolve()
// 20160828 fixed in new version of selenium webdriver
// await Promise.resolve()
yield driver.get('https://wx.qq.com/')
await driver.get('https://wx.qq.com/')
t.pass('should open wx.qq.com')
const n = (yield wb.getBrowserPids()).length
const n = (await wb.getBrowserPids()).length
// console.log(n)
// yield new Promise((resolve) => {
// await new Promise((resolve) => {
// setTimeout(() => {
// resolve()
// }, 3000)
// })
t.ok(n > 0, 'should exist browser process after get()')
const retAdd = yield driverExecute('return 1+1')
t.equal(retAdd, 2, 'should return 2 for execute 1+1 in browser')
const retInject = yield driverExecute(injectio, 8788)
t.ok(retInject, 'should return a object contains status of inject operation')
t.equal(retInject.code, 200, 'should got code 200 for a success wechaty inject')
})
.catch(e => t.fail('promise rejected. e:' + e)) // Rejected
.then(_ => { // Finally
wb.quit()
.then(_ => t.end())
})
.catch(e => t.fail(e)) // Exception
t.truthy(n > 0, 'should exist browser process after get()')
const retAdd = await driverExecute('return 1+1')
t.is(retAdd, 2, 'should return 2 for execute 1+1 in browser')
const retInject = await driverExecute(injectio, 8788)
t.truthy(retInject, 'should return a object contains status of inject operation')
t.is(retInject.code, 200, 'should got code 200 for a success wechaty inject')
await wb.quit()
// })
// .catch(e => t.fail('promise rejected. e:' + e)) // Rejected
// .then(_ => { // Finally
// wb.quit()
// .then(_ => t.end())
// })
// .catch(e => t.fail(e)) // Exception
return
......
const test = require('tape')
const Wechaty = require('../')
import { test } from 'ava'
import { Wechaty } from '../'
test('Wechaty Framework', t => {
t.ok(Wechaty , 'should export Wechaty')
t.ok(Wechaty.Message , 'should export Wechaty.Message')
t.ok(Wechaty.Contact , 'should export Wechaty.Contact')
t.ok(Wechaty.Room , 'should export Wechaty.Room')
t.truthy(Wechaty , 'should export Wechaty')
t.truthy(Wechaty.Message , 'should export Wechaty.Message')
t.truthy(Wechaty.Contact , 'should export Wechaty.Contact')
t.truthy(Wechaty.Room , 'should export Wechaty.Room')
t.ok(Wechaty.IoBot , 'should export Wechaty.IoBot')
t.truthy(Wechaty.IoBot , 'should export Wechaty.IoBot')
t.ok(Wechaty.log , 'should export Wechaty.log')
t.truthy(Wechaty.log , 'should export Wechaty.log')
t.ok(Wechaty.Puppet , 'should export Wechaty.Puppet')
t.ok(Wechaty.PuppetWeb , 'should export Wechaty.PuppetWeb')
t.truthy(Wechaty.Puppet , 'should export Wechaty.Puppet')
t.truthy(Wechaty.PuppetWeb , 'should export Wechaty.PuppetWeb')
const bot = new Wechaty()
t.equal(bot.version(true), Wechaty.version, 'should export version in package.json')
t.end()
t.is(bot.version(true), Wechaty.version, 'should export version in package.json')
})
test('Wechaty Config setting', t => {
const Config = Wechaty.Config
t.ok(Config , 'should export Wechaty.Config')
t.ok(Config.DEFAULT_HEAD , 'should has DEFAULT_HEAD')
t.ok(Config.DEFAULT_PUPPET , 'should has DEFAULT_PUPPET')
t.ok(Config.DEFAULT_PORT , 'should has DEFAULT_PORT')
t.ok(Config.DEFAULT_PUPPET_PORT, 'should has DEFAULT_PUPPET_PORT')
t.end()
t.truthy(Config , 'should export Wechaty.Config')
t.truthy(Config.DEFAULT_HEAD , 'should has DEFAULT_HEAD')
t.truthy(Config.DEFAULT_PUPPET , 'should has DEFAULT_PUPPET')
t.truthy(Config.DEFAULT_PORT , 'should has DEFAULT_PORT')
t.truthy(Config.DEFAULT_PUPPET_PORT, 'should has DEFAULT_PUPPET_PORT')
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册