Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /promptdriven_pdd /tests /test_template_registry.py
| # Test plan and tests for pdd.template_registry | |
| # | |
| # Test plan (detailed) | |
| # | |
| # Overview: | |
| # The module pdd.template_registry provides discovery and handling of "templates" defined by .prompt files | |
| # with YAML front matter. Public API functions: | |
| # - list_templates(filter_tag: Optional[str]) -> List[dict] | |
| # - load_template(name: str) -> dict | |
| # - show_template(name: str) -> dict | |
| # - copy_template(name: str, dest_dir: str) -> str | |
| # | |
| # Key behaviours to test: | |
| # 1) Discovery of project templates under ./prompts/**.prompt (relative to CWD). | |
| # - Edge case: prompts directory does not exist -> list_templates should return []. | |
| # - Test: create a temporary CWD without prompts directory; list_templates() returns []. | |
| # - Test: create a prompts tree in tmp CWD with a valid .prompt with YAML front matter -> list contains it. | |
| # | |
| # 2) Packaged templates under pdd/templates/**.prompt (importlib.resources.files). | |
| # - The package pdd (the module under test) lives on disk; create pdd/templates files next to that package in tests. | |
| # - Edge case: packaged template present but project overrides with same name -> project entry should win. | |
| # - Test: create one packaged template and a project template with same name, but different description -> list should include only the project version. | |
| # | |
| # 3) Front matter parsing and validation: | |
| # - Valid front matter yields template metadata. | |
| # - Missing front matter (no leading '---') should be ignored (not indexed). | |
| # - Malformed front matter (e.g., YAML that loads to non-dict) should be ignored. | |
| # - variables should be preserved as-is in load/show. | |
| # - tags normalized to lowercase list of strings. | |
| # - filter_tag behavior should be case-insensitive. | |
| # | |
| # 4) load_template: | |
| # - load_template with existing template name returns full metadata including absolute path. | |
| # - load_template with non-existent name raises FileNotFoundError with helpful message. | |
| # | |
| # 5) show_template: | |
| # - show_template returns dict with 'summary' and other allowed sections but excludes prompt body (we can't access body via API; ensure fields equal metadata). | |
| # | |
| # 6) copy_template: | |
| # - copy_template copies underlying .prompt file to destination directory and returns destination path. | |
| # - If dest dir doesn't exist, it is created. | |
| # | |
| # 7) Robustness: | |
| # - Ensure functions tolerate optional/missing keys such as usage, discover, output_schema, notes. | |
| # | |
| # Which edge cases to formally prove with Z3 vs unit tests: | |
| # - Z3 is suitable for small logical invariants or algebraic properties. For this module the main invariants are: | |
| # * list_templates produces unique template names (no duplicates). | |
| # * tag filtering is case-insensitive. | |
| # However, these are properties that depend on file system state and parsing; Z3 cannot easily model file IO or YAML parsing. | |
| # Therefore Z3 formal verification is of limited usefulness here. We'll include a small Z3 model that encodes the uniqueness property | |
| # in an abstract way (pure logical model) as an illustrative formal check; but the real behaviour is verified with unit tests. | |
| # | |
| # Test strategy summary: | |
| # - Unit tests using pytest, temporary directories (tmp_path), and monkeypatch to set CWD so project discovery sees prompts dir. | |
| # - For packaged templates, create files under the actual pdd package folder (adjacent to the module), then clean up after each test. | |
| # - Avoid touching global internal state; use only public API functions. | |
| # - For Z3: include an optional test that runs only if z3 is available; otherwise it is skipped. | |
| # | |
| # Tests to implement (each as separate pytest function): | |
| # 1. test_list_templates_no_prompts_dir_returns_empty | |
| # 2. test_project_template_discovered_and_metadata_parsed | |
| # 3. test_missing_front_matter_ignored_and_malformed_ignored | |
| # 4. test_filter_tag_case_insensitive | |
| # 5. test_project_overrides_packaged_template | |
| # 6. test_load_template_missing_raises | |
| # 7. test_show_template_contains_expected_sections | |
| # 8. test_copy_template_creates_destination_and_returns_path | |
| # 9. test_tags_normalized_lowercase | |
| # 10. test_z3_uniqueness_invariant (optional: run only if z3 is installed) | |
| # | |
| # Implementation notes for tests: | |
| # - Use tmp_path as test workspace root and monkeypatch.chdir(tmp_path) so that project templates are discovered under tmp_path/prompts. | |
| # - To simulate packaged templates, create files under the pdd package directory: | |
| # locate the package directory via import pdd and Path(pdd.__file__).parent / "templates". | |
| # Create the templates directory if it doesn't exist, write .prompt files, and remove them at the end of the test. | |
| # - When writing .prompt files, include YAML front matter at file start: | |
| # --- | |
| # name: mytemplate | |
| # description: desc | |
| # version: "1.0" | |
| # tags: [Tag1, tag2] | |
| # language: python | |
| # output: text | |
| # variables: | |
| # var1: | |
| # example: foo | |
| # --- | |
| # prompt body... | |
| # | |
| # - Tests avoid importing or using private functions or variables. | |
| # - Tests assert exact keys in returned dicts and that content matches expected. | |
| # | |
| # Z3 note: | |
| # - The Z3 test encodes a simple abstract model: given a set of template names (strings), the model asserts that after indexing, names are unique. | |
| # - This is not a proof of code correctness vs FS, but demonstrates a small formal check. The test is skipped if z3 is absent. | |
| # Begin tests | |
| import os | |
| from pathlib import Path | |
| import shutil | |
| import sys | |
| import pytest | |
| # Import the module under test (public API) | |
| import pdd.template_registry as tr | |
| def _write_prompt_file(path: Path, front_matter: str, body: str = "body\n"): | |
| """ | |
| Helper to write a .prompt file with given YAML front matter (without leading/trailing '---'). | |
| The function writes the '---\n' + front_matter + '\n---\n' + body to file. | |
| """ | |
| content = f"---\n{front_matter}\n---\n{body}" | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| path.write_text(content, encoding="utf-8") | |
| return path | |
| def test_list_templates_no_prompts_dir_returns_empty(tmp_path, monkeypatch): | |
| # Ensure we start in an isolated cwd without prompts dir | |
| monkeypatch.chdir(tmp_path) | |
| # Remove prompts if exists | |
| prompts_dir = tmp_path / "prompts" | |
| if prompts_dir.exists(): | |
| shutil.rmtree(prompts_dir) | |
| items = tr.list_templates() | |
| assert isinstance(items, list) | |
| assert items, "Packaged templates should still be listed" | |
| names = {item["name"] for item in items} | |
| assert "architecture/architecture_json" in names | |
| def test_project_template_discovered_and_metadata_parsed(tmp_path, monkeypatch): | |
| # Create a project prompt with full front matter | |
| monkeypatch.chdir(tmp_path) | |
| fm = "\n".join([ | |
| "name: myproj", | |
| "description: Project template", | |
| "version: '0.1'", | |
| "tags: [Foo, Bar]", | |
| "language: python", | |
| "output: json", | |
| "variables:", | |
| " x:", | |
| " example: 1", | |
| ]) | |
| fp = tmp_path / "prompts" / "sub" / "myproj.prompt" | |
| _write_prompt_file(fp, fm, body="print('hello')\n") | |
| items = tr.list_templates() | |
| names = {item["name"] for item in items} | |
| assert "myproj" in names | |
| assert "architecture/architecture_json" in names | |
| proj = next(item for item in items if item["name"] == "myproj") | |
| assert "Project template" in proj["description"] | |
| assert proj["version"] == "0.1" | |
| # tags normalized to lowercase | |
| assert set(proj["tags"]) == {"foo", "bar"} | |
| # load_template returns full metadata including variables | |
| meta = tr.load_template("myproj") | |
| assert meta["name"] == "myproj" | |
| assert meta["language"] == "python" | |
| assert meta["output"] == "json" | |
| assert isinstance(meta["variables"], dict) | |
| assert "x" in meta["variables"] | |
| # show_template summary contains path and does not include body | |
| shown = tr.show_template("myproj") | |
| assert "summary" in shown | |
| assert shown["summary"]["name"] == "myproj" | |
| assert shown["variables"] == meta["variables"] | |
| def test_missing_front_matter_ignored_and_malformed_ignored(tmp_path, monkeypatch): | |
| monkeypatch.chdir(tmp_path) | |
| # File without front matter | |
| p1 = tmp_path / "prompts" / "nofm.prompt" | |
| p1.parent.mkdir(parents=True, exist_ok=True) | |
| p1.write_text("This file has no front matter\n", encoding="utf-8") | |
| # File with front matter that loads to a list (malformed for our purposes) | |
| p2 = tmp_path / "prompts" / "badfm.prompt" | |
| bad_fm_body = "- just\n- a\n- list" | |
| p2.write_text(f"---\n{bad_fm_body}\n---\nbody\n", encoding="utf-8") | |
| # Packaged templates should still be listed, but project files without | |
| # valid front matter must be ignored | |
| items = tr.list_templates() | |
| names = {item["name"] for item in items} | |
| assert "architecture/architecture_json" in names | |
| # Ensure the malformed/missing front-matter files weren't indexed | |
| assert "nofm" not in names | |
| assert "badfm" not in names | |
| def test_filter_tag_case_insensitive(tmp_path, monkeypatch): | |
| monkeypatch.chdir(tmp_path) | |
| fm = "\n".join([ | |
| "name: tagtest", | |
| "tags: [Alpha, beta]", | |
| ]) | |
| fp = tmp_path / "prompts" / "tagtest.prompt" | |
| _write_prompt_file(fp, fm) | |
| # filter with different cases | |
| res1 = tr.list_templates(filter_tag="alpha") | |
| assert len(res1) == 1 | |
| res2 = tr.list_templates(filter_tag="ALPHA") | |
| assert len(res2) == 1 | |
| res3 = tr.list_templates(filter_tag="BeTa") | |
| assert len(res3) == 1 | |
| res4 = tr.list_templates(filter_tag="missing") | |
| assert res4 == [] | |
| def test_project_overrides_packaged_template(tmp_path, monkeypatch): | |
| """ | |
| Create a packaged template alongside the pdd package, then create a project template with same name. | |
| Ensure project overrides packaged. | |
| """ | |
| # Setup CWD such that project discovery works | |
| monkeypatch.chdir(tmp_path) | |
| # Prepare packaged template under the pdd package's templates directory | |
| import pdd # package under test | |
| pkg_dir = Path(pdd.__file__).parent | |
| templates_dir = pkg_dir / "templates" | |
| templates_dir.mkdir(parents=True, exist_ok=True) | |
| packaged_fp = templates_dir / "over.prompt" | |
| packaged_fm = "\n".join([ | |
| "name: over", | |
| "description: Packaged version", | |
| "tags: [pkg]", | |
| ]) | |
| packaged_fp.write_text(f"---\n{packaged_fm}\n---\npackaged body\n", encoding="utf-8") | |
| try: | |
| # Create project template with same name but different description | |
| proj_fp = tmp_path / "prompts" / "over.prompt" | |
| proj_fm = "\n".join([ | |
| "name: over", | |
| "description: Project version (override)", | |
| "tags: [proj]", | |
| ]) | |
| _write_prompt_file(proj_fp, proj_fm, body="project body\n") | |
| items = tr.list_templates() | |
| # There should be exactly one template named 'over' and it should have project's description and tag | |
| names = [i["name"] for i in items] | |
| assert "over" in names | |
| meta = tr.load_template("over") | |
| assert "Project version (override)" in meta["description"] | |
| assert "proj" in meta["tags"] | |
| assert "pkg" not in meta["tags"] | |
| finally: | |
| # Cleanup packaged file we created | |
| try: | |
| packaged_fp.unlink() | |
| except Exception: | |
| pass | |
| def test_load_template_missing_raises(tmp_path, monkeypatch): | |
| monkeypatch.chdir(tmp_path) | |
| with pytest.raises(FileNotFoundError) as exc: | |
| tr.load_template("doesnotexist") | |
| assert "not found" in str(exc.value).lower() | |
| def test_show_template_contains_expected_sections(tmp_path, monkeypatch): | |
| monkeypatch.chdir(tmp_path) | |
| fm = "\n".join([ | |
| "name: showme", | |
| "description: Show me", | |
| "version: '2.0'", | |
| "tags: [x]", | |
| "language: markdown", | |
| "output: text", | |
| "variables: {a: {example: 10}}", | |
| "usage: {example: yes}", | |
| "discover: {key: val}", | |
| "output_schema: {type: object}", | |
| "notes: Some notes", | |
| ]) | |
| fp = tmp_path / "prompts" / "showme.prompt" | |
| _write_prompt_file(fp, fm, body="body\n") | |
| res = tr.show_template("showme") | |
| assert "summary" in res | |
| s = res["summary"] | |
| assert s["name"] == "showme" | |
| assert s["language"] == "markdown" | |
| assert res["variables"] == {"a": {"example": 10}} | |
| assert "usage" in res and res["usage"] == {"example": "yes"} or res["usage"] == {"example": True} or isinstance(res["usage"], dict) | |
| # output_schema is present but may be elided in other implementations; here it's included | |
| assert "output_schema" in res | |
| assert res["notes"] == "Some notes" | |
| def test_copy_template_creates_destination_and_returns_path(tmp_path, monkeypatch): | |
| monkeypatch.chdir(tmp_path) | |
| fm = "\n".join([ | |
| "name: copyme", | |
| "description: to copy", | |
| "tags: [c]", | |
| ]) | |
| src_fp = tmp_path / "prompts" / "copyme.prompt" | |
| _write_prompt_file(src_fp, fm, body="COPYBODY\n") | |
| dest_dir = tmp_path / "output" / "destfolder" | |
| dest = tr.copy_template("copyme", str(dest_dir)) | |
| dest_path = Path(dest) | |
| assert dest_path.exists() | |
| content = dest_path.read_text(encoding="utf-8") | |
| assert "COPYBODY" in content | |
| # Ensure returned path is absolute | |
| assert dest_path.is_absolute() | |
| def test_tags_normalized_lowercase(tmp_path, monkeypatch): | |
| monkeypatch.chdir(tmp_path) | |
| fm = "\n".join([ | |
| "name: tagnorm", | |
| "tags: TagOnly", | |
| ]) | |
| fp = tmp_path / "prompts" / "t.prompt" | |
| _write_prompt_file(fp, fm) | |
| meta = tr.load_template("t") # name defaults to filename when name missing; here 't' | |
| # Our file has name 't' because we set name to "tagnorm"? Note: we set name tagnorm but filename t -> so name is tagnorm | |
| # Actually front matter name present: 't' file with 'name: tagnorm' => load_template by 'tagnorm' | |
| meta = tr.load_template("tagnorm") | |
| assert isinstance(meta["tags"], list) | |
| assert meta["tags"] == ["tagonly"] | |
| # Optional Z3-based formal check for uniqueness invariant | |
| def test_z3_uniqueness_invariant(): | |
| """ | |
| A tiny formal check using Z3 to demonstrate an invariant: given a finite set of template names, | |
| the index should produce unique names. This is an abstract check (not IO-based). | |
| It simply encodes the uniqueness property logically and checks satisfiability for a counterexample. | |
| """ | |
| try: | |
| from z3 import Solver, String, Distinct, sat, Not | |
| except Exception: | |
| pytest.skip("z3 import failed") | |
| # Create three symbolic template names (strings) | |
| a = String("a") | |
| b = String("b") | |
| c = String("c") | |
| s = Solver() | |
| # Suppose there exists an index result where names are not all distinct (i.e., a == b) | |
| s.add(a == b) | |
| s.add(b == "alpha") | |
| s.add(c == "gamma") | |
| # Check satisfiable: yes (counterexample exists), but our intended invariant is that real index produces distinct names. | |
| result = s.check() | |
| assert result == sat | |
| # The point of this test: Z3 runs and confirms the abstract model is coherent. | |
| # Real file-system checks are done in unit tests above. | |
Xet Storage Details
- Size:
- 15.1 kB
- Xet hash:
- 477eee19fc01d6e5b0fd359704b72f68792fa3681c90f77815d98f868b3dbc31
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.