-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-fixture.ts
121 lines (97 loc) · 2.29 KB
/
create-fixture.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* eslint-disable sort-keys */
import {
PuppetMock,
mock,
} from 'wechaty-puppet-mock'
import {
Wechaty,
WechatyBuilder,
Message,
Room,
Contact,
} from 'wechaty'
import type {
ContactSelf,
} from 'wechaty'
import type { EnvironmentMock } from './mock.js'
interface Fixture {
mocker: {
mocker : mock.Mocker,
room : mock.RoomMock,
bot : mock.ContactMock,
player : mock.ContactMock,
},
wechaty : {
wechaty : Wechaty,
room : Room,
bot : ContactSelf,
player : Contact,
/**
* A message should be that:
* 1. said by player
* 1. in room
* 1. with type `text`, random content
*/
message : Message,
},
moList: Message[],
mtList: Message[],
}
async function * createFixture (
environment?: EnvironmentMock,
): AsyncGenerator<Fixture> {
const mocker = new mock.Mocker()
if (environment) {
mocker.use(environment)
}
const puppet = new PuppetMock({ mocker })
const wechaty = WechatyBuilder.build({ puppet })
await wechaty.start()
const bot = mocker.createContact({ name: 'Bot' })
const player = mocker.createContact({ name: 'Player' })
mocker.login(bot)
const room = mocker.createRoom({
memberIdList: [
bot.id,
player.id,
],
})
const messageFuture = new Promise<Message>(resolve => wechaty.once('message', resolve))
player.say().to(bot)
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 {
mocker: {
mocker,
bot,
player,
room,
},
wechaty: {
wechaty,
bot : wechaty.currentUser,
player : (await wechaty.Contact.find({ id: player.id }))!,
room : (await wechaty.Room.find({ id: room.id }))!,
message,
},
moList,
mtList,
}
await wechaty.stop()
}
export { createFixture }