astrbot_plugin_imagestudio/tests/test_workflow_material_snapshot.py

192 行
6.7 KiB
Python

from __future__ import annotations
import pytest
from backend.dto.common import ApiError
from backend.services.workflow_material_snapshot_service import WorkflowMaterialSnapshotService
from database.db_helper import Database, DatabaseSettings, SqliteSettings
from database.repositories.material_repository import MaterialRepository
from database.repositories.profile_repository import ProfileRepository
from database.repositories.workflow_repository import WorkflowRepository
def _fixture(tmp_path):
database = Database(
DatabaseSettings(
backend="sqlite",
sqlite=SqliteSettings(path=str(tmp_path / "material-snapshot.sqlite3")),
)
)
database.initialize()
profiles = ProfileRepository(database)
profiles.create(
{
"profile_id": "profile-material-snapshot",
"name": "Material snapshot",
"owner_platform": "webui",
"owner_user_id": "system",
"status": "active",
"metadata": {},
}
)
workflows = WorkflowRepository(database)
workflows.create_workflow(
{
"workflow_id": "workflow-material-snapshot",
"profile_id": "profile-material-snapshot",
"workflow_type": "character_gradual",
"display_name": "Material snapshot",
"status": "running",
"current_branch": "main",
"metadata": {},
}
)
workflows.create_node(
{
"node_id": "node-material-snapshot",
"workflow_id": "workflow-material-snapshot",
"node_key": "headshot",
"title": "Headshot",
"status": "draft",
"branch_name": "main",
"sort_order": 40,
"metadata": {},
}
)
materials = MaterialRepository(database)
materials.create_category(
{
"category_id": "category-style",
"name": "风格",
"category_type": "style",
"sort_order": 10,
"metadata": {},
}
)
materials.create_material(
{
"material_id": "material-confirmed",
"category_id": "category-style",
"display_name": "Ink style",
"image_uri": "storage://materials/ink.png",
"thumbnail_uri": "storage://materials/ink-thumb.png",
"prompt": "ink line art",
"negative_prompt": "photorealistic",
"tags": ["ink", "line"],
"recognition_status": "confirmed",
"metadata": {
"body_slot": "style",
"generation_capsule": {"prompt_parts": ["ink line art"]},
"raw_analysis": {"source_payload": "must not be copied"},
"unsafe_preview": "data:image/png;base64,AAAA",
},
}
)
return WorkflowMaterialSnapshotService(workflows, materials), materials
def _bindings(material_id: str = "material-confirmed") -> dict:
return {
"selected_material_ids": [material_id],
"active_bindings": [
{
"material": {"material_id": material_id},
"target_node_keys": ["headshot"],
"reference_strength": {"key": "medium", "weight": 0.55},
}
],
}
def test_freeze_captures_an_immutable_confirmed_material_snapshot(tmp_path) -> None:
service, materials = _fixture(tmp_path)
snapshot = service.freeze(
workflow_id="workflow-material-snapshot",
node_id="node-material-snapshot",
branch_name="main",
material_bindings=_bindings(),
temporary_references=[
{
"temporary_reference_id": "temp-hair",
"display_name": "Hair reference",
"image_uri": "storage://references/hair.png",
"prompt": "preserve silver hair silhouette",
}
],
navigation_context={"round_id": "round-1", "return_view": "generate"},
)
original_hash = snapshot["snapshot_hash"]
materials.update_material(
"material-confirmed",
{"prompt": "changed later", "image_uri": "storage://materials/changed.png"},
)
assert snapshot["material_ids"] == ["material-confirmed"]
assert snapshot["materials"][0]["prompt"] == "ink line art"
assert snapshot["materials"][0]["category_name"] == "风格"
assert "raw_analysis" not in snapshot["materials"][0]["metadata"]
assert "unsafe_preview" not in snapshot["materials"][0]["metadata"]
assert snapshot["temporary_references"][0]["image_uri"] == "storage://references/hair.png"
assert snapshot["navigation_context"]["round_id"] == "round-1"
assert snapshot["snapshot_hash"] == original_hash
def test_unconfirmed_material_is_rejected(tmp_path) -> None:
service, materials = _fixture(tmp_path)
materials.create_material(
{
"material_id": "material-pending",
"display_name": "Pending",
"image_uri": "storage://materials/pending.png",
"recognition_status": "pending_confirm",
"metadata": {},
}
)
with pytest.raises(ApiError) as captured:
service.freeze(
workflow_id="workflow-material-snapshot",
node_id="node-material-snapshot",
branch_name="main",
material_bindings=_bindings("material-pending"),
)
assert captured.value.details["required_action"] == "recognize_and_confirm_material"
def test_inline_data_url_is_never_persisted_in_a_round_snapshot(tmp_path) -> None:
service, _materials = _fixture(tmp_path)
with pytest.raises(ApiError) as captured:
service.freeze(
workflow_id="workflow-material-snapshot",
node_id="node-material-snapshot",
branch_name="main",
material_bindings=_bindings(),
temporary_references=[{"image_uri": "data:image/png;base64,AAAA"}],
)
assert captured.value.details["required_action"] == "upload_or_import_reference_first"
def test_branch_and_navigation_context_must_match_the_active_node(tmp_path) -> None:
service, _materials = _fixture(tmp_path)
with pytest.raises(ApiError):
service.freeze(
workflow_id="workflow-material-snapshot",
node_id="node-material-snapshot",
branch_name="green",
material_bindings=_bindings(),
)
with pytest.raises(ApiError) as navigation_error:
service.freeze(
workflow_id="workflow-material-snapshot",
node_id="node-material-snapshot",
branch_name="main",
material_bindings=_bindings(),
navigation_context={"workflow_id": "another-workflow"},
)
assert navigation_error.value.details["field"] == "workflow_id"