< Summary

Information
Class: Orchestrator.Commands.Shared.JustificationConsoleWriter
Assembly: Orchestrator
File(s): /home/runner/work/KicktippAi/KicktippAi/src/Orchestrator/Commands/Shared/JustificationConsoleWriter.cs
Line coverage
48%
Covered lines: 26
Uncovered lines: 28
Coverable lines: 54
Total lines: 129
Line coverage: 48.1%
Branch coverage
31%
Covered branches: 19
Total branches: 60
Branch coverage: 31.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
WriteJustification(...)80%1010100%
HasContent(...)4.55%2502222.22%
HasSourceContent(...)0%4260%
WriteSources(...)33.33%511235.29%
WriteUncertainties(...)60%231050%

File(s)

/home/runner/work/KicktippAi/KicktippAi/src/Orchestrator/Commands/Shared/JustificationConsoleWriter.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3using EHonda.KicktippAi.Core;
 4using Spectre.Console;
 5
 6namespace Orchestrator.Commands.Shared;
 7
 8internal sealed class JustificationConsoleWriter
 9{
 10    private readonly IAnsiConsole _console;
 11
 112    public JustificationConsoleWriter(IAnsiConsole console)
 13    {
 114        _console = console;
 115    }
 16
 17    public void WriteJustification(
 18        PredictionJustification? justification,
 19        string headingMarkup,
 20        string indent,
 21        string fallbackMarkup)
 22    {
 123        if (justification == null || !HasContent(justification))
 24        {
 125            _console.MarkupLine(fallbackMarkup);
 126            return;
 27        }
 28
 129        _console.MarkupLine(headingMarkup);
 30
 131        if (!string.IsNullOrWhiteSpace(justification.KeyReasoning))
 32        {
 133            _console.MarkupLine($"{indent}[white]Key reasoning:[/] {Markup.Escape(justification.KeyReasoning.Trim())}");
 34        }
 35
 136        WriteSources("Most valuable context sources", justification.ContextSources?.MostValuable, indent);
 137        WriteSources("Least valuable context sources", justification.ContextSources?.LeastValuable, indent);
 138        WriteUncertainties(justification.Uncertainties, indent);
 139    }
 40
 41    private static bool HasContent(PredictionJustification justification)
 42    {
 143        if (!string.IsNullOrWhiteSpace(justification.KeyReasoning))
 44        {
 145            return true;
 46        }
 47
 048        if (justification.ContextSources?.MostValuable != null &&
 049            justification.ContextSources.MostValuable.Any(HasSourceContent))
 50        {
 051            return true;
 52        }
 53
 054        if (justification.ContextSources?.LeastValuable != null &&
 055            justification.ContextSources.LeastValuable.Any(HasSourceContent))
 56        {
 057            return true;
 58        }
 59
 060        return justification.Uncertainties != null &&
 061               justification.Uncertainties.Any(item => !string.IsNullOrWhiteSpace(item));
 62    }
 63
 64    private static bool HasSourceContent(PredictionJustificationContextSource source)
 65    {
 066        return !string.IsNullOrWhiteSpace(source?.DocumentName) ||
 067               !string.IsNullOrWhiteSpace(source?.Details);
 68    }
 69
 70    private void WriteSources(
 71        string heading,
 72        IReadOnlyList<PredictionJustificationContextSource>? sources,
 73        string indent)
 74    {
 175        if (sources == null)
 76        {
 077            return;
 78        }
 79
 180        var entries = sources
 181            .Where(HasSourceContent)
 182            .ToList();
 83
 184        if (entries.Count == 0)
 85        {
 186            return;
 87        }
 88
 089        _console.MarkupLine($"{indent}[white]{heading}[/]");
 90
 091        foreach (var entry in entries)
 92        {
 093            var documentName = string.IsNullOrWhiteSpace(entry.DocumentName)
 094                ? "Unnamed document"
 095                : entry.DocumentName.Trim();
 96
 097            var details = string.IsNullOrWhiteSpace(entry.Details)
 098                ? "No details provided"
 099                : entry.Details.Trim();
 100
 0101            _console.MarkupLine($"{indent}  • [yellow]{Markup.Escape(documentName)}[/]: {Markup.Escape(details)}");
 102        }
 0103    }
 104
 105    private void WriteUncertainties(IReadOnlyList<string>? uncertainties, string indent)
 106    {
 1107        if (uncertainties == null)
 108        {
 0109            return;
 110        }
 111
 1112        var items = uncertainties
 0113            .Where(item => !string.IsNullOrWhiteSpace(item))
 0114            .Select(item => item.Trim())
 1115            .ToList();
 116
 1117        if (items.Count == 0)
 118        {
 1119            return;
 120        }
 121
 0122        _console.MarkupLine($"{indent}[white]Uncertainties:[/]");
 123
 0124        foreach (var item in items)
 125        {
 0126            _console.MarkupLine($"{indent}  • {Markup.Escape(item)}");
 127        }
 0128    }
 129}