Skip to content

Commit

Permalink
feat(test-utils): support mockDatabase / mockStart options
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 4, 2020
1 parent 504b403 commit aef4e72
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/koishi-core/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export abstract class Server {
}

prepare(data: any) {
if (this.app.status !== AppStatus.open) return
const meta = camelCase<Meta>(data)
if (!this.bots[meta.selfId]) {
const bot = this.bots.find(bot => !bot.selfId)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-core/tests/help.spec.ts
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
8 changes: 5 additions & 3 deletions packages/koishi-core/tests/runtime.spec.ts
Original file line number Diff line number Diff line change
@@ -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([])

Expand Down Expand Up @@ -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')
Expand All @@ -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 () => {
Expand Down
11 changes: 6 additions & 5 deletions packages/koishi-core/tests/server.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -48,18 +48,19 @@ 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
})
})

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')
Expand Down
12 changes: 10 additions & 2 deletions packages/koishi-test-utils/src/app.ts
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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() {
Expand Down
5 changes: 2 additions & 3 deletions packages/koishi-test-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 1 addition & 1 deletion packages/koishi-test-utils/src/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions packages/koishi-test-utils/tests/memory.spec.ts
Original file line number Diff line number Diff line change
@@ -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' {
Expand All @@ -19,7 +19,7 @@ interface FooData {
bar: string
}

extendDatabase(MemoryDatabase, {
extendDatabase<typeof MemoryDatabase>('koishi-test-utils', {
async createFoo(data: Partial<FooData> = {}) {
return this.$create('foo', data) as FooData
},
Expand All @@ -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 = [],
})
Expand Down
9 changes: 7 additions & 2 deletions packages/koishi-utils/tests/time.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions packages/plugin-common/tests/admin.spec.ts
Original file line number Diff line number Diff line change
@@ -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'))
Expand Down
7 changes: 3 additions & 4 deletions packages/plugin-common/tests/info.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
9 changes: 5 additions & 4 deletions packages/plugin-github/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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'
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(),
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-teach/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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 {
'dialogue/memory'(dialogue: Dialogue, test: DialogueTest): boolean | void
}
}

extendDatabase(MemoryDatabase, {
extendDatabase(memory.MemoryDatabase, {
async getDialoguesById(ids) {
if (!ids.length) return []
const table = this.$table('dialogue')
Expand Down

0 comments on commit aef4e72

Please sign in to comment.