248 行
10 KiB
Python
248 行
10 KiB
Python
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from backend.dto.common import ApiError, ErrorCode
|
|
from backend.routes.node_routes import generate_node, retry_node
|
|
from backend.services.character_workflow_service import CharacterWorkflowService
|
|
from backend.services.image_service import ImageService
|
|
from backend.services.review_service import ReviewService
|
|
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
|
|
from engines.review_flow import ReviewFlowEngine, ReviewPolicy
|
|
|
|
|
|
def _service(tmp_path) -> tuple[WorkflowService, Database, RequestContext, str]:
|
|
database = Database(
|
|
DatabaseSettings(
|
|
backend="sqlite",
|
|
sqlite=SqliteSettings(path=str(tmp_path / "character-progression.sqlite3")),
|
|
)
|
|
)
|
|
database.initialize()
|
|
ctx = RequestContext.system("character-progression-test")
|
|
profiles = ProfileRepository(database)
|
|
profiles.create(
|
|
{
|
|
"profile_id": "profile-progress",
|
|
"name": "Progress test",
|
|
"base_prompt": "one stable character",
|
|
"appearance_prompt": "black hair",
|
|
"style_prompt": "anime",
|
|
"negative_prompt": "low quality",
|
|
"owner_platform": "webui",
|
|
"owner_user_id": "system",
|
|
"status": "active",
|
|
"metadata": {},
|
|
}
|
|
)
|
|
service = WorkflowService(
|
|
workflow_repository=WorkflowRepository(database),
|
|
profile_repository=profiles,
|
|
audit_service=AuditService(database),
|
|
)
|
|
detail = service.create_workflow(
|
|
"profile-progress",
|
|
{
|
|
"workflow_id": "workflow-progress",
|
|
"workflow_type": "character_gradual",
|
|
"display_name": "Detailed character",
|
|
},
|
|
ctx,
|
|
)
|
|
return service, database, ctx, detail.workflow.workflow_id
|
|
|
|
|
|
def _nodes_by_key(service: WorkflowService, workflow_id: str):
|
|
return {node.node_key: node for node in service.list_nodes(workflow_id)}
|
|
|
|
|
|
def _execute_import(service: WorkflowService, workflow_id: str, ctx: RequestContext):
|
|
return CharacterWorkflowService(service).execute_workflow(workflow_id, ctx)
|
|
|
|
|
|
def test_execute_character_only_confirms_import_and_unlocks_profile_brief(tmp_path) -> None:
|
|
service, _database, ctx, workflow_id = _service(tmp_path)
|
|
nodes = _nodes_by_key(service, workflow_id)
|
|
|
|
with pytest.raises(ApiError) as initial_error:
|
|
service.update_node(nodes["profile_brief"].node_id, {"prompt": "too early"}, ctx)
|
|
assert initial_error.value.details["required_action"] == "execute_character_workflow"
|
|
|
|
result = _execute_import(service, workflow_id, ctx)
|
|
|
|
assert result["progression"]["import_confirmed"] is True
|
|
assert result["progression"]["current_node_key"] == "profile_brief"
|
|
assert result["prepared_node_ids"] == [nodes["profile_brief"].node_id]
|
|
assert nodes["prompt_plan"].node_id in result["locked_node_ids"]
|
|
refreshed = _nodes_by_key(service, workflow_id)
|
|
assert refreshed["profile_brief"].prompt
|
|
assert refreshed["prompt_plan"].prompt is None
|
|
with pytest.raises(ApiError) as future_error:
|
|
service.update_node(refreshed["prompt_plan"].node_id, {"prompt": "skip"}, ctx)
|
|
assert future_error.value.details["reason"] == "character_gradual_progression_locked"
|
|
assert future_error.value.details["current_node_key"] == "profile_brief"
|
|
|
|
|
|
def test_planning_metadata_advances_one_node_at_a_time(tmp_path) -> None:
|
|
service, _database, ctx, workflow_id = _service(tmp_path)
|
|
_execute_import(service, workflow_id, ctx)
|
|
nodes = _nodes_by_key(service, workflow_id)
|
|
|
|
service.update_node(nodes["profile_brief"].node_id, {"status": "completed"}, ctx)
|
|
service.ensure_node_action_allowed(nodes["prompt_plan"].node_id, "update")
|
|
with pytest.raises(ApiError):
|
|
service.ensure_node_action_allowed(nodes["material_bind"].node_id, "update")
|
|
|
|
service.update_node(
|
|
nodes["prompt_plan"].node_id,
|
|
{"metadata": {"prompt_plan": {"rows": []}}},
|
|
ctx,
|
|
)
|
|
with pytest.raises(ApiError):
|
|
service.ensure_node_action_allowed(nodes["material_bind"].node_id, "update")
|
|
service.update_node(nodes["prompt_plan"].node_id, {"status": "completed"}, ctx)
|
|
service.ensure_node_action_allowed(nodes["material_bind"].node_id, "update")
|
|
with pytest.raises(ApiError) as completed_edit:
|
|
service.update_node(nodes["prompt_plan"].node_id, {"prompt": "edit after confirm"}, ctx)
|
|
assert completed_edit.value.details["reason"] == "character_gradual_node_already_completed"
|
|
|
|
service.update_node(
|
|
nodes["material_bind"].node_id,
|
|
{"metadata": {"material_bindings": {"selected_material_ids": []}}},
|
|
ctx,
|
|
)
|
|
service.update_node(nodes["material_bind"].node_id, {"status": "completed"}, ctx)
|
|
service.ensure_node_action_allowed(nodes["headshot"].node_id, "generate")
|
|
with pytest.raises(ApiError):
|
|
service.ensure_node_action_allowed(nodes["upper_body"].node_id, "generate")
|
|
|
|
|
|
def test_generation_completion_requires_adoption_before_unlocking_next_node(tmp_path) -> None:
|
|
service, _database, ctx, workflow_id = _service(tmp_path)
|
|
_execute_import(service, workflow_id, ctx)
|
|
nodes = _nodes_by_key(service, workflow_id)
|
|
service.update_node(nodes["profile_brief"].node_id, {"status": "completed"}, ctx)
|
|
service.update_node(nodes["prompt_plan"].node_id, {"metadata": {"prompt_plan": {}}}, ctx)
|
|
service.update_node(nodes["prompt_plan"].node_id, {"status": "completed"}, ctx)
|
|
service.update_node(nodes["material_bind"].node_id, {"metadata": {"material_bindings": {}}}, ctx)
|
|
service.update_node(nodes["material_bind"].node_id, {"status": "completed"}, ctx)
|
|
|
|
service.update_node(nodes["headshot"].node_id, {"status": "running"}, ctx)
|
|
service.update_node(nodes["headshot"].node_id, {"status": "completed"}, ctx)
|
|
service.ensure_node_action_allowed(nodes["headshot"].node_id, "review")
|
|
with pytest.raises(ApiError):
|
|
service.ensure_node_action_allowed(nodes["upper_body"].node_id, "generate")
|
|
|
|
service.update_node(
|
|
nodes["headshot"].node_id,
|
|
{"status": "selected", "selected_image_id": "image-headshot"},
|
|
ctx,
|
|
allow_locked_adoption=True,
|
|
)
|
|
service.ensure_node_action_allowed(nodes["upper_body"].node_id, "generate")
|
|
assert service.get_workflow(workflow_id).workflow.current_node_id == nodes["upper_body"].node_id
|
|
|
|
|
|
def test_locked_future_node_is_rejected_by_generate_retry_review_and_image_mutation(tmp_path) -> None:
|
|
service, database, ctx, workflow_id = _service(tmp_path)
|
|
_execute_import(service, workflow_id, ctx)
|
|
nodes = _nodes_by_key(service, workflow_id)
|
|
future = nodes["headshot"]
|
|
|
|
generated = generate_node(ctx, future.node_id, {}, service=service)
|
|
retried = retry_node(ctx, future.node_id, {}, service=service)
|
|
assert generated["ok"] is False
|
|
assert retried["ok"] is False
|
|
assert generated["details"]["reason"] == "character_gradual_progression_locked"
|
|
assert retried["details"]["reason"] == "character_gradual_progression_locked"
|
|
|
|
review_service = ReviewService(
|
|
service,
|
|
ImageRepository(database),
|
|
review_engine=ReviewFlowEngine(ReviewPolicy()),
|
|
)
|
|
with pytest.raises(ApiError):
|
|
review_service.review_node(future.node_id)
|
|
|
|
images = ImageRepository(database)
|
|
image_service = ImageService(images, WorkflowRepository(database), AuditService(database))
|
|
with pytest.raises(ApiError):
|
|
image_service.create_image(
|
|
{
|
|
"image_id": "image-future-created",
|
|
"profile_id": "profile-progress",
|
|
"workflow_id": workflow_id,
|
|
"node_id": future.node_id,
|
|
"image_uri": "storage://generated/future-created.png",
|
|
},
|
|
ctx,
|
|
)
|
|
image = images.create(
|
|
{
|
|
"image_id": "image-future",
|
|
"profile_id": "profile-progress",
|
|
"workflow_id": workflow_id,
|
|
"node_id": future.node_id,
|
|
"image_uri": "storage://generated/future.png",
|
|
"metadata": {},
|
|
}
|
|
)
|
|
with pytest.raises(ApiError):
|
|
image_service.select_image(image.image_id, ctx)
|
|
|
|
|
|
def test_progression_metadata_and_structure_cannot_be_forged(tmp_path) -> None:
|
|
service, _database, ctx, workflow_id = _service(tmp_path)
|
|
nodes = _nodes_by_key(service, workflow_id)
|
|
|
|
workflow = service.update_workflow(
|
|
workflow_id,
|
|
{"metadata": {"character_execution": {"import_confirmed": True, "executor": "character_workflow"}}},
|
|
ctx,
|
|
)
|
|
assert "character_execution" not in workflow.metadata
|
|
with pytest.raises(ApiError) as completion_error:
|
|
service.update_workflow(workflow_id, {"status": "completed"}, ctx)
|
|
assert completion_error.value.details["reason"] == "character_gradual_workflow_incomplete"
|
|
|
|
_execute_import(service, workflow_id, ctx)
|
|
preserved = service.get_workflow(workflow_id).workflow.metadata["character_execution"]
|
|
cleared = service.update_workflow(workflow_id, {"metadata": None}, ctx)
|
|
assert cleared.metadata["character_execution"] == preserved
|
|
with pytest.raises(ApiError) as structure_error:
|
|
service.update_node(nodes["profile_brief"].node_id, {"sort_order": 999}, ctx)
|
|
assert structure_error.value.details["reason"] == "character_gradual_structure_locked"
|
|
|
|
|
|
def test_legacy_planning_metadata_is_still_recognized(tmp_path) -> None:
|
|
service, _database, _ctx, workflow_id = _service(tmp_path)
|
|
nodes = _nodes_by_key(service, workflow_id)
|
|
service.workflow_repository.update_workflow(
|
|
workflow_id,
|
|
{
|
|
"status": "running",
|
|
"metadata": {
|
|
"character_execution": {
|
|
"executor": "character_workflow",
|
|
"workflow_type": "character_gradual",
|
|
}
|
|
},
|
|
},
|
|
)
|
|
service.workflow_repository.update_node(nodes["profile_brief"].node_id, {"status": "completed"})
|
|
service.workflow_repository.update_node(
|
|
nodes["prompt_plan"].node_id,
|
|
{"metadata": {"prompt_plan": {"rows": []}}},
|
|
)
|
|
service.workflow_repository.update_node(
|
|
nodes["material_bind"].node_id,
|
|
{"metadata": {"material_bindings": {"selected_material_ids": []}}},
|
|
)
|
|
|
|
service.ensure_node_action_allowed(nodes["headshot"].node_id, "generate")
|