Skip to content

Add unit tests for StandardRetrospectiveCorrection#32

Open
ps2 wants to merge 1 commit into
tidepool-org:mainfrom
ps2:rc-tests
Open

Add unit tests for StandardRetrospectiveCorrection#32
ps2 wants to merge 1 commit into
tidepool-org:mainfrom
ps2:rc-tests

Conversation

@ps2
Copy link
Copy Markdown
Collaborator

@ps2 ps2 commented May 19, 2026

Summary

StandardRetrospectiveCorrection (the P-only retrospective correction controller) had no dedicated unit tests — its behavior was only covered transitively via the higher-level LoopAlgorithm tests. Adds 9 focused tests in StandardRetrospectiveCorrectionTests.swift.

Tests

  • Recency gating: stale / nil / empty discrepancy lists clear the correction and return empty.
  • Total correction = latest discrepancy: Standard RC uses ONLY .last from the discrepancy list. Verified that older entries are ignored even when they would change the answer.
  • Effect direction: positive and negative discrepancies project glucose forward in the expected direction; last sample ≈ starting + discrepancy.
  • First sample anchored: effect starts at the starting glucose value, since the correction hasn't yet had time to apply.
  • Short-discrepancy clamping: discrepancies shorter than retrospectiveCorrectionGroupingInterval use the grouping interval as the velocity-denominator (preventing over-amplified projections from very short windows).

Test plan

  • All existing tests pass
  • All 9 new tests pass (xcrun swift test --filter StandardRetrospectiveCorrectionTests)

Note

This intentionally does NOT add tests to IntegralRetrospectiveCorrection's existing test file — IRC's test surface is being shaped separately.

StandardRetrospectiveCorrection (the P-only retrospective correction
controller) had no dedicated unit tests — its behavior was only
covered transitively via the higher-level LoopAlgorithm tests.

Adds 9 tests covering:
- Recency gating: stale / nil / empty discrepancy lists clear the
  correction and return empty.
- Total correction effect equals the latest (most-recent) discrepancy
  magnitude (Standard is P-only on .last).
- Positive / negative discrepancies project glucose forward in the
  expected direction, with the last sample ≈ starting + discrepancy.
- The first effect sample equals the starting glucose value at the
  starting date (correction hasn't yet had time to apply).
- Only the latest discrepancy contributes — older entries are ignored
  (the key behavioral difference vs IntegralRC, which integrates them).
- Short discrepancies are clamped to retrospectiveCorrectionGroupingInterval
  to prevent over-amplified velocity from very short windows.

These tests will serve as a backstop while the active-insulin / EGP
decomposition work modifies the glucose-effect computation upstream of
the RC discrepancy calculation.
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.

2 participants