astrbot_plugin_imagestudio/tests/test_node_dependency_context_service.py

217 行
9.7 KiB
Python

from __future__ import annotations
import tempfile
import unittest
from pathlib import Path
from backend.routes.node_routes import get_node_dependency_context
from backend.services.image_service import ImageService
from backend.services.node_dependency_context_service import NodeDependencyContextService
from backend.services.profile_service import ProfileService
from backend.services.workflow_service import WorkflowService
from core.audit_service import AuditService
from core.identity import RequestContext
from database.db_helper import Database, DatabaseSettings, SqliteSettings
from database.repositories.image_repository import ImageRepository
from database.repositories.profile_repository import ProfileRepository
from database.repositories.workflow_repository import WorkflowRepository
class NodeDependencyContextServiceTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.database = Database(
DatabaseSettings(sqlite=SqliteSettings(path=str(Path(self.tmp.name) / "imagestudio.db")))
)
self.database.initialize()
self.audit_service = AuditService(self.database)
self.profile_repository = ProfileRepository(self.database)
self.workflow_repository = WorkflowRepository(self.database)
self.image_repository = ImageRepository(self.database)
self.profile_service = ProfileService(self.profile_repository, self.audit_service)
self.workflow_service = WorkflowService(
self.workflow_repository,
self.profile_repository,
self.audit_service,
)
self.image_service = ImageService(self.image_repository, self.workflow_repository, self.audit_service)
self.context_service = NodeDependencyContextService(self.workflow_service, self.image_service)
self.ctx = RequestContext.from_astrbot(
username="admin",
user_id="user_001",
permissions={"can_write"},
)
self.profile = self.profile_service.create_profile({"name": "Nova"}, self.ctx)
def tearDown(self):
self.tmp.cleanup()
def test_context_includes_completed_predecessor_prompts_and_selected_images(self):
detail = self.workflow_service.create_workflow(
self.profile.profile_id,
{"workflow_type": "character_gradual", "display_name": "Gradual"},
self.ctx,
)
first, second, target = detail.nodes[:3]
image = self.image_service.create_image(
{
"node_id": first.node_id,
"image_uri": "storage://images/headshot.png",
"thumbnail_uri": "storage://thumbnails/headshot.png",
"is_selected": True,
"metadata": {"kind": "headshot"},
},
self.ctx,
)
self.workflow_service.update_node(
first.node_id,
{"status": "completed", "prompt": "headshot, silver hair"},
self.ctx,
)
self.workflow_service.update_node(
second.node_id,
{"status": "completed", "prompt": "half body, white blouse"},
self.ctx,
)
context = self.context_service.get_context(target.node_id)
self.assertEqual(context["dependency_count"], 2)
self.assertEqual(context["selected_image_ids"], [image.image_id])
self.assertIn("headshot, silver hair", context["prompt_context"])
self.assertEqual(context["dependencies"][0]["selected_image"]["image_uri"], "storage://images/headshot.png")
self.assertEqual(context["injection_template"]["version"], "2026-07-09.node-injection-schema.v1")
self.assertEqual(context["injection_template"]["template_kind"], "material_bind")
self.assertEqual(context["injection_template"]["language_policy"]["translation_mode"], "backend_llm")
self.assertIn("expected_response_schema", context["injection_template"])
self.assertNotIn(str(self.tmp.name), str(context))
def test_context_includes_node_scoped_material_bindings(self):
detail = self.workflow_service.create_workflow(
self.profile.profile_id,
{"workflow_type": "character_gradual", "display_name": "Gradual"},
self.ctx,
)
material_bind = next(node for node in detail.nodes if node.node_key == "material_bind")
headshot = next(node for node in detail.nodes if node.node_key == "headshot")
half_body = next(node for node in detail.nodes if node.node_key == "half_body")
self.workflow_service.update_node(
material_bind.node_id,
{
"metadata": {
"material_bindings": {
"rules": [
{
"id": "rule_style",
"category": "风格",
"material_id": "mat_style",
"material_label": "Soft light",
"material_prompt": "soft watercolor",
"bind_to_stages": [{"stage_id": "portrait-draft", "stage_title": "头像"}],
"reference_strength": "强",
"status": "ready",
},
{
"id": "rule_clothing",
"category": "服装",
"material_id": "mat_clothing",
"material_label": "White blouse",
"material_prompt": "white lace blouse",
"bind_to_stages": [{"stage_id": "halfbody-draft", "stage_title": "半身像"}],
"reference_strength": "中",
"status": "ready",
},
]
}
}
},
self.ctx,
)
headshot_context = self.context_service.get_context(headshot.node_id)
half_body_context = self.context_service.get_context(half_body.node_id)
self.assertEqual(headshot_context["material_bindings"]["target_node_key"], "headshot")
self.assertEqual(headshot_context["material_bindings"]["generation_params"]["reference_material_ids"], ["mat_style"])
self.assertEqual(half_body_context["material_bindings"]["generation_params"]["reference_material_ids"], ["mat_clothing"])
self.assertEqual(
headshot_context["injection_template"]["material_bindings"]["schema_version"],
"2026-07-09.material-binding-schema.v1",
)
def test_context_includes_node_scoped_prompt_plan(self):
detail = self.workflow_service.create_workflow(
self.profile.profile_id,
{"workflow_type": "character_gradual", "display_name": "Gradual"},
self.ctx,
)
prompt_plan = next(node for node in detail.nodes if node.node_key == "prompt_plan")
headshot = next(node for node in detail.nodes if node.node_key == "headshot")
full_body = next(node for node in detail.nodes if node.node_key == "full_body")
self.workflow_service.update_node(
prompt_plan.node_id,
{
"metadata": {
"prompt_plan": {
"items": [
{
"id": "portrait",
"title": "头像节点",
"prompt": "Nova, face reference, clear eyes",
"negative": "low quality, blurry",
"inherits": ["name", "hair", "eyes"],
"status": "ready",
},
{
"id": "fullbody",
"title": "全身节点",
"prompt": "Nova, full body standing pose",
"negative": "bad anatomy",
"conflicts": ["shoe details need review"],
"status": "review",
},
]
}
}
},
self.ctx,
)
headshot_context = self.context_service.get_context(headshot.node_id)
full_body_context = self.context_service.get_context(full_body.node_id)
self.assertEqual(headshot_context["prompt_plan"]["target_node_key"], "headshot")
self.assertEqual(headshot_context["prompt_plan"]["generation_params"]["prompt_patch"], "Nova, face reference, clear eyes")
self.assertEqual(full_body_context["prompt_plan"]["generation_params"]["negative_patch"], "bad anatomy")
self.assertEqual(
headshot_context["injection_template"]["prompt_plan"]["schema_version"],
"2026-07-09.prompt-plan-schema.v1",
)
def test_dependency_context_route_requires_authentication(self):
detail = self.workflow_service.create_workflow(
self.profile.profile_id,
{"workflow_type": "character_fast", "display_name": "Fast"},
self.ctx,
)
allowed = get_node_dependency_context(
self.ctx,
detail.nodes[1].node_id,
{},
self.context_service,
)
anonymous = get_node_dependency_context(
RequestContext(),
detail.nodes[1].node_id,
{},
self.context_service,
)
self.assertTrue(allowed["ok"])
self.assertFalse(anonymous["ok"])
self.assertEqual(anonymous["error_code"], "UNAUTHORIZED")
if __name__ == "__main__":
unittest.main()