"""Local tools: Mermaid check and Terraform fmt/validate/plan in a scratch directory.""" import shutil import pytest from app.tools.local import ( LOCAL_TOOLS, TerraformRunner, local_tools, mermaid_check, ) needs_terraform = pytest.mark.skipif( shutil.which("terraform") is None, reason="terraform CLI not installed" ) VALID_TF = { "main.tf": 'variable "region" {\n type = string\n}\n\noutput "region" {\n value = var.region\n}\n', } # --- mermaid --------------------------------------------------------------------------- async def test_mermaid_accepts_a_flowchart(): result = await mermaid_check( "flowchart TD\n A[Client] -->|HTTPS| B(Cloud Run)\n B --> C[(Database)]\n" ) assert result.message.startswith("Mermaid check passed") @pytest.mark.parametrize( ("diagram", "problem"), [ ("", "empty"), ("A --> B", "diagram type"), ("flowchart TD\n A[Client --> B", "Unbalanced"), ("%% comment only\n", "diagram type"), ], ) async def test_mermaid_reports_problems(diagram, problem): result = await mermaid_check(diagram) assert result.message.startswith("Mermaid check failed") assert problem in result.message # --- terraform --------------------------------------------------------------------------- @needs_terraform async def test_fmt_returns_formatted_files_without_touching_the_input(): files = {"main.tf": 'variable "region" {\ntype=string\n}\n'} result = await TerraformRunner().fmt(files) assert "main.tf" in result.message assert " type = string" in result.message assert files["main.tf"] == 'variable "region" {\ntype=string\n}\n' @needs_terraform async def test_fmt_reports_already_formatted_files(): result = await TerraformRunner().fmt(VALID_TF) assert result.message == "terraform fmt: all files are already formatted." @needs_terraform async def test_validate_passes_a_valid_configuration(): result = await TerraformRunner().validate(VALID_TF) assert result.message.startswith("terraform validate passed") @needs_terraform async def test_validate_reports_errors(): result = await TerraformRunner().validate( {"main.tf": 'output "x" {\n value = var.missing\n}\n'} ) assert result.message.startswith("terraform validate failed") assert "missing" in result.message async def test_unsafe_file_names_are_refused(): runner = TerraformRunner() for files in ({"../main.tf": ""}, {"/etc/main.tf": ""}, {"notes.txt": ""}, {}): result = await runner.validate(files) assert result.message.startswith("Refused"), files async def test_missing_terraform_binary_is_reported_clearly(): result = await TerraformRunner(binary="terraform-not-installed").validate(VALID_TF) assert "terraform-not-installed" in result.message assert "not installed" in result.message async def test_plan_never_runs_apply_and_refuses_unsafe_variables(monkeypatch): commands: list[list[str]] = [] async def fake_run(self, args, cwd, env=None): commands.append(list(args)) return 0, "ok" monkeypatch.setattr(TerraformRunner, "_run", fake_run) runner = TerraformRunner() result = await runner.plan(VALID_TF, {"region": "europe-west2"}) refused = await runner.plan(VALID_TF, {"bad name": "x"}) assert result.message.startswith("terraform plan") assert refused.message.startswith("Refused") flat = [arg for command in commands for arg in command] assert "apply" not in flat and "destroy" not in flat plan = next(command for command in commands if command[1] == "plan") assert "-lock=false" in plan and "-input=false" in plan assert "-var=region=europe-west2" in plan # --- registry ------------------------------------------------------------------------------ def test_local_tools_are_built_by_name_with_their_tier(): tools = local_tools(["mermaid_check", "terraform_plan"], TerraformRunner()) assert [(t.name, t.tier) for t in tools] == [ ("mermaid_check", "general"), ("terraform_plan", "cloud_read"), ] assert set(LOCAL_TOOLS) == { "mermaid_check", "terraform_fmt", "terraform_validate", "terraform_plan", } with pytest.raises(KeyError, match="terraform_apply"): local_tools(["terraform_apply"], TerraformRunner())