from __future__ import annotations import unittest from backend.dto.workflow_dto import WorkflowNode from backend.services.material_binding_schema_service import ( MATERIAL_BINDING_SCHEMA_VERSION, node_material_binding_view, normalize_material_bindings, ) class MaterialBindingSchemaServiceTests(unittest.TestCase): def test_normalize_material_bindings_indexes_active_rules_by_stage_and_node(self): headshot = _node("node_headshot", "headshot", "Headshot") half_body = _node("node_half_body", "half_body", "Half body") full_body = _node("node_full_body", "full_body", "Full body") normalized = normalize_material_bindings( { "profile_id": "profile_001", "profile_name": "Nova", "selected_material_ids": ["mat_style"], "rules": [ { "id": "rule_style", "category": "风格", "material_id": "mat_style", "material_label": "Soft light", "material_group": "style", "material_prompt": "soft watercolor, clean line art", "material_colors": ["#f7d9e5"], "bind_to_stages": [ {"stage_id": "portrait-draft", "stage_title": "头像"}, {"stage_id": "halfbody-draft", "stage_title": "半身像"}, ], "inheritance_rule": "keep style across portrait and half body", "reference_strength": "强", "required": True, "status": "ready", }, { "id": "rule_clothing", "category": "服装", "material_id": "", "material_prompt": "", "bind_to_stages": [{"stage_id": "body-sheet", "stage_title": "设定图"}], "reference_strength": "中", }, ], }, [headshot, half_body, full_body], ) self.assertEqual(normalized["schema_version"], MATERIAL_BINDING_SCHEMA_VERSION) self.assertFalse(normalized["reference_policy"]["default_use_reference"]) self.assertEqual(normalized["summary"]["rule_count"], 2) self.assertEqual(normalized["summary"]["active_rule_count"], 1) self.assertEqual(normalized["summary"]["disabled_categories"], ["服装"]) self.assertIn("portrait-draft", normalized["by_stage"]) self.assertIn("headshot", normalized["by_node_key"]) self.assertIn("half_body", normalized["by_node_key"]) self.assertIn("node_headshot", normalized["by_node_id"]) self.assertNotIn("node_full_body", normalized["by_node_id"]) half_body_view = node_material_binding_view(normalized, half_body) self.assertTrue(half_body_view["has_references"]) self.assertEqual(half_body_view["active_bindings"][0]["material"]["material_id"], "mat_style") self.assertEqual(half_body_view["generation_params"]["reference_material_ids"], ["mat_style"]) def test_empty_material_rule_means_do_not_use_reference(self): headshot = _node("node_headshot", "headshot", "Headshot") normalized = normalize_material_bindings( { "rules": [ { "category": "构图", "bind_to_stages": [{"stage_id": "portrait-draft", "stage_title": "头像"}], "reference_strength": "中", } ] }, [headshot], ) view = node_material_binding_view(normalized, headshot) self.assertFalse(normalized["summary"]["has_references"]) self.assertFalse(view["has_references"]) self.assertEqual(view["generation_params"]["reference_material_ids"], []) def _node(node_id: str, node_key: str, title: str) -> WorkflowNode: return WorkflowNode( node_id=node_id, workflow_id="workflow_001", node_key=node_key, title=title, description=None, step_name=node_key, node_type="generation", status="draft", branch_name="main", prompt=None, negative_prompt=None, model_route=None, selected_image_id=None, sort_order=1, metadata={}, created_at="2026-01-01T00:00:00Z", updated_at="2026-01-01T00:00:00Z", ) if __name__ == "__main__": unittest.main()