|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.UI.Xaml; |
| 6 | +using Microsoft.UI.Xaml.Controls; |
| 7 | +using Microsoft.UI.Xaml.Documents; |
| 8 | +using OpenClaw.Chat; |
| 9 | +using OpenClawTray.Chat; |
| 10 | +using OpenClawTray.FunctionalUI.Hosting; |
| 11 | +using Windows.Graphics; |
| 12 | +using static OpenClawTray.FunctionalUI.Factories; |
| 13 | +using static OpenClaw.Tray.UITests.TestSupport; |
| 14 | + |
| 15 | +namespace OpenClaw.Tray.UITests; |
| 16 | + |
| 17 | +/// <summary> |
| 18 | +/// Deterministic real-runtime proof of the chat-bubble selection-scope contract |
| 19 | +/// requested during review of the "one selectable text block" change. Unlike the |
| 20 | +/// record-level <see cref="MarkdownRendererCoalesceTests"/> (which assert Element |
| 21 | +/// shape with no WinUI runtime), this test mounts a real |
| 22 | +/// <see cref="OpenClawChatTimeline"/> through the shipping FunctionalUI reconciler |
| 23 | +/// on the live STA/WinRT UI thread, renders one assistant message that mixes every |
| 24 | +/// relevant block type, and walks the realized visual tree. |
| 25 | +/// |
| 26 | +/// It pins two invariants of the shipped renderer, not a mock rendering path: |
| 27 | +/// 1. Consecutive prose paragraphs, a simple list, and a trailing paragraph all |
| 28 | +/// land in ONE <see cref="RichTextBlock"/> — a single continuous drag-select |
| 29 | +/// / copy scope. |
| 30 | +/// 2. A fenced code block and a table each remain their OWN island control, so |
| 31 | +/// their chrome survives and selection does not bleed prose text into them |
| 32 | +/// (their text is absent from the prose RichTextBlock and present in separate |
| 33 | +/// TextBlocks). |
| 34 | +/// |
| 35 | +/// The live native drag gesture itself is a human/interactive artifact; this test |
| 36 | +/// is the automated, deterministic stand-in a maintainer can run on the PR head. |
| 37 | +/// </summary> |
| 38 | +[Collection(UICollection.Name)] |
| 39 | +public sealed class ChatBubbleSelectionScopeProofTests |
| 40 | +{ |
| 41 | + private readonly UIThreadFixture _ui; |
| 42 | + |
| 43 | + public ChatBubbleSelectionScopeProofTests(UIThreadFixture ui) => _ui = ui; |
| 44 | + |
| 45 | + // One assistant message: two prose paragraphs, a three-item bullet list, a |
| 46 | + // trailing paragraph, a fenced code block, and a small table. Distinctive |
| 47 | + // NATO tokens make the tree-walk assertions unambiguous. |
| 48 | + private const string ContractMessage = |
| 49 | + "Alpha paragraph one describing the change.\n" + |
| 50 | + "\n" + |
| 51 | + "Bravo paragraph two with more detail.\n" + |
| 52 | + "\n" + |
| 53 | + "- Charlie first list item\n" + |
| 54 | + "- Delta second list item\n" + |
| 55 | + "- Echo third list item\n" + |
| 56 | + "\n" + |
| 57 | + "Foxtrot trailing paragraph after the list.\n" + |
| 58 | + "\n" + |
| 59 | + "```csharp\n" + |
| 60 | + "var golfCode = 1;\n" + |
| 61 | + "```\n" + |
| 62 | + "\n" + |
| 63 | + "| Hotel | India |\n" + |
| 64 | + "| ------ | ----- |\n" + |
| 65 | + "| Juliet | Kilo |\n"; |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public async Task AssistantMessage_ProseAndListShareOneSelectableBlock_CodeAndTableStayIslands() |
| 69 | + { |
| 70 | + await _ui.ResetContainerAsync(); |
| 71 | + |
| 72 | + var props = new OpenClawChatTimelineProps( |
| 73 | + SessionId: "ui-proof-selection-scope", |
| 74 | + Entries: new List<ChatTimelineItem> |
| 75 | + { |
| 76 | + new(Id: "assistant-contract", Kind: ChatTimelineItemKind.Assistant, Text: ContractMessage), |
| 77 | + }, |
| 78 | + HasMoreHistory: false, |
| 79 | + OnLoadMoreHistory: null, |
| 80 | + UserSenderLabel: "UI proof user", |
| 81 | + AssistantSenderLabel: "UI proof assistant", |
| 82 | + DefaultModel: "proof-model", |
| 83 | + ShowToolCalls: true, |
| 84 | + ScrollToBottomToken: 0); |
| 85 | + |
| 86 | + FunctionalHostControl? host = null; |
| 87 | + |
| 88 | + await _ui.RunOnUIAsync(() => |
| 89 | + { |
| 90 | + TestApp.EnsureFluentBrushFallbacks(Application.Current.Resources); |
| 91 | + _ui.TestWindow.AppWindow.MoveAndResize(new RectInt32(-32000, -32000, 960, 720)); |
| 92 | + _ui.Container.Width = 900; |
| 93 | + _ui.Container.Height = 640; |
| 94 | + |
| 95 | + host = new FunctionalHostControl |
| 96 | + { |
| 97 | + Width = 860, |
| 98 | + Height = 560, |
| 99 | + SuppressAutoDispose = true, |
| 100 | + }; |
| 101 | + _ui.Container.Children.Add(host); |
| 102 | + host.Mount(_ => Component<OpenClawChatTimeline, OpenClawChatTimelineProps>(props)); |
| 103 | + }); |
| 104 | + |
| 105 | + await DrainRenderQueueAsync(); |
| 106 | + await DrainRenderQueueAsync(); |
| 107 | + |
| 108 | + await _ui.RunOnUIAsync(() => |
| 109 | + { |
| 110 | + var repeater = FindLogical<ItemsRepeater>(host!).Single(); |
| 111 | + Assert.NotNull(repeater.TryGetElement(0)); |
| 112 | + |
| 113 | + var richTextTexts = CollectRichTextBlockTexts(host!); |
| 114 | + var textBlockTexts = FindDescendants<TextBlock>(host!) |
| 115 | + .Select(tb => tb.Text ?? string.Empty) |
| 116 | + .ToArray(); |
| 117 | + |
| 118 | + // (1) Prose + list + trailing paragraph coalesce into exactly ONE |
| 119 | + // RichTextBlock — one continuous selection / copy scope. |
| 120 | + var proseBlock = Assert.Single( |
| 121 | + richTextTexts, t => t.Contains("Alpha paragraph one", StringComparison.Ordinal)); |
| 122 | + |
| 123 | + Assert.Contains("Bravo paragraph two", proseBlock, StringComparison.Ordinal); |
| 124 | + Assert.Contains("Charlie first list item", proseBlock, StringComparison.Ordinal); |
| 125 | + Assert.Contains("Delta second list item", proseBlock, StringComparison.Ordinal); |
| 126 | + Assert.Contains("Echo third list item", proseBlock, StringComparison.Ordinal); |
| 127 | + Assert.Contains("Foxtrot trailing paragraph", proseBlock, StringComparison.Ordinal); |
| 128 | + |
| 129 | + // (2) The code block and table are NOT part of that prose selection |
| 130 | + // scope: their text must not appear inside the prose RichTextBlock. |
| 131 | + Assert.DoesNotContain("golfCode", proseBlock, StringComparison.Ordinal); |
| 132 | + Assert.DoesNotContain("Juliet", proseBlock, StringComparison.Ordinal); |
| 133 | + Assert.DoesNotContain("Kilo", proseBlock, StringComparison.Ordinal); |
| 134 | + |
| 135 | + // ...but they ARE rendered, as their own selectable island controls |
| 136 | + // (separate TextBlocks), so the chrome/boundary is preserved. |
| 137 | + Assert.Contains(textBlockTexts, t => t.Contains("golfCode", StringComparison.Ordinal)); |
| 138 | + Assert.Contains(textBlockTexts, t => t.Contains("Juliet", StringComparison.Ordinal)); |
| 139 | + |
| 140 | + Console.WriteLine( |
| 141 | + "CHAT_BUBBLE_SELECTION_SCOPE_PROOF " + |
| 142 | + $"proseRichTextBlocks={richTextTexts.Count(t => t.Contains("Alpha paragraph one", StringComparison.Ordinal))} " + |
| 143 | + "proseListTrailingContiguous=true " + |
| 144 | + "codeIsland=separate " + |
| 145 | + "tableIsland=separate"); |
| 146 | + }); |
| 147 | + |
| 148 | + await _ui.RunOnUIAsync(() => host!.Dispose()); |
| 149 | + } |
| 150 | + |
| 151 | + // Returns the concatenated text of each RichTextBlock separately (one string |
| 152 | + // per control), so a caller can assert which blocks share a single control. |
| 153 | + private static string[] CollectRichTextBlockTexts(DependencyObject host) |
| 154 | + { |
| 155 | + var texts = new List<string>(); |
| 156 | + foreach (var rtb in FindDescendants<RichTextBlock>(host)) |
| 157 | + { |
| 158 | + var sb = new System.Text.StringBuilder(); |
| 159 | + foreach (var block in rtb.Blocks) |
| 160 | + { |
| 161 | + if (block is Paragraph p) |
| 162 | + { |
| 163 | + foreach (var inline in p.Inlines) AppendInlineText(inline, sb); |
| 164 | + sb.Append('\n'); |
| 165 | + } |
| 166 | + } |
| 167 | + texts.Add(sb.ToString()); |
| 168 | + } |
| 169 | + return texts.ToArray(); |
| 170 | + } |
| 171 | + |
| 172 | + private static void AppendInlineText(Inline inline, System.Text.StringBuilder sb) |
| 173 | + { |
| 174 | + switch (inline) |
| 175 | + { |
| 176 | + case Run run: |
| 177 | + sb.Append(run.Text); |
| 178 | + break; |
| 179 | + case Span span: |
| 180 | + foreach (var child in span.Inlines) AppendInlineText(child, sb); |
| 181 | + break; |
| 182 | + case LineBreak: |
| 183 | + sb.Append('\n'); |
| 184 | + break; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + private async Task DrainRenderQueueAsync() |
| 189 | + { |
| 190 | + await _ui.RunOnUIAsync(() => { }); |
| 191 | + await Task.Delay(50); |
| 192 | + await _ui.RunOnUIAsync(() => _ui.Container.UpdateLayout()); |
| 193 | + await _ui.RunOnUIAsync(() => { }); |
| 194 | + } |
| 195 | +} |
0 commit comments