|
| 1 | +using OpenClawTray.Chat.Markdown; |
| 2 | +using OpenClawTray.FunctionalUI; |
| 3 | +using OpenClawTray.FunctionalUI.Core; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace OpenClaw.Tray.UITests; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Regression tests for GitHub issue #636 ("Text is sometimes clipped in Chat"). |
| 10 | +/// |
| 11 | +/// The bug: list rows used to be built as a horizontal <see cref="StackElement"/> |
| 12 | +/// (<c>HStack(marker, content)</c>), which materializes to a horizontal |
| 13 | +/// <see cref="Microsoft.UI.Xaml.Controls.StackPanel"/>. A horizontal StackPanel |
| 14 | +/// measures children with infinite available width, so the paragraph's |
| 15 | +/// <see cref="Microsoft.UI.Xaml.Controls.TextBlock"/> (with |
| 16 | +/// <c>TextWrapping = Wrap</c>) never wraps — long bullets render on a single |
| 17 | +/// line and get clipped by the chat bubble's max width. |
| 18 | +/// |
| 19 | +/// The fix: each row is now a 2-column <see cref="GridElement"/> |
| 20 | +/// (<c>Auto</c> marker + <c>*</c> content). The Star column gives the content |
| 21 | +/// a finite measure width, so wrap works correctly. |
| 22 | +/// |
| 23 | +/// These assertions live at the FunctionalUI <see cref="Element"/> record level |
| 24 | +/// (no WinUI runtime needed) because that's where the regression would surface: |
| 25 | +/// any change of the row container back to a horizontal stack would fail these |
| 26 | +/// tests immediately. |
| 27 | +/// </summary> |
| 28 | +public sealed class MarkdownRendererListTests |
| 29 | +{ |
| 30 | + [Fact] |
| 31 | + public void UnorderedListRow_IsGridWithAutoMarkerAndStarContent() |
| 32 | + { |
| 33 | + const string longBullet = |
| 34 | + "- This is an unusually long bullet that must wrap across multiple " + |
| 35 | + "lines instead of being clipped at the right edge of the chat bubble."; |
| 36 | + |
| 37 | + var element = ChatMarkdownRenderer.Render(longBullet); |
| 38 | + |
| 39 | + var list = Assert.IsType<StackElement>(element); |
| 40 | + Assert.Equal(Microsoft.UI.Xaml.Controls.Orientation.Vertical, list.Orientation); |
| 41 | + Assert.Single(list.Children); |
| 42 | + |
| 43 | + var row = Assert.IsType<GridElement>(list.Children[0]!); |
| 44 | + AssertListRowShape(row); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void OrderedListRows_EachUseGridWithAutoMarkerAndStarContent() |
| 49 | + { |
| 50 | + const string ordered = "1. first item\n2. second item\n3. third item"; |
| 51 | + |
| 52 | + var element = ChatMarkdownRenderer.Render(ordered); |
| 53 | + |
| 54 | + var list = Assert.IsType<StackElement>(element); |
| 55 | + Assert.Equal(Microsoft.UI.Xaml.Controls.Orientation.Vertical, list.Orientation); |
| 56 | + Assert.Equal(3, list.Children.Count); |
| 57 | + |
| 58 | + foreach (var child in list.Children) |
| 59 | + { |
| 60 | + var row = Assert.IsType<GridElement>(child!); |
| 61 | + AssertListRowShape(row); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public void ListRow_IsNotAHorizontalStackPanel() |
| 67 | + { |
| 68 | + // Direct guard against the original regression: if anyone reverts the |
| 69 | + // row container to HStack(...), this fails with a clear message. |
| 70 | + const string bullet = "- something"; |
| 71 | + |
| 72 | + var element = ChatMarkdownRenderer.Render(bullet); |
| 73 | + |
| 74 | + var list = Assert.IsType<StackElement>(element); |
| 75 | + var row = list.Children[0]; |
| 76 | + if (row is StackElement stackRow) |
| 77 | + Assert.Fail( |
| 78 | + $"List row regressed to a {stackRow.Orientation} StackElement. " + |
| 79 | + "Issue #636 requires a GridElement so the content column gets " + |
| 80 | + "a finite measure width and TextBlock wrap works."); |
| 81 | + } |
| 82 | + |
| 83 | + private static void AssertListRowShape(GridElement row) |
| 84 | + { |
| 85 | + // Auto column for the marker, Star column for the wrapping content. |
| 86 | + Assert.Equal(new[] { "Auto", "*" }, row.Columns); |
| 87 | + Assert.Equal(new[] { "Auto" }, row.Rows); |
| 88 | + Assert.Equal(2, row.Children.Count); |
| 89 | + |
| 90 | + var marker = row.Children[0]; |
| 91 | + var content = row.Children[1]; |
| 92 | + Assert.NotNull(marker); |
| 93 | + Assert.NotNull(content); |
| 94 | + |
| 95 | + Assert.NotNull(marker!.GridPosition); |
| 96 | + Assert.Equal(0, marker.GridPosition!.Row); |
| 97 | + Assert.Equal(0, marker.GridPosition.Column); |
| 98 | + |
| 99 | + Assert.NotNull(content!.GridPosition); |
| 100 | + Assert.Equal(0, content.GridPosition!.Row); |
| 101 | + Assert.Equal(1, content.GridPosition.Column); |
| 102 | + } |
| 103 | +} |
0 commit comments