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

fix tests by PuppetMock and sinon.sandbox

上级 757181e4
......@@ -82,7 +82,7 @@ export abstract class PuppetAccessory extends EventEmitter {
log.verbose('PuppetAccessory', '<%s> constructor(%s)',
this[PUPPET_ACCESSORY_NAME],
name,
name || '',
)
}
......
......@@ -33,11 +33,12 @@ import {
import {
log,
} from '../config'
} from '../config'
import {
ContactPayload,
} from '../puppet/contact'
} from '../puppet/contact'
import Profile from '../profile'
import Wechaty from '../wechaty'
import {
MockContact,
......@@ -54,14 +55,23 @@ export class PuppetMock extends Puppet {
private user?: MockContact
constructor(
public options: PuppetOptions,
public options: PuppetOptions = {} as any,
) {
super(options, {
Contact: MockContact,
FriendRequest: MockFriendRequest,
Message: MockMessage,
Room: MockRoom,
})
super(
options.profile
? options
: {
profile: new Profile(),
wechaty: new Wechaty(),
}
,
{
Contact: MockContact,
FriendRequest: MockFriendRequest,
Message: MockMessage,
Room: MockRoom,
},
)
}
public toString() {
......
......@@ -57,9 +57,7 @@ test('Puppet smoke testing', async t => {
})
test('login/logout events', sinonTest(async function (t: test.Test) {
const sandbox = sinon.sandbox.create()
const sandbox = sinon.createSandbox()
sandbox.stub(Contact, 'findAll')
.onFirstCall().resolves([])
.onSecondCall().resolves([1])
......
......@@ -801,6 +801,7 @@ export class PuppetPuppeteer extends Puppet {
private contactParseRawPayload(
rawPayload: PuppeteerContactRawPayload,
): ContactPayload {
log.verbose('PuppetPuppeteer', 'contactParseRawPayload(%s)', rawPayload)
if (!rawPayload.UserName) {
throw new Error('contactParsePayload() got empty rawPayload!')
}
......@@ -842,6 +843,7 @@ export class PuppetPuppeteer extends Puppet {
}
public async contactPayload(contact: PuppeteerContact): Promise<ContactPayload> {
log.verbose('PuppetPuppeteer', 'contactPayload(%s)', contact)
try {
const rawPayload = await this.bridge.getContact(contact.id) as PuppeteerContactRawPayload
return this.contactParseRawPayload(rawPayload)
......
......@@ -24,49 +24,49 @@ import * as sinon from 'sinon'
import cloneClass from 'clone-class'
import Profile from '../profile'
import Wechaty from '../wechaty' // `Wechaty` need to be imported before `Puppet`
import {
log,
} from '../config'
import {
PuppetMock,
} from '../puppet-mock/puppet-mock'
import PuppetPuppeteer from './puppet-puppeteer'
import PuppeteerContact from './puppeteer-contact'
test('Contact smoke testing', async t => {
// tslint:disable-next-line:variable-name
const MyContact = cloneClass(PuppeteerContact)
const puppet = new PuppetPuppeteer({
profile: new Profile(),
wechaty: new Wechaty(),
})
/* tslint:disable:variable-name */
const UserName = '@0bb3e4dd746fdbd4a80546aef66f4085'
const NickName = 'NickNameTest'
const RemarkName = 'AliasTest'
const sandbox = sinon.sandbox.create()
const sandbox = sinon.createSandbox()
sandbox.stub(puppet, 'contactPayload')
.callsFake(function(id: string) {
function mockContactPayload(id: string) {
log.verbose('PuppeteerContactTest', 'mockContactPayload(%s)', id)
return new Promise<any>((resolve, reject) => {
if (id !== UserName) return resolve({})
setTimeout(() => {
return resolve({
UserName: UserName,
NickName: NickName,
RemarkName: RemarkName,
})
}, 10)
setImmediate(() => resolve({
UserName: UserName,
NickName: NickName,
RemarkName: RemarkName,
}))
})
})
}
const puppet = new PuppetMock()
sandbox.stub(puppet, 'contactPayload').callsFake(mockContactPayload)
// tslint:disable-next-line:variable-name
const MyContact = cloneClass(PuppeteerContact)
MyContact.puppet = puppet
const c = new MyContact(UserName)
t.is(c.id, UserName, 'id/UserName right')
await c.ready()
t.is(c.id , UserName, 'UserName set')
t.is(c.name(), NickName, 'NickName set')
t.is(c.alias(), RemarkName, 'should get the right alias from Contact')
......
......@@ -81,7 +81,9 @@ export class PuppeteerContact extends Contact implements Sayable {
await this.puppet.send(m)
}
public name() { return Misc.plainText(this.payload && this.payload.name || '') }
public name(): string {
return Misc.plainText(this.payload && this.payload.name || '')
}
public alias() : string | null
public alias(newAlias: string) : Promise<void>
......@@ -179,22 +181,23 @@ export class PuppeteerContact extends Contact implements Sayable {
}
public async ready(): Promise<void> {
// log.silly('PuppeteerContact', 'ready(' + (contactGetter ? typeof contactGetter : '') + ')')
if (!this.id) {
const e = new Error('ready() call on an un-inited contact')
throw e
}
log.silly('PuppeteerContact', 'ready()')
if (this.isReady()) { // already ready
log.silly('PuppeteerContact', 'ready() isReady() true')
return
}
try {
this.payload = await this.puppet.contactPayload(this)
log.silly('PuppeteerContact', `contactGetter(${this.id}) resolved`)
log.silly('PuppeteerContact', `ready() this.puppet.contactPayload(%s) resolved`, this)
console.log(this.payload)
} catch (e) {
log.error('PuppeteerContact', `contactGetter(${this.id}) exception: %s`, e.message)
log.error('PuppeteerContact', `ready() this.puppet.contactPayload(%s) exception: %s`,
this,
e.message,
)
Raven.captureException(e)
throw e
}
......
......@@ -87,7 +87,7 @@ test('ready()', async t => {
const expectedMsgId = '3009511950433684462'
// Mock
function mockGetContact(id: string) {
function mockContactPayload(id: string) {
log.silly('TestMessage', `mocked getContact(${id})`)
return new Promise((resolve, reject) => {
let obj = {}
......@@ -119,10 +119,11 @@ test('ready()', async t => {
// config.puppetInstance()
// .getContact = mockGetContact
MyRoom.puppet = MyContact.puppet = MyMessage.puppet = {
...puppet,
getContact: mockGetContact,
} as any
const sandbox = sinon.createSandbox()
const puppet = new PuppetMock()
sandbox.stub(puppet, 'contactPayload').callsFake(mockContactPayload)
MyRoom.puppet = MyContact.puppet = MyMessage.puppet = puppet
const m = new MyMessage(rawData)
......@@ -140,6 +141,8 @@ test('ready()', async t => {
t.is(fc.name() , expectedFromNickName, 'contact ready for FromNickName')
t.is(tc.id , expectedToUserName , 'contact ready for ToUserName')
t.is(tc.name() , expectedToNickName , 'contact ready for ToNickName')
sandbox.restore()
})
test('find()', async t => {
......@@ -216,7 +219,7 @@ test('mentioned()', async t => {
// puppet1 = { getContact: mockContactGetter }
// config.puppetInstance(puppet1)
// }
const sandbox = sinon.sandbox.create()
const sandbox = sinon.createSandbox()
const puppet = new PuppetMock({
profile: new Profile(),
......
......@@ -20,11 +20,12 @@
*/
// tslint:disable:no-shadowed-variable
import * as test from 'blue-tape'
// import * as sinon from 'sinon'
import * as sinon from 'sinon'
import cloneClass from 'clone-class'
import Profile from '../profile'
import PuppetMock from '../puppet-mock'
import Wechaty from '../wechaty'
import PuppetPuppeteer from './puppet-puppeteer'
......@@ -39,13 +40,10 @@ const MyContact = cloneClass(PuppeteerContact)
// tslint:disable-next-line:variable-name
const MyMessage = cloneClass(PuppeteerMessage)
const puppet = new PuppetPuppeteer({
profile: new Profile(),
wechaty: new Wechaty(),
})
const puppet = new PuppetMock()
const MOCK_USER_ID = 'TEST-USER-ID'
puppet.login(MyContact.load(MOCK_USER_ID))
puppet.emit('login', MyContact.load(MOCK_USER_ID))
MyContact.puppet = MyMessage.puppet = MyRoom.puppet = puppet
......@@ -106,7 +104,7 @@ test('Room smoking test', async t => {
}
// Mock
const mockContactGetter = function (id: string) {
const mockContactPayload = function (id: string) {
return new Promise((resolve, reject) => {
if (id !== EXPECTED.id && !(id in CONTACT_LIST)) return resolve({})
if (id === EXPECTED.id) {
......@@ -134,7 +132,11 @@ test('Room smoking test', async t => {
// puppet = { getContact: mockContactGetter }
// config.puppetInstance(puppet)
// }
MyContact.puppet = MyMessage.puppet = MyRoom.puppet = { getContact: mockContactGetter } as any
const sandbox = sinon.createSandbox()
const puppet = new PuppetMock()
sandbox.stub(puppet, 'contactPayload').callsFake(mockContactPayload)
MyContact.puppet = MyMessage.puppet = MyRoom.puppet = puppet
await r.ready()
t.is((r as any).payload['id'] , EXPECTED.id, 'should set id/UserName')
......
......@@ -73,12 +73,13 @@ export interface ContactPayload {
* [Examples/Contact-Bot]{@link https://github.com/Chatie/wechaty/blob/master/examples/contact-bot.ts}
*/
export abstract class Contact extends PuppetAccessory implements Sayable {
protected static readonly pool = new Map<string, Contact>()
// tslint:disable-next-line:variable-name
public static Type = ContactType
public static Gender = Gender
protected static readonly pool = new Map<string, Contact>()
/**
* @private
* About the Generic: https://stackoverflow.com/q/43003970/1123955
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册