1 | #!/usr/bin/env ts-node
|
2 | "use strict";
|
3 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
4 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
5 | };
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | const blue_tape_1 = __importDefault(require("blue-tape"));
|
27 | const sinon_1 = __importDefault(require("sinon"));
|
28 | const wechaty_puppet_mock_1 = require("wechaty-puppet-mock");
|
29 | const wechaty_1 = require("./wechaty");
|
30 | const mod_1 = require("./mod");
|
31 | const wechaty_puppet_1 = require("wechaty-puppet");
|
32 | class WechatyTest extends wechaty_1.Wechaty {
|
33 | wechatifyUserModulesTest(puppet) {
|
34 | return this.wechatifyUserModules(puppet);
|
35 | }
|
36 | }
|
37 | blue_tape_1.default('Export of the Framework', async (t) => {
|
38 | t.ok(mod_1.Contact, 'should export Contact');
|
39 | t.ok(mod_1.Friendship, 'should export Friendship');
|
40 | t.ok(mod_1.IoClient, 'should export IoClient');
|
41 | t.ok(mod_1.Message, 'should export Message');
|
42 | t.ok(wechaty_puppet_1.Puppet, 'should export Puppet');
|
43 | t.ok(mod_1.Room, 'should export Room');
|
44 | t.ok(wechaty_1.Wechaty, 'should export Wechaty');
|
45 | t.ok(mod_1.log, 'should export log');
|
46 | });
|
47 | blue_tape_1.default('static VERSION', async (t) => {
|
48 | t.true('VERSION' in wechaty_1.Wechaty, 'Wechaty should has a static VERSION property');
|
49 | });
|
50 | blue_tape_1.default('Config setting', async (t) => {
|
51 | t.ok(mod_1.config, 'should export Config');
|
52 |
|
53 | });
|
54 | blue_tape_1.default('event:start/stop', async (t) => {
|
55 | const wechaty = new wechaty_1.Wechaty({ puppet: 'wechaty-puppet-mock' });
|
56 | const startSpy = sinon_1.default.spy();
|
57 | const stopSpy = sinon_1.default.spy();
|
58 | wechaty.on('start', startSpy);
|
59 | wechaty.on('stop', stopSpy);
|
60 | await wechaty.start();
|
61 | await wechaty.stop();
|
62 |
|
63 | t.ok(startSpy.calledOnce, 'should get event:start once');
|
64 | t.ok(stopSpy.calledOnce, 'should get event:stop once');
|
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 | blue_tape_1.default.skip('SKIP DEALING WITH THE LISTENER EXCEPTIONS. on(event, Function)', async (t) => {
|
100 | const spy = sinon_1.default.spy();
|
101 | const wechaty = wechaty_1.Wechaty.instance();
|
102 | const EXPECTED_ERROR = new Error('testing123');
|
103 | wechaty.on('message', () => { throw EXPECTED_ERROR; });
|
104 |
|
105 | wechaty.on('error', spy);
|
106 | const messageFuture = new Promise(resolve => wechaty.once('message', resolve));
|
107 | wechaty.emit('message', {});
|
108 | await messageFuture;
|
109 | await wechaty.stop();
|
110 | t.ok(spy.calledOnce, 'should get event:error once');
|
111 | t.equal(spy.firstCall.args[0], EXPECTED_ERROR, 'should get error from message listener');
|
112 | });
|
113 | blue_tape_1.default.skip('SKIP DEALING WITH THE LISTENER EXCEPTIONS. test async error', async (t) => {
|
114 |
|
115 | class MyWechatyTest extends wechaty_1.Wechaty {
|
116 | }
|
117 | const EXPECTED_ERROR = new Error('test');
|
118 | const bot = new MyWechatyTest({
|
119 | puppet: new wechaty_puppet_mock_1.PuppetMock(),
|
120 | });
|
121 | const asyncErrorFunction = function () {
|
122 | return new Promise((resolve, reject) => {
|
123 | setTimeout(function () {
|
124 | reject(EXPECTED_ERROR);
|
125 | }, 100);
|
126 |
|
127 |
|
128 | if (+new Date() < 0) {
|
129 | resolve();
|
130 | }
|
131 | });
|
132 | };
|
133 | bot.on('message', async () => {
|
134 | await asyncErrorFunction();
|
135 | });
|
136 | bot.on('error', (e) => {
|
137 | t.ok(e.message === EXPECTED_ERROR.message);
|
138 | });
|
139 | bot.emit('message', {});
|
140 | await bot.stop();
|
141 | });
|
142 | blue_tape_1.default('use plugin', async (t) => {
|
143 |
|
144 | class MyWechatyTest extends wechaty_1.Wechaty {
|
145 | }
|
146 | let result = '';
|
147 | const myGlobalPlugin = function () {
|
148 | return function (bot) {
|
149 | bot.on('message', () => (result += 'FROM_GLOBAL_PLUGIN:'));
|
150 | };
|
151 | };
|
152 | const myPlugin = function () {
|
153 | return function (bot) {
|
154 | bot.on('message', () => (result += 'FROM_MY_PLUGIN:'));
|
155 | };
|
156 | };
|
157 | MyWechatyTest.use(myGlobalPlugin());
|
158 | const bot = new MyWechatyTest({
|
159 | puppet: new wechaty_puppet_mock_1.PuppetMock(),
|
160 | });
|
161 | bot.use(myPlugin());
|
162 | bot.on('message', () => (result += 'FROM_BOT'));
|
163 | bot.emit('message', {});
|
164 | await bot.stop();
|
165 | t.ok(result === 'FROM_GLOBAL_PLUGIN:FROM_MY_PLUGIN:FROM_BOT');
|
166 | });
|
167 | blue_tape_1.default('initPuppetAccessory()', async (t) => {
|
168 | const wechatyTest = new WechatyTest();
|
169 | const puppet = new wechaty_puppet_mock_1.PuppetMock();
|
170 | t.doesNotThrow(() => wechatyTest.wechatifyUserModulesTest(puppet), 'should not throw for the 1st time init');
|
171 | t.throws(() => wechatyTest.wechatifyUserModulesTest(puppet), 'should throw for the 2nd time init');
|
172 | });
|
173 |
|
174 | blue_tape_1.default('Wechaty restart for many times', async (t) => {
|
175 | const wechaty = new wechaty_1.Wechaty({
|
176 | puppet: new wechaty_puppet_mock_1.PuppetMock(),
|
177 | });
|
178 | try {
|
179 | for (let i = 0; i < 3; i++) {
|
180 | await wechaty.start();
|
181 | await wechaty.stop();
|
182 | t.pass('start/stop-ed at #' + i);
|
183 | }
|
184 | t.pass('Wechaty start/restart successed.');
|
185 | }
|
186 | catch (e) {
|
187 | t.fail(e);
|
188 | }
|
189 | });
|
190 | blue_tape_1.default('@event ready', async (t) => {
|
191 | const puppet = new wechaty_puppet_mock_1.PuppetMock();
|
192 | const wechaty = new wechaty_1.Wechaty({ puppet });
|
193 | const sandbox = sinon_1.default.createSandbox();
|
194 | const spy = sandbox.spy();
|
195 | wechaty.on('ready', spy);
|
196 | t.true(spy.notCalled, 'should no ready event with new wechaty instance');
|
197 | await wechaty.start();
|
198 | t.true(spy.notCalled, 'should no ready event right start wechaty started');
|
199 | puppet.emit('ready', { data: 'test' });
|
200 | t.true(spy.calledOnce, 'should fire ready event after puppet ready');
|
201 | await wechaty.stop();
|
202 | await wechaty.start();
|
203 | puppet.emit('ready', { data: 'test' });
|
204 | t.true(spy.calledTwice, 'should fire ready event second time after stop/start wechaty');
|
205 | await wechaty.stop();
|
206 | });
|
207 | blue_tape_1.default('ready()', async (t) => {
|
208 | const puppet = new wechaty_puppet_mock_1.PuppetMock();
|
209 | const wechaty = new wechaty_1.Wechaty({ puppet });
|
210 | const sandbox = sinon_1.default.createSandbox();
|
211 | const spy = sandbox.spy();
|
212 | wechaty.ready()
|
213 | .then(spy)
|
214 | .catch(e => t.fail('rejection: ' + e));
|
215 | t.true(spy.notCalled, 'should not ready with new wechaty instance');
|
216 | await wechaty.start();
|
217 | t.true(spy.notCalled, 'should not ready after right start wechaty');
|
218 | puppet.emit('ready', { data: 'test' });
|
219 | await new Promise(resolve => setImmediate(resolve));
|
220 | t.true(spy.calledOnce, 'should ready after puppet ready');
|
221 | await wechaty.stop();
|
222 | await wechaty.start();
|
223 | wechaty.ready()
|
224 | .then(spy)
|
225 | .catch(e => t.fail('rejection: ' + e));
|
226 | puppet.emit('ready', { data: 'test' });
|
227 | await new Promise(resolve => setImmediate(resolve));
|
228 | t.true(spy.calledTwice, 'should ready again after stop/start wechaty');
|
229 | await wechaty.stop();
|
230 | });
|
231 | blue_tape_1.default('on/off event listener management', async (t) => {
|
232 | const puppet = new wechaty_puppet_mock_1.PuppetMock();
|
233 | const wechaty = new wechaty_1.Wechaty({ puppet });
|
234 | const onMessage = (_) => { };
|
235 | t.equal(wechaty.listenerCount('message'), 0, 'should no listener after initializing');
|
236 | wechaty.on('message', onMessage);
|
237 | t.equal(wechaty.listenerCount('message'), 1, 'should +1 listener after on(message)');
|
238 | wechaty.off('message', onMessage);
|
239 | t.equal(wechaty.listenerCount('message'), 0, 'should -1 listener after off(message)');
|
240 | });
|
241 |
|
\ | No newline at end of file |