Skip to content

Commit

Permalink
fix: support array message format
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 7, 2020
1 parent af50c85 commit 766ba8e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
17 changes: 11 additions & 6 deletions packages/koishi-core/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down
6 changes: 2 additions & 4 deletions packages/koishi-core/tests/runtime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ jest.setTimeout(1000)

beforeAll(() => app.start())

afterAll(() => {
app.stop()
afterAll(async () => {
await app.stop()
server.close()
})

app.command('foo <text>', { checkArgCount: true })
// make coverage happy
.userFields(['name', 'flag', 'authority', 'usage'])
.shortcut('bar1', { args: ['bar'] })
.shortcut('bar4', { oneArg: true, fuzzy: true })
.action(({ meta }, bar) => {
Expand Down
14 changes: 13 additions & 1 deletion packages/koishi-core/tests/validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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')
})
})
1 change: 1 addition & 0 deletions packages/koishi-core/tests/ws-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down

0 comments on commit 766ba8e

Please sign in to comment.