acdream/tests/AcDream.Core.Tests/Chat/SpewBoxStateTests.cs
Erik e0e7888308 fix(chat): CH2 rework — SpewBox tick-driven visibility + binary-derived error table
Reworks Campaign CH slice CH2 per the REJECT-review findings doc
(docs/research/2026-08-09-ch2-review-findings.md).

BLOCKER 1 — SpewBoxController never rendered a line and leaked its
pending queue. LinesProvider only ran through UiText.OnDraw, which
gates on Visible — and the box started invisible, so the provider (the
sole caller of SpewBoxState.Tick) never ran. Gave the controller an
explicit per-frame Tick(now) driven by UiRoot's global-message-3
broadcast (a zero-size GlobalTimeSink child, the same pattern
VendorUiController.DragOverGlobalTimeSink already uses), matching
retail's gmSpewBoxUI::Update. LinesProvider now only returns the
cache. Tests rewritten to drive root.Tick(...) instead of calling the
provider directly, plus new coverage for visibility-without-a-draw,
queue-drain-without-a-draw, and bounded-queue-across-many-ticks.

BLOCKER 2 — re-derived the HandleFailureEvent routing table from the
PDB-paired binary instead of the pseudo-C's ~33-char string previews.
tools/pdb-extract/sweep_weenie_strings.py sweeps every push imm32 in
VA 0x571990-0x575480, dereferences into .rdata/.data, and decodes the
full UTF-16LE literal. Added the 5 ids dispatched via else-if (missed
by case-label enumeration), resolved 0x4F8 (previously excluded),
fixed 18 wrong strings (16 the review flagged + 2 more — 0x4E9 and
0x518 — an automated diff between every swept literal and the landed
table found). Every changed row cross-checked against ACE's
WeenieError/WeenieErrorWithString enum doc comments; both oracles
agreed on every row, including a case where the review's own proposed
text for the new 0x4E8 row was itself wrong (it was 0x4E9's text) —
corrected via the else-if block's own instruction address plus the ACE
cross-check. Pinned table count: 344 (338 + 5 + 0x4F8).

SHOULD-FIX 1 — RuntimeCommunicationState.ResetSpewBox was dead code;
folded into the ChatIdentity generation-reset stage (same lifetime
boundary), with a reset assertion added to the existing populated-reset
test.

SHOULD-FIX 2 — AddText trimmed only the trailing end and invented an
empty-string early return; retail's AddTextToScroll trims both ends
(trim(&str, 1, 1, ws)) and has no empty guard. Both retired.

SHOULD-FIX 3 — ShowWeenieError bypassed the AddText chokepoint via
ChatLog.OnWeenieError (hardcoded LogTextType 0x00); routed through
Communication.AddText(Resolve(code, param)) instead, and
ChatLog.OnWeenieError is deleted — GameEventWiring's legacy no-router
fallback now resolves + calls OnSystemMessage directly.

SHOULD-FIX 4 — retail's HandleFailureEvent switch has no default case;
an unmapped id now resolves to a null Text (silence toward the
player) instead of the invented "WeenieError 0xNNNN" hex fallback,
with a diagnostics-only console log line for the id.

NITs — AP-TBD placeholders corrected to their real register rows
(AP-178, not the unrelated AP-177 lifetime row); filed AP-180 for the
windowId dual-destination gap and corrected three stale "lands with
CH2" comments; extended SpewBoxLayoutDumpDiagnostic from dats.Portal
to dats.Local and found the SpewBox element for real — LayoutDesc
0x21000011, element 0x10000048, size 450x72, MaxConcurrentItems
(ListBox property 0x10000028) = 4, not retail's code default of 1.
AP-178 narrowed accordingly; SpewBoxState.MaxConcurrentItems and
SpewBoxController's extent/anchor/OneLine are now authored rather than
placeholder (absolute screen position and colour remain open); fixed
the "19 ids... lists 18" miscount by retiring the stale paragraph in
the class doc rewrite; aligned the UseDone handler's silent-status
check with the other two WeenieError handlers.

Full Release suite: 11,914 passed / 4 skipped / 0 failed (build 0
errors).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-09 18:14:26 +02:00

180 lines
6.1 KiB
C#

using AcDream.Core.Chat;
namespace AcDream.Core.Tests.Chat;
/// <summary>
/// Campaign CH slice CH2: unit tests for <see cref="SpewBoxState"/>, the
/// pure-state port of retail's <c>gmSpewBoxUI</c> pending/visible queue
/// split (research doc §3.1/§7.2).
/// </summary>
public sealed class SpewBoxStateTests
{
[Fact]
public void Enqueue_DoesNotBecomeVisibleUntilTick()
{
// Retail decouples enqueue (RecvNotice_DisplayFinalStringInfo) from
// display (Update, driven by UI tick global message 3) by one frame.
var state = new SpewBoxState();
state.Enqueue("You can't jump while in the air");
Assert.Equal(0, state.Count);
Assert.Empty(state.Snapshot());
}
[Fact]
public void Tick_DrainsPendingIntoVisible()
{
var state = new SpewBoxState();
state.Enqueue("You can't jump while in the air");
state.Tick(nowSeconds: 0d);
Assert.Equal(1, state.Count);
SpewBoxEntry entry = Assert.Single(state.Snapshot());
Assert.Equal("You can't jump while in the air", entry.Text);
}
[Fact]
public void Tick_InsertsNewestAtIndexZero()
{
// Retail: InsertItem(item, 0) — CH2 REJECT-review rework NIT 3
// raised MaxConcurrentItems from the code default (1) to the
// AUTHORED LayoutDesc value (4, see SpewBoxState.MaxConcurrentItems's
// own doc comment), so two entries now comfortably coexist without
// triggering the overflow rule — this test can assert the ordering
// guarantee directly instead of relying on eviction as a side effect.
var state = new SpewBoxState();
state.Enqueue("first");
state.Tick(0d);
state.Enqueue("second");
state.Tick(0d);
Assert.Equal(2, state.Count);
SpewBoxEntry[] snapshot = state.Snapshot();
Assert.Equal("second", snapshot[0].Text);
Assert.Equal("first", snapshot[1].Text);
}
[Fact]
public void Tick_IdenticalRepeat_RefreshesInPlace_DoesNotStack()
{
// Retail (0x004D5EF6-0x004D5F91): if the current item 0 has
// byte-identical text, that older item is deleted first — a
// repeated message refreshes instead of stacking a duplicate.
var state = new SpewBoxState();
state.Enqueue("You are too encumbered to carry that!");
state.Tick(0d);
state.Enqueue("You are too encumbered to carry that!");
state.Tick(1d);
Assert.Equal(1, state.Count);
SpewBoxEntry entry = Assert.Single(state.Snapshot());
Assert.Equal("You are too encumbered to carry that!", entry.Text);
// The refreshed entry carries the LATER expiry (re-inserted at t=1).
Assert.Equal(1d + SpewBoxState.DefaultLifetime.TotalSeconds, entry.ExpiresAtSeconds);
}
[Fact]
public void Tick_DifferentText_DoesNotDedupe()
{
// CH2 REJECT-review rework NIT 3: with the AUTHORED
// MaxConcurrentItems == 4, two distinct messages both fit without
// any eviction — dedupe (index-0-only) is the only thing that could
// collapse them, and it correctly does not apply to different text.
var state = new SpewBoxState();
state.Enqueue("first message");
state.Tick(0d);
state.Enqueue("second message");
state.Tick(0d);
Assert.Equal(2, state.Count);
SpewBoxEntry[] snapshot = state.Snapshot();
Assert.Equal("second message", snapshot[0].Text);
Assert.Equal("first message", snapshot[1].Text);
}
[Fact]
public void Tick_Overflow_DropsOldest_RespectingMaxConcurrentItems()
{
// CH2 REJECT-review rework NIT 3: MaxConcurrentItems is the
// AUTHORED LayoutDesc value (4), not retail's code default (1) —
// enqueue past the cap to actually exercise the overflow rule.
var state = new SpewBoxState();
Assert.Equal(4, SpewBoxState.MaxConcurrentItems);
for (int i = 0; i < SpewBoxState.MaxConcurrentItems + 1; i++)
{
state.Enqueue($"line {i}");
state.Tick(0d);
}
Assert.Equal(SpewBoxState.MaxConcurrentItems, state.Count);
SpewBoxEntry[] snapshot = state.Snapshot();
// Newest at index 0; "line 0" (the oldest) dropped by overflow.
Assert.Equal("line 4", snapshot[0].Text);
Assert.Equal("line 1", snapshot[3].Text);
Assert.DoesNotContain(snapshot, e => e.Text == "line 0");
}
[Fact]
public void Tick_PrunesExpiredEntries()
{
var state = new SpewBoxState();
state.Enqueue("fading message");
state.Tick(nowSeconds: 0d);
Assert.Equal(1, state.Count);
double justPastExpiry = SpewBoxState.DefaultLifetime.TotalSeconds + 0.001;
state.Tick(justPastExpiry);
Assert.Equal(0, state.Count);
Assert.Empty(state.Snapshot());
}
[Fact]
public void Tick_NoPendingNoExpired_RevisionUnchanged()
{
var state = new SpewBoxState();
state.Enqueue("stays visible a while");
state.Tick(0d);
long revisionAfterFirstTick = state.Revision;
// Well within the lifetime window, nothing pending — a second Tick
// should be a pure no-op.
state.Tick(0.5d);
Assert.Equal(revisionAfterFirstTick, state.Revision);
}
[Fact]
public void Reset_ClearsPendingAndVisible()
{
var state = new SpewBoxState();
state.Enqueue("pending, never ticked");
state.Enqueue("about to be visible");
state.Tick(0d);
Assert.True(state.Count > 0);
state.Reset();
Assert.Equal(0, state.Count);
Assert.Empty(state.Snapshot());
// A Reset while text was still pending (never ticked) must also
// discard the pending queue — ticking afterward shows nothing.
state.Tick(1d);
Assert.Equal(0, state.Count);
}
[Fact]
public void Revision_AdvancesOnTickThatChangesVisibleSet()
{
var state = new SpewBoxState();
long initial = state.Revision;
state.Enqueue("a line");
state.Tick(0d);
Assert.True(state.Revision > initial);
}
}