Skip to content

Commit

Permalink
refa: migrate mock and puppeteer plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jul 10, 2022
1 parent ef76432 commit ba70ac8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 71 deletions.
88 changes: 35 additions & 53 deletions plugins/mock/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Adapter, App, Bot, Channel, Context, Schema, Session, User } from 'koishi'
import { Adapter, Bot, Channel, Context, Session, User } from 'koishi'
import { MessageClient } from './client'
import { Webhook } from './webhook'

declare module 'koishi' {
namespace Context {
interface Services {
mock: MockAdapter
}
interface Context {
mock: MockAdapter
}

interface User {
Expand All @@ -16,15 +14,28 @@ declare module 'koishi' {

export const DEFAULT_SELF_ID = '514'

interface BotConfig extends Bot.BaseConfig {
selfId: string
export namespace MockBot {
export interface Config extends Bot.Config {
selfId: string
}
}

export class MockBot extends Bot<BotConfig> {
constructor(adapter: MockAdapter, config: BotConfig) {
super(adapter, config)
this.selfId = config.selfId
export class MockBot extends Bot {
constructor(ctx: Context, config: MockBot.Config) {
super(ctx, config)
this.selfId = config.selfId ?? DEFAULT_SELF_ID
this.status = 'online'
ctx.plugin(MockAdapter, this)
}

client(userId: string, channelId?: string) {
return new MessageClient(this, userId, channelId)
}

receive(meta: Partial<Session>) {
const session = this.session(meta)
this.dispatch(session)
return session.id
}

async getMessage(channelId: string, messageId: string) {
Expand All @@ -35,39 +46,24 @@ export class MockBot extends Bot<BotConfig> {
time: 0,
subtype: null,
messageType: null,
author: { userId: this.selfId } as Bot.Author,
author: { userId: this.selfId },
}
}
}

export class MockAdapter extends Adapter<BotConfig> {
public app: App
MockBot.prototype.platform = 'mock'

export class MockAdapter extends Adapter.Server<MockBot> {
public app: Context
public webhook: Webhook
public platform = 'mock'

constructor(ctx: Context, config: MockAdapter.Config) {
super(ctx, config)
this.app = ctx.app
this.webhook = new Webhook(ctx.app)
constructor(ctx: Context, bot: MockBot) {
super()
this.app = ctx.root
this.webhook = new Webhook(ctx.root)
ctx.mock = this
ctx.bots.adapters.mock = this

for (const selfId of config.selfIds) {
this.addBot(selfId)
}
}

addBot(selfId = DEFAULT_SELF_ID) {
const bot = this.bots.find(bot => bot.selfId === selfId)
if (bot) return bot

return this.ctx.bots.create('mock', { selfId }, MockBot)
}

async stop() {}

async start() {}

async initUser(id: string, authority = 1, data?: Partial<User>) {
await this.app.database.create('user', { mock: id, authority, ...data })
}
Expand All @@ -77,34 +73,20 @@ export class MockAdapter extends Adapter<BotConfig> {
}

client(userId: string, channelId?: string) {
return new MessageClient(this, userId, channelId)
return new MessageClient(this.bots[0], userId, channelId)
}

session(meta: Partial<Session>) {
const bot = this.bots[0]
return new Session(bot, {
selfId: bot.selfId,
platform: bot.platform,
timestamp: Date.now(),
...meta,
})
return this.bots[0].session(meta)
}

receive(meta: Partial<Session>) {
const session = this.session(meta)
this.dispatch(session)
return session.id
return this.bots[0].receive(meta)
}
}

export namespace MockAdapter {
export interface Config {
selfIds?: string[]
}

export const Config = Schema.object({
selfIds: Schema.array(String).default([DEFAULT_SELF_ID]),
})
export interface Config {}
}

Context.service('mock')
16 changes: 8 additions & 8 deletions plugins/mock/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'assert'
import { App, Session } from 'koishi'
import { Context, Session } from 'koishi'
import { format } from 'util'
import { MockAdapter } from './adapter'
import { MockBot } from './adapter'

const RECEIVED_UNEXPECTED = 'expected "%s" to be not replied but received "%s"'
const RECEIVED_NOTHING = 'expected "%s" to be replied but received nothing'
Expand All @@ -10,17 +10,17 @@ const RECEIVED_NTH_NOTHING = 'expected "%s" to be replied at index %s but receiv
const RECEIVED_NTH_OTHERWISE = 'expected "%s" to be replied with %s at index %s but received "%s"'

export class MessageClient {
public app: App
public app: Context
public meta: Session.Payload

private replies: string[] = []

constructor(public mock: MockAdapter, public userId: string, public channelId?: string) {
this.app = mock.ctx.app
constructor(public bot: MockBot, public userId: string, public channelId?: string) {
this.app = bot.ctx.app
this.meta = {
platform: 'mock',
type: 'message',
selfId: mock.bots[0].selfId,
selfId: bot.selfId,
userId,
author: {
userId,
Expand Down Expand Up @@ -50,15 +50,15 @@ export class MessageClient {
}
const send = async (content: string) => {
if (!content) return
const session = await this.app.bots[0].session({ ...this.meta, content })
const session = this.app.bots[0].session({ ...this.meta, content })
if (!session?.content) return []
this.replies.push(session.content)
return []
}
const dispose = this.app.on('middleware', (session) => {
if (session.id === uuid) process.nextTick(_resolve)
})
const uuid = this.mock.receive({ ...this.meta, send, content })
const uuid = this.bot.receive({ ...this.meta, send, content })
})
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/mock/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MockAdapter } from './adapter'
import { MockBot } from './adapter'

export * from './adapter'
export * from './client'
export * from './webhook'

export default MockAdapter
export default MockBot
6 changes: 3 additions & 3 deletions plugins/mock/src/webhook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Dict } from 'koishi'
import { Context, Dict } from 'koishi'
import { Socket } from 'net'
import { IncomingMessage, ServerResponse } from 'http'

Expand All @@ -11,7 +11,7 @@ export namespace Webhook {
}

export class Webhook {
constructor(public app: App) {}
constructor(public app: Context) {}

async head(path: string, headers?: Dict<any>) {
const res = await this.receive('HEAD', path, headers, '')
Expand Down Expand Up @@ -71,7 +71,7 @@ export class Webhook {
if (typeof callback === 'function') callback()
return res
}
this.app._httpServer.emit('request', req, res)
this.app.router._http.emit('request', req, res)
req.emit('data', body)
req.emit('end')
})
Expand Down
8 changes: 3 additions & 5 deletions plugins/puppeteer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import * as screenshot from './screenshot'
export * from './svg'

declare module 'koishi' {
namespace Context {
interface Services {
puppeteer: Puppeteer
}
interface Context {
puppeteer: Puppeteer
}

interface EventMap {
interface Events {
'puppeteer/validate'(url: string): string
}
}
Expand Down

0 comments on commit ba70ac8

Please sign in to comment.