summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/conftest.py2
-rw-r--r--tests/test_config.py74
-rw-r--r--tests/test_schema.py62
3 files changed, 138 insertions, 0 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index 862b4f5..e4a7c4b 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -14,6 +14,7 @@ from mcp_sd15.config import (
GenerationConfig,
ModelConfig,
OutputConfig,
+ PromptConfig,
ServerConfig,
)
@@ -30,6 +31,7 @@ def app_config(tmp_path: Path) -> AppConfig:
),
output=OutputConfig(tmp_path / "outputs", False),
server=ServerConfig(),
+ prompt=PromptConfig(),
)
diff --git a/tests/test_config.py b/tests/test_config.py
index 9a6bf89..b0dfac0 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -195,3 +195,77 @@ def test_rejects_empty_host(tmp_path: Path) -> None:
)
with pytest.raises(ConfigError, match="host"):
load_config(path, validate_paths=False)
+
+
+def test_prompt_defaults_to_empty_when_omitted(tmp_path: Path) -> None:
+ path = tmp_path / "config.toml"
+ path.write_text(
+ f'[model]\nroot = "{tmp_path.as_posix()}"\ncheckpoint = "base.safetensors"\n',
+ encoding="utf-8",
+ )
+ config = load_config(path, validate_paths=False)
+ assert config.prompt.positive == ""
+ assert config.prompt.negative == ""
+
+
+def test_prompt_reads_files_from_root_prompts(tmp_path: Path) -> None:
+ root = tmp_path / "models"
+ (root / "checkpoints").mkdir(parents=True)
+ (root / "checkpoints" / "base.safetensors").touch()
+ (root / "prompts").mkdir()
+ (root / "prompts" / "positive.txt").write_text("masterpiece, best quality", encoding="utf-8")
+ (root / "prompts" / "negative.txt").write_text("low quality, blurry", encoding="utf-8")
+ path = tmp_path / "config.toml"
+ path.write_text(
+ f'[model]\nroot = "{root.as_posix()}"\ncheckpoint = "base.safetensors"\n'
+ '[prompt]\npositive_file = "positive.txt"\nnegative_file = "negative.txt"\n',
+ encoding="utf-8",
+ )
+ config = load_config(path)
+ assert config.prompt.positive == "masterpiece, best quality"
+ assert config.prompt.negative == "low quality, blurry"
+
+
+def test_prompt_rejects_path_traversal(tmp_path: Path) -> None:
+ root = tmp_path / "models"
+ (root / "checkpoints").mkdir(parents=True)
+ (root / "checkpoints" / "base.safetensors").touch()
+ path = tmp_path / "config.toml"
+ path.write_text(
+ f'[model]\nroot = "{root.as_posix()}"\ncheckpoint = "base.safetensors"\n'
+ '[prompt]\npositive_file = "../escape.txt"\n',
+ encoding="utf-8",
+ )
+ with pytest.raises(ConfigError, match="file name"):
+ load_config(path, validate_paths=False)
+
+
+def test_prompt_rejects_missing_file(tmp_path: Path) -> None:
+ root = tmp_path / "models"
+ (root / "checkpoints").mkdir(parents=True)
+ (root / "checkpoints" / "base.safetensors").touch()
+ path = tmp_path / "config.toml"
+ path.write_text(
+ f'[model]\nroot = "{root.as_posix()}"\ncheckpoint = "base.safetensors"\n'
+ '[prompt]\npositive_file = "absent.txt"\n',
+ encoding="utf-8",
+ )
+ with pytest.raises(ConfigError, match="prompt file"):
+ load_config(path, validate_paths=False)
+
+
+def test_prompt_accepts_only_positive(tmp_path: Path) -> None:
+ root = tmp_path / "models"
+ (root / "checkpoints").mkdir(parents=True)
+ (root / "checkpoints" / "base.safetensors").touch()
+ (root / "prompts").mkdir()
+ (root / "prompts" / "positive.txt").write_text("masterpiece", encoding="utf-8")
+ path = tmp_path / "config.toml"
+ path.write_text(
+ f'[model]\nroot = "{root.as_posix()}"\ncheckpoint = "base.safetensors"\n'
+ '[prompt]\npositive_file = "positive.txt"\n',
+ encoding="utf-8",
+ )
+ config = load_config(path)
+ assert config.prompt.positive == "masterpiece"
+ assert config.prompt.negative == ""
diff --git a/tests/test_schema.py b/tests/test_schema.py
index 15a5f9b..51ec4a2 100644
--- a/tests/test_schema.py
+++ b/tests/test_schema.py
@@ -41,3 +41,65 @@ def test_inpaint_requires_both_images() -> None:
def test_arbitrary_dimensions_are_not_accepted() -> None:
with pytest.raises(InvalidRequestError, match="size"):
TextToImageRequest.create("cat", size="640x480")
+
+
+def test_base_prompts_are_combined_with_user_prompts() -> None:
+ request = TextToImageRequest.create(
+ "a cat",
+ "blurry",
+ base_positive="masterpiece, best quality",
+ base_negative="low quality, watermark",
+ )
+ assert request.positive_prompt == "masterpiece, best quality, a cat"
+ assert request.negative_prompt == "low quality, watermark\nblurry"
+
+
+def test_base_positive_only_does_not_add_trailing_separator() -> None:
+ request = TextToImageRequest.create(
+ "a cat",
+ base_positive="masterpiece",
+ )
+ assert request.positive_prompt == "masterpiece, a cat"
+ assert request.negative_prompt == ""
+
+
+def test_base_negative_only_does_not_add_leading_separator() -> None:
+ request = TextToImageRequest.create(
+ "a cat",
+ "blurry",
+ base_negative="low quality",
+ )
+ assert request.positive_prompt == "a cat"
+ assert request.negative_prompt == "low quality\nblurry"
+
+
+def test_empty_base_prompts_preserve_user_prompts() -> None:
+ request = TextToImageRequest.create("a cat", "blurry")
+ assert request.positive_prompt == "a cat"
+ assert request.negative_prompt == "blurry"
+
+
+def test_guided_request_combines_base_prompts() -> None:
+ request = GuidedImageRequest.create(
+ "a dancer",
+ "openpose",
+ "abc",
+ "deformed",
+ base_positive="masterpiece",
+ base_negative="low quality",
+ )
+ assert request.positive_prompt == "masterpiece, a dancer"
+ assert request.negative_prompt == "low quality\ndeformed"
+
+
+def test_inpaint_request_combines_base_prompts() -> None:
+ request = InpaintImageRequest.create(
+ "a vase",
+ "abc",
+ "def",
+ "artifacts",
+ base_positive="masterpiece",
+ base_negative="low quality",
+ )
+ assert request.positive_prompt == "masterpiece, a vase"
+ assert request.negative_prompt == "low quality\nartifacts"