acdream/tests/AcDream.App.Tests/UI/Layout/ChatTranscriptRendererTests.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

166 lines
7.2 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using AcDream.App.UI.Layout;
using AcDream.Core.Chat;
using AcDream.UI.Abstractions.Panels.Chat;
namespace AcDream.App.Tests.UI.Layout;
/// <summary>
/// <see cref="ChatTranscriptRenderer.WrapText"/> tests — moved here from
/// <c>ChatWindowControllerTests</c> (CH6a/b REJECT-review NIT 5: WrapText
/// itself moved off <c>ChatWindowController</c> onto
/// <see cref="ChatTranscriptRenderer"/> to close the circular dependency
/// where <see cref="ChatTranscriptRenderer.BuildLines"/> called back into
/// one of its own two consumers).
///
/// <para>
/// Campaign CH user-gate round 1, item F: <c>/help</c> (and "probably many
/// places") never split on embedded <c>'\n'</c> — the whole multi-line blob
/// rode the single early-out as ONE line. Split on <c>'\n'</c> first, then
/// word-wrap each segment; a single-segment text keeps the pre-existing
/// early-out behavior exactly.
/// </para>
/// </summary>
public class ChatTranscriptRendererTests
{
private static float MeasureByCharCount(string s) => s.Length;
[Fact]
public void WrapText_EmbeddedNewlines_ProduceOneRenderedLinePerSegment()
{
string text = "line one\nline two\nline three";
// maxW is generous — every segment fits without word-wrapping, so
// this isolates the newline-split behavior specifically.
var lines = new List<string>(ChatTranscriptRenderer.WrapText(text, 1000f, MeasureByCharCount));
Assert.Equal(new[] { "line one", "line two", "line three" }, lines);
}
[Fact]
public void WrapText_CarriageReturnNewline_NormalizesTheSameAsBareNewline()
{
string text = "line one\r\nline two";
var lines = new List<string>(ChatTranscriptRenderer.WrapText(text, 1000f, MeasureByCharCount));
Assert.Equal(new[] { "line one", "line two" }, lines);
}
[Fact]
public void WrapText_SegmentLongerThanMaxWidth_StillWordWraps()
{
// Each segment is independently word-wrapped by the SAME algorithm
// the single-line path always used — a multi-line server message
// whose second line overflows the window still wraps that line.
string text = "short\nthis segment is much too long to fit on one line";
var lines = new List<string>(ChatTranscriptRenderer.WrapText(text, 10f, MeasureByCharCount));
Assert.Equal("short", lines[0]);
Assert.True(lines.Count > 2, "the long second segment should have wrapped into multiple lines");
Assert.All(lines, line => Assert.True(MeasureByCharCount(line) <= 10f));
Assert.Equal(
"this segment is much too long to fit on one line",
string.Join(" ", lines.Skip(1)));
}
[Fact]
public void WrapText_SingleSegmentText_KeepsTheEarlyOutBehavior()
{
// No '\n' at all — the pre-existing single-line early-out path
// (whole text fits => returned verbatim as one fragment) is
// unchanged.
string text = "no newlines here";
var lines = new List<string>(ChatTranscriptRenderer.WrapText(text, 1000f, MeasureByCharCount));
Assert.Equal(new[] { text }, lines);
}
[Fact]
public void WrapText_ConsecutiveNewlines_ProduceABlankLine()
{
string text = "first\n\nthird";
var lines = new List<string>(ChatTranscriptRenderer.WrapText(text, 1000f, MeasureByCharCount));
Assert.Equal(new[] { "first", "", "third" }, lines);
}
// ── BuildLines: default-color seed (Campaign CH round 4, Fix 5) ─────────
// docs/research/2026-08-10-retail-ui-text-style.md §5.2 Fix 5: the seed for
// a line whose LogTextType falls OUTSIDE RetailChatColorTable's 34 entries
// must be the ELEMENT's own authored default fill (the transcript's
// UiText.DefaultColor), not the color table's own index-0x00 slot. Every
// line with an IN-RANGE type must still resolve its OWN exact table color —
// this parameter must never perturb that half.
private static float MeasureByWidth(string s) => s.Length;
private static readonly Vector4 OffWhite = new(0.8f, 0.8f, 0.8f, 1f); // ARGB(255,204,204,204)
[Fact]
public void BuildLines_InRangeLogTextType_AlwaysUsesItsOwnTableColor_RegardlessOfDefaultColor()
{
// The 34-entry table remains the per-message color authority — this is
// the regression guard the task explicitly calls for: an in-range type
// (0x02 Speech -> pure white) must resolve to the SAME color whether the
// caller passes the new authored off-white default or an unrelated color.
var lines = new[] { new FormattedLine("hello", ChatKind.LocalSpeech, null, LogTextType: 0x02u) };
var withOffWhiteDefault = ChatTranscriptRenderer.BuildLines(lines, 1000f, MeasureByWidth, null, OffWhite);
var withUnrelatedDefault = ChatTranscriptRenderer.BuildLines(
lines, 1000f, MeasureByWidth, null, new Vector4(0f, 1f, 0f, 1f));
Assert.True(RetailChatColorTable.TryGetColor(0x02u, out Vector4 expected));
Assert.Equal(expected, Assert.Single(withOffWhiteDefault).Color);
Assert.Equal(expected, Assert.Single(withUnrelatedDefault).Color);
}
[Fact]
public void BuildLines_OutOfRangeLogTextType_AsFirstLine_UsesTheSuppliedDefaultColor()
{
// A LogTextType >= 34 (RetailChatColorTable.Colors.Count) is the
// out-of-range carry-forward case (see RetailChatColorTable's own doc):
// as the very FIRST line, there is no prior color to carry, so the
// element's own authored default fill applies — not colorGreen (the
// color table's unrelated 0x00 "Default" slot, the pre-fix seed).
var lines = new[] { new FormattedLine("mystery", ChatKind.System, null, LogTextType: 0xFFu) };
var result = ChatTranscriptRenderer.BuildLines(lines, 1000f, MeasureByWidth, null, OffWhite);
Assert.Equal(OffWhite, Assert.Single(result).Color);
}
[Fact]
public void BuildLines_OutOfRangeLogTextType_AfterAnInRangeLine_CarriesForwardThePriorTableColor()
{
// Retail's carry-forward rule (color-table doc §3.2): an out-of-range
// type leaves m_curFontColor UNCHANGED — it inherits whatever the
// PREVIOUS line resolved to, not the seed again. This must hold
// regardless of what defaultColor is passed.
var lines = new[]
{
new FormattedLine("says hi", ChatKind.LocalSpeech, null, LogTextType: 0x03u), // Tell -> yellow
new FormattedLine("mystery", ChatKind.System, null, LogTextType: 0xFFu), // out of range
};
var result = ChatTranscriptRenderer.BuildLines(lines, 1000f, MeasureByWidth, null, OffWhite);
Assert.True(RetailChatColorTable.TryGetColor(0x03u, out Vector4 tellColor));
Assert.Equal(2, result.Count);
Assert.Equal(tellColor, result[0].Color);
Assert.Equal(tellColor, result[1].Color); // carried forward, NOT OffWhite
}
[Fact]
public void BuildLines_EmptyDetailed_ReturnsEmpty_RegardlessOfDefaultColor()
{
var result = ChatTranscriptRenderer.BuildLines(
System.Array.Empty<FormattedLine>(), 1000f, MeasureByWidth, null, OffWhite);
Assert.Empty(result);
}
}