Server/project/tests/CustomEnvironment.ts

54 lines
1.7 KiB
TypeScript
Raw Normal View History

import "reflect-metadata";
import { container, DependencyContainer, Lifecycle } from "tsyringe";
import { Container } from "@spt-aki/di/Container";
import { IDatabaseTables } from "@spt-aki/models/spt/server/IDatabaseTables";
import { DatabaseServer } from "@spt-aki/servers/DatabaseServer";
import { ImporterUtil } from "@spt-aki/utils/ImporterUtil";
2023-11-10 23:21:20 +01:00
import path from "node:path";
import type { Environment } from "vitest";
2023-11-10 23:21:20 +01:00
// Manually mock the logger.
import { WinstonLogger } from "@tests/__mocks__/WinstonLogger.mock";
2023-11-10 23:21:20 +01:00
export default <Environment>{
name: "spt-aki-server",
transformMode: "ssr",
async setup()
{
// Register all of the dependencies in the container.
Container.registerTypes(container);
Container.registerListTypes(container);
// Override registration to the container.
2023-11-10 23:21:20 +01:00
container.register<WinstonLogger>("WinstonLogger", WinstonLogger, {lifecycle: Lifecycle.Singleton});
// Import the database.
await importDatabase(container);
return {
async teardown()
2023-11-10 23:21:20 +01:00
{},
};
2023-11-10 23:21:20 +01:00
},
};
/**
* Reads the database JSON files and imports them into memory.
*
* @param container The dependency container.
* @returns A void promise.
*/
async function importDatabase(container: DependencyContainer): Promise<void>
{
const importerUtil = container.resolve<ImporterUtil>("ImporterUtil");
const databaseServer = container.resolve<DatabaseServer>("DatabaseServer");
// Read the data from the JSON files.
const databaseDir = path.resolve("./assets/database");
const dataToImport = await importerUtil.loadAsync<IDatabaseTables>(`${databaseDir}/`);
// Save the data to memory.
databaseServer.setTables(dataToImport);
}