add testing infrastructure and example test (#276)

This commit is contained in:
evanpelle
2025-03-17 12:20:23 -07:00
committed by GitHub
parent bd91d7c00b
commit cd1f8b9586
15 changed files with 246 additions and 464 deletions
+38
View File
@@ -0,0 +1,38 @@
import { generateMap } from "../../src/scripts/TerrainMapGenerator";
import fs from "fs/promises";
import path from "path";
import { createGame } from "../../src/core/game/GameImpl";
import { genTerrainFromBin } from "../../src/core/game/TerrainMapLoader";
import { TestConfig } from "./TestConfig";
import { TestServerConfig } from "./TestServerConfig";
import { UserSettings } from "../../src/core/game/UserSettings";
import { Difficulty, GameType } from "../../src/core/game/Game";
export async function setup(mapName: string) {
// Load the specified map
const mapPath = path.join(__dirname, "..", "testdata", `${mapName}.png`);
const imageBuffer = await fs.readFile(mapPath);
const { map, miniMap } = await generateMap(imageBuffer);
const gameMap = await genTerrainFromBin(String.fromCharCode.apply(null, map));
const miniGameMap = await genTerrainFromBin(
String.fromCharCode.apply(null, miniMap),
);
const nationMap = { nations: [] };
// Configure the game
const serverConfig = new TestServerConfig();
const gameConfig = {
gameMap: null,
gameType: GameType.Singleplayer,
difficulty: Difficulty.Medium,
disableNPCs: false,
bots: 0,
infiniteGold: false,
infiniteTroops: false,
instantBuild: false,
};
const config = new TestConfig(serverConfig, gameConfig, new UserSettings());
// Create and return the game
return createGame(gameMap, miniGameMap, nationMap, config);
}
+3
View File
@@ -0,0 +1,3 @@
import { DefaultConfig } from "../../src/core/configuration/DefaultConfig";
export class TestConfig extends DefaultConfig {}
+57
View File
@@ -0,0 +1,57 @@
import { GameEnv, ServerConfig } from "../../src/core/configuration/Config";
import { GameMapType } from "../../src/core/game/Game";
import { GameID } from "../../src/core/Schemas";
export class TestServerConfig implements ServerConfig {
turnIntervalMs(): number {
throw new Error("Method not implemented.");
}
gameCreationRate(): number {
throw new Error("Method not implemented.");
}
lobbyMaxPlayers(map: GameMapType): number {
throw new Error("Method not implemented.");
}
discordRedirectURI(): string {
throw new Error("Method not implemented.");
}
numWorkers(): number {
throw new Error("Method not implemented.");
}
workerIndex(gameID: GameID): number {
throw new Error("Method not implemented.");
}
workerPath(gameID: GameID): string {
throw new Error("Method not implemented.");
}
workerPort(gameID: GameID): number {
throw new Error("Method not implemented.");
}
workerPortByIndex(workerID: number): number {
throw new Error("Method not implemented.");
}
env(): GameEnv {
throw new Error("Method not implemented.");
}
adminToken(): string {
throw new Error("Method not implemented.");
}
adminHeader(): string {
throw new Error("Method not implemented.");
}
gitCommit(): string {
throw new Error("Method not implemented.");
}
r2Bucket(): string {
throw new Error("Method not implemented.");
}
r2Endpoint(): string {
throw new Error("Method not implemented.");
}
r2AccessKey(): string {
throw new Error("Method not implemented.");
}
r2SecretKey(): string {
throw new Error("Method not implemented.");
}
}