< Summary

Information
Class: RoleRadar.Domain.Tracking.DateInferenceRule
Assembly: RoleRadar.Domain
File(s): /home/runner/work/roleradar/roleradar/src/RoleRadar.Domain/Tracking/DateInferenceRule.cs
Line coverage
100%
Covered lines: 68
Uncovered lines: 0
Coverable lines: 68
Total lines: 145
Line coverage: 100%
Branch coverage
86%
Covered branches: 26
Total branches: 30
Branch coverage: 86.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Resolve(...)80%303094.11%

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
 15public sealed record ResolvedDateOutcome(
 16    DateOnly ResolvedDate,
 17    DateResolutionKind Kind,
 18    bool IsApproximate,
 19    bool IsFlagged,
 20    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    {
 33437        if (string.IsNullOrWhiteSpace(rawInput))
 38        {
 6839            return new ResolvedDateOutcome(
 6840                ResolvedDate: defaultDate,
 6841                Kind: DateResolutionKind.DefaultedBlank,
 6842                IsApproximate: false,
 6843                IsFlagged: false,
 6844                Note: null);
 45        }
 46
 26647        var text = rawInput.Trim();
 26648        var isApproximate = false;
 26649        string? note = null;
 50
 51        // Check for leading approximation marker "~"
 26652        if (text.StartsWith('~'))
 53        {
 854            isApproximate = true;
 855            text = text[1..].Trim();
 56        }
 57
 58        // Check for parenthetical e.g. "2026-10-15 (stated date)" or entirely parenthesized "(10/15)"
 26659        var match = ParentheticalRegex().Match(text);
 26660        if (match.Success)
 61        {
 862            var inner = match.Groups[1].Value.Trim();
 863            var remaining = text.Remove(match.Index, match.Length).Trim();
 864            if (string.IsNullOrWhiteSpace(remaining))
 65            {
 66                // The entire date was wrapped in parentheses e.g. "(10/15)"
 667                text = inner;
 668                isApproximate = true;
 669                note = "stated date in parentheses";
 70            }
 71            else
 72            {
 273                note = inner;
 274                text = remaining;
 75            }
 76        }
 77
 78        // 1. Try full ISO date YYYY-MM-DD
 26679        if (DateOnly.TryParseExact(text, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out var fullDa
 80        {
 20081            return new ResolvedDateOutcome(
 20082                ResolvedDate: fullDate,
 20083                Kind: DateResolutionKind.Exact,
 20084                IsApproximate: isApproximate,
 20085                IsFlagged: isApproximate || note is not null,
 20086                Note: note);
 87        }
 88
 89        // 2. Try month-day partial format M/D, MM/DD, M-D, MM-DD
 6690        var mdMatch = MonthDayRegex().Match(text);
 6691        if (mdMatch.Success)
 92        {
 6493            var month = int.Parse(mdMatch.Groups["month"].Value, CultureInfo.InvariantCulture);
 6494            var day = int.Parse(mdMatch.Groups["day"].Value, CultureInfo.InvariantCulture);
 95
 6496            if (month is < 1 or > 12)
 97            {
 298                return new ResolvedDateOutcome(
 299                    ResolvedDate: defaultDate,
 2100                    Kind: DateResolutionKind.DefaultedImpossible,
 2101                    IsApproximate: isApproximate,
 2102                    IsFlagged: true,
 2103                    Note: note);
 104            }
 105
 62106            var daysInTargetMonth = DateTime.DaysInMonth(defaultDate.Year, month);
 62107            if (day < 1 || day > daysInTargetMonth)
 108            {
 6109                return new ResolvedDateOutcome(
 6110                    ResolvedDate: defaultDate,
 6111                    Kind: DateResolutionKind.DefaultedImpossible,
 6112                    IsApproximate: isApproximate,
 6113                    IsFlagged: true,
 6114                    Note: note);
 115            }
 116
 56117            var candidate = new DateOnly(defaultDate.Year, month, day);
 56118            if (candidate > defaultDate)
 119            {
 120                // Rollback rule: derives date in previous calendar year
 2121                var rollbackYear = defaultDate.Year - 1;
 2122                var daysInRollbackMonth = DateTime.DaysInMonth(rollbackYear, month);
 2123                var safeDay = Math.Min(day, daysInRollbackMonth);
 2124                var rollbackDate = new DateOnly(rollbackYear, month, safeDay);
 125
 2126                return new ResolvedDateOutcome(
 2127                    ResolvedDate: rollbackDate,
 2128                    Kind: DateResolutionKind.InferredRollback,
 2129                    IsApproximate: isApproximate,
 2130                    IsFlagged: isApproximate || note is not null,
 2131                    Note: note);
 132            }
 133
 54134            return new ResolvedDateOutcome(
 54135                ResolvedDate: candidate,
 54136                Kind: DateResolutionKind.InferredSameYear,
 54137                IsApproximate: isApproximate,
 54138                IsFlagged: isApproximate || note is not null,
 54139                Note: note);
 140        }
 141
 142        // Any unrecognized or malformed date format throws ArgumentException for caller validation handling
 2143        throw new ArgumentException($"Malformed date: '{rawInput}'. Expected ISO 'YYYY-MM-DD' or month-day 'M/D'.", name
 144    }
 145}