/
math_master.spec.ts
executable file
·144 lines (119 loc) · 3.67 KB
/
math_master.spec.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
import {
test,
} from 'tstest'
import {
WechatyBuilder,
types,
} from 'wechaty'
import {
mock,
PuppetMock,
} from 'wechaty-puppet-mock'
import {
WechatyVorpal,
} from 'wechaty-vorpal'
import { MathMaster } from './math_master.js'
test('math_master', async t => {
/**
* Setup Wechaty Mock System
*/
const mocker = new mock.Mocker()
const puppet = new PuppetMock({ mocker })
const wechaty = WechatyBuilder.build({ puppet })
/**
* Install Vorpal & Match Master Command
*/
const WechatyVorpalPlugin = WechatyVorpal({
contact : true,
use : MathMaster(),
})
wechaty.use(WechatyVorpalPlugin)
/**
* Start Wechaty Environment
*/
await wechaty.start()
const [bot, player] = mocker.createContacts(2) as [mock.ContactMock, mock.ContactMock]
mocker.login(bot)
/**
* Message Processing Logic
*/
const onMessageText = (message: mock.MessageMock) => {
const text = message.text()
if (typeof text !== 'string') { return }
const talker = message.talker()
// console.info(`${talker.payload.name}: ${text}`)
const MATH_RE = /(\d+) \+ (\d+) = \?/
const match = text.match(MATH_RE)
if (match) {
const result = parseInt(match[1]!, 10) + parseInt(match[2]!, 10)
// const timeout = match[1].length * 1000
// console.info('timeout:', timeout)
// setTimeout(() => {
// player.say(String(result)).to(talker)
// }, timeout)
setImmediate(() => player.say(String(result)).to(talker))
}
}
const onMessage = (message: mock.MessageMock) => {
const msgType = message.type()
switch (msgType) {
case types.Message.Text:
onMessageText(message)
break
case types.Message.Image:
// console.info('image message')
break
default:
// console.info('other message, type: %s[%s]', Message.Type[type], type)
break
}
}
player.on('message', onMessage)
// const messageFuture = new Promise<Message>(resolve => wechaty.once('message', resolve))
player.say('math_master').to(bot)
const scoreFuture = expectGameScore(player)
const boardFuture = expectGameBoard(player, bot)
const score = await scoreFuture
t.ok(score >= 1, 'should play game and get a score')
// console.info('before expectGameBoard')
await boardFuture
// console.info('after expectGameBoard')
await new Promise(setImmediate)
await new Promise(resolve => setTimeout(resolve, 100))
await wechaty.stop()
// console.info('after wechaty.stop()')
})
function expectGameScore (player: mock.ContactMock) {
return new Promise<number>(resolve => {
const onMessage = (message: mock.MessageMock) => {
if (message.type() !== types.Message.Text) { return }
const text = message.text() || ''
if (!/Game Over/i.test(text)) { return }
const match = text.match(/score is: (\d+)!/i)
if (match) {
const n = parseInt(match[1]!, 10)
resolve(n)
} else {
resolve(0)
}
player.off('message', onMessage)
}
player.on('message', onMessage)
})
}
function expectGameBoard (player: mock.ContactMock, bot: mock.ContactMock) {
return new Promise<void>(resolve => {
const onMessage = (message: mock.MessageMock) => {
if (message.type() !== types.Message.Text) { return }
const text = message.text() || ''
if (/ say /i.test(text)) {
// console.info('say ...')
player.say('my comment to leader board').to(bot)
resolve()
player.off('message', onMessage)
}
}
player.on('message', onMessage)
})
}