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

add createFixture (#2022)

上级 86db819f
#!/usr/bin/env ts-node
import {
test,
sinon,
} from 'tstest'
import { MockContact } from 'wechaty-puppet-mock/dist/src/mocker/user/mock-contact'
import { Message } from './user/message'
import { createFixture } from './create-fixture'
test('createFixture() initial state', async (t) => {
for await (const fixture of createFixture()) {
t.true(fixture.message instanceof Message, 'should have message instance')
t.equal(fixture.message.type(), Message.Type.Text, 'should have message with text type')
t.equal(typeof fixture.message.text(), 'string', 'should have message with text content')
t.equal(fixture.moList.length, 0, 'should be empty mo list')
t.equal(fixture.mtList.length, 0, 'should be empty mt list')
t.true(fixture.mary instanceof MockContact, 'should get mock contact mary')
t.true(fixture.mike instanceof MockContact, 'should get mock contact mike')
}
})
test('createFixture() Mobile Originated', async (t) => {
for await (const fixture of createFixture()) {
const spy = sinon.spy()
fixture.wechaty.on('message', spy)
fixture.user.say().to(fixture.mary)
await new Promise(setImmediate)
t.true(spy.called, 'should received message event')
t.equal(spy.args[0][0].from().id, fixture.user.id, 'should get user as from')
t.equal(spy.args[0][0].to().id, fixture.mary.id, 'should get mary as to')
t.equal(fixture.moList.length, 1, 'should be 1 mo')
t.equal(fixture.mtList.length, 0, 'should be empty mt list')
t.equal(fixture.moList[0].id, spy.args[0][0].id, 'should get the same message instance')
}
})
test('createFixture() Mobile Terminated', async (t) => {
for await (const fixture of createFixture()) {
const spy = sinon.spy()
fixture.wechaty.on('message', spy)
fixture.mary.say().to(fixture.user)
await new Promise(setImmediate)
t.true(spy.called, 'should received message event')
t.equal(spy.args[0][0].to().id, fixture.user.id, 'should get user as to')
t.equal(spy.args[0][0].from().id, fixture.mary.id, 'should get mary as from')
t.equal(fixture.moList.length, 0, 'should be 0 mo')
t.equal(fixture.mtList.length, 1, 'should be 1 mt')
t.equal(fixture.mtList[0].id, spy.args[0][0].id, 'should get the same message instance')
}
})
test('user.say() multiple times with moList', async t => {
for await (const fixture of createFixture()) {
const TEXT_LIST = [
'one',
'two',
'three',
]
for (const text of TEXT_LIST) {
await fixture.user.say(text).to(fixture.mary)
}
await new Promise(setImmediate)
t.equal(fixture.moList.length, TEXT_LIST.length, 'should receive all TEXT_LIST')
for (let i = 0; i < TEXT_LIST.length; i++) {
t.ok(fixture.moList[i], `should exist moList for ${i}`)
t.deepEqual(fixture.moList[i].text(), TEXT_LIST[i], `should get TEXT_LIST[${i}]: ${TEXT_LIST[i]}`)
}
}
})
/* eslint-disable sort-keys */
import {
PuppetMock,
Mocker,
} from 'wechaty-puppet-mock'
import { MockContact } from 'wechaty-puppet-mock/dist/src/mocker/user/mock-contact'
import { MockRoom } from 'wechaty-puppet-mock/dist/src/mocker/user/mock-room'
import { Wechaty } from './wechaty'
import { Message } from './user/message'
interface Fixture {
wechaty: Wechaty,
mocker: Mocker,
message: Message,
moList: Message[],
mtList: Message[],
user: MockContact,
mary: MockContact,
mike: MockContact,
room: MockRoom,
}
async function * createFixture (): AsyncGenerator<Fixture> {
const mocker = new Mocker()
const puppet = new PuppetMock({ mocker })
const wechaty = new Wechaty({ puppet })
await wechaty.start()
const [user, mike, mary] = mocker.createContacts(3)
mocker.login(user)
const room = mocker.createRoom({
memberIdList: [
user.id,
mike.id,
mary.id,
],
})
const messageFuture = new Promise<Message>(resolve => wechaty.once('message', resolve))
mike.say().to(room)
const message = await messageFuture
// Mobile Terminated
const mtList = [] as Message[]
const recordMobileTerminatedMessage = (message: Message) => {
if (!message.self()) {
mtList.push(message)
}
}
wechaty.on('message', recordMobileTerminatedMessage)
// Mobile Originated
const moList = [] as Message[]
const recordMobileOriginatedMessage = (message: Message) => {
if (message.self()) {
moList.push(message)
}
}
wechaty.on('message', recordMobileOriginatedMessage)
yield {
wechaty,
mocker,
message,
moList,
mtList,
user,
mary,
mike,
room,
}
await wechaty.stop()
}
export { createFixture }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册