Files
iLSP/tests/test_proxy.py
Henrik Jess Nielsen cd17e9bfaa
Some checks failed
Build and Deploy iLSP / test (push) Successful in 19s
Build and Deploy iLSP / build-and-deploy (push) Failing after 29s
Add unit tests, smoke test script, fix CI to debian-host + test job
- tests/test_catalog.py: 5 unit tests for PypiCatalog (fetch, cache, sort prefix, completions)
- tests/test_proxy.py: 5 unit tests for BicepProxy (framing, injection, list result, passthrough)
- tests/conftest.py: pytest asyncio_mode=auto config
- scripts/smoke_test.sh: end-to-end TCP + health smoke test script
- .gitea/workflows/ci.yml: split into test + build-and-deploy jobs (test blocks deploy)
  - runs-on: debian-host (was ubuntu-latest = broken)
  - test job installs deps + runs pytest before building image
- pyproject.toml: [project.optional-dependencies] dev = pytest + pytest-asyncio
2026-05-10 12:38:41 +02:00

86 lines
2.6 KiB
Python

"""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"