Skip to content

[DRK-2635] test(tests): Tier-2 rule against per-save state in hook instance fields - #550

Open
baoduy wants to merge 1 commit into
devfrom
feature/drk-2635-arch-enforcement-hook-state
Open

baoduy wants to merge 1 commit into
devfrom
feature/drk-2635-arch-enforcement-hook-state

Conversation

@baoduy

@baoduy baoduy commented Nov 30, 2026

Copy link
Copy Markdown
Owner

Summary

Test-only enforcement from the 2026-12 monthly architecture review (DRK-2635). It adds one Tier-2 architecture rule, HookInstanceStateArchitectureTests, in EfCore.AuditLogs.Tests/Architecture/.

Rule: a concrete SaveChanges hook (IHookBaseAsync) in DKNet.EfCore.AuditLogs, DKNet.EfCore.Events or DKNet.EfCore.DataAuthorization must not keep a mutable collection (List<>, Dictionary<,>, HashSet<>, concurrent collections, and similar) in an instance field.

Why: hooks are resolved scoped (AddHook → AddKeyedScoped). The same instance therefore runs for the outer save and for every nested SaveChangesAsync on the same DbContext. HookRunnerInterceptor gives each nesting level its own HookContext/SnapshotContext, but a collection field shared across levels is overwritten or cleared by the nested save. The outer save's audit entries or declared events are then silently lost. Finding: DRK-2640.

Baseline (KnownViolations, shrink-only):

  • DKNet.EfCore.AuditLogs.Internals.EfCoreAuditHook._cache
  • DKNet.EfCore.Events.Internals.EventHook._declaredEvents

The class also carries two self-checks:

  • a stale-entry check, which fails once a fix lands so the entry gets deleted;
  • a vacuous-scan check, which fails if any scanned package stops shipping a hook.

EfCore.AuditLogs.Tests.csproj gains a ProjectReference to DKNet.EfCore.Events so the scan can see EventHook.

Evidence

  • dotnet build src/DKNet.FW.sln -c Release: 60 projects, 0 warnings, 0 errors.
  • dotnet test src/EfCore/EfCore.AuditLogs.Tests: 72 passed, 0 failed (3 new).
  • KnownViolations_AreAllStillViolations passes, which proves the detector finds both current offenders.
  • dotnet format whitespace --verify-no-changes is clean on the new file.
  • Local host runs only the .NET 11 runtime, so the test host was run with DOTNET_ROLL_FORWARD=Major. CI runs on .NET 10.

No production code is touched. Coverage impact: none on library code.

Merge danger

Low. Two test-project files only. The new ProjectReference adds DKNet.EfCore.Events (and its transitive Mapster dependency) to the AuditLogs test build. No shared fixtures changed.

Related findings from the same review, which this PR does not fix: DRK-2636 … DRK-2645.

🤖 Generated with Claude Code

…elds [DRK-2635]

Tier-2 architecture rule from the 2026-12 architecture review (finding
DRK-2640). SaveChanges hooks are resolved scoped, so one instance serves
the outer save and every nested save on the same DbContext; a mutable
collection field carrying state from BeforeSaveAsync to AfterSaveAsync is
overwritten by the nested save and the outer save's audit entries or
declared events are silently lost.

- Scan the AuditLogs, Events and DataAuthorization hook assemblies
- KnownViolations baseline: EfCoreAuditHook._cache, EventHook._declaredEvents
- Stale-entry and vacuous-scan self-checks
- EfCore.AuditLogs.Tests references DKNet.EfCore.Events for the scan

Test-only change; no production code touched.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>
@github-actions

Copy link
Copy Markdown

📊 Code Coverage Report

| Metric | Coverage |
|--------|----------|
| **Line Coverage** | 95.9% |
| **Branch Coverage** | 88.3% |
| **Method Coverage** | 91.2% |

**Lines:** 7096/undefined covered
**Branches:** 2979/undefined covered

📈 [View Full Coverage Report](https://github.com/baoduy/DKNet/actions/runs/46463997267)

@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.25%. Comparing base (b4ce9d6) to head (0e77f5c).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev     #550   +/-   ##
=======================================
  Coverage   93.25%   93.25%           
=======================================
  Files         219      219           
  Lines        7191     7191           
  Branches     1220     1220           
=======================================
  Hits         6706     6706           
  Misses        259      259           
  Partials      226      226           
Flag Coverage Δ
unittests 93.25% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant