@juzi/wechaty
Version:
Wechaty is a RPA SDK for Chatbot Makers.
125 lines • 5.66 kB
JavaScript
;
/**
* 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.
*
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const tstest_1 = require("tstest");
const PUPPET = __importStar(require("@juzi/wechaty-puppet"));
const wechaty_puppet_mock_1 = require("@juzi/wechaty-puppet-mock");
const wechaty_builder_js_1 = require("../wechaty-builder.js");
(0, tstest_1.test)('recalled()', async (t) => {
const EXPECTED_RECALL_MESSAGE_ID = 'message-id-1';
const EXPECTED_RECALLED_MESSAGE_ID = 'message-id-2';
const EXPECTED_MESSAGE_TIMESTAMP = new Date().getTime();
const EXPECTED_ROOM_TOPIC = 'topic';
const EXPECTED_ROOM_ID = 'room-id';
const EXPECTED_TALKER_CONTACT_ID = 'talker-contact-id';
const EXPECTED_LISTENER_CONTACT_ID = 'listener-contact-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, 'messagePayload').callsFake(async (id) => {
await new Promise(resolve => setImmediate(resolve));
if (id === EXPECTED_RECALL_MESSAGE_ID) {
return {
id: EXPECTED_RECALL_MESSAGE_ID,
talkerId: EXPECTED_TALKER_CONTACT_ID,
text: EXPECTED_RECALLED_MESSAGE_ID,
timestamp: EXPECTED_MESSAGE_TIMESTAMP,
type: PUPPET.types.Message.Recalled,
};
}
else {
return {
id: EXPECTED_RECALLED_MESSAGE_ID,
listenerId: EXPECTED_LISTENER_CONTACT_ID,
roomId: EXPECTED_ROOM_ID,
talkerId: EXPECTED_TALKER_CONTACT_ID,
text: '',
timestamp: EXPECTED_MESSAGE_TIMESTAMP,
type: PUPPET.types.Message.Text,
};
}
});
sandbox.stub(puppet, 'roomPayload').callsFake(async () => {
await new Promise(resolve => setImmediate(resolve));
return {
topic: EXPECTED_ROOM_TOPIC,
};
});
sandbox.stub(puppet, 'roomMemberList').callsFake(async () => {
await new Promise((resolve) => setImmediate(resolve));
return [EXPECTED_TALKER_CONTACT_ID, EXPECTED_LISTENER_CONTACT_ID];
});
sandbox.stub(puppet, 'contactPayload').callsFake(async (id) => {
await new Promise(setImmediate);
return {
id,
name: id,
};
});
const fakeIdSearcher = async (...args) => {
await new Promise(setImmediate);
return [args[0].id];
};
sandbox.stub(puppet, 'messageSearch').callsFake(fakeIdSearcher);
sandbox.stub(puppet, 'contactSearch').callsFake(fakeIdSearcher);
await puppet.login(EXPECTED_LISTENER_CONTACT_ID);
const message = await wechaty.Message.find({ id: EXPECTED_RECALL_MESSAGE_ID });
if (!message) {
throw new Error('no message for id: ' + EXPECTED_RECALL_MESSAGE_ID);
}
const recalledMessage = await message.toRecalled();
t.ok(recalledMessage, 'recalled message should exist.');
t.equal(recalledMessage.id, EXPECTED_RECALLED_MESSAGE_ID, 'Recalled message should have the right id.');
t.equal(recalledMessage.talker().id, EXPECTED_TALKER_CONTACT_ID, 'Recalled message should have the right from contact id.');
t.equal(recalledMessage.listener().id, EXPECTED_LISTENER_CONTACT_ID, 'Recalled message should have the right to contact id.');
t.equal(recalledMessage.room().id, EXPECTED_ROOM_ID, 'Recalled message should have the right room id.');
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=message.spec.js.map