< Summary

Information
Class: RoleRadar.Domain.Notes.Note
Assembly: RoleRadar.Domain
File(s): /home/runner/work/roleradar/roleradar/src/RoleRadar.Domain/Notes/Note.cs
Line coverage
95%
Covered lines: 191
Uncovered lines: 9
Coverable lines: 200
Total lines: 382
Line coverage: 95.5%
Branch coverage
67%
Covered branches: 27
Total branches: 40
Branch coverage: 67.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Id()100%11100%
get_OwnerId()100%11100%
get_Title()100%11100%
get_Body()100%11100%
get_CurrentBodyHash()100%11100%
get_Tags()100%11100%
get_Area()100%11100%
get_Provenance()100%11100%
get_ImportMetadata()100%11100%
get_Status()100%11100%
get_VersionToken()100%11100%
get_RevisionCount()100%11100%
get_CreatedAtUtc()100%11100%
get_UpdatedAtUtc()100%11100%
get_CompanyId()100%11100%
get_PostingId()100%11100%
get_ApplicationId()100%11100%
get_HasPostImportEdits()50%22100%
.ctor(...)100%11100%
Create(...)100%11100%
CreateImported(...)50%2296.15%
Rehydrate(...)100%11100%
AppendBody(...)50%2288.88%
ReplaceBodyWithRevision(...)80%1010100%
UpdateProvenance(...)100%11100%
Archive(...)100%11100%
NormalizeTags(...)83.33%121285.71%
NormalizeArea(...)50%5466.66%
NormalizeTitle(...)50%5466.66%
ValidateBody(...)50%2280%
EnsureUtc(...)50%22100%

File(s)

/home/runner/work/roleradar/roleradar/src/RoleRadar.Domain/Notes/Note.cs

