Skip to content

Commit

Permalink
fix(test-utils): resolve replies at next tick
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 8, 2020
1 parent c64f716 commit a2a9faf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
8 changes: 5 additions & 3 deletions packages/koishi-test-utils/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ class MockedServer extends Server {
}

post(path: string, body: any, headers?: Record<string, any>) {
return this.receive('POST', path, headers, JSON.stringify(body))
return this.receive('POST', path, {
...headers,
'content-type': 'application/json',
}, JSON.stringify(body))
}

receive(method: string, path: string, headers: Record<string, any>, content: string) {
Expand All @@ -36,7 +39,6 @@ class MockedServer extends Server {
req.url = path
req.method = method
Object.assign(req.headers, headers)
req.headers['content-type'] = 'application/json'
req.headers['content-length'] = '' + content.length
return new Promise<MockedResponse>((resolve) => {
const res = new http.ServerResponse(req)
Expand Down Expand Up @@ -133,7 +135,7 @@ export class TestSession {
if (length >= count) _resolve()
}
const dispose = this.app.on('middleware', (session) => {
if (session.$uuid === uuid) _resolve()
if (session.$uuid === uuid) process.nextTick(_resolve)
})
const uuid = this.app.receive({ ...this.meta, $send, message })
})
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-github/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class GitHub extends Webhooks {

async request(url: string, session: ReplySession, params: any, accept = 'application/vnd.github.v3+json') {
if (!session.$user.ghAccessToken) {
return this.authorize(session, '如果想使用此功能,请对机器人进行授权。输入你的 GitHub 用户名。')
return this.authorize(session, '要使用此功能,请对机器人进行授权。输入你的 GitHub 用户名。')
}

try {
Expand Down
49 changes: 42 additions & 7 deletions packages/plugin-github/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { App, BASE_SELF_ID } from 'koishi-test-utils'
import { Random } from 'koishi-utils'
import { fn, spyOn } from 'jest-mock'
import { fn, Mock, spyOn } from 'jest-mock'
import { expect } from 'chai'
import { readdirSync } from 'fs-extra'
import { resolve } from 'path'
Expand All @@ -17,7 +17,8 @@ app.plugin(github, {
repos: { 'koishijs/koishi': [123] },
})

const session = app.session(123)
const session1 = app.session(123)
const session2 = app.session(456)

// override listen
const listen = spyOn(app.server, 'listen')
Expand All @@ -29,6 +30,7 @@ const sendGroupMsg = app.bots[0].sendGroupMsg = fn()
before(async () => {
await app.start()
await app.database.getUser(123, 3)
await app.database.getUser(456, 3)
await app.database.getGroup(123, BASE_SELF_ID)
})

Expand Down Expand Up @@ -57,8 +59,8 @@ describe('GitHub Plugin', () => {
})

it('github command', async () => {
await session.shouldReply('github', '请输入用户名。')
await session.shouldReply('github satori', /^/)
await session1.shouldReply('github', '请输入用户名。')
await session1.shouldReply('github satori', /^/)
})

let counter = 10000
Expand Down Expand Up @@ -89,15 +91,48 @@ describe('GitHub Plugin', () => {

describe('Quick Interactions', () => {
it('no operation', async () => {
await session.shouldNotReply(`[CQ:reply,id=${idMap['issue_comment.created.1']}]`)
await session.shouldNotReply(`[CQ:reply,id=${idMap['issue_comment.created.1']}] .noop`)
await session1.shouldNotReply(`[CQ:reply,id=${idMap['issue_comment.created.1']}]`)
await session1.shouldNotReply(`[CQ:reply,id=${idMap['issue_comment.created.1']}] .noop`)
})

it('link', async () => {
await session.shouldReply(
await session1.shouldReply(
`[CQ:reply,id=${idMap['issue_comment.created.1']}] .link`,
'https://github.com/koishijs/koishi/issues/19#issuecomment-576277946',
)
})

type MockedReplyCallback = (err: NodeJS.ErrnoException, result: nock.ReplyFnResult) => void
type MockedReply = Mock<void, [uri: string, body: nock.Body, callback: MockedReplyCallback]>

const api = nock('https://api.github.com')
const mockResponse = (uri: string, payload: nock.ReplyFnResult) => {
const mock: MockedReply = fn((uri, body, callback) => callback(null, payload))
api.post(uri).reply(mock)
return mock
}

const createReaction = mockResponse('/repos/koishijs/koishi/issues/comments/576277946/reactions', [200])
const createComment = mockResponse('/repos/koishijs/koishi/issues/19/comments', [200])

it('react', async () => {
await session1.shouldNotReply(`[CQ:reply,id=${idMap['issue_comment.created.1']}] laugh`)
expect(createReaction.mock.calls).to.have.length(1)
expect(createComment.mock.calls).to.have.length(0)
})

it('reply', async () => {
await session1.shouldNotReply(`[CQ:reply,id=${idMap['issue_comment.created.1']}] test`)
expect(createReaction.mock.calls).to.have.length(1)
expect(createComment.mock.calls).to.have.length(1)
})

it('token not found', async () => {
await session2.shouldReply(
`[CQ:reply,id=${idMap['issue_comment.created.1']}] test`,
'要使用此功能,请对机器人进行授权。输入你的 GitHub 用户名。',
)
await session2.shouldReply('satori', /^/)
})
})
})

0 comments on commit a2a9faf

Please sign in to comment.