diff --git a/packages/koishi-core/tests/hook.spec.ts b/packages/koishi-core/tests/hook.spec.ts index 351b132c2a..a0482b65e8 100644 --- a/packages/koishi-core/tests/hook.spec.ts +++ b/packages/koishi-core/tests/hook.spec.ts @@ -97,7 +97,7 @@ describe('Hook API', () => { const mid2 = wrap((_, 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]) }) @@ -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]) }) @@ -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]) }) @@ -130,7 +130,7 @@ describe('Hook API', () => { const mid5 = wrap((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]) }) @@ -138,7 +138,7 @@ describe('Hook API', () => { 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) }) @@ -146,7 +146,7 @@ describe('Hook API', () => { 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) }) diff --git a/packages/koishi-test-utils/src/app.ts b/packages/koishi-test-utils/src/app.ts index 797ad4574b..ef399dde84 100644 --- a/packages/koishi-test-utils/src/app.ts +++ b/packages/koishi-test-utils/src/app.ts @@ -34,16 +34,6 @@ export class MockedApp extends App { })) } - receiveMessage(meta: Session): Promise - receiveMessage(type: 'user', message: string, userId: number): Promise - receiveMessage(type: 'group', message: string, userId: number, groupId: number): Promise - 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) { @@ -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((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 { @@ -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]}"`) } } }