Skip to content

Commit

Permalink
fix(core): await cmd._checkUser
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 7, 2020
1 parent 766ba8e commit 3783f42
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Lint
run: yarn lint
- name: Unit Test
run: yarn jest --coverage
run: yarn jest
- name: Report Coverage
uses: codecov/codecov-action@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"bump": "ts-node build/bump",
"dep": "ts-node build/dep",
"docs": "cd docs & yarn dev",
"jest": "jest .*\\.spec\\.ts --runInBand",
"jest": "jest .*\\.spec\\.ts --coverage --runInBand",
"lint": "eslint packages/*/src/**/*.ts --cache",
"start": "ts-node build/start"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi-core/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export class Command {
}

// check authority and usage
if (!this._checkUser(meta, options)) return
if (!await this._checkUser(meta, options)) return

// execute command
showCommandLog('execute %s', this.name)
Expand Down
31 changes: 21 additions & 10 deletions packages/koishi-core/tests/runtime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,40 +171,51 @@ describe('command execution', () => {
expect(app.getCommand('fo', meta)).toBeUndefined()
})

test('excute command', () => {
app.executeCommandLine('foo bar', meta)
test('excute command', async () => {
await app.executeCommandLine('foo bar', meta)
expect(mock).toBeCalledTimes(1)
expect(mock).toBeCalledWith('foobar')
})

test('command error', () => {
test('command error', async () => {
const mock1 = jest.fn()
const mock2 = jest.fn()
app.receiver.on('error', mock1)
app.receiver.on('error/command', mock2)
app.executeCommandLine('err', meta)
await app.executeCommandLine('err', meta)
expect(mock1).toBeCalledTimes(1)
expect(mock1.mock.calls[0][0]).toHaveProperty('message', 'command error')
expect(mock2).toBeCalledTimes(1)
expect(mock2.mock.calls[0][0]).toHaveProperty('message', 'command error')
})

test('command not found', () => {
test('command events', async () => {
app.command('skipped-command').action(({ next }) => next())
const beforeCommandCallback = jest.fn()
const afterCommandCallback = jest.fn()
app.receiver.on('before-command', beforeCommandCallback)
app.receiver.on('after-command', afterCommandCallback)
app.runCommand('skipped-command', meta)
expect(beforeCommandCallback).toBeCalledTimes(1)
expect(afterCommandCallback).toBeCalledTimes(0)
})

test('command not found', async () => {
app.runCommand('bar', meta, ['foo'])
expect(mock).toBeCalledTimes(1)
expect(mock).toBeCalledWith(messages.COMMAND_NOT_FOUND)
app.executeCommandLine('bar', meta, mock)
await app.executeCommandLine('bar', meta, mock)
expect(mock).toBeCalledTimes(2)
})

test('insufficient arguments', () => {
app.executeCommandLine('foo', meta)
test('insufficient arguments', async () => {
await app.executeCommandLine('foo', meta)
expect(mock).toBeCalledTimes(1)
expect(mock).toBeCalledWith(messages.INSUFFICIENT_ARGUMENTS)
})

test('redunant arguments', () => {
app.executeCommandLine('foo bar baz', meta)
test('redunant arguments', async () => {
await app.executeCommandLine('foo bar baz', meta)
expect(mock).toBeCalledTimes(1)
expect(mock).toBeCalledWith(messages.REDUNANT_ARGUMENTS)
})
Expand Down
6 changes: 3 additions & 3 deletions packages/koishi-core/tests/validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ app.command('cmd1', { authority: 2, maxUsage: 1 })
.option('--baz', '', { notUsage: true })
.action(({ meta }) => meta.$send('1:' + meta.$user.id))

app.command('cmd2', { minInterval: 100, showWarning: true })
app.command('cmd2', { minInterval: () => 100 })
.option('--bar', '', { authority: 3 })
.option('--baz', '', { notUsage: true })
.action(({ meta }) => meta.$send('2:' + meta.$user.id))
Expand Down Expand Up @@ -89,7 +89,7 @@ describe('command validation', () => {
await session2.shouldHaveResponse('cmd1', messages.LOW_AUTHORITY)
await session1.shouldHaveResponse('cmd1 --bar', messages.LOW_AUTHORITY)
app.command('cmd1', { showWarning: false })
await session2.shouldHaveNoResponse('cmd1')
await session1.shouldHaveNoResponse('cmd1 --bar')
})

test('check usage', async () => {
Expand All @@ -107,6 +107,6 @@ describe('command validation', () => {
await session2.shouldHaveResponse('cmd2', '2:456')
await session2.shouldHaveResponse('cmd2', messages.TOO_FREQUENT)
app.command('cmd2', { showWarning: false })
await session1.shouldHaveNoResponse('cmd2')
await session2.shouldHaveNoResponse('cmd2')
})
})
2 changes: 0 additions & 2 deletions packages/test-utils/src/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,10 @@ export class ServerSession {

async testSnapshot (message: string) {
await expect(this.waitForResponse(message)).resolves.toMatchSnapshot(message)
await sleep(0)
}

async shouldHaveResponse (message: string, response: string) {
await expect(this.waitForResponse(message)).resolves.toBe(response)
await sleep(0)
}

async shouldHaveNoResponse (message: string): Promise<void> {
Expand Down

0 comments on commit 3783f42

Please sign in to comment.