54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
import canned_prompts as cp
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture()
|
||
|
|
def package(tmp_path: Path) -> Path:
|
||
|
|
pkg = tmp_path / "pkg"
|
||
|
|
pkg.mkdir()
|
||
|
|
(pkg / "prompt.yaml").write_text(
|
||
|
|
"""\
|
||
|
|
format: canned-prompt/v0.1
|
||
|
|
id: demo/hello
|
||
|
|
name: Hello
|
||
|
|
version: 1.0.0
|
||
|
|
summary: Say hello.
|
||
|
|
template: prompt.md
|
||
|
|
inputs:
|
||
|
|
- name: person
|
||
|
|
required: true
|
||
|
|
parameters:
|
||
|
|
tone:
|
||
|
|
type: enum
|
||
|
|
values: [warm, formal]
|
||
|
|
default: warm
|
||
|
|
""",
|
||
|
|
encoding="utf-8",
|
||
|
|
)
|
||
|
|
(pkg / "prompt.md").write_text(
|
||
|
|
"Say hello to {{ person }} in a {{ tone }} tone.\n", encoding="utf-8"
|
||
|
|
)
|
||
|
|
return pkg
|
||
|
|
|
||
|
|
|
||
|
|
def test_validate_and_render(package: Path) -> None:
|
||
|
|
manifest = cp.validate_package(package)
|
||
|
|
values = cp.resolve_values(manifest, {"person": "Ada"})
|
||
|
|
rendered = cp.render_template((package / "prompt.md").read_text(), values)
|
||
|
|
assert rendered == "Say hello to Ada in a warm tone.\n"
|
||
|
|
|
||
|
|
|
||
|
|
def test_missing_required_input_fails(package: Path) -> None:
|
||
|
|
manifest = cp.validate_package(package)
|
||
|
|
with pytest.raises(cp.CannedPromptError, match="missing required input"):
|
||
|
|
cp.resolve_values(manifest, {})
|
||
|
|
|
||
|
|
|
||
|
|
def test_undeclared_placeholder_fails(package: Path) -> None:
|
||
|
|
(package / "prompt.md").write_text("{{ missing }}\n", encoding="utf-8")
|
||
|
|
with pytest.raises(cp.CannedPromptError, match="undeclared placeholders"):
|
||
|
|
cp.validate_package(package)
|