"""A hook payload fitted to a budget: what is kept, in what order, what its ids say, and which rule deliveries are recorded.""" import contextvars from unittest.mock import MagicMock, patch from grimoire.services import hook_payload as hp from grimoire.services import rule_deliveries from grimoire.services.hook_payload import Part def _p(text, **kw): return Part(text=text, **kw) def test_parts_are_kept_in_order_and_the_payload_stays_within_the_budget(): parts = [_p("a" * 600, ids={hp.RULE: (1,)}), _p("b" * 600, ids={hp.RULE: (2,)}), _p("c" * 600, ids={hp.NOTE: (9,)})] fitted = hp.assemble(parts, 1300) assert len(fitted.context) <= 1300 assert fitted.ids(hp.RULE) == [1, 2] and fitted.ids(hp.NOTE) == [] assert fitted.context.index("a") < fitted.context.index("b") def test_a_part_that_does_not_fit_is_skipped_and_a_smaller_later_one_still_fits(): parts = [_p("r" * 500), _p("S" * 2000, ids={hp.SHAPE: ("core",)}), _p("n" * 300, ids={hp.NOTE: (4,)})] fitted = hp.assemble(parts, 1000) assert "S" not in fitted.context assert fitted.ids(hp.SHAPE) == [] and fitted.ids(hp.NOTE) == [4] def test_a_blocks_header_prints_once_before_its_first_kept_part_and_is_charged(): head = "> Possibly relevant:" parts = [_p("> - #1 one", block="menu", header=head, ids={hp.NOTE: (1,)}), _p("> - #2 two", block="menu", header=head, ids={hp.NOTE: (2,)})] fitted = hp.assemble(parts, 5000) assert fitted.context == f"{head}\n> - #1 one\n> - #2 two" # The header counts against the part it rides: one that fits alone does # not fit once its block's header is added. long_head, line = "h" * 600, "x" * 500 tight = hp.assemble([_p(line, block="m", header=long_head, ids={hp.NOTE: (1,)})], 1000) assert tight.context == "" and tight.ids(hp.NOTE) == [] def test_separate_blocks_are_separated_by_a_blank_line(): fitted = hp.assemble([_p("rule", block="rules"), _p("shape")], 5000) assert fitted.context == "rule\n\nshape" def test_keep_only_filters_a_response_list_to_what_was_shown(): fitted = hp.assemble([_p("x" * 400, ids={hp.RULE: (3,)}), _p("y" * 9000, ids={hp.RULE: (5,)})], 2000) assert fitted.keep_only(hp.RULE, [5, 3, 7]) == [3] def test_the_budget_is_bounded_under_the_hook_context_cap(): assert hp.HARD_MAX < 8_000 assert hp.clamp(50_000) == hp.HARD_MAX assert hp.clamp(10) == hp.MIN_BUDGET assert hp.clamp("not a number") == hp.DEFAULT_BUDGET assert hp.MIN_BUDGET <= hp.DEFAULT_BUDGET <= hp.HARD_MAX def test_a_result_without_parts_is_one_part_carrying_all_its_ids(): result = {"context": "whole", "rule_ids": [1, 2]} parts = hp.parts_of(result, {hp.RULE: "rule_ids"}) assert parts == [Part(text="whole", ids={hp.RULE: (1, 2)})] assert hp.parts_of({"context": ""}, {}) == [] own = {"context": "c", "parts": [_p("p")]} assert hp.parts_of(own, {}) == [_p("p")] and "parts" not in own def test_rule_parts_pair_each_line_with_its_rule(): parts = hp.rule_parts(["one", "two"], [11, 34], block="rules") assert [p.ids[hp.RULE] for p in parts] == [(11,), (34,)] # ── the delivery record ──────────────────────────────────────────────── # Each runs in a copy of the context, so the session it binds ends with it. def test_a_bound_request_records_only_the_rule_lines_that_were_kept(): contextvars.copy_context().run(_bound_request_records_only_kept) def _bound_request_records_only_kept(): spawned = MagicMock() with patch.object(rule_deliveries, "spawn", spawned), \ patch.object(rule_deliveries, "_insert", lambda rows: rows): rule_deliveries.bind_session("s-1") rule_deliveries.record(1, None, "pre_tool_rule", [(11, "pre_tool_rule", "full"), (34, "pre_tool_rule", "full")]) assert not spawned.called, "a bound request's rows wait for the payload" rule_deliveries.flush({11}) rows = spawned.call_args.args[0] assert [r["rule_id"] for r in rows] == [11] spawned.reset_mock() rule_deliveries.flush() assert not spawned.called, "the buffer is spent once flushed" def test_a_session_named_outright_records_at_once(): contextvars.copy_context().run(_named_session_records_at_once) def _named_session_records_at_once(): spawned = MagicMock() with patch.object(rule_deliveries, "spawn", spawned), \ patch.object(rule_deliveries, "_insert", lambda rows: rows): rule_deliveries.bind_session("s-2") rule_deliveries.record(1, "other", "x", [(5, "x", "full")]) assert spawned.called