acdream/tests/AcDream.App.Tests/UI/SpewBoxControllerTests.cs
Erik bcc34ee301 feat(chat): retail text style — two-plane glyph outlines, authored SpewBox/chat styles
Campaign CH round 4, user-gate items 1+2. Root cause: retail ships a
second (background) glyph atlas per font, dilated 2px on every side,
plus two border-pixel scalars (Font.NumHorizontalBorderPixels/
NumVerticalBorderPixels) that acdream's font reader never read — so
even the pre-existing outline parameter drew almost nothing once
enabled. Landed together (either half alone is a no-op or a
regression):

- UiDatFont carries BorderX/BorderY from the DAT font resource.
- UiRenderContext.DrawStringDat inflates the background blit's source
  and destination rect by that margin and restructures into retail's
  exact two-pass whole-string outline-then-fill model
  (UIElement_Text::DrawSelf), plus the 8-neighbour +-1px fallback for
  fonts with no background atlas. Corrects the stale "property 0xd"
  comment to the real ids, 0x21 (Outline) / 0x22 (OutlineColor).
- LayoutDesc property 0x21/0x22 import (ElementInfo.Outline/
  OutlineColor, LayoutImporter.ReadState, ElementReader.Merge/
  ApplyCanonicalLegacyProjection, DatWidgetFactory.BuildText) so every
  authored-outline element across the DAT set is correct at once.
- SpewBox: RetailFontId corrected from a round-3 heuristic
  (0x40000025) to the actually-authored 0x40000001 (18px bold serif),
  Outline=true set on the controller's UiText. Fill colour stays the
  user-gate-round-1-pinned yellow — font atlases are alpha-only
  (PFID_A8), so there is no baked shading that could explain the
  screenshot's gold as anything other than the outline itself.
- Chat transcript: default fill now seeds from its authored
  ARGB(255,204,204,204) instead of an unrelated color-table slot
  (ChatTranscriptRenderer.BuildLines takes the transcript's own
  DefaultColor as a parameter); the 34-entry LogTextType table is
  untouched, and every existing CH1 conformance test stays green
  unmodified.

Regenerated the committed chat_2100006f.json fixture from the real
installed DAT, confirming end to end (not by missing-field default)
that the transcript carries no outline.

Tests: font-reader border fields + inflation math pinned against the
real DAT font, two-pass draw ordering/tint/inflation via a new
TextRenderer.DebugSpriteSegmentVerts test seam, property 0x21/0x22
import at both the ElementReader.Merge and StateDesc-property layers,
SpewBox font/outline, and the chat default-shade seed with the color
table proven untouched.

Full Release suite: 12,610 passed / 4 skipped / 0 failed
(AcDream.slnx, complete solution).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-10 19:28:34 +02:00

304 lines
13 KiB
C#

using System.Collections.Generic;
using AcDream.App.UI;
using AcDream.Core.Chat;
using AcDream.UI.Abstractions.Panels.SpewBox;
using DatReaderWriter.Types;
namespace AcDream.App.Tests.UI;
/// <summary>
/// CH2 REJECT-review rework, BLOCKER 1
/// (<c>docs/research/2026-08-09-ch2-review-findings.md</c>): these tests
/// drive the controller's frame hook (<c>UiRoot.Tick</c>, the global-message-3
/// broadcast) rather than calling <c>UiText.LinesProvider</c> directly. The
/// original tests invoked the provider straight through, bypassing the
/// <c>Visible</c> gate that <c>UiText.OnDraw</c> checks before ever polling
/// it — that is exactly why the original bug (start-invisible → provider
/// never called by drawing → no line ever visible, pending queue never
/// drains) was invisible to the original test suite.
/// </summary>
public sealed class SpewBoxControllerTests
{
[Fact]
public void Controller_IsClickThroughOverlayAndDisposesFromRetainedRoot()
{
var root = new UiRoot
{
Width = 1280f,
Height = 720f,
};
var state = new SpewBoxState();
using (var controller = new SpewBoxController(root, new SpewBoxVM(state)))
{
UiText text = Assert.IsType<UiText>(root.Children.OfType<UiText>().Single());
Assert.False(text.Visible);
Assert.True(text.ClickThrough);
Assert.Equal(int.MaxValue, text.ZOrder);
state.Enqueue("You can't jump while in the air");
// Drive the frame hook, NOT the provider — this is the tick
// that drains the pending queue in production, wired through
// UiRoot.Tick's IUiGlobalTimeListener broadcast.
root.Tick(dt: 0d, nowMs: 1000L);
UiText.Line line = Assert.Single(text.LinesProvider!());
Assert.Equal("You can't jump while in the air", line.Text);
Assert.True(text.Visible);
}
Assert.Empty(root.Children);
}
[Fact]
public void Controller_NoContent_LeavesTextHiddenWithEmptyLines()
{
var root = new UiRoot { Width = 1280f, Height = 720f };
using var controller = new SpewBoxController(root, new SpewBoxVM(new SpewBoxState()));
root.Tick(dt: 0d, nowMs: 1000L);
UiText text = Assert.IsType<UiText>(root.Children.OfType<UiText>().Single());
Assert.Empty(text.LinesProvider!());
Assert.False(text.Visible);
}
[Fact]
public void Controller_BecomesVisible_WithoutAnyDrawHavingHappenedFirst()
{
// BLOCKER 1 acceptance (a): the original bug was start-invisible →
// LinesProvider only reachable through UiElement.DrawSelfAndChildren
// (which returns early on !Visible) → provider never called → never
// visible. This test never calls anything draw-shaped — no OnDraw,
// no DrawSelfAndChildren, no UiRenderContext — only the tick.
var root = new UiRoot { Width = 1280f, Height = 720f };
var state = new SpewBoxState();
using var controller = new SpewBoxController(root, new SpewBoxVM(state));
UiText text = Assert.IsType<UiText>(root.Children.OfType<UiText>().Single());
Assert.False(text.Visible);
state.Enqueue("Out of Range!");
root.Tick(dt: 0d, nowMs: 500L);
Assert.True(text.Visible);
Assert.Equal("Out of Range!", Assert.Single(text.LinesProvider!()).Text);
}
[Fact]
public void Controller_PendingQueueDrains_WithoutAnyDrawPass()
{
// BLOCKER 1 acceptance (b): SpewBoxState.Tick is the only caller
// that drains _pending (SpewBoxState.cs's Queue<string>). Before the
// fix, that call only happened inside LinesProvider, which drawing
// gated behind Visible — so the pending queue was an unbounded
// per-session leak. Enqueue, tick the frame hook, and assert the
// underlying state actually drained (Count reflects the visible
// set, not the still-pending queue) — never touching drawing.
var root = new UiRoot { Width = 1280f, Height = 720f };
var state = new SpewBoxState();
using var controller = new SpewBoxController(root, new SpewBoxVM(state));
state.Enqueue("first");
Assert.Equal(0, state.Count); // still pending, not yet drained
root.Tick(dt: 0d, nowMs: 100L);
Assert.Equal(1, state.Count); // drained into the visible list
Assert.Equal("first", state.Snapshot()[0].Text);
}
[Fact]
public void Controller_QueueStaysBounded_AcrossManyTicksWithoutADrawPass()
{
// BLOCKER 1 acceptance (c): repeated enqueue+tick cycles must never
// let the visible set grow past SpewBoxState.MaxConcurrentItems.
// CH2 re-review nit 4 (docs/plans/2026-08-09-chat-parity-campaign.md):
// corrected — MaxConcurrentItems is the shipped LayoutDesc's
// AUTHORED value, 4, not retail's code-default of 1 (the fallback
// gmSpewBoxUI::PostInit uses only when the property is absent; see
// SpewBoxState.MaxConcurrentItems). This is what "unbounded
// per-session leak" would have looked like if the drain never ran
// at all.
var root = new UiRoot { Width = 1280f, Height = 720f };
var state = new SpewBoxState();
using var controller = new SpewBoxController(root, new SpewBoxVM(state));
for (int i = 0; i < 50; i++)
{
state.Enqueue($"line {i}");
root.Tick(dt: 0d, nowMs: 1000L + i);
Assert.True(state.Count <= SpewBoxState.MaxConcurrentItems);
}
UiText text = Assert.IsType<UiText>(root.Children.OfType<UiText>().Single());
Assert.True(text.LinesProvider!().Count <= SpewBoxState.MaxConcurrentItems);
// Newest-at-top, matching retail's InsertItem(item, 0).
Assert.Equal("line 49", text.LinesProvider!()[0].Text);
}
[Fact]
public void Controller_RenderedOrder_NewerMessageIsTheTopmostLine()
{
// CH2 re-review nit 3 (docs/plans/2026-08-09-chat-parity-campaign.md):
// pins the RENDERED order, not UiText.LinesProvider's documented
// contract (oldest-first) — SpewBoxController deliberately inverts
// that (see Tick's ordering comment) because the box is now
// top-aligned and retail shows the newest interface-text line on
// top. Drives root.Tick, not the provider, matching every other
// test in this file.
var root = new UiRoot { Width = 1280f, Height = 720f };
var state = new SpewBoxState();
using var controller = new SpewBoxController(root, new SpewBoxVM(state));
state.Enqueue("older message");
root.Tick(dt: 0d, nowMs: 1000L);
state.Enqueue("newer message");
root.Tick(dt: 0d, nowMs: 1001L);
UiText text = Assert.IsType<UiText>(root.Children.OfType<UiText>().Single());
IReadOnlyList<UiText.Line> rendered = text.LinesProvider!();
Assert.Equal(2, rendered.Count);
Assert.Equal("newer message", rendered[0].Text);
Assert.Equal("older message", rendered[1].Text);
}
[Fact]
public void Dispose_RemovesBothTheTextAndTheGlobalTimeSinkFromTheRoot()
{
var root = new UiRoot { Width = 1280f, Height = 720f };
var controller = new SpewBoxController(root, new SpewBoxVM(new SpewBoxState()));
Assert.Equal(2, root.Children.Count); // UiText + the tick sink
controller.Dispose();
Assert.Empty(root.Children);
}
// ── Campaign CH user-gate round 3 (2026-08-10), finding (a) ─────────
[Fact]
public void Construction_MountsFlushToTheViewportTop()
{
// The user reported the box "still not aligned all the way to the
// top" — TopOffset moved from the round-1 60px placeholder to 0.
var root = new UiRoot { Width = 1280f, Height = 720f };
using var controller = new SpewBoxController(root, new SpewBoxVM(new SpewBoxState()));
UiText text = Assert.IsType<UiText>(root.Children.OfType<UiText>().Single());
Assert.Equal(0f, text.Top);
}
[Fact]
public void Tick_KeepsTopFlushAndRecentersX_AcrossAResize()
{
var root = new UiRoot { Width = 1280f, Height = 720f };
var state = new SpewBoxState();
using var controller = new SpewBoxController(root, new SpewBoxVM(state));
state.Enqueue("resize me");
root.Tick(dt: 0d, nowMs: 1000L);
UiText text = Assert.IsType<UiText>(root.Children.OfType<UiText>().Single());
float widthBefore = root.Width;
Assert.Equal((widthBefore - 450f) / 2f, text.Left);
Assert.Equal(0f, text.Top);
// Simulate a window resize, then the next per-frame tick.
root.Width = 1920f;
root.Tick(dt: 0d, nowMs: 1016L);
Assert.Equal((1920f - 450f) / 2f, text.Left);
Assert.Equal(0f, text.Top); // top offset never depends on width
}
[Fact]
public void Construction_WithResolvedRetailFont_WiresDatFontOntoTheText()
{
// Campaign CH user-gate round 3: the retail dat font (id
// SpewBoxController.RetailFontId) is now actually WIRED, where
// before the controller never set DatFont/Font at all and silently
// fell through to the render context's default debug font.
var root = new UiRoot { Width = 1280f, Height = 720f };
var font = new UiDatFont(
fgTex: 1, fgW: 64, fgH: 64,
bgTex: 0, bgW: 0, bgH: 0,
lineHeight: 11f, baselineOffset: 9f,
glyphs: new Dictionary<char, FontCharDesc>());
using var controller = new SpewBoxController(
root, new SpewBoxVM(new SpewBoxState()), font, debugFont: null);
UiText text = Assert.IsType<UiText>(root.Children.OfType<UiText>().Single());
Assert.Same(font, text.DatFont);
Assert.Equal(11f, text.DatFont!.LineHeight);
}
[Fact]
public void Construction_WithoutAResolvedFont_FallsBackToTheSuppliedDebugFont()
{
// No installed-DAT font available (e.g. headless) -- the debug
// bitmap font parameter is still wired through, matching every
// other retained-UI controller's dat-font/debug-font pattern.
var root = new UiRoot { Width = 1280f, Height = 720f };
using var controller = new SpewBoxController(
root, new SpewBoxVM(new SpewBoxState()), font: null, debugFont: null);
UiText text = Assert.IsType<UiText>(root.Children.OfType<UiText>().Single());
Assert.Null(text.DatFont);
Assert.Null(text.Font);
}
// ── Campaign CH user-gate round 4 (2026-08-10) — font/outline AUTHORED ──
[Fact]
public void RetailFontId_IsTheAuthoredEighteenPixelFace_NotTheRound3Heuristic()
{
// docs/research/2026-08-10-retail-ui-text-style.md §4.2: base style
// 0x10000377 (the line template's own BaseElement) authors FontDID
// [0x40000001] directly — three independent cross-checks (FontDID,
// the 18px authored line height, and 4x18=72 = the authored box
// height) all agree. Font 0x40000025 (the round-3 "smallest
// confirmed-used font" heuristic) is retired.
Assert.Equal(0x40000001u, SpewBoxController.RetailFontId);
}
[Fact]
public void Construction_SetsOutlineTrue_MatchingTheAuthoredLineTemplate()
{
// The line template's state 0x10000002 authors property 0x21
// (Outline) = true with no authored 0x22 (OutlineColor), so the
// ctor black default applies — the heavy black border in the
// user's retail screenshot that earlier rounds never reproduced.
var root = new UiRoot { Width = 1280f, Height = 720f };
using var controller = new SpewBoxController(root, new SpewBoxVM(new SpewBoxState()));
UiText text = Assert.IsType<UiText>(root.Children.OfType<UiText>().Single());
Assert.True(text.Outline);
Assert.Equal(UiRenderContext.DefaultOutlineColor, text.OutlineColor);
}
[Fact]
public void Construction_WithTheAuthoredFont_LineHeightMatchesTheAuthoredEighteenPixelBox()
{
// Three-way cross-check from the class remarks: the resolved font's
// MaxCharHeight (18) matches the authored per-line box height (18),
// and 4 concurrent items x 18px = the authored 72px box height.
var root = new UiRoot { Width = 1280f, Height = 720f };
var font = new UiDatFont(
fgTex: 1, fgW: 64, fgH: 64,
bgTex: 2, bgW: 64, bgH: 64, // background atlas present, like the real 0x40000001
lineHeight: 18f, baselineOffset: 14f,
glyphs: new Dictionary<char, FontCharDesc>());
using var controller = new SpewBoxController(
root, new SpewBoxVM(new SpewBoxState()), font, debugFont: null);
UiText text = Assert.IsType<UiText>(root.Children.OfType<UiText>().Single());
Assert.Equal(18f, text.DatFont!.LineHeight);
}
}