acdream/tests/AcDream.Core.Tests/Plugins/PluginLoaderTests.cs
Erik 0c0c042dca feat(core): add IGameState, IEvents, WorldEvents with replay-on-subscribe
Adds WorldEntitySnapshot, IGameState, IEvents abstractions; WorldEvents
implements replay-on-subscribe with per-handler exception swallowing;
WorldGameState tracks entities; AppPluginHost exposes all three; stubs
wired in Program.cs to keep build green ahead of Task 9 live wiring.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-10 20:29:29 +02:00

118 lines
3.7 KiB
C#

using AcDream.Core.Plugins;
using AcDream.Plugin.Abstractions;
namespace AcDream.Core.Tests.Plugins;
public class PluginLoaderTests
{
private static string FixturePluginPath()
{
// walk up from the test bin dir to the repo root, then into the fixture's build output
var baseDir = AppContext.BaseDirectory;
var configuration = new DirectoryInfo(baseDir).Parent!.Name; // Debug / Release
var repoRoot = FindRepoRoot(baseDir);
return Path.Combine(
repoRoot,
"tests", "AcDream.Core.Tests.Fixtures.HelloPlugin", "bin", configuration, "net10.0",
"AcDream.Core.Tests.Fixtures.HelloPlugin.dll");
}
private static string FindRepoRoot(string startDir)
{
var dir = new DirectoryInfo(startDir);
while (dir is not null && !File.Exists(Path.Combine(dir.FullName, "AcDream.slnx")))
dir = dir.Parent;
return dir?.FullName ?? throw new InvalidOperationException("repo root not found");
}
private sealed class StubHost : IPluginHost
{
public IPluginLogger Log { get; } = new StubLogger();
public IGameState State { get; } = new StubState();
public IEvents Events { get; } = new StubEvents();
}
private sealed class StubLogger : IPluginLogger
{
public void Info(string message) { }
public void Warn(string message) { }
public void Error(string message, Exception? exception = null) { }
}
private sealed class StubState : IGameState
{
public IReadOnlyList<WorldEntitySnapshot> Entities { get; } = Array.Empty<WorldEntitySnapshot>();
}
private sealed class StubEvents : IEvents
{
public event Action<WorldEntitySnapshot> EntitySpawned
{
add { }
remove { }
}
}
[Fact]
public void Load_FixtureDll_InstantiatesPluginAndCallsInitialize()
{
var dllPath = FixturePluginPath();
Assert.True(File.Exists(dllPath), $"fixture dll not found: {dllPath}");
var host = new StubHost();
var manifest = new PluginManifest(
Id: "acdream.test.hello",
DisplayName: "Hello",
Version: "0.0.1",
EntryDll: Path.GetFileName(dllPath),
ApiVersion: 1,
Dependencies: Array.Empty<string>());
var loaded = PluginLoader.Load(
pluginDirectory: Path.GetDirectoryName(dllPath)!,
manifest: manifest,
host: host);
Assert.True(loaded.Success);
Assert.NotNull(loaded.Plugin);
Assert.Equal("HelloPlugin", loaded.Plugin!.GetType().Name);
}
[Fact]
public void Load_MissingDll_ReturnsFailure()
{
var host = new StubHost();
var manifest = new PluginManifest(
Id: "x",
DisplayName: "X",
Version: "0.0.1",
EntryDll: "nope.dll",
ApiVersion: 1,
Dependencies: Array.Empty<string>());
var loaded = PluginLoader.Load("/does/not/exist", manifest, host);
Assert.False(loaded.Success);
Assert.NotNull(loaded.Error);
}
[Fact]
public void Load_DllWithNoPluginImpl_ReturnsFailure()
{
// Use AcDream.Core.dll itself — it has no IAcDreamPlugin impl
var coreDllDir = AppContext.BaseDirectory;
var host = new StubHost();
var manifest = new PluginManifest(
Id: "x",
DisplayName: "X",
Version: "0.0.1",
EntryDll: "AcDream.Core.dll",
ApiVersion: 1,
Dependencies: Array.Empty<string>());
var loaded = PluginLoader.Load(coreDllDir, manifest, host);
Assert.False(loaded.Success);
Assert.Contains("IAcDreamPlugin", loaded.Error!.Message);
}
}