feat(ui+net): #16 LiveCommandBus + WorldSession.Send{Talk,Tell,Channel} + SendChatCmd wiring
Replaces NullCommandBus.Instance in PanelContext with a real LiveCommandBus when a live session is active. Panels publish SendChatCmd; the host routes it to the right wire opcode + emits a ChatLog.OnSelfSent local echo (optimistic; retail-equivalent for Talk). Pieces: - ChatChannelKind enum (UI.Abstractions) - mirrors holtburger's ChatChannelKind (references/holtburger/.../client/types.rs:35-49). - SendChatCmd record (UI.Abstractions) - (Channel, TargetName?, Text). - LiveCommandBus (UI.Abstractions) - single-handler-per-type; Register<T> throws on double-register; Publish<T> logs missing handler but does not throw. - ChannelResolver (UI.Abstractions) - port of holtburger's resolve_legacy_channel (client/commands.rs:50-62) mapping ChatChannelKind to legacy ChatChannel ids verbatim from holtburger-protocol/.../chat/types.rs:8-24 (Fellow=0x0800, AllegianceBroadcast=0x02000000, Vassals=0x1000, Patron=0x2000, Monarch=0x4000, CoVassals=0x01000000). - WorldSession.SendTalk / SendTell / SendChannel - 3-line wrappers around existing ChatRequests.Build* + SendGameAction. Internal GameActionCapture seam + InternalsVisibleTo for tests. - GameWindow registers SendChatCmd handler: Say -> SendTalk + ChatLog echo, Tell -> SendTell + echo, channel kinds -> ChannelResolver.Resolve -> SendChannel + echo. 12 new tests across SendChatCmd + LiveCommandBus + ChannelResolver + WorldSessionChat. NullCommandBus.Instance retained for back-compat when no live session. Solution total: 893 green (51 + 229 + 613). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
ff5ed9ec0b
commit
8e6e5a0b61
10 changed files with 457 additions and 1 deletions
79
tests/AcDream.Core.Net.Tests/WorldSessionChatTests.cs
Normal file
79
tests/AcDream.Core.Net.Tests/WorldSessionChatTests.cs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
using System.Net;
|
||||
using AcDream.Core.Net;
|
||||
using AcDream.Core.Net.Messages;
|
||||
|
||||
namespace AcDream.Core.Net.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Phase I.3 — verifies that <see cref="WorldSession.SendTalk"/>,
|
||||
/// <see cref="WorldSession.SendTell"/>, and <see cref="WorldSession.SendChannel"/>
|
||||
/// produce the same wire bytes that <see cref="ChatRequests"/> builders do,
|
||||
/// using a sequence number drawn from <see cref="WorldSession.NextGameActionSequence"/>.
|
||||
///
|
||||
/// <para>
|
||||
/// Uses the internal <c>GameActionCapture</c> test seam to intercept the
|
||||
/// game-action body before it hits the (unseeded) ISAAC-encrypted wire path.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public sealed class WorldSessionChatTests
|
||||
{
|
||||
private static WorldSession NewSession()
|
||||
{
|
||||
// Bind to a throwaway loopback endpoint; we never actually
|
||||
// exchange packets — the capture hook intercepts the body.
|
||||
var ep = new IPEndPoint(IPAddress.Loopback, 65000);
|
||||
return new WorldSession(ep);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SendTalk_EmitsBytesIdenticalToChatRequestsBuildTalk()
|
||||
{
|
||||
using var session = NewSession();
|
||||
byte[]? captured = null;
|
||||
session.GameActionCapture = body => captured = body;
|
||||
|
||||
session.SendTalk("hello");
|
||||
|
||||
// After SendTalk, the sequence counter has been incremented to 1.
|
||||
// ChatRequests.BuildTalk(seq=1, "hello") should match exactly.
|
||||
byte[] expected = ChatRequests.BuildTalk(1, "hello");
|
||||
Assert.NotNull(captured);
|
||||
Assert.Equal(expected, captured);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SendTell_EmitsBytesIdenticalToChatRequestsBuildTell()
|
||||
{
|
||||
using var session = NewSession();
|
||||
byte[]? captured = null;
|
||||
session.GameActionCapture = body => captured = body;
|
||||
|
||||
session.SendTell("Alice", "hey");
|
||||
|
||||
byte[] expected = ChatRequests.BuildTell(1, "Alice", "hey");
|
||||
Assert.NotNull(captured);
|
||||
Assert.Equal(expected, captured);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SendChannel_IncrementsSequence_AndMatchesBuildChatChannel()
|
||||
{
|
||||
using var session = NewSession();
|
||||
var captured = new System.Collections.Generic.List<byte[]>();
|
||||
session.GameActionCapture = body => captured.Add(body);
|
||||
|
||||
session.SendChannel(channelId: 0x00000800u, "raid plan");
|
||||
session.SendChannel(channelId: 0x02000000u, "allegiance ping");
|
||||
|
||||
Assert.Equal(2, captured.Count);
|
||||
Assert.Equal(ChatRequests.BuildChatChannel(1, 0x00000800u, "raid plan"), captured[0]);
|
||||
Assert.Equal(ChatRequests.BuildChatChannel(2, 0x02000000u, "allegiance ping"), captured[1]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SendTalk_NullText_Throws()
|
||||
{
|
||||
using var session = NewSession();
|
||||
Assert.Throws<ArgumentNullException>(() => session.SendTalk(null!));
|
||||
}
|
||||
}
|
||||
73
tests/AcDream.UI.Abstractions.Tests/LiveCommandBusTests.cs
Normal file
73
tests/AcDream.UI.Abstractions.Tests/LiveCommandBusTests.cs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
namespace AcDream.UI.Abstractions.Tests;
|
||||
|
||||
public sealed class LiveCommandBusTests
|
||||
{
|
||||
private sealed record FakeCmd(int Value);
|
||||
private sealed record OtherCmd(string Tag);
|
||||
|
||||
[Fact]
|
||||
public void Publish_InvokesRegisteredHandler_ForMatchingType()
|
||||
{
|
||||
var bus = new LiveCommandBus();
|
||||
FakeCmd? captured = null;
|
||||
bus.Register<FakeCmd>(c => captured = c);
|
||||
|
||||
bus.Publish(new FakeCmd(42));
|
||||
|
||||
Assert.NotNull(captured);
|
||||
Assert.Equal(42, captured!.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Publish_DoesNotThrow_WhenNoHandlerRegistered()
|
||||
{
|
||||
var bus = new LiveCommandBus();
|
||||
|
||||
// No handler for OtherCmd — must not throw.
|
||||
bus.Publish(new OtherCmd("x"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Register_Twice_ForSameType_Throws()
|
||||
{
|
||||
var bus = new LiveCommandBus();
|
||||
bus.Register<FakeCmd>(_ => { });
|
||||
|
||||
Assert.Throws<InvalidOperationException>(() => bus.Register<FakeCmd>(_ => { }));
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ChannelResolverTests
|
||||
{
|
||||
[Fact]
|
||||
public void Resolve_LegacyChannels_MatchesHoltburgerIds()
|
||||
{
|
||||
// Holtburger references/holtburger/crates/holtburger-protocol/src/messages/chat/types.rs:8-24
|
||||
// and holtburger-core/src/client/commands.rs:50-62. Six legacy channels
|
||||
// map to known ChatChannel ids; the rest fall through to TurbineChat.
|
||||
Assert.Equal((0x00000800u, "Fellowship"), AsTuple(ChannelResolver.Resolve(ChatChannelKind.Fellowship)));
|
||||
Assert.Equal((0x02000000u, "Allegiance"), AsTuple(ChannelResolver.Resolve(ChatChannelKind.Allegiance)));
|
||||
Assert.Equal((0x00001000u, "Vassals"), AsTuple(ChannelResolver.Resolve(ChatChannelKind.Vassals)));
|
||||
Assert.Equal((0x00002000u, "Patron"), AsTuple(ChannelResolver.Resolve(ChatChannelKind.Patron)));
|
||||
Assert.Equal((0x00004000u, "Monarch"), AsTuple(ChannelResolver.Resolve(ChatChannelKind.Monarch)));
|
||||
Assert.Equal((0x01000000u, "CoVassals"), AsTuple(ChannelResolver.Resolve(ChatChannelKind.CoVassals)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Resolve_NonLegacyChannels_ReturnsNull()
|
||||
{
|
||||
// Say + Tell are handled separately by the SendChatCmd dispatcher.
|
||||
// General/Trade/etc. require a TurbineChat channel id we don't yet wire.
|
||||
Assert.Null(ChannelResolver.Resolve(ChatChannelKind.Say));
|
||||
Assert.Null(ChannelResolver.Resolve(ChatChannelKind.Tell));
|
||||
Assert.Null(ChannelResolver.Resolve(ChatChannelKind.General));
|
||||
Assert.Null(ChannelResolver.Resolve(ChatChannelKind.Trade));
|
||||
Assert.Null(ChannelResolver.Resolve(ChatChannelKind.Unknown));
|
||||
}
|
||||
|
||||
private static (uint ChannelId, string DisplayName) AsTuple(ChannelResolver.Resolved? r)
|
||||
{
|
||||
Assert.NotNull(r);
|
||||
return (r!.Value.ChannelId, r.Value.DisplayName);
|
||||
}
|
||||
}
|
||||
33
tests/AcDream.UI.Abstractions.Tests/SendChatCmdTests.cs
Normal file
33
tests/AcDream.UI.Abstractions.Tests/SendChatCmdTests.cs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
namespace AcDream.UI.Abstractions.Tests;
|
||||
|
||||
public sealed class SendChatCmdTests
|
||||
{
|
||||
[Fact]
|
||||
public void Construct_DefaultsTargetNameToNull_ForNonTellChannels()
|
||||
{
|
||||
var cmd = new SendChatCmd(ChatChannelKind.Say, TargetName: null, Text: "hello");
|
||||
|
||||
Assert.Equal(ChatChannelKind.Say, cmd.Channel);
|
||||
Assert.Null(cmd.TargetName);
|
||||
Assert.Equal("hello", cmd.Text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equality_HoldsForRecordsWithSameValues()
|
||||
{
|
||||
var a = new SendChatCmd(ChatChannelKind.Tell, "Alice", "hi");
|
||||
var b = new SendChatCmd(ChatChannelKind.Tell, "Alice", "hi");
|
||||
|
||||
Assert.Equal(a, b);
|
||||
Assert.Equal(a.GetHashCode(), b.GetHashCode());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equality_DiffersWhenChannelDiffers()
|
||||
{
|
||||
var a = new SendChatCmd(ChatChannelKind.Fellowship, null, "raid time");
|
||||
var b = new SendChatCmd(ChatChannelKind.Allegiance, null, "raid time");
|
||||
|
||||
Assert.NotEqual(a, b);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue