File: Testing\TestRunReporter.cs
Web Access
Project: src\src\Aspire.Cli\Aspire.Cli.csproj (aspire)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
using Aspire.Cli.Backchannel;
using Aspire.Cli.Commands;
using Aspire.Cli.Interaction;
using Aspire.Cli.Resources;
using Aspire.Cli.Utils;
using Aspire.Shared;
using Microsoft.Extensions.Logging;
using Spectre.Console;
 
namespace Aspire.Cli.Testing;
 
internal sealed class TestRunReporter
{
    private const UnixFileMode FileModeBits = UnixFileMode.UserRead | UnixFileMode.UserWrite;
    private static readonly string[] s_terminalStates = ["Finished", "Exited", "FailedToStart", "RuntimeUnhealthy"];
    private readonly CliExecutionContext _executionContext;
    private readonly IInteractionService _interaction;
    private readonly bool _interactive;
    private readonly OutputFormat _format;
    private readonly TimeProvider _timeProvider;
    private readonly ILogger _logger;
    private readonly Dictionary<string, TestResourceState> _resources = new(StringComparers.ResourceName);
    private readonly List<PublishedTestResult> _results = [];
    private readonly Dictionary<string, TestCollectionState> _collections = new(StringComparers.ResourceName);
    private readonly Dictionary<string, (string State, int? ExitCode)> _displayedResourceStates = new(StringComparers.ResourceName);
    private readonly HashSet<string> _displayedResourceSummaries = new(StringComparers.ResourceName);
    private readonly DateTimeOffset _startedAt;
    private readonly object _gate = new();
    private bool _completed;
    private bool _summaryDisplayed;
    private DateTimeOffset? _completedAt;
    private TestRunSummary? _lastSummary;
    private bool? _completedOutcome;
    public bool JsonDisplayed { get; private set; }
 
    public string OutputPath { get; }
    public string SummaryPath => Path.Combine(OutputPath, "summary.json");
 
    private TestRunReporter(CliExecutionContext executionContext, IInteractionService interaction,
        bool interactive, OutputFormat format, TimeProvider timeProvider, ILogger logger, string outputPath)
    {
        _executionContext = executionContext;
        _interaction = interaction;
        _interactive = interactive && format != OutputFormat.Json;
        _format = format;
        _timeProvider = timeProvider;
        _logger = logger;
        _startedAt = timeProvider.GetUtcNow();
        OutputPath = outputPath;
    }
 
    public static TestRunReporter Create(CliExecutionContext executionContext, IInteractionService interaction,
        bool interactive, OutputFormat format, TimeProvider timeProvider, ILogger logger)
    {
        var parent = Path.Combine(executionContext.AspireHomeDirectory.FullName, "testruns");
        ValidateAncestors(parent, executionContext);
        CreatePrivateDirectory(parent, repairExisting: true, executionContext);
 
        // Reserve the name exclusively before creating its directory. Other invocations follow
        // the same protocol; an existing directory is never adopted as a new test run.
        var id = $"{timeProvider.GetUtcNow():yyyyMMddTHHmmssfffZ}-{Guid.NewGuid():N}";
        var reservation = Path.Combine(parent, $".{id}.reserve");
        var outputPath = Path.Combine(parent, id);
        var reserved = CreateFile(reservation);
        try
        {
            if (Directory.Exists(outputPath) || new DirectoryInfo(outputPath).LinkTarget is not null)
            {
                throw new IOException($"The test output directory '{outputPath}' already exists.");
            }
            // Only a new, reserved path reaches this call. Reuse DirectoryHelper through
            // SocketPermissionHelper to restore owner access removed by umask; later writes
            // still validate the existing directory without repairing its permissions.
            CreatePrivateDirectory(outputPath, repairExisting: true, executionContext);
        }
        finally
        {
            reserved.Dispose();
            File.Delete(reservation);
        }
 
        var reporter = new TestRunReporter(executionContext, interaction, interactive, format, timeProvider, logger, outputPath);
        reporter.Persist("running", exitCode: null, error: null);
        if (format != OutputFormat.Json)
        {
            interaction.DisplayMessage(KnownEmojis.Microscope, TestCommandStrings.RunningHeader);
            interaction.DisplaySubtleMessage(string.Format(CultureInfo.CurrentCulture,
                TestCommandStrings.OutputDirectory, MarkupHelpers.SafeFileLink(interaction, outputPath)), allowMarkup: true);
        }
 
        return reporter;
    }
 