#LineLine coverage
 1using RoleRadar.Domain.Common;
 2
 3namespace RoleRadar.Domain.Notes;
 4
 5public sealed class Note
 6{
 7    public const int MaxTitleChars = 300;
 8    public const int MaxAreaChars = 64;
 9    public const int MaxTagCount = 32;
 10    public const int MaxTagChars = 64;
 11    public const int MaxBodyBytes = 8 * 1024 * 1024; // 8 MiB (8,388,608 bytes) of UTF-8
 12    public const int DefaultGetMaxChars = 200_000;
 13    public const int MaxGetMaxChars = 200_000;
 14
 450615    public Guid Id { get; }
 16
 112617    public string OwnerId { get; }
 18
 678819    public string Title { get; private set; }
 20
 1214821    public string Body { get; private set; }
 22
 731423    public string CurrentBodyHash { get; private set; }
 24
 667225    public IReadOnlyList<string> Tags { get; private set; }
 26
 668027    public string Area { get; private set; }
 28
 1228829    public ProvenanceMarker Provenance { get; private set; }
 30
 1095631    public NoteImportMetadata? ImportMetadata { get; private set; }
 32
 812033    public NoteStatus Status { get; private set; }
 34
 828035    public Guid VersionToken { get; private set; }
 36
 498037    public int RevisionCount { get; private set; }
 38
 199239    public DateTime CreatedAtUtc { get; }
 40
 721641    public DateTime UpdatedAtUtc { get; private set; }
 42
 476243    public Guid? CompanyId { get; private set; }
 44
 476245    public Guid? PostingId { get; private set; }
 46
 476247    public Guid? ApplicationId { get; private set; }
 48
 49    /// <summary>
 50    /// Detects whether an imported note's body has been modified since its last import write.
 51    /// Compares CurrentBodyHash against ImportMetadata.LastImportedBodyHash only (excluding Provenance.Date).
 52    /// </summary>
 53    public bool HasPostImportEdits =>
 22254        ImportMetadata is not null &&
 22255        !string.Equals(CurrentBodyHash, ImportMetadata.LastImportedBodyHash, StringComparison.Ordinal);
 56
 382657    private Note(
 382658        Guid id,
 382659        string ownerId,
 382660        string title,
 382661        string body,
 382662        string currentBodyHash,
 382663        IReadOnlyList<string> tags,
 382664        string area,
 382665        ProvenanceMarker provenance,
 382666        NoteImportMetadata? importMetadata,
 382667        NoteStatus status,
 382668        Guid versionToken,
 382669        int revisionCount,
 382670        DateTime createdAtUtc,
 382671        DateTime updatedAtUtc,
 382672        Guid? companyId = null,
 382673        Guid? postingId = null,
 382674        Guid? applicationId = null)
 75    {
 382676        Id = id;
 382677        OwnerId = ownerId;
 382678        Title = title;
 382679        Body = body;
 382680        CurrentBodyHash = currentBodyHash;
 382681        Tags = tags;
 382682        Area = area;
 382683        Provenance = provenance;
 382684        ImportMetadata = importMetadata;
 382685        Status = status;
 382686        VersionToken = versionToken;
 382687        RevisionCount = revisionCount;
 382688        CreatedAtUtc = EnsureUtc(createdAtUtc);
 382689        UpdatedAtUtc = EnsureUtc(updatedAtUtc);
 382690        CompanyId = companyId;
 382691        PostingId = postingId;
 382692        ApplicationId = applicationId;
 382693    }
 94
 95    public static Note Create(
 96        string ownerId,
 97        string title,
 98        string body,
 99        IEnumerable<string>? tags,
 100        string area,
 101        ProvenanceMarker provenance,
 102        DateTime createdAtUtc,
 103        Guid? companyId = null,
 104        Guid? postingId = null,
 105        Guid? applicationId = null)
 106    {
 184107        var validOwner = new OwnerId(ownerId).Value;
 184108        var normalizedTitle = NormalizeTitle(title);
 184109        ValidateBody(body);
 184110        var normalizedTags = NormalizeTags(tags);
 184111        var normalizedArea = NormalizeArea(area);
 184112        ArgumentNullException.ThrowIfNull(provenance);
 184113        var utcNow = EnsureUtc(createdAtUtc);
 114
 184115        return new Note(
 184116            id: Guid.CreateVersion7(),
 184117            ownerId: validOwner,
 184118            title: normalizedTitle,
 184119            body: body,
 184120            currentBodyHash: Sha256Hasher.ComputeLowerHex(body),
 184121            tags: normalizedTags,
 184122            area: normalizedArea,
 184123            provenance: provenance,
 184124            importMetadata: null,
 184125            status: NoteStatus.Active,
 184126            versionToken: Guid.CreateVersion7(),
 184127            revisionCount: 0,
 184128            createdAtUtc: utcNow,
 184129            updatedAtUtc: utcNow,
 184130            companyId: companyId,
 184131            postingId: postingId,
 184132            applicationId: applicationId);
 133    }
 134
 135    public static Note CreateImported(
 136        string ownerId,
 137        string title,
 138        string body,
 139        IEnumerable<string>? tags,
 140        string area,
 141        ProvenanceMarker provenance,
 142        NoteImportMetadata importMetadata,
 143        DateTime createdAtUtc)
 144    {
 804145        ArgumentNullException.ThrowIfNull(importMetadata);
 804146        var validOwner = new OwnerId(ownerId).Value;
 804147        var normalizedTitle = NormalizeTitle(title);
 804148        ValidateBody(body);
 804149        var bodyHash = Sha256Hasher.ComputeLowerHex(body);
 804150        if (!string.Equals(bodyHash, importMetadata.LastImportedBodyHash, StringComparison.Ordinal))
 151        {
 0152            throw new ArgumentException("ImportMetadata.LastImportedBodyHash must match the SHA-256 hash of body.", name
 153        }
 154
 804155        var normalizedTags = NormalizeTags(tags);
 804156        var normalizedArea = NormalizeArea(area);
 804157        ArgumentNullException.ThrowIfNull(provenance);
 804158        var utcNow = EnsureUtc(createdAtUtc);
 159
 804160        return new Note(
 804161            id: Guid.CreateVersion7(),
 804162            ownerId: validOwner,
 804163            title: normalizedTitle,
 804164            body: body,
 804165            currentBodyHash: bodyHash,
 804166            tags: normalizedTags,
 804167            area: normalizedArea,
 804168            provenance: provenance,
 804169            importMetadata: importMetadata,
 804170            status: NoteStatus.Active,
 804171            versionToken: Guid.CreateVersion7(),
 804172            revisionCount: 0,
 804173            createdAtUtc: utcNow,
 804174            updatedAtUtc: utcNow);
 175    }
 176
 177    public static Note Rehydrate(
 178        Guid id,
 179        string ownerId,
 180        string title,
 181        string body,
 182        string currentBodyHash,
 183        IReadOnlyList<string> tags,
 184        string area,
 185        ProvenanceMarker provenance,
 186        NoteImportMetadata? importMetadata,
 187        NoteStatus status,
 188        Guid versionToken,
 189        int revisionCount,
 190        DateTime createdAtUtc,
 191        DateTime updatedAtUtc,
 192        Guid? companyId = null,
 193        Guid? postingId = null,
 194        Guid? applicationId = null) =>
 2838195        new(
 2838196            id,
 2838197            ownerId,
 2838198            title,
 2838199            body,
 2838200            currentBodyHash,
 2838201            tags,
 2838202            area,
 2838203            provenance,
 2838204            importMetadata,
 2838205            status,
 2838206            versionToken,
 2838207            revisionCount,
 2838208            createdAtUtc,
 2838209            updatedAtUtc,
 2838210            companyId,
 2838211            postingId,
 2838212            applicationId);
 213
 214    /// <summary>
 215    /// Appends text to the note body without creating a full-body revision snapshot or incrementing RevisionCount.
 216    /// Rotates VersionToken and updates CurrentBodyHash and UpdatedAtUtc.
 217    /// </summary>
 218    public void AppendBody(string textToAppend, DateTime occurredAtUtc)
 219    {
 194220        if (string.IsNullOrEmpty(textToAppend))
 221        {
 0222            throw new ArgumentException("Text to append cannot be null or empty.", nameof(textToAppend));
 223        }
 224
 194225        var updatedBody = string.Concat(Body, textToAppend);
 194226        ValidateBody(updatedBody);
 227
 194228        Body = updatedBody;
 194229        CurrentBodyHash = Sha256Hasher.ComputeLowerHex(updatedBody);
 194230        VersionToken = Guid.CreateVersion7();
 194231        UpdatedAtUtc = EnsureUtc(occurredAtUtc);
 194232    }
 233
 234    /// <summary>
 235    /// Replaces the note body on a destructive write ("replace-section" or "import-overwrite"),
 236    /// returning a full prior-body NoteRevision snapshot, incrementing RevisionCount, and rotating VersionToken.
 237    /// </summary>
 238    public NoteRevision ReplaceBodyWithRevision(
 239        string newBody,
 240        string reason,
 241        DateTime occurredAtUtc,
 242        string? updatedTitle = null,
 243        ProvenanceMarker? updatedProvenance = null,
 244        int? updatedSectionOrderIndex = null,
 245        bool updateLastImportedHash = false)
 246    {
 22247        ValidateBody(newBody);
 22248        var utcOccurred = EnsureUtc(occurredAtUtc);
 22249        var nextRevisionNumber = RevisionCount + 1;
 250
 22251        var revision = NoteRevision.Create(
 22252            ownerId: OwnerId,
 22253            noteId: Id,
 22254            revisionNumber: nextRevisionNumber,
 22255            reason: reason,
 22256            priorBody: Body,
 22257            priorVersionToken: VersionToken,
 22258            createdAtUtc: utcOccurred);
 259
 22260        var newBodyHash = Sha256Hasher.ComputeLowerHex(newBody);
 22261        Body = newBody;
 22262        CurrentBodyHash = newBodyHash;
 22263        RevisionCount = nextRevisionNumber;
 22264        VersionToken = Guid.CreateVersion7();
 22265        UpdatedAtUtc = utcOccurred;
 266
 22267        if (updatedTitle is not null)
 268        {
 8269            Title = NormalizeTitle(updatedTitle);
 270        }
 271
 22272        if (updatedProvenance is not null)
 273        {
 8274            Provenance = updatedProvenance;
 275        }
 276
 22277        if (updateLastImportedHash && ImportMetadata is not null)
 278        {
 8279            ImportMetadata = ImportMetadata.WithUpdatedImportState(
 8280                updatedSectionOrderIndex ?? ImportMetadata.SectionOrderIndex,
 8281                newBodyHash);
 282        }
 283
 22284        return revision;
 285    }
 286
 287    public void UpdateProvenance(ProvenanceMarker provenance, DateTime occurredAtUtc)
 288    {
 2289        ArgumentNullException.ThrowIfNull(provenance);
 2290        Provenance = provenance;
 2291        VersionToken = Guid.CreateVersion7();
 2292        UpdatedAtUtc = EnsureUtc(occurredAtUtc);
 2293    }
 294
 295    public void Archive(DateTime occurredAtUtc)
 296    {
 8297        Status = NoteStatus.Archived;
 8298        VersionToken = Guid.CreateVersion7();
 8299        UpdatedAtUtc = EnsureUtc(occurredAtUtc);
 8300    }
 301
 302    public static IReadOnlyList<string> NormalizeTags(IEnumerable<string>? tags)
 303    {
 1104304        if (tags is null)
 305        {
 32306            return [];
 307        }
 308
 1072309        var normalized = new List<string>();
 1072310        var seen = new HashSet<string>(StringComparer.Ordinal);
 3932311        foreach (var raw in tags)
 312        {
 894313            if (string.IsNullOrWhiteSpace(raw))
 314            {
 315                continue;
 316            }
 317
 892318            var tag = raw.Trim().ToLowerInvariant();
 892319            if (tag.Length > MaxTagChars)
 320            {
 0321                throw new ArgumentException($"Tag length ({tag.Length}) exceeds maximum allowed ({MaxTagChars} character
 322            }
 323
 892324            if (seen.Add(tag))
 325            {
 890326                normalized.Add(tag);
 327            }
 328        }
 329
 1072330        if (normalized.Count > MaxTagCount)
 331        {
 0332            throw new ArgumentException($"Tag count ({normalized.Count}) exceeds maximum allowed ({MaxTagCount}).", name
 333        }
 334
 1072335        return normalized;
 336    }
 337
 338    public static string NormalizeArea(string area)
 339    {
 1204340        if (string.IsNullOrWhiteSpace(area))
 341        {
 0342            throw new ArgumentException("Area label cannot be null or whitespace.", nameof(area));
 343        }
 344
 1204345        var normalized = area.Trim().ToLowerInvariant();
 1204346        if (normalized.Length > MaxAreaChars)
 347        {
 0348            throw new ArgumentException($"Area label length ({normalized.Length}) exceeds maximum allowed ({MaxAreaChars
 349        }
 350
 1204351        return normalized;
 352    }
 353
 354    private static string NormalizeTitle(string title)
 355    {
 996356        if (string.IsNullOrWhiteSpace(title))
 357        {
 0358            throw new ArgumentException("Note title cannot be null or whitespace.", nameof(title));
 359        }
 360
 996361        var normalized = title.Trim();
 996362        if (normalized.Length > MaxTitleChars)
 363        {
 0364            throw new ArgumentException($"Note title length ({normalized.Length}) exceeds maximum allowed ({MaxTitleChar
 365        }
 366
 996367        return normalized;
 368    }
 369
 370    private static void ValidateBody(string body)
 371    {
 1204372        ArgumentNullException.ThrowIfNull(body);
 1204373        var byteCount = System.Text.Encoding.UTF8.GetByteCount(body);
 1204374        if (byteCount > MaxBodyBytes)
 375        {
 0376            throw new ArgumentException($"Note body byte length ({byteCount}) exceeds maximum allowed ({MaxBodyBytes})."
 377        }
 1204378    }
 379
 380    private static DateTime EnsureUtc(DateTime value) =>
 8866381        value.Kind == DateTimeKind.Utc ? value : DateTime.SpecifyKind(value, DateTimeKind.Utc);
 382}

Methods/Properties

get_Id()
get_OwnerId()
get_Title()
get_Body()
get_CurrentBodyHash()
get_Tags()
get_Area()
get_Provenance()
get_ImportMetadata()
get_Status()
get_VersionToken()
get_RevisionCount()
get_CreatedAtUtc()
get_UpdatedAtUtc()
get_CompanyId()
get_PostingId()
get_ApplicationId()
get_HasPostImportEdits()
.ctor(System.Guid,System.String,System.String,System.String,System.String,System.Collections.Generic.IReadOnlyList`1<System.String>,System.String,RoleRadar.Domain.Notes.ProvenanceMarker,RoleRadar.Domain.Notes.NoteImportMetadata,RoleRadar.Domain.Notes.NoteStatus,System.Guid,System.Int32,System.DateTime,System.DateTime,System.Nullable`1<System.Guid>,System.Nullable`1<System.Guid>,System.Nullable`1<System.Guid>)
Create(System.String,System.String,System.String,System.Collections.Generic.IEnumerable`1<System.String>,System.String,RoleRadar.Domain.Notes.ProvenanceMarker,System.DateTime,System.Nullable`1<System.Guid>,System.Nullable`1<System.Guid>,System.Nullable`1<System.Guid>)
CreateImported(System.String,System.String,System.String,System.Collections.Generic.IEnumerable`1<System.String>,System.String,RoleRadar.Domain.Notes.ProvenanceMarker,RoleRadar.Domain.Notes.NoteImportMetadata,System.DateTime)
Rehydrate(System.Guid,System.String,System.String,System.String,System.String,System.Collections.Generic.IReadOnlyList`1<System.String>,System.String,RoleRadar.Domain.Notes.ProvenanceMarker,RoleRadar.Domain.Notes.NoteImportMetadata,RoleRadar.Domain.Notes.NoteStatus,System.Guid,System.Int32,System.DateTime,System.DateTime,System.Nullable`1<System.Guid>,System.Nullable`1<System.Guid>,System.Nullable`1<System.Guid>)
AppendBody(System.String,System.DateTime)
ReplaceBodyWithRevision(System.String,System.String,System.DateTime,System.String,RoleRadar.Domain.Notes.ProvenanceMarker,System.Nullable`1<System.Int32>,System.Boolean)
UpdateProvenance(RoleRadar.Domain.Notes.ProvenanceMarker,System.DateTime)
Archive(System.DateTime)
NormalizeTags(System.Collections.Generic.IEnumerable`1<System.String>)
NormalizeArea(System.String)
NormalizeTitle(System.String)
ValidateBody(System.String)
EnsureUtc(System.DateTime)