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