using System.Diagnostics; using System.IO; using System.Text.Json; using System.Windows; using XFEToolBox.Tools.BilibiliDanmaku; namespace BilibiliDanmaku.RuntimeFixture; public partial class SmokeWindow : Window { public SmokeWindow() { InitializeComponent(); Loaded += async (_, _) => { string? marker = Environment.GetEnvironmentVariable("XFE_DANMAKU_SMOKE_RESULT"); try { if (string.IsNullOrWhiteSpace(marker)) throw new InvalidOperationException("Only run using the validation harness."); var bindings = new StringWriter(); PresentationTraceSources.DataBindingSource.Listeners.Add(new TextWriterTraceListener(bindings)); PresentationTraceSources.DataBindingSource.Switch.Level = SourceLevels.Error; await Task.Delay(3000); using var vm = new MainPageViewModel(new FixtureClient(), new FixtureProfileClient(), TimeSpan.Zero); var page = new MainPage(vm); Root.Children.Add(page); vm.Input = "BV1xx411c7mD"; await vm.QueryCommand.ExecuteAsync(null); if (vm.IsBatchResolving || vm.BatchTotalCount != 2 || vm.BatchResolvedCount != 2) throw new InvalidOperationException("Runtime automatic batch resolution failed: " + vm.BatchResolutionText); if (vm.IsValidatingCandidates || vm.CandidateValidationPassed != 2 || vm.CandidateValidationCompleted != vm.CandidateValidationTotal) throw new InvalidOperationException("Runtime automatic candidate filtering failed: " + vm.CandidateValidationText); vm.Keyword = "中文"; await vm.ApplyFilterCommand.ExecuteAsync(null); if (vm.PageItems.Count != 1) throw new InvalidOperationException("Runtime filter failed: " + vm.Status); vm.SelectedItem = vm.PageItems[0]; if (!vm.ResolveSenderCommand.CanExecute(null)) throw new InvalidOperationException("Runtime midHash command disabled."); if (vm.IsBusy || vm.IsResolvingSender || !vm.SenderResolutionText.Split('\n').Contains("1000") || vm.SelectedItem.SenderText != "未公开" || !vm.CandidateUids.SequenceEqual([1000L])) throw new InvalidOperationException("Runtime midHash resolution failed: " + vm.SenderResolutionText); await vm.ProfileLoadTask; if (vm.IsLoadingProfile || vm.Profile?.Uid != 1000 || vm.Profile.Name != "运行时预览用户" || vm.DetailsTabIndex != 1 || !vm.CopyUidCommand.CanExecute(null) || !vm.CopyProfileUrlCommand.CanExecute(null) || !vm.OpenProfileCommand.CanExecute(null)) throw new InvalidOperationException("Runtime profile preview failed: " + vm.ProfileStatus); await Dispatcher.InvokeAsync(() => { }, System.Windows.Threading.DispatcherPriority.ApplicationIdle); UpdateLayout(); if (page.ActualWidth <= 0 || !vm.SelectionDetail.Contains("中文") || !vm.SelectionDetail.Contains(vm.SenderResolutionText) || page.FindName("ResolveSenderButton") is not System.Windows.Controls.Button || page.FindName("SenderResolutionLabel") is null || bindings.ToString().Length > 0) throw new InvalidOperationException("Runtime page binding failed: " + bindings); if (page.FindName("CandidatePicker") is not System.Windows.Controls.ComboBox || page.FindName("ProfileNameLabel") is not System.Windows.Controls.TextBlock name || name.Text != "运行时预览用户" || page.FindName("CopyUidButton") is not System.Windows.Controls.Button) throw new InvalidOperationException("Runtime profile controls failed."); using var export = JsonDocument.Parse(DanmakuExport.Json([vm.SelectedItem], "fixture", "complete")); var exported = export.RootElement.GetProperty("items")[0]; if (exported.GetProperty("sender").ValueKind != JsonValueKind.Null || exported.GetProperty("midHash").GetString() != "b427a317") throw new InvalidOperationException("Runtime export inferred an identity or omitted midHash."); File.WriteAllText(marker, JsonSerializer.Serialize(new { success = true, selected = vm.PageItems.Count, sender = vm.SelectedItem.SenderText, midHash = vm.SelectedItem.MidHash, batchResolved = vm.BatchResolvedCount, batchTotal = vm.BatchTotalCount, senderCandidates = vm.SenderResolutionText, profileUid = vm.Profile.Uid, profileName = vm.Profile.Name, bindings = bindings.ToString(), clr = typeof(object).Assembly.Location })); } catch (Exception e) { if (!string.IsNullOrEmpty(marker)) File.WriteAllText(marker, JsonSerializer.Serialize(new { success = false, error = e.ToString() })); Environment.ExitCode = 1; } finally { Close(); } }; } private sealed class FixtureClient : IBilibiliClient { public Task GetVideoAsync(string input, CancellationToken token) => Task.FromResult(new VideoInfo(input,"运行时验证",[new(62131,1,"P1",120)],1)); public Task GetDanmakuAsync(VideoPart part, long? fromMs, long? toMs, IProgress? progress, CancellationToken token) => Task.FromResult(new DanmakuResult([new("1",1000,"中文 🎬",1,0xffffff,1735689600,"b427a317"),new("2",2000,"test",1,0xffffff,1735689600,"a6920cf9")],1,1,false,null,0,120000)); public void Dispose() { } } private sealed class FixtureProfileClient : IBilibiliProfileClient { public Task GetProfileAsync(long uid, CancellationToken token) { token.ThrowIfCancellationRequested(); return uid is 1000 or 2000 ? Task.FromResult(new BilibiliProfile(uid, "运行时预览用户", "", "仅用于预览卡片验证的资料。", 6, 123)) : Task.FromException(new InvalidDataException("模拟账号不存在")); } public void Dispose() { } } }