From 6e0d5d9efb906af6f8dbd2f4cbbc3d6667c121c0 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 19 Sep 2020 10:40:44 +0900 Subject: [PATCH] wip --- package.json | 99 +++++++++++++++++++----------- src/ai.ts | 6 +- src/index.ts | 2 + src/misskey/user.ts | 4 +- src/modules/birthday/index.ts | 4 +- src/modules/chart/index.ts | 6 +- src/modules/core/index.ts | 6 +- src/modules/dice/index.ts | 6 +- src/modules/emoji-react/index.ts | 2 +- src/modules/emoji/index.ts | 6 +- src/modules/follow/index.ts | 4 +- src/modules/fortune/index.ts | 6 +- src/modules/guessing-game/index.ts | 6 +- src/modules/kazutori/index.ts | 23 ++++--- src/modules/keyword/index.ts | 4 +- src/modules/maze/index.ts | 6 +- src/modules/noting/index.ts | 4 +- src/modules/ping/index.ts | 4 +- src/modules/poll/index.ts | 6 +- src/modules/reversi/back.ts | 2 +- src/modules/reversi/index.ts | 6 +- src/modules/server/index.ts | 4 +- src/modules/sleep-report/index.ts | 4 +- src/modules/talk/index.ts | 4 +- src/modules/timer/index.ts | 6 +- src/modules/valentine/index.ts | 4 +- src/modules/welcome/index.ts | 2 +- test/_mocks_/account.ts | 7 +++ test/_mocks_/ws.ts | 17 +++++ test/_modules_/test.ts | 26 ++++++++ test/core.ts | 20 ++++++ test/tsconfig.json | 15 +++++ tsconfig.json | 8 ++- 33 files changed, 225 insertions(+), 104 deletions(-) create mode 100644 test/_mocks_/account.ts create mode 100644 test/_mocks_/ws.ts create mode 100644 test/_modules_/test.ts create mode 100644 test/core.ts create mode 100644 test/tsconfig.json diff --git a/package.json b/package.json index 677c489..557e921 100644 --- a/package.json +++ b/package.json @@ -1,38 +1,67 @@ { "_v": "1.2.6", - "main": "./built/index.js", - "scripts": { - "start": "node ./built", - "build": "tsc" - }, - "dependencies": { - "@types/chalk": "2.2.0", - "@types/lokijs": "1.5.3", - "@types/node": "14.6.0", - "@types/promise-retry": "1.1.3", - "@types/random-seed": "0.3.3", - "@types/request-promise-native": "1.0.17", - "@types/seedrandom": "2.4.28", - "@types/twemoji-parser": "12.1.0", - "@types/uuid": "8.3.0", - "@types/ws": "7.2.6", - "autobind-decorator": "2.4.0", - "canvas": "2.6.1", - "chalk": "4.1.0", - "lokijs": "1.5.11", - "memory-streams": "0.1.3", - "misskey-reversi": "0.0.5", - "promise-retry": "2.0.1", - "random-seed": "0.3.0", - "reconnecting-websocket": "4.4.0", - "request": "2.88.2", - "request-promise-native": "1.0.9", - "seedrandom": "3.0.5", - "timeout-as-promise": "1.0.0", - "ts-node": "9.0.0", - "twemoji-parser": "13.0.0", - "typescript": "4.0.2", - "uuid": "8.3.0", - "ws": "7.3.1" - } + "main": "./built/index.js", + "scripts": { + "start": "node ./built", + "build": "tsc", + "test": "jest" + }, + "dependencies": { + "@types/chalk": "2.2.0", + "@types/lokijs": "1.5.3", + "@types/node": "14.6.0", + "@types/promise-retry": "1.1.3", + "@types/random-seed": "0.3.3", + "@types/request-promise-native": "1.0.17", + "@types/seedrandom": "2.4.28", + "@types/twemoji-parser": "12.1.0", + "@types/uuid": "8.3.0", + "@types/ws": "7.2.6", + "autobind-decorator": "2.4.0", + "canvas": "2.6.1", + "chalk": "4.1.0", + "lokijs": "1.5.11", + "memory-streams": "0.1.3", + "misskey-reversi": "0.0.5", + "module-alias": "2.2.2", + "promise-retry": "2.0.1", + "random-seed": "0.3.0", + "reconnecting-websocket": "4.4.0", + "request": "2.88.2", + "request-promise-native": "1.0.9", + "seedrandom": "3.0.5", + "timeout-as-promise": "1.0.0", + "ts-node": "9.0.0", + "twemoji-parser": "13.0.0", + "typescript": "4.0.2", + "uuid": "8.3.0", + "ws": "7.3.1" + }, + "devDependencies": { + "@types/jest": "26.0.14", + "jest": "26.4.2", + "ts-jest": "26.3.0" + }, + "_moduleAliases": { + "@": "built" + }, + "jest": { + "testRegex": "/test/.*", + "moduleFileExtensions": [ + "ts", + "js" + ], + "transform": { + "^.+\\.ts$": "ts-jest" + }, + "globals": { + "ts-jest": { + "tsConfig": "test/tsconfig.json" + } + }, + "moduleNameMapper": { + "^@/(.+)": "/src/$1", + "^#/(.+)": "/test/$1" + } + } } diff --git a/src/ai.ts b/src/ai.ts index 9982655..8468585 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -80,9 +80,11 @@ export default class 藍 { this.account = account; this.modules = modules; - this.log('Lodaing the memory...'); + const file = process.env.NODE_ENV === 'test' ? 'test.memory.json' : 'memory.json'; - this.db = new loki('memory.json', { + this.log(`Lodaing the memory from ${file}...`); + + this.db = new loki(file, { autoload: true, autosave: true, autosaveInterval: 1000, diff --git a/src/index.ts b/src/index.ts index e2735cd..9f5510d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,7 @@ // AiOS bootstrapper +import 'module-alias/register'; + import * as chalk from 'chalk'; import * as request from 'request-promise-native'; const promiseRetry = require('promise-retry'); diff --git a/src/misskey/user.ts b/src/misskey/user.ts index c992643..6c8fc3f 100644 --- a/src/misskey/user.ts +++ b/src/misskey/user.ts @@ -2,7 +2,7 @@ export type User = { id: string; name: string; username: string; - host: string; - isFollowing: boolean; + host?: string | null; + isFollowing?: boolean; isBot: boolean; }; diff --git a/src/modules/birthday/index.ts b/src/modules/birthday/index.ts index 1680578..7971623 100644 --- a/src/modules/birthday/index.ts +++ b/src/modules/birthday/index.ts @@ -1,7 +1,7 @@ import autobind from 'autobind-decorator'; -import Module from '../../module'; +import Module from '@/module'; import Friend from '../../friend'; -import serifs from '../../serifs'; +import serifs from '@/serifs'; function zeroPadding(num: number, length: number): string { return ('0000000000' + num).slice(-length); diff --git a/src/modules/chart/index.ts b/src/modules/chart/index.ts index a0a0409..ac661d6 100644 --- a/src/modules/chart/index.ts +++ b/src/modules/chart/index.ts @@ -1,7 +1,7 @@ import autobind from 'autobind-decorator'; -import Module from '../../module'; -import serifs from '../../serifs'; -import Message from '../../message'; +import Module from '@/module'; +import serifs from '@/serifs'; +import Message from '@/message'; import { renderChart } from './render-chart'; import { items } from '../../vocabulary'; import config from '../../config'; diff --git a/src/modules/core/index.ts b/src/modules/core/index.ts index 10cf836..2daeca3 100644 --- a/src/modules/core/index.ts +++ b/src/modules/core/index.ts @@ -1,7 +1,7 @@ import autobind from 'autobind-decorator'; -import Module from '../../module'; -import Message from '../../message'; -import serifs from '../../serifs'; +import Module from '@/module'; +import Message from '@/message'; +import serifs from '@/serifs'; import { safeForInterpolate } from '../../utils/safe-for-interpolate'; const titles = ['さん', 'くん', '君', 'ちゃん', '様', '先生']; diff --git a/src/modules/dice/index.ts b/src/modules/dice/index.ts index e46e42b..646ac01 100644 --- a/src/modules/dice/index.ts +++ b/src/modules/dice/index.ts @@ -1,7 +1,7 @@ import autobind from 'autobind-decorator'; -import Module from '../../module'; -import Message from '../../message'; -import serifs from '../../serifs'; +import Module from '@/module'; +import Message from '@/message'; +import serifs from '@/serifs'; export default class extends Module { public readonly name = 'dice'; diff --git a/src/modules/emoji-react/index.ts b/src/modules/emoji-react/index.ts index c06ba91..27728ab 100644 --- a/src/modules/emoji-react/index.ts +++ b/src/modules/emoji-react/index.ts @@ -2,7 +2,7 @@ import autobind from 'autobind-decorator'; import { parse } from 'twemoji-parser'; import { Note } from '../../misskey/note'; -import Module from '../../module'; +import Module from '@/module'; import Stream from '../../stream'; import includes from '../../utils/includes'; diff --git a/src/modules/emoji/index.ts b/src/modules/emoji/index.ts index b56be5c..3dbd5b8 100644 --- a/src/modules/emoji/index.ts +++ b/src/modules/emoji/index.ts @@ -1,7 +1,7 @@ import autobind from 'autobind-decorator'; -import Module from '../../module'; -import Message from '../../message'; -import serifs from '../../serifs'; +import Module from '@/module'; +import Message from '@/message'; +import serifs from '@/serifs'; const hands = [ '👏', diff --git a/src/modules/follow/index.ts b/src/modules/follow/index.ts index b6a1b26..0f52047 100644 --- a/src/modules/follow/index.ts +++ b/src/modules/follow/index.ts @@ -1,6 +1,6 @@ import autobind from 'autobind-decorator'; -import Module from '../../module'; -import Message from '../../message'; +import Module from '@/module'; +import Message from '@/message'; export default class extends Module { public readonly name = 'follow'; diff --git a/src/modules/fortune/index.ts b/src/modules/fortune/index.ts index 8e39667..d0bfa1f 100644 --- a/src/modules/fortune/index.ts +++ b/src/modules/fortune/index.ts @@ -1,7 +1,7 @@ import autobind from 'autobind-decorator'; -import Module from '../../module'; -import Message from '../../message'; -import serifs from '../../serifs'; +import Module from '@/module'; +import Message from '@/message'; +import serifs from '@/serifs'; import * as seedrandom from 'seedrandom'; import { genItem } from '../../vocabulary'; diff --git a/src/modules/guessing-game/index.ts b/src/modules/guessing-game/index.ts index 77c5c2f..6063f0d 100644 --- a/src/modules/guessing-game/index.ts +++ b/src/modules/guessing-game/index.ts @@ -1,8 +1,8 @@ import autobind from 'autobind-decorator'; import * as loki from 'lokijs'; -import Module from '../../module'; -import Message from '../../message'; -import serifs from '../../serifs'; +import Module from '@/module'; +import Message from '@/message'; +import serifs from '@/serifs'; export default class extends Module { public readonly name = 'guessingGame'; diff --git a/src/modules/kazutori/index.ts b/src/modules/kazutori/index.ts index 84119e9..c41504e 100644 --- a/src/modules/kazutori/index.ts +++ b/src/modules/kazutori/index.ts @@ -1,18 +1,17 @@ import autobind from 'autobind-decorator'; import * as loki from 'lokijs'; -import Module from '../../module'; -import Message from '../../message'; -import serifs from '../../serifs'; - -type User = { - id: string; - username: string; - host: string; -}; +import Module from '@/module'; +import Message from '@/message'; +import serifs from '@/serifs'; +import { User } from '@/misskey/user'; type Game = { votes: { - user: User; + user: { + id: string; + username: string; + host: User['host']; + }; number: number; }[]; isEnded: boolean; @@ -173,14 +172,14 @@ export default class extends Module { return; } - function acct(user: User): string { + function acct(user: Game['votes'][0]['user']): string { return user.host ? `@${user.username}@${user.host}` : `@${user.username}`; } let results: string[] = []; - let winner: User | null = null; + let winner: Game['votes'][0]['user'] | null = null; for (let i = 100; i >= 0; i--) { const users = game.votes diff --git a/src/modules/keyword/index.ts b/src/modules/keyword/index.ts index 43b302d..6f5d572 100644 --- a/src/modules/keyword/index.ts +++ b/src/modules/keyword/index.ts @@ -1,8 +1,8 @@ import autobind from 'autobind-decorator'; import * as loki from 'lokijs'; -import Module from '../../module'; +import Module from '@/module'; import config from '../../config'; -import serifs from '../../serifs'; +import serifs from '@/serifs'; import { mecab } from './mecab'; function kanaToHira(str: string) { diff --git a/src/modules/maze/index.ts b/src/modules/maze/index.ts index e3a1f94..8311e94 100644 --- a/src/modules/maze/index.ts +++ b/src/modules/maze/index.ts @@ -1,9 +1,9 @@ import autobind from 'autobind-decorator'; -import Module from '../../module'; -import serifs from '../../serifs'; +import Module from '@/module'; +import serifs from '@/serifs'; import { genMaze } from './gen-maze'; import { renderMaze } from './render-maze'; -import Message from '../../message'; +import Message from '@/message'; export default class extends Module { public readonly name = 'maze'; diff --git a/src/modules/noting/index.ts b/src/modules/noting/index.ts index c30506c..9b6bcd1 100644 --- a/src/modules/noting/index.ts +++ b/src/modules/noting/index.ts @@ -1,6 +1,6 @@ import autobind from 'autobind-decorator'; -import Module from '../../module'; -import serifs from '../../serifs'; +import Module from '@/module'; +import serifs from '@/serifs'; import { genItem } from '../../vocabulary'; export default class extends Module { diff --git a/src/modules/ping/index.ts b/src/modules/ping/index.ts index c7c4455..147a7d5 100644 --- a/src/modules/ping/index.ts +++ b/src/modules/ping/index.ts @@ -1,6 +1,6 @@ import autobind from 'autobind-decorator'; -import Module from '../../module'; -import Message from '../../message'; +import Module from '@/module'; +import Message from '@/message'; export default class extends Module { public readonly name = 'ping'; diff --git a/src/modules/poll/index.ts b/src/modules/poll/index.ts index bb75c41..037d507 100644 --- a/src/modules/poll/index.ts +++ b/src/modules/poll/index.ts @@ -1,7 +1,7 @@ import autobind from 'autobind-decorator'; -import Message from '../../message'; -import Module from '../../module'; -import serifs from '../../serifs'; +import Message from '@/message'; +import Module from '@/module'; +import serifs from '@/serifs'; import { genItem } from '../../vocabulary'; import config from '../../config'; import { Note } from '../../misskey/note'; diff --git a/src/modules/reversi/back.ts b/src/modules/reversi/back.ts index fed3a9a..0fd4f3b 100644 --- a/src/modules/reversi/back.ts +++ b/src/modules/reversi/back.ts @@ -9,7 +9,7 @@ import * as request from 'request-promise-native'; import Reversi, { Color } from 'misskey-reversi'; import config from '../../config'; -import serifs from '../../serifs'; +import serifs from '@/serifs'; import { User } from '../../misskey/user'; const db = {}; diff --git a/src/modules/reversi/index.ts b/src/modules/reversi/index.ts index d34e073..f47b285 100644 --- a/src/modules/reversi/index.ts +++ b/src/modules/reversi/index.ts @@ -1,9 +1,9 @@ import * as childProcess from 'child_process'; import autobind from 'autobind-decorator'; -import Module from '../../module'; -import serifs from '../../serifs'; +import Module from '@/module'; +import serifs from '@/serifs'; import config from '../../config'; -import Message from '../../message'; +import Message from '@/message'; import Friend from '../../friend'; import getDate from '../../utils/get-date'; diff --git a/src/modules/server/index.ts b/src/modules/server/index.ts index b9e7ebd..5cfdbac 100644 --- a/src/modules/server/index.ts +++ b/src/modules/server/index.ts @@ -1,6 +1,6 @@ import autobind from 'autobind-decorator'; -import Module from '../../module'; -import serifs from '../../serifs'; +import Module from '@/module'; +import serifs from '@/serifs'; import config from '../../config'; export default class extends Module { diff --git a/src/modules/sleep-report/index.ts b/src/modules/sleep-report/index.ts index bda02cc..50784f5 100644 --- a/src/modules/sleep-report/index.ts +++ b/src/modules/sleep-report/index.ts @@ -1,6 +1,6 @@ import autobind from 'autobind-decorator'; -import Module from '../../module'; -import serifs from '../../serifs'; +import Module from '@/module'; +import serifs from '@/serifs'; export default class extends Module { public readonly name = 'sleepReport'; diff --git a/src/modules/talk/index.ts b/src/modules/talk/index.ts index fb0b972..e2a6d8e 100644 --- a/src/modules/talk/index.ts +++ b/src/modules/talk/index.ts @@ -1,7 +1,7 @@ import autobind from 'autobind-decorator'; import { HandlerResult } from '../../ai'; -import Module from '../../module'; -import Message from '../../message'; +import Module from '@/module'; +import Message from '@/message'; import serifs, { getSerif } from '../../serifs'; import getDate from '../../utils/get-date'; diff --git a/src/modules/timer/index.ts b/src/modules/timer/index.ts index 280abf1..9029a8d 100644 --- a/src/modules/timer/index.ts +++ b/src/modules/timer/index.ts @@ -1,7 +1,7 @@ import autobind from 'autobind-decorator'; -import Module from '../../module'; -import Message from '../../message'; -import serifs from '../../serifs'; +import Module from '@/module'; +import Message from '@/message'; +import serifs from '@/serifs'; export default class extends Module { public readonly name = 'timer'; diff --git a/src/modules/valentine/index.ts b/src/modules/valentine/index.ts index 9bf5568..0a350ac 100644 --- a/src/modules/valentine/index.ts +++ b/src/modules/valentine/index.ts @@ -1,7 +1,7 @@ import autobind from 'autobind-decorator'; -import Module from '../../module'; +import Module from '@/module'; import Friend from '../../friend'; -import serifs from '../../serifs'; +import serifs from '@/serifs'; export default class extends Module { public readonly name = 'valentine'; diff --git a/src/modules/welcome/index.ts b/src/modules/welcome/index.ts index baf6e37..33bad7c 100644 --- a/src/modules/welcome/index.ts +++ b/src/modules/welcome/index.ts @@ -1,5 +1,5 @@ import autobind from 'autobind-decorator'; -import Module from '../../module'; +import Module from '@/module'; export default class extends Module { public readonly name = 'welcome'; diff --git a/test/_mocks_/account.ts b/test/_mocks_/account.ts new file mode 100644 index 0000000..3d53135 --- /dev/null +++ b/test/_mocks_/account.ts @@ -0,0 +1,7 @@ +export const account = { + id: '0', + name: '藍', + username: 'ai', + host: null, + isBot: true, +}; diff --git a/test/_mocks_/ws.ts b/test/_mocks_/ws.ts new file mode 100644 index 0000000..5b31e2c --- /dev/null +++ b/test/_mocks_/ws.ts @@ -0,0 +1,17 @@ +import * as websocket from 'websocket'; + +export class StreamingApi { + private ws: WS; + + constructor() { + this.ws = new WS('ws://localhost/streaming'); + } + + public async waitForMainChannelConnected() { + await expect(this.ws).toReceiveMessage("hello"); + } + + public send(message) { + this.ws.send(JSON.stringify(message)); + } +} diff --git a/test/_modules_/test.ts b/test/_modules_/test.ts new file mode 100644 index 0000000..56ef18e --- /dev/null +++ b/test/_modules_/test.ts @@ -0,0 +1,26 @@ +import autobind from 'autobind-decorator'; +import Module from '@/module'; +import Message from '@/message'; + +export default class extends Module { + public readonly name = 'test'; + + @autobind + public install() { + return { + mentionHook: this.mentionHook + }; + } + + @autobind + private async mentionHook(msg: Message) { + if (msg.text && msg.text.includes('ping')) { + msg.reply('PONG!', { + immediate: true + }); + return true; + } else { + return false; + } + } +} diff --git a/test/core.ts b/test/core.ts new file mode 100644 index 0000000..e46313a --- /dev/null +++ b/test/core.ts @@ -0,0 +1,20 @@ +import 藍 from '@/ai'; +import { account } from '#/_mocks_/account'; +import TestModule from '#/_modules_/test'; +import { StreamingApi } from '#/_mocks_/ws'; + +process.env.NODE_ENV = 'test'; + +let ai: 藍; + +beforeEach(() => { + ai = new 藍(account, [ + new TestModule(), + ]); +}); + +test('mention hook', async () => { + const streaming = new StreamingApi(); + + +}); diff --git a/test/tsconfig.json b/test/tsconfig.json new file mode 100644 index 0000000..87add8f --- /dev/null +++ b/test/tsconfig.json @@ -0,0 +1,15 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "baseUrl": ".", + "rootDir": "../", + "paths": { + "@/*": ["../src/*"], + "#/*": ["./*"] + }, + }, + "compileOnSave": false, + "include": [ + "**/*.ts" + ] +} diff --git a/tsconfig.json b/tsconfig.json index e0a70d6..f20004c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,12 +8,16 @@ "strictNullChecks": true, "experimentalDecorators": true, "sourceMap": false, - "target": "es2017", + "target": "es2020", "module": "commonjs", "removeComments": false, "noLib": false, "outDir": "built", - "rootDir": "src" + "rootDir": "src", + "baseUrl": ".", + "paths": { + "@/*": ["src/*"] + }, }, "compileOnSave": false, "include": [