2023-10-26 20:12:19 +02:00
|
|
|
import "reflect-metadata";
|
2023-10-27 23:48:24 +02:00
|
|
|
|
2023-11-13 17:43:37 +01:00
|
|
|
import path from "node:path";
|
2024-05-21 19:59:04 +02:00
|
|
|
import { DependencyContainer, Lifecycle, container } from "tsyringe";
|
2023-11-10 23:21:20 +01:00
|
|
|
import type { Environment } from "vitest";
|
2024-05-21 19:59:04 +02:00
|
|
|
import { Container } from "@spt/di/Container";
|
|
|
|
import { IDatabaseTables } from "@spt/models/spt/server/IDatabaseTables";
|
|
|
|
import { DatabaseServer } from "@spt/servers/DatabaseServer";
|
|
|
|
import { ImporterUtil } from "@spt/utils/ImporterUtil";
|
2023-10-26 20:12:19 +02:00
|
|
|
|
2023-11-10 23:21:20 +01:00
|
|
|
// Manually mock the logger.
|
2023-11-02 16:58:16 +01:00
|
|
|
import { WinstonLogger } from "@tests/__mocks__/WinstonLogger.mock";
|
|
|
|
|
2023-11-10 23:21:20 +01:00
|
|
|
export default <Environment>{
|
2024-05-21 19:59:04 +02:00
|
|
|
name: "spt-server",
|
2023-10-28 05:55:34 +02:00
|
|
|
transformMode: "ssr",
|
|
|
|
async setup()
|
2023-10-26 20:12:19 +02:00
|
|
|
{
|
2023-10-30 03:21:21 +01:00
|
|
|
// Register all of the dependencies in the container.
|
2023-10-28 21:39:17 +02:00
|
|
|
Container.registerTypes(container);
|
|
|
|
Container.registerListTypes(container);
|
2023-10-26 20:12:19 +02:00
|
|
|
|
2023-11-06 21:35:46 +01:00
|
|
|
// Override registration to the container.
|
2023-11-13 18:38:16 +01:00
|
|
|
container.register<WinstonLogger>("WinstonLogger", WinstonLogger, { lifecycle: Lifecycle.Singleton });
|
2023-11-02 16:58:16 +01:00
|
|
|
|
2023-10-27 23:48:24 +02:00
|
|
|
// Import the database.
|
2023-10-28 05:55:34 +02:00
|
|
|
await importDatabase(container);
|
2023-10-26 23:19:16 +02:00
|
|
|
|
2023-10-28 05:55:34 +02:00
|
|
|
return {
|
2023-10-30 03:21:21 +01:00
|
|
|
async teardown()
|
2023-11-10 23:21:20 +01:00
|
|
|
{},
|
2023-10-28 05:55:34 +02:00
|
|
|
};
|
2023-11-10 23:21:20 +01:00
|
|
|
},
|
2023-10-28 05:55:34 +02:00
|
|
|
};
|
2023-10-27 23:48:24 +02:00
|
|
|
|
2023-10-30 03:21:21 +01:00
|
|
|
/**
|
|
|
|
* Reads the database JSON files and imports them into memory.
|
|
|
|
*
|
|
|
|
* @param container The dependency container.
|
|
|
|
* @returns A void promise.
|
|
|
|
*/
|
2023-10-28 05:55:34 +02:00
|
|
|
async function importDatabase(container: DependencyContainer): Promise<void>
|
|
|
|
{
|
|
|
|
const importerUtil = container.resolve<ImporterUtil>("ImporterUtil");
|
|
|
|
const databaseServer = container.resolve<DatabaseServer>("DatabaseServer");
|
2023-10-27 23:48:24 +02:00
|
|
|
|
2023-10-28 05:55:34 +02:00
|
|
|
// Read the data from the JSON files.
|
|
|
|
const databaseDir = path.resolve("./assets/database");
|
|
|
|
const dataToImport = await importerUtil.loadAsync<IDatabaseTables>(`${databaseDir}/`);
|
2023-10-27 23:48:24 +02:00
|
|
|
|
2023-10-28 05:55:34 +02:00
|
|
|
// Save the data to memory.
|
|
|
|
databaseServer.setTables(dataToImport);
|
2023-10-26 20:12:19 +02:00
|
|
|
}
|