summaryrefslogtreecommitdiff
path: root/tests/test_config.py
diff options
context:
space:
mode:
authortanaka takashi <isazaka@gmail.com>2026-09-12 21:19:16 +0900
committertanaka takashi <isazaka@gmail.com>2026-09-12 21:19:16 +0900
commit796251e5b0da524584c9e81b8ad6e1dcc0931aa3 (patch)
tree783d55ee9c557ebed5bdd32d99366f22f3fc1139 /tests/test_config.py
parent4f3f548b1b55d4c7477250fc3373b3bd6d0d397f (diff)
base prompt結合機能を追加
config.tomlの[prompt]でpositive/negativeのbase promptファイルを指定し、 MCP経由でLLMから渡されたプロンプトと結合してpipelineへ渡す機能を追加する。 プロンプトファイルは<root>/prompts/<file>に固定し、TOMLにはファイル名のみ 指定する。モデルパスと同じ方針でパストラバーサルを拒否する。 結合方式: - positive: base_positive, user_positive の順でカンマ区切り - negative: base_negative\nuser_negative の順で改行区切り - baseが空の場合はuserプロンプトのみ(後方互換) - [prompt]省略時はbase promptなし(後方互換) 結合場所はschema.pyのTextToImageRequest.create()に集約し、GuidedImageRequest とInpaintImageRequestは内部でTextToImageRequest.create()を呼ぶため1箇所の 変更で3ツールすべてに適用される。MCP Tool入力にはbase promptを公開しない。 変更内容: - config.py: PromptConfig dataclass、AppConfig.promptフィールド、 _read_promptヘルパー、load_configの[prompt]読込・validation追加。 positive_file/negative_fileはファイル名のみ受け付け、<root>/prompts/へ解決 - schema.py: _combineヘルパー、TextToImageRequest.create()に base_positive/base_negative引数追加、GuidedImageRequest.create()と InpaintImageRequest.create()から転送 - tools/generate_image.py: service.config.promptからbaseを取りcreate()へ渡す - tools/generate_guided_image.py: 同上 - tools/inpaint_image.py: 同上 - tests/conftest.py: app_config fixtureにprompt=PromptConfig()追加 - tests/test_config.py: [prompt]の正常系・異常系テスト追加 (省略時空、ファイル読込、パストラバーサル拒否、ファイル不存在、positiveのみ) - tests/test_schema.py: 結合ロジックのテスト追加 (base両方、positiveのみ、negativeのみ、base空、guided/inpaint転送) - doc/config.md: §10 [prompt]セクション追記、設定項目一覧表・ディレクトリ構成・ 全設定例更新、既存セクション番号を§11〜§13へ繰り下げ - config.example.toml: [prompt]コメント例追記 - doc/Specifications.md: モデル構成表にBase prompt行追加、推奨ディレクトリに prompts/追記、設定例更新 - doc/Design.md: 設定オブジェクトにPromptConfig追記、モデルパス解決表に prompts/追加 - doc/mcp.md: §2.1にbase prompt結合の記載追記 - README.md: [prompt]設定例と結合規則の説明追記 検証: - ruff format/check、pyright 0 errors、pytest 61 passed - 実GPU・実モデルでstreamable-http経由generate_imageでbase prompt結合を確認 positive='masterpiece, best quality, ..., sharp focus, a small red cube on a white table' negative='low quality, ..., jpeg artifacts\nnoise' base前置・user後置の結合順序が仕様通り機能することをサーバログで検証済み
Diffstat (limited to 'tests/test_config.py')
-rw-r--r--tests/test_config.py74
1 files changed, 74 insertions, 0 deletions
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 == ""