UNPKG

9.56 kBJavaScriptView Raw
1#!/usr/bin/env ts-node
2"use strict";
3var __importDefault = (this && this.__importDefault) || function (mod) {
4 return (mod && mod.__esModule) ? mod : { "default": mod };
5};
6Object.defineProperty(exports, "__esModule", { value: true });
7/**
8 * Wechaty Chatbot SDK - https://github.com/wechaty/wechaty
9 *
10 * @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and
11 * Wechaty Contributors <https://github.com/wechaty>.
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 */
26const blue_tape_1 = __importDefault(require("blue-tape"));
27const sinon_1 = __importDefault(require("sinon"));
28const wechaty_puppet_mock_1 = require("wechaty-puppet-mock");
29const wechaty_1 = require("./wechaty");
30const mod_1 = require("./mod");
31const wechaty_puppet_1 = require("wechaty-puppet");
32class WechatyTest extends wechaty_1.Wechaty {
33 wechatifyUserModulesTest(puppet) {
34 return this.wechatifyUserModules(puppet);
35 }
36}
37blue_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});
47blue_tape_1.default('static VERSION', async (t) => {
48 t.true('VERSION' in wechaty_1.Wechaty, 'Wechaty should has a static VERSION property');
49});
50blue_tape_1.default('Config setting', async (t) => {
51 t.ok(mod_1.config, 'should export Config');
52 // t.ok(config.default.DEFAULT_PUPPET , 'should has DEFAULT_PUPPET')
53});
54blue_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 // console.log(startSpy.callCount)
63 t.ok(startSpy.calledOnce, 'should get event:start once');
64 t.ok(stopSpy.calledOnce, 'should get event:stop once');
65});
66//
67// FIXME: restore this unit test !!!
68//
69// test.only('event:scan', async t => {
70// const m = {} as any
71// const asyncHook = asyncHooks.createHook({
72// init(asyncId: number, type: string, triggerAsyncId: number, resource: Object) {
73// m[asyncId] = type
74// },
75// before(asyncId) {
76// // delete m[asyncId]
77// },
78// after(asyncId) {
79// // delete m[asyncId]
80// },
81// destroy(asyncId) {
82// delete m[asyncId]
83// },
84// })
85// asyncHook.enable()
86// const wechaty = Wechaty.instance()
87// const spy = sinon.spy()
88// wechaty.on('scan', spy)
89// const scanFuture = new Promise(resolve => wechaty.once('scan', resolve))
90// // wechaty.once('scan', () => console.log('FAINT'))
91// await wechaty.start()
92// await scanFuture
93// // await new Promise(r => setTimeout(r, 1000))
94// await wechaty.stop()
95// t.ok(spy.calledOnce, 'should get event:scan')
96// asyncHook.disable()
97// console.log(m)
98// })
99blue_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 // wechaty.on('scan', () => 42)
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});
113blue_tape_1.default.skip('SKIP DEALING WITH THE LISTENER EXCEPTIONS. test async error', async (t) => {
114 // Do not modify the global Wechaty instance
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 // tslint ask resolve must be called,
127 // so write a falsy value, so that it never called
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});
142blue_tape_1.default('use plugin', async (t) => {
143 // Do not modify the gloabl Wechaty instance
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});
167blue_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// TODO: add test for event args
174blue_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});
190blue_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});
207blue_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});
231blue_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//# sourceMappingURL=wechaty.spec.js.map
\No newline at end of file