< Summary

Information
Class: RoleRadar.Domain.Tracking.ResolvedDateOutcome
Assembly: RoleRadar.Domain
File(s): /home/runner/work/roleradar/roleradar/src/RoleRadar.Domain/Tracking/DateInferenceRule.cs
Line coverage
100%
Covered lines: 6
Uncovered lines: 0
Coverable lines: 6
Total lines: 145
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_ResolvedDate()100%11100%
get_Kind()100%11100%
get_IsApproximate()100%11100%
get_IsFlagged()100%11100%
get_Note()100%11100%

File(s)

/home/runner/work/roleradar/roleradar/src/RoleRadar.Domain/Tracking/DateInferenceRule.cs

#LineLine coverage
 1using System.Globalization;
 2using System.Text.RegularExpressions;
 3
 4namespace RoleRadar.Domain.Tracking;
 5
 6public enum DateResolutionKind
 7{
 8    Exact,
 9    InferredSameYear,
 10    InferredRollback,
 11    DefaultedBlank,
 12    DefaultedImpossible
 13}
 14
 33215public sealed record ResolvedDateOutcome(
 48416    DateOnly ResolvedDate,
 63817    DateResolutionKind Kind,
 1018    bool IsApproximate,
 33019    bool IsFlagged,
 34020    string? Note);
 21
 22/// <summary>
 23/// Domain rules for parsing and inferring dates from legacy tracker inputs,
 24/// supporting month-day derivation with calendar rollback, approximation markers,
 25/// stated-date parentheticals, and graceful defaulting of impossible dates.
 26/// </summary>
 27public static partial class DateInferenceRule
 28{
 29    [GeneratedRegex(@"\(([^)]+)\)")]
 30    private static partial Regex ParentheticalRegex();
 31
 32    [GeneratedRegex(@"^(?<month>\d{1,2})[/\-](?<day>\d{1,2})$")]
 33    private static partial Regex MonthDayRegex();
 34
 35    public static ResolvedDateOutcome Resolve(string? rawInput, DateOnly defaultDate)
 36    {
 37        if (string.IsNullOrWhiteSpace(rawInput))
 38        {
 39            return new ResolvedDateOutcome(
 40                ResolvedDate: defaultDate,
 41                Kind: DateResolutionKind.DefaultedBlank,
 42                IsApproximate: false,
 43                IsFlagged: false,
 44                Note: null);
 45        }
 46
 47        var text = rawInput.Trim();
 48        var isApproximate = false;
 49        string? note = null;
 50
 51        // Check for leading approximation marker "~"
 52        if (text.StartsWith('~'))
 53        {
 54            isApproximate = true;
 55            text = text[1..].Trim();
 56        }
 57
 58        // Check for parenthetical e.g. "2026-10-15 (stated date)" or entirely parenthesized "(10/15)"
 59        var match = ParentheticalRegex().Match(text);
 60        if (match.Success)
 61        {
 62            var inner = match.Groups[1].Value.Trim();
 63            var remaining = text.Remove(match.Index, match.Length).Trim();
 64            if (string.IsNullOrWhiteSpace(remaining))
 65            {
 66                // The entire date was wrapped in parentheses e.g. "(10/15)"
 67                text = inner;
 68                isApproximate = true;
 69                note = "stated date in parentheses";
 70            }
 71            else
 72            {
 73                note = inner;
 74                text = remaining;
 75            }
 76        }
 77
 78        // 1. Try full ISO date YYYY-MM-DD
 79        if (DateOnly.TryParseExact(text, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out var fullDa
 80        {
 81            return new ResolvedDateOutcome(
 82                ResolvedDate: fullDate,
 83                Kind: DateResolutionKind.Exact,
 84                IsApproximate: isApproximate,
 85                IsFlagged: isApproximate || note is not null,
 86                Note: note);
 87        }
 88
 89        // 2. Try month-day partial format M/D, MM/DD, M-D, MM-DD
 90        var mdMatch = MonthDayRegex().Match(text);
 91        if (mdMatch.Success)
 92        {
 93            var month = int.Parse(mdMatch.Groups["month"].Value, CultureInfo.InvariantCulture);
 94            var day = int.Parse(mdMatch.Groups["day"].Value, CultureInfo.InvariantCulture);
 95
 96            if (month is < 1 or > 12)
 97            {
 98                return new ResolvedDateOutcome(
 99                    ResolvedDate: defaultDate,
 100                    Kind: DateResolutionKind.DefaultedImpossible,
 101                    IsApproximate: isApproximate,
 102                    IsFlagged: true,
 103                    Note: note);
 104            }
 105
 106            var daysInTargetMonth = DateTime.DaysInMonth(defaultDate.Year, month);
 107            if (day < 1 || day > daysInTargetMonth)
 108            {
 109                return new ResolvedDateOutcome(
 110                    ResolvedDate: defaultDate,
 111                    Kind: DateResolutionKind.DefaultedImpossible,
 112                    IsApproximate: isApproximate,
 113                    IsFlagged: true,
 114                    Note: note);
 115            }
 116
 117            var candidate = new DateOnly(defaultDate.Year, month, day);
 118            if (candidate > defaultDate)
 119            {
 120                // Rollback rule: derives date in previous calendar year
 121                var rollbackYear = defaultDate.Year - 1;
 122                var daysInRollbackMonth = DateTime.DaysInMonth(rollbackYear, month);
 123                var safeDay = Math.Min(day, daysInRollbackMonth);
 124                var rollbackDate = new DateOnly(rollbackYear, month, safeDay);
 125
 126                return new ResolvedDateOutcome(
 127                    ResolvedDate: rollbackDate,
 128                    Kind: DateResolutionKind.InferredRollback,
 129                    IsApproximate: isApproximate,
 130                    IsFlagged: isApproximate || note is not null,
 131                    Note: note);
 132            }
 133
 134            return new ResolvedDateOutcome(
 135                ResolvedDate: candidate,
 136                Kind: DateResolutionKind.InferredSameYear,
 137                IsApproximate: isApproximate,
 138                IsFlagged: isApproximate || note is not null,
 139                Note: note);
 140        }
 141
 142        // Any unrecognized or malformed date format throws ArgumentException for caller validation handling
 143        throw new ArgumentException($"Malformed date: '{rawInput}'. Expected ISO 'YYYY-MM-DD' or month-day 'M/D'.", name
 144    }
 145}