Skip to content

Commit

Permalink
feat(test-utils): deprecate app.receiveMessage()
Browse files Browse the repository at this point in the history
in favor of app.createSession().send()
  • Loading branch information
shigma committed Sep 3, 2020
1 parent 2ee304a commit fc147d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
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.receiveMessage('user', 'foo', 123)
await app.createSession('user', 123).send('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.receiveMessage('user', 'foo', 123)
await app.createSession('user', 123).send('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.receiveMessage('user', 'foo', 123)
await app.createSession('user', 123).send('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.receiveMessage('user', 'foo', 123)
await app.createSession('user', 123).send('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.receiveMessage('user', 'foo', 123)
await app.createSession('user', 123).send('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.receiveMessage('user', 'foo', 123)
await app.createSession('user', 123).send('foo')
await sleep(0)
expect(midWarn.mock.calls).to.have.length(1)
})
Expand Down
28 changes: 11 additions & 17 deletions packages/koishi-test-utils/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@ export class MockedApp extends App {
}))
}

receiveMessage(meta: Session): Promise<void>
receiveMessage(type: 'user', message: string, userId: number): Promise<void>
receiveMessage(type: 'group', message: string, userId: number, groupId: number): Promise<void>
receiveMessage(type: 'user' | 'group' | Session, message?: string, userId?: number, ctxId: number = userId) {
return new Promise((resolve) => {
this.once('after-middleware', () => resolve())
this.receive(typeof type === 'string' ? createMessageMeta(this, type, message, userId, ctxId) : type)
})
}

createSession(type: 'user', userId: number): TestSession
createSession(type: 'group', userId: number, groupId: number): TestSession
createSession(type: 'user' | 'group', userId: number, ctxId: number = userId) {
Expand Down Expand Up @@ -77,14 +67,17 @@ export class TestSession {
const $send = async (message: string) => {
if (message) this.replies.push(message)
}
await this.app.receiveMessage(new Session(this.app, { ...this.meta, message, $send }))
const last = this.replies[this.replies.length - 1]
this.replies = []
return last
return new Promise<string[]>((resolve) => {
this.app.once('after-middleware', () => {
resolve(this.replies)
this.replies = []
})
this.app.receive({ ...this.meta, message, $send })
})
}

shouldHaveReply(message: string, reply?: string) {
const assertion = expect(this.send(message)).eventually
const assertion = expect(this.send(message).then(replies => replies[replies.length - 1])).eventually
if (reply) {
return assertion.equal(reply)
} else {
Expand All @@ -93,8 +86,9 @@ export class TestSession {
}

async shouldHaveNoReply(message: string) {
if (await this.send(message) !== undefined) {
throw new AssertionError(`expected "${message}" to have no reply but got "${this.replies}"`)
const replies = await this.send(message)
if (replies.length) {
throw new AssertionError(`expected "${message}" to have no reply but got "${this.replies[0]}"`)
}
}
}
Expand Down

0 comments on commit fc147d6

Please sign in to comment.