yumechi-no-kuni/packages/backend/test-server/entry.ts
eternal-flame-AD 109d8f8008
All checks were successful
Test (production install and build) / production (22.11.0) (push) Successful in 1m53s
Lint / pnpm_install (pull_request) Successful in 1m36s
Publish Docker image / Build (push) Successful in 5m12s
Publish Docker image / Build (pull_request) Successful in 4m28s
Test (production install and build) / production (22.11.0) (pull_request) Successful in 1m13s
Lint / lint (backend) (pull_request) Successful in 2m47s
Test (backend) / unit (22.11.0) (pull_request) Successful in 8m38s
Lint / lint (frontend-embed) (pull_request) Successful in 2m25s
Test (backend) / e2e (22.11.0) (pull_request) Successful in 11m35s
Lint / lint (frontend-shared) (pull_request) Successful in 2m28s
Lint / lint (misskey-bubble-game) (pull_request) Successful in 2m37s
Lint / lint (frontend) (pull_request) Successful in 9m50s
Lint / lint (misskey-js) (pull_request) Successful in 2m27s
Lint / lint (sw) (pull_request) Successful in 2m45s
Lint / lint (misskey-reversi) (pull_request) Successful in 2m58s
Lint / typecheck (backend) (pull_request) Successful in 2m42s
Lint / typecheck (misskey-js) (pull_request) Successful in 1m49s
Lint / typecheck (sw) (pull_request) Successful in 1m59s
don't log metrics for postgres in testing
Signed-off-by: eternal-flame-AD <yume@yumechi.jp>
2024-11-17 11:58:15 -06:00

95 lines
2.5 KiB
TypeScript

import { portToPid } from 'pid-port';
import fkill from 'fkill';
import Fastify from 'fastify';
import { NestFactory } from '@nestjs/core';
import { MainModule } from '@/MainModule.js';
import { ServerService } from '@/server/ServerService.js';
import { loadConfig } from '@/config.js';
import { INestApplicationContext } from '@nestjs/common';
const config = loadConfig();
const originEnv = JSON.stringify(process.env);
process.env.NODE_ENV = 'test';
let app: INestApplicationContext;
let serverService: ServerService;
/**
* テスト用のサーバインスタンスを起動する
*/
async function launch() {
await killTestServer();
app = await NestFactory.createApplicationContext(MainModule, {
logger: ["debug", "log", "error", "warn", "verbose"],
});
serverService = app.get(ServerService);
await serverService.launch();
await startControllerEndpoints();
// ジョブキューは必要な時にテストコード側で起動する
// ジョブキューが動くとテスト結果の確認に支障が出ることがあるので意図的に動かさないでいる
console.log('application initialized.');
}
/**
* 既に重複したポートで待ち受けしているサーバがある場合はkillする
*/
async function killTestServer() {
//
try {
const pid = await portToPid(config.port);
if (pid) {
await fkill(pid, { force: true });
}
} catch {
// NOP;
}
}
/**
* 別プロセスに切り離してしまったが故に出来なくなった環境変数の書き換え等を実現するためのエンドポイントを作る
* @param port
*/
async function startControllerEndpoints(port = config.port + 1000) {
const fastify = Fastify();
fastify.post<{ Body: { key?: string, value?: string } }>('/env', async (req, res) => {
console.log(req.body);
const key = req.body['key'];
if (!key) {
res.code(400).send({ success: false });
return;
}
process.env[key] = req.body['value'];
res.code(200).send({ success: true });
});
fastify.post<{ Body: { key?: string, value?: string } }>('/env-reset', async (req, res) => {
process.env = JSON.parse(originEnv);
await serverService.dispose();
await app.close();
await killTestServer();
console.log('starting application...');
app = await NestFactory.createApplicationContext(MainModule, {
logger: ["debug", "log", "error", "warn", "verbose"],
});
serverService = app.get(ServerService);
await serverService.launch();
res.code(200).send({ success: true });
});
await fastify.listen({ port: port, host: 'localhost' });
}
export default launch;