diff --git a/packages/koishi-core/src/server.ts b/packages/koishi-core/src/server.ts
index 3426117d20..d16592abb3 100644
--- a/packages/koishi-core/src/server.ts
+++ b/packages/koishi-core/src/server.ts
@@ -44,7 +44,6 @@ export abstract class Server {
}
prepare(data: any) {
- if (this.app.status !== AppStatus.open) return
const meta = camelCase(data)
if (!this.bots[meta.selfId]) {
const bot = this.bots.find(bot => !bot.selfId)
@@ -55,6 +54,7 @@ export abstract class Server {
}
dispatch(session: Session) {
+ if (this.app.status !== AppStatus.open) return
const events: string[] = []
if (session.postType === 'message' || session.postType === 'send') {
events.push(session.postType)
diff --git a/packages/koishi-core/tests/help.spec.ts b/packages/koishi-core/tests/help.spec.ts
index 41b2356b0b..8aab6ed7c1 100644
--- a/packages/koishi-core/tests/help.spec.ts
+++ b/packages/koishi-core/tests/help.spec.ts
@@ -1,13 +1,13 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
-import { memory, App } from 'koishi-test-utils'
+import { App } from 'koishi-test-utils'
import { Time } from 'koishi-utils'
import { Message } from 'koishi-core'
import { install } from '@sinonjs/fake-timers'
Message.GLOBAL_HELP_EPILOG = 'EPILOG'
-const app = new App().plugin(memory)
+const app = new App({ mockDatabase: true })
const session = app.session(123)
const now = Date.now()
diff --git a/packages/koishi-core/tests/runtime.spec.ts b/packages/koishi-core/tests/runtime.spec.ts
index b74c0c2a7c..0d38b128af 100644
--- a/packages/koishi-core/tests/runtime.spec.ts
+++ b/packages/koishi-core/tests/runtime.spec.ts
@@ -1,15 +1,15 @@
-import { App, memory } from 'koishi-test-utils'
+import { App } from 'koishi-test-utils'
import { User, Group, Command } from 'koishi-core'
import { sleep } from 'koishi-utils'
+import { install } from '@sinonjs/fake-timers'
const app = new App({
+ mockDatabase: true,
groupCacheAge: Number.EPSILON,
userCacheAge: Number.EPSILON,
similarityCoefficient: 0,
})
-app.plugin(memory)
-
// make coverage happy
Command.groupFields([])
@@ -232,6 +232,7 @@ describe('Runtime', () => {
})
it('check frequency', async () => {
+ const clock = install()
cmd2.config.minInterval = () => 1000
cmd2.config.showWarning = true
await session2.shouldReply('cmd2', 'cmd2:456')
@@ -241,6 +242,7 @@ describe('Runtime', () => {
cmd2.config.showWarning = false
await session2.shouldNotReply('cmd2')
delete cmd2.config.minInterval
+ clock.uninstall()
})
it('check arg count', async () => {
diff --git a/packages/koishi-core/tests/server.spec.ts b/packages/koishi-core/tests/server.spec.ts
index ff40558cb8..d33d83902a 100644
--- a/packages/koishi-core/tests/server.spec.ts
+++ b/packages/koishi-core/tests/server.spec.ts
@@ -1,4 +1,4 @@
-import { App, BASE_SELF_ID, memory } from 'koishi-test-utils'
+import { App, BASE_SELF_ID } from 'koishi-test-utils'
import { App as RealApp, extendDatabase, Group, Session } from 'koishi-core'
import { expect } from 'chai'
import { fn, spyOn } from 'jest-mock'
@@ -48,8 +48,6 @@ describe('Server API', () => {
it('server.prepare', async () => {
const app = new App()
delete app.bots[0].selfId
- expect(app.server.prepare({ selfId: BASE_SELF_ID + 1 })).to.be.undefined
- await app.start()
expect(app.server.prepare({ selfId: BASE_SELF_ID + 1 })).to.be.ok
expect(app.bots[0].selfId).to.equal(BASE_SELF_ID + 1)
expect(app.server.prepare({ selfId: BASE_SELF_ID })).to.be.undefined
@@ -57,9 +55,12 @@ describe('Server API', () => {
})
describe('Sender API', () => {
- const app = new App({ broadcastDelay: Number.EPSILON }).plugin(memory)
- const bot = app.bots[0]
+ const app = new App({
+ mockDatabase: true,
+ broadcastDelay: Number.EPSILON,
+ })
+ const bot = app.bots[0]
const sendGroupMsg = bot.sendGroupMsg = fn(async (id) => {
if (id === 456) return 789
throw new Error('bar')
diff --git a/packages/koishi-test-utils/src/app.ts b/packages/koishi-test-utils/src/app.ts
index ff5b887b03..f69e0110d8 100644
--- a/packages/koishi-test-utils/src/app.ts
+++ b/packages/koishi-test-utils/src/app.ts
@@ -1,5 +1,6 @@
-import { AppOptions, App, Server, Session } from 'koishi-core'
+import { AppOptions, App, Server, Session, AppStatus } from 'koishi-core'
import { assert } from 'chai'
+import * as memory from './memory'
export const BASE_SELF_ID = 514
@@ -16,11 +17,18 @@ class MockedAppServer extends Server {
Server.types.mock = MockedAppServer
+interface MockedAppOptions extends AppOptions {
+ mockStart?: boolean
+ mockDatabase?: boolean
+}
+
export class MockedApp extends App {
public server: MockedAppServer
- constructor(options: AppOptions = {}) {
+ constructor(options: MockedAppOptions = {}) {
super({ selfId: BASE_SELF_ID, type: 'mock', ...options })
+ if (options.mockStart !== false) this.status = AppStatus.open
+ if (options.mockDatabase) this.plugin(memory)
}
get selfId() {
diff --git a/packages/koishi-test-utils/src/index.ts b/packages/koishi-test-utils/src/index.ts
index 7bf95c346e..0c8d62989e 100644
--- a/packages/koishi-test-utils/src/index.ts
+++ b/packages/koishi-test-utils/src/index.ts
@@ -1,7 +1,6 @@
-import memory from './memory'
+import { MemoryDatabase } from './memory'
-export { memory }
+export default MemoryDatabase
export * from './app'
export * from './database'
-export * from './memory'
diff --git a/packages/koishi-test-utils/src/memory.ts b/packages/koishi-test-utils/src/memory.ts
index c098e2d08c..679cb703e7 100644
--- a/packages/koishi-test-utils/src/memory.ts
+++ b/packages/koishi-test-utils/src/memory.ts
@@ -93,6 +93,6 @@ extendDatabase(MemoryDatabase, {
},
})
-export default function apply(app: App, config: MemoryConfig = {}) {
+export function apply(app: App, config: MemoryConfig = {}) {
app.database = new MemoryDatabase(app, config) as any
}
diff --git a/packages/koishi-test-utils/tests/memory.spec.ts b/packages/koishi-test-utils/tests/memory.spec.ts
index 995948f9a5..e687cc87c7 100644
--- a/packages/koishi-test-utils/tests/memory.spec.ts
+++ b/packages/koishi-test-utils/tests/memory.spec.ts
@@ -1,5 +1,5 @@
import { extendDatabase } from 'koishi-core'
-import { MemoryDatabase, testDatabase, memory, MockedApp } from 'koishi-test-utils'
+import MemoryDatabase, { testDatabase, App } from 'koishi-test-utils'
import { expect } from 'chai'
declare module 'koishi-core/dist/database' {
@@ -19,7 +19,7 @@ interface FooData {
bar: string
}
-extendDatabase(MemoryDatabase, {
+extendDatabase('koishi-test-utils', {
async createFoo(data: Partial = {}) {
return this.$create('foo', data) as FooData
},
@@ -33,7 +33,7 @@ extendDatabase(MemoryDatabase, {
},
})
-const app = testDatabase(new MockedApp().plugin(memory), {
+const app = testDatabase(new App({ mockDatabase: true }), {
beforeEachUser: app => app.database.$store.user = [],
beforeEachGroup: app => app.database.$store.group = [],
})
diff --git a/packages/koishi-utils/tests/time.spec.ts b/packages/koishi-utils/tests/time.spec.ts
index 386f1a8651..9b70acd53c 100644
--- a/packages/koishi-utils/tests/time.spec.ts
+++ b/packages/koishi-utils/tests/time.spec.ts
@@ -8,8 +8,13 @@ describe('Time Manipulations', () => {
const now = Date.UTC(2020, 3, 1, 1, 30)
const date = new Date(now)
- before(() => clock = install({ now, shouldAdvanceTime: true }))
- after(() => clock.uninstall())
+ before(() => {
+ clock = install({ now })
+ })
+
+ after(() => {
+ clock.uninstall()
+ })
it('timezone offset', () => {
Time.setTimezoneOffset(-480)
diff --git a/packages/plugin-common/tests/admin.spec.ts b/packages/plugin-common/tests/admin.spec.ts
index 57cc38d8c5..7e99ed3cd1 100644
--- a/packages/plugin-common/tests/admin.spec.ts
+++ b/packages/plugin-common/tests/admin.spec.ts
@@ -1,13 +1,12 @@
-import { MockedApp, memory } from 'koishi-test-utils'
+import { App } from 'koishi-test-utils'
import { User, Group } from 'koishi-core'
import { enumKeys } from 'koishi-utils'
import { expect } from 'chai'
import * as admin from '../src/admin'
-const app = new MockedApp()
+const app = new App({ mockDatabase: true })
const session = app.session(123, 321)
-app.plugin(memory)
app.plugin(admin)
app.command('foo', { maxUsage: 10 }).action(({ session }) => session.$send('bar'))
app.command('bar', { maxUsage: 10 }).action(({ session }) => session.$send('foo'))
diff --git a/packages/plugin-common/tests/info.spec.ts b/packages/plugin-common/tests/info.spec.ts
index a9bae32fbc..47b7c6a502 100644
--- a/packages/plugin-common/tests/info.spec.ts
+++ b/packages/plugin-common/tests/info.spec.ts
@@ -1,16 +1,15 @@
-import { MockedApp, TestSession, memory } from 'koishi-test-utils'
+import { App, TestSession } from 'koishi-test-utils'
import * as info from '../src/info'
// make coverage happy
info.registerUserInfo(() => '')
info.registerUserInfo(() => 'foo', ['flag'], -1)
-let app: MockedApp, session: TestSession
+let app: App, session: TestSession
describe('info', () => {
beforeEach(async () => {
- app = new MockedApp()
- app.plugin(memory)
+ app = new App({ mockDatabase: true })
session = app.session(123)
await app.start()
await app.database.getUser(123, 3)
diff --git a/packages/plugin-github/tests/index.spec.ts b/packages/plugin-github/tests/index.spec.ts
index d6edcb4831..81bc448526 100644
--- a/packages/plugin-github/tests/index.spec.ts
+++ b/packages/plugin-github/tests/index.spec.ts
@@ -1,4 +1,4 @@
-import { App, BASE_SELF_ID, memory } from 'koishi-test-utils'
+import { App, BASE_SELF_ID } from 'koishi-test-utils'
import { Random } from 'koishi-utils'
import { fn, spyOn } from 'jest-mock'
import { expect } from 'chai'
@@ -6,9 +6,10 @@ import { readdirSync } from 'fs-extra'
import { resolve } from 'path'
import * as github from 'koishi-plugin-github'
-const app = new App({ port: 10000 })
-
-app.plugin(memory)
+const app = new App({
+ port: 10000,
+ mockDatabase: true,
+})
app.plugin(github, {
secret: Random.uuid(),
diff --git a/packages/plugin-teach/tests/utils.ts b/packages/plugin-teach/tests/utils.ts
index 4439b96344..33f053ea8a 100644
--- a/packages/plugin-teach/tests/utils.ts
+++ b/packages/plugin-teach/tests/utils.ts
@@ -1,7 +1,7 @@
import { extendDatabase, Context } from 'koishi-core'
import { defineProperty, Observed, clone, intersection } from 'koishi-utils'
import { Dialogue, DialogueTest, equal } from 'koishi-plugin-teach'
-import { MemoryDatabase, memory } from 'koishi-test-utils'
+import * as memory from 'koishi-test-utils/dist/memory'
declare module 'koishi-core/dist/context' {
interface EventMap {
@@ -9,7 +9,7 @@ declare module 'koishi-core/dist/context' {
}
}
-extendDatabase(MemoryDatabase, {
+extendDatabase(memory.MemoryDatabase, {
async getDialoguesById(ids) {
if (!ids.length) return []
const table = this.$table('dialogue')