acdream/tests/AcDream.Core.Tests/Chat/ChatLogTests.cs
Erik ff5ed9ec0b feat(net): #18 holtburger inbound chat parity - EmoteText, SoulEmote, ServerMessage, PlayerKilled, WeenieError + Windows-1252 codec
Five sub-changes:

1. Windows-1252 codec switch (global). Every Encoding.ASCII call site
   in src/AcDream.Core.Net/Messages/ -> Encoding.GetEncoding(1252).
   Touched HearSpeech, ChatRequests, GameEvents, AppraiseInfoParser,
   CharacterList, CreateObject, PlayerDescriptionParser, SocialActions.
   New Encodings.cs module-init registers CodePagesEncodingProvider
   (System.Text.Encoding.CodePages ships with .NET 10 SDK but isn't
   auto-registered). Matches retail + holtburger; accented names
   no longer round-trip-broken.

2. New parsers (opcodes confirmed against holtburger opcodes.rs):
   - EmoteText (0x01E0)     { u32 senderGuid, string16 senderName, string16 text }
   - SoulEmote (0x01E2)     same wire layout as EmoteText
   - ServerMessage (0xF7E0) { string16 message, u32 chatType }
   - PlayerKilled (0x019E)  { string16 deathMessage, u32 victimGuid, u32 killerGuid }
   Shared StringReader.cs has the CP1252 String16L primitive.

3. WorldSession dispatch. ProcessDatagram adds branches for the four
   new top-level opcodes + fires session-level events (EmoteHeard,
   SoulEmoteHeard, ServerMessageReceived, PlayerKilledReceived).
   0x0295 SetTurbineChatChannels stubbed with TODO for parallel I.6.

4. GameEventWiring routes WeenieError + WeenieErrorWithString
   (parsers existed but were unrouted) -> chat.OnWeenieError.

5. ChatLog adapters: Emote / SoulEmote ChatKind values, OnEmote,
   OnSoulEmote, OnPlayerKilled, OnWeenieError. OnLocalSpeech now
   substitutes empty sender -> "You" per holtburger client/messages.rs.
   ChatVM.FormatEntry handles new kinds (asterisk + sender + text).

22 new tests covering parser round-trips + reject-bad-opcode +
ChatLog adapter coverage + Win-1252 round-trip with non-ASCII chars.
Solution total: 881 green (210->225 in Core.Net.Tests, 606->613 in Core.Tests).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-25 19:06:01 +02:00

175 lines
5.3 KiB
C#

