refactor: extract chat-input pure helpers for unit-testable submit + history math

ChatBox.SendMessage reads bytes from ValidateMessage so Encoding.UTF8.GetBytes
runs once per send. ValidateMessage takes an injectable sanitiser so xUnit can
exercise the length-equality gate without ClientStructs game memory.

CompactInputSubmitter and CompactInputHistoryNavigator lift the deterministic
parts of ChatInputBar's pop-out submit and history-up/down callback into POCO
helpers under HellionChat/_Helpers/. The ImGui buffer splice
(DeleteChars/InsertChars) stays at the call site because it needs the live
callback data.

Behavior is identical to the previous inline implementation; tests in the
local Build Suite repo pin the contracts.
This commit is contained in:
2026-05-08 08:21:13 +02:00
parent 0ed88691c2
commit 1c354d18bb
4 changed files with 146 additions and 46 deletions
+15 -2
View File
@@ -16,6 +16,18 @@ public unsafe class ChatBox
}
public static void SendMessage(string message)
{
var bytes = ValidateMessage(message);
SendMessageUnsafe(bytes);
}
// Validation split out so the deterministic checks (UTF-8 length, sanitise
// round-trip) can run in xUnit without ClientStructs game memory. The
// sanitiser is injectable so tests can pin throw behaviour without invoking
// Utf8String->SanitizeString, which only resolves in-process. Returns the
// already-encoded bytes so SendMessage doesn't pay GetBytes twice.
// TEST-MIRROR: ../../../Hellion Build test/GameFunctions/ChatBoxTests.cs
internal static byte[] ValidateMessage(string message, Func<string, string>? sanitiserOverride = null)
{
var bytes = Encoding.UTF8.GetBytes(message);
if (bytes.Length == 0)
@@ -24,10 +36,11 @@ public unsafe class ChatBox
if (bytes.Length > 500)
throw new ArgumentException(Language.ChatBox_Error_Too_Long, nameof(message));
if (message.Length != SanitiseText(message).Length)
var sanitiser = sanitiserOverride ?? SanitiseText;
if (message.Length != sanitiser(message).Length)
throw new ArgumentException(Language.ChatBox_Error_Invalid, nameof(message));
SendMessageUnsafe(bytes);
return bytes;
}
private static string SanitiseText(string text)