SponsorBlockServer/test/test.ts

70 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-07-12 08:43:46 +02:00
import Mocha from "mocha";
import fs from "fs";
import path from "path";
2021-09-23 00:52:35 +02:00
import { config } from "../src/config";
import { createServer } from "../src/app";
import { createMockServer } from "./mocks";
import { Logger } from "../src/utils/logger";
import { initDb } from "../src/databases/databases";
import { ImportMock } from "ts-mock-imports";
2021-07-12 08:43:46 +02:00
import * as rateLimitMiddlewareModule from "../src/middleware/requestRateLimit";
import rateLimit from "express-rate-limit";
2021-12-19 08:03:50 +01:00
import redis from "../src/utils/redis";
import { resetRedis, resetPostgres } from "./utils/reset";
2021-03-07 06:21:56 +01:00
async function init() {
2021-07-12 08:43:46 +02:00
ImportMock.mockFunction(rateLimitMiddlewareModule, "rateLimitMiddleware", rateLimit({
2021-07-07 09:47:08 +02:00
skip: () => true
2021-03-07 06:21:56 +01:00
}));
// delete old test database
2021-07-05 09:14:05 +02:00
if (fs.existsSync(config.db)) fs.unlinkSync(config.db);
2021-03-07 06:21:56 +01:00
if (fs.existsSync(config.privateDB)) fs.unlinkSync(config.privateDB);
2023-02-21 04:56:38 +01:00
if (config?.redis?.enabled) await resetRedis();
if (config?.postgres) await resetPostgres();
2021-03-07 06:21:56 +01:00
await initDb();
2023-07-05 07:25:38 +02:00
const dbMode = config.postgres ? "postgres"
: "sqlite";
2021-07-12 08:43:46 +02:00
Logger.info(`Database Mode: ${dbMode}`);
2021-09-01 22:52:41 +02:00
// set commit at headCommit
(global as any).HEADCOMMIT = "test";
2021-03-07 06:21:56 +01:00
// Instantiate a Mocha instance.
const mocha = new Mocha();
2020-10-17 20:56:54 +02:00
2022-08-22 17:07:30 +02:00
const testDirs = ["./test/cases"];
2021-03-07 06:21:56 +01:00
// Add each .ts file to the mocha instance
testDirs.forEach(testDir => {
fs.readdirSync(testDir)
.filter((file) =>
// Only keep the .ts files
file.slice(-3) === ".ts"
)
.forEach(function(file) {
mocha.addFile(
path.join(testDir, file)
);
});
});
2021-03-07 06:21:56 +01:00
const mockServer = createMockServer(() => {
Logger.info("Started mock HTTP Server");
const server = createServer(() => {
Logger.info("Started main HTTP server");
// Run the tests.
mocha.run((failures) => {
mockServer.close();
server.close();
redis.quit();
2021-03-07 06:21:56 +01:00
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
process.exit();
2021-03-07 06:21:56 +01:00
});
2020-10-17 20:56:54 +02:00
});
});
2021-03-07 06:21:56 +01:00
}
init();