using AcDream.Core.Chat;
using Xunit;
namespace AcDream.Core.Tests.Chat;
public sealed class ChatLogTests
{
[Fact]
public void OnLocalSpeech_AppendsEntry_FiresEvent()
{
var log = new ChatLog();
ChatEntry? seen = null;
log.EntryAppended += e => seen = e;
log.OnLocalSpeech("Alice", "hi", 0xAA, isRanged: false);
Assert.Equal(1, log.Count);
Assert.NotNull(seen);
Assert.Equal(ChatKind.LocalSpeech, seen!.Value.Kind);
Assert.Equal("Alice", seen.Value.Sender);
Assert.Equal("hi", seen.Value.Text);
}
[Fact]
public void OnLocalSpeech_Ranged_SetsRangedKind()
{
var log = new ChatLog();
log.OnLocalSpeech("Bob", "SHOUT", 0xBB, isRanged: true);
Assert.Equal(ChatKind.RangedSpeech, log.Snapshot()[0].Kind);
}
[Fact]
public void OnChannelBroadcast_SetsChannelId()
{
var log = new ChatLog();
log.OnChannelBroadcast(channelId: 42, sender: "Alice", text: "allegiance motd");
var e = log.Snapshot()[0];
Assert.Equal(42u, e.ChannelId);
Assert.Equal(ChatKind.Channel, e.Kind);
}
[Fact]
public void OnTellReceived_SetsTellKind()
{
var log = new ChatLog();
log.OnTellReceived("Alice", "psst", 0xAA);
Assert.Equal(ChatKind.Tell, log.Snapshot()[0].Kind);
}
[Fact]
public void OnSystemMessage_EncodesChatType_AsChannelId()
{
var log = new ChatLog();
log.OnSystemMessage("Your spell fizzled!", chatType: 5);
var e = log.Snapshot()[0];
Assert.Equal(ChatKind.System, e.Kind);
Assert.Equal(5u, e.ChannelId);
}
[Fact]
public void OnSelfSent_EchoesOutbound()
{
var log = new ChatLog();
log.OnSelfSent(ChatKind.Tell, "hey", targetOrChannel: "Alice");
var e = log.Snapshot()[0];
Assert.Equal("Alice", e.Sender);
Assert.Equal("hey", e.Text);
}
[Fact]
public void RingBuffer_DropsOldestBeyondCapacity()
{
var log = new ChatLog(maxEntries: 3);
log.OnLocalSpeech("A", "1", 0, false);
log.OnLocalSpeech("B", "2", 0, false);
log.OnLocalSpeech("C", "3", 0, false);
log.OnLocalSpeech("D", "4", 0, false);
var snap = log.Snapshot();
Assert.Equal(3, snap.Length);
Assert.Equal("2", snap[0].Text); // "1" was dropped
Assert.Equal("4", snap[2].Text);
}
[Fact]
public void Clear_EmptiesBuffer()
{
var log = new ChatLog();
log.OnLocalSpeech("A", "1", 0, false);
log.Clear();
Assert.Equal(0, log.Count);
}
// ── Phase I.5: emote / soul-emote / killed / weenie-error adapters ──
[Fact]
public void OnEmote_AppendsEmoteEntry()
{
var log = new ChatLog();
log.OnEmote("Caith", "waves at you", 0xCAFE);
var e = log.Snapshot()[0];
Assert.Equal(ChatKind.Emote, e.Kind);
Assert.Equal("Caith", e.Sender);
Assert.Equal("waves at you", e.Text);
Assert.Equal(0xCAFEu, e.SenderGuid);
}
[Fact]
public void OnSoulEmote_AppendsSoulEmoteEntry()
{
var log = new ChatLog();
log.OnSoulEmote("Bob", "dances", 0xBEEF);
var e = log.Snapshot()[0];
Assert.Equal(ChatKind.SoulEmote, e.Kind);
Assert.Equal("Bob", e.Sender);
Assert.Equal("dances", e.Text);
Assert.Equal(0xBEEFu, e.SenderGuid);
}
[Fact]
public void OnPlayerKilled_AppendsSystemEntry_StoresGuids()
{
var log = new ChatLog();
log.OnPlayerKilled("Caith was killed by a Drudge.",
victimGuid: 0x12345678u, killerGuid: 0x90ABCDEFu);
var e = log.Snapshot()[0];
Assert.Equal(ChatKind.System, e.Kind);
Assert.Equal("Caith was killed by a Drudge.", e.Text);
Assert.Equal(0x12345678u, e.SenderGuid);
Assert.Equal(0x90ABCDEFu, e.ChannelId); // killer guid stashed here
}
[Fact]
public void OnWeenieError_PlainCode_AppendsSystemEntry()
{
var log = new ChatLog();
log.OnWeenieError(errorId: 0x1234, param: null);
var e = log.Snapshot()[0];
Assert.Equal(ChatKind.System, e.Kind);
Assert.Contains("0x1234", e.Text);
Assert.Equal(0x1234u, e.ChannelId);
}
[Fact]
public void OnWeenieError_WithString_AppendsInterpolation()
{
var log = new ChatLog();
log.OnWeenieError(errorId: 0x5678, param: "Mana Stone");
var e = log.Snapshot()[0];
Assert.Equal(ChatKind.System, e.Kind);
Assert.Contains("Mana Stone", e.Text);
}
[Fact]
public void OnLocalSpeech_EmptySender_SubstitutesYou()
{
// Holtburger client/messages.rs lines 476-487 — empty sender
// means the player is the speaker (echo back of their own
// ranged shout). Substitute "You" so the chat line reads
// "You: hello" instead of ": hello".
var log = new ChatLog();
log.OnLocalSpeech(sender: "", text: "hello", senderGuid: 0, isRanged: false);
var e = log.Snapshot()[0];
Assert.Equal("You", e.Sender);
Assert.Equal("hello", e.Text);
}
[Fact]
public void OnLocalSpeech_NonEmptySender_KeepsAsIs()
{
var log = new ChatLog();
log.OnLocalSpeech(sender: "Alice", text: "hi", senderGuid: 0xAA, isRanged: false);
Assert.Equal("Alice", log.Snapshot()[0].Sender);
}
}