This commit is contained in:
syuilo 2020-09-19 10:40:44 +09:00
parent 725305a2df
commit 6e0d5d9efb
33 changed files with 225 additions and 104 deletions

View file

@ -1,38 +1,67 @@
{ {
"_v": "1.2.6", "_v": "1.2.6",
"main": "./built/index.js", "main": "./built/index.js",
"scripts": { "scripts": {
"start": "node ./built", "start": "node ./built",
"build": "tsc" "build": "tsc",
}, "test": "jest"
"dependencies": { },
"@types/chalk": "2.2.0", "dependencies": {
"@types/lokijs": "1.5.3", "@types/chalk": "2.2.0",
"@types/node": "14.6.0", "@types/lokijs": "1.5.3",
"@types/promise-retry": "1.1.3", "@types/node": "14.6.0",
"@types/random-seed": "0.3.3", "@types/promise-retry": "1.1.3",
"@types/request-promise-native": "1.0.17", "@types/random-seed": "0.3.3",
"@types/seedrandom": "2.4.28", "@types/request-promise-native": "1.0.17",
"@types/twemoji-parser": "12.1.0", "@types/seedrandom": "2.4.28",
"@types/uuid": "8.3.0", "@types/twemoji-parser": "12.1.0",
"@types/ws": "7.2.6", "@types/uuid": "8.3.0",
"autobind-decorator": "2.4.0", "@types/ws": "7.2.6",
"canvas": "2.6.1", "autobind-decorator": "2.4.0",
"chalk": "4.1.0", "canvas": "2.6.1",
"lokijs": "1.5.11", "chalk": "4.1.0",
"memory-streams": "0.1.3", "lokijs": "1.5.11",
"misskey-reversi": "0.0.5", "memory-streams": "0.1.3",
"promise-retry": "2.0.1", "misskey-reversi": "0.0.5",
"random-seed": "0.3.0", "module-alias": "2.2.2",
"reconnecting-websocket": "4.4.0", "promise-retry": "2.0.1",
"request": "2.88.2", "random-seed": "0.3.0",
"request-promise-native": "1.0.9", "reconnecting-websocket": "4.4.0",
"seedrandom": "3.0.5", "request": "2.88.2",
"timeout-as-promise": "1.0.0", "request-promise-native": "1.0.9",
"ts-node": "9.0.0", "seedrandom": "3.0.5",
"twemoji-parser": "13.0.0", "timeout-as-promise": "1.0.0",
"typescript": "4.0.2", "ts-node": "9.0.0",
"uuid": "8.3.0", "twemoji-parser": "13.0.0",
"ws": "7.3.1" "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": {
"^@/(.+)": "<rootDir>/src/$1",
"^#/(.+)": "<rootDir>/test/$1"
}
}
} }

View file

@ -80,9 +80,11 @@ export default class 藍 {
this.account = account; this.account = account;
this.modules = modules; 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, autoload: true,
autosave: true, autosave: true,
autosaveInterval: 1000, autosaveInterval: 1000,

View file

@ -1,5 +1,7 @@
// AiOS bootstrapper // AiOS bootstrapper
import 'module-alias/register';
import * as chalk from 'chalk'; import * as chalk from 'chalk';
import * as request from 'request-promise-native'; import * as request from 'request-promise-native';
const promiseRetry = require('promise-retry'); const promiseRetry = require('promise-retry');

View file

@ -2,7 +2,7 @@ export type User = {
id: string; id: string;
name: string; name: string;
username: string; username: string;
host: string; host?: string | null;
isFollowing: boolean; isFollowing?: boolean;
isBot: boolean; isBot: boolean;
}; };

View file

@ -1,7 +1,7 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Module from '../../module'; import Module from '@/module';
import Friend from '../../friend'; import Friend from '../../friend';
import serifs from '../../serifs'; import serifs from '@/serifs';
function zeroPadding(num: number, length: number): string { function zeroPadding(num: number, length: number): string {
return ('0000000000' + num).slice(-length); return ('0000000000' + num).slice(-length);

View file

@ -1,7 +1,7 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Module from '../../module'; import Module from '@/module';
import serifs from '../../serifs'; import serifs from '@/serifs';
import Message from '../../message'; import Message from '@/message';
import { renderChart } from './render-chart'; import { renderChart } from './render-chart';
import { items } from '../../vocabulary'; import { items } from '../../vocabulary';
import config from '../../config'; import config from '../../config';

View file

@ -1,7 +1,7 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Module from '../../module'; import Module from '@/module';
import Message from '../../message'; import Message from '@/message';
import serifs from '../../serifs'; import serifs from '@/serifs';
import { safeForInterpolate } from '../../utils/safe-for-interpolate'; import { safeForInterpolate } from '../../utils/safe-for-interpolate';
const titles = ['さん', 'くん', '君', 'ちゃん', '様', '先生']; const titles = ['さん', 'くん', '君', 'ちゃん', '様', '先生'];

View file

@ -1,7 +1,7 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Module from '../../module'; import Module from '@/module';
import Message from '../../message'; import Message from '@/message';
import serifs from '../../serifs'; import serifs from '@/serifs';
export default class extends Module { export default class extends Module {
public readonly name = 'dice'; public readonly name = 'dice';

View file

@ -2,7 +2,7 @@ import autobind from 'autobind-decorator';
import { parse } from 'twemoji-parser'; import { parse } from 'twemoji-parser';
import { Note } from '../../misskey/note'; import { Note } from '../../misskey/note';
import Module from '../../module'; import Module from '@/module';
import Stream from '../../stream'; import Stream from '../../stream';
import includes from '../../utils/includes'; import includes from '../../utils/includes';

View file

@ -1,7 +1,7 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Module from '../../module'; import Module from '@/module';
import Message from '../../message'; import Message from '@/message';
import serifs from '../../serifs'; import serifs from '@/serifs';
const hands = [ const hands = [
'👏', '👏',

View file

@ -1,6 +1,6 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Module from '../../module'; import Module from '@/module';
import Message from '../../message'; import Message from '@/message';
export default class extends Module { export default class extends Module {
public readonly name = 'follow'; public readonly name = 'follow';

View file

@ -1,7 +1,7 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Module from '../../module'; import Module from '@/module';
import Message from '../../message'; import Message from '@/message';
import serifs from '../../serifs'; import serifs from '@/serifs';
import * as seedrandom from 'seedrandom'; import * as seedrandom from 'seedrandom';
import { genItem } from '../../vocabulary'; import { genItem } from '../../vocabulary';

View file

@ -1,8 +1,8 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import * as loki from 'lokijs'; import * as loki from 'lokijs';
import Module from '../../module'; import Module from '@/module';
import Message from '../../message'; import Message from '@/message';
import serifs from '../../serifs'; import serifs from '@/serifs';
export default class extends Module { export default class extends Module {
public readonly name = 'guessingGame'; public readonly name = 'guessingGame';

View file

@ -1,18 +1,17 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import * as loki from 'lokijs'; import * as loki from 'lokijs';
import Module from '../../module'; import Module from '@/module';
import Message from '../../message'; import Message from '@/message';
import serifs from '../../serifs'; import serifs from '@/serifs';
import { User } from '@/misskey/user';
type User = {
id: string;
username: string;
host: string;
};
type Game = { type Game = {
votes: { votes: {
user: User; user: {
id: string;
username: string;
host: User['host'];
};
number: number; number: number;
}[]; }[];
isEnded: boolean; isEnded: boolean;
@ -173,14 +172,14 @@ export default class extends Module {
return; return;
} }
function acct(user: User): string { function acct(user: Game['votes'][0]['user']): string {
return user.host return user.host
? `@${user.username}@${user.host}` ? `@${user.username}@${user.host}`
: `@${user.username}`; : `@${user.username}`;
} }
let results: string[] = []; let results: string[] = [];
let winner: User | null = null; let winner: Game['votes'][0]['user'] | null = null;
for (let i = 100; i >= 0; i--) { for (let i = 100; i >= 0; i--) {
const users = game.votes const users = game.votes

View file

@ -1,8 +1,8 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import * as loki from 'lokijs'; import * as loki from 'lokijs';
import Module from '../../module'; import Module from '@/module';
import config from '../../config'; import config from '../../config';
import serifs from '../../serifs'; import serifs from '@/serifs';
import { mecab } from './mecab'; import { mecab } from './mecab';
function kanaToHira(str: string) { function kanaToHira(str: string) {

View file

@ -1,9 +1,9 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Module from '../../module'; import Module from '@/module';
import serifs from '../../serifs'; import serifs from '@/serifs';
import { genMaze } from './gen-maze'; import { genMaze } from './gen-maze';
import { renderMaze } from './render-maze'; import { renderMaze } from './render-maze';
import Message from '../../message'; import Message from '@/message';
export default class extends Module { export default class extends Module {
public readonly name = 'maze'; public readonly name = 'maze';

View file

@ -1,6 +1,6 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Module from '../../module'; import Module from '@/module';
import serifs from '../../serifs'; import serifs from '@/serifs';
import { genItem } from '../../vocabulary'; import { genItem } from '../../vocabulary';
export default class extends Module { export default class extends Module {

View file

@ -1,6 +1,6 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Module from '../../module'; import Module from '@/module';
import Message from '../../message'; import Message from '@/message';
export default class extends Module { export default class extends Module {
public readonly name = 'ping'; public readonly name = 'ping';

View file

@ -1,7 +1,7 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Message from '../../message'; import Message from '@/message';
import Module from '../../module'; import Module from '@/module';
import serifs from '../../serifs'; import serifs from '@/serifs';
import { genItem } from '../../vocabulary'; import { genItem } from '../../vocabulary';
import config from '../../config'; import config from '../../config';
import { Note } from '../../misskey/note'; import { Note } from '../../misskey/note';

View file

@ -9,7 +9,7 @@
import * as request from 'request-promise-native'; import * as request from 'request-promise-native';
import Reversi, { Color } from 'misskey-reversi'; import Reversi, { Color } from 'misskey-reversi';
import config from '../../config'; import config from '../../config';
import serifs from '../../serifs'; import serifs from '@/serifs';
import { User } from '../../misskey/user'; import { User } from '../../misskey/user';
const db = {}; const db = {};

View file

@ -1,9 +1,9 @@
import * as childProcess from 'child_process'; import * as childProcess from 'child_process';
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Module from '../../module'; import Module from '@/module';
import serifs from '../../serifs'; import serifs from '@/serifs';
import config from '../../config'; import config from '../../config';
import Message from '../../message'; import Message from '@/message';
import Friend from '../../friend'; import Friend from '../../friend';
import getDate from '../../utils/get-date'; import getDate from '../../utils/get-date';

View file

@ -1,6 +1,6 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Module from '../../module'; import Module from '@/module';
import serifs from '../../serifs'; import serifs from '@/serifs';
import config from '../../config'; import config from '../../config';
export default class extends Module { export default class extends Module {

View file

@ -1,6 +1,6 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Module from '../../module'; import Module from '@/module';
import serifs from '../../serifs'; import serifs from '@/serifs';
export default class extends Module { export default class extends Module {
public readonly name = 'sleepReport'; public readonly name = 'sleepReport';

View file

@ -1,7 +1,7 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import { HandlerResult } from '../../ai'; import { HandlerResult } from '../../ai';
import Module from '../../module'; import Module from '@/module';
import Message from '../../message'; import Message from '@/message';
import serifs, { getSerif } from '../../serifs'; import serifs, { getSerif } from '../../serifs';
import getDate from '../../utils/get-date'; import getDate from '../../utils/get-date';

View file

@ -1,7 +1,7 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Module from '../../module'; import Module from '@/module';
import Message from '../../message'; import Message from '@/message';
import serifs from '../../serifs'; import serifs from '@/serifs';
export default class extends Module { export default class extends Module {
public readonly name = 'timer'; public readonly name = 'timer';

View file

@ -1,7 +1,7 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Module from '../../module'; import Module from '@/module';
import Friend from '../../friend'; import Friend from '../../friend';
import serifs from '../../serifs'; import serifs from '@/serifs';
export default class extends Module { export default class extends Module {
public readonly name = 'valentine'; public readonly name = 'valentine';

View file

@ -1,5 +1,5 @@
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Module from '../../module'; import Module from '@/module';
export default class extends Module { export default class extends Module {
public readonly name = 'welcome'; public readonly name = 'welcome';

7
test/_mocks_/account.ts Normal file
View file

@ -0,0 +1,7 @@
export const account = {
id: '0',
name: '藍',
username: 'ai',
host: null,
isBot: true,
};

17
test/_mocks_/ws.ts Normal file
View file

@ -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));
}
}

26
test/_modules_/test.ts Normal file
View file

@ -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;
}
}
}

20
test/core.ts Normal file
View file

@ -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();
});

15
test/tsconfig.json Normal file
View file

@ -0,0 +1,15 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"rootDir": "../",
"paths": {
"@/*": ["../src/*"],
"#/*": ["./*"]
},
},
"compileOnSave": false,
"include": [
"**/*.ts"
]
}

View file

@ -8,12 +8,16 @@
"strictNullChecks": true, "strictNullChecks": true,
"experimentalDecorators": true, "experimentalDecorators": true,
"sourceMap": false, "sourceMap": false,
"target": "es2017", "target": "es2020",
"module": "commonjs", "module": "commonjs",
"removeComments": false, "removeComments": false,
"noLib": false, "noLib": false,
"outDir": "built", "outDir": "built",
"rootDir": "src" "rootDir": "src",
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
},
}, },
"compileOnSave": false, "compileOnSave": false,
"include": [ "include": [