using System.IO; using System.IO.Compression; using Chronos.App.Infrastructure; using Chronos.App.Settings; using Chronos.Core.Video; using Chronos.Testing; namespace Chronos.App.Tests.Windows; /// The desktop diagnostics bundle: every entry present, the saved server token and passwords never inside. [WindowsOnly] public sealed class DiagnosticsBundleTests(TestContext context) { private const string Token = "AQAAANCMnd8BFdERjHoAwE-saved-token"; [Test] public async Task TheBundleCarriesReportsAndLogsButNoSecrets() { var environment = AppEnvironment.ForCapture(); try { var settings = new AppSettings { Language = "en", ServerUrl = "https://nas.local:8443", ServerUsername = "alice", ProtectedToken = Token }; var services = new AppServices(environment, settings, () => { }, new NoDialogs()) { Encoders = new OneEncoder() }; await File.WriteAllTextAsync(Path.Combine(environment.LogDirectory, "chronos-20261002.log"), $"signed in with password={Token}\n"); var path = await DiagnosticsBundle.CreateAsync(services, context.CreateTempDirectory(), ["Self-test H.264 Test: passed"]); using var zip = ZipFile.OpenRead(path); var texts = zip.Entries.ToDictionary(e => e.FullName, e => { using var reader = new StreamReader(e.Open()); return reader.ReadToEnd(); }); Assert.SequenceEqual(new[] { "codecs.txt", "environment.txt", "jobs-failed.json", "logs/chronos-20261002.log", "settings.json" }, texts.Keys.Order(StringComparer.Ordinal)); foreach (var (name, text) in texts) Assert.False(text.Contains(Token, StringComparison.Ordinal), $"{name} leaks the token"); Assert.Contains("Self-test H.264 Test: passed", texts["environment.txt"]); Assert.Contains("Test encoder", texts["codecs.txt"]); Assert.Contains("\"ServerUsername\": \"alice\"", texts["settings.json"]); Assert.Contains("\"ProtectedToken\": \"[removed]\"", texts["settings.json"]); } finally { environment.DeleteCaptureData(); } } [Test] public async Task BundlesNeverOverwriteEachOtherAndAFailedOneLeavesNothing() { var environment = AppEnvironment.ForCapture(); try { var services = new AppServices(environment, new AppSettings { Language = "en" }, () => { }, new NoDialogs()) { Encoders = new OneEncoder() }; await File.WriteAllTextAsync(Path.Combine(environment.LogDirectory, "chronos-20261002.log"), "started\n"); var folder = context.CreateTempDirectory(); var first = await DiagnosticsBundle.CreateAsync(services, folder); var second = await DiagnosticsBundle.CreateAsync(services, folder); Assert.NotEqual(first, second); Assert.SequenceEqual(new[] { first, second }.Order(StringComparer.Ordinal), Directory.GetFiles(folder).Order(StringComparer.Ordinal)); // The encoder inventory runs after the job query and before the zip is written: cancelling there fails the // bundle half-way, once its temporary file exists. using var cts = new CancellationTokenSource(); services.Encoders = new OneEncoder(cts.Cancel); var empty = context.CreateTempDirectory(); await Assert.ThrowsAsync(() => DiagnosticsBundle.CreateAsync(services, empty, ct: cts.Token)); Assert.Empty(Directory.GetFiles(empty)); } finally { environment.DeleteCaptureData(); } } private sealed class OneEncoder(Action? onDetect = null) : IEncoderInventory { public string? Adapter() => "Test adapter"; public IReadOnlyList Detect() { onDetect?.Invoke(); return [new EncoderRow("H.264", "Test encoder", null, EncoderSource.MediaFoundation, true, false, 4096, 2304, VideoCodec.H264)]; } public Task SelfTestAsync(EncoderRow row, CancellationToken ct) => Task.FromResult(new SelfTestResult(true, 100, null)); } private sealed class NoDialogs : IAppDialogs { public string? PickFolder(string title, string? initialFolder = null) => null; public string? PickJsonFile(string title) => null; public bool Confirm(ConfirmRequest request) => false; public bool SignIn(string server, string? username, Func> signIn) => false; } }