Skip to content

Commit

Permalink
test(teach): probability tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 1, 2020
1 parent d54e4cd commit 6ffde4a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 16 deletions.
12 changes: 6 additions & 6 deletions packages/koishi-core/tests/session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,19 @@ describe('Session API', () => {
app.middleware(async (session, next) => {
if (session.message !== 'prompt') return next()
await session.$send('prompt text')
session.$prompt().then(
message => session.$send('received ' + message),
() => session.$send('received nothing'),
)
;(async () => {
const message = await session.$prompt() || 'nothing'
await session.$send('received ' + message)
})()
})

it('session.$prompt (resolved)', async () => {
it('session.$prompt 1', async () => {
await session.shouldHaveReply('prompt', 'prompt text')
await session.shouldHaveReply('foo', 'received foo')
await session.shouldHaveNoReply('foo')
})

it('session.$prompt (rejected)', async () => {
it('session.$prompt 2', async () => {
app.options.promptTimeout = 0
await session.shouldHaveReply('prompt', 'prompt text')
await sleep(0)
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-eval/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ before(() => app.start())

after(() => app.stop())

describe('koishi-plugin-eval', () => {
describe('Plugin Eval', () => {
it('basic support', async () => {
await ses.shouldHaveReply('> 1+1', '2')
await ses.shouldHaveNoReply('>> 1+1')
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-teach/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
"koishi-core": "^2.1.0"
},
"devDependencies": {
"@sinonjs/fake-timers": "^6.0.1",
"@types/sinonjs__fake-timers": "^6.0.1",
"jest-mock": "^26.3.0",
"koishi-plugin-mongo": "^1.0.2",
"koishi-plugin-mysql": "^2.0.0",
"koishi-test-utils": "^4.0.0"
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-teach/src/receiver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context, User, Session, NextFunction, Command } from 'koishi-core'
import { CQCode, simplify, noop, escapeRegExp } from 'koishi-utils'
import { CQCode, simplify, noop, escapeRegExp, Random } from 'koishi-utils'
import { Dialogue, DialogueTest } from './utils'

declare module 'koishi-core/dist/app' {
Expand Down Expand Up @@ -179,7 +179,7 @@ export async function triggerDialogue(ctx: Context, session: Session, config: Di
let dialogue: Dialogue
const total = await getTotalWeight(ctx, state)
if (!total) return next()
const target = Math.random() * Math.max(1, total)
const target = Random.real(Math.max(1, total))
let pointer = 0
for (const _dialogue of dialogues) {
pointer += _dialogue._weight
Expand Down
62 changes: 55 additions & 7 deletions packages/plugin-teach/tests/basic.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { App } from 'koishi-test-utils'
import { Random } from 'koishi-utils'
import { fn, spyOn } from 'jest-mock'
import { install, InstalledClock } from '@sinonjs/fake-timers'
import * as teach from 'koishi-plugin-teach'
import * as utils from './utils'
import { expect } from 'chai'
import { fn } from 'jest-mock'

describe('Plugin Teach', () => {
describe('basic support', () => {
describe('Basic Support', () => {
const app = new App({ prefix: '.' })
const session1 = app.createSession('group', 123, 456)
const session2 = app.createSession('group', 321, 456)
Expand Down Expand Up @@ -78,7 +79,7 @@ describe('Plugin Teach', () => {
})

function createEnvironment(config: teach.Config) {
const app = new App({ userCacheAge: Number.EPSILON })
const app = new App({ userCacheAge: Number.EPSILON, nickname: ['koishi', 'satori'] })
const u2id = 200, u3id = 300, u4id = 400
const g1id = 100, g2id = 200
const u2 = app.createSession('user', u2id)
Expand Down Expand Up @@ -119,7 +120,54 @@ describe('Plugin Teach', () => {
const DETAIL_HEAD = '编号为 1 的问答信息:\n问题:foo\n回答:bar\n'
const SEARCH_HEAD = '问题“foo”的回答如下:\n'

describe('context', () => {
describe('Internal', () => {
const { u3g1 } = createEnvironment({})

let clock: InstalledClock
const randomReal = spyOn(Random, 'real')

before(() => {
clock = install({ shouldAdvanceTime: true, advanceTimeDelta: 5 })
randomReal.mockReturnValue(1 - Number.EPSILON)
})

after(() => {
clock.uninstall()
randomReal.mockRestore()
})

it('appellative', async () => {
await u3g1.shouldHaveReply('# koishi,foo bar', '问答已添加,编号为 1。')
await u3g1.shouldHaveNoReply('foo')
await u3g1.shouldHaveReply('koishi, foo', 'bar')
await u3g1.shouldHaveReply('satori, foo', 'bar')
// TODO support at-trigger
// await u3g1.shouldHaveReply(`[CQ:at,qq=${app.selfId}] foo`, 'bar')
await u3g1.shouldHaveReply('#1', '编号为 1 的问答信息:\n问题:koishi,foo\n回答:bar\n触发权重:p=0, P=1')
await u3g1.shouldHaveReply('## foo', SEARCH_HEAD + '1. [p=0, P=1] bar')
})

it('activated', async () => {
await u3g1.shouldHaveReply('# koishi ?', '问答已添加,编号为 2。')
await u3g1.shouldHaveReply('koishi', '?')
await u3g1.shouldHaveReply('foo', 'bar')

// due to mocked Random.real
await u3g1.shouldHaveReply('# satori ! -p 0.5', '问答已添加,编号为 3。')
await u3g1.shouldHaveNoReply('satori')
})

it('regular expression', async () => {
clock.runAll()
await u3g1.shouldHaveReply('# foo baz -xP 0.5', '问答已添加,编号为 4。')
await u3g1.shouldHaveNoReply('foo')
await u3g1.shouldHaveReply('koishi, fooo', 'baz')
await u3g1.shouldHaveReply('#4 -p 0.5 -P 1', '问答 4 已成功修改。')
await u3g1.shouldHaveReply('koishi, fooo', 'baz')
})
})

describe('Context', () => {
const { u3, u3g1, u3g2 } = createEnvironment({ useContext: true })

it('validate options 1', async () => {
Expand Down Expand Up @@ -186,7 +234,7 @@ describe('Plugin Teach', () => {
})
})

describe('writer', () => {
describe('Writer', () => {
const { app, u2, u2g1, u3g1, u4g2 } = createEnvironment({ useWriter: true })

app.command('test').action(({ session }) => '' + session.userId)
Expand Down Expand Up @@ -246,7 +294,7 @@ describe('Plugin Teach', () => {
})
})

describe('restriction', () => {
describe('Restriction', () => {
// make coverage happy
new App().plugin(teach, { throttle: [] })
new App().plugin(teach, { preventLoop: [] })
Expand Down

0 comments on commit 6ffde4a

Please sign in to comment.