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

try to fix puppeteer-friend.spec

上级 32f0bda4
......@@ -90,7 +90,7 @@ export class Contact extends PuppetAccessory implements Sayable {
*/
public static load<T extends typeof Contact>(this: T, id: string): T['prototype'] {
if (!id || typeof id !== 'string') {
throw new Error('Contact.load(): id not found')
throw new Error('Contact.load(): id not found: ' + id)
}
const existingContact = this.pool.get(id)
......
......@@ -369,14 +369,18 @@ export class PuppetPuppeteer extends Puppet {
return MessageType.Image
case WebMessageType.VOICE:
return MessageType.Audio
return MessageType.Audio
case WebMessageType.MICROVIDEO:
case WebMessageType.VIDEO:
return MessageType.Video
return MessageType.Video
case WebMessageType.TEXT:
return MessageType.Text
return MessageType.Text
case WebMessageType.SYS:
// FIXME: should we use better message type at here???
return MessageType.Text
// VERIFYMSG = 37,
// POSSIBLEFRIEND_MSG = 40,
......@@ -388,7 +392,6 @@ export class PuppetPuppeteer extends Puppet {
// VOIPNOTIFY = 52,
// VOIPINVITE = 53,
// SYSNOTICE = 9999,
// SYS = 10000,
// RECALLED = 10002,
default:
throw new Error('un-supported WebMsgType: ' + webMsgType)
......@@ -556,7 +559,7 @@ export class PuppetPuppeteer extends Puppet {
const rawPayload = await this.bridge.getContact(id) as WebContactRawPayload
return rawPayload
} catch (e) {
log.error('PuppetPuppeteer', 'contactRawPayload(%d) exception: %s', id, e.message)
log.error('PuppetPuppeteer', 'contactRawPayload(%s) exception: %s', id, e.message)
Raven.captureException(e)
throw e
}
......
......@@ -19,55 +19,52 @@
*/
// tslint:disable:no-shadowed-variable
import * as test from 'blue-tape'
// import * as sinon from 'sinon'
import * as sinon from 'sinon'
// const sinonTest = require('sinon-test')(sinon)
import cloneClass from 'clone-class'
import Profile from '../profile'
import Wechaty from '../wechaty'
import {
log,
} from '../config'
import {
Contact,
} from '../contact'
import {
Profile,
} from '../profile'
import {
Wechaty,
} from '../wechaty'
import {
// Puppet,
// FriendRequest,
WebRecomendInfo,
} from './web-schemas'
// import {
// PuppetMock,
// } from '../puppet-mock/'
import Contact from '../contact'
import Message from '../message'
import FriendRequest from '../friend-request'
import { PuppetPuppeteer } from './puppet-puppeteer'
PuppetPuppeteer,
} from './puppet-puppeteer'
import {
WebMessageRawPayload,
} from './web-schemas'
test('PuppetPuppeteerFriendRequest.receive smoke testing', async t => {
// tslint:disable-next-line:variable-name
const MyFriendRequest = cloneClass(FriendRequest)
// tslint:disable-next-line:variable-name
const MyContact = cloneClass(Contact)
const wechaty = new Wechaty()
const puppet = new PuppetPuppeteer({
profile: new Profile(),
wechaty: new Wechaty(),
wechaty,
})
MyFriendRequest.puppet = MyContact.puppet = puppet
;
(wechaty as any).initPuppetAccessory(puppet)
/* tslint:disable:max-line-length */
const rawMessageData = `
const rawMessagePayload: WebMessageRawPayload = JSON.parse(`
{"MsgId":"3225371967511173931","FromUserName":"fmessage","ToUserName":"@f7321198e0349f1b38c9f2ef158f70eb","MsgType":37,"Content":"&lt;msg fromusername=\\"wxid_a8d806dzznm822\\" encryptusername=\\"v1_c1e03a32c60dd9a9e14f1092132808a2de0ad363f79b303693654282954fbe4d3e12481166f4b841f28de3dd58b0bd54@stranger\\" fromnickname=\\"李卓桓.PreAngel\\" content=\\"我是群聊&amp;quot;Wechaty&amp;quot;的李卓桓.PreAngel\\" shortpy=\\"LZHPREANGEL\\" imagestatus=\\"3\\" scene=\\"14\\" country=\\"CN\\" province=\\"Beijing\\" city=\\"Haidian\\" sign=\\"投资人中最会飞的程序员。好友请加 918999 ,因为本号好友已满。\\" percard=\\"1\\" sex=\\"1\\" alias=\\"zixia008\\" weibo=\\"\\" weibonickname=\\"\\" albumflag=\\"0\\" albumstyle=\\"0\\" albumbgimgid=\\"911623988445184_911623988445184\\" snsflag=\\"49\\" snsbgimgid=\\"http://mmsns.qpic.cn/mmsns/zZSYtpeVianSQYekFNbuiajROicLficBzzeGuvQjnWdGDZ4budZovamibQnoKWba7D2LeuQRPffS8aeE/0\\" snsbgobjectid=\\"12183966160653848744\\" mhash=\\"\\" mfullhash=\\"\\" bigheadimgurl=\\"http://wx.qlogo.cn/mmhead/ver_1/xct7OPTbuU6iaS8gTaK2VibhRs3rATwnU1rCUwWu8ic89EGOynaic2Y4MUdKr66khhAplcfFlm7xbXhum5reania3fXDXH6CI9c3Bb4BODmYAh04/0\\" smallheadimgurl=\\"http://wx.qlogo.cn/mmhead/ver_1/xct7OPTbuU6iaS8gTaK2VibhRs3rATwnU1rCUwWu8ic89EGOynaic2Y4MUdKr66khhAplcfFlm7xbXhum5reania3fXDXH6CI9c3Bb4BODmYAh04/132\\" ticket=\\"v2_ba70dfbdb1b10168d61c1ab491be19e219db11ed5c28701f605efb4dccbf132f664d8a4c9ef6e852b2a4e8d8638be81d125c2e641f01903669539c53f1e582b2@stranger\\" opcode=\\"2\\" googlecontact=\\"\\" qrticket=\\"\\" chatroomusername=\\"2332413729@chatroom\\" sourceusername=\\"\\" sourcenickname=\\"\\"&gt;&lt;brandlist count=\\"0\\" ver=\\"670564024\\"&gt;&lt;/brandlist&gt;&lt;/msg&gt;","Status":3,"ImgStatus":1,"CreateTime":1475567560,"VoiceLength":0,"PlayLength":0,"FileName":"","FileSize":"","MediaId":"","Url":"","AppMsgType":0,"StatusNotifyCode":0,"StatusNotifyUserName":"","RecommendInfo":{"UserName":"@04a0fa314d0d8d50dc54e2ec908744ebf46b87404d143fd9a6692182dd90bd49","NickName":"李卓桓.PreAngel","Province":"北京","City":"海淀","Content":"我是群聊\\"Wechaty\\"的李卓桓.PreAngel","Signature":"投资人中最会飞的程序员。好友请加 918999 ,因为本号好友已满。","Alias":"zixia008","Scene":14,"AttrStatus":233251,"Sex":1,"Ticket":"v2_ba70dfbdb1b10168d61c1ab491be19e219db11ed5c28701f605efb4dccbf132f664d8a4c9ef6e852b2a4e8d8638be81d125c2e641f01903669539c53f1e582b2@stranger","OpCode":2,"HeadImgUrl":"/cgi-bin/mmwebwx-bin/webwxgeticon?seq=0&username=@04a0fa314d0d8d50dc54e2ec908744ebf46b87404d143fd9a6692182dd90bd49&skey=@crypt_f9cec94b_5b073dca472bd5e41771d309bb8c37bd&msgid=3225371967511173931","MMFromVerifyMsg":true},"ForwardFlag":0,"AppInfo":{"AppID":"","Type":0},"HasProductId":0,"Ticket":"","ImgHeight":0,"ImgWidth":0,"SubMsgType":0,"NewMsgId":3225371967511174000,"MMPeerUserName":"fmessage","MMDigest":"李卓桓.PreAngel想要将你加为朋友","MMIsSend":false,"MMIsChatRoom":false,"MMUnread":true,"LocalID":"3225371967511173931","ClientMsgId":"3225371967511173931","MMActualContent":"&lt;msg fromusername=\\"wxid_a8d806dzznm822\\" encryptusername=\\"v1_c1e03a32c60dd9a9e14f1092132808a2de0ad363f79b303693654282954fbe4d3e12481166f4b841f28de3dd58b0bd54@stranger\\" fromnickname=\\"李卓桓.PreAngel\\" content=\\"我是群聊&amp;quot;Wechaty&amp;quot;的李卓桓.PreAngel\\" shortpy=\\"LZHPREANGEL\\" imagestatus=\\"3\\" scene=\\"14\\" country=\\"CN\\" province=\\"Beijing\\" city=\\"Haidian\\" sign=\\"投资人中最会飞的程序员。好友请加 918999 ,因为本号好友已满。\\" percard=\\"1\\" sex=\\"1\\" alias=\\"zixia008\\" weibo=\\"\\" weibonickname=\\"\\" albumflag=\\"0\\" albumstyle=\\"0\\" albumbgimgid=\\"911623988445184_911623988445184\\" snsflag=\\"49\\" snsbgimgid=\\"http://mmsns.qpic.cn/mmsns/zZSYtpeVianSQYekFNbuiajROicLficBzzeGuvQjnWdGDZ4budZovamibQnoKWba7D2LeuQRPffS8aeE/0\\" snsbgobjectid=\\"12183966160653848744\\" mhash=\\"\\" mfullhash=\\"\\" bigheadimgurl=\\"http://wx.qlogo.cn/mmhead/ver_1/xct7OPTbuU6iaS8gTaK2VibhRs3rATwnU1rCUwWu8ic89EGOynaic2Y4MUdKr66khhAplcfFlm7xbXhum5reania3fXDXH6CI9c3Bb4BODmYAh04/0\\" smallheadimgurl=\\"http://wx.qlogo.cn/mmhead/ver_1/xct7OPTbuU6iaS8gTaK2VibhRs3rATwnU1rCUwWu8ic89EGOynaic2Y4MUdKr66khhAplcfFlm7xbXhum5reania3fXDXH6CI9c3Bb4BODmYAh04/132\\" ticket=\\"v2_ba70dfbdb1b10168d61c1ab491be19e219db11ed5c28701f605efb4dccbf132f664d8a4c9ef6e852b2a4e8d8638be81d125c2e641f01903669539c53f1e582b2@stranger\\" opcode=\\"2\\" googlecontact=\\"\\" qrticket=\\"\\" chatroomusername=\\"2332413729@chatroom\\" sourceusername=\\"\\" sourcenickname=\\"\\"&gt;&lt;brandlist count=\\"0\\" ver=\\"670564024\\"&gt;&lt;/brandlist&gt;&lt;/msg&gt;","MMActualSender":"fmessage","MMDigestTime":"15:52","MMDisplayTime":1475567560,"MMTime":"15:52"}
`
const rawObj = JSON.parse(rawMessageData)
`)
const info = (rawObj.RecommendInfo as WebRecomendInfo)
const info = rawMessagePayload.RecommendInfo!
// FIXME: use wechaty.Contact.load
const contact = MyContact.load(info.UserName)
const hello = info.Content
const ticket = info.Ticket
const contact = wechaty.Contact.load(info.UserName)
const hello = info.Content
const ticket = info.Ticket
const fr = MyFriendRequest.createReceive(
const fr = wechaty.FriendRequest.createReceive(
contact,
hello,
ticket,
......@@ -75,38 +72,50 @@ test('PuppetPuppeteerFriendRequest.receive smoke testing', async t => {
t.is(fr.hello(), '我是群聊"Wechaty"的李卓桓.PreAngel', 'should has right request message')
t.true(fr.contact() instanceof Contact, 'should have a Contact instance')
t.is(fr.type(), MyFriendRequest.Type.Receive, 'should be receive type')
t.is(fr.type(), wechaty.FriendRequest.Type.Receive, 'should be receive type')
})
test('PuppetPuppeteerFriendRequest.confirm smoke testing', async t => {
// tslint:disable-next-line:variable-name
const MyFriendRequest = cloneClass(FriendRequest)
// tslint:disable-next-line:variable-name
const MyContact = cloneClass(Contact)
// tslint:disable-next-line:variable-name
const MyMessage = cloneClass(Message)
const puppet = new PuppetPuppeteer({
const wechaty = new Wechaty()
const puppet = new PuppetPuppeteer({
profile: new Profile(),
wechaty: new Wechaty(),
wechaty,
})
MyMessage.puppet = MyFriendRequest.puppet = MyContact.puppet = puppet
;
(wechaty as any).initPuppetAccessory(puppet)
/* tslint:disable:max-line-length */
const rawMessageData = `
const rawMessagePayload: WebMessageRawPayload = JSON.parse(`
{"MsgId":"3382012679535022763","FromUserName":"@04a0fa314d0d8d50dc54e2ec908744ebf46b87404d143fd9a6692182dd90bd49","ToUserName":"@f7321198e0349f1b38c9f2ef158f70eb","MsgType":10000,"Content":"You have added 李卓桓.PreAngel as your WeChat contact. Start chatting!","Status":4,"ImgStatus":1,"CreateTime":1475569920,"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":3382012679535022600,"MMPeerUserName":"@04a0fa314d0d8d50dc54e2ec908744ebf46b87404d143fd9a6692182dd90bd49","MMDigest":"You have added 李卓桓.PreAngel as your WeChat contact. Start chatting!","MMIsSend":false,"MMIsChatRoom":false,"LocalID":"3382012679535022763","ClientMsgId":"3382012679535022763","MMActualContent":"You have added 李卓桓.PreAngel as your WeChat contact. Start chatting!","MMActualSender":"@04a0fa314d0d8d50dc54e2ec908744ebf46b87404d143fd9a6692182dd90bd49","MMDigestTime":"16:32","MMDisplayTime":1475569920,"MMTime":"16:32"}
`
const rawObj = JSON.parse(rawMessageData)
// FIXME: use wechaty.Message.create...()
const m = new MyMessage(rawObj)
m.puppet = puppet
`)
function mockMessageRawPayload(_: string) {
log.verbose('PuppeteerFriendRequestTest', 'mockMessageRawPayload()')
return rawMessagePayload
}
function mockContactPayload(_: string) {
log.verbose('PuppeteerFriendRequestTest', 'mockContactPayload()')
t.true(/^You have added (.+) as your WeChat contact. Start chatting!$/.test(m.text()), 'should match confirm message')
return {}
}
const contact = m.from()
const fr = MyFriendRequest.createConfirm(contact || new MyContact('xx'))
const sandbox = sinon.createSandbox()
sandbox.stub((puppet as any), 'messageRawPayload').callsFake(mockMessageRawPayload)
sandbox.stub(puppet, 'contactPayload') .callsFake(mockContactPayload)
const msg = wechaty.Message.createMT(rawMessagePayload.MsgId)
await msg.ready()
t.true(/^You have added (.+) as your WeChat contact. Start chatting!$/.test(msg.text()), 'should match confirm message')
const contact = msg.from()
const fr = wechaty.FriendRequest.createConfirm(contact || wechaty.Contact.load('xx'))
t.true(fr.contact() instanceof Contact, 'should have a Contact instance')
t.is(fr.type(), MyFriendRequest.Type.Confirm, 'should be confirm type')
t.is(fr.type(), wechaty.FriendRequest.Type.Confirm, 'should be confirm type')
sandbox.restore()
})
......@@ -112,7 +112,6 @@ export abstract class Puppet extends EventEmitter implements Sayable {
constructor(
public options: PuppetOptions,
// classes?: PuppetClasses,
) {
super()
......@@ -121,6 +120,9 @@ export abstract class Puppet extends EventEmitter implements Sayable {
this.state = new StateSwitch(this.constructor.name, log)
this.watchdog = new Watchdog(WATCHDOG_TIMEOUT, 'Puppet')
/**
* 1. Init Classes
*/
if ( !this.options.wechaty.Contact
|| !this.options.wechaty.FriendRequest
|| !this.options.wechaty.Message
......
......@@ -231,7 +231,7 @@ export class Wechaty extends PuppetAccessory implements Sayable {
public on(event: 'start' , listener: string | ((this: Wechaty) => void)) : this
public on(event: 'stop' , listener: string | ((this: Wechaty) => void)) : this
// guard for the above event: make sure it includes all the possible values
public on(event: never, listener: any): this
public on(event: never, listener: never): never
/**
* @desc Wechaty Class Event Type
......@@ -603,7 +603,7 @@ export class Wechaty extends PuppetAccessory implements Sayable {
* @deprecated
*/
public self(): Contact {
log.warn('Wechaty', 'self() DEPRECATED')
log.warn('Wechaty', 'self() DEPRECATED. use userSelf() instead.')
return this.userSelf()
}
......@@ -684,7 +684,7 @@ export class Wechaty extends PuppetAccessory implements Sayable {
* @private
*/
public async reset(reason?: string): Promise<void> {
log.verbose('Wechaty', 'reset() because %s', reason)
log.verbose('Wechaty', 'reset() because %s', reason || 'no reason')
await this.puppet.stop()
await this.puppet.start()
return
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册