Files
2026-07-10 10:24:29 +08:00

67 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using Xunit;
using XWorld.Framework;
using XWorld.Server.Host;
namespace XWorld.Server.Host.Tests
{
public class FrameworkVersionGateTests
{
private static readonly IReadOnlyList<PlayerInfo> Players = new[]
{
new PlayerInfo { PlayerId = 1, Name = "Human", IsAI = false },
new PlayerInfo { PlayerId = -1, Name = "AI", IsAI = true },
};
[Fact]
public void Load_GameWithinFrameworkVersion_Loads()
{
// RPS game.json has minFrameworkVersion=1 == FrameworkInfo.Version (1 > 1 is false → loads)
var module = new GameModuleLoader().Load(TestGames.Root, "rps", 1);
Assert.Equal("rps", module.GameId);
Assert.Equal(1, module.Version);
IGameServerRoom room = module.CreateRoom();
Assert.NotNull(room);
}
[Fact]
public void Load_GameRequiresNewerFramework_Rejected()
{
string tempRoot = Path.Combine(Path.GetTempPath(), "FrameworkGateTest_" + Guid.NewGuid().ToString("N"));
string tempModuleDir = Path.Combine(tempRoot, "rps", "1");
Directory.CreateDirectory(tempModuleDir);
try
{
// Copy RPS DLLs and game.json from the real TestGames fixture
string srcDir = Path.Combine(TestGames.Root, "rps", "1");
foreach (var file in Directory.GetFiles(srcDir))
File.Copy(file, Path.Combine(tempModuleDir, Path.GetFileName(file)));
// Rewrite game.json with minFrameworkVersion > current
string gameJsonPath = Path.Combine(tempModuleDir, "game.json");
string original = File.ReadAllText(gameJsonPath);
string patched = original.Replace(
$"\"minFrameworkVersion\": {FrameworkInfo.Version}",
$"\"minFrameworkVersion\": {FrameworkInfo.Version + 1}");
// Fallback: replace numeric value directly if exact string didn't match
if (patched == original)
patched = System.Text.RegularExpressions.Regex.Replace(
original,
@"""minFrameworkVersion""\s*:\s*\d+",
$"\"minFrameworkVersion\": {FrameworkInfo.Version + 1}");
File.WriteAllText(gameJsonPath, patched);
var ex = Assert.ThrowsAny<Exception>(
() => new GameModuleLoader().Load(tempRoot, "rps", 1));
Assert.IsType<InvalidOperationException>(ex);
}
finally
{
Directory.Delete(tempRoot, recursive: true);
}
}
}
}