from __future__ import annotations import unittest from backend.dto.workflow_dto import WorkflowNode from backend.services.prompt_plan_schema_service import ( PROMPT_PLAN_SCHEMA_VERSION, node_prompt_plan_view, normalize_prompt_plan, ) class PromptPlanSchemaServiceTests(unittest.TestCase): def test_normalize_prompt_plan_indexes_matrix_by_target_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_prompt_plan( { "profile_id": "profile_001", "profile_name": "Nova", "items": [ { "id": "portrait", "title": "头像节点", "prompt": "Nova, face reference, clear eyes", "negative": "low quality, blurry", "weight": "face identity 1.25", "weightBasis": "lock identity first", "inherits": ["name", "hair", "eyes"], "status": "ready", }, { "id": "fullbody", "title": "全身节点", "prompt": "Nova, full body standing pose", "negative": "bad anatomy", "weight_changes": [{"label": "body proportion", "value": 1.18, "reason": "keep silhouette"}], "conflicts": ["shoe and accessory inheritance needs review"], "status": "review", }, ], "guard_checks": [ {"label": "冲突检查", "value": "shoe and accessory inheritance needs review", "status": "review"} ], }, [headshot, half_body, full_body], ) self.assertEqual(normalized["schema_version"], PROMPT_PLAN_SCHEMA_VERSION) self.assertEqual(normalized["summary"]["target_count"], 2) self.assertEqual(normalized["summary"]["review_count"], 1) self.assertEqual(normalized["summary"]["conflict_count"], 2) self.assertIn("portrait", normalized["by_target"]) self.assertIn("headshot", normalized["by_node_key"]) self.assertIn("full_body", normalized["by_node_key"]) self.assertIn(headshot.node_id, normalized["by_node_id"]) self.assertNotIn(half_body.node_id, normalized["by_node_id"]) headshot_view = node_prompt_plan_view(normalized, headshot) self.assertTrue(headshot_view["has_prompt_plan"]) self.assertEqual(headshot_view["generation_params"]["prompt_patch"], "Nova, face reference, clear eyes") self.assertEqual(headshot_view["generation_params"]["negative_patch"], "low quality, blurry") self.assertEqual(headshot_view["generation_params"]["inheritance_fields"], ["name", "hair", "eyes"]) def test_llm_update_shape_is_accepted(self): headshot = _node("node_headshot", "headshot", "Headshot") normalized = normalize_prompt_plan( { "updates": [ { "target": "portrait", "positive_patch": "clearer gaze, stronger face lock", "negative_patch": "duplicate face", "weight_suggestions": [{"label": "eye color", "value": 1.18}], "inheritance_changes": ["preserve silver hair"], } ], "guardrails": {"negative_prompt": "duplicate face"}, }, [headshot], ) view = node_prompt_plan_view(normalized, headshot) self.assertEqual(view["generation_params"]["prompt_patch"], "clearer gaze, stronger face lock") self.assertEqual(view["generation_params"]["negative_patch"], "duplicate face") self.assertEqual(view["generation_params"]["weight_suggestions"][0]["label"], "eye color") 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()