Skip to content

Commit

Permalink
feat(test-utils): receiver API updates
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 4, 2020
1 parent af6a7c0 commit 3db7992
Show file tree
Hide file tree
Showing 13 changed files with 470 additions and 452 deletions.
48 changes: 24 additions & 24 deletions packages/koishi-core/tests/help.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { install } from '@sinonjs/fake-timers'
Message.GLOBAL_HELP_EPILOG = 'EPILOG'

const app = new App().plugin(memory)
const session = app.createSession('user', 123)
const session = app.session(123)
const now = Date.now()

before(async () => {
Expand All @@ -23,23 +23,23 @@ let message: string

describe('Help Command', () => {
it('basic support', async () => {
await session.shouldHaveReply('help', [
await session.shouldReply('help', [
'当前可用的指令有:',
' help 显示帮助信息',
'EPILOG',
].join('\n'))

await session.shouldHaveReply('help help', message = [
await session.shouldReply('help help', message = [
'help [command]',
'显示帮助信息',
'可用的选项有:',
' -a, --authority 显示权限设置',
' -H, --show-hidden 查看隐藏的选项和指令',
].join('\n'))

await session.shouldHaveReply('help heip', '指令未找到。你要找的是不是“help”?发送空行或句号以调用推测的指令。')
await session.shouldHaveReply('.', message)
await session.shouldHaveReply('help -h', message)
await session.shouldReply('help heip', '指令未找到。你要找的是不是“help”?发送空行或句号以调用推测的指令。')
await session.shouldReply('.', message)
await session.shouldReply('help -h', message)
})

it('command attributes', async () => {
Expand All @@ -55,15 +55,15 @@ describe('Help Command', () => {

const clock = install({ now })

await session.shouldHaveReply('help foo1', 'foo1\nDESCRIPTION\n别名:foo。')
await session.shouldHaveReply('help foo2', 'foo2\nDESCRIPTION\n最低权限:2 级。')
await session.shouldHaveReply('help foo3', 'foo3\nDESCRIPTION(指令已禁用)')
await session.shouldHaveReply('help foo4', 'foo4\nDESCRIPTION\nUSAGE TEXT')
await session.shouldHaveReply('help foo5', 'foo5\nDESCRIPTION\n123')
await session.shouldHaveReply('help foo6', 'foo6\nDESCRIPTION\n使用示例:\n EXAMPLE TEXT')
await session.shouldHaveReply('help foo7', 'foo7\nDESCRIPTION\n已调用次数:1/3。')
await session.shouldHaveReply('help foo8', 'foo8\nDESCRIPTION\n距离下次调用还需:60/180 秒。')
await session.shouldHaveReply('help foo9', 'foo9\nDESCRIPTION\n距离下次调用还需:0/180 秒。')
await session.shouldReply('help foo1', 'foo1\nDESCRIPTION\n别名:foo。')
await session.shouldReply('help foo2', 'foo2\nDESCRIPTION\n最低权限:2 级。')
await session.shouldReply('help foo3', 'foo3\nDESCRIPTION(指令已禁用)')
await session.shouldReply('help foo4', 'foo4\nDESCRIPTION\nUSAGE TEXT')
await session.shouldReply('help foo5', 'foo5\nDESCRIPTION\n123')
await session.shouldReply('help foo6', 'foo6\nDESCRIPTION\n使用示例:\n EXAMPLE TEXT')
await session.shouldReply('help foo7', 'foo7\nDESCRIPTION\n已调用次数:1/3。')
await session.shouldReply('help foo8', 'foo8\nDESCRIPTION\n距离下次调用还需:60/180 秒。')
await session.shouldReply('help foo9', 'foo9\nDESCRIPTION\n距离下次调用还需:0/180 秒。')

clock.uninstall()
})
Expand All @@ -75,18 +75,18 @@ describe('Help Command', () => {
.option('opt2', '选项2', { notUsage: true })
.option('opt3', '选项3', { hidden: true })

await session.shouldHaveReply('help bar', message = 'bar\nDESCRIPTION\n已调用次数:0/2。')
await session.shouldReply('help bar', message = 'bar\nDESCRIPTION\n已调用次数:0/2。')

bar.config.hideOptions = false

await session.shouldHaveReply('help bar', [
await session.shouldReply('help bar', [
message,
'可用的选项有:',
' --opt1 选项1',
' --opt2 选项2(不计入总次数)',
].join('\n'))

await session.shouldHaveReply('help bar -H', [
await session.shouldReply('help bar -H', [
message,
'可用的选项有:',
' -h, --help 显示此信息',
Expand All @@ -95,7 +95,7 @@ describe('Help Command', () => {
' --opt3 选项3',
].join('\n'))

await session.shouldHaveReply('help bar -a', [
await session.shouldReply('help bar -a', [
message,
'可用的选项有(括号内为额外要求的权限等级):',
' (2) --opt1 选项1',
Expand All @@ -109,21 +109,21 @@ describe('Help Command', () => {
const foo1 = foo2.subcommand('foo1')
const foo3 = foo1.subcommand('foo3')

await session.shouldHaveReply('help foo2', [
await session.shouldReply('help foo2', [
'foo2',
'DESCRIPTION',
'可用的子指令有:',
' foo1 DESCRIPTION',
].join('\n'))

await session.shouldHaveReply('help foo2 -a', [
await session.shouldReply('help foo2 -a', [
'foo2',
'DESCRIPTION',
'可用的子指令有(括号内为对应的最低权限等级,标有星号的表示含有子指令):',
' foo1 (1*) DESCRIPTION',
].join('\n'))

await session.shouldHaveReply('help foo1 -a', [
await session.shouldReply('help foo1 -a', [
'foo1',
'DESCRIPTION',
'别名:foo。',
Expand All @@ -136,7 +136,7 @@ describe('Help Command', () => {
Message.GLOBAL_HELP_EPILOG = ''

const app = new App()
const session = app.createSession('user', 123)
await session.shouldHaveReply('help', '当前可用的指令有:\n help 显示帮助信息')
const session = app.session(123)
await session.shouldReply('help', '当前可用的指令有:\n help 显示帮助信息')
})
})
12 changes: 6 additions & 6 deletions packages/koishi-core/tests/hook.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('Hook API', () => {
const mid2 = wrap<Middleware>((_, next) => next())
app.addMiddleware(mid1)
app.addMiddleware(mid2)
await app.createSession('user', 123).send('foo')
await app.session(123).receive('foo')
expect(callSequence).to.deep.equal([mid1, mid2])
})

Expand All @@ -107,7 +107,7 @@ describe('Hook API', () => {
app.addMiddleware(mid1)
app.addMiddleware(mid2)
expect(callSequence).to.deep.equal([])
await app.createSession('user', 123).send('foo')
await app.session(123).receive('foo')
expect(callSequence).to.deep.equal([mid1])
})

Expand All @@ -118,7 +118,7 @@ describe('Hook API', () => {
app.addMiddleware(mid1)
app.prependMiddleware(mid2)
app.prependMiddleware(mid3)
await app.createSession('user', 123).send('foo')
await app.session(123).receive('foo')
expect(callSequence).to.deep.equal([mid3, mid2, mid1])
})

Expand All @@ -130,23 +130,23 @@ describe('Hook API', () => {
const mid5 = wrap<NextFunction>((next) => next())
app.addMiddleware(mid1)
app.addMiddleware(mid2)
await app.createSession('user', 123).send('foo')
await app.session(123).receive('foo')
expect(callSequence).to.deep.equal([mid1, mid2, mid3, mid4, mid5])
})

it('middleware error', async () => {
midWarn.mockClear()
const errorMessage = 'error message'
app.addMiddleware(() => { throw new Error(errorMessage) })
await app.createSession('user', 123).send('foo')
await app.session(123).receive('foo')
expect(midWarn.mock.calls).to.have.length(1)
})

it('isolated next function', async () => {
midWarn.mockClear()
app.addMiddleware((_, next) => (next(), undefined))
app.addMiddleware((_, next) => sleep(0).then(() => next()))
await app.createSession('user', 123).send('foo')
await app.session(123).receive('foo')
await sleep(0)
expect(midWarn.mock.calls).to.have.length(1)
})
Expand Down
Loading

0 comments on commit 3db7992

Please sign in to comment.