    public async Task<bool> WaitForCompletionAsync(IAppHostCliBackchannel backchannel, CancellationToken cancellationToken)
    {
        bool? passed = null;
        await foreach (var item in backchannel.WatchTestRunAsync(cancellationToken).ConfigureAwait(false))
        {
            if (passed is not null)
            {
                throw new InvalidOperationException("The test stream continued after aggregate completion.");
            }
 
            if ((item.Resource is null ? 0 : 1) + (item.Result is null ? 0 : 1) +
                (item.Collection is null ? 0 : 1) + (item.Passed is null ? 0 : 1) != 1)
            {
                throw new InvalidOperationException("The AppHost sent an invalid test resource event.");
            }
            if (item.Resource is { } resource)
            {
                Update(resource);
            }
            else if (item.Result is { } testResult)
            {
                UpdateResult(testResult);
            }
            else if (item.Collection is { } collection)
            {
                UpdateCollection(collection);
            }
            else if (item.Passed is { } result)
            {
                passed = result;
            }
            else
            {
                throw new InvalidOperationException("The AppHost sent an invalid test resource event.");
            }
        }
 
        lock (_gate)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (passed is true && (_resources.Count == 0 || _resources.Values.Any(r => r.Outcome != "passed") ||
                _results.Any(r => r.IsFailure()) ||
                _collections.Values.Any(c => c.Status is not ("completed" or "notConfigured")) ||
                _resources.Values.GroupBy(r => r.ResourceName, StringComparers.ResourceName)
                    .Any(group => group.Count() != group.First().ExpectedReplicas ||
                        group.Any(r => r.ExpectedReplicas != group.First().ExpectedReplicas))))
            {
                throw new InvalidOperationException("The AppHost reported success without complete passing test resource outcomes.");
            }
 
            return passed ?? throw new InvalidOperationException("The test stream ended without an aggregate outcome.");
        }
    }
 
    private void Update(TestResourceState resource)
    {
        lock (_gate)
        {
            // Cancellation/early AppHost exit may race an in-flight RPC frame. Freeze the report
            // before finalization so late updates cannot overwrite a cancelled/failed summary.
            if (_completed)
            {
                _logger.LogDebug("Ignoring test resource update after finalizing the run.");
                return;
            }
            UpdateCore(resource);
        }
    }
 
    private void UpdateResult(PublishedTestResult result)
    {
        lock (_gate)
        {
            if (_completed)
            {
                _logger.LogDebug("Ignoring test result after finalizing the run.");
                return;
            }
            ArgumentException.ThrowIfNullOrWhiteSpace(result.Name);
            var failed = result.IsFailure();
            if (result.DurationMilliseconds is { } duration && (!double.IsFinite(duration) || duration < 0) ||
                !_resources.Values.Any(r => StringComparers.ResourceName.Equals(r.ResourceName, result.ResourceName)))
            {
                throw new InvalidOperationException("The AppHost sent an invalid published test result.");
            }
            _results.Add(result);
            Persist("running", exitCode: null, error: null);
            if (_interactive)
            {
                _interaction.DisplayMessage(KnownEmojis.Microscope, string.Format(CultureInfo.CurrentCulture,
                    TestCommandStrings.PublishedResult, FormatResourceName(result.ResourceName),
                    $"[blue]{result.Name.EscapeMarkup()}[/]", FormatOutcome(result.Outcome, failed ? "red" : null)), allowMarkup: true);
            }
        }
    }
 
    private void UpdateCollection(TestCollectionState collection)
    {
        lock (_gate)
        {
            if (_completed)
            {
                _logger.LogDebug("Ignoring test collection update after finalizing the run.");
                return;
            }
            if (collection.CollectorCount < 0 ||
                collection.Status is not ("notConfigured" or "running" or "completed" or "failed" or "cancelled") ||
                collection.Status == "notConfigured" && collection.CollectorCount != 0 ||
                !_resources.Values.Any(r => StringComparers.ResourceName.Equals(r.ResourceName, collection.ResourceName)))
            {
                throw new InvalidOperationException("The AppHost sent an invalid test collection state.");
            }
            _collections[collection.ResourceName] = collection;
            Persist("running", exitCode: null, error: null);
            if (_interactive && collection.Error is { } error)
            {
                _interaction.DisplayError(error);
            }
            DisplayResourceSummary(collection.ResourceName);
        }
    }
 
    private void UpdateCore(TestResourceState resource)
    {
        ArgumentException.ThrowIfNullOrEmpty(resource.ResourceName);
        if (resource.ExpectedReplicas < 1 || resource.Outcome is not ("pending" or "passed" or "failed") ||
            resource.Outcome != "pending" && string.IsNullOrEmpty(resource.ResourceId) ||
            resource.Outcome == "passed" && (resource.ExitCode != 0 ||
                !StringComparers.ResourceState.Equals(resource.State, "Finished") &&
                !StringComparers.ResourceState.Equals(resource.State, "Exited")))
        {
            throw new InvalidOperationException("The AppHost sent an invalid test resource outcome.");
        }
        if (resource.ResourceId is null && _resources.Values.Any(r =>
            StringComparers.ResourceName.Equals(r.ResourceName, resource.ResourceName) && r.ResourceId is not null))
        {
            // A late model placeholder must not reappear as an unfinished extra replica.
            return;
        }
 
        var key = Key(resource.ResourceName, resource.ResourceId);
        if (_resources.TryGetValue(key, out var previous))
        {
            // A final outcome is authoritative even if the independent observation stream
            // queues an older/pending snapshot after the completion waiter publishes it.
            if (previous == resource || previous.Outcome != "pending" && resource.Outcome == "pending")
            {
                return;
            }
            if (previous.Outcome != "pending" && resource.Outcome != "pending")
            {
                throw new InvalidOperationException("The AppHost sent conflicting final test resource outcomes.");
            }
        }
 
        if (resource.ResourceId is not null)
        {
            _resources.Remove(Key(resource.ResourceName, resourceId: null));
        }
        _resources[key] = resource;
        Persist("running", exitCode: null, error: null);
        if (_interactive)
        {
            DisplayResourceState(resource);
            DisplayResourceSummary(resource.ResourceName);
        }
    }
 
    private void DisplayResourceState(TestResourceState resource)
    {
        if (string.IsNullOrWhiteSpace(resource.State))
        {
            return;
        }
 
        // Model placeholders and single runtime replicas describe the same logical resource.
        // Outcome resolution can also replay a terminal snapshot without a process transition.
        var identity = resource.ExpectedReplicas == 1 ? resource.ResourceName : resource.ResourceId ?? resource.ResourceName;
        var terminal = IsTerminal(resource);
        var state = (State: resource.State, ExitCode: terminal ? resource.ExitCode : null);
        if (_displayedResourceStates.TryGetValue(identity, out var previous) &&
            StringComparers.ResourceState.Equals(previous.State, state.State) && previous.ExitCode == state.ExitCode)
        {
            return;
        }
        _displayedResourceStates[identity] = state;
        var name = resource.ExpectedReplicas > 1 && resource.ResourceId is not null
            ? $"{resource.ResourceName} ({resource.ResourceId})"
            : resource.ResourceName;
        var message = terminal
            ? string.Format(CultureInfo.CurrentCulture, TestCommandStrings.TerminalResourceStatus,
                FormatResourceName(name), resource.State.EscapeMarkup(),
                resource.ExitCode?.ToString(CultureInfo.CurrentCulture) ?? TestCommandStrings.UnknownExitCode)
            : string.Format(CultureInfo.CurrentCulture, TestCommandStrings.ResourceStatus,
                FormatResourceName(name), resource.State.EscapeMarkup());
        _interaction.DisplayMessage(KnownEmojis.Gear, message, allowMarkup: true);
    }
 
    private void DisplayResourceSummary(string resourceName, bool finalizing = false)
    {
        if (!_interactive || _displayedResourceSummaries.Contains(resourceName))
        {
            return;
        }
        var results = _results.Where(r => StringComparers.ResourceName.Equals(r.ResourceName, resourceName)).ToArray();
        if (results.Length == 0)
        {
            return;
        }
        var resources = _resources.Values.Where(r => StringComparers.ResourceName.Equals(r.ResourceName, resourceName)).ToArray();
        var resourceComplete = resources.Length == resources[0].ExpectedReplicas &&
            resources.All(r => r.Outcome is "passed" or "failed");
        var collectionComplete = _collections.TryGetValue(resourceName, out var collection) &&
            collection.Status is "completed" or "failed";
        if (!finalizing && (!resourceComplete || !collectionComplete))
        {
            return;
        }
 
        // Process completion precedes artifact harvesting. Counts are final only after both
        // lifetimes finish; interruption instead reports the actual results published so far.
        var complete = resourceComplete && collectionComplete;
        var outcome = !complete ? "incomplete"
            : resources.Any(r => r.Outcome == "failed") || collection!.Status == "failed" ||
                results.Any(r => r.IsFailure()) ? "failed" : "passed";
        _interaction.DisplayMessage(KnownEmojis.BarChart, string.Format(CultureInfo.CurrentCulture,
            complete ? TestCommandStrings.ResourceResults : TestCommandStrings.PartialResourceResults,
            FormatResourceName(resourceName), FormatOutcome(outcome),
            FormatCount(results.Count(r => r.Outcome == "passed"), "green"),
            FormatCount(results.Count(r => r.IsFailure()), "red"),
            FormatCount(results.Count(r => r.Outcome == "skipped"), "yellow")), allowMarkup: true);
        _displayedResourceSummaries.Add(resourceName);
    }
 
    private static bool IsTerminal(TestResourceState resource) =>
        resource.Outcome is "passed" or "failed" ||
        s_terminalStates.Contains(resource.State, StringComparers.ResourceState);
 
    private static string FormatResourceName(string name) => $"[teal]{name.EscapeMarkup()}[/]";
 
    private static string FormatCount(int count, string color) => $"[{color}]{count.ToString(CultureInfo.CurrentCulture)}[/]";
 
    private static string FormatOutcome(string outcome, string? color = null)
    {
        color ??= outcome switch
        {
            "passed" => "green",
            "failed" => "red",
            _ => "yellow"
        };
        return $"[{color}]{DisplayOutcome(outcome).EscapeMarkup()}[/]";
    }
 
    public void Complete(CommandResult result)
    {
        lock (_gate)
        {
            _completed = true;
            _completedAt ??= _timeProvider.GetUtcNow();
            CompleteCore(result);
        }
    }
 
    private void CompleteCore(CommandResult result)
    {
        var status = StatusFor(result);
        foreach (var (key, resource) in _resources.ToArray())
        {
            if (resource.Outcome == "pending")
            {
                _resources[key] = resource with { Outcome = "incomplete" };
            }
        }
        foreach (var (key, collection) in _collections.ToArray())
        {
            if (collection.Status == "running")
            {
                _collections[key] = collection with { Status = "incomplete" };
            }
        }
        Persist(status, result.ExitCode, result.ErrorMessage);
        DisplaySummary(status);
    }
 
    public void ReportCompletedResources(bool passed)
    {
        lock (_gate)
        {
            _completed = true;
            _completedOutcome = passed;
            _completedAt ??= _timeProvider.GetUtcNow();
            // Resources and collectors are complete, but a keep-alive CLI has not exited yet.
            Persist(passed ? "passed" : "failed", exitCode: null, error: null);
            DisplaySummary(passed ? "passed" : "failed");
        }
    }
 
    private void DisplaySummary(string status)
    {
        if (_summaryDisplayed)
        {
            return;
        }
        if (_format == OutputFormat.Json)
        {
            WriteJson(_lastSummary ?? throw new InvalidOperationException("The test summary has not been persisted."));
            _summaryDisplayed = true;
            return;
        }
        if (_interactive)
        {
            foreach (var resourceName in _results.Select(r => r.ResourceName).Distinct(StringComparers.ResourceName))
            {
                DisplayResourceSummary(resourceName, finalizing: true);
            }
            _interaction.DisplayMessage(KnownEmojis.Microscope, string.Format(CultureInfo.CurrentCulture,
                TestCommandStrings.SummaryHeader, FormatOutcome(status)), allowMarkup: true);
            if (_results.Count > 0)
            {
                var totals = ResultSummary(status);
                _interaction.DisplayMessage(KnownEmojis.Microscope, string.Format(CultureInfo.CurrentCulture,
                    TestCommandStrings.PublishedSummary, FormatCount(totals.Passed, "green"),
                    FormatCount(totals.Failed, "red"), FormatCount(totals.Skipped, "yellow")), allowMarkup: true);
            }
        }
 
        _interaction.DisplaySubtleMessage(string.Format(CultureInfo.CurrentCulture,
            TestCommandStrings.SummaryFile, MarkupHelpers.SafeFileLink(_interaction, SummaryPath)), allowMarkup: true);
        _summaryDisplayed = true;
    }
 
    private void Persist(string status, int? exitCode, string? error)
    {
        ValidateAncestors(OutputPath, _executionContext);
        CreatePrivateDirectory(OutputPath, repairExisting: false, _executionContext);
        if (new FileInfo(SummaryPath).LinkTarget is not null)
        {
            throw new IOException($"The test summary '{SummaryPath}' must not be a symbolic link.");
        }
 
        var summary = CreateSummary(status, exitCode, error);
        var temporaryPath = Path.Combine(OutputPath, $".summary-{Guid.NewGuid():N}.tmp");
        try
        {
            using (var stream = CreateFile(temporaryPath))
            {
                JsonSerializer.Serialize(stream, summary, TestRunJsonContext.Default.TestRunSummary);
                stream.Flush(flushToDisk: true);
            }
            // Replace the directory entry, never truncate an existing file or follow its link.
            // Readers see either the previous complete summary or this complete summary.
            File.Move(temporaryPath, SummaryPath, overwrite: true);
            _lastSummary = summary;
        }
        finally
        {
            try
            {
                File.Delete(temporaryPath);
            }
            catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
            {
                _logger.LogWarning(ex, "Could not clean up temporary test summary {Path}.", temporaryPath);
            }
        }
    }
 
    public void WriteJsonResult(CommandResult result)
    {
        lock (_gate)
        {
            _completedAt ??= _timeProvider.GetUtcNow();
            WriteJson(CreateSummary(StatusFor(result), result.ExitCode, result.ErrorMessage));
        }
    }
 
    private string StatusFor(CommandResult result) => result.ExitCode switch
    {
        CliExitCodes.Success => "passed",
        CliExitCodes.Cancelled => "cancelled",
        // Inspection expiry does not undo completed resource/result collection.
        CliExitCodes.WaitTimeout when _completedOutcome is { } passed => passed ? "passed" : "failed",
        CliExitCodes.WaitTimeout => "timedOut",
        _ => "failed"
    };
 
    private void WriteJson(TestRunSummary summary)
    {
        if (JsonDisplayed)
        {
            return;
        }
        _interaction.DisplayRawText(JsonSerializer.Serialize(summary, TestRunJsonContext.Default.TestRunSummary), ConsoleOutput.Standard);
        JsonDisplayed = true;
    }
 
    private TestRunSummary CreateSummary(string status, int? exitCode, string? error) => new()
    {
        RunId = Path.GetFileName(OutputPath),
        OutputPath = OutputPath,
        SummaryPath = SummaryPath,
        StartedAt = _startedAt,
        CompletedAt = _completedAt,
        CliExitCode = exitCode,
        Error = error,
        TestResults = ResourceReports(),
        TestSummary = ResultSummary(status)
    };
 
    private TestSummary ResultSummary(string outcome) => new()
    {
        Outcome = outcome,
        Passed = _results.Count(r => r.Outcome == "passed"),
        Failed = _results.Count(r => r.IsFailure()),
        Skipped = _results.Count(r => r.Outcome == "skipped")
    };
 
    private Dictionary<string, TestResourceReport> ResourceReports() => _resources.Values
        .GroupBy(r => r.ResourceName, StringComparers.ResourceName)
        .OrderBy(group => group.Key, StringComparers.ResourceName)
        .ToDictionary(group => group.Key, group =>
        {
            var replicas = group.OrderBy(r => r.ResourceId, StringComparers.ResourceName).ToArray();
            var expectedReplicas = replicas[0].ExpectedReplicas;
            var single = expectedReplicas == 1 && replicas.Length == 1 ? replicas[0] : null;
            _collections.TryGetValue(group.Key, out var collection);
            var outcome = replicas.Any(r => r.Outcome == "failed") ? "failed"
                : replicas.Length == expectedReplicas && replicas.All(r => r.Outcome == "passed") ? "passed"
                : _completed || replicas.Any(r => r.Outcome == "incomplete") ? "incomplete" : "pending";
            // Different replica exits have no single authoritative exit code. Keep every
            // instance below rather than choose one code or let a name-keyed entry overwrite it.
            var exitCodes = replicas.Select(r => r.ExitCode).Distinct().ToArray();
            return new TestResourceReport
            {
                ResourceId = single?.ResourceId,
                State = replicas.All(r => StringComparers.ResourceState.Equals(r.State, replicas[0].State)) ? replicas[0].State : null,
                ExitCode = replicas.Length == expectedReplicas && exitCodes.Length == 1 ? exitCodes[0] : null,
                Outcome = outcome,
                ExpectedReplicas = expectedReplicas,
                CollectorStatus = collection?.Status,
                CollectorError = collection?.Error,
                OutputPath = collection?.OutputPath,
                TestCases = _results.Where(r => StringComparers.ResourceName.Equals(r.ResourceName, group.Key))
                    .Select(r => new TestCaseReport
                    {
                        Name = r.Name,
                        Outcome = r.Outcome,
                        DurationMilliseconds = r.DurationMilliseconds,
                        Message = r.Message
                    }).ToArray(),
                Replicas = single is null
                    ? replicas.Where(r => r.ResourceId is not null).ToDictionary(r => r.ResourceId!, r => new TestReplicaReport
                    {
                        State = r.State,
                        ExitCode = r.ExitCode,
                        Outcome = r.Outcome
                    }, StringComparer.Ordinal)
                    : null
            };
        }, StringComparers.ResourceName);
 
    private static string Key(string resourceName, string? resourceId) => $"{resourceName}\0{resourceId}";
 
    private static string DisplayOutcome(string outcome) => outcome switch
    {
        "passed" => TestCommandStrings.Passed,
        "failed" => TestCommandStrings.Failed,
        "cancelled" => TestCommandStrings.Cancelled,
        "timedOut" => TestCommandStrings.TimedOut,
        "incomplete" => TestCommandStrings.Incomplete,
        "skipped" => TestCommandStrings.Skipped,
        "inconclusive" => TestCommandStrings.Inconclusive,
        "error" => TestCommandStrings.Error,
        _ => TestCommandStrings.Pending
    };
 
    private static FileStream CreateFile(string path)
    {
        var options = new FileStreamOptions { Mode = FileMode.CreateNew, Access = FileAccess.Write, Share = FileShare.None };
        if (!OperatingSystem.IsWindows())
        {
            options.UnixCreateMode = FileModeBits;
        }
 
        var stream = new FileStream(path, options);
        try
        {
            if (!OperatingSystem.IsWindows())
            {
                // A restrictive umask may remove owner bits at creation. Restore only the owner's
                // access through the open handle; the file is never exposed with broader access.
                File.SetUnixFileMode(stream.SafeFileHandle, FileModeBits);
            }
 
            return stream;
        }
        catch
        {
            stream.Dispose();
            throw;
        }
    }
 
    private static void CreatePrivateDirectory(string path, bool repairExisting, CliExecutionContext executionContext)
    {
        // Artifacts need the same directory boundary as filesystem-backed IPC: Unix 0700,
        // protected owner-only Windows ACLs supplied at creation, and no symlink traversal.
        SocketPermissionHelper.CreateDirectory(path, repairExisting,
            executionContext.WorkingDirectory.FullName, executionContext.HomeDirectory.FullName, TemporaryStorage.SystemTempPath);
    }
 
    private static void ValidateAncestors(string path, CliExecutionContext executionContext)
    {
        var comparison = OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
        for (var current = new DirectoryInfo(path).Parent; current is not null; current = current.Parent)
        {
            if (string.Equals(current.FullName, executionContext.HomeDirectory.FullName, comparison) ||
                string.Equals(current.FullName, Path.TrimEndingDirectorySeparator(TemporaryStorage.SystemTempPath), comparison))
            {
                break;
            }
            if (current.LinkTarget is not null || current.Exists && (current.Attributes & FileAttributes.ReparsePoint) != 0)
            {
                throw new IOException($"The test output path must not traverse a symbolic link or reparse point: '{current.FullName}'.");
            }
            if (!OperatingSystem.IsWindows() && current.Exists &&
                (File.GetUnixFileMode(current.FullName) & (UnixFileMode.GroupWrite | UnixFileMode.OtherWrite)) != 0)
            {
                throw new IOException($"The test output parent '{current.FullName}' must not be writable by other users.");
            }
        }
    }
}
 
