"""The one delivery order, key by key, and both rule arms reading it.""" from types import SimpleNamespace from unittest.mock import AsyncMock, MagicMock import pytest from grimoire.services import retrieval_pipeline as rp from grimoire.services import rule_order as ro def _r(rid, enforcement="inform", salience="normal"): return SimpleNamespace(id=rid, title=f"rule {rid}", kind="rule", when_to_apply="when", enforcement=enforcement, salience=salience) def _ids(cands): return [c.rule.id for c in ro.ordered(cands)] def test_a_rule_that_stops_the_work_outranks_any_line_beside_it(): cands = [ro.Candidate(_r(1), similarity=0.95), ro.Candidate(_r(2, "hold"), similarity=0.71), ro.Candidate(_r(3, "enforce"), similarity=0.70)] assert _ids(cands) == [3, 2, 1] def test_salience_decides_between_rules_of_the_same_enforcement(): cands = [ro.Candidate(_r(1, salience="low")), ro.Candidate(_r(2)), ro.Candidate(_r(3, salience="high"))] assert _ids(cands) == [3, 2, 1] def test_a_trigger_outranks_a_mount_and_a_mount_outranks_a_resemblance(): cands = [ro.Candidate(_r(1), specificity=ro.BY_RESEMBLANCE, similarity=0.9), ro.Candidate(_r(2), specificity=ro.BY_MOUNT), ro.Candidate(_r(3), specificity=ro.BY_TRIGGER)] assert _ids(cands) == [3, 2, 1] def test_a_first_mention_outranks_a_reminder_at_the_same_rank(): cands = [ro.Candidate(_r(1), seen=True), ro.Candidate(_r(2))] assert _ids(cands) == [2, 1] def test_history_breaks_the_tie_before_similarity_does(): cands = [ro.Candidate(_r(1), opened_rate=0.1, similarity=0.9), ro.Candidate(_r(2), opened_rate=0.8, similarity=0.7), ro.Candidate(_r(3), similarity=0.99)] # Unknown history ranks between them, as an even chance. assert _ids(cands) == [2, 3, 1] def test_ties_keep_the_arms_own_order(): cands = [ro.Candidate(_r(i)) for i in (4, 2, 9)] assert _ids(cands) == [4, 2, 9] def test_a_rule_with_no_declared_fields_reads_as_inform_normal(): bare = SimpleNamespace(id=1) assert ro.order_key(ro.Candidate(bare))[:2] == (2, 1) @pytest.mark.parametrize(("delivered", "opened", "rate"), [(0, 0, 0.5), (1, 1, 2 / 3), (10, 0, 1 / 12)]) def test_the_open_rate_is_smoothed(delivered, opened, rate): assert ro.opened_rate(delivered, opened) == pytest.approx(rate) PUSH = [{"moment": "work.deliver", "tool": "Bash", "match": "git push", "via": "default"}] @pytest.mark.asyncio async def test_the_moment_arm_delivers_in_that_order_before_its_cap(): pairs = [(_r(i), "work.deliver") for i in range(1, 11)] + [(_r(99, "hold"), "work.deliver")] result = await rp.run_moment_arm( PUSH, rp.RuleMoment(user_id=1, query="", project_id=2), io=rp.MomentIO(lookup=AsyncMock(return_value=pairs), record_rule_surfaced=MagicMock()), ) # The holding rule was mounted last and still leads; the cap cut ordinary ones. assert result.shown_rule_ids[0] == 99 assert len(result.shown_rule_ids) == rp.MOMENT_RULE_LIMIT @pytest.mark.asyncio async def test_the_semantic_arm_delivers_in_that_order(): hits = [(0.91, _r(1)), (0.74, _r(2, "hold"))] io = rp.RuleIO(search=AsyncMock(return_value=hits), record_retrieval=MagicMock(), record_rule_surfaced=MagicMock()) result = await rp.run_rule_arm( rp.PROMPT_RULE, rp.RuleMoment(user_id=1, query="q", project_id=2, where="here"), floor=0.5, budget=5, io=io, ) assert result.shown_rule_ids[:2] == [2, 1] @pytest.mark.asyncio async def test_each_line_shown_is_recorded_as_a_delivery_with_its_form(): recorded = MagicMock() pairs = [(_r(5), "work.finish"), (_r(6), "work.finish")] await rp.run_moment_arm( PUSH, rp.RuleMoment(user_id=1, query="", project_id=2, exclude=frozenset({6})), io=rp.MomentIO(lookup=AsyncMock(return_value=pairs), record_rule_surfaced=MagicMock(), record_delivery=recorded), ) _uid, _sid, source, lines = recorded.call_args.args assert source == rp.MOMENT_RULE_SOURCE assert lines == [(5, "work.finish", "full"), (6, "work.finish", "repeat")] @pytest.mark.asyncio async def test_history_reorders_rules_of_equal_rank_and_a_failed_read_is_unknown(): pairs = [(_r(5), "work.finish"), (_r(6), "work.finish")] async def history(_uid, _pairs): return {6: 0.9, 5: 0.1} result = await rp.run_moment_arm( PUSH, rp.RuleMoment(user_id=1, query="", project_id=2), io=rp.MomentIO(lookup=AsyncMock(return_value=pairs), record_rule_surfaced=MagicMock(), history=history), ) assert result.shown_rule_ids == [6, 5] async def broken(_uid, _pairs): raise RuntimeError("no database") result = await rp.run_moment_arm( PUSH, rp.RuleMoment(user_id=1, query="", project_id=2), io=rp.MomentIO(lookup=AsyncMock(return_value=pairs), record_rule_surfaced=MagicMock(), history=broken), ) assert result.shown_rule_ids == [5, 6]