using System.Collections;
using CommunityToolkit.WinUI.Controls;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Automation.Peers;
using Microsoft.UI.Xaml.Automation.Provider;
using Microsoft.UI.Xaml.Controls;
using Newtonsoft.Json.Linq;
namespace XFEWarframe.Desktop.Editing;
/// Opt-in native input verification, using an isolated server and disposable local field models.
internal static class SuggestionVerification
{
internal static async Task VerifyAsync(FrameworkElement root, SuggestionProvider catalog)
{
var field = new EditorField(new JObject { ["key"] = "choice", ["label"] = "Choice verification", ["type"] = "choice", ["value"] = null,
["options"] = new JArray(new JObject { ["value"] = null, ["label"] = "Automatic" },
new JObject { ["value"] = "/A", ["label"] = "Same name" }, new JObject { ["value"] = "/B", ["label"] = "Same name" }) });
var card = EditorForm.CreateCards([field]).Single();
var choice = (AutoSuggestBox)card.Content;
var remote = new AutoSuggestBox { DisplayMemberPath = "Label" };
var catalogInput = new AutoSuggestBox { DisplayMemberPath = "Label" };
Suggestions.SetProvider(catalogInput, catalog);
var stack = new StackPanel { Spacing = 12, MinWidth = 420 };
stack.Children.Add(card); stack.Children.Add(remote); stack.Children.Add(catalogInput);
var dialog = new ContentDialog { XamlRoot = root.XamlRoot, Title = "AutoSuggest verification", Content = stack, CloseButtonText = "Close" };
var showing = dialog.ShowAsync().AsTask();
try
{
await UntilAsync(() => dialog.IsLoaded && choice.IsLoaded && catalogInput.IsLoaded, "Native suggestion inputs did not load.");
dialog.UpdateLayout(); choice.Focus(FocusState.Programmatic);
await TypeAsync(choice, "no-match");
if (!field.Changed || !field.HasUnresolvedChoice || Suggestions.GetSelectedItem(choice) is not null) throw new InvalidOperationException("Unmatched text retained a valid selection.");
try { field.Serialize(); throw new InvalidOperationException("Invalid text was serialized as the nullable automatic choice."); }
catch (InvalidOperationException ex) when (ex.Message.Contains("请选择列表中的选项", StringComparison.Ordinal)) { }
await TypeAsync(choice, "Same name");
if (!choice.IsSuggestionListOpen) throw new InvalidOperationException("Matching suggestions were not shown in the native popup.");
await SubmitAsync(choice);
if (Suggestions.GetSelectedItem(choice) is not null) throw new InvalidOperationException("An ambiguous label selected the wrong item.");
await TypeAsync(choice, "/B"); await SubmitAsync(choice);
if (field.Serialize().ToString() != "/B" || choice.Text != "Same name") throw new InvalidOperationException("Exact path selection did not preserve its identity.");
await TypeAsync(choice, "Automatic"); await SubmitAsync(choice);
if (field.Serialize().Type != JTokenType.Null || field.HasUnresolvedChoice) throw new InvalidOperationException("The valid nullable choice could not be restored.");
var oldResult = new TaskCompletionSource>(TaskCreationOptions.RunContinuationsAsynchronously);
var oldStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
Suggestions.SetProvider(remote, new(async query =>
{
if (query == "old") { oldStarted.TrySetResult(); return await oldResult.Task; }
return [new SearchSuggestion(query)];
}));
remote.Focus(FocusState.Programmatic);
await TypeAsync(remote, "old", waitResults: false); await oldStarted.Task.WaitAsync(TimeSpan.FromSeconds(5));
await TypeAsync(remote, "new");
oldResult.SetResult([new SearchSuggestion("old")]); await Task.Delay(30);
if (Results(remote).Cast().Single().Label != "new") throw new InvalidOperationException("A stale response replaced current suggestions.");
Suggestions.SetContext(remote, "another-account");
if (Results(remote).Any()) throw new InvalidOperationException("Account/category changes retained old candidates.");
Suggestions.SetProvider(remote, new(_ => Task.FromException>(new InvalidOperationException("Isolated lookup failure"))));
await TypeAsync(remote, "failure");
if (Results(remote).Any() || Suggestions.GetSearchError(remote).Length == 0) throw new InvalidOperationException("Failed lookups retained stale options or hid the failure.");
catalogInput.Focus(FocusState.Programmatic);
const string type = "/Lotus/Powersuits/Excalibur/Excalibur";
await TypeAsync(catalogInput, type);
if (!Results(catalogInput).Cast().Any(x => x.Key == type)) throw new InvalidOperationException("Native catalog suggestions did not query the real API.");
await SubmitAsync(catalogInput);
if (Suggestions.GetSelectedItem(catalogInput) is not SearchSuggestion selected || selected.Key != type) throw new InvalidOperationException("Catalog suggestion submission lost the item path.");
}
finally { dialog.Hide(); await showing; }
}
private static IEnumerable