|
| 1 | +"""TDD: Wire telemetry.build_filter_summary + IdeaGenerator integration. |
| 2 | +
|
| 3 | +Phase 4 plumbing was landed; this wires it end-to-end so the cron runner |
| 4 | +actually injects the saturation summary into Claude's prompt. |
| 5 | +
|
| 6 | +- engine/telemetry.build_filter_summary(db, ...) returns the dict shape |
| 7 | + expected by build_generation_prompt(filter_summary=...). |
| 8 | +- IdeaGenerator.generate(... filter_summary=...) forwards it to the |
| 9 | + prompt builder so cron only needs to build the summary once and pass it in. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +from datetime import UTC, datetime, timedelta |
| 15 | + |
| 16 | +import pytest |
| 17 | +import pytest_asyncio |
| 18 | + |
| 19 | +from project_forge.models import FilteredIdea, Idea, IdeaCategory |
| 20 | +from project_forge.storage.db import Database |
| 21 | + |
| 22 | + |
| 23 | +def _idea(name: str, *, category: IdeaCategory = IdeaCategory.SECURITY_TOOL) -> Idea: |
| 24 | + return Idea( |
| 25 | + name=name, |
| 26 | + tagline=f"tag for {name}", |
| 27 | + description="d", |
| 28 | + category=category, |
| 29 | + market_analysis="m", |
| 30 | + feasibility_score=0.8, |
| 31 | + mvp_scope="mvp", |
| 32 | + tech_stack=["python"], |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def _filtered(name: str, *, category: IdeaCategory = IdeaCategory.SECURITY_TOOL, |
| 37 | + days_ago: int = 1) -> FilteredIdea: |
| 38 | + fi = FilteredIdea( |
| 39 | + idea_name=name, |
| 40 | + idea_tagline="t", |
| 41 | + idea_category=category, |
| 42 | + filter_reason="duplicate:tagline_similarity:0.9", |
| 43 | + original_idea_json="{}", |
| 44 | + ) |
| 45 | + fi.filtered_at = datetime.now(UTC) - timedelta(days=days_ago) |
| 46 | + return fi |
| 47 | + |
| 48 | + |
| 49 | +@pytest_asyncio.fixture |
| 50 | +async def db(tmp_path): |
| 51 | + d = Database(tmp_path / "wire_filter.db") |
| 52 | + await d.connect() |
| 53 | + yield d |
| 54 | + await d.close() |
| 55 | + |
| 56 | + |
| 57 | +# ── build_filter_summary: produces shape consumed by build_generation_prompt |
| 58 | + |
| 59 | + |
| 60 | +class TestBuildFilterSummary: |
| 61 | + @pytest.mark.asyncio |
| 62 | + async def test_returns_expected_keys(self, db): |
| 63 | + from project_forge.engine.telemetry import build_filter_summary |
| 64 | + |
| 65 | + summary = await build_filter_summary(db) |
| 66 | + assert "saturated_concepts" in summary |
| 67 | + assert "high_filter_rate_categories" in summary |
| 68 | + |
| 69 | + @pytest.mark.asyncio |
| 70 | + async def test_lists_top_saturated_concepts(self, db): |
| 71 | + from project_forge.engine.telemetry import build_filter_summary |
| 72 | + |
| 73 | + for name in ( |
| 74 | + "certificate alpha", "certificate beta", "certificate gamma", |
| 75 | + "certificate delta", "certificate epsilon", |
| 76 | + ): |
| 77 | + await db.save_filtered_idea(_filtered(name)) |
| 78 | + for name in ("detection one", "detection two"): |
| 79 | + await db.save_filtered_idea(_filtered(name)) |
| 80 | + |
| 81 | + summary = await build_filter_summary(db, top_concepts=2) |
| 82 | + # Most-rejected concept first |
| 83 | + assert summary["saturated_concepts"][0] == "certificate" |
| 84 | + assert len(summary["saturated_concepts"]) <= 2 |
| 85 | + |
| 86 | + @pytest.mark.asyncio |
| 87 | + async def test_lists_high_filter_rate_categories(self, db): |
| 88 | + from project_forge.engine.telemetry import build_filter_summary |
| 89 | + |
| 90 | + # security-tool: 1 accept, 4 reject → 0.80 |
| 91 | + await db.save_idea(_idea("Accept ST")) |
| 92 | + for n in ("R1", "R2", "R3", "R4"): |
| 93 | + await db.save_filtered_idea(_filtered(n)) |
| 94 | + # privacy: 1 accept, 0 reject → 0.00 (well below threshold) |
| 95 | + await db.save_idea(_idea("Accept PR", category=IdeaCategory.PRIVACY)) |
| 96 | + |
| 97 | + summary = await build_filter_summary(db, rate_threshold=0.5) |
| 98 | + cats = [c for c, _ in summary["high_filter_rate_categories"]] |
| 99 | + assert "security-tool" in cats |
| 100 | + assert "privacy" not in cats |
| 101 | + |
| 102 | + @pytest.mark.asyncio |
| 103 | + async def test_empty_db_returns_empty_lists(self, db): |
| 104 | + from project_forge.engine.telemetry import build_filter_summary |
| 105 | + |
| 106 | + summary = await build_filter_summary(db) |
| 107 | + assert summary["saturated_concepts"] == [] |
| 108 | + assert summary["high_filter_rate_categories"] == [] |
| 109 | + |
| 110 | + @pytest.mark.asyncio |
| 111 | + async def test_categories_sorted_by_rate_descending(self, db): |
| 112 | + from project_forge.engine.telemetry import build_filter_summary |
| 113 | + |
| 114 | + # security-tool: 4 of 5 = 0.80 |
| 115 | + await db.save_idea(_idea("Accept ST")) |
| 116 | + for n in ("R1", "R2", "R3", "R4"): |
| 117 | + await db.save_filtered_idea(_filtered(n, category=IdeaCategory.SECURITY_TOOL)) |
| 118 | + # automation: 7 of 10 = 0.70 |
| 119 | + for i in range(3): |
| 120 | + await db.save_idea(_idea(f"Acc auto {i}", category=IdeaCategory.AUTOMATION)) |
| 121 | + for i in range(7): |
| 122 | + await db.save_filtered_idea(_filtered(f"Rej auto {i}", category=IdeaCategory.AUTOMATION)) |
| 123 | + |
| 124 | + summary = await build_filter_summary(db, rate_threshold=0.6) |
| 125 | + rates = summary["high_filter_rate_categories"] |
| 126 | + # First entry must have higher rate than second |
| 127 | + assert len(rates) >= 2 |
| 128 | + assert rates[0][1] >= rates[1][1] |
| 129 | + |
| 130 | + |
| 131 | +# ── IdeaGenerator forwards filter_summary to build_generation_prompt |
| 132 | + |
| 133 | + |
| 134 | +class TestIdeaGeneratorFilterSummaryForwarding: |
| 135 | + def test_generate_accepts_filter_summary_kwarg(self, monkeypatch): |
| 136 | + """IdeaGenerator.generate must accept and forward filter_summary.""" |
| 137 | + import asyncio |
| 138 | + |
| 139 | + from project_forge.engine.generator import IdeaGenerator |
| 140 | + |
| 141 | + captured = {} |
| 142 | + |
| 143 | + _FAKE_JSON = ( |
| 144 | + '{"name":"X","tagline":"t","description":"d","category":"security-tool",' |
| 145 | + '"market_analysis":"m","feasibility_score":0.8,"mvp_scope":"mvp",' |
| 146 | + '"tech_stack":["py"]}' |
| 147 | + ) |
| 148 | + |
| 149 | + # Stub anthropic + the prompt builder so we observe what was passed |
| 150 | + class _StubMessages: |
| 151 | + def create(self, **kwargs): # noqa: ARG002 |
| 152 | + class _Resp: |
| 153 | + content = [type("X", (), {"text": _FAKE_JSON})] |
| 154 | + return _Resp() |
| 155 | + |
| 156 | + class _StubClient: |
| 157 | + messages = _StubMessages() |
| 158 | + |
| 159 | + def stub_build_prompt(**kwargs): |
| 160 | + captured.update(kwargs) |
| 161 | + return "PROMPT" |
| 162 | + |
| 163 | + gen = IdeaGenerator.__new__(IdeaGenerator) |
| 164 | + gen.client = _StubClient() |
| 165 | + gen.model = "stub-model" |
| 166 | + monkeypatch.setattr( |
| 167 | + "project_forge.engine.generator.build_generation_prompt", |
| 168 | + stub_build_prompt, |
| 169 | + ) |
| 170 | + |
| 171 | + summary = { |
| 172 | + "saturated_concepts": ["certificate"], |
| 173 | + "high_filter_rate_categories": [("security-tool", 0.80)], |
| 174 | + } |
| 175 | + |
| 176 | + asyncio.run(gen.generate( |
| 177 | + category=IdeaCategory.SECURITY_TOOL, |
| 178 | + filter_summary=summary, |
| 179 | + )) |
| 180 | + |
| 181 | + assert captured.get("filter_summary") == summary |
0 commit comments