summaryrefslogtreecommitdiff
path: root/tests/test_config.py
diff options
context:
space:
mode:
authortanaka takashi <isazaka@gmail.com>2026-09-12 18:04:56 +0900
committertanaka takashi <isazaka@gmail.com>2026-09-12 18:04:56 +0900
commitcba15a62829a0fe57a03c8031f981e8f790e2efc (patch)
tree3bc0ef1a243f356f55c2fa97f63c3bfd5c77e8fc /tests/test_config.py
feat: Stable Diffusion 1.x MCPサーバを実装
仕様書・設計書に基づき、ローカルCUDA GPU上のStable Diffusion 1.x互換モデルをMCP経由で利用するサーバを新規実装。 実装内容: - generate_image、generate_guided_image、inpaint_imageの3ツールをMCP SDK v2で公開 - TOMLからcheckpoint、VAE、複数LoRAと重み、ControlNet、生成設定を読み込み - モデル種別ごとの固定ディレクトリ解決と起動時の設定・ファイル存在検証を追加 - Depth/MiDaS、OpenPose body/full、ControlNet Inpaintの前処理とパイプライン切替を実装 - FP16/CUDA、1枚固定生成、ControlNetオンデマンドロード、単一GPU直列実行を実装 - Base64 PNG/JPEG入力の形式・サイズ検証と任意のdata URI入力に対応 - seed、サイズ、モード、時刻、処理時間をPNG metadataへ記録 - 任意のローカルPNG保存、生成エラー分類、CUDA OOM後のキャッシュ回復を追加 - --configと--versionを備えたstdio CLIエントリポイントを追加 開発・運用: - Python 3.11およびuvによる依存・lock管理を構成 - config.example.tomlとセットアップ・運用手順をREADMEへ追加 - Ruff、Pyright、pytestの設定を追加 - 設定、パス解決、schema、画像検証、サービスルーティング、排他制御、MCP画像返却をテスト 検証結果: - pytest: 30 passed - Ruff lint/format: passed - Pyright: 0 errors, 0 warnings - CLI versionおよびMCPクライアント経由のPNG返却を確認 - 実GPU smoke testはCUDA GPUとモデルファイルがないため対象外
Diffstat (limited to 'tests/test_config.py')
-rw-r--r--tests/test_config.py122
1 files changed, 122 insertions, 0 deletions
diff --git a/tests/test_config.py b/tests/test_config.py
new file mode 100644
index 0000000..5e4341a
--- /dev/null
+++ b/tests/test_config.py
@@ -0,0 +1,122 @@
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+
+from mcp_sd15.config import ConfigError, load_config
+
+
+def make_models(root: Path) -> None:
+ for folder, names in {
+ "checkpoints": ["base.safetensors"],
+ "vae": ["vae.safetensors"],
+ "lora": ["a.safetensors", "b.safetensors"],
+ "controlnet": ["depth.safetensors", "pose.safetensors", "inpaint.safetensors"],
+ }.items():
+ destination = root / folder
+ destination.mkdir(parents=True)
+ for name in names:
+ (destination / name).touch()
+
+
+def valid_toml(root: Path) -> str:
+ return f'''[model]
+root = "{root.as_posix()}"
+checkpoint = "base.safetensors"
+vae = "vae.safetensors"
+[[model.lora]]
+file = "a.safetensors"
+weight = 0.8
+[[model.lora]]
+file = "b.safetensors"
+weight = -0.2
+[generation]
+steps = 20
+cfg_scale = 6.5
+scheduler = "euler_a"
+default_size = "portrait_512"
+[controlnet.depth]
+enabled = true
+model = "depth.safetensors"
+preprocessor = "midas"
+[controlnet.openpose]
+enabled = true
+model = "pose.safetensors"
+[controlnet.inpaint]
+enabled = true
+model = "inpaint.safetensors"
+[output]
+enabled = true
+directory = "generated"
+'''
+
+
+def test_loads_and_resolves_all_model_paths(tmp_path: Path) -> None:
+ root = tmp_path / "models"
+ make_models(root)
+ path = tmp_path / "config.toml"
+ path.write_text(valid_toml(root), encoding="utf-8")
+
+ config = load_config(path)
+
+ assert config.model.checkpoint_path() == root / "checkpoints" / "base.safetensors"
+ assert config.model.vae_path() == root / "vae" / "vae.safetensors"
+ assert config.model.lora_paths() == (
+ root / "lora" / "a.safetensors",
+ root / "lora" / "b.safetensors",
+ )
+ assert config.generation.default_size == "portrait_512"
+ assert config.output.directory == tmp_path / "generated"
+ assert config.controlnet_path("openpose_full") == root / "controlnet" / "pose.safetensors"
+
+
+@pytest.mark.parametrize(
+ "line, message",
+ [
+ ('checkpoint = "../base.safetensors"', "file name"),
+ ("steps = 0", "steps"),
+ ('scheduler = "unknown"', "scheduler"),
+ ('default_size = "1024"', "default_size"),
+ ('preprocessor = "zoe"', "preprocessor"),
+ ],
+)
+def test_rejects_invalid_values(tmp_path: Path, line: str, message: str) -> None:
+ root = tmp_path / "models"
+ make_models(root)
+ text = valid_toml(root)
+ if line.startswith("checkpoint"):
+ text = text.replace('checkpoint = "base.safetensors"', line)
+ elif line.startswith("steps"):
+ text = text.replace("steps = 20", line)
+ elif line.startswith("scheduler"):
+ text = text.replace('scheduler = "euler_a"', line)
+ elif line.startswith("default_size"):
+ text = text.replace('default_size = "portrait_512"', line)
+ else:
+ text = text.replace('preprocessor = "midas"', line)
+ path = tmp_path / "bad.toml"
+ path.write_text(text, encoding="utf-8")
+
+ with pytest.raises(ConfigError, match=message):
+ load_config(path)
+
+
+def test_missing_model_fails_startup_validation(tmp_path: Path) -> None:
+ root = tmp_path / "models"
+ make_models(root)
+ (root / "controlnet" / "depth.safetensors").unlink()
+ path = tmp_path / "config.toml"
+ path.write_text(valid_toml(root), encoding="utf-8")
+
+ with pytest.raises(ConfigError, match=r"depth\.safetensors"):
+ load_config(path)
+
+
+def test_can_skip_path_checks_for_schema_only_use(tmp_path: Path) -> None:
+ path = tmp_path / "config.toml"
+ path.write_text(
+ '[model]\nroot = "missing"\ncheckpoint = "base.safetensors"\n', encoding="utf-8"
+ )
+ config = load_config(path, validate_paths=False)
+ assert config.model.root == (tmp_path / "missing").resolve()