"""A pointer line names a record once a session, and is counted once. A pointer line names, by id, records that cleared the floor but got no line of their own. The plugin's ledger of what a session has seen holds only the records given a line, so the server asks its own delivery ledger which records this session was already shown or pointed at, and leaves those out. Without that a handoff note is named again on every edit and every message. And each delivery is booked once: the snippets beside an edited file are named in a pointer line, so they are `pointed`, never also `pushed`. """ from __future__ import annotations import re from unittest.mock import AsyncMock, MagicMock, patch import pytest from grimoire.services import note_usage from grimoire.services import plugin_context as pc from tests.helpers import fake_note # Six equally close notes: the menu shows its top three, and the rest are # named in the pointer line. HITS = [(0.8 - i * 0.001, fake_note(id=20 + i, title=f"a note {i}", note_type="note", user_id=1)) for i in range(6)] async def _pointer(ledger: set[int]) -> tuple[list[int], MagicMock]: """The ids the auto-inject pointer line names, given what the session's delivery ledger already holds, and the booking call.""" calls: list[int] = [] async def _search(*_a, **kw): calls.append(1) return HITS if len(calls) == 1 else [] booked = MagicMock() with patch.object(pc, "get_autoinject_config", AsyncMock(return_value={"enabled": True, "threshold": 0.55, "top_k": 3})), \ patch.object(pc, "semantic_search_notes", _search), \ patch.object(pc, "superseded_ids", AsyncMock(return_value=set())), \ patch.object(pc, "system_names_for", AsyncMock(return_value={})), \ patch.object(pc, "record_retrieval", MagicMock()), \ patch.object(pc, "record_surfaced", MagicMock()), \ patch.object(pc.record_deliveries_svc, "delivered_this_session", AsyncMock(return_value=set(ledger))), \ patch.object(pc.record_deliveries_svc, "record", booked): out = (await pc.build_autoinject_hint(1, "q", project_id=2))["context"] line = next((ln for ln in out.splitlines() if pc.POINTER_LEAD in ln), "") return [int(i) for i in re.findall(r"#(\d+)", line)], booked @pytest.mark.asyncio async def test_a_pointer_names_what_the_session_has_not_met(): named, booked = await _pointer(set()) assert named, "six close notes and a menu of three should leave a pointer line" booked.assert_called_once() assert list(booked.call_args.args[1]) == named assert booked.call_args.args[2] == "auto_inject_pointer" @pytest.mark.asyncio async def test_a_record_already_pointed_at_this_session_is_not_named_again(): first, _ = await _pointer(set()) again, booked = await _pointer({first[0]}) assert first[0] not in again assert set(again) == set(first) - {first[0]} # Only what the line names is booked, so the ledger stays a record of # what the session actually saw. assert first[0] not in booked.call_args.args[1] @pytest.mark.asyncio async def test_no_line_when_the_session_has_met_every_record_it_would_name(): first, _ = await _pointer(set()) again, booked = await _pointer(set(first)) assert again == [] booked.assert_not_called() # --- each delivery booked once -------------------------------------------- def _booked_for(source: str) -> MagicMock: booked = MagicMock() with patch.object(note_usage, "_schedule"), \ patch("grimoire.services.record_deliveries.record", booked): note_usage.record_surfaced(user_id=1, note_ids=[7], source=source, project_id=2) return booked def test_snippets_beside_an_edited_file_are_not_also_booked_as_pushed(): # Their door books them as `pointed` when it names them. _booked_for("write_path_place").assert_not_called() def test_a_line_shown_in_full_is_booked_as_pushed(): # The same call books an ordinary line, so the test above is about the # place arm and not about booking being switched off. _booked_for("write_path_sync").assert_called_once()