Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wechaty
wechaty
提交
7154db21
W
wechaty
项目概览
wechaty
/
wechaty
上一次同步 大约 3 年
通知
304
Star
10499
Fork
6
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
152
列表
看板
标记
里程碑
合并请求
1
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
wechaty
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
152
Issue
152
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7154db21
编写于
5月 10, 2018
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix tests by PuppetMock and sinon.sandbox
上级
757181e4
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
79 addition
and
60 deletion
+79
-60
src/puppet-accessory.ts
src/puppet-accessory.ts
+1
-1
src/puppet-mock/puppet-mock.ts
src/puppet-mock/puppet-mock.ts
+20
-10
src/puppet-puppeteer/puppet-puppeteer.spec.ts
src/puppet-puppeteer/puppet-puppeteer.spec.ts
+1
-3
src/puppet-puppeteer/puppet-puppeteer.ts
src/puppet-puppeteer/puppet-puppeteer.ts
+2
-0
src/puppet-puppeteer/puppeteer-contact.spec.ts
src/puppet-puppeteer/puppeteer-contact.spec.ts
+23
-23
src/puppet-puppeteer/puppeteer-contact.ts
src/puppet-puppeteer/puppeteer-contact.ts
+11
-8
src/puppet-puppeteer/puppeteer-message.spec.ts
src/puppet-puppeteer/puppeteer-message.spec.ts
+9
-6
src/puppet-puppeteer/puppeteer-room.spec.ts
src/puppet-puppeteer/puppeteer-room.spec.ts
+10
-8
src/puppet/contact.ts
src/puppet/contact.ts
+2
-1
未找到文件。
src/puppet-accessory.ts
浏览文件 @
7154db21
...
...
@@ -82,7 +82,7 @@ export abstract class PuppetAccessory extends EventEmitter {
log
.
verbose
(
'
PuppetAccessory
'
,
'
<%s> constructor(%s)
'
,
this
[
PUPPET_ACCESSORY_NAME
],
name
,
name
||
''
,
)
}
...
...
src/puppet-mock/puppet-mock.ts
浏览文件 @
7154db21
...
...
@@ -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
()
{
...
...
src/puppet-puppeteer/puppet-puppeteer.spec.ts
浏览文件 @
7154db21
...
...
@@ -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
])
...
...
src/puppet-puppeteer/puppet-puppeteer.ts
浏览文件 @
7154db21
...
...
@@ -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
)
...
...
src/puppet-puppeteer/puppeteer-contact.spec.ts
浏览文件 @
7154db21
...
...
@@ -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
'
)
...
...
src/puppet-puppeteer/puppeteer-contact.ts
浏览文件 @
7154db21
...
...
@@ -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
}
...
...
src/puppet-puppeteer/puppeteer-message.spec.ts
浏览文件 @
7154db21
...
...
@@ -87,7 +87,7 @@ test('ready()', async t => {
const
expectedMsgId
=
'
3009511950433684462
'
// Mock
function
mock
GetContact
(
id
:
string
)
{
function
mock
ContactPayload
(
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
(),
...
...
src/puppet-puppeteer/puppeteer-room.spec.ts
浏览文件 @
7154db21
...
...
@@ -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
mockContact
Getter
=
function
(
id
:
string
)
{
const
mockContact
Payload
=
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
'
)
...
...
src/puppet/contact.ts
浏览文件 @
7154db21
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录