diff --git a/src/create-fixture.spec.ts b/src/create-fixture.spec.ts new file mode 100755 index 0000000000000000000000000000000000000000..78cc41c6f965f25ead759ff230fade33ff3eb6e0 --- /dev/null +++ b/src/create-fixture.spec.ts @@ -0,0 +1,81 @@ +#!/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]}`) + } + } +}) diff --git a/src/create-fixture.ts b/src/create-fixture.ts new file mode 100644 index 0000000000000000000000000000000000000000..d9781dac481a5e3b2eb53a8f73ce805ce979f462 --- /dev/null +++ b/src/create-fixture.ts @@ -0,0 +1,82 @@ +/* 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 { + 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(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 }