Skip to content

Commit

Permalink
test: fix broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 18, 2020
1 parent cdb98c0 commit 91d2719
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/koishi-core/tests/runtime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('Runtime', () => {
await session4.shouldNotReply('cmd2')
await session1.shouldReply('-cmd2', 'cmd2:123')
await session4.shouldReply('-cmd2', 'cmd2:123')
await session4.shouldNotReply(`[CQ:reply,id=123][CQ:at,qq=${app.selfId}] cmd2`)
await session4.shouldNotReply(`[CQ:reply,id=123] [CQ:at,qq=${app.selfId}] cmd2`)
})

it('single nickname', async () => {
Expand Down
13 changes: 12 additions & 1 deletion packages/koishi-test-utils/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppOptions, App, Server, Session, AppStatus } from 'koishi-core'
import { AppOptions, App, Server, Session, AppStatus, Bot, SenderInfo } from 'koishi-core'
import { assert } from 'chai'
import { Socket } from 'net'
import * as http from 'http'
Expand All @@ -12,6 +12,17 @@ interface MockedResponse {
headers: Record<string, any>
}

Bot.prototype.getMsg = async function (this: Bot, messageId: number) {
return {
messageId,
message: '',
time: 0,
realId: 0,
messageType: null,
sender: { userId: this.selfId } as SenderInfo,
}
}

class MockedServer extends Server {
constructor(app: App) {
super(app)
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-common/tests/sender.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Sender Commands', () => {
sendPrivateMsg.mockClear()
await session1.shouldNotReply('bar')
expect(sendPrivateMsg.mock.calls).to.have.length(0)
await session1.shouldNotReply('[CQ:reply,id=1000] bar')
await session1.shouldNotReply(`[CQ:reply,id=1000] [CQ:at,qq=${app.selfId}] bar`)
expect(sendPrivateMsg.mock.calls).to.have.length(1)
expect(sendPrivateMsg.mock.calls).to.have.shape([[123, 'bar']])
})
Expand Down
18 changes: 9 additions & 9 deletions packages/plugin-github/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ describe('GitHub Plugin', () => {

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

it('link', async () => {
await session1.shouldReply(
`[CQ:reply,id=${idMap['issue_comment.created.1']}] .link`,
`[CQ:reply,id=${idMap['issue_comment.created.1']}] [CQ:at,qq=${BASE_SELF_ID}] .link`,
'https://github.com/koishijs/koishi/issues/19#issuecomment-576277946',
)
})
Expand All @@ -134,34 +134,34 @@ describe('GitHub Plugin', () => {

it('react', async () => {
const reaction = mockResponse('/repos/koishijs/koishi/issues/comments/576277946/reactions', [200])
await session1.shouldNotReply(`[CQ:reply,id=${idMap['issue_comment.created.1']}] laugh`)
await session1.shouldNotReply(`[CQ:reply,id=${idMap['issue_comment.created.1']}] [CQ:at,qq=${BASE_SELF_ID}] laugh`)
expect(reaction.mock.calls).to.have.length(1)
})

it('reply', async () => {
const comment = mockResponse('/repos/koishijs/koishi/issues/19/comments', [200])
await session1.shouldNotReply(`[CQ:reply,id=${idMap['issue_comment.created.1']}] test`)
await session1.shouldNotReply(`[CQ:reply,id=${idMap['issue_comment.created.1']}] [CQ:at,qq=${BASE_SELF_ID}] test`)
expect(comment.mock.calls).to.have.length(1)
})

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

it('request error', async () => {
apiScope.post('/repos/koishijs/koishi/issues/19/comments').replyWithError('foo')
await session1.shouldReply(`[CQ:reply,id=${idMap['issue_comment.created.1']}] test`, '发送失败。')
await session1.shouldReply(`[CQ:reply,id=${idMap['issue_comment.created.1']}] [CQ:at,qq=${BASE_SELF_ID}] test`, '发送失败。')
})

it('refresh token', async () => {
const unauthorized = mockResponse('/repos/koishijs/koishi/issues/19/comments', [401])
tokenInterceptor.reply(401)
await session1.shouldReply(
`[CQ:reply,id=${idMap['issue_comment.created.1']}] test`,
`[CQ:reply,id=${idMap['issue_comment.created.1']}] [CQ:at,qq=${BASE_SELF_ID}] test`,
'令牌已失效,需要重新授权。输入你的 GitHub 用户名。',
)
expect(unauthorized.mock.calls).to.have.length(1)
Expand All @@ -172,7 +172,7 @@ describe('GitHub Plugin', () => {
const unauthorized = mockResponse('/repos/koishijs/koishi/issues/19/comments', [401])
tokenInterceptor.reply(200, payload)
const notFound = mockResponse('/repos/koishijs/koishi/issues/19/comments', [404])
await session1.shouldReply(`[CQ:reply,id=${idMap['issue_comment.created.1']}] test`, '发送失败。')
await session1.shouldReply(`[CQ:reply,id=${idMap['issue_comment.created.1']}] [CQ:at,qq=${BASE_SELF_ID}] test`, '发送失败。')
expect(unauthorized.mock.calls).to.have.length(1)
expect(notFound.mock.calls).to.have.length(1)
})
Expand Down

0 comments on commit 91d2719

Please sign in to comment.