Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
test(SKY-11847): matrix workflow-parameter-validation tests (65→28, coverage-identical) (#7133)
Suchintan2bd0c0766565
diff --git a/tests/unit/test_cdp_download_interceptor.py b/tests/unit/test_cdp_download_interceptor.pyindex ee7a08e0a..eaa26feb1 100644--- a/tests/unit/test_cdp_download_interceptor.py+++ b/tests/unit/test_cdp_download_interceptor.py@@ -11,113 +11,204 @@ from skyvern.webeye.cdp_download_interceptor import CDPDownloadInterceptor, extrclass TestIsDownloadResponse:"""Tests for is_download_response()."""- def test_attachment_header(self) -> None:- headers = {"content-disposition": 'attachment; filename="report.csv"', "content-type": "text/csv"}- assert is_download_response(headers, 200) is True-- def test_attachment_header_case_insensitive(self) -> None:- headers = {"content-disposition": 'Attachment; filename="report.csv"', "content-type": "text/csv"}- assert is_download_response(headers, 200) is True-- def test_download_mime_type_pdf(self) -> None:- headers = {"content-type": "application/pdf"}- assert is_download_response(headers, 200) is True-- def test_download_mime_type_zip(self) -> None:- headers = {"content-type": "application/zip"}- assert is_download_response(headers, 200) is True-- def test_download_mime_type_xlsx(self) -> None:- headers = {- "content-type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",- }- assert is_download_response(headers, 200) is True-- def test_download_mime_type_octet_stream(self) -> None:- headers = {"content-type": "application/octet-stream"}- assert is_download_response(headers, 200) is True-- def test_download_mime_type_with_charset(self) -> None:- headers = {"content-type": "application/pdf; charset=utf-8"}- assert is_download_response(headers, 200) is True-- def test_html_not_download(self) -> None:- headers = {"content-type": "text/html"}- assert is_download_response(headers, 200) is False-- def test_json_not_download(self) -> None:- headers = {"content-type": "application/json"}- assert is_download_response(headers, 200) is False-- def test_json_with_attachment_not_download(self) -> None:- """JSON responses with Content-Disposition: attachment should NOT be treated as downloads."""- headers = {- "content-disposition": "attachment",- "content-type": "application/json",- }- assert is_download_response(headers, 200) is False-- def test_xml_not_download(self) -> None:- headers = {"content-type": "application/xml"}- assert is_download_response(headers, 200) is False-- def test_grpc_not_download(self) -> None:- headers = {"content-type": "application/grpc"}- assert is_download_response(headers, 200) is False-- def test_empty_headers_not_download(self) -> None:- assert is_download_response({}, 200) is False-- # Resource type filtering — XHR/Fetch require BOTH attachment AND download MIME- def test_xhr_attachment_and_download_mime_is_download(self) -> None:- """XHR with attachment + download MIME type should be treated as download."""- headers = {"content-disposition": "attachment", "content-type": "application/octet-stream"}- assert is_download_response(headers, 200, resource_type="XHR") is True-- def test_fetch_attachment_and_download_mime_is_download(self) -> None:- """Fetch with attachment + download MIME type should be treated as download."""- headers = {"content-disposition": "attachment", "content-type": "application/pdf"}- assert is_download_response(headers, 200, resource_type="Fetch") is True-- def test_xhr_attachment_pdf_is_download(self) -> None:- """Real-world case: XHR download with attachment header and PDF content-type."""- headers = {- "content-disposition": "attachment; filename=Invoice_12345.pdf; filename*=UTF-8''Invoice_12345.pdf",- "content-type": "application/pdf",- }- assert is_download_response(headers, 200, resource_type="XHR") is True-- # XHR/Fetch with attachment but non-download MIME should NOT be download- def test_xhr_attachment_text_plain_not_download(self) -> None:- """Google-style XHR: text/plain + attachment should NOT be treated as download."""- headers = {"content-disposition": 'attachment; filename="f.txt"', "content-type": "text/plain; charset=UTF-8"}- assert is_download_response(headers, 200, resource_type="XHR") is False-- def test_xhr_attachment_text_html_not_download(self) -> None:- """XHR with text/html + attachment should NOT be treated as download."""- headers = {"content-disposition": "attachment", "content-type": "text/html"}- assert is_download_response(headers, 200, resource_type="XHR") is False-- def test_fetch_attachment_only_not_download(self) -> None:- """Fetch with attachment but no download MIME type should NOT be download."""- headers = {"content-disposition": "attachment"}- assert is_download_response(headers, 200, resource_type="Fetch") is False-- def test_xhr_attachment_csv_not_download(self) -> None:- """Known limitation: CSV via XHR is not detected because text/csv is not in DOWNLOAD_MIME_TYPES."""- headers = {"content-disposition": "attachment", "content-type": "text/csv"}- assert is_download_response(headers, 200, resource_type="XHR") is False-- # XHR/Fetch without attachment header should NOT be download (MIME-only false positive)- def test_xhr_mime_only_not_download(self) -> None:- """XHR with download MIME type but no Content-Disposition at all should NOT be treated as download."""- headers = {"content-type": "application/pdf"}- assert is_download_response(headers, 200, resource_type="XHR") is False-- def test_fetch_mime_only_not_download(self) -> None:- """Fetch with download MIME type but no Content-Disposition at all should NOT be treated as download."""- headers = {"content-type": "application/octet-stream"}- assert is_download_response(headers, 200, resource_type="Fetch") is False+ @pytest.mark.parametrize(+ ("headers", "status_code", "resource_type", "expected"),+ [+ pytest.param(+ {"content-disposition": 'Attachment; filename="report.csv"', "content-type": "text/csv"},+ 200,+ "",+ True,+ id="attachment_header",+ ),+ pytest.param(+ {"content-disposition": 'attachment; filename="report.csv"', "content-type": "text/csv"},+ 200,+ "",+ True,+ id="attachment_header_lowercase",+ ),+ pytest.param(+ {"content-type": "application/pdf"},+ 200,+ "",+ True,+ id="download_mime_pdf",+ ),+ pytest.param(+ {"content-type": "application/zip"},+ 200,+ "",+ True,+ id="download_mime_zip",+ ),+ pytest.param(+ {"content-type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},+ 200,+ "",+ True,+ id="download_mime_xlsx",+ ),+ pytest.param(+ {"content-type": "application/octet-stream"},+ 200,+ "",+ True,+ id="download_mime_octet_stream",+ ),+ pytest.param(+ {"content-type": "application/pdf; charset=utf-8"},+ 200,+ "",+ True,+ id="download_mime_with_charset",+ ),+ pytest.param({"content-type": "text/html"}, 200, "", False, id="html_not_download"),+ pytest.param({"content-type": "application/json"}, 200, "", False, id="json_not_download"),+ pytest.param(+ {"content-disposition": "attachment", "content-type": "application/json"},+ 200,+ "",+ False,+ id="api_attachment_not_download",+ ),+ pytest.param({"content-type": "application/xml"}, 200, "", False, id="xml_not_download"),+ pytest.param({"content-type": "application/grpc"}, 200, "", False, id="grpc_not_download"),+ pytest.param({}, 200, "", False, id="empty_headers_not_download"),+ pytest.param(+ {"content-disposition": "attachment", "content-type": "application/octet-stream"},+ 200,+ "XHR",+ True,+ id="xhr_attachment_download_mime",+ ),+ pytest.param(+ {"content-disposition": "attachment", "content-type": "application/pdf"},+ 200,+ "Fetch",+ True,+ id="fetch_attachment_download_mime",+ ),+ pytest.param(+ {+ "content-disposition": (+ "attachment; filename=Invoice_12345.pdf; filename*=UTF-8''Invoice_12345.pdf"+ ),+ "content-type": "application/pdf",+ },+ 200,+ "XHR",+ True,+ id="xhr_attachment_pdf_filename_star",+ ),+ pytest.param(+ {"content-disposition": 'attachment; filename="f.txt"', "content-type": "text/plain; charset=UTF-8"},+ 200,+ "XHR",+ False,+ id="xhr_attachment_text_plain_not_download",+ ),+ pytest.param(+ {"content-disposition": "attachment", "content-type": "text/html"},+ 200,+ "XHR",+ False,+ id="xhr_attachment_text_html_not_download",+ ),+ pytest.param(+ {"content-disposition": "attachment"},+ 200,+ "Fetch",+ False,+ id="fetch_attachment_only_not_download",+ ),+ pytest.param(+ {"content-disposition": "attachment", "content-type": "text/csv"},+ 200,+ "XHR",+ False,+ id="xhr_attachment_csv_not_download",+ ),+ pytest.param(+ {"content-type": "application/pdf"},+ 200,+ "XHR",+ False,+ id="xhr_mime_only_not_download",+ ),+ pytest.param(+ {"content-type": "application/octet-stream"},+ 200,+ "Fetch",+ False,+ id="fetch_mime_only_not_download",+ ),+ pytest.param(+ {"content-disposition": "attachment", "content-type": "application/json"},+ 200,+ "XHR",+ False,+ id="xhr_json_attachment_not_download",+ ),+ pytest.param(+ {"content-type": "application/octet-stream"},+ 200,+ "Font",+ False,+ id="font_resource_type_not_download",+ ),+ pytest.param(+ {"content-type": "application/octet-stream"},+ 200,+ "Stylesheet",+ False,+ id="stylesheet_resource_type_not_download",+ ),+ pytest.param(+ {"content-type": "application/octet-stream"},+ 200,+ "Script",+ False,+ id="script_resource_type_not_download",+ ),+ pytest.param(+ {"content-type": "application/octet-stream"},+ 200,+ "Image",+ False,+ id="image_resource_type_not_download",+ ),+ pytest.param(+ {"content-disposition": "attachment", "content-type": "application/pdf"},+ 200,+ "Document",+ True,+ id="document_resource_type_is_download",+ ),+ pytest.param(+ {"content-disposition": "attachment", "content-type": "application/pdf"},+ 404,+ "",+ False,+ id="error_status_code_not_download",+ ),+ pytest.param(+ {"content-type": "application/octet-stream"},+ 500,+ "",+ False,+ id="server_error_not_download",+ ),+ ],+ )+ def test_is_download_response_table(+ self,+ headers: dict[str, str],+ status_code: int,+ resource_type: str,+ expected: bool,+ ) -> None:+ assert is_download_response(headers, status_code, resource_type=resource_type) is expecteddef test_xhr_inline_pdf_with_filename_not_download(self) -> None:"""XHR with inline + filename is NOT a CDP download — handled by ScopedXhrDownloadCapture instead."""@@ -127,45 +218,6 @@ class TestIsDownloadResponse:}assert is_download_response(headers, 200, resource_type="XHR") is False- # XHR/Fetch with attachment but API content-type should still be filtered- def test_xhr_json_attachment_not_download(self) -> None:- """XHR with JSON content-type and attachment header should NOT be download."""- headers = {"content-disposition": "attachment", "content-type": "application/json"}- assert is_download_response(headers, 200, resource_type="XHR") is False-- def test_font_resource_type_not_download(self) -> None:- headers = {"content-type": "application/octet-stream"}- assert is_download_response(headers, 200, resource_type="Font") is False-- def test_stylesheet_resource_type_not_download(self) -> None:- headers = {"content-type": "application/octet-stream"}- assert is_download_response(headers, 200, resource_type="Stylesheet") is False-- def test_script_resource_type_not_download(self) -> None:- headers = {"content-type": "application/octet-stream"}- assert is_download_response(headers, 200, resource_type="Script") is False-- def test_image_resource_type_not_download(self) -> None:- headers = {"content-type": "application/octet-stream"}- assert is_download_response(headers, 200, resource_type="Image") is False-- def test_document_resource_type_is_download(self) -> None:- """Document resource type (link click) should allow download detection."""- headers = {"content-disposition": "attachment", "content-type": "application/pdf"}- assert is_download_response(headers, 200, resource_type="Document") is True-- def test_empty_resource_type_is_download(self) -> None:- headers = {"content-type": "application/pdf"}- assert is_download_response(headers, 200, resource_type="") is True-- def test_error_status_code_not_download(self) -> None:- headers = {"content-disposition": "attachment", "content-type": "application/pdf"}- assert is_download_response(headers, 404) is False-- def test_server_error_not_download(self) -> None:- headers = {"content-type": "application/octet-stream"}- assert is_download_response(headers, 500) is False-class TestExtractFilename:"""Tests for extract_filename().@@ -174,59 +226,73 @@ class TestExtractFilename:the caller (_resolve_save_path) is responsible for generating a fallback name."""- def test_rfc5987_filename_star(self) -> None:- headers = {"content-disposition": "attachment; filename*=UTF-8''my%20report%282024%29.pdf"}- result = extract_filename(headers, "https://example.com/download")- assert result == "my report(2024).pdf"-- def test_regular_filename(self) -> None:- headers = {"content-disposition": 'attachment; filename="report.csv"'}- result = extract_filename(headers, "https://example.com/download")- assert result == "report.csv"-- def test_unquoted_filename(self) -> None:- headers = {"content-disposition": "attachment; filename=report.csv"}- result = extract_filename(headers, "https://example.com/download")- assert result == "report.csv"-- def test_filename_star_takes_priority(self) -> None:- headers = {- "content-disposition": "attachment; filename=\"fallback.csv\"; filename*=UTF-8''preferred.csv",- }- result = extract_filename(headers, "https://example.com/download")- assert result == "preferred.csv"-- def test_url_path_fallback(self) -> None:- headers: dict[str, str] = {}- result = extract_filename(headers, "https://example.com/files/document.pdf")- assert result == "document.pdf"-- def test_url_path_with_encoded_chars(self) -> None:- headers: dict[str, str] = {}- result = extract_filename(headers, "https://example.com/files/my%20report.xlsx")- assert result == "my report.xlsx"-- def test_url_path_no_extension_returns_empty(self) -> None:- """No extension in URL path and no Content-Disposition — returns empty string."""- headers: dict[str, str] = {}- result = extract_filename(headers, "https://example.com/download")- assert result == ""-- def test_no_headers_no_url_returns_empty(self) -> None:- """Completely empty inputs — returns empty string for _resolve_save_path to handle."""- result = extract_filename({}, "https://example.com/api/export")- assert result == ""-- def test_empty_content_disposition(self) -> None:- headers = {"content-disposition": ""}- result = extract_filename(headers, "https://example.com/files/data.csv")- assert result == "data.csv"-- def test_content_disposition_inline(self) -> None:- """inline disposition without filename should fall back to URL."""- headers = {"content-disposition": "inline"}- result = extract_filename(headers, "https://example.com/files/report.pdf")- assert result == "report.pdf"+ @pytest.mark.parametrize(+ ("headers", "url", "expected"),+ [+ pytest.param(+ {"content-disposition": "attachment; filename*=UTF-8''my%20report%282024%29.pdf"},+ "https://example.com/download",+ "my report(2024).pdf",+ id="rfc5987_filename_star",+ ),+ pytest.param(+ {"content-disposition": 'attachment; filename="report.csv"'},+ "https://example.com/download",+ "report.csv",+ id="regular_filename",+ ),+ pytest.param(+ {"content-disposition": "attachment; filename=report.csv"},+ "https://example.com/download",+ "report.csv",+ id="unquoted_filename",+ ),+ pytest.param(+ {"content-disposition": "attachment; filename=\"fallback.csv\"; filename*=UTF-8''preferred.csv"},+ "https://example.com/download",+ "preferred.csv",+ id="filename_star_takes_priority",+ ),+ pytest.param(+ {},+ "https://example.com/files/document.pdf",+ "document.pdf",+ id="url_path_fallback",+ ),+ pytest.param(+ {},+ "https://example.com/files/my%20report.xlsx",+ "my report.xlsx",+ id="url_path_with_encoded_chars",+ ),+ pytest.param(+ {},+ "https://example.com/download",+ "",+ id="url_path_no_extension_returns_empty",+ ),+ pytest.param(+ {},+ "https://example.com/api/export",+ "",+ id="no_headers_no_url_returns_empty",+ ),+ pytest.param(+ {"content-disposition": ""},+ "https://example.com/files/data.csv",+ "data.csv",+ id="empty_content_disposition_url_fallback",+ ),+ pytest.param(+ {"content-disposition": "inline"},+ "https://example.com/files/report.pdf",+ "report.pdf",+ id="content_disposition_inline_url_fallback",+ ),+ ],+ )+ def test_extract_filename_table(self, headers: dict[str, str], url: str, expected: str) -> None:+ assert extract_filename(headers, url) == expecteddef test_path_traversal_returned_raw(self) -> None:"""extract_filename returns raw name; sanitization is done in _resolve_save_path."""@@ -242,19 +308,42 @@ class TestResolveSavePath:interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))return interceptor- def test_normal_filename(self, tmp_path: Path) -> None:- interceptor = self._make_interceptor(tmp_path)- save_path, filename = interceptor._resolve_save_path("report.pdf")- assert filename == "report.pdf"- assert save_path == tmp_path / "report.pdf"-- def test_empty_filename_gets_uuid_fallback(self, tmp_path: Path) -> None:- """Empty filename should generate a download_{uuid} fallback."""- interceptor = self._make_interceptor(tmp_path)- save_path, filename = interceptor._resolve_save_path("")- assert filename.startswith("download_")- assert len(filename) > len("download_")- assert save_path == tmp_path / filename+ @pytest.mark.parametrize(+ ("raw_filename", "content_type", "expected_filename", "preexisting_file", "output_dir_parts"),+ [+ pytest.param("report.pdf", "", "report.pdf", False, (), id="normal_filename"),+ pytest.param("", "", None, False, (), id="empty_filename_uuid_fallback"),+ pytest.param(None, "", None, False, (), id="default_param_empty_string"),+ pytest.param("report.pdf", "", "report.pdf", True, (), id="collision_returns_same_path"),+ pytest.param("file.txt", "", "file.txt", False, ("sub", "dir"), id="creates_missing_output_dir"),+ ],+ )+ def test_resolve_save_path_table(+ self,+ tmp_path: Path,+ raw_filename: str | None,+ content_type: str,+ expected_filename: str | None,+ preexisting_file: bool,+ output_dir_parts: tuple[str, ...],+ ) -> None:+ output_dir = tmp_path.joinpath(*output_dir_parts)+ interceptor = self._make_interceptor(output_dir)+ if preexisting_file:+ (output_dir / raw_filename).write_bytes(b"existing")++ if raw_filename is None:+ save_path, filename = interceptor._resolve_save_path()+ else:+ save_path, filename = interceptor._resolve_save_path(raw_filename, content_type)++ if expected_filename is None:+ assert filename.startswith("download_")+ assert len(filename) > len("download_")+ else:+ assert filename == expected_filename+ assert save_path == output_dir / filename+ assert output_dir.exists()def test_empty_filename_gets_pdf_uuid_fallback(self, tmp_path: Path) -> None:interceptor = self._make_interceptor(tmp_path)@@ -263,12 +352,6 @@ class TestResolveSavePath:assert filename.endswith(".pdf")assert save_path == tmp_path / filename- def test_default_param_empty_string(self, tmp_path: Path) -> None:- """Calling without arguments should also trigger fallback."""- interceptor = self._make_interceptor(tmp_path)- _, filename = interceptor._resolve_save_path()- assert filename.startswith("download_")-def test_path_traversal_sanitized(self, tmp_path: Path) -> None:"""Path traversal components should be stripped — only the final name is kept."""interceptor = self._make_interceptor(tmp_path)@@ -295,30 +378,6 @@ class TestResolveSavePath:assert filename == "invoice_5_19_2026.pdf"assert save_path == tmp_path / "invoice_5_19_2026.pdf"- def test_increments_download_index(self, tmp_path: Path) -> None:- interceptor = self._make_interceptor(tmp_path)- assert interceptor._download_index == 0- interceptor._resolve_save_path("a.pdf")- assert interceptor._download_index == 1- interceptor._resolve_save_path("b.pdf")- assert interceptor._download_index == 2-- def test_collision_warning_logged(self, tmp_path: Path) -> None:- """Existing file with the same name should warn but still return the path."""- interceptor = self._make_interceptor(tmp_path)- # Create a file that will collide- (tmp_path / "report.pdf").write_bytes(b"existing")- save_path, filename = interceptor._resolve_save_path("report.pdf")- assert filename == "report.pdf"- assert save_path == tmp_path / "report.pdf"-- def test_creates_output_dir_if_missing(self, tmp_path: Path) -> None:- nested = tmp_path / "sub" / "dir"- interceptor = CDPDownloadInterceptor(output_dir=str(nested))- save_path, _ = interceptor._resolve_save_path("file.txt")- assert nested.exists()- assert save_path.parent == nested-class TestCDPDownloadInterceptorProxyAuth:"""Tests for CDP proxy authentication handling (Fetch.authRequired + continueWithAuth)."""diff --git a/tests/unit/test_workflow_parameter_validation.py b/tests/unit/test_workflow_parameter_validation.pyindex ce0df96d2..862a9b188 100644--- a/tests/unit/test_workflow_parameter_validation.py+++ b/tests/unit/test_workflow_parameter_validation.py@@ -4,6 +4,8 @@ These tests ensure that parameter keys and block labels are valid Python/Jinja2preventing runtime errors like "'State_' is undefined" when using keys like "State_/_Province"."""+from collections.abc import Callable+import pytestfrom pydantic import ValidationError@@ -18,303 +20,119 @@ from skyvern.schemas.workflows import (from skyvern.utils.templating import replace_jinja_reference-class TestParameterKeyValidation:- """Tests for parameter key validation."""-- def test_valid_parameter_key_simple(self) -> None:- """Test that simple valid keys are accepted."""- param = WorkflowParameterYAML(- key="my_parameter",- workflow_parameter_type=WorkflowParameterType.STRING,- )- assert param.key == "my_parameter"-- def test_valid_parameter_key_with_numbers(self) -> None:- """Test that keys with numbers (not at start) are accepted."""- param = WorkflowParameterYAML(- key="param123",- workflow_parameter_type=WorkflowParameterType.STRING,- )- assert param.key == "param123"-- def test_valid_parameter_key_underscore_prefix(self) -> None:- """Test that keys starting with underscore are accepted."""- param = WorkflowParameterYAML(- key="_private_param",- workflow_parameter_type=WorkflowParameterType.STRING,- )- assert param.key == "_private_param"-- def test_valid_parameter_key_single_letter(self) -> None:- """Test that single letter keys are accepted."""- param = WorkflowParameterYAML(- key="x",- workflow_parameter_type=WorkflowParameterType.STRING,- )- assert param.key == "x"-- def test_invalid_parameter_key_with_slash(self) -> None:- """Test that keys with '/' are rejected (the main bug case from SKY-7356)."""- with pytest.raises(ValidationError) as exc_info:- WorkflowParameterYAML(- key="State_/_Province",+class TestIdentifierValidation:+ """Tests for parameter key and block label validation."""++ @pytest.mark.parametrize(+ ("model_name", "value"),+ [+ pytest.param("parameter", "my_parameter", id="parameter-key"),+ pytest.param("parameter", "param123", id="parameter-key-with-numbers"),+ pytest.param("parameter", "_private_param", id="parameter-key-underscore-prefix"),+ pytest.param("parameter", "x", id="parameter-key-single-letter"),+ pytest.param("block", "my_task", id="block-label"),+ pytest.param("block", "task123", id="block-label-with-numbers"),+ pytest.param("block", "_private_task", id="block-label-underscore-prefix"),+ ],+ )+ def test_valid_keys(self, model_name: str, value: str) -> None:+ if model_name == "parameter":+ param = WorkflowParameterYAML(+ key=value,workflow_parameter_type=WorkflowParameterType.STRING,)- error_msg = str(exc_info.value)- assert "not a valid parameter name" in error_msg-- def test_invalid_parameter_key_with_hyphen(self) -> None:- """Test that keys with '-' are rejected."""+ assert param.key == value+ else:+ block = TaskBlockYAML(label=value, url="https://example.com")+ assert block.label == value++ @pytest.mark.parametrize(+ ("model_name", "value", "error_fragment"),+ [+ pytest.param("parameter", "State_/_Province", "not a valid parameter name", id="parameter-invalid-char"),+ pytest.param("parameter", "state-or-province", "not a valid parameter name", id="parameter-hyphen"),+ pytest.param("parameter", "some.property", "not a valid parameter name", id="parameter-dot"),+ pytest.param("parameter", "123param", "not a valid parameter name", id="parameter-digit-prefix"),+ pytest.param("parameter", "my parameter", "whitespace", id="parameter-whitespace"),+ pytest.param("parameter", "my\tparameter", "whitespace", id="parameter-tab"),+ pytest.param("parameter", "param*value", "not a valid parameter name", id="parameter-asterisk"),+ pytest.param("block", "task/block", "not a valid label", id="block-invalid-char"),+ pytest.param("block", "task-block", "not a valid label", id="block-hyphen"),+ pytest.param("block", "123task", "not a valid label", id="block-digit-prefix"),+ pytest.param("block", "", "empty", id="block-empty"),+ pytest.param("block", " ", "empty", id="block-whitespace-only"),+ pytest.param("block", "my task", "not a valid label", id="block-space"),+ ],+ )+ def test_invalid_keys(self, model_name: str, value: str, error_fragment: str) -> None:with pytest.raises(ValidationError) as exc_info:- WorkflowParameterYAML(- key="state-or-province",- workflow_parameter_type=WorkflowParameterType.STRING,- )- error_msg = str(exc_info.value)- assert "not a valid parameter name" in error_msg-- def test_invalid_parameter_key_with_dot(self) -> None:- """Test that keys with '.' are rejected."""- with pytest.raises(ValidationError) as exc_info:- WorkflowParameterYAML(- key="some.property",- workflow_parameter_type=WorkflowParameterType.STRING,- )- error_msg = str(exc_info.value)- assert "not a valid parameter name" in error_msg-- def test_invalid_parameter_key_starts_with_digit(self) -> None:- """Test that keys starting with a digit are rejected."""- with pytest.raises(ValidationError) as exc_info:- WorkflowParameterYAML(- key="123param",- workflow_parameter_type=WorkflowParameterType.STRING,- )- error_msg = str(exc_info.value)- assert "not a valid parameter name" in error_msg-- def test_invalid_parameter_key_with_space(self) -> None:- """Test that keys with spaces are rejected."""- with pytest.raises(ValidationError) as exc_info:- WorkflowParameterYAML(- key="my parameter",- workflow_parameter_type=WorkflowParameterType.STRING,- )- error_msg = str(exc_info.value)- assert "whitespace" in error_msg-- def test_invalid_parameter_key_with_tab(self) -> None:- """Test that keys with tabs are rejected."""- with pytest.raises(ValidationError) as exc_info:- WorkflowParameterYAML(- key="my\tparameter",- workflow_parameter_type=WorkflowParameterType.STRING,- )- error_msg = str(exc_info.value)- assert "whitespace" in error_msg-- def test_invalid_parameter_key_with_asterisk(self) -> None:- """Test that keys with '*' are rejected."""- with pytest.raises(ValidationError) as exc_info:- WorkflowParameterYAML(- key="param*value",- workflow_parameter_type=WorkflowParameterType.STRING,- )- error_msg = str(exc_info.value)- assert "not a valid parameter name" in error_msg---class TestBlockLabelValidation:- """Tests for block label validation."""-- def test_valid_block_label_simple(self) -> None:- """Test that simple valid labels are accepted."""- block = TaskBlockYAML(label="my_task", url="https://example.com")- assert block.label == "my_task"-- def test_valid_block_label_with_numbers(self) -> None:- """Test that labels with numbers (not at start) are accepted."""- block = TaskBlockYAML(label="task123", url="https://example.com")- assert block.label == "task123"-- def test_valid_block_label_underscore_prefix(self) -> None:- """Test that labels starting with underscore are accepted."""- block = TaskBlockYAML(label="_private_task", url="https://example.com")- assert block.label == "_private_task"-- def test_invalid_block_label_with_slash(self) -> None:- """Test that labels with '/' are rejected."""- with pytest.raises(ValidationError) as exc_info:- TaskBlockYAML(label="task/block", url="https://example.com")- error_msg = str(exc_info.value)- assert "not a valid label" in error_msg-- def test_invalid_block_label_with_hyphen(self) -> None:- """Test that labels with '-' are rejected."""- with pytest.raises(ValidationError) as exc_info:- TaskBlockYAML(label="task-block", url="https://example.com")- error_msg = str(exc_info.value)- assert "not a valid label" in error_msg-- def test_invalid_block_label_starts_with_digit(self) -> None:- """Test that labels starting with a digit are rejected."""- with pytest.raises(ValidationError) as exc_info:- TaskBlockYAML(label="123task", url="https://example.com")- error_msg = str(exc_info.value)- assert "not a valid label" in error_msg-- def test_invalid_block_label_empty(self) -> None:- """Test that empty labels are rejected."""- with pytest.raises(ValidationError) as exc_info:- TaskBlockYAML(label="", url="https://example.com")- error_msg = str(exc_info.value)- assert "empty" in error_msg.lower()-- def test_invalid_block_label_whitespace_only(self) -> None:- """Test that whitespace-only labels are rejected."""- with pytest.raises(ValidationError) as exc_info:- TaskBlockYAML(label=" ", url="https://example.com")- error_msg = str(exc_info.value)- assert "empty" in error_msg.lower()-- def test_invalid_block_label_with_space(self) -> None:- """Test that labels with spaces are rejected."""- with pytest.raises(ValidationError) as exc_info:- TaskBlockYAML(label="my task", url="https://example.com")- error_msg = str(exc_info.value)- assert "not a valid label" in error_msg---class TestSanitizeBlockLabel:- """Tests for the sanitize_block_label function."""-- def test_sanitize_slash(self) -> None:- """Test that slashes are replaced with underscores."""- assert sanitize_block_label("State/Province") == "State_Province"-- def test_sanitize_hyphen(self) -> None:- """Test that hyphens are replaced with underscores."""- assert sanitize_block_label("my-block") == "my_block"-- def test_sanitize_dot(self) -> None:- """Test that dots are replaced with underscores."""- assert sanitize_block_label("block.name") == "block_name"-- def test_sanitize_multiple_special_chars(self) -> None:- """Test that multiple special characters are handled."""- assert sanitize_block_label("State_/_Province") == "State_Province"-- def test_sanitize_consecutive_underscores(self) -> None:- """Test that consecutive underscores are collapsed."""- assert sanitize_block_label("a__b___c") == "a_b_c"-- def test_sanitize_leading_trailing_underscores(self) -> None:- """Test that leading/trailing underscores are removed."""- assert sanitize_block_label("_my_block_") == "my_block"-- def test_sanitize_digit_prefix(self) -> None:- """Test that labels starting with digits get underscore prefix."""- assert sanitize_block_label("123abc") == "_123abc"-- def test_sanitize_digit_prefix_after_strip(self) -> None:- """Test that digit prefix is added after stripping underscores."""- assert sanitize_block_label("_123abc") == "_123abc"-- def test_sanitize_all_invalid_chars(self) -> None:- """Test that if all chars are invalid, default is returned."""- assert sanitize_block_label("///") == "block"-- def test_sanitize_empty_string(self) -> None:- """Test that empty string returns default."""- assert sanitize_block_label("") == "block"-- def test_sanitize_valid_label_unchanged(self) -> None:- """Test that valid labels are unchanged."""- assert sanitize_block_label("my_valid_label") == "my_valid_label"-- def test_sanitize_spaces(self) -> None:- """Test that spaces are replaced with underscores."""- assert sanitize_block_label("my block name") == "my_block_name"---class TestSanitizeParameterKey:- """Tests for the sanitize_parameter_key function."""-- def test_sanitize_slash(self) -> None:- """Test that slashes are replaced with underscores."""- assert sanitize_parameter_key("State/Province") == "State_Province"-- def test_sanitize_hyphen(self) -> None:- """Test that hyphens are replaced with underscores."""- assert sanitize_parameter_key("my-param") == "my_param"-- def test_sanitize_dot(self) -> None:- """Test that dots are replaced with underscores."""- assert sanitize_parameter_key("param.name") == "param_name"-- def test_sanitize_all_invalid_chars(self) -> None:- """Test that if all chars are invalid, default is returned."""- assert sanitize_parameter_key("///") == "parameter"-- def test_sanitize_empty_string(self) -> None:- """Test that empty string returns default."""- assert sanitize_parameter_key("") == "parameter"-- def test_sanitize_valid_key_unchanged(self) -> None:- """Test that valid keys are unchanged."""- assert sanitize_parameter_key("my_valid_key") == "my_valid_key"+ if model_name == "parameter":+ WorkflowParameterYAML(+ key=value,+ workflow_parameter_type=WorkflowParameterType.STRING,+ )+ else:+ TaskBlockYAML(label=value, url="https://example.com")++ assert error_fragment in str(exc_info.value).lower()+++class TestSanitizers:+ """Tests for identifier sanitizers."""++ @pytest.mark.parametrize(+ ("sanitizer", "raw_value", "expected"),+ [+ pytest.param(sanitize_block_label, "State/Province", "State_Province", id="block-label-slash"),+ pytest.param(sanitize_block_label, "my-block", "my_block", id="block-label-hyphen"),+ pytest.param(sanitize_block_label, "block.name", "block_name", id="block-label-dot"),+ pytest.param(sanitize_block_label, "State_/_Province", "State_Province", id="block-label-mixed-specials"),+ pytest.param(sanitize_block_label, "a__b___c", "a_b_c", id="block-label-collapse-underscores"),+ pytest.param(sanitize_block_label, "_my_block_", "my_block", id="block-label-trim-underscores"),+ pytest.param(sanitize_block_label, "123abc", "_123abc", id="block-label-digit-prefix"),+ pytest.param(sanitize_block_label, "_123abc", "_123abc", id="block-label-digit-prefix-after-strip"),+ pytest.param(sanitize_block_label, "///", "block", id="block-label-default"),+ pytest.param(sanitize_block_label, "", "block", id="block-label-empty-default"),+ pytest.param(sanitize_block_label, "my_valid_label", "my_valid_label", id="block-label-valid-unchanged"),+ pytest.param(sanitize_block_label, "my block name", "my_block_name", id="block-label-spaces"),+ pytest.param(sanitize_parameter_key, "State/Province", "State_Province", id="parameter-key-slash"),+ pytest.param(sanitize_parameter_key, "my-param", "my_param", id="parameter-key-hyphen"),+ pytest.param(sanitize_parameter_key, "param.name", "param_name", id="parameter-key-dot"),+ pytest.param(sanitize_parameter_key, "///", "parameter", id="parameter-key-default"),+ pytest.param(sanitize_parameter_key, "", "parameter", id="parameter-key-empty-default"),+ pytest.param(sanitize_parameter_key, "my_valid_key", "my_valid_key", id="parameter-key-valid-unchanged"),+ ],+ )+ def test_sanitizers(self, sanitizer: Callable[[str], str], raw_value: str, expected: str) -> None:+ assert sanitizer(raw_value) == expectedclass TestReplaceJinjaReference:"""Tests for the replace_jinja_reference function."""- def test_replace_simple_reference(self) -> None:- """Test replacing a simple Jinja reference."""- text = "Value is {{ old_key }}"- result = replace_jinja_reference(text, "old_key", "new_key")- assert result == "Value is {{ new_key }}"-- def test_replace_reference_no_spaces(self) -> None:- """Test replacing a reference without spaces."""- text = "Value is {{old_key}}"- result = replace_jinja_reference(text, "old_key", "new_key")- assert result == "Value is {{new_key}}"-- def test_replace_reference_with_attribute(self) -> None:- """Test replacing a reference with attribute access."""- text = "Value is {{ old_key.field }}"- result = replace_jinja_reference(text, "old_key", "new_key")- assert result == "Value is {{ new_key.field }}"-- def test_replace_reference_with_filter(self) -> None:- """Test replacing a reference with filter."""- text = "Value is {{ old_key | default('') }}"- result = replace_jinja_reference(text, "old_key", "new_key")- assert result == "Value is {{ new_key | default('') }}"-- def test_replace_reference_with_index(self) -> None:- """Test replacing a reference with index access."""- text = "Value is {{ old_key[0] }}"- result = replace_jinja_reference(text, "old_key", "new_key")- assert result == "Value is {{ new_key[0] }}"-- def test_replace_multiple_references(self) -> None:- """Test replacing multiple occurrences."""- text = "{{ old_key }} and {{ old_key.field }}"- result = replace_jinja_reference(text, "old_key", "new_key")- assert result == "{{ new_key }} and {{ new_key.field }}"-- def test_no_replace_partial_match(self) -> None:- """Test that partial matches are not replaced."""- text = "{{ old_key_extended }}"- result = replace_jinja_reference(text, "old_key", "new_key")- assert result == "{{ old_key_extended }}"-- def test_no_replace_different_key(self) -> None:- """Test that different keys are not affected."""- text = "{{ other_key }}"- result = replace_jinja_reference(text, "old_key", "new_key")- assert result == "{{ other_key }}"+ @pytest.mark.parametrize(+ ("text", "expected"),+ [+ pytest.param("Value is {{ old_key }}", "Value is {{ new_key }}", id="simple-reference"),+ pytest.param("Value is {{old_key}}", "Value is {{new_key}}", id="no-space-reference"),+ pytest.param("Value is {{ old_key.field }}", "Value is {{ new_key.field }}", id="attribute-access"),+ pytest.param(+ "Value is {{ old_key | default('') }}",+ "Value is {{ new_key | default('') }}",+ id="filter-expression",+ ),+ pytest.param("Value is {{ old_key[0] }}", "Value is {{ new_key[0] }}", id="index-access"),+ pytest.param(+ "{{ old_key }} and {{ old_key.field }}",+ "{{ new_key }} and {{ new_key.field }}",+ id="multiple-occurrences",+ ),+ pytest.param("{{ old_key_extended }}", "{{ old_key_extended }}", id="partial-match-unchanged"),+ pytest.param("{{ other_key }}", "{{ other_key }}", id="different-key-unchanged"),+ ],+ )+ def test_replace_jinja_reference(self, text: str, expected: str) -> None:+ assert replace_jinja_reference(text, "old_key", "new_key") == expectedclass TestSanitizeWorkflowYamlWithReferences: