"""The write path's structural issue arm: an issue that names the file being written, or is open in its area, is listed before anything found by meaning, withheld from that search, measured under its own source, and not looked for at all when the user's table turns issues off at this door.""" from __future__ import annotations from unittest.mock import AsyncMock, MagicMock, patch import pytest from grimoire.services import record_kinds, record_links from tests.helpers import fake_note, fake_task from tests.test_write_path_trigger import REAL_CODE, _cfg async def _hint(linked, *, semantic=(), kinds=None, project_id=2): from grimoire.services import plugin_context as pc search = AsyncMock(return_value=list(semantic)) surfaced = MagicMock() lookup = AsyncMock(return_value=linked) patches = [ patch.object(pc, "get_writepath_config", AsyncMock(return_value=_cfg())), patch.object(pc.snippets_svc, "list_snippets", AsyncMock(return_value=([], 0))), patch.object(pc, "semantic_search_notes", search), patch.object(pc, "record_retrieval", MagicMock()), patch.object(pc, "record_surfaced", surfaced), patch.object(pc, "owner_names_for", AsyncMock(return_value={})), patch.object(record_links, "issues_for_path", lookup), ] if kinds is not None: patches.append(patch.object(record_kinds, "kinds_for", AsyncMock(return_value=kinds))) for p in patches: p.start() try: out = await pc.build_write_path_hint(1, "src/x.py", code=REAL_CODE, project_id=project_id) finally: for p in reversed(patches): p.stop() return out, search, surfaced, lookup @pytest.mark.asyncio async def test_a_linked_issue_is_listed_before_meaning_and_withheld_from_it(): issue = fake_task(id=41, title="the lock leaked in x.py", task_kind="issue", status="done", user_id=1, note_type="note", data=None) similar = (0.90, fake_note(id=7, title="a snippet like it", note_type="snippet", user_id=1)) out, search, surfaced, lookup = await _hint( [(record_links.NAMES_FILE, issue)], semantic=[similar], ) lines = [ln for ln in out["context"].splitlines() if ln.startswith("> - #")] assert lines[0].startswith('> - #41 [names this file ยท issue (done)]') assert lookup.await_args.args[:3] == (1, 2, "src/x.py") # The search by meaning is told the linked issue is already listed. assert 41 in search.await_args.kwargs["exclude_ids"] by_source = {c.kwargs["source"]: c.kwargs["note_ids"] for c in surfaced.call_args_list} assert by_source["write_path_linked"] == [41] assert "write_path_place" not in by_source @pytest.mark.asyncio async def test_no_issue_is_looked_up_when_the_door_turned_issues_off(): _out, _search, _surfaced, lookup = await _hint( [], kinds=frozenset({"snippet", "note", "lesson"}), ) lookup.assert_not_awaited() @pytest.mark.asyncio async def test_no_issue_is_looked_up_without_a_project(): _out, _search, _surfaced, lookup = await _hint([], project_id=0) lookup.assert_not_awaited()