|
| 1 | +"""TDD: Shadow validation — verify generation patches actually move metrics. |
| 2 | +
|
| 3 | +Phase 3 (issue #56). SI's generation-mode patches now declare a target |
| 4 | +metric; this phase enforces it. After applying a patch, the runner |
| 5 | +generates a small batch of ideas in an isolated temp DB and compares |
| 6 | +metric snapshots before/after. If the declared target didn't move |
| 7 | +in the right direction, the patch is rejected. |
| 8 | +
|
| 9 | +Tests target the pure logic: |
| 10 | +- parse_target_metric: extracts metric name + direction from market_analysis |
| 11 | +- metric_value: looks up a metric in a telemetry snapshot |
| 12 | +- validate_patch_against_metrics: gate returns (pass, reason) |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from project_forge.engine.shadow import ( |
| 20 | + metric_value, |
| 21 | + parse_target_metric, |
| 22 | + validate_patch_against_metrics, |
| 23 | +) |
| 24 | +from project_forge.models import IdeaCategory |
| 25 | + |
| 26 | +# ── parse_target_metric ────────────────────────────────────────────── |
| 27 | + |
| 28 | + |
| 29 | +class TestParseTargetMetric: |
| 30 | + def test_parses_simple_metric_should_drop(self): |
| 31 | + result = parse_target_metric("Target metric: filter_rate should drop.") |
| 32 | + assert result is not None |
| 33 | + name, direction = result |
| 34 | + assert name == "filter_rate" |
| 35 | + assert direction == "drop" |
| 36 | + |
| 37 | + def test_parses_indexed_metric(self): |
| 38 | + result = parse_target_metric( |
| 39 | + "Target metric: filter_rate[security-tool] should drop by 10%.", |
| 40 | + ) |
| 41 | + assert result is not None |
| 42 | + name, direction = result |
| 43 | + assert name == "filter_rate[security-tool]" |
| 44 | + assert direction == "drop" |
| 45 | + |
| 46 | + def test_parses_should_rise(self): |
| 47 | + result = parse_target_metric( |
| 48 | + "Target metric: novelty should rise. Current 0.91 → expected 0.85.", |
| 49 | + ) |
| 50 | + assert result is not None |
| 51 | + name, direction = result |
| 52 | + assert name == "novelty" |
| 53 | + assert direction == "rise" |
| 54 | + |
| 55 | + def test_returns_none_when_missing(self): |
| 56 | + assert parse_target_metric("This patch will be cool.") is None |
| 57 | + |
| 58 | + def test_case_insensitive(self): |
| 59 | + result = parse_target_metric("TARGET METRIC: filter_rate SHOULD drop") |
| 60 | + assert result is not None |
| 61 | + assert result[0] == "filter_rate" |
| 62 | + assert result[1] == "drop" |
| 63 | + |
| 64 | + |
| 65 | +# ── metric_value ───────────────────────────────────────────────────── |
| 66 | + |
| 67 | + |
| 68 | +def _snapshot(): |
| 69 | + return { |
| 70 | + "filter_rate_by_category": { |
| 71 | + IdeaCategory.SECURITY_TOOL: 0.85, |
| 72 | + IdeaCategory.PRIVACY: 0.40, |
| 73 | + }, |
| 74 | + "saturation_per_concept": [("certificate", 1357), ("detection", 952)], |
| 75 | + "novelty_trend": [("2026-05-07", 0.91), ("2026-05-08", 0.93)], |
| 76 | + "diversity_lever_usage": {"contrarian": 0.33}, |
| 77 | + "coverage_gaps": [IdeaCategory.SELF_IMPROVEMENT], |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | +class TestMetricValue: |
| 82 | + def test_indexed_filter_rate(self): |
| 83 | + v = metric_value(_snapshot(), "filter_rate[security-tool]") |
| 84 | + assert v == pytest.approx(0.85) |
| 85 | + |
| 86 | + def test_aggregate_filter_rate(self): |
| 87 | + # Average across categories when no index |
| 88 | + v = metric_value(_snapshot(), "filter_rate") |
| 89 | + assert v == pytest.approx((0.85 + 0.40) / 2) |
| 90 | + |
| 91 | + def test_latest_novelty(self): |
| 92 | + # 'novelty' resolves to most-recent novelty_trend value |
| 93 | + v = metric_value(_snapshot(), "novelty") |
| 94 | + assert v == pytest.approx(0.93) |
| 95 | + |
| 96 | + def test_unknown_metric_returns_none(self): |
| 97 | + assert metric_value(_snapshot(), "unobtanium") is None |
| 98 | + |
| 99 | + |
| 100 | +# ── validate_patch_against_metrics ─────────────────────────────────── |
| 101 | + |
| 102 | + |
| 103 | +class TestValidatePatch: |
| 104 | + def test_accepts_when_drop_metric_dropped(self): |
| 105 | + baseline = _snapshot() |
| 106 | + after = {**baseline, |
| 107 | + "filter_rate_by_category": { |
| 108 | + IdeaCategory.SECURITY_TOOL: 0.70, # was 0.85 |
| 109 | + IdeaCategory.PRIVACY: 0.40, |
| 110 | + }} |
| 111 | + ok, reason = validate_patch_against_metrics( |
| 112 | + baseline, after, "filter_rate[security-tool]", "drop", |
| 113 | + ) |
| 114 | + assert ok, reason |
| 115 | + |
| 116 | + def test_rejects_when_drop_metric_rose(self): |
| 117 | + baseline = _snapshot() |
| 118 | + after = {**baseline, |
| 119 | + "filter_rate_by_category": { |
| 120 | + IdeaCategory.SECURITY_TOOL: 0.92, # got worse |
| 121 | + IdeaCategory.PRIVACY: 0.40, |
| 122 | + }} |
| 123 | + ok, reason = validate_patch_against_metrics( |
| 124 | + baseline, after, "filter_rate[security-tool]", "drop", |
| 125 | + ) |
| 126 | + assert not ok |
| 127 | + assert "regress" in reason.lower() or "worse" in reason.lower() or "rose" in reason.lower() |
| 128 | + |
| 129 | + def test_rejects_when_metric_unchanged(self): |
| 130 | + baseline = _snapshot() |
| 131 | + after = {**baseline} |
| 132 | + ok, reason = validate_patch_against_metrics( |
| 133 | + baseline, after, "filter_rate[security-tool]", "drop", |
| 134 | + ) |
| 135 | + assert not ok |
| 136 | + assert "no" in reason.lower() or "unchanged" in reason.lower() or "noop" in reason.lower() |
| 137 | + |
| 138 | + def test_accepts_when_rise_metric_rose(self): |
| 139 | + baseline = _snapshot() |
| 140 | + after = { |
| 141 | + **baseline, |
| 142 | + "novelty_trend": [("2026-05-07", 0.91), ("2026-05-08", 0.85)], |
| 143 | + # Lower similarity = HIGHER novelty |
| 144 | + } |
| 145 | + # Note: target is 'novelty' which means lower similarity is better. |
| 146 | + # We model 'novelty rise' as similarity DROP — so caller must use |
| 147 | + # the right direction. Test that the comparator respects the literal direction. |
| 148 | + # If author said 'rise' and value went DOWN, that's a regression. |
| 149 | + ok, _ = validate_patch_against_metrics( |
| 150 | + baseline, after, "novelty", "rise", |
| 151 | + ) |
| 152 | + # 0.85 < 0.93, so for direction=rise this is a regress |
| 153 | + assert not ok |
| 154 | + |
| 155 | + def test_unknown_metric_rejects(self): |
| 156 | + ok, reason = validate_patch_against_metrics( |
| 157 | + _snapshot(), _snapshot(), "unobtanium", "drop", |
| 158 | + ) |
| 159 | + assert not ok |
| 160 | + assert "unknown" in reason.lower() or "missing" in reason.lower() |
0 commit comments