internal sealed class TestRunSummary
{
    public int SchemaVersion { get; init; } = 1;
    public string? RunId { get; init; }
    public string? OutputPath { get; init; }
    public string? SummaryPath { get; init; }
    public DateTimeOffset StartedAt { get; init; }
    public DateTimeOffset? CompletedAt { get; init; }
    public int? CliExitCode { get; init; }
    public string? Error { get; init; }
    public Dictionary<string, TestResourceReport> TestResults { get; init; } = new(StringComparers.ResourceName);
    public required TestSummary TestSummary { get; init; }
}
 
internal sealed class TestResourceReport
{
    public string? ResourceId { get; init; }
    public string? State { get; init; }
    public int? ExitCode { get; init; }
    public required string Outcome { get; init; }
    public int ExpectedReplicas { get; init; }
    public string? CollectorStatus { get; init; }
    public string? CollectorError { get; init; }
    public string? OutputPath { get; init; }
    public TestCaseReport[] TestCases { get; init; } = [];
    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
    public Dictionary<string, TestReplicaReport>? Replicas { get; init; }
}
 
internal sealed class TestReplicaReport
{
    public string? State { get; init; }
    public int? ExitCode { get; init; }
    public required string Outcome { get; init; }
}
 
internal sealed class TestCaseReport
{
    public required string Name { get; init; }
    public required string Outcome { get; init; }
    public double? DurationMilliseconds { get; init; }
    public string? Message { get; init; }
}
 
internal sealed class TestSummary
{
    public required string Outcome { get; init; }
    public int Passed { get; init; }
    public int Failed { get; init; }
    public int Skipped { get; init; }
}
 
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, WriteIndented = true)]
[JsonSerializable(typeof(TestRunSummary))]
internal sealed partial class TestRunJsonContext : JsonSerializerContext;