@pacoyang/wechaty-puppet-wechat 0.28.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/.editorconfig +14 -0
  2. package/.eslintrc.js +7 -0
  3. package/.github/workflows/npm.yml +91 -0
  4. package/.markdownlintrc +13 -0
  5. package/.travis.yml +62 -0
  6. package/.vscode/settings.json +73 -0
  7. package/CODEOWNERS +9 -0
  8. package/LICENSE +201 -0
  9. package/NOTICE +8 -0
  10. package/README.md +145 -0
  11. package/dist/examples/demo.d.ts +20 -0
  12. package/dist/examples/demo.d.ts.map +1 -0
  13. package/dist/examples/demo.js +113 -0
  14. package/dist/examples/demo.js.map +1 -0
  15. package/dist/scripts/retry-unit-tests.d.ts +10 -0
  16. package/dist/scripts/retry-unit-tests.d.ts.map +1 -0
  17. package/dist/scripts/retry-unit-tests.js +47 -0
  18. package/dist/scripts/retry-unit-tests.js.map +1 -0
  19. package/dist/src/bridge.d.ts +100 -0
  20. package/dist/src/bridge.d.ts.map +1 -0
  21. package/dist/src/bridge.js +927 -0
  22. package/dist/src/bridge.js.map +1 -0
  23. package/dist/src/bridge.spec.d.ts +21 -0
  24. package/dist/src/bridge.spec.d.ts.map +1 -0
  25. package/dist/src/bridge.spec.js +191 -0
  26. package/dist/src/bridge.spec.js.map +1 -0
  27. package/dist/src/config.d.ts +10 -0
  28. package/dist/src/config.d.ts.map +1 -0
  29. package/dist/src/config.js +57 -0
  30. package/dist/src/config.js.map +1 -0
  31. package/dist/src/event.d.ts +23 -0
  32. package/dist/src/event.d.ts.map +1 -0
  33. package/dist/src/event.js +166 -0
  34. package/dist/src/event.js.map +1 -0
  35. package/dist/src/event.spec.d.ts +3 -0
  36. package/dist/src/event.spec.d.ts.map +1 -0
  37. package/dist/src/event.spec.js +41 -0
  38. package/dist/src/event.spec.js.map +1 -0
  39. package/dist/src/firer.d.ts +50 -0
  40. package/dist/src/firer.d.ts.map +1 -0
  41. package/dist/src/firer.js +409 -0
  42. package/dist/src/firer.js.map +1 -0
  43. package/dist/src/firer.spec.d.ts +21 -0
  44. package/dist/src/firer.spec.d.ts.map +1 -0
  45. package/dist/src/firer.spec.js +190 -0
  46. package/dist/src/firer.spec.js.map +1 -0
  47. package/dist/src/mod.d.ts +23 -0
  48. package/dist/src/mod.d.ts.map +1 -0
  49. package/dist/src/mod.js +28 -0
  50. package/dist/src/mod.js.map +1 -0
  51. package/dist/src/puppet-wechat.d.ts +164 -0
  52. package/dist/src/puppet-wechat.d.ts.map +1 -0
  53. package/dist/src/puppet-wechat.js +1287 -0
  54. package/dist/src/puppet-wechat.js.map +1 -0
  55. package/dist/src/puppet-wechat.spec.d.ts +21 -0
  56. package/dist/src/puppet-wechat.spec.d.ts.map +1 -0
  57. package/dist/src/puppet-wechat.spec.js +123 -0
  58. package/dist/src/puppet-wechat.spec.js.map +1 -0
  59. package/dist/src/pure-function-helpers/is-type.d.ts +3 -0
  60. package/dist/src/pure-function-helpers/is-type.d.ts.map +1 -0
  61. package/dist/src/pure-function-helpers/is-type.js +12 -0
  62. package/dist/src/pure-function-helpers/is-type.js.map +1 -0
  63. package/dist/src/pure-function-helpers/message-extname.d.ts +3 -0
  64. package/dist/src/pure-function-helpers/message-extname.d.ts.map +1 -0
  65. package/dist/src/pure-function-helpers/message-extname.js +44 -0
  66. package/dist/src/pure-function-helpers/message-extname.js.map +1 -0
  67. package/dist/src/pure-function-helpers/message-filename.d.ts +3 -0
  68. package/dist/src/pure-function-helpers/message-filename.d.ts.map +1 -0
  69. package/dist/src/pure-function-helpers/message-filename.js +19 -0
  70. package/dist/src/pure-function-helpers/message-filename.js.map +1 -0
  71. package/dist/src/pure-function-helpers/message-raw-payload-parser.d.ts +4 -0
  72. package/dist/src/pure-function-helpers/message-raw-payload-parser.d.ts.map +1 -0
  73. package/dist/src/pure-function-helpers/message-raw-payload-parser.js +67 -0
  74. package/dist/src/pure-function-helpers/message-raw-payload-parser.js.map +1 -0
  75. package/dist/src/pure-function-helpers/mod.d.ts +7 -0
  76. package/dist/src/pure-function-helpers/mod.d.ts.map +1 -0
  77. package/dist/src/pure-function-helpers/mod.js +23 -0
  78. package/dist/src/pure-function-helpers/mod.js.map +1 -0
  79. package/dist/src/pure-function-helpers/normalize-scan-status.d.ts +3 -0
  80. package/dist/src/pure-function-helpers/normalize-scan-status.d.ts.map +1 -0
  81. package/dist/src/pure-function-helpers/normalize-scan-status.js +21 -0
  82. package/dist/src/pure-function-helpers/normalize-scan-status.js.map +1 -0
  83. package/dist/src/pure-function-helpers/normalize-scan-status.spec.d.ts +21 -0
  84. package/dist/src/pure-function-helpers/normalize-scan-status.spec.d.ts.map +1 -0
  85. package/dist/src/pure-function-helpers/normalize-scan-status.spec.js +37 -0
  86. package/dist/src/pure-function-helpers/normalize-scan-status.spec.js.map +1 -0
  87. package/dist/src/pure-function-helpers/web-message-type.d.ts +4 -0
  88. package/dist/src/pure-function-helpers/web-message-type.d.ts.map +1 -0
  89. package/dist/src/pure-function-helpers/web-message-type.js +58 -0
  90. package/dist/src/pure-function-helpers/web-message-type.js.map +1 -0
  91. package/dist/src/pure-function-helpers/xml.d.ts +14 -0
  92. package/dist/src/pure-function-helpers/xml.d.ts.map +1 -0
  93. package/dist/src/pure-function-helpers/xml.js +64 -0
  94. package/dist/src/pure-function-helpers/xml.js.map +1 -0
  95. package/dist/src/version.d.ts +6 -0
  96. package/dist/src/version.d.ts.map +1 -0
  97. package/dist/src/version.js +9 -0
  98. package/dist/src/version.js.map +1 -0
  99. package/dist/src/version.spec.d.ts +3 -0
  100. package/dist/src/version.spec.d.ts.map +1 -0
  101. package/dist/src/version.spec.js +13 -0
  102. package/dist/src/version.spec.js.map +1 -0
  103. package/dist/src/web-schemas.d.ts +249 -0
  104. package/dist/src/web-schemas.d.ts.map +1 -0
  105. package/dist/src/web-schemas.js +118 -0
  106. package/dist/src/web-schemas.js.map +1 -0
  107. package/dist/src/wechaty-bro.js +868 -0
  108. package/dist/tests/puppeteer-contact.spec.d.ts +21 -0
  109. package/dist/tests/puppeteer-contact.spec.d.ts.map +1 -0
  110. package/dist/tests/puppeteer-contact.spec.js +67 -0
  111. package/dist/tests/puppeteer-contact.spec.js.map +1 -0
  112. package/dist/tests/puppeteer-friendship.spec.d.ts +21 -0
  113. package/dist/tests/puppeteer-friendship.spec.d.ts.map +1 -0
  114. package/dist/tests/puppeteer-friendship.spec.js +115 -0
  115. package/dist/tests/puppeteer-friendship.spec.js.map +1 -0
  116. package/dist/tests/puppeteer-message.spec.d.ts +21 -0
  117. package/dist/tests/puppeteer-message.spec.d.ts.map +1 -0
  118. package/dist/tests/puppeteer-message.spec.js +184 -0
  119. package/dist/tests/puppeteer-message.spec.js.map +1 -0
  120. package/dist/tests/puppeteer-room.spec.d.ts +21 -0
  121. package/dist/tests/puppeteer-room.spec.d.ts.map +1 -0
  122. package/dist/tests/puppeteer-room.spec.js +129 -0
  123. package/dist/tests/puppeteer-room.spec.js.map +1 -0
  124. package/dist/tests/puppeteer.spec.d.ts +21 -0
  125. package/dist/tests/puppeteer.spec.d.ts.map +1 -0
  126. package/dist/tests/puppeteer.spec.js +213 -0
  127. package/dist/tests/puppeteer.spec.js.map +1 -0
  128. package/docs/images/puppeteer-logo.png +0 -0
  129. package/docs/images/wechaty-puppet-wechat.png +0 -0
  130. package/examples/demo.ts +130 -0
  131. package/package.json +94 -0
  132. package/scripts/generate-version.sh +18 -0
  133. package/scripts/npm-pack-testing.sh +49 -0
  134. package/scripts/package-publish-config-tag.sh +13 -0
  135. package/scripts/retry-unit-tests.ts +57 -0
  136. package/src/bridge.spec.ts +224 -0
  137. package/src/bridge.ts +1122 -0
  138. package/src/config.ts +71 -0
  139. package/src/event.spec.ts +41 -0
  140. package/src/event.ts +247 -0
  141. package/src/firer.spec.ts +208 -0
  142. package/src/firer.ts +530 -0
  143. package/src/mod.ts +29 -0
  144. package/src/puppet-wechat.spec.ts +149 -0
  145. package/src/puppet-wechat.ts +1659 -0
  146. package/src/pure-function-helpers/is-type.ts +7 -0
  147. package/src/pure-function-helpers/message-extname.ts +57 -0
  148. package/src/pure-function-helpers/message-filename.ts +25 -0
  149. package/src/pure-function-helpers/message-raw-payload-parser.ts +85 -0
  150. package/src/pure-function-helpers/mod.ts +6 -0
  151. package/src/pure-function-helpers/normalize-scan-status.spec.ts +44 -0
  152. package/src/pure-function-helpers/normalize-scan-status.ts +23 -0
  153. package/src/pure-function-helpers/web-message-type.ts +71 -0
  154. package/src/pure-function-helpers/xml.ts +79 -0
  155. package/src/typings.d.ts +19 -0
  156. package/src/version.spec.ts +10 -0
  157. package/src/version.ts +6 -0
  158. package/src/web-schemas.ts +298 -0
  159. package/src/wechaty-bro.js +868 -0
  160. package/tests/fixtures/inject-file.js +7 -0
  161. package/tests/fixtures/smoke-testing.ts +42 -0
  162. package/tests/puppeteer-contact.spec.ts +78 -0
  163. package/tests/puppeteer-friendship.spec.ts +157 -0
  164. package/tests/puppeteer-message.spec.ts +249 -0
  165. package/tests/puppeteer-room.spec.ts +166 -0
  166. package/tests/puppeteer.spec.ts +262 -0
  167. package/tsconfig.json +26 -0
