From a0fd83101cc601049ec15dfc2ced826110fb1909 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Wed, 22 Jan 2020 19:56:27 +0800 Subject: [PATCH] feat(test-utils): add receiver api --- packages/test-utils/src/app.ts | 42 ++++-- .../tests/__snapshots__/app.spec.ts.snap | 2 +- packages/test-utils/tests/app.spec.ts | 130 +++++++++++++++++- 3 files changed, 157 insertions(+), 17 deletions(-) diff --git a/packages/test-utils/src/app.ts b/packages/test-utils/src/app.ts index 57ff087b41..3d56f438fd 100644 --- a/packages/test-utils/src/app.ts +++ b/packages/test-utils/src/app.ts @@ -1,4 +1,4 @@ -import { AppOptions, App, Sender, Server, ContextType, Meta } from 'koishi-core' +import { AppOptions, App, Sender, Server, ContextType, Meta, FileInfo } from 'koishi-core' import { MockedServer, RequestParams, RequestData, RequestHandler } from './mocks' import { Session, createMessageMeta } from './session' import { BASE_SELF_ID } from './utils' @@ -49,23 +49,35 @@ export class MockedApp extends App { } receiveFriendRequest (userId: number, flag = 'flag') { - this.receive({ - postType: 'request', - requestType: 'friend', - userId, - flag, - }) + this.receive({ postType: 'request', requestType: 'friend', userId, flag }) } receiveGroupRequest (userId: number, subType: 'add' | 'invite', groupId = 10000, flag = 'flag') { - this.receive({ - postType: 'request', - requestType: 'group', - subType, - userId, - groupId, - flag, - }) + this.receive({ postType: 'request', requestType: 'group', subType, userId, groupId, flag }) + } + + receiveGroupUpload (file: FileInfo, userId: number, groupId = 10000) { + this.receive({ postType: 'notice', noticeType: 'group_upload', file, userId, groupId }) + } + + receiveGroupAdmin (subType: 'set' | 'unset', userId: number, groupId = 10000) { + this.receive({ postType: 'notice', noticeType: 'group_admin', subType, userId, groupId }) + } + + receiveGroupIncrease (subType: 'approve' | 'invite', userId: number, groupId = 10000, operatorId = 1000) { + this.receive({ postType: 'notice', noticeType: 'group_increase', subType, userId, groupId, operatorId }) + } + + receiveGroupDecrease (subType: 'leave' | 'kick' | 'kick_me', userId: number, groupId = 10000, operatorId = 1000) { + this.receive({ postType: 'notice', noticeType: 'group_decrease', subType, userId, groupId, operatorId }) + } + + receiveGroupBan (subType: 'ban' | 'lift_ban', duration: number, userId: number, groupId = 10000, operatorId = 1000) { + this.receive({ postType: 'notice', noticeType: 'group_ban', subType, userId, groupId, operatorId, duration }) + } + + receiveFriendAdd (userId: number) { + this.receive({ postType: 'notice', noticeType: 'friend_add', userId }) } receiveMessage (meta: Meta): Promise diff --git a/packages/test-utils/tests/__snapshots__/app.spec.ts.snap b/packages/test-utils/tests/__snapshots__/app.spec.ts.snap index 988f79ea53..62ddd5ac1f 100644 --- a/packages/test-utils/tests/__snapshots__/app.spec.ts.snap +++ b/packages/test-utils/tests/__snapshots__/app.spec.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Mocked Server Implementation shouldMatchSnapshot 1`] = ` +exports[`Sender shouldMatchSnapshot 1`] = ` Array [ "send_private_msg", Object { diff --git a/packages/test-utils/tests/app.spec.ts b/packages/test-utils/tests/app.spec.ts index 952373db6c..149e4da367 100644 --- a/packages/test-utils/tests/app.spec.ts +++ b/packages/test-utils/tests/app.spec.ts @@ -6,7 +6,7 @@ beforeAll(() => app.start()) afterAll(() => app.stop()) -describe('Mocked Server Implementation', () => { +describe('Sender', () => { test('shouldHaveLastRequest', async () => { await app.sender.sendPrivateMsgAsync(123, 'foo') app.shouldHaveLastRequest('send_private_msg') @@ -45,3 +45,131 @@ describe('Mocked Server Implementation', () => { await expect(app.sender.sendPrivateMsg(123, 'foo')).resolves.toBe(321) }) }) + +describe('Receiver', () => { + test('receiveFriendRequest', async () => { + const mock = jest.fn() + app.receiver.on('request/friend', mock) + app.receiveFriendRequest(123) + expect(mock).toBeCalledTimes(1) + expect(mock).toBeCalledWith({ + $approve: expect.anything(), + $reject: expect.anything(), + postType: 'request', + requestType: 'friend', + userId: 123, + selfId: 514, + flag: 'flag', + }) + }) + + test('receiveGroupRequest', async () => { + const mock = jest.fn() + app.receiver.on('request/group/add', mock) + app.receiveGroupRequest(123, 'add') + expect(mock).toBeCalledTimes(1) + expect(mock).toBeCalledWith({ + $approve: expect.anything(), + $reject: expect.anything(), + postType: 'request', + requestType: 'group', + subType: 'add', + userId: 123, + selfId: 514, + groupId: 10000, + flag: 'flag', + }) + }) + + test('receiveGroupUpload', async () => { + const mock = jest.fn() + app.receiver.on('group-upload', mock) + app.receiveGroupUpload({} as any, 123) + expect(mock).toBeCalledTimes(1) + expect(mock).toBeCalledWith({ + postType: 'notice', + noticeType: 'group_upload', + userId: 123, + selfId: 514, + groupId: 10000, + file: {}, + }) + }) + + test('receiveGroupAdmin', async () => { + const mock = jest.fn() + app.receiver.on('group-admin/set', mock) + app.receiveGroupAdmin('set', 123) + expect(mock).toBeCalledTimes(1) + expect(mock).toBeCalledWith({ + postType: 'notice', + noticeType: 'group_admin', + subType: 'set', + userId: 123, + selfId: 514, + groupId: 10000, + }) + }) + + test('receiveGroupIncrease', async () => { + const mock = jest.fn() + app.receiver.on('group-increase/invite', mock) + app.receiveGroupIncrease('invite', 123) + expect(mock).toBeCalledTimes(1) + expect(mock).toBeCalledWith({ + postType: 'notice', + noticeType: 'group_increase', + subType: 'invite', + userId: 123, + selfId: 514, + groupId: 10000, + operatorId: 1000, + }) + }) + + test('receiveGroupDecrease', async () => { + const mock = jest.fn() + app.receiver.on('group-decrease/kick', mock) + app.receiveGroupDecrease('kick', 123) + expect(mock).toBeCalledTimes(1) + expect(mock).toBeCalledWith({ + postType: 'notice', + noticeType: 'group_decrease', + subType: 'kick', + userId: 123, + selfId: 514, + groupId: 10000, + operatorId: 1000, + }) + }) + + test('receiveGroupBan', async () => { + const mock = jest.fn() + app.receiver.on('group-ban/ban', mock) + app.receiveGroupBan('ban', 60, 123) + expect(mock).toBeCalledTimes(1) + expect(mock).toBeCalledWith({ + postType: 'notice', + noticeType: 'group_ban', + subType: 'ban', + userId: 123, + selfId: 514, + groupId: 10000, + operatorId: 1000, + duration: 60, + }) + }) + + test('receiveFriendAdd', async () => { + const mock = jest.fn() + app.receiver.on('friend-add', mock) + app.receiveFriendAdd(123) + expect(mock).toBeCalledTimes(1) + expect(mock).toBeCalledWith({ + postType: 'notice', + noticeType: 'friend_add', + userId: 123, + selfId: 514, + }) + }) +})