Skip to content

Commit

Permalink
feat(teach): support asset transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 26, 2021
1 parent 8b6f0a4 commit b009502
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 154 deletions.
3 changes: 0 additions & 3 deletions packages/plugin-teach/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import update, { create } from './update'
import mongo from './database/mongo'
import mysql from './database/mysql'
import context from './plugins/context'
import image from './plugins/image'
import throttle from './plugins/throttle'
import probability from './plugins/probability'
import successor from './plugins/successor'
Expand All @@ -22,7 +21,6 @@ export * from './receiver'
export * from './search'
export * from './update'
export * from './plugins/context'
export * from './plugins/image'
export * from './plugins/throttle'
export * from './plugins/probability'
export * from './plugins/successor'
Expand Down Expand Up @@ -182,7 +180,6 @@ export function apply(ctx: Context, config: Config = {}) {
// options
ctx.plugin(internal, config)
ctx.plugin(context, config)
ctx.plugin(image, config)
ctx.plugin(throttle, config)
ctx.plugin(probability, config)
ctx.plugin(successor, config)
Expand Down
10 changes: 10 additions & 0 deletions packages/plugin-teach/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,14 @@ export default function apply(ctx: Context, config: Dialogue.Config) {
return template('teach.prohibited-command', command.name)
}
})

ctx.before('dialogue/modify', async ({ args }) => {
if (!args[1]) return
try {
args[1] = await ctx.transformAssets(args[1])
} catch (error) {
ctx.logger('teach').warn(error.message)
return '上传图片时发生错误。'
}
})
}
129 changes: 0 additions & 129 deletions packages/plugin-teach/src/plugins/image.ts

This file was deleted.

16 changes: 15 additions & 1 deletion packages/plugin-teach/tests/environment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Database, Context } from 'koishi-core'
import { Database, Context, Assets } from 'koishi-core'
import { defineProperty, Observed, clone, intersection } from 'koishi-utils'
import { Dialogue, DialogueTest, equal, Config, apply } from 'koishi-plugin-teach'
import { App } from 'koishi-test-utils'
Expand Down Expand Up @@ -106,13 +106,27 @@ function getProduct({ startTime, endTime }: Dialogue, time: number) {
return (startTime - time) * (time - endTime) * (endTime - startTime)
}

class MockAssets implements Assets {
types = ['image'] as const

async upload(url: string) {
return url
}

async stats() {
return {}
}
}

export default function (config: Config) {
const app = new App({
userCacheAge: Number.EPSILON,
nickname: ['koishi', 'satori'],
mockDatabase: true,
})

app.assets = new MockAssets()

const u2id = '200', u3id = '300', u4id = '400'
const g1id = '100', g2id = '200'
const u2 = app.session(u2id)
Expand Down
27 changes: 6 additions & 21 deletions packages/plugin-teach/tests/misc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { App } from 'koishi-test-utils'
import { Logger, Random, Time } from 'koishi-utils'
import { Logger, Time } from 'koishi-utils'
import createEnvironment from './environment'
import { expect } from 'chai'
import { install } from '@sinonjs/fake-timers'
import * as teach from 'koishi-plugin-teach'
import axios from 'axios'
import jest from 'jest-mock'

const DETAIL_HEAD = '编号为 1 的问答信息:\n问题:foo\n回答:bar\n'
Expand Down Expand Up @@ -42,37 +40,24 @@ describe('Teach Plugin - Miscellaneous', () => {
})
})

describe('Image (Client)', () => {
describe('Assets', () => {
const logger = new Logger('teach')
const axiosGet = jest.spyOn(axios, 'get')
const uploadKey = Random.uuid()
const imageServer = 'https://127.0.0.1/image'
const uploadServer = 'https://127.0.0.1/upload'
const { u3g1 } = createEnvironment({ uploadKey, uploadServer, imageServer })
const { app, u3g1 } = createEnvironment({})
const upload = jest.spyOn(app.assets, 'upload')

it('upload succeed', async () => {
axiosGet.mockReturnValue(Promise.resolve())
upload.mockResolvedValue('https://127.0.0.1/image/baz')
await u3g1.shouldReply('# foo [CQ:image,file=baz,url=bar]', '问答已添加,编号为 1。')
await u3g1.shouldReply('foo', '[CQ:image,url=https://127.0.0.1/image/baz]')
expect(axiosGet.mock.calls).to.have.shape([[uploadServer, {
params: { file: 'baz', url: 'bar' },
}]])
})

it('upload failed', async () => {
logger.level = Logger.ERROR
axiosGet.mockReturnValue(Promise.reject(new Error('failed')))
upload.mockRejectedValue('failed')
await u3g1.shouldReply('#1 fooo', '问答 1 已成功修改。')
await u3g1.shouldReply('#1 ~ [CQ:image,file=bar,url=baz]', '上传图片时发生错误。')
logger.level = Logger.WARN
})

it('get status', async () => {
axiosGet.mockReturnValue(Promise.resolve({
data: { totalSize: 10000000, totalCount: 10 },
}))
await u3g1.shouldReply('##', '共收录了 1 个问题和 1 个回答。\n收录图片 10 张,总体积 9.5 MB。')
})
})

describe('Rate Limit', () => {
Expand Down

0 comments on commit b009502

Please sign in to comment.