UNPKG

@juzi/wechaty

Version:

Wechaty is a RPA SDK for Chatbot Makers.

192 lines 8.51 kB
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Wechaty Chatbot SDK - https://github.com/wechaty/wechaty * * @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and * Wechaty Contributors <https://github.com/wechaty>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ const tstest_1 = require("tstest"); const wechaty_puppet_mock_1 = require("@juzi/wechaty-puppet-mock"); const wechaty_builder_js_1 = require("../wechaty-builder.js"); (0, tstest_1.test)('findAll()', async (t) => { const EXPECTED_ROOM_ID = 'test-id'; const EXPECTED_ROOM_TOPIC = 'test-topic'; const EXPECTED_ROOM_ID_LIST = [EXPECTED_ROOM_ID]; const sandbox = tstest_1.sinon.createSandbox(); const puppet = new wechaty_puppet_mock_1.PuppetMock(); const wechaty = wechaty_builder_js_1.WechatyBuilder.build({ puppet }); await wechaty.start(); sandbox.stub(puppet, 'roomSearch').resolves(EXPECTED_ROOM_ID_LIST); sandbox.stub(puppet, 'roomMemberList').resolves([]); sandbox.stub(puppet, 'roomPayload').callsFake(async () => { await new Promise(resolve => setImmediate(resolve)); return { topic: EXPECTED_ROOM_TOPIC, }; }); const mockContact = puppet.mocker.createContact({ name: 'bot' }); await puppet.mocker.login(mockContact); const future = new Promise(resolve => { wechaty.on('login', resolve); }); await future; const roomList = await wechaty.Room.findAll(); t.equal(roomList.length, 1, 'should find 1 room'); t.equal(await roomList[0].topic(), EXPECTED_ROOM_TOPIC, 'should get topic from payload'); await wechaty.stop(); }); (0, tstest_1.test)('room.findAll() not login test', async (t) => { const EXPECTED_ROOM_ID = 'test-id'; const EXPECTED_ROOM_TOPIC = 'test-topic'; const EXPECTED_ROOM_ID_LIST = [EXPECTED_ROOM_ID]; const sandbox = tstest_1.sinon.createSandbox(); const puppet = new wechaty_puppet_mock_1.PuppetMock(); const wechaty = wechaty_builder_js_1.WechatyBuilder.build({ puppet }); await wechaty.start(); sandbox.stub(puppet, 'roomSearch').resolves(EXPECTED_ROOM_ID_LIST); sandbox.stub(puppet, 'roomMemberList').resolves([]); sandbox.stub(puppet, 'roomPayload').callsFake(async () => { await new Promise(resolve => setImmediate(resolve)); return { topic: EXPECTED_ROOM_TOPIC, }; }); await t.rejects(wechaty.Room.findAll()); await wechaty.stop(); }); (0, tstest_1.test)('room.say() smoke testing', async () => { const sandbox = tstest_1.sinon.createSandbox(); const callback = tstest_1.sinon.spy(); const puppet = new wechaty_puppet_mock_1.PuppetMock(); const wechaty = wechaty_builder_js_1.WechatyBuilder.build({ puppet }); const bot = puppet.mocker.createContact(); puppet.mocker.login(bot); await wechaty.start(); const EXPECTED_ROOM_ID = 'roomId'; const EXPECTED_ROOM_TOPIC = 'test-topic'; const EXPECTED_ROOM_ADDITIONAL_INFO = { subjectA: 'A', subjectB: 'B', }; const EXPECTED_CONTACT_1_ID = 'contact1'; const EXPECTED_CONTACT_1_ALIAS = 'little1'; const EXPECTED_CONTACT_2_ID = 'contact2'; const EXPECTED_CONTACT_2_ALIAS = 'big2'; const CONTACT_MAP = {}; CONTACT_MAP[EXPECTED_CONTACT_1_ID] = EXPECTED_CONTACT_1_ALIAS; CONTACT_MAP[EXPECTED_CONTACT_2_ID] = EXPECTED_CONTACT_2_ALIAS; sandbox.stub(puppet, 'roomMemberPayload').callsFake(async (_, contactId) => { await new Promise(resolve => setImmediate(resolve)); return { id: contactId, roomAlias: CONTACT_MAP[contactId], }; }); sandbox.stub(puppet, 'roomPayload').callsFake(async () => { await new Promise(resolve => setImmediate(resolve)); return { topic: EXPECTED_ROOM_TOPIC, additionalInfo: JSON.stringify(EXPECTED_ROOM_ADDITIONAL_INFO), }; }); sandbox.stub(puppet, 'contactPayload').callsFake(async (contactId) => { await new Promise(resolve => setImmediate(resolve)); return { id: contactId, }; }); // sandbox.spy(puppet, 'messageSendText') sandbox.stub(puppet, 'messageSendText').callsFake(callback); sandbox.stub(puppet, 'roomMemberList').resolves([]); const fakeIdSearcher = async (...args) => { await new Promise(setImmediate); return [args[0].id]; }; sandbox.stub(puppet, 'contactSearch').callsFake(fakeIdSearcher); sandbox.stub(puppet, 'roomSearch').callsFake(fakeIdSearcher); const room = await wechaty.Room.find({ id: EXPECTED_ROOM_ID }); const contact1 = await wechaty.Contact.find({ id: EXPECTED_CONTACT_1_ID }); const contact2 = await wechaty.Contact.find({ id: EXPECTED_CONTACT_2_ID }); if (!room || !contact1 || !contact2) { throw new Error('find by id: not found'); } // await contact1.sync() // await contact2.sync() // await room.sync() (0, tstest_1.test)('room additional info', async (t) => { callback.resetHistory(); const additionalInfo = room.additionalInfo(); t.same(additionalInfo, EXPECTED_ROOM_ADDITIONAL_INFO, 'additional info should be matched'); }); (0, tstest_1.test)('say with Tagged Template', async (t) => { callback.resetHistory(); await room.say `To be ${contact1} or not to be ${contact2}`; t.same(callback.getCall(0).args, [ // { contactId: EXPECTED_CONTACT_1_ID, roomId: EXPECTED_ROOM_ID }, EXPECTED_ROOM_ID, 'To be @little1 or not to be @big2', [EXPECTED_CONTACT_1_ID, EXPECTED_CONTACT_2_ID], ], 'Tagged Template say should be matched'); }); (0, tstest_1.test)('say with regular mention contact', async (t) => { callback.resetHistory(); await room.say('Yo', contact1); t.same(callback.getCall(0).args, [ // { contactId: EXPECTED_CONTACT_1_ID, roomId: EXPECTED_ROOM_ID }, EXPECTED_ROOM_ID, '@little1 Yo', [EXPECTED_CONTACT_1_ID], ], 'Single mention should work with old ways'); }); (0, tstest_1.test)('say with multiple mention contact', async (t) => { callback.resetHistory(); await room.say('hey buddies, let\'s party', contact1, contact2); t.same(callback.getCall(0).args, [ // { contactId: EXPECTED_CONTACT_1_ID, roomId: EXPECTED_ROOM_ID }, EXPECTED_ROOM_ID, '@little1 @big2 hey buddies, let\'s party', [EXPECTED_CONTACT_1_ID, EXPECTED_CONTACT_2_ID], ], 'Multiple mention should work with new way'); }); (0, tstest_1.test)('say with @all', async (t) => { callback.resetHistory(); await room.say('hey buddies, let\'s party', '@all', contact1); t.same(callback.getCall(0).args, [ // { contactId: EXPECTED_CONTACT_1_ID, roomId: EXPECTED_ROOM_ID }, EXPECTED_ROOM_ID, '@all @little1 hey buddies, let\'s party', ['@all', EXPECTED_CONTACT_1_ID], ], 'should be alble to call with say'); }); (0, tstest_1.test)('say template string array with @all', async (t) => { callback.resetHistory(); await room.say `hey ${'@all'} let's party, especially ${contact1}`; t.same(callback.getCall(0).args, [ // { contactId: EXPECTED_CONTACT_1_ID, roomId: EXPECTED_ROOM_ID }, EXPECTED_ROOM_ID, 'hey @all let\'s party, especially @little1', ['@all', EXPECTED_CONTACT_1_ID], ], 'should be alble to call with template string array'); }); await wechaty.stop(); }); (0, tstest_1.test)('ProtectedProperties', async (t) => { const noOneLeft = true; t.ok(noOneLeft, 'should match Wechaty properties for every protected property'); }); //# sourceMappingURL=room.spec.js.map