wechaty-puppet
Version:
Abstract Puppet for Wechaty
362 lines • 14.9 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const blue_tape_1 = __importDefault(require("blue-tape"));
const sinon_1 = __importDefault(require("sinon"));
const memory_card_1 = require("memory-card");
const contact_1 = require("./schemas/contact");
const puppet_1 = require("./puppet");
class PuppetTest extends puppet_1.Puppet {
start() {
return __awaiter(this, void 0, void 0, function* () { return {}; });
}
stop() {
return __awaiter(this, void 0, void 0, function* () { return {}; });
}
ding(data) {
return __awaiter(this, void 0, void 0, function* () { return data; });
}
logout() {
return __awaiter(this, void 0, void 0, function* () { return {}; });
}
contactAlias(contactId, alias) {
return __awaiter(this, void 0, void 0, function* () { return { contactId, alias }; });
}
contactAvatar(contactId, file) {
return __awaiter(this, void 0, void 0, function* () { return { contactId, file }; });
}
contactList() {
return __awaiter(this, void 0, void 0, function* () { return {}; });
}
contactQrcode(contactId) {
return __awaiter(this, void 0, void 0, function* () { return { contactId }; });
}
contactRawPayload(id) {
return __awaiter(this, void 0, void 0, function* () { return { id }; });
}
contactRawPayloadParser(rawPayload) {
return __awaiter(this, void 0, void 0, function* () { return { rawPayload }; });
}
/**
*
* Friendship
*
*/
friendshipRawPayload(id) {
return __awaiter(this, void 0, void 0, function* () { return { id }; });
}
friendshipRawPayloadParser(rawPayload) {
return __awaiter(this, void 0, void 0, function* () { return rawPayload; });
}
friendshipAdd(contactId, hello) {
return __awaiter(this, void 0, void 0, function* () { return { contactId, hello }; });
}
friendshipAccept(friendshipId) {
return __awaiter(this, void 0, void 0, function* () { return { friendshipId }; });
}
/**
*
* Message
*
*/
messageFile(messageId) {
return __awaiter(this, void 0, void 0, function* () { return { messageId }; });
}
messageForward(to, messageId) {
return __awaiter(this, void 0, void 0, function* () { return { to, messageId }; });
}
messageSendContact(receiver, contactId) {
return __awaiter(this, void 0, void 0, function* () { return { receiver, contactId }; });
}
messageSendFile(to, file) {
return __awaiter(this, void 0, void 0, function* () { return { to, file }; });
}
messageSendText(to, text) {
return __awaiter(this, void 0, void 0, function* () { return { to, text }; });
}
messageRawPayload(id) {
return __awaiter(this, void 0, void 0, function* () { return { id }; });
}
messageRawPayloadParser(rawPayload) {
return __awaiter(this, void 0, void 0, function* () { return { rawPayload }; });
}
/**
*
* Room Invitation
*
*/
roomInvitationAccept(roomInvitationId) {
return __awaiter(this, void 0, void 0, function* () { return; });
}
roomInvitationRawPayload(roomInvitationId) {
return __awaiter(this, void 0, void 0, function* () { return { roomInvitationId }; });
}
roomInvitationRawPayloadParser(rawPayload) {
return __awaiter(this, void 0, void 0, function* () { return rawPayload; });
}
roomAnnounce(roomId, text) {
return __awaiter(this, void 0, void 0, function* () { return { roomId, text }; });
}
roomAdd(roomId, contactId) {
return __awaiter(this, void 0, void 0, function* () { return { roomId, contactId }; });
}
roomAvatar(roomId) {
return __awaiter(this, void 0, void 0, function* () { return { roomId }; });
}
roomCreate(contactIdList, topic) {
return __awaiter(this, void 0, void 0, function* () { return { contactIdList, topic }; });
}
roomDel(roomId, contactId) {
return __awaiter(this, void 0, void 0, function* () { return { roomId, contactId }; });
}
roomQuit(roomId) {
return __awaiter(this, void 0, void 0, function* () { return { roomId }; });
}
roomQrcode(roomId) {
return __awaiter(this, void 0, void 0, function* () { return { roomId }; });
}
roomTopic(roomId, topic) {
return __awaiter(this, void 0, void 0, function* () { return { roomId, topic }; });
}
roomList() {
return __awaiter(this, void 0, void 0, function* () { return {}; });
}
roomMemberList(roomId) {
return __awaiter(this, void 0, void 0, function* () { return { roomId }; });
}
roomRawPayload(id) {
return __awaiter(this, void 0, void 0, function* () { return { id }; });
}
roomRawPayloadParser(rawPayload) {
return __awaiter(this, void 0, void 0, function* () { return { rawPayload }; });
}
roomMemberRawPayload(roomId, contactId) {
return __awaiter(this, void 0, void 0, function* () { return { roomId, contactId }; });
}
roomMemberRawPayloadParser(rawPayload) {
return __awaiter(this, void 0, void 0, function* () { return rawPayload; });
}
/**
* expose to public for internal methods:
*/
roomQueryFilterFactory(query) {
return super.roomQueryFilterFactory(query);
}
contactQueryFilterFactory(query) {
return super.contactQueryFilterFactory(query);
}
reset(reason) {
return super.reset(reason);
}
}
blue_tape_1.default('contactQueryFilterFunction()', (t) => __awaiter(this, void 0, void 0, function* () {
const TEXT_REGEX = 'query by regex';
const TEXT_TEXT = 'query by text';
const PAYLOAD_LIST = [
{
alias: TEXT_TEXT,
avatar: 'mock',
gender: contact_1.ContactGender.Unknown,
id: 'id1',
name: TEXT_REGEX,
type: contact_1.ContactType.Personal,
},
{
alias: TEXT_REGEX,
avatar: 'mock',
gender: contact_1.ContactGender.Unknown,
id: 'id2',
name: TEXT_TEXT,
type: contact_1.ContactType.Personal,
},
{
alias: TEXT_TEXT,
avatar: 'mock',
gender: contact_1.ContactGender.Unknown,
id: 'id3',
name: TEXT_REGEX,
type: contact_1.ContactType.Personal,
},
{
alias: TEXT_REGEX,
avatar: 'mock',
gender: contact_1.ContactGender.Unknown,
id: 'id4',
name: TEXT_TEXT,
type: contact_1.ContactType.Personal,
},
];
const REGEX_VALUE = new RegExp(TEXT_REGEX);
const TEXT_VALUE = TEXT_TEXT;
const puppet = new PuppetTest();
t.test('filter name by regex', (t) => __awaiter(this, void 0, void 0, function* () {
const QUERY = { name: REGEX_VALUE };
const ID_LIST = ['id1', 'id3'];
const func = puppet.contactQueryFilterFactory(QUERY);
const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id);
t.deepEqual(idList, ID_LIST, 'should filter the query to id list');
}));
t.test('filter name by text', (t) => __awaiter(this, void 0, void 0, function* () {
const QUERY = { name: TEXT_VALUE };
const ID_LIST = ['id2', 'id4'];
const func = puppet.contactQueryFilterFactory(QUERY);
const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id);
t.deepEqual(idList, ID_LIST, 'should filter query to id list');
}));
t.test('filter alias by regex', (t) => __awaiter(this, void 0, void 0, function* () {
const QUERY = { alias: REGEX_VALUE };
const ID_LIST = ['id2', 'id4'];
const func = puppet.contactQueryFilterFactory(QUERY);
const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id);
t.deepEqual(idList, ID_LIST, 'should filter query to id list');
}));
t.test('filter alias by text', (t) => __awaiter(this, void 0, void 0, function* () {
const QUERY = { alias: TEXT_VALUE };
const ID_LIST = ['id1', 'id3'];
const func = puppet.contactQueryFilterFactory(QUERY);
const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id);
t.deepEqual(idList, ID_LIST, 'should filter query to id list');
}));
t.test('throw if filter key unknown', (t) => __awaiter(this, void 0, void 0, function* () {
t.throws(() => puppet.contactQueryFilterFactory({ xxxx: 'test' }), 'should throw');
}));
t.test('throw if filter key are more than one', (t) => __awaiter(this, void 0, void 0, function* () {
t.throws(() => puppet.contactQueryFilterFactory({
alias: 'test',
name: 'test',
}), 'should throw');
}));
}));
blue_tape_1.default('roomQueryFilterFunction()', (t) => __awaiter(this, void 0, void 0, function* () {
const TEXT_REGEX = 'query by regex';
const TEXT_TEXT = 'query by text';
const PAYLOAD_LIST = [
{
id: 'id1',
memberIdList: [],
topic: TEXT_TEXT,
},
{
id: 'id2',
memberIdList: [],
topic: TEXT_REGEX,
},
{
id: 'id3',
memberIdList: [],
topic: TEXT_TEXT,
},
{
id: 'id4',
memberIdList: [],
topic: TEXT_REGEX,
},
];
const REGEX_VALUE = new RegExp(TEXT_REGEX);
const TEXT_VALUE = TEXT_TEXT;
const puppet = new PuppetTest();
t.test('filter name by regex', (t) => __awaiter(this, void 0, void 0, function* () {
const QUERY = { topic: REGEX_VALUE };
const ID_LIST = ['id2', 'id4'];
const func = puppet.roomQueryFilterFactory(QUERY);
const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id);
t.deepEqual(idList, ID_LIST, 'should filter the query to id list');
}));
t.test('filter name by text', (t) => __awaiter(this, void 0, void 0, function* () {
const QUERY = { topic: TEXT_VALUE };
const ID_LIST = ['id1', 'id3'];
const func = puppet.roomQueryFilterFactory(QUERY);
const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id);
t.deepEqual(idList, ID_LIST, 'should filter query to id list');
}));
t.test('throw if filter key unknown', (t) => __awaiter(this, void 0, void 0, function* () {
t.throws(() => puppet.roomQueryFilterFactory({ xxx: 'test' }), 'should throw');
}));
t.test('throw if filter key are more than one', (t) => __awaiter(this, void 0, void 0, function* () {
t.throws(() => puppet.roomQueryFilterFactory({
alias: 'test',
topic: 'test',
}), 'should throw');
}));
}));
blue_tape_1.default('contactRoomList()', (t) => __awaiter(this, void 0, void 0, function* () {
const puppet = new PuppetTest();
const sandbox = sinon_1.default.createSandbox();
const CONTACT_ID_1 = 'contact-id-1';
const CONTACT_ID_2 = 'contact-id-2';
const CONTACT_ID_3 = 'contact-id-3';
const ROOM_ID_1 = 'room-id-1';
const ROOM_ID_2 = 'room-id-2';
const ROOM_PAYLOAD_LIST = [
{
id: ROOM_ID_1,
memberIdList: [
CONTACT_ID_1,
CONTACT_ID_2,
],
topic: 'room-topic-1',
},
{
id: ROOM_ID_2,
memberIdList: [
CONTACT_ID_2,
CONTACT_ID_3,
],
topic: 'room-topic-2',
},
];
sandbox.stub(puppet, 'roomList').resolves(ROOM_PAYLOAD_LIST.map(payload => payload.id));
sandbox.stub(puppet, 'roomPayload').callsFake(roomId => {
for (const payload of ROOM_PAYLOAD_LIST) {
if (payload.id === roomId) {
return payload;
}
}
throw new Error('no payload for room id ' + roomId);
});
const roomIdList1 = yield puppet.contactRoomList(CONTACT_ID_1);
const roomIdList2 = yield puppet.contactRoomList(CONTACT_ID_2);
const roomIdList3 = yield puppet.contactRoomList(CONTACT_ID_3);
t.deepEqual(roomIdList1, [ROOM_ID_1], 'should get room 1 for contact 1');
t.deepEqual(roomIdList2, [ROOM_ID_1, ROOM_ID_2], 'should get room 1&2 for contact 2');
t.deepEqual(roomIdList3, [ROOM_ID_2], 'should get room 2 for contact 3');
}));
blue_tape_1.default('reset event throttle for reset()', (t) => __awaiter(this, void 0, void 0, function* () {
const puppet = new PuppetTest({});
const sandbox = sinon_1.default.createSandbox();
const timer = sandbox.useFakeTimers();
const reset = sandbox.stub(puppet, 'reset');
puppet.emit('reset', 'testing');
t.equal(reset.callCount, 1, 'should call reset() immediately');
timer.tick(1000 - 1);
puppet.emit('reset', 'testing 2');
t.equal(reset.callCount, 1, 'should not call reset() again in the following 1 second');
timer.tick(1000 + 1);
puppet.emit('reset', 'testing 2');
t.equal(reset.callCount, 2, 'should call reset() again after 1 second');
sandbox.restore();
}));
blue_tape_1.default('setMemory() memory without name', (t) => __awaiter(this, void 0, void 0, function* () {
const puppet = new PuppetTest();
const memory = new memory_card_1.MemoryCard();
t.doesNotThrow(() => puppet.setMemory(memory), 'should not throw when set a no-name memory first time ');
t.doesNotThrow(() => puppet.setMemory(memory), 'should not throw when set a no-name memory second time');
}));
blue_tape_1.default('setMemory() memory with a name', (t) => __awaiter(this, void 0, void 0, function* () {
const puppet = new PuppetTest();
const memory = new memory_card_1.MemoryCard('name');
t.doesNotThrow(() => puppet.setMemory(memory), 'should not throw when set a named memory first time ');
t.throws(() => puppet.setMemory(memory), 'should throw when set a named memory second time');
}));
//# sourceMappingURL=puppet.spec.js.map