Update utils.ts
This commit is contained in:
parent
02ec5b1dbe
commit
2205c61edf
|
@ -1,14 +1,15 @@
|
||||||
import * as fs from 'node:fs';
|
import * as fs from 'node:fs';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import { dirname } from 'node:path';
|
import { dirname } from 'node:path';
|
||||||
|
import * as childProcess from 'child_process';
|
||||||
|
import * as http from 'node:http';
|
||||||
|
import { SIGKILL } from 'constants';
|
||||||
import * as WebSocket from 'ws';
|
import * as WebSocket from 'ws';
|
||||||
import * as misskey from 'misskey-js';
|
import * as misskey from 'misskey-js';
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
import FormData from 'form-data';
|
import FormData from 'form-data';
|
||||||
import * as childProcess from 'child_process';
|
import { DataSource } from 'typeorm';
|
||||||
import * as http from 'node:http';
|
|
||||||
import loadConfig from '../src/config/load.js';
|
import loadConfig from '../src/config/load.js';
|
||||||
import { SIGKILL } from 'constants';
|
|
||||||
import { entities } from '../src/db/postgre.js';
|
import { entities } from '../src/db/postgre.js';
|
||||||
|
|
||||||
const _filename = fileURLToPath(import.meta.url);
|
const _filename = fileURLToPath(import.meta.url);
|
||||||
|
@ -27,29 +28,29 @@ export const async = (fn: Function) => (done: Function) => {
|
||||||
|
|
||||||
export const request = async (endpoint: string, params: any, me?: any): Promise<{ body: any, status: number }> => {
|
export const request = async (endpoint: string, params: any, me?: any): Promise<{ body: any, status: number }> => {
|
||||||
const auth = me ? {
|
const auth = me ? {
|
||||||
i: me.token
|
i: me.token,
|
||||||
} : {};
|
} : {};
|
||||||
|
|
||||||
const res = await fetch(`http://localhost:${port}/api${endpoint}`, {
|
const res = await fetch(`http://localhost:${port}/api${endpoint}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify(Object.assign(auth, params))
|
body: JSON.stringify(Object.assign(auth, params)),
|
||||||
});
|
});
|
||||||
|
|
||||||
const status = res.status;
|
const status = res.status;
|
||||||
const body = res.status !== 204 ? await res.json().catch() : null;
|
const body = res.status !== 204 ? await res.json().catch() : null;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
body, status
|
body, status,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const signup = async (params?: any): Promise<any> => {
|
export const signup = async (params?: any): Promise<any> => {
|
||||||
const q = Object.assign({
|
const q = Object.assign({
|
||||||
username: 'test',
|
username: 'test',
|
||||||
password: 'test'
|
password: 'test',
|
||||||
}, params);
|
}, params);
|
||||||
|
|
||||||
const res = await request('/signup', q);
|
const res = await request('/signup', q);
|
||||||
|
@ -59,7 +60,7 @@ export const signup = async (params?: any): Promise<any> => {
|
||||||
|
|
||||||
export const post = async (user: any, params?: misskey.Endpoints['notes/create']['req']): Promise<misskey.entities.Note> => {
|
export const post = async (user: any, params?: misskey.Endpoints['notes/create']['req']): Promise<misskey.entities.Note> => {
|
||||||
const q = Object.assign({
|
const q = Object.assign({
|
||||||
text: 'test'
|
text: 'test',
|
||||||
}, params);
|
}, params);
|
||||||
|
|
||||||
const res = await request('/notes/create', q, user);
|
const res = await request('/notes/create', q, user);
|
||||||
|
@ -70,7 +71,7 @@ export const post = async (user: any, params?: misskey.Endpoints['notes/create']
|
||||||
export const react = async (user: any, note: any, reaction: string): Promise<any> => {
|
export const react = async (user: any, note: any, reaction: string): Promise<any> => {
|
||||||
await request('/notes/reactions/create', {
|
await request('/notes/reactions/create', {
|
||||||
noteId: note.id,
|
noteId: note.id,
|
||||||
reaction: reaction
|
reaction: reaction,
|
||||||
}, user);
|
}, user);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -112,8 +113,8 @@ export function connectStream(user: any, channel: string, listener: (message: Re
|
||||||
channel: channel,
|
channel: channel,
|
||||||
id: 'a',
|
id: 'a',
|
||||||
pong: true,
|
pong: true,
|
||||||
params: params
|
params: params,
|
||||||
}
|
},
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -124,8 +125,8 @@ export const simpleGet = async (path: string, accept = '*/*'): Promise<{ status?
|
||||||
return await new Promise((resolve, reject) => {
|
return await new Promise((resolve, reject) => {
|
||||||
const req = http.request(`http://localhost:${port}${path}`, {
|
const req = http.request(`http://localhost:${port}${path}`, {
|
||||||
headers: {
|
headers: {
|
||||||
Accept: accept
|
Accept: accept,
|
||||||
}
|
},
|
||||||
}, res => {
|
}, res => {
|
||||||
if (res.statusCode! >= 400) {
|
if (res.statusCode! >= 400) {
|
||||||
reject(res);
|
reject(res);
|
||||||
|
@ -146,7 +147,7 @@ export function launchServer(callbackSpawnedProcess: (p: childProcess.ChildProce
|
||||||
return (done: (err?: Error) => any) => {
|
return (done: (err?: Error) => any) => {
|
||||||
const p = childProcess.spawn('node', [_dirname + '/../index.js'], {
|
const p = childProcess.spawn('node', [_dirname + '/../index.js'], {
|
||||||
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
|
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
|
||||||
env: { NODE_ENV: 'test', PATH: process.env.PATH }
|
env: { NODE_ENV: 'test', PATH: process.env.PATH },
|
||||||
});
|
});
|
||||||
callbackSpawnedProcess(p);
|
callbackSpawnedProcess(p);
|
||||||
p.on('message', message => {
|
p.on('message', message => {
|
||||||
|
@ -158,12 +159,7 @@ export function launchServer(callbackSpawnedProcess: (p: childProcess.ChildProce
|
||||||
export async function initTestDb(justBorrow = false, initEntities?: any[]) {
|
export async function initTestDb(justBorrow = false, initEntities?: any[]) {
|
||||||
if (process.env.NODE_ENV !== 'test') throw 'NODE_ENV is not a test';
|
if (process.env.NODE_ENV !== 'test') throw 'NODE_ENV is not a test';
|
||||||
|
|
||||||
try {
|
return new DataSource({
|
||||||
const conn = await getConnection();
|
|
||||||
await conn.close();
|
|
||||||
} catch (e) {}
|
|
||||||
|
|
||||||
return await createConnection({
|
|
||||||
type: 'postgres',
|
type: 'postgres',
|
||||||
host: config.db.host,
|
host: config.db.host,
|
||||||
port: config.db.port,
|
port: config.db.port,
|
||||||
|
@ -172,7 +168,7 @@ export async function initTestDb(justBorrow = false, initEntities?: any[]) {
|
||||||
database: config.db.db,
|
database: config.db.db,
|
||||||
synchronize: true && !justBorrow,
|
synchronize: true && !justBorrow,
|
||||||
dropSchema: true && !justBorrow,
|
dropSchema: true && !justBorrow,
|
||||||
entities: initEntities || entities
|
entities: initEntities || entities,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,7 +181,7 @@ export function startServer(timeout = 30 * 1000): Promise<childProcess.ChildProc
|
||||||
|
|
||||||
const p = childProcess.spawn('node', [_dirname + '/../built/index.js'], {
|
const p = childProcess.spawn('node', [_dirname + '/../built/index.js'], {
|
||||||
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
|
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
|
||||||
env: { NODE_ENV: 'test', PATH: process.env.PATH }
|
env: { NODE_ENV: 'test', PATH: process.env.PATH },
|
||||||
});
|
});
|
||||||
|
|
||||||
p.on('error', e => rej(e));
|
p.on('error', e => rej(e));
|
||||||
|
|
Loading…
Reference in New Issue