Sharkey/test/utils.ts
rinsuki 3ecb0ab161 Add GitHub Actions (#5522)
* add .github/workflows/nodejs.yml

* fix

* GitHub Actions: Node.js 8.xはサポートしない

* add .github/workflows/docker.yml

* Dockerビルドをキャッシュするように

* Run test in github actions

* 同リポジトリ内からのプルリクだと無駄に二回走るのを抑制

* 6925c00のdocker.ymlへの適応忘れ

* fix .circleci/misskey/test.yml

* test実行時にDBとかredisとか動かすように

* fix

* fix tests (#5544)

* fix test

* fix compile errors

* PATH引き継ぎでchild_process.spawn時のENOENTを修正

*  サーバー起動処理を共通化

* fix coding style

* fd=4をipcに使うように

* fix port

* fix

* fix ws port

* #4033 にテストケースを追従

* fix?

* fix??

* fix

* fix

* fix

* maybe fix

* fix

* node 10.xサポートしてなかった

* 11.10じゃないとだめだった

* fix

* remove chart test

* fix

* chart test復活

* fix

* 一回一回コネクションを閉じる

* Revert "一回一回コネクションを閉じる"

This reverts commit 56e35cf4f83070744c8dd852f1a7075011d88828.

* 一回一回sync→dropしてるのをやめてみる

* fix

* fix

* …

* キャッシュを切ってみる

* add ts to require target

* omg fix

* Revert "キャッシュを切ってみる"

This reverts commit 88161c59d2ea769ddf87143ba4fd4660a06afdf2.

* done呼び忘れ

* 実際の文字数リミットと違ってたので対応

* テストケースがバグってたので修正

* Revert "一回一回sync→dropしてるのをやめてみる"

This reverts commit a9e543ba2eef790ac7a14ae8799b898765748e35.

* fix

* fix

* fix

* fix?

* fix

* chartのconnectionを分離する

* fix

* fix

* fix tsconfig?

* Revert "fix tsconfig?"

This reverts commit ba9269eaf65507ff97ec1dd2e27260fb2cf0510b.

* fix

* TS_NODE_FILES を scripts の方で指定

* Windowsェ

* Circle CIの実行条件をmasterへのpushのみに

Co-authored-by: syuilo <Syuilotan@yahoo.co.jp>
2020-01-09 14:35:04 +09:00

118 lines
2.9 KiB
TypeScript

import * as fs from 'fs';
import * as WebSocket from 'ws';
const fetch = require('node-fetch');
import * as req from 'request';
import * as childProcess from 'child_process';
export const async = (fn: Function) => (done: Function) => {
fn().then(() => {
done();
}, (err: Error) => {
done(err);
});
};
export const request = async (endpoint: string, params: any, me?: any): Promise<{ body: any, status: number }> => {
const auth = me ? {
i: me.token
} : {};
try {
const res = await fetch('http://localhost:8080/api' + endpoint, {
method: 'POST',
body: JSON.stringify(Object.assign(auth, params))
});
const status = res.status;
const body = res.status !== 204 ? await res.json().catch() : null;
return {
body, status
};
} catch (e) {
return {
body: null, status: 500
};
}
};
export const signup = async (params?: any): Promise<any> => {
const q = Object.assign({
username: 'test',
password: 'test'
}, params);
const res = await request('/signup', q);
return res.body;
};
export const post = async (user: any, params?: any): Promise<any> => {
const q = Object.assign({
text: 'test'
}, params);
const res = await request('/notes/create', q, user);
return res.body ? res.body.createdNote : null;
};
export const react = async (user: any, note: any, reaction: string): Promise<any> => {
await request('/notes/reactions/create', {
noteId: note.id,
reaction: reaction
}, user);
};
export const uploadFile = (user: any, path?: string): Promise<any> => new Promise((ok, rej) => {
req.post({
url: 'http://localhost:8080/api/drive/files/create',
formData: {
i: user.token,
file: fs.createReadStream(path || __dirname + '/resources/Lenna.png')
},
json: true
}, (err, httpResponse, body) => {
ok(body);
});
});
export function connectStream(user: any, channel: string, listener: (message: Record<string, any>) => any, params?: any): Promise<WebSocket> {
return new Promise((res, rej) => {
const ws = new WebSocket(`ws://localhost:8080/streaming?i=${user.token}`);
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
}
}));
});
});
}
export function launchServer(callbackSpawnedProcess: (p: childProcess.ChildProcess) => void, moreProcess: () => Promise<void> = async () => {}) {
return (done: (err?: Error) => any) => {
const p = childProcess.spawn('node', [__dirname + '/../index.js'], {
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));
});
};
}