From 766ba8efcd724717d89608a692c68a00059aa1cd Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Tue, 7 Jan 2020 15:49:29 +0800 Subject: [PATCH] fix: support array message format --- packages/koishi-core/src/server.ts | 17 +++++++++++------ packages/koishi-core/tests/runtime.spec.ts | 6 ++---- packages/koishi-core/tests/validation.spec.ts | 14 +++++++++++++- packages/koishi-core/tests/ws-client.spec.ts | 1 + 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/packages/koishi-core/src/server.ts b/packages/koishi-core/src/server.ts index e735862abc..fcd6fd5102 100644 --- a/packages/koishi-core/src/server.ts +++ b/packages/koishi-core/src/server.ts @@ -3,7 +3,7 @@ import debug from 'debug' import * as http from 'http' import { errors } from './messages' import { createHmac } from 'crypto' -import { camelCase, snakeCase, capitalize, paramCase } from 'koishi-utils' +import { camelCase, snakeCase, capitalize, paramCase, CQCode } from 'koishi-utils' import { Meta, VersionInfo, ContextType } from './meta' import { App, AppOptions } from './app' import { CQResponse } from './sender' @@ -57,12 +57,17 @@ export abstract class Server { // polyfill CQHTTP 3.x events // https://cqhttp.cc/docs/4.12/#/UpgradeGuide /* eslint-disable dot-notation */ - if (typeof meta.anonymous === 'string') { - meta.anonymous = { - name: meta.anonymous, - flag: meta['anonymousFlag'], + if (meta.postType === 'message') { + if (typeof meta.anonymous === 'string') { + meta.anonymous = { + name: meta.anonymous, + flag: meta['anonymousFlag'], + } + delete meta['anonymousFlag'] + } + if (Array.isArray(meta.message)) { + meta.message = CQCode.stringifyAll(meta.message) } - delete meta['anonymousFlag'] // @ts-ignore } else if (meta.postType === 'event') { meta.postType = 'notice' diff --git a/packages/koishi-core/tests/runtime.spec.ts b/packages/koishi-core/tests/runtime.spec.ts index f7800a8dd2..53b20dfd93 100644 --- a/packages/koishi-core/tests/runtime.spec.ts +++ b/packages/koishi-core/tests/runtime.spec.ts @@ -12,14 +12,12 @@ jest.setTimeout(1000) beforeAll(() => app.start()) -afterAll(() => { - app.stop() +afterAll(async () => { + await app.stop() server.close() }) app.command('foo ', { checkArgCount: true }) - // make coverage happy - .userFields(['name', 'flag', 'authority', 'usage']) .shortcut('bar1', { args: ['bar'] }) .shortcut('bar4', { oneArg: true, fuzzy: true }) .action(({ meta }, bar) => { diff --git a/packages/koishi-core/tests/validation.spec.ts b/packages/koishi-core/tests/validation.spec.ts index 185b4a9b47..e65da0162b 100644 --- a/packages/koishi-core/tests/validation.spec.ts +++ b/packages/koishi-core/tests/validation.spec.ts @@ -11,10 +11,13 @@ const path = resolve(__dirname, '../temp') const server = createServer() const app = createApp({ - database: { level: { path } } + database: { level: { path } }, }) app.command('cmd1', { authority: 2, maxUsage: 1 }) + // make coverage happy + .userFields(['name', 'flag', 'authority', 'usage']) + .groupFields(['id', 'flag', 'assignee']) .option('--bar', '', { authority: 3 }) .option('--baz', '', { notUsage: true }) .action(({ meta }) => meta.$send('1:' + meta.$user.id)) @@ -82,19 +85,28 @@ describe('middleware validation', () => { describe('command validation', () => { test('check authority', async () => { + app.command('cmd1', { showWarning: true }) await session2.shouldHaveResponse('cmd1', messages.LOW_AUTHORITY) await session1.shouldHaveResponse('cmd1 --bar', messages.LOW_AUTHORITY) + app.command('cmd1', { showWarning: false }) + await session2.shouldHaveNoResponse('cmd1') }) test('check usage', async () => { + app.command('cmd1', { showWarning: true }) await session1.shouldHaveResponse('cmd1', '1:123') await session1.shouldHaveResponse('cmd1 --baz', '1:123') await session1.shouldHaveResponse('cmd1', messages.USAGE_EXHAUSTED) await session1.shouldHaveResponse('cmd1 --baz', '1:123') + app.command('cmd1', { showWarning: false }) + await session1.shouldHaveNoResponse('cmd1') }) test('check frequency', async () => { + app.command('cmd2', { showWarning: true }) await session2.shouldHaveResponse('cmd2', '2:456') await session2.shouldHaveResponse('cmd2', messages.TOO_FREQUENT) + app.command('cmd2', { showWarning: false }) + await session1.shouldHaveNoResponse('cmd2') }) }) diff --git a/packages/koishi-core/tests/ws-client.spec.ts b/packages/koishi-core/tests/ws-client.spec.ts index 734b750890..ca498afd3c 100644 --- a/packages/koishi-core/tests/ws-client.spec.ts +++ b/packages/koishi-core/tests/ws-client.spec.ts @@ -66,6 +66,7 @@ describe('WebSocket Server', () => { }) test('make polyfills', async () => { + await postMeta({ postType: 'message', groupId: 123, message: [{ type: 'text', data: { text: 'foo' } }] as any }) await postMeta({ postType: 'message', groupId: 123, message: '', anonymous: 'foo' as any, ['anonymousFlag' as any]: 'bar' }) await postMeta({ postType: 'request', userId: 123, requestType: 'friend', message: 'baz' }) await postMeta({ postType: 'event' as any, userId: 123, ['event' as any]: 'frient_add' })