|
| 1 | +from datetime import datetime, timezone |
| 2 | +from types import SimpleNamespace |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | +from uuid import uuid4 |
| 5 | + |
| 6 | +import pytest |
| 7 | +from fastapi import HTTPException |
| 8 | +from pydantic import ValidationError |
| 9 | + |
| 10 | +from apps.gateway.api.v1.endpoints import workflow as workflow_endpoint |
| 11 | + |
| 12 | + |
| 13 | +def _workflow(): |
| 14 | + return SimpleNamespace( |
| 15 | + id=uuid4(), |
| 16 | + organization_id=uuid4(), |
| 17 | + graph={ |
| 18 | + "nodes": [ |
| 19 | + { |
| 20 | + "id": "llm-answer", |
| 21 | + "type": "llmNode", |
| 22 | + "data": { |
| 23 | + "title": "답변", |
| 24 | + "model_id": "gpt-4.1-mini", |
| 25 | + "auto_model_routing": False, |
| 26 | + }, |
| 27 | + } |
| 28 | + ], |
| 29 | + "edges": [], |
| 30 | + }, |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +def _request(): |
| 35 | + return workflow_endpoint.ModelRoutingBootstrapRequest( |
| 36 | + task_description="회사 정책 근거를 비교해 안전한 답변을 작성합니다.", |
| 37 | + default_model_id="gpt-4.1-mini", |
| 38 | + fallback_model_id="gpt-4.1", |
| 39 | + expected_graph_hash="a" * 64, |
| 40 | + expected_updated_at=datetime(2026, 7, 18, tzinfo=timezone.utc), |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def test_model_routing_bootstrap_request_requires_graph_cas_expectation(): |
| 45 | + with pytest.raises(ValidationError): |
| 46 | + workflow_endpoint.ModelRoutingBootstrapRequest( |
| 47 | + task_description="회사 정책 근거를 비교해 안전한 답변을 작성합니다.", |
| 48 | + default_model_id="gpt-4.1-mini", |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def test_model_routing_bootstrap_writes_artifact_and_graph_through_cas(): |
| 53 | + workflow = _workflow() |
| 54 | + current_user = SimpleNamespace(id=uuid4()) |
| 55 | + bootstrap = SimpleNamespace(id=uuid4(), task_fingerprint="fingerprint-v1") |
| 56 | + candidate_models = [ |
| 57 | + SimpleNamespace(model_id="gpt-4.1-mini"), |
| 58 | + SimpleNamespace(model_id="gpt-4.1"), |
| 59 | + ] |
| 60 | + graph_metadata = { |
| 61 | + "graph_hash": "b" * 64, |
| 62 | + "updated_at": "2026-07-18T00:00:01+00:00", |
| 63 | + } |
| 64 | + |
| 65 | + with ( |
| 66 | + patch.object( |
| 67 | + workflow_endpoint, |
| 68 | + "ensure_workflow_permission", |
| 69 | + return_value=workflow, |
| 70 | + ), |
| 71 | + patch.object( |
| 72 | + workflow_endpoint, |
| 73 | + "_lock_workflow_for_cas_graph_write", |
| 74 | + return_value=workflow, |
| 75 | + ) as lock_workflow, |
| 76 | + patch.object( |
| 77 | + workflow_endpoint, |
| 78 | + "_model_routing_candidates_for_user", |
| 79 | + return_value=candidate_models, |
| 80 | + ), |
| 81 | + patch.object( |
| 82 | + workflow_endpoint.PersistedModelRoutingBootstrapStore, |
| 83 | + "create_ready", |
| 84 | + return_value=bootstrap, |
| 85 | + ) as create_ready, |
| 86 | + patch.object( |
| 87 | + workflow_endpoint.PersistedModelRoutingBootstrapStore, |
| 88 | + "public_summary", |
| 89 | + return_value={"id": str(bootstrap.id), "status": "ready"}, |
| 90 | + ), |
| 91 | + patch.object( |
| 92 | + workflow_endpoint, |
| 93 | + "_commit_graph_write_with_canonical_metadata", |
| 94 | + return_value=graph_metadata, |
| 95 | + ) as commit_graph, |
| 96 | + ): |
| 97 | + result = workflow_endpoint.create_model_routing_bootstrap_endpoint( |
| 98 | + str(workflow.id), |
| 99 | + "llm-answer", |
| 100 | + _request(), |
| 101 | + db=MagicMock(), |
| 102 | + current_user=current_user, |
| 103 | + ) |
| 104 | + |
| 105 | + lock_workflow.assert_called_once() |
| 106 | + create_ready.assert_called_once() |
| 107 | + commit_graph.assert_called_once() |
| 108 | + node_data = workflow.graph["nodes"][0]["data"] |
| 109 | + assert node_data["auto_model_routing"] is True |
| 110 | + assert node_data["model_routing_bootstrap_id"] == str(bootstrap.id) |
| 111 | + assert node_data["model_routing_task_description"].startswith("회사 정책") |
| 112 | + assert result == {"id": str(bootstrap.id), "status": "ready", **graph_metadata} |
| 113 | + |
| 114 | + |
| 115 | +def test_model_routing_bootstrap_stale_graph_stops_before_artifact_creation(): |
| 116 | + workflow = _workflow() |
| 117 | + conflict = HTTPException(status_code=409, detail="workflow.graph_conflict") |
| 118 | + |
| 119 | + with ( |
| 120 | + patch.object( |
| 121 | + workflow_endpoint, |
| 122 | + "ensure_workflow_permission", |
| 123 | + return_value=workflow, |
| 124 | + ), |
| 125 | + patch.object( |
| 126 | + workflow_endpoint, |
| 127 | + "_lock_workflow_for_cas_graph_write", |
| 128 | + side_effect=conflict, |
| 129 | + ), |
| 130 | + patch.object( |
| 131 | + workflow_endpoint.PersistedModelRoutingBootstrapStore, |
| 132 | + "create_ready", |
| 133 | + ) as create_ready, |
| 134 | + pytest.raises(HTTPException) as exc_info, |
| 135 | + ): |
| 136 | + workflow_endpoint.create_model_routing_bootstrap_endpoint( |
| 137 | + str(workflow.id), |
| 138 | + "llm-answer", |
| 139 | + _request(), |
| 140 | + db=MagicMock(), |
| 141 | + current_user=SimpleNamespace(id=uuid4()), |
| 142 | + ) |
| 143 | + |
| 144 | + assert exc_info.value.status_code == 409 |
| 145 | + assert exc_info.value.detail == "workflow.graph_conflict" |
| 146 | + create_ready.assert_not_called() |
0 commit comments