feat: YAML pipeline template autocomplete (AzDO + GHA)
All checks were successful
Build and Deploy iLSP / test (push) Successful in 25s
Build and Deploy iLSP / build-and-deploy (push) Successful in 1m34s

- Add scripts/sync_pipeline_templates.py — scans LRU AzDO and GHA template
  repos; outputs unified pipeline_templates_catalog.json (48 templates: 45
  AzDO + 3 GHA)
- Add scripts/template_sources.yml — source config (AzDO alias, GHA org)
- Add pipeline_templates_catalog.json — baked catalog (49 KB)
- Add ilsp/yaml_lsp/catalog.py — PipelineTemplateCatalog with completion item
  generators for template paths, param names, allowed values, GHA inputs
- Add ilsp/yaml_lsp/proxy.py — async WS↔TCP bridge with LSP frame buffering,
  per-connection document tracking, AzDO/GHA context detection, and completion
  injection (LRU items sortText 0_, standard items downgraded to 9_)
- Wire yaml_ws_handler into server.py (replaces raw _ws_proxy call)
- Load PipelineTemplateCatalog at startup; reload + health report template count
- Update push_catalogs.sh to push pipeline_templates_catalog.json
- Update Dockerfile to bake pipeline_templates_catalog.json as image fallback
- Add tests/test_yaml_catalog.py (14 tests) + tests/test_yaml_proxy.py (18 tests)
  All 67 tests green
This commit is contained in:
Henrik Jess Nielsen
2026-05-10 15:59:37 +02:00
parent 5501254b55
commit 333d986e76
11 changed files with 3328 additions and 12 deletions

149
tests/test_yaml_catalog.py Normal file
View File

@@ -0,0 +1,149 @@
"""Tests for PipelineTemplateCatalog — loading and completion items."""
import json
import pathlib
import tempfile
import pytest
from ilsp.yaml_lsp.catalog import PipelineTemplateCatalog
_SAMPLE_CATALOG = {
"synced_at": "2025-01-01T00:00:00+00:00",
"template_count": 3,
"templates": {
"tasks/k8s/deploy.yaml@pipeline-templates": {
"format": "azdo",
"title": "deploy",
"path": "tasks/k8s/deploy.yaml",
"alias": "pipeline-templates",
"parameters": [
{"name": "projectName", "type": "string", "required": True},
{"name": "environment", "type": "string", "required": True,
"allowed": ["dev", "test", "prod"]},
{"name": "imageTag", "type": "string", "required": False, "default": "latest"},
],
},
"tasks/node/build-and-publish-nextjs.yml@pipeline-templates": {
"format": "azdo",
"title": "build-and-publish-nextjs",
"path": "tasks/node/build-and-publish-nextjs.yml",
"alias": "pipeline-templates",
"parameters": [
{"name": "buildScriptName", "type": "string", "required": True},
{"name": "npmExecuteable", "type": "string", "required": False,
"default": "npm", "allowed": ["npm", "pnpm"]},
],
},
"LRU-Digital/CommonLoginTestApplication/.github/workflows/k8s.yml@main": {
"format": "gha",
"title": "k8s",
"org": "LRU-Digital",
"repo": "CommonLoginTestApplication",
"path": ".github/workflows/k8s.yml",
"ref": "main",
"parameters": [
{"name": "environment", "type": "string", "required": True},
{"name": "project-name", "type": "string", "required": True},
{"name": "short-name", "type": "string", "required": True},
{"name": "acr-name", "type": "string", "required": False, "default": "lruk8s"},
],
},
},
}
@pytest.fixture(autouse=True)
def load_catalog(tmp_path, monkeypatch):
"""Write a temp catalog and load it."""
catalog_file = tmp_path / "pipeline_templates_catalog.json"
catalog_file.write_text(json.dumps(_SAMPLE_CATALOG), encoding="utf-8")
import ilsp.yaml_lsp.catalog as mod
original_paths = mod._CATALOG_PATHS
monkeypatch.setattr(mod, "_CATALOG_PATHS", [catalog_file])
PipelineTemplateCatalog.load()
yield
monkeypatch.setattr(mod, "_CATALOG_PATHS", original_paths)
class TestCatalogLoad:
def test_template_count(self):
assert PipelineTemplateCatalog.template_count() == 3
def test_get_template_exists(self):
tmpl = PipelineTemplateCatalog.get_template("tasks/k8s/deploy.yaml@pipeline-templates")
assert tmpl is not None
assert tmpl["format"] == "azdo"
def test_get_template_missing(self):
assert PipelineTemplateCatalog.get_template("nonexistent@alias") is None
class TestAzdoCompletions:
def test_azdo_template_keys(self):
items = PipelineTemplateCatalog.azdo_template_completion_items()
labels = [i["label"] for i in items]
assert "tasks/k8s/deploy.yaml" in labels
assert "tasks/node/build-and-publish-nextjs.yml" in labels
# GHA not included
assert not any("github/workflows" in l for l in labels)
def test_azdo_param_names(self):
items = PipelineTemplateCatalog.azdo_param_completion_items(
"tasks/k8s/deploy.yaml@pipeline-templates"
)
names = [i["filterText"] for i in items]
assert "projectName" in names
assert "environment" in names
assert "imageTag" in names
def test_required_marked_in_label(self):
items = PipelineTemplateCatalog.azdo_param_completion_items(
"tasks/k8s/deploy.yaml@pipeline-templates"
)
by_name = {i["filterText"]: i for i in items}
assert by_name["projectName"]["label"] == "projectName*"
assert by_name["imageTag"]["label"] == "imageTag"
def test_azdo_allowed_values(self):
items = PipelineTemplateCatalog.azdo_param_value_items(
"tasks/k8s/deploy.yaml@pipeline-templates",
"environment",
)
vals = [i["label"] for i in items]
assert vals == ["dev", "test", "prod"]
def test_azdo_no_allowed_values_returns_empty(self):
items = PipelineTemplateCatalog.azdo_param_value_items(
"tasks/k8s/deploy.yaml@pipeline-templates",
"projectName",
)
assert items == []
def test_azdo_unknown_template_param_names_empty(self):
assert PipelineTemplateCatalog.azdo_param_completion_items("no@such") == []
class TestGhaCompletions:
def test_gha_workflow_refs(self):
items = PipelineTemplateCatalog.gha_workflow_completion_items()
labels = [i["label"] for i in items]
assert any("CommonLoginTestApplication" in l for l in labels)
# AzDO not included
assert not any("@pipeline-templates" in l for l in labels)
def test_gha_input_names(self):
key = "LRU-Digital/CommonLoginTestApplication/.github/workflows/k8s.yml@main"
items = PipelineTemplateCatalog.gha_input_completion_items(key)
names = [i["filterText"] for i in items]
assert "environment" in names
assert "project-name" in names
assert "acr-name" in names
def test_gha_required_marked(self):
key = "LRU-Digital/CommonLoginTestApplication/.github/workflows/k8s.yml@main"
items = PipelineTemplateCatalog.gha_input_completion_items(key)
by_name = {i["filterText"]: i for i in items}
assert by_name["environment"]["label"] == "environment*"
assert by_name["acr-name"]["label"] == "acr-name"

