Initial commit: Client Doc docs Server Tools
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
using XWorld.Framework;
|
||||
using XWorld.Server.Host;
|
||||
|
||||
namespace XWorld.Server.Host.Tests
|
||||
{
|
||||
public class RoomCtxTests
|
||||
{
|
||||
private sealed class FakeOutput : IRoomOutput
|
||||
{
|
||||
public List<NetMessage> Broadcasts = new List<NetMessage>();
|
||||
public List<(int, NetMessage)> Sends = new List<(int, NetMessage)>();
|
||||
public void Broadcast(NetMessage m) => Broadcasts.Add(m);
|
||||
public void SendTo(int playerId, NetMessage m) => Sends.Add((playerId, m));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Broadcast_And_SendTo_RouteToOutput()
|
||||
{
|
||||
var output = new FakeOutput();
|
||||
var ctx = new RoomCtx(new DeterministicRandom(1), new ManualClock(),
|
||||
new ConsoleLogger(), new InMemoryStorage(), output, _ => { });
|
||||
|
||||
ctx.Broadcast(new NetMessage(7, new byte[] { 1 }));
|
||||
ctx.SendTo(42, new NetMessage(8, new byte[] { 2 }));
|
||||
|
||||
Assert.Single(output.Broadcasts);
|
||||
Assert.Equal((ushort)7, output.Broadcasts[0].Opcode);
|
||||
Assert.Single(output.Sends);
|
||||
Assert.Equal(42, output.Sends[0].Item1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EndRoom_InvokesCallback()
|
||||
{
|
||||
bool ended = false;
|
||||
var ctx = new RoomCtx(new DeterministicRandom(1), new ManualClock(),
|
||||
new ConsoleLogger(), new InMemoryStorage(), new FakeOutput(), _ => ended = true);
|
||||
ctx.EndRoom();
|
||||
Assert.True(ended);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EndRoom_WithResult_PassesResultToCallback()
|
||||
{
|
||||
RoomEndResult seen = null;
|
||||
var ctx = new RoomCtx(new DeterministicRandom(1), new ManualClock(),
|
||||
new ConsoleLogger(), new InMemoryStorage(), new FakeOutput(), r => seen = r);
|
||||
var result = new RoomEndResult { WinnerPlayerId = 42, ResultBlob = new byte[] { 1 } };
|
||||
ctx.EndRoom(result);
|
||||
Assert.Same(result, seen);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EndRoom_Parameterless_PassesNull()
|
||||
{
|
||||
var seen = new RoomEndResult(); // 先放非 null,验证回调真的收到 null
|
||||
var ctx = new RoomCtx(new DeterministicRandom(1), new ManualClock(),
|
||||
new ConsoleLogger(), new InMemoryStorage(), new FakeOutput(), r => seen = r);
|
||||
ctx.EndRoom();
|
||||
Assert.Null(seen);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ManualClock_Advances()
|
||||
{
|
||||
var clock = new ManualClock();
|
||||
Assert.Equal(0f, clock.Now);
|
||||
clock.Advance(1.5f);
|
||||
clock.Advance(0.5f);
|
||||
Assert.Equal(2f, clock.Now);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InMemoryStorage_SaveLoad_RoundTrips_AndMissingReturnsNull()
|
||||
{
|
||||
var s = new InMemoryStorage();
|
||||
Assert.Null(s.Load("k"));
|
||||
s.Save("k", new byte[] { 9 });
|
||||
Assert.Equal(new byte[] { 9 }, s.Load("k"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user