using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Newtonsoft.Json.Linq; using XFEWarframe.Desktop.Editing; using XFEWarframe.Player.Profiles.CrossVersionProfiles; using XFEWarframe.Player.Views; namespace XFEWarframe.Player.Utilities.Diagnostics; internal static partial class DesktopVerification { private static async Task PollEquipmentUntilAsync(EquipmentEditorModel model, Func ready) { // An automatic timer may already be refreshing. Wait for that operation instead of asserting an intermediate state. for (var i = 0; i < 100; i++) { await model.CheckChangesAsync(); if (!model.IsBusy && ready()) return; await Task.Delay(30); } throw new InvalidOperationException($"Permission polling did not settle: busy={model.IsBusy}; tabs={model.Tabs.Count}; message={model.Message}"); } private static async Task VerifyInventoryPermissionRecoveryAsync(EditorPage page) { // Only the opt-in isolated harness has this administrative transport; normal player requests use their own token. var origin = new Uri(AppPath.Argument("verify-api")!); if (!origin.IsLoopback) throw new InvalidOperationException("Permission verification requires the isolated loopback server."); var config = JObject.Parse(await File.ReadAllTextAsync(Path.Combine(AppPath.Argument("root")!, "server.json"))); using var client = new System.Net.Http.HttpClient { BaseAddress = origin }; client.DefaultRequestHeaders.Authorization = new("Bearer", config.Value("AdminToken")!); var before = JObject.Parse(await client.GetStringAsync("control/v1/editor/permissions")); var saved = new JObject(before["fields"]!.Select(x => new JProperty(x.Value("key")!, x.Value("value")))); async Task UpdateAsync(JObject values) { var current = JObject.Parse(await client.GetStringAsync("control/v1/editor/permissions")); using var response = await client.PostAsync("control/v1/editor/permissions", new System.Net.Http.StringContent( new JObject { ["version"] = current["version"]!.DeepClone(), ["values"] = values }.ToString(), System.Text.Encoding.UTF8, "application/json")); response.EnsureSuccessStatusCode(); } var vm = page.ViewModel; var item = vm.SelectedItem!.Key; var query = vm.Query; var category = vm.Category!.Key; var notice = (InfoBar)page.FindName("PermissionNotice"); try { await UpdateAsync(new JObject(saved.Properties().Select(x => new JProperty(x.Name, false)))); await vm.CheckChangesAsync(); page.UpdateLayout(); if (vm.LastError is not null || vm.Account.EditingEnabled || !vm.HasPermissionNotice || !notice.IsOpen || vm.DetailButtonVisibility != Visibility.Collapsed || vm.Items.Any(x => x.CanEdit) || vm.Items.Count == 0) throw new InvalidOperationException("Revocation did not preserve a read-only inventory and explain the missing edit permission: " + vm.LastError); vm.EditItemCommand.Execute(null); if (!ReferenceEquals(page.Frame.Content, page)) throw new InvalidOperationException("Read-only inventory opened the equipment editor."); } finally { await UpdateAsync(saved); await vm.CheckChangesAsync(); } page.UpdateLayout(); if (vm.LastError is not null || !vm.Account.EditingEnabled || vm.HasPermissionNotice || notice.IsOpen || vm.DetailButtonVisibility != Visibility.Visible || vm.SelectedItem?.Key != item || vm.Query != query || vm.Category?.Key != category) throw new InvalidOperationException("Restoring permissions failed to restore the edit entry and selected suit without manual refresh: " + vm.LastError); } }