Files
ruvnet--RuView/archive/v1/tests/unit/test_phase_sanitizer.py
T
rUv 81cc241b9e chore(repo): move v1/ → archive/v1/ + add archive/README.md (#430)
The Rust port at v2/ has been the primary codebase since the rename
in #427. The Python implementation at v1/ is no longer the active
target; the only load-bearing path is the deterministic proof bundle
at v1/data/proof/ (per ADR-011 / ADR-028 witness verification).

Move the whole Python tree into archive/v1/ and document the policy
in archive/README.md: no new features, bug fixes only when they affect
a still-load-bearing path (currently just the proof), CI continues to
verify the proof on every push and PR.

Path references updated in 26 files via path-pattern sed (only
matches v1/<known-child> patterns, never bare v1 or API URLs like
/api/v1/). Two double-prefix typos (archive/archive/v1/) caught and
hand-fixed in verify-pipeline.yml and ADR-011.

Validated:
- Python proof verify.py imports cleanly at archive/v1/data/proof/
  (numpy/scipy still required; CI installs requirements-lock.txt
  from archive/v1/ now)
- cargo test --workspace --no-default-features → 1,539 passed,
  0 failed, 8 ignored (unaffected by Python tree relocation)
- ESP32-S3 on COM7 untouched (no firmware paths changed)

After-merge: contributors should re-run any local `python v1/...`
commands as `python archive/v1/...` (CLAUDE.md and CHANGELOG already
updated).
2026-04-25 23:07:52 -04:00

96 lines
3.8 KiB
Python

import pytest
import numpy as np
import time
from unittest.mock import Mock, patch
from src.core.phase_sanitizer import PhaseSanitizer, PhaseSanitizationError
_SANITIZER_CONFIG = {
"unwrapping_method": "numpy",
"outlier_threshold": 3.0,
"smoothing_window": 5,
"enable_outlier_removal": True,
"enable_smoothing": True,
"enable_noise_filtering": True,
"noise_threshold": 0.1,
}
class TestPhaseSanitizer:
"""Test suite for Phase Sanitizer following London School TDD principles"""
@pytest.fixture
def mock_phase_data(self):
"""Generate synthetic phase data strictly within valid [-π, π] range"""
return np.array([
[0.1, 0.2, 0.4, 0.3, 0.5],
[-1.0, -0.1, 0.0, 0.1, 0.2],
[0.0, 0.1, 0.2, 0.3, 0.4],
])
@pytest.fixture
def phase_sanitizer(self):
"""Create Phase Sanitizer instance for testing"""
return PhaseSanitizer(config=_SANITIZER_CONFIG)
def test_unwrap_phase_removes_discontinuities(self, phase_sanitizer):
"""Test that phase unwrapping removes 2π discontinuities"""
# Create data with explicit 2π jump
jumpy = np.array([[0.1, 0.2, 0.2 + 2 * np.pi, 0.4, 0.5]])
result = phase_sanitizer.unwrap_phase(jumpy)
assert result is not None
assert isinstance(result, np.ndarray)
assert result.shape == jumpy.shape
phase_diffs = np.abs(np.diff(result[0]))
assert np.all(phase_diffs < np.pi) # No jumps larger than π
def test_remove_outliers_returns_same_shape(self, phase_sanitizer, mock_phase_data):
"""Test that outlier removal preserves array shape"""
result = phase_sanitizer.remove_outliers(mock_phase_data)
assert result is not None
assert isinstance(result, np.ndarray)
assert result.shape == mock_phase_data.shape
def test_smooth_phase_reduces_noise(self, phase_sanitizer, mock_phase_data):
"""Test that phase smoothing reduces noise while preserving trends"""
rng = np.random.default_rng(42)
noisy_data = mock_phase_data + rng.normal(0, 0.05, mock_phase_data.shape)
# Clip to valid range after adding noise
noisy_data = np.clip(noisy_data, -np.pi, np.pi)
result = phase_sanitizer.smooth_phase(noisy_data)
assert result is not None
assert isinstance(result, np.ndarray)
assert result.shape == noisy_data.shape
assert np.var(result) <= np.var(noisy_data)
def test_sanitize_raises_for_1d_input(self, phase_sanitizer):
"""Sanitizer should raise PhaseSanitizationError on 1D input"""
with pytest.raises(PhaseSanitizationError, match="Phase data must be 2D array"):
phase_sanitizer.sanitize_phase(np.array([0.1, 0.2, 0.3]))
def test_sanitize_raises_for_empty_2d_input(self, phase_sanitizer):
"""Sanitizer should raise PhaseSanitizationError on empty 2D input"""
with pytest.raises(PhaseSanitizationError, match="Phase data cannot be empty"):
phase_sanitizer.sanitize_phase(np.empty((0, 5)))
def test_sanitize_full_pipeline_integration(self, phase_sanitizer, mock_phase_data):
"""Test that full sanitization pipeline works correctly"""
result = phase_sanitizer.sanitize_phase(mock_phase_data)
assert result is not None
assert isinstance(result, np.ndarray)
assert result.shape == mock_phase_data.shape
assert np.all(np.isfinite(result))
def test_sanitize_performance_requirement(self, phase_sanitizer, mock_phase_data):
"""Test that phase sanitization meets performance requirements (<5ms)"""
start_time = time.perf_counter()
phase_sanitizer.sanitize_phase(mock_phase_data)
processing_time = time.perf_counter() - start_time
assert processing_time < 0.005 # < 5 ms