2022-02-27 11:07:39 +09:00
|
|
|
import * as fs from 'node:fs';
|
2022-05-14 16:09:47 +09:00
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
import { dirname } from 'node:path';
|
2019-04-07 21:50:36 +09:00
|
|
|
import * as WebSocket from 'ws';
|
2021-10-31 15:30:22 +09:00
|
|
|
import * as misskey from 'misskey-js';
|
2020-04-09 23:42:23 +09:00
|
|
|
import fetch from 'node-fetch';
|
2022-02-27 11:07:39 +09:00
|
|
|
import FormData from 'form-data';
|
2020-01-09 14:35:04 +09:00
|
|
|
import * as childProcess from 'child_process';
|
2022-04-16 17:19:30 +09:00
|
|
|
import * as http from 'node:http';
|
2022-02-27 11:07:39 +09:00
|
|
|
import loadConfig from '../src/config/load.js';
|
2021-06-05 14:54:07 +09:00
|
|
|
import { SIGKILL } from 'constants';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { entities } from '../src/db/postgre.js';
|
2021-03-12 23:10:15 +09:00
|
|
|
|
2022-05-14 16:09:47 +09:00
|
|
|
const _filename = fileURLToPath(import.meta.url);
|
|
|
|
const _dirname = dirname(_filename);
|
|
|
|
|
2021-06-12 22:40:17 +09:00
|
|
|
const config = loadConfig();
|
|
|
|
export const port = config.port;
|
2019-01-23 12:15:27 +09:00
|
|
|
|
|
|
|
export const async = (fn: Function) => (done: Function) => {
|
|
|
|
fn().then(() => {
|
|
|
|
done();
|
|
|
|
}, (err: Error) => {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
export const request = async (endpoint: string, params: any, me?: any): Promise<{ body: any, status: number }> => {
|
2019-01-23 12:15:27 +09:00
|
|
|
const auth = me ? {
|
|
|
|
i: me.token
|
|
|
|
} : {};
|
|
|
|
|
2021-03-12 23:10:15 +09:00
|
|
|
const res = await fetch(`http://localhost:${port}/api${endpoint}`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify(Object.assign(auth, params))
|
|
|
|
});
|
2019-01-23 12:15:27 +09:00
|
|
|
|
2021-03-12 23:10:15 +09:00
|
|
|
const status = res.status;
|
|
|
|
const body = res.status !== 204 ? await res.json().catch() : null;
|
|
|
|
|
|
|
|
return {
|
|
|
|
body, status
|
|
|
|
};
|
2019-01-23 12:15:27 +09:00
|
|
|
};
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
export const signup = async (params?: any): Promise<any> => {
|
2019-01-23 12:15:27 +09:00
|
|
|
const q = Object.assign({
|
|
|
|
username: 'test',
|
|
|
|
password: 'test'
|
|
|
|
}, params);
|
|
|
|
|
|
|
|
const res = await request('/signup', q);
|
|
|
|
|
|
|
|
return res.body;
|
|
|
|
};
|
|
|
|
|
2021-10-31 15:30:22 +09:00
|
|
|
export const post = async (user: any, params?: misskey.Endpoints['notes/create']['req']): Promise<misskey.entities.Note> => {
|
2019-01-23 12:15:27 +09:00
|
|
|
const q = Object.assign({
|
|
|
|
text: 'test'
|
|
|
|
}, params);
|
|
|
|
|
|
|
|
const res = await request('/notes/create', q, user);
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
return res.body ? res.body.createdNote : null;
|
2019-01-23 12:15:27 +09:00
|
|
|
};
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
export const react = async (user: any, note: any, reaction: string): Promise<any> => {
|
2019-01-23 12:15:27 +09:00
|
|
|
await request('/notes/reactions/create', {
|
|
|
|
noteId: note.id,
|
|
|
|
reaction: reaction
|
|
|
|
}, user);
|
|
|
|
};
|
|
|
|
|
2020-04-09 23:42:23 +09:00
|
|
|
export const uploadFile = (user: any, path?: string): Promise<any> => {
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('i', user.token);
|
2022-05-14 16:09:47 +09:00
|
|
|
formData.append('file', fs.createReadStream(path || _dirname + '/resources/Lenna.png'));
|
2020-04-09 23:42:23 +09:00
|
|
|
|
2021-03-12 23:10:15 +09:00
|
|
|
return fetch(`http://localhost:${port}/api/drive/files/create`, {
|
2020-04-09 23:42:23 +09:00
|
|
|
method: 'post',
|
|
|
|
body: formData,
|
|
|
|
timeout: 30 * 1000,
|
|
|
|
}).then(res => {
|
|
|
|
if (!res.ok) {
|
|
|
|
throw `${res.status} ${res.statusText}`;
|
|
|
|
} else {
|
|
|
|
return res.json();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2019-01-23 12:15:27 +09:00
|
|
|
|
2019-04-18 14:29:17 +09:00
|
|
|
export function connectStream(user: any, channel: string, listener: (message: Record<string, any>) => any, params?: any): Promise<WebSocket> {
|
2019-04-07 21:50:36 +09:00
|
|
|
return new Promise((res, rej) => {
|
2021-03-12 23:10:15 +09:00
|
|
|
const ws = new WebSocket(`ws://localhost:${port}/streaming?i=${user.token}`);
|
2019-01-23 12:15:27 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
ws.on('open', () => {
|
|
|
|
ws.on('message', data => {
|
|
|
|
const msg = JSON.parse(data.toString());
|
|
|
|
if (msg.type == 'channel' && msg.body.id == 'a') {
|
|
|
|
listener(msg.body);
|
|
|
|
} else if (msg.type == 'connected' && msg.body.id == 'a') {
|
|
|
|
res(ws);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
type: 'connect',
|
|
|
|
body: {
|
|
|
|
channel: channel,
|
|
|
|
id: 'a',
|
|
|
|
pong: true,
|
|
|
|
params: params
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-01-09 14:35:04 +09:00
|
|
|
|
2021-03-13 23:22:54 +09:00
|
|
|
export const simpleGet = async (path: string, accept = '*/*'): Promise<{ status?: number, type?: string, location?: string }> => {
|
2021-03-12 23:10:15 +09:00
|
|
|
// node-fetchだと3xxを取れない
|
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
const req = http.request(`http://localhost:${port}${path}`, {
|
|
|
|
headers: {
|
|
|
|
Accept: accept
|
|
|
|
}
|
|
|
|
}, res => {
|
|
|
|
if (res.statusCode! >= 400) {
|
|
|
|
reject(res);
|
|
|
|
} else {
|
|
|
|
resolve({
|
|
|
|
status: res.statusCode,
|
|
|
|
type: res.headers['content-type'],
|
|
|
|
location: res.headers.location,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
req.end();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-01-09 14:35:04 +09:00
|
|
|
export function launchServer(callbackSpawnedProcess: (p: childProcess.ChildProcess) => void, moreProcess: () => Promise<void> = async () => {}) {
|
|
|
|
return (done: (err?: Error) => any) => {
|
2022-05-14 16:09:47 +09:00
|
|
|
const p = childProcess.spawn('node', [_dirname + '/../index.js'], {
|
2020-01-09 14:35:04 +09:00
|
|
|
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
|
|
|
|
env: { NODE_ENV: 'test', PATH: process.env.PATH }
|
|
|
|
});
|
|
|
|
callbackSpawnedProcess(p);
|
|
|
|
p.on('message', message => {
|
|
|
|
if (message === 'ok') moreProcess().then(() => done()).catch(e => done(e));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2021-06-05 14:54:07 +09:00
|
|
|
|
2021-06-12 22:40:17 +09:00
|
|
|
export async function initTestDb(justBorrow = false, initEntities?: any[]) {
|
|
|
|
if (process.env.NODE_ENV !== 'test') throw 'NODE_ENV is not a test';
|
|
|
|
|
|
|
|
try {
|
|
|
|
const conn = await getConnection();
|
|
|
|
await conn.close();
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
return await createConnection({
|
|
|
|
type: 'postgres',
|
|
|
|
host: config.db.host,
|
|
|
|
port: config.db.port,
|
|
|
|
username: config.db.user,
|
|
|
|
password: config.db.pass,
|
|
|
|
database: config.db.db,
|
|
|
|
synchronize: true && !justBorrow,
|
|
|
|
dropSchema: true && !justBorrow,
|
|
|
|
entities: initEntities || entities
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function startServer(timeout = 30 * 1000): Promise<childProcess.ChildProcess> {
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
const t = setTimeout(() => {
|
|
|
|
p.kill(SIGKILL);
|
|
|
|
rej('timeout to start');
|
|
|
|
}, timeout);
|
|
|
|
|
2022-05-14 16:09:47 +09:00
|
|
|
const p = childProcess.spawn('node', [_dirname + '/../built/index.js'], {
|
2021-06-12 22:40:17 +09:00
|
|
|
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
|
|
|
|
env: { NODE_ENV: 'test', PATH: process.env.PATH }
|
|
|
|
});
|
|
|
|
|
|
|
|
p.on('error', e => rej(e));
|
|
|
|
|
|
|
|
p.on('message', message => {
|
|
|
|
if (message === 'ok') {
|
|
|
|
clearTimeout(t);
|
|
|
|
res(p);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-05 14:54:07 +09:00
|
|
|
export function shutdownServer(p: childProcess.ChildProcess, timeout = 20 * 1000) {
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
const t = setTimeout(() => {
|
|
|
|
p.kill(SIGKILL);
|
|
|
|
res('force exit');
|
|
|
|
}, timeout);
|
|
|
|
|
|
|
|
p.once('exit', () => {
|
|
|
|
clearTimeout(t);
|
|
|
|
res('exited');
|
|
|
|
});
|
|
|
|
|
|
|
|
p.kill();
|
|
|
|
});
|
|
|
|
}
|