package/src/config.ts ADDED
@@ -0,0 +1,71 @@
1
+ /// <reference path="./typings.d.ts" />
2
+
3
+ import { OperationOptions } from 'retry'
4
+
5
+ import {
6
+ FileBox,
7
+ log,
8
+ } from 'wechaty-puppet'
9
+ import qrImage from 'qr-image'
10
+
11
+ import { VERSION } from './version'
12
+
13
+ import promiseRetry = require('promise-retry')
14
+
15
+ export async function retry<T> (
16
+ retryableFn: (
17
+ retry: (error: Error) => never,
18
+ attempt: number,
19
+ ) => Promise<T>,
20
+ ): Promise<T> {
21
+ /**
22
+ * 60 seconds: (to be confirmed)
23
+ * factor: 3
24
+ * minTimeout: 10
25
+ * maxTimeout: 20 * 1000
26
+ * retries: 9
27
+ */
28
+ const factor = 3
29
+ const minTimeout = 10
30
+ const maxTimeout = 20 * 1000
31
+ const retries = 9
32
+ // const unref = true
33
+
34
+ const retryOptions: OperationOptions = {
35
+ factor,
36
+ maxTimeout,
37
+ minTimeout,
38
+ retries,
39
+ }
40
+ return promiseRetry(retryOptions, retryableFn)
41
+ }
42
+
43
+ export function envHead (): boolean {
44
+ const KEY = 'WECHATY_PUPPET_WECHAT_PUPPETEER_HEAD'
45
+ return KEY in process.env
46
+ ? !!process.env[KEY]
47
+ : false
48
+
49
+ }
50
+
51
+ export function envStealthless (): boolean {
52
+ const KEY = 'WECHATY_PUPPET_WECHAT_PUPPETEER_STEALTHLESS'
53
+ return !!process.env[KEY]
54
+
55
+ }
56
+
57
+ export function qrCodeForChatie (): FileBox {
58
+ const CHATIE_OFFICIAL_ACCOUNT_QRCODE = 'http://weixin.qq.com/r/qymXj7DEO_1ErfTs93y5'
59
+ const name = 'qrcode-for-chatie.png'
60
+ const type = 'png'
61
+
62
+ const qrStream = qrImage.image(CHATIE_OFFICIAL_ACCOUNT_QRCODE, { type })
63
+ return FileBox.fromStream(qrStream, name)
64
+ }
65
+
66
+ export const MEMORY_SLOT = 'PUPPET_WECHAT'
67
+
68
+ export {
69
+ VERSION,
70
+ log,
71
+ }
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ts-node
2
+
3
+ /**
4
+ * Wechaty - https://github.com/chatie/wechaty
5
+ *
6
+ * @copyright 2016-2018 Huan LI <zixia@zixia.net>
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ *
20
+ */
21
+ import test from 'blue-tape'
22
+ // tslint:disable:no-shadowed-variable
23
+ // import sinon from 'sinon'
24
+
25
+ import {
26
+ // Event,
27
+ PuppetWeChat,
28
+ } from './puppet-wechat'
29
+
30
+ test('Puppet Puppeteer Event smoke testing', async (t) => {
31
+ const puppet = new PuppetWeChat()
32
+
33
+ try {
34
+ await puppet.start()
35
+ t.pass('should be inited')
36
+ await puppet.stop()
37
+ t.pass('should be quited')
38
+ } catch (e) {
39
+ t.fail('exception: ' + e.message)
40
+ }
41
+ })
package/src/event.ts ADDED
@@ -0,0 +1,247 @@
1
+ /**
2
+ * Wechaty - https://github.com/chatie/wechaty
3
+ *
4
+ * @copyright 2016-2018 Huan LI <zixia@zixia.net>
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ *
18
+ */
19
+ import {
20
+ WatchdogFood,
21
+ } from 'watchdog'
22
+
23
+ import {
24
+ log,
25
+ } from './config'
26
+ // import {
27
+ // PuppetScanEvent,
28
+ // } from 'wechaty-puppet'
29
+
30
+ import {
31
+ Firer,
32
+ } from './firer'
33
+ import {
34
+ PuppetWeChat,
35
+ } from './puppet-wechat'
36
+ import {
37
+ WebMessageRawPayload,
38
+ WebMessageType,
39
+ } from './web-schemas'
40
+
41
+ import {
42
+ normalizeScanStatus,
43
+ } from './pure-function-helpers/normalize-scan-status'
44
+
45
+ /* tslint:disable:variable-name */
46
+ export const Event = {
47
+ onDing,
48
+
49
+ onLog,
50
+ onLogin,
51
+ onLogout,
52
+
53
+ onMessage,
54
+ onScan,
55
+ onUnload,
56
+
57
+ }
58
+
59
+ function onDing (
60
+ this: PuppetWeChat,
61
+ data: any,
62
+ ): void {
63
+ log.silly('PuppetWeChatEvent', 'onDing(%s)', data)
64
+ this.emit('heartbeat', { data })
65
+ }
66
+
67
+ async function onScan (
68
+ this : PuppetWeChat,
69
+ // Do not use PuppetScanPayload at here, use { code: number, url: string } instead,
70
+ // because this is related with Browser Hook Code:
71
+ // wechaty-bro.js
72
+ payloadFromBrowser : { code: number, url: string },
73
+ ): Promise<void> {
74
+ log.verbose('PuppetWeChatEvent', 'onScan({code: %d, url: %s})', payloadFromBrowser.code, payloadFromBrowser.url)
75
+
76
+ // if (this.state.off()) {
77
+ // log.verbose('PuppetWeChatEvent', 'onScan(%s) state.off()=%s, NOOP',
78
+ // payload, this.state.off())
79
+ // return
80
+ // }
81
+
82
+ this.scanPayload = {
83
+ qrcode: payloadFromBrowser.url,
84
+ status: payloadFromBrowser.code,
85
+ }
86
+
87
+ /**
88
+ * When wx.qq.com push a new QRCode to Scan, there will be cookie updates(?)
89
+ */
90
+ await this.saveCookie()
91
+
92
+ if (this.logonoff()) {
93
+ log.verbose('PuppetWeChatEvent', 'onScan() there has user when got a scan event. emit logout and set it to null')
94
+ await this.logout()
95
+ }
96
+
97
+ // feed watchDog a `scan` type of food
98
+ const food: WatchdogFood = {
99
+ data: payloadFromBrowser,
100
+ type: 'scan',
101
+ }
102
+ this.emit('heartbeat', food)
103
+
104
+ const qrcode = payloadFromBrowser.url.replace(/\/qrcode\//, '/l/')
105
+ const status = normalizeScanStatus(payloadFromBrowser.code)
106
+
107
+ this.emit('scan', { qrcode, status })
108
+ }
109
+
110
+ function onLog (data: any): void {
111
+ log.silly('PuppetWeChatEvent', 'onLog(%s)', data)
112
+ }
113
+
114
+ async function onLogin (
115
+ this: PuppetWeChat,
116
+ note: string,
117
+ ttl = 30,
118
+ ): Promise<void> {
119
+ log.verbose('PuppetWeChatEvent', 'onLogin(%s, %d)', note, ttl)
120
+
121
+ const TTL_WAIT_MILLISECONDS = 1 * 1000
122
+ if (ttl <= 0) {
123
+ log.verbose('PuppetWeChatEvent', 'onLogin(%s) TTL expired')
124
+ this.emit('error', { data: 'onLogin() TTL expired.' })
125
+ return
126
+ }
127
+
128
+ // if (this.state.off()) {
129
+ // log.verbose('PuppetWeChatEvent', 'onLogin(%s, %d) state.off()=%s, NOOP',
130
+ // note, ttl, this.state.off())
131
+ // return
132
+ // }
133
+
134
+ if (this.logonoff()) {
135
+ log.verbose('onLogin() user had already logined: ' + this.selfId())
136
+ return
137
+ // throw new Error('onLogin() user had already logined: ' + this.selfId())
138
+ // await this.logout()
139
+ }
140
+
141
+ this.scanPayload = undefined
142
+
143
+ try {
144
+ /**
145
+ * save login user id to this.userId
146
+ *
147
+ * issue #772: this.bridge might not inited if the 'login' event fired too fast(because of auto login)
148
+ */
149
+ const userId = await this.bridge.getUserName()
150
+
151
+ if (!userId) {
152
+ log.verbose('PuppetWeChatEvent', 'onLogin() browser not fully loaded(ttl=%d), retry later', ttl)
153
+ const html = await this.bridge.innerHTML()
154
+ log.silly('PuppetWeChatEvent', 'onLogin() innerHTML: %s', html.substr(0, 500))
155
+ setTimeout(onLogin.bind(this, note, ttl - 1), TTL_WAIT_MILLISECONDS)
156
+ return
157
+ }
158
+
159
+ log.silly('PuppetWeChatEvent', 'bridge.getUserName: %s', userId)
160
+
161
+ // const user = this.Contact.load(userId)
162
+ // await user.ready()
163
+
164
+ log.silly('PuppetWeChatEvent', `onLogin() user ${userId} logined`)
165
+
166
+ // if (this.state.on() === true) {
167
+ await this.saveCookie()
168
+ // }
169
+
170
+ // fix issue https://github.com/Chatie/wechaty-puppet-wechat/issues/107
171
+ // we do not wait `ready` before emit `login`
172
+ this.waitStable().catch(e => {
173
+ log.error('PuppetWeChatEvent', 'onLogin() this.waitStable() rejection: %s', e && e.message)
174
+ })
175
+
176
+ await this.login(userId)
177
+
178
+ } catch (e) {
179
+ log.error('PuppetWeChatEvent', 'onLogin() exception: %s', e)
180
+ throw e
181
+ }
182
+ }
183
+
184
+ async function onLogout (
185
+ this: PuppetWeChat,
186
+ data: any,
187
+ ): Promise<void> {
188
+ log.verbose('PuppetWeChatEvent', 'onLogout(%s)', data)
189
+
190
+ if (this.logonoff()) {
191
+ await this.logout()
192
+ } else {
193
+ // not logged-in???
194
+ log.error('PuppetWeChatEvent', 'onLogout() without self-user')
195
+ }
196
+ }
197
+
198
+ async function onMessage (
199
+ this : PuppetWeChat,
200
+ rawPayload : WebMessageRawPayload,
201
+ ): Promise<void> {
202
+ const firer = new Firer(this)
203
+
204
+ /**
205
+ * Fire Events if match message type & content
206
+ */
207
+ switch (rawPayload.MsgType) {
208
+
209
+ case WebMessageType.VERIFYMSG:
210
+ this.emit('friendship', { friendshipId: rawPayload.MsgId })
211
+ // firer.checkFriendRequest(rawPayload)
212
+ break
213
+
214
+ case WebMessageType.SYS:
215
+ /**
216
+ * /^@@/.test() return true means it's a room
217
+ */
218
+ if (/^@@/.test(rawPayload.FromUserName)) {
219
+ const joinResult = await firer.checkRoomJoin(rawPayload)
220
+ const leaveResult = await firer.checkRoomLeave(rawPayload)
221
+ const topicRestul = await firer.checkRoomTopic(rawPayload)
222
+
223
+ if (!joinResult && !leaveResult && !topicRestul) {
224
+ log.silly('PuppetWeChatEvent', `checkRoomSystem message: <${rawPayload.Content}> not found`)
225
+ }
226
+ } else {
227
+ await firer.checkFriendConfirm(rawPayload)
228
+ }
229
+ break
230
+ }
231
+
232
+ this.emit('message', { messageId: rawPayload.MsgId })
233
+ }
234
+
235
+ async function onUnload (this: PuppetWeChat): Promise<void> {
236
+ log.silly('PuppetWeChatEvent', 'onUnload()')
237
+ /*
238
+ try {
239
+ await this.quit()
240
+ await this.init()
241
+ } catch (e) {
242
+ log.error('PuppetWeChatEvent', 'onUnload() exception: %s', e)
243
+ this.emit('error', e)
244
+ throw e
245
+ }
246
+ */
247
+ }
@@ -0,0 +1,208 @@
1
+ #!/usr/bin/env ts-node
2
+ /**
3
+ * Wechaty - https://github.com/chatie/wechaty
4
+ *
5
+ * @copyright 2016-2018 Huan LI <zixia@zixia.net>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ *
19
+ */
20
+
21
+ /**
22
+ * Process the Message to find which event to FIRE
23
+ */
24
+
25
+ // tslint:disable:no-shadowed-variable
26
+ import test from 'blue-tape'
27
+ // import sinon from 'sinon'
28
+
29
+ import { Firer } from './firer'
30
+ import { PuppetWeChat } from './puppet-wechat'
31
+
32
+ const SELF_ID = 'self-id'
33
+ const mockPuppetWeChat = {
34
+ selfId: () => SELF_ID,
35
+ } as any as PuppetWeChat
36
+
37
+ test('parseFriendConfirm()', async (t) => {
38
+ const contentList = [
39
+ [
40
+ 'You have added 李卓桓 as your WeChat contact. Start chatting!',
41
+ '李卓桓',
42
+ ],
43
+ [
44
+ '你已添加了李卓桓,现在可以开始聊天了。',
45
+ '李卓桓',
46
+ ],
47
+ [
48
+ 'johnbassserver@gmail.com just added you to his/her contacts list. Send a message to him/her now!',
49
+ 'johnbassserver@gmail.com',
50
+ ],
51
+ [
52
+ 'johnbassserver@gmail.com刚刚把你添加到通讯录,现在可以开始聊天了。',
53
+ 'johnbassserver@gmail.com',
54
+ ],
55
+ ]
56
+ let result: boolean
57
+
58
+ const firer = new Firer(mockPuppetWeChat)
59
+
60
+ contentList.forEach(([content]) => {
61
+ result = (firer as any).parseFriendConfirm(content)
62
+ t.true(result, 'should be truthy for confirm msg: ' + content)
63
+ })
64
+
65
+ result = (firer as any).parseFriendConfirm('fsdfsdfasdfasdfadsa')
66
+ t.false(result, 'should be falsy for other msg')
67
+ })
68
+
69
+ test('parseRoomJoin()', async (t) => {
70
+ const contentList: Array<[string, string, string[]]> = [
71
+ [
72
+ 'You invited 管理员 to the group chat. ',
73
+ 'You',
74
+ ['管理员'],
75
+ ],
76
+ [
77
+ 'You invited 李卓桓.PreAngel、Bruce LEE to the group chat. ',
78
+ 'You',
79
+ ['李卓桓.PreAngel', 'Bruce LEE'],
80
+ ],
81
+ [
82
+ '管理员 invited 小桔建群助手 to the group chat',
83
+ '管理员',
84
+ ['小桔建群助手'],
85
+ ],
86
+ [
87
+ '管理员 invited 庆次、小桔妹 to the group chat',
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
+ '"管理员" joined group chat via the QR code you shared. ',
113
+ 'you',
114
+ ['管理员'],
115
+ ],
116
+ [
117
+ '"宁锐锋" joined the group chat via the QR Code shared by "管理员".',
118
+ '管理员',
119
+ ['宁锐锋'],
120
+ ],
121
+ ]
122
+
123
+ const firer = new Firer(mockPuppetWeChat)
124
+
125
+ let result
126
+ contentList.forEach(([content, inviter, inviteeList]) => {
127
+ result = (firer as any).parseRoomJoin(content)
128
+ t.ok(result, 'should check room join message right for ' + content)
129
+ t.deepEqual(result[0], inviteeList, 'should get inviteeList right')
130
+ t.is(result[1], inviter, 'should get inviter right')
131
+ })
132
+
133
+ t.throws(() => {
134
+ (firer as any).parseRoomJoin('fsadfsadfsdfsdfs')
135
+ }, Error, 'should throws if message is not expected')
136
+ })
137
+
138
+ test('parseRoomLeave()', async (t) => {
139
+ const contentLeaverList = [
140
+ [
141
+ 'You removed "Bruce LEE" from the group chat',
142
+ 'Bruce LEE',
143
+ ],
144
+ [
145
+ '你将"李佳芮"移出了群聊',
146
+ '李佳芮',
147
+ ],
148
+ ]
149
+
150
+ const contentRemoverList = [
151
+ [
152
+ 'You were removed from the group chat by "桔小秘"',
153
+ '桔小秘',
154
+ ],
155
+ [
156
+ '你被"李佳芮"移出群聊',
157
+ '李佳芮',
158
+ ],
159
+ ]
160
+
161
+ const firer = new Firer(mockPuppetWeChat)
162
+
163
+ contentLeaverList.forEach(([content, leaver]) => {
164
+ const resultLeaver = (firer as any).parseRoomLeave(content)[0]
165
+ t.ok(resultLeaver, 'should get leaver for leave message: ' + content)
166
+ t.is(resultLeaver, leaver, 'should get leaver name right')
167
+ })
168
+
169
+ contentRemoverList.forEach(([content, remover]) => {
170
+ const resultRemover = (firer as any).parseRoomLeave(content)[1]
171
+ t.ok(resultRemover, 'should get remover for leave message: ' + content)
172
+ t.is(resultRemover, remover, 'should get leaver name right')
173
+ })
174
+
175
+ t.throws(() => {
176
+ (firer as any).parseRoomLeave('fafdsfsdfafa')
177
+ }, Error, 'should throw if message is not expected')
178
+ })
179
+
180
+ test('parseRoomTopic()', async (t) => {
181
+ const contentList = [
182
+ [
183
+ '"李卓桓.PreAngel" changed the group name to "ding"',
184
+ '李卓桓.PreAngel',
185
+ 'ding',
186
+ ],
187
+ [
188
+ '"李佳芮"修改群名为“dong”',
189
+ '李佳芮',
190
+ 'dong',
191
+ ],
192
+ ]
193
+
194
+ const firer = new Firer(mockPuppetWeChat)
195
+
196
+ let result
197
+ contentList.forEach(([content, changer, topic]) => {
198
+ result = (firer as any).parseRoomTopic(content)
199
+ t.ok(result, 'should check topic right for content: ' + content)
200
+ t.is(topic, result[0], 'should get right topic')
201
+ t.is(changer, result[1], 'should get right changer')
202
+ })
203
+
204
+ t.throws(() => {
205
+ (firer as any).parseRoomTopic('fafdsfsdfafa')
206
+ }, Error, 'should throw if message is not expected')
207
+
208
+ })