"""Unit tests for Bicep proxy message injection.""" import json import pytest from ilsp.bicep_lsp.modules import BicepModuleCatalog from ilsp.bicep_lsp.proxy import BicepProxy, _ContentLengthFramer def test_frame_produces_correct_header(): body = b'{"jsonrpc":"2.0"}' framed = _ContentLengthFramer.frame(body) assert framed.startswith(b"Content-Length: 17\r\n\r\n") assert framed.endswith(body) @pytest.fixture(autouse=True) def reset_modules(): BicepModuleCatalog._modules = [] yield def _make_proxy() -> BicepProxy: return BicepProxy.__new__(BicepProxy) def _completion_response(items: list) -> dict: return { "jsonrpc": "2.0", "id": 1, "result": {"isIncomplete": False, "items": items}, } def test_standard_items_not_downgraded_without_lru(): """Without LRU modules, standard items keep their original sortText.""" proxy = _make_proxy() msg = _completion_response([{"label": "Microsoft.Storage", "sortText": "az"}]) out = json.loads(proxy._maybe_inject_completions(msg)) # No LRU modules → no downgrade, original sortText preserved assert out["result"]["items"][0]["sortText"] == "az" def test_lru_modules_injected_at_top(): BicepModuleCatalog._modules = [{ "name": "appservice", "path": "bicep/modules/appservice", "versions": ["2.3.0", "latest"], "latest": "latest", "registry": "iactemplatereg.azurecr.io", }] proxy = _make_proxy() msg = _completion_response([{"label": "Microsoft.Web/sites", "sortText": "az"}]) out = json.loads(proxy._maybe_inject_completions(msg)) items = out["result"]["items"] assert items[0]["label"] == "appservice" assert items[0]["sortText"].startswith("0_lru_") assert items[1]["label"] == "Microsoft.Web/sites" assert items[1]["sortText"].startswith("1_az_") def test_list_result_also_handled(): BicepModuleCatalog._modules = [{ "name": "roleassignments", "path": "bicep/modules/roleassignments", "versions": ["2.0.0"], "latest": "2.0.0", "registry": "iactemplatereg.azurecr.io", }] proxy = _make_proxy() msg = {"jsonrpc": "2.0", "id": 2, "result": [{"label": "az-item", "sortText": "az"}]} out = json.loads(proxy._maybe_inject_completions(msg)) assert isinstance(out["result"], list) assert out["result"][0]["label"] == "roleassignments" def test_non_completion_message_passthrough(): proxy = _make_proxy() msg = {"jsonrpc": "2.0", "method": "initialized", "params": {}} out = json.loads(proxy._maybe_inject_completions(msg)) assert out["method"] == "initialized"