using Autofac; using Store.Management; using Store.Management.Api; using Store.Management.Api.Shops; using Store.Management.Framework.Controls; using Store.Management.Framework.Services; using Store.Management.ViewModels; using Store.Management.Views; using System.Net; using System.Net.Http; using System.Text; using System.Text.Json; using System.Windows; using System.Windows.Controls; using System.Windows.Threading; internal static class ShopManagementChecks { public static int Run() { using ShopHandler handler = new(); using HttpClient http = new(handler); using StoreApiClient client = new(new ApiClientOptions { BaseUrl = new Uri("https://test.invalid/api/v1/") }, http); client.Auth.LoginAsync(new Store.Management.Api.Authentication.LoginRequest { UserName = "tester", Password = "password" }).GetAwaiter().GetResult(); Prism.Autofac.AutofacContainerExtension extension = new(); extension.RegisterInstance(typeof(StoreApiClient), client); extension.FinalizeExtension(); using IContainer container = extension.Instance; IShopService service = container.Resolve(); ShopManagementViewModel vm = new(service, new TestStatusBar()) { Account = " account ", ShopId = " shop-1 ", ShopName = " shop name ", Notes = " note " }; vm.SearchCommand.Execute().GetAwaiter().GetResult(); Check(handler.Path == "/api/v1/shop/GetAllShops" && handler.Authorization == "Bearer shop-test-token", "route and authentication"); Check(handler.Query!.SearchUserName == "account" && handler.Query.SearchId == "shop-1" && handler.Query.SearchName == "shop name" && handler.Query.SearchNotes == "note", "trimmed search fields"); Check(vm.Total == 101 && vm.Shops.Single().ShopAccount == "sample shop" && vm.Shops[0].GroupName == "group" && vm.Shops[0].LoginStatus == "状态:未登录" && vm.Shops[0].IsLoggedOut, "mapping and total"); vm.Account = "not submitted"; vm.PageCommand.Execute(new DataGridPageRequest(2, 50)).GetAwaiter().GetResult(); Check(handler.Query!.SearchUserName == "account" && vm.Page == 2, "paging preserves submitted filters"); vm.SearchCommand.Execute().GetAwaiter().GetResult(); Check(vm.Page == 1 && handler.Query!.SearchUserName == "not submitted", "search resets page"); vm.ResetCommand.Execute().GetAwaiter().GetResult(); Check(vm.Account == "" && vm.ShopId == "" && vm.ShopName == "" && vm.Notes == "" && handler.Query!.SearchNotes == "", "reset"); handler.Total = 1; vm.PageCommand.Execute(new DataGridPageRequest(3, 50)).GetAwaiter().GetResult(); Check(vm.Page == 1 && handler.Query!.Page == 1, "shrinking results return to valid page"); handler.Fail = true; vm.SearchCommand.Execute().GetAwaiter().GetResult(); Check(vm.Shops.Count == 0 && vm.Total == 0 && !vm.LoadingStatus.IsLoading && vm.LoadingStatus.ErrorMessage == "query failed", "failure clears stale rows and loading"); handler.Fail = false; vm.SearchCommand.Execute().GetAwaiter().GetResult(); Check(vm.Shops.Count == 1 && vm.LoadingStatus.ErrorMessage == "", "retry recovers"); handler.Total = 0; vm.SearchCommand.Execute().GetAwaiter().GetResult(); Check(vm.Total == 0 && vm.Shops.Count == 0, "empty result"); int count = handler.Count; try { client.Shops.SearchAsync(new ShopQueryRequest { Page = 0 }).GetAwaiter().GetResult(); throw new Exception("Missing page validation"); } catch (System.ComponentModel.DataAnnotations.ValidationException) { Check(handler.Count == count, "invalid page never sent"); } Check(!new Store.Management.Framework.ViewModels.ShopItemViewModel { LoginStatus = "已登录" }.IsLoggedOut && !new Store.Management.Framework.ViewModels.ShopItemViewModel().IsLoggedOut, "normal and null login status"); var cookies = service.GetCookiesAsync("shop-1").GetAwaiter().GetResult(); Check(cookies.Single().Cookies == "test-cookie" && cookies[0].ShopId == "shop-1", "cookie response"); Check(!vm.CopyCookieCommand.CanExecute(new Store.Management.Framework.ViewModels.ShopItemViewModel()), "missing shop ID disables copy"); handler.Total = 1; vm.SearchCommand.Execute().GetAwaiter().GetResult(); App app = new(); app.InitializeComponent(); app.ShutdownMode = ShutdownMode.OnExplicitShutdown; ShopManagementView view = new() { DataContext = vm }; Window window = new() { Content = view, Width = 1000, Height = 650, ShowInTaskbar = false }; try { window.Show(); app.Dispatcher.Invoke(() => { }, DispatcherPriority.ApplicationIdle); view.UpdateLayout(); Check(view.IsLoaded && view.ActualWidth > 0, "page resources and layout"); TextBox[] searchInputs = Descendants(view).Where(input => input.GetBindingExpression(TextBox.TextProperty)?.ParentBinding.Path.Path is "Account" or "ShopId" or "ShopName" or "Notes").ToArray(); Check(searchInputs.Length == 4 && searchInputs.All(input => input.Width == 180 && input.Height == 32 && input.ReadLocalValue(FrameworkElement.StyleProperty) == DependencyProperty.UnsetValue), "search inputs inherit scoped styles"); Button searchButton = Descendants