232
tests/test_yaml_proxy.py Normal file
View File

@@ -0,0 +1,232 @@
"""Tests for YAML LSP proxy — document format detection and context detection."""
import json
import pytest
from ilsp.yaml_lsp.proxy import (
_detect_doc_format,
_detect_azdo_context,
_detect_gha_context,
_inject_completions,
_LspFrameBuffer,
)
from ilsp.yaml_lsp.catalog import PipelineTemplateCatalog
# ── LspFrameBuffer ─────────────────────────────────────────────────────────────
class TestLspFrameBuffer:
def test_single_frame(self):
body = b'{"jsonrpc":"2.0"}'
framed = f"Content-Length: {len(body)}\r\n\r\n".encode() + body
buf = _LspFrameBuffer()
frames = buf.feed(framed)
assert frames == [body]
def test_two_frames_in_one_feed(self):
body1 = b'{"a":1}'
body2 = b'{"b":2}'
def frame(b): return f"Content-Length: {len(b)}\r\n\r\n".encode() + b
buf = _LspFrameBuffer()
frames = buf.feed(frame(body1) + frame(body2))
assert frames == [body1, body2]
def test_partial_frame_buffered(self):
body = b'{"partial":true}'
header = f"Content-Length: {len(body)}\r\n\r\n".encode()
buf = _LspFrameBuffer()
assert buf.feed(header + body[:3]) == []
frames = buf.feed(body[3:])
assert frames == [body]
# ── Document format detection ──────────────────────────────────────────────────
class TestDocFormatDetection:
def test_azdo_template_line(self):
lines = [
"trigger: [main]",
"stages:",
" - stage: Build",
" jobs:",
" - template: tasks/k8s/deploy.yaml@pipeline-templates",
]
assert _detect_doc_format(lines) == "azdo"
def test_azdo_pipeline_keywords(self):
lines = ["trigger: [main]", "stages:", " - stage: Build"]
assert _detect_doc_format(lines) == "azdo"
def test_gha_workflow_call(self):
lines = [
"on:",
" workflow_call:",
" inputs:",
" environment:",
" type: string",
]
assert _detect_doc_format(lines) == "gha"
def test_gha_runs_on(self):
lines = [
"on: [push]",
"jobs:",
" build:",
" runs-on: ubuntu-latest",
]
assert _detect_doc_format(lines) == "gha"
def test_unknown_empty(self):
assert _detect_doc_format([]) == "unknown"
# ── AzDO context detection ────────────────────────────────────────────────────
class TestAzdoContextDetection:
def test_template_path_context(self):
lines = [
" - template: tasks/k8s/",
]
ctx = _detect_azdo_context(lines, 0, len(lines[0]))
assert ctx["type"] == "template_path"
assert ctx["format"] == "azdo"
def test_param_name_context(self):
lines = [
" - template: tasks/k8s/deploy.yaml@pipeline-templates",
" parameters:",
" projectName: ",
" ",
]
# Cursor on line 3 (blank), after parameters block
ctx = _detect_azdo_context(lines, 3, 10)
# Should detect we're in a parameters block under a template
assert ctx["format"] == "azdo"
# type is param_name (we're inside parameters block)
assert ctx["type"] in ("param_name", "unknown")
def test_template_path_prefix_extracted(self):
lines = [" - template: tasks/k8s/dep"]
ctx = _detect_azdo_context(lines, 0, len(lines[0]))
assert ctx["type"] == "template_path"
assert "dep" in ctx["prefix"]
# ── GHA context detection ──────────────────────────────────────────────────────
class TestGhaContextDetection:
def test_workflow_ref_context(self):
lines = [
" - name: Deploy",
" uses: LRU-Digital/CommonLoginTestApplication/.github/workflows/k8s.yml@",
]
ctx = _detect_gha_context(lines, 1, len(lines[1]))
assert ctx["type"] == "workflow_ref"
assert ctx["format"] == "gha"
def test_input_name_context(self):
lines = [
" uses: LRU-Digital/CommonLoginTestApplication/.github/workflows/k8s.yml@main",
" with:",
" ",
]
ctx = _detect_gha_context(lines, 2, 8)
assert ctx["format"] == "gha"
assert ctx["type"] in ("input_name", "unknown")
def test_workflow_ref_prefix_extracted(self):
lines = [" uses: LRU-Digital/Repo/.github/workflows/dep"]
ctx = _detect_gha_context(lines, 0, len(lines[0]))
assert ctx["type"] == "workflow_ref"
assert "dep" in ctx["prefix"]
# ── Completion injection ───────────────────────────────────────────────────────
_SAMPLE_CATALOG_DATA = {
"synced_at": "2025-01-01T00:00:00+00:00",
"template_count": 1,
"templates": {
"tasks/k8s/deploy.yaml@pipeline-templates": {
"format": "azdo",
"title": "deploy",
"path": "tasks/k8s/deploy.yaml",
"alias": "pipeline-templates",
"parameters": [
{"name": "projectName", "type": "string", "required": True},
{"name": "environment", "type": "string", "required": True,
"allowed": ["dev", "test", "prod"]},
],
},
},
}
@pytest.fixture(autouse=True)
def load_catalog(tmp_path, monkeypatch):
import json as _json
f = tmp_path / "pipeline_templates_catalog.json"
f.write_text(_json.dumps(_SAMPLE_CATALOG_DATA))
import ilsp.yaml_lsp.catalog as mod
monkeypatch.setattr(mod, "_CATALOG_PATHS", [f])
PipelineTemplateCatalog.load()
class TestInjection:
def test_azdo_template_path_injects_at_top(self):
msg = {"jsonrpc": "2.0", "id": 1, "result": {"items": [{"label": "existing"}]}}
ctx = {"type": "template_path", "format": "azdo", "prefix": ""}
modified = json.loads(_inject_completions(msg, ctx))
labels = [i["label"] for i in modified["result"]["items"]]
assert "tasks/k8s/deploy.yaml" in labels
# LRU items appear before standard items
lru_idx = next(i for i, l in enumerate(labels) if "deploy.yaml" in l)
existing_idx = next(i for i, l in enumerate(labels) if l == "existing")
assert lru_idx < existing_idx
def test_azdo_param_names_injected(self):
msg = {"jsonrpc": "2.0", "id": 2, "result": {"items": []}}
ctx = {
"type": "param_name", "format": "azdo",
"template_key": "tasks/k8s/deploy.yaml@pipeline-templates",
}
modified = json.loads(_inject_completions(msg, ctx))
names = [i.get("filterText", i["label"]) for i in modified["result"]["items"]]
assert "projectName" in names
assert "environment" in names
def test_azdo_allowed_values_injected(self):
msg = {"jsonrpc": "2.0", "id": 3, "result": {"items": []}}
ctx = {
"type": "param_value", "format": "azdo",
"template_key": "tasks/k8s/deploy.yaml@pipeline-templates",
"param": "environment",
}
modified = json.loads(_inject_completions(msg, ctx))
vals = [i["label"] for i in modified["result"]["items"]]
assert "dev" in vals
assert "test" in vals
assert "prod" in vals
def test_unknown_context_no_injection(self):
msg = {"jsonrpc": "2.0", "id": 4, "result": {"items": [{"label": "x"}]}}
ctx = {"type": "unknown", "format": "unknown"}
modified = json.loads(_inject_completions(msg, ctx))
assert modified["result"]["items"] == [{"label": "x"}]
def test_no_result_passthrough(self):
msg = {"jsonrpc": "2.0", "id": 5}
ctx = {"type": "template_path", "format": "azdo"}
modified = json.loads(_inject_completions(msg, ctx))
assert modified == msg
def test_existing_items_downgraded(self):
msg = {
"jsonrpc": "2.0", "id": 6,
"result": {"items": [{"label": "standard", "sortText": "aaa"}]},
}
ctx = {"type": "template_path", "format": "azdo", "prefix": ""}
modified = json.loads(_inject_completions(msg, ctx))
items = modified["result"]["items"]
std = next(i for i in items if i["label"] == "standard")
assert std["sortText"].startswith("9_")