2023-04-07 10:56:46 +09:00
|
|
|
import * as fs from "node:fs";
|
|
|
|
import * as path from "node:path";
|
|
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
import { dirname } from "node:path";
|
|
|
|
import * as childProcess from "child_process";
|
|
|
|
import * as http from "node:http";
|
|
|
|
import { SIGKILL } from "constants";
|
|
|
|
import WebSocket from "ws";
|
|
|
|
import * as misskey from "calckey-js";
|
|
|
|
import fetch from "node-fetch";
|
|
|
|
import FormData from "form-data";
|
|
|
|
import { DataSource } from "typeorm";
|
|
|
|
import loadConfig from "../src/config/load.js";
|
|
|
|
import { entities } from "../src/db/postgre.js";
|
|
|
|
import got from "got";
|
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) => {
|
2023-04-07 10:56:46 +09:00
|
|
|
fn().then(
|
|
|
|
() => {
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
(err: Error) => {
|
|
|
|
done(err);
|
|
|
|
},
|
|
|
|
);
|
2019-01-23 12:15:27 +09:00
|
|
|
};
|
|
|
|
|
2022-06-26 19:16:32 +09:00
|
|
|
export const api = async (endpoint: string, params: any, me?: any) => {
|
2023-04-07 10:56:46 +09:00
|
|
|
endpoint = endpoint.replace(/^\//, "");
|
2022-06-26 19:16:32 +09:00
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
const auth = me
|
|
|
|
? {
|
|
|
|
i: me.token,
|
|
|
|
}
|
|
|
|
: {};
|
2022-06-26 19:16:32 +09:00
|
|
|
|
|
|
|
const res = await got<string>(`http://localhost:${port}/api/${endpoint}`, {
|
2023-04-07 10:56:46 +09:00
|
|
|
method: "POST",
|
2022-06-26 19:16:32 +09:00
|
|
|
headers: {
|
2023-04-07 10:56:46 +09:00
|
|
|
"Content-Type": "application/json",
|
2022-06-26 19:16:32 +09:00
|
|
|
},
|
|
|
|
body: JSON.stringify(Object.assign(auth, params)),
|
|
|
|
retry: {
|
|
|
|
limit: 0,
|
|
|
|
},
|
|
|
|
hooks: {
|
|
|
|
beforeError: [
|
2023-04-07 10:56:46 +09:00
|
|
|
(error) => {
|
2022-06-26 19:16:32 +09:00
|
|
|
const { response } = error;
|
|
|
|
if (response && response.body) console.warn(response.body);
|
|
|
|
return error;
|
2023-04-07 10:56:46 +09:00
|
|
|
},
|
|
|
|
],
|
2022-06-26 19:16:32 +09:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const status = res.statusCode;
|
|
|
|
const body = res.statusCode !== 204 ? await JSON.parse(res.body) : null;
|
|
|
|
|
|
|
|
return {
|
|
|
|
status,
|
2023-04-07 10:56:46 +09:00
|
|
|
body,
|
2022-06-26 19:16:32 +09:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
export const request = async (
|
|
|
|
endpoint: string,
|
|
|
|
params: any,
|
|
|
|
me?: any,
|
|
|
|
): Promise<{ body: any; status: number }> => {
|
|
|
|
const auth = me
|
|
|
|
? {
|
|
|
|
i: me.token,
|
|
|
|
}
|
|
|
|
: {};
|
2019-01-23 12:15:27 +09:00
|
|
|
|
2021-03-12 23:10:15 +09:00
|
|
|
const res = await fetch(`http://localhost:${port}/api${endpoint}`, {
|
2023-04-07 10:56:46 +09:00
|
|
|
method: "POST",
|
2021-03-12 23:10:15 +09:00
|
|
|
headers: {
|
2023-04-07 10:56:46 +09:00
|
|
|
"Content-Type": "application/json",
|
2021-03-12 23:10:15 +09:00
|
|
|
},
|
2022-05-21 17:40:43 +09:00
|
|
|
body: JSON.stringify(Object.assign(auth, params)),
|
2021-03-12 23:10:15 +09:00
|
|
|
});
|
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 {
|
2023-04-07 10:56:46 +09:00
|
|
|
body,
|
|
|
|
status,
|
2021-03-12 23:10:15 +09:00
|
|
|
};
|
2019-01-23 12:15:27 +09:00
|
|
|
};
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
export const signup = async (params?: any): Promise<any> => {
|
2023-04-07 10:56:46 +09:00
|
|
|
const q = Object.assign(
|
|
|
|
{
|
|
|
|
username: "test",
|
|
|
|
password: "test",
|
|
|
|
},
|
|
|
|
params,
|
|
|
|
);
|
2019-01-23 12:15:27 +09:00
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
const res = await api("signup", q);
|
2019-01-23 12:15:27 +09:00
|
|
|
|
|
|
|
return res.body;
|
|
|
|
};
|
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
export const post = async (
|
|
|
|
user: any,
|
|
|
|
params?: misskey.Endpoints["notes/create"]["req"],
|
|
|
|
): Promise<misskey.entities.Note> => {
|
|
|
|
const q = Object.assign(
|
|
|
|
{
|
|
|
|
text: "test",
|
|
|
|
},
|
|
|
|
params,
|
|
|
|
);
|
2019-01-23 12:15:27 +09:00
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
const res = await api("notes/create", q, user);
|
2019-01-23 12:15:27 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
return res.body ? res.body.createdNote : null;
|
2019-01-23 12:15:27 +09:00
|
|
|
};
|
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
export const react = async (
|
|
|
|
user: any,
|
|
|
|
note: any,
|
|
|
|
reaction: string,
|
|
|
|
): Promise<any> => {
|
|
|
|
await api(
|
|
|
|
"notes/reactions/create",
|
|
|
|
{
|
|
|
|
noteId: note.id,
|
|
|
|
reaction: reaction,
|
|
|
|
},
|
|
|
|
user,
|
|
|
|
);
|
2019-01-23 12:15:27 +09:00
|
|
|
};
|
|
|
|
|
2022-06-26 19:16:32 +09:00
|
|
|
/**
|
|
|
|
* Upload file
|
|
|
|
* @param user User
|
|
|
|
* @param _path Optional, absolute path or relative from ./resources/
|
|
|
|
*/
|
|
|
|
export const uploadFile = async (user: any, _path?: string): Promise<any> => {
|
2023-04-07 10:56:46 +09:00
|
|
|
const absPath =
|
|
|
|
_path == null
|
|
|
|
? `${_dirname}/resources/Lenna.jpg`
|
|
|
|
: path.isAbsolute(_path)
|
|
|
|
? _path
|
|
|
|
: `${_dirname}/resources/${_path}`;
|
2022-06-26 19:16:32 +09:00
|
|
|
|
|
|
|
const formData = new FormData() as any;
|
2023-04-07 10:56:46 +09:00
|
|
|
formData.append("i", user.token);
|
|
|
|
formData.append("file", fs.createReadStream(absPath));
|
|
|
|
formData.append("force", "true");
|
|
|
|
|
|
|
|
const res = await got<string>(
|
|
|
|
`http://localhost:${port}/api/drive/files/create`,
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
body: formData,
|
|
|
|
retry: {
|
|
|
|
limit: 0,
|
|
|
|
},
|
2022-06-26 19:16:32 +09:00
|
|
|
},
|
2023-04-07 10:56:46 +09:00
|
|
|
);
|
2022-06-26 19:16:32 +09:00
|
|
|
|
|
|
|
const body = res.statusCode !== 204 ? await JSON.parse(res.body) : null;
|
|
|
|
|
|
|
|
return body;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const uploadUrl = async (user: any, url: string) => {
|
|
|
|
let file: any;
|
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
const ws = await connectStream(user, "main", (msg) => {
|
|
|
|
if (msg.type === "driveFileCreated") {
|
2022-06-26 19:16:32 +09:00
|
|
|
file = msg.body;
|
2022-05-21 17:40:43 +09:00
|
|
|
}
|
|
|
|
});
|
2022-06-26 19:16:32 +09:00
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
await api(
|
|
|
|
"drive/files/upload-from-url",
|
|
|
|
{
|
|
|
|
url,
|
|
|
|
force: true,
|
|
|
|
},
|
|
|
|
user,
|
|
|
|
);
|
2022-06-26 19:16:32 +09:00
|
|
|
|
|
|
|
await sleep(5000);
|
|
|
|
ws.close();
|
|
|
|
|
|
|
|
return file;
|
2020-04-09 23:42:23 +09:00
|
|
|
};
|
2019-01-23 12:15:27 +09:00
|
|
|
|
2023-04-07 10:56:46 +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) => {
|
2023-04-07 10:56:46 +09:00
|
|
|
const ws = new WebSocket(
|
|
|
|
`ws://localhost:${port}/streaming?i=${user.token}`,
|
|
|
|
);
|
2019-01-23 12:15:27 +09:00
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
ws.on("open", () => {
|
|
|
|
ws.on("message", (data) => {
|
2019-04-07 21:50:36 +09:00
|
|
|
const msg = JSON.parse(data.toString());
|
2023-04-07 10:56:46 +09:00
|
|
|
if (msg.type === "channel" && msg.body.id === "a") {
|
2019-04-07 21:50:36 +09:00
|
|
|
listener(msg.body);
|
2023-04-07 10:56:46 +09:00
|
|
|
} else if (msg.type === "connected" && msg.body.id === "a") {
|
2019-04-07 21:50:36 +09:00
|
|
|
res(ws);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
ws.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type: "connect",
|
|
|
|
body: {
|
|
|
|
channel: channel,
|
|
|
|
id: "a",
|
|
|
|
pong: true,
|
|
|
|
params: params,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2019-04-07 21:50:36 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-01-09 14:35:04 +09:00
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
export const waitFire = async (
|
|
|
|
user: any,
|
|
|
|
channel: string,
|
|
|
|
trgr: () => any,
|
|
|
|
cond: (msg: Record<string, any>) => boolean,
|
|
|
|
params?: any,
|
|
|
|
) => {
|
2022-06-26 19:16:32 +09:00
|
|
|
return new Promise<boolean>(async (res, rej) => {
|
|
|
|
let timer: NodeJS.Timeout;
|
|
|
|
|
|
|
|
let ws: WebSocket;
|
|
|
|
try {
|
2023-04-07 10:56:46 +09:00
|
|
|
ws = await connectStream(
|
|
|
|
user,
|
|
|
|
channel,
|
|
|
|
(msg) => {
|
|
|
|
if (cond(msg)) {
|
|
|
|
ws.close();
|
|
|
|
if (timer) clearTimeout(timer);
|
|
|
|
res(true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
params,
|
|
|
|
);
|
2022-06-26 19:16:32 +09:00
|
|
|
} catch (e) {
|
|
|
|
rej(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ws!) return;
|
|
|
|
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
ws.close();
|
|
|
|
res(false);
|
2022-07-03 20:54:54 +09:00
|
|
|
}, 3000);
|
2022-06-26 19:16:32 +09:00
|
|
|
|
|
|
|
try {
|
|
|
|
await trgr();
|
|
|
|
} catch (e) {
|
|
|
|
ws.close();
|
|
|
|
if (timer) clearTimeout(timer);
|
|
|
|
rej(e);
|
|
|
|
}
|
2023-04-07 10:56:46 +09:00
|
|
|
});
|
2022-06-26 19:16:32 +09:00
|
|
|
};
|
|
|
|
|
2023-04-07 10:56:46 +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) => {
|
2023-04-07 10:56:46 +09:00
|
|
|
const req = http.request(
|
|
|
|
`http://localhost:${port}${path}`,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
Accept: accept,
|
|
|
|
},
|
2022-05-21 17:40:43 +09:00
|
|
|
},
|
2023-04-07 10:56:46 +09:00
|
|
|
(res) => {
|
|
|
|
if (res.statusCode! >= 400) {
|
|
|
|
reject(res);
|
|
|
|
} else {
|
|
|
|
resolve({
|
|
|
|
status: res.statusCode,
|
|
|
|
type: res.headers["content-type"],
|
|
|
|
location: res.headers.location,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2021-03-12 23:10:15 +09:00
|
|
|
|
|
|
|
req.end();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
export function launchServer(
|
|
|
|
callbackSpawnedProcess: (p: childProcess.ChildProcess) => void,
|
|
|
|
moreProcess: () => Promise<void> = async () => {},
|
|
|
|
) {
|
2020-01-09 14:35:04 +09:00
|
|
|
return (done: (err?: Error) => any) => {
|
2023-04-07 10:56:46 +09:00
|
|
|
const p = childProcess.spawn("node", [_dirname + "/../index.js"], {
|
|
|
|
stdio: ["inherit", "inherit", "inherit", "ipc"],
|
|
|
|
env: { NODE_ENV: "test", PATH: process.env.PATH },
|
2020-01-09 14:35:04 +09:00
|
|
|
});
|
|
|
|
callbackSpawnedProcess(p);
|
2023-04-07 10:56:46 +09:00
|
|
|
p.on("message", (message) => {
|
|
|
|
if (message === "ok")
|
|
|
|
moreProcess()
|
|
|
|
.then(() => done())
|
|
|
|
.catch((e) => done(e));
|
2020-01-09 14:35:04 +09:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2021-06-05 14:54:07 +09:00
|
|
|
|
2021-06-12 22:40:17 +09:00
|
|
|
export async function initTestDb(justBorrow = false, initEntities?: any[]) {
|
2023-04-07 10:56:46 +09:00
|
|
|
if (process.env.NODE_ENV !== "test") throw "NODE_ENV is not a test";
|
2021-06-12 22:40:17 +09:00
|
|
|
|
2022-05-21 22:07:01 +09:00
|
|
|
const db = new DataSource({
|
2023-04-07 10:56:46 +09:00
|
|
|
type: "postgres",
|
2021-06-12 22:40:17 +09:00
|
|
|
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 17:40:43 +09:00
|
|
|
entities: initEntities || entities,
|
2021-06-12 22:40:17 +09:00
|
|
|
});
|
2022-05-21 22:07:01 +09:00
|
|
|
|
|
|
|
await db.initialize();
|
|
|
|
|
|
|
|
return db;
|
2021-06-12 22:40:17 +09:00
|
|
|
}
|
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
export function startServer(
|
|
|
|
timeout = 60 * 1000,
|
|
|
|
): Promise<childProcess.ChildProcess> {
|
2021-06-12 22:40:17 +09:00
|
|
|
return new Promise((res, rej) => {
|
|
|
|
const t = setTimeout(() => {
|
|
|
|
p.kill(SIGKILL);
|
2023-04-07 10:56:46 +09:00
|
|
|
rej("timeout to start");
|
2021-06-12 22:40:17 +09:00
|
|
|
}, timeout);
|
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
const p = childProcess.spawn("node", [_dirname + "/../built/index.js"], {
|
|
|
|
stdio: ["inherit", "inherit", "inherit", "ipc"],
|
|
|
|
env: { NODE_ENV: "test", PATH: process.env.PATH },
|
2021-06-12 22:40:17 +09:00
|
|
|
});
|
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
p.on("error", (e) => rej(e));
|
2021-06-12 22:40:17 +09:00
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
p.on("message", (message) => {
|
|
|
|
if (message === "ok") {
|
2021-06-12 22:40:17 +09:00
|
|
|
clearTimeout(t);
|
|
|
|
res(p);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
export function shutdownServer(
|
|
|
|
p: childProcess.ChildProcess,
|
|
|
|
timeout = 20 * 1000,
|
|
|
|
) {
|
2021-06-05 14:54:07 +09:00
|
|
|
return new Promise((res, rej) => {
|
|
|
|
const t = setTimeout(() => {
|
|
|
|
p.kill(SIGKILL);
|
2023-04-07 10:56:46 +09:00
|
|
|
res("force exit");
|
2021-06-05 14:54:07 +09:00
|
|
|
}, timeout);
|
|
|
|
|
2023-04-07 10:56:46 +09:00
|
|
|
p.once("exit", () => {
|
2021-06-05 14:54:07 +09:00
|
|
|
clearTimeout(t);
|
2023-04-07 10:56:46 +09:00
|
|
|
res("exited");
|
2021-06-05 14:54:07 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
p.kill();
|
|
|
|
});
|
|
|
|
}
|
2022-06-26 19:16:32 +09:00
|
|
|
|
|
|
|
export function sleep(msec: number) {
|
2023-04-07 10:56:46 +09:00
|
|
|
return new Promise<void>((res) => {
|
2022-06-26 19:16:32 +09:00
|
|
|
setTimeout(() => {
|
|
|
|
res();
|
|
|
|
}, msec);
|
|
|
|
});
|
|
|
|
}
|