Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 10, 2020
2 parents a0b7c05 + 960e100 commit 1e1f0b3
Show file tree
Hide file tree
Showing 54 changed files with 1,779 additions and 439 deletions.
1 change: 1 addition & 0 deletions .mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
'packages/koishi-utils/tests/*.spec.ts',
'packages/koishi-test-utils/tests/*.spec.ts',
'packages/plugin-common/tests/admin.spec.ts',
'packages/plugin-common/tests/handler.spec.ts',
'packages/plugin-common/tests/sender.spec.ts',
'packages/plugin-eval/tests/*.spec.ts',
'packages/plugin-github/tests/*.spec.ts',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@
"kleur": "^4.1.1",
"latest-version": "^5.1.0",
"mocha": "^8.1.3",
"nock": "^13.0.4",
"open": "^7.2.1",
"ora": "^5.0.0",
"ora": "^5.1.0",
"p-map": "^4.0.0",
"prompts": "^2.3.2",
"rimraf": "^3.0.2",
Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-cqhttp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^2.2.1"
"koishi-core": "^2.2.2"
},
"devDependencies": {
"@types/ms": "^0.7.31",
"@types/ws": "^7.2.6",
"get-port": "^5.1.1",
"koishi-test-utils": "^5.0.0"
"koishi-test-utils": "^5.0.1"
},
"dependencies": {
"axios": "^0.20.0",
"ms": "^2.1.2",
"ws": "^7.3.1",
"koishi-utils": "^3.1.4"
"koishi-utils": "^3.1.5"
}
}
8 changes: 4 additions & 4 deletions packages/koishi-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-core",
"description": "Core features for Koishi",
"version": "2.2.1",
"version": "2.2.2",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"engines": {
Expand Down Expand Up @@ -36,16 +36,16 @@
"devDependencies": {
"@types/koa": "^2.11.4",
"@types/lru-cache": "^5.1.0",
"koishi-test-utils": "^5.0.0"
"koishi-test-utils": "^5.0.1"
},
"dependencies": {
"@types/koa-bodyparser": "^4.3.0",
"@types/koa-router": "^7.4.1",
"fastest-levenshtein": "^1.0.12",
"koa": "^2.13.0",
"koa-bodyparser": "^4.3.0",
"koa-router": "^9.4.0",
"koishi-utils": "^3.1.4",
"leven": "^3.1.0",
"koishi-utils": "^3.1.5",
"lru-cache": "^6.0.0"
}
}
6 changes: 3 additions & 3 deletions packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Context, Middleware, NextFunction } from './context'
import { Group, User, Database } from './database'
import { BotOptions, Server } from './server'
import { Session } from './session'
import { simplify, defineProperty, Time, Observed, coerce, escapeRegExp } from 'koishi-utils'
import { simplify, defineProperty, Time, Observed, coerce, escapeRegExp, makeArray } from 'koishi-utils'
import help from './plugins/help'
import shortcut from './plugins/shortcut'
import suggest from './plugins/suggest'
Expand Down Expand Up @@ -105,7 +105,7 @@ export class App extends Context {

prepare() {
const { nickname, prefix } = this.options
const nicknames = Array.isArray(nickname) ? nickname : nickname ? [nickname] : []
const nicknames = makeArray(nickname)
const prefixes = Array.isArray(prefix) ? prefix : [prefix || '']
this._nameRE = createLeadingRE(nicknames, '@?', '([,,]\\s*|\\s+)')
this._prefixRE = createLeadingRE(prefixes)
Expand Down Expand Up @@ -144,7 +144,7 @@ export class App extends Context {

let capture: RegExpMatchArray, atSelf = false
// eslint-disable-next-line no-cond-assign
if (capture = message.match(/^\[CQ:reply,id=(-?\d+)\]/)) {
if (capture = message.match(/^\[CQ:reply,id=(-?\d+)\]\s*/)) {
session.$reply = +capture[1]
message = message.slice(capture[0].length)
}
Expand Down
16 changes: 8 additions & 8 deletions packages/koishi-core/src/plugins/suggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Session } from '../session'
import { Message } from './message'
import { getCommands } from './help'
import { format } from 'util'
import leven from 'leven'
import { distance } from 'fastest-levenshtein'

declare module '../session' {
interface Session {
Expand Down Expand Up @@ -62,13 +62,13 @@ Session.prototype.$suggest = function $suggest(this: Session, options: SuggestOp

let suggestions: string[], minDistance = Infinity
for (const name of items) {
const distance = leven(name, target)
if (name.length <= 2 || distance > name.length * coefficient) continue
if (distance === minDistance) {
const dist = distance(name, target)
if (name.length <= 2 || dist > name.length * coefficient) continue
if (dist === minDistance) {
suggestions.push(name)
} else if (distance < minDistance) {
} else if (dist < minDistance) {
suggestions = [name]
minDistance = distance
minDistance = dist
}
}
if (!suggestions) return next(() => this.$send(prefix))
Expand All @@ -90,8 +90,8 @@ Session.prototype.$suggest = function $suggest(this: Session, options: SuggestOp

export default function apply(ctx: Context) {
ctx.middleware((session, next) => {
const { $argv, $parsed, $prefix, $appel, messageType } = session
if ($argv || messageType !== 'private' && $prefix === null && !$appel) return next()
const { $argv, $reply, $parsed, $prefix, $appel, messageType } = session
if ($argv || $reply || messageType !== 'private' && $prefix === null && !$appel) return next()
const target = $parsed.split(/\s/, 1)[0].toLowerCase()
if (!target) return next()

Expand Down
6 changes: 3 additions & 3 deletions packages/koishi-test-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-test-utils",
"description": "Test utilities for Koishi",
"version": "5.0.0",
"version": "5.0.1",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"engines": {
Expand Down Expand Up @@ -40,8 +40,8 @@
"dependencies": {
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"koishi-core": "^2.2.1",
"koishi-utils": "^3.1.4"
"koishi-core": "^2.2.2",
"koishi-utils": "^3.1.5"
},
"devDependencies": {
"@types/chai": "^4.2.12",
Expand Down
53 changes: 49 additions & 4 deletions packages/koishi-test-utils/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { AppOptions, App, Server, Session, AppStatus } from 'koishi-core'
import { assert } from 'chai'
import { Socket } from 'net'
import * as http from 'http'
import * as memory from './memory'

export const BASE_SELF_ID = 514

class MockedAppServer extends Server {
interface MockedResponse {
code: number
body: string
headers: Record<string, any>
}

class MockedServer extends Server {
constructor(app: App) {
super(app)
this.bots.forEach(bot => bot.ready = true)
Expand All @@ -13,17 +21,54 @@ class MockedAppServer extends Server {
_close() {}

async _listen() {}

get(path: string, headers?: Record<string, any>) {
return this.receive('GET', path, headers, '')
}

post(path: string, body: any, headers?: Record<string, any>) {
return this.receive('POST', path, {
...headers,
'content-type': 'application/json',
}, JSON.stringify(body))
}

receive(method: string, path: string, headers: Record<string, any>, content: string) {
const socket = new Socket()
const req = new http.IncomingMessage(socket)
req.url = path
req.method = method
Object.assign(req.headers, headers)
req.headers['content-length'] = '' + content.length
return new Promise<MockedResponse>((resolve) => {
const res = new http.ServerResponse(req)
let body = ''
res.write = (chunk: any) => {
body += chunk
return true
}
res.end = (chunk: any) => {
res.write(chunk)
const code = res.statusCode
const headers = res.getHeaders()
resolve({ code, body, headers })
}
this.server.emit('request', req, res)
req.emit('data', content)
req.emit('end')
})
}
}

Server.types.mock = MockedAppServer
Server.types.mock = MockedServer

interface MockedAppOptions extends AppOptions {
mockStart?: boolean
mockDatabase?: boolean
}

export class MockedApp extends App {
public server: MockedAppServer
public server: MockedServer

constructor(options: MockedAppOptions = {}) {
super({ selfId: BASE_SELF_ID, type: 'mock', ...options })
Expand Down Expand Up @@ -90,7 +135,7 @@ export class TestSession {
if (length >= count) _resolve()
}
const dispose = this.app.on('middleware', (session) => {
if (session.$uuid === uuid) _resolve()
if (session.$uuid === uuid) process.nextTick(_resolve)
})
const uuid = this.app.receive({ ...this.meta, $send, message })
})
Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-utils",
"description": "Utilities for Koishi",
"version": "3.1.4",
"version": "3.1.5",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -33,7 +33,7 @@
],
"devDependencies": {
"@types/supports-color": "^5.3.0",
"koishi-test-utils": "^5.0.0"
"koishi-test-utils": "^5.0.1"
},
"dependencies": {
"supports-color": "^7.2.0"
Expand Down
6 changes: 6 additions & 0 deletions packages/koishi-utils/src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ export function coerce(val: any) {
const { stack } = val instanceof Error ? val : new Error(val as any)
return stack
}

export function makeArray<T>(source: T | T[]) {
return Array.isArray(source) ? source
: source === null || source === undefined ? []
: [source]
}
8 changes: 4 additions & 4 deletions packages/koishi/ecosystem.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"description": "Chess Plugin for Koishi"
},
"koishi-plugin-common": {
"version": "3.0.0-beta.16",
"version": "3.0.0",
"description": "Common plugins for Koishi"
},
"koishi-plugin-eval": {
Expand All @@ -20,7 +20,7 @@
"description": "Execute JavaScript in Koishi"
},
"koishi-plugin-github": {
"version": "2.0.2",
"version": "2.0.3",
"description": "GitHub webhook plugin for Koishi"
},
"koishi-plugin-image-search": {
Expand Down Expand Up @@ -51,11 +51,11 @@
"description": "Schedule plugin for Koishi"
},
"koishi-plugin-status": {
"version": "2.0.0-beta.14",
"version": "2.0.0-beta.15",
"description": "Show Status of Koishi"
},
"koishi-plugin-teach": {
"version": "1.0.3",
"version": "1.0.4",
"description": "Teach plugin for Koishi"
},
"koishi-plugin-tools": {
Expand Down
6 changes: 3 additions & 3 deletions packages/koishi/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi",
"description": "A QQ bot framework based on CQHTTP",
"version": "2.2.1",
"version": "2.2.2",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"engines": {
Expand Down Expand Up @@ -41,8 +41,8 @@
"cac": "^6.6.1",
"kleur": "^4.1.1",
"koishi-adapter-cqhttp": "^1.0.3",
"koishi-core": "^2.2.1",
"koishi-plugin-common": "^3.0.0-beta.16",
"koishi-core": "^2.2.2",
"koishi-plugin-common": "^3.0.0",
"prompts": "^2.3.2"
}
}
4 changes: 2 additions & 2 deletions packages/plugin-chess/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
"game"
],
"peerDependencies": {
"koishi-core": "^2.2.1",
"koishi-core": "^2.2.2",
"koishi-plugin-puppeteer": "^1.0.0"
},
"dependencies": {
"koishi-utils": "^3.1.4"
"koishi-utils": "^3.1.5"
}
}
8 changes: 4 additions & 4 deletions packages/plugin-common/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-common",
"description": "Common plugins for Koishi",
"version": "3.0.0-beta.16",
"version": "3.0.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -31,12 +31,12 @@
"plugin"
],
"peerDependencies": {
"koishi-core": "^2.2.1"
"koishi-core": "^2.2.2"
},
"devDependencies": {
"koishi-test-utils": "^5.0.0"
"koishi-test-utils": "^5.0.1"
},
"dependencies": {
"koishi-utils": "^3.1.4"
"koishi-utils": "^3.1.5"
}
}
Loading

0 comments on commit 1e1f0b3

Please sign in to comment.