diff --git a/package.json b/package.json index 557e921..63f513c 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "_v": "1.2.6", + "_v": "1.2.7", "main": "./built/index.js", "scripts": { "start": "node ./built", @@ -38,9 +38,16 @@ "ws": "7.3.1" }, "devDependencies": { + "@koa/router": "9.4.0", "@types/jest": "26.0.14", + "@types/koa": "2.11.4", + "@types/koa__router": "8.0.2", + "@types/websocket": "1.0.1", "jest": "26.4.2", - "ts-jest": "26.3.0" + "koa": "2.13.0", + "koa-json-body": "5.3.0", + "ts-jest": "26.3.0", + "websocket": "1.0.32" }, "_moduleAliases": { "@": "built" diff --git a/src/modules/poll/index.ts b/src/modules/poll/index.ts index ba45d6c..da06f32 100644 --- a/src/modules/poll/index.ts +++ b/src/modules/poll/index.ts @@ -61,6 +61,7 @@ export default class extends Module { ['絵文字になってほしいもの', '絵文字になってほしいものはどれですか?'], ['Misskey本部にありそうなもの', 'みなさんは、Misskey本部にありそうなものはどれだと思いますか?'], ['燃えるゴミ', 'みなさんは、どれが燃えるゴミだと思いますか?'], + ['好きなおにぎりの具', 'みなさんの好きなおにぎりの具はなんですか?'], ['そして輝くウルトラ', 'みなさんは、そして輝くウルトラ…?'], ]; diff --git a/src/vocabulary.ts b/src/vocabulary.ts index c90b634..d37c36e 100644 --- a/src/vocabulary.ts +++ b/src/vocabulary.ts @@ -90,6 +90,7 @@ export const itemPrefixes = [ 'シュレディンガーの', '分散型', '卵かけ', + '次世代', ]; export const items = [ diff --git a/test/_mocks_/misskey.ts b/test/_mocks_/misskey.ts new file mode 100644 index 0000000..de1212f --- /dev/null +++ b/test/_mocks_/misskey.ts @@ -0,0 +1,67 @@ +import * as http from 'http'; +import * as Koa from 'koa'; +import * as websocket from 'websocket'; + +export class Misskey { + private server: http.Server; + private streaming: websocket.connection; + + constructor() { + const app = new Koa(); + + this.server = http.createServer(app.callback()); + + const ws = new websocket.server({ + httpServer: this.server + }); + + ws.on('request', async (request) => { + const q = request.resourceURL.query as ParsedUrlQuery; + + this.streaming = request.accept(); + }); + + this.server.listen(3000); + } + + public waitForStreamingMessage(handler) { + return new Promise((resolve, reject) => { + const onMessage = (data: websocket.IMessage) => { + if (data.utf8Data == null) return; + const message = JSON.parse(data.utf8Data); + const result = handler(message); + if (result) { + this.streaming.off('message', onMessage); + resolve(); + } + }; + this.streaming.on('message', onMessage); + }); + } + + public async waitForMainChannelConnected() { + await this.waitForStreamingMessage(message => { + const { type, body } = message; + if (type === 'connect') { + const { channel, id, params, pong } = body; + + if (channel !== 'main') return; + + if (pong) { + this.sendStreamingMessage('connected', { + id: id + }); + } + + return true; + } + }); + } + + public sendStreamingMessage(type: string, payload: any) { + this.streaming.send(JSON.stringify({ + type: type, + body: payload + })); + } +}