223 行
7.7 KiB
Python
223 行
7.7 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
from backend.dto.common import ApiError
|
||
|
|
from backend.services.cross_review_service import CrossReviewService
|
||
|
|
from backend.services.generation_cross_review_service import GenerationCrossReviewPlanner
|
||
|
|
from backend.services.model_provider_service import ModelProviderService
|
||
|
|
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
||
|
|
from database.repositories.model_provider_repository import ModelProviderRepository
|
||
|
|
|
||
|
|
|
||
|
|
def _fixture(tmp_path):
|
||
|
|
database = Database(
|
||
|
|
DatabaseSettings(
|
||
|
|
backend="sqlite",
|
||
|
|
sqlite=SqliteSettings(path=str(tmp_path / "cross-review.sqlite3")),
|
||
|
|
)
|
||
|
|
)
|
||
|
|
database.initialize()
|
||
|
|
service = ModelProviderService(ModelProviderRepository(database))
|
||
|
|
for provider_id in ("provider-a", "provider-b"):
|
||
|
|
service.upsert_provider(
|
||
|
|
{
|
||
|
|
"provider_id": provider_id,
|
||
|
|
"display_name": provider_id,
|
||
|
|
"provider_type": "openai_compatible",
|
||
|
|
"base_url": f"https://{provider_id}.example.com/v1",
|
||
|
|
"enabled": True,
|
||
|
|
}
|
||
|
|
)
|
||
|
|
routes = [
|
||
|
|
("route-primary", "provider-a", "image-alpha", "image_generation"),
|
||
|
|
("route-primary-alias", "provider-a", "image-alpha", "image_generation"),
|
||
|
|
("route-secondary", "provider-a", "image-beta", "image_generation"),
|
||
|
|
("route-tertiary", "provider-b", "image-gamma", "image_generation"),
|
||
|
|
("route-vision", "provider-b", "vision-only", "vision_recognition"),
|
||
|
|
]
|
||
|
|
for index, (route_name, provider_id, model_id, modality) in enumerate(routes, start=1):
|
||
|
|
service.upsert_route(
|
||
|
|
{
|
||
|
|
"route_id": f"route-{index}",
|
||
|
|
"route_name": route_name,
|
||
|
|
"provider_id": provider_id,
|
||
|
|
"model_id": model_id,
|
||
|
|
"modality": modality,
|
||
|
|
"enabled": True,
|
||
|
|
}
|
||
|
|
)
|
||
|
|
return GenerationCrossReviewPlanner(service)
|
||
|
|
|
||
|
|
|
||
|
|
def test_plan_keeps_primary_and_uses_distinct_image_models(tmp_path) -> None:
|
||
|
|
planner = _fixture(tmp_path)
|
||
|
|
|
||
|
|
plan = planner.plan(
|
||
|
|
primary_route="route-primary",
|
||
|
|
configured_routes=[
|
||
|
|
"route-primary",
|
||
|
|
"route-primary-alias",
|
||
|
|
"route-secondary",
|
||
|
|
"route-tertiary",
|
||
|
|
],
|
||
|
|
source_candidate_id="candidate-primary",
|
||
|
|
source_image_id="image-primary",
|
||
|
|
)
|
||
|
|
|
||
|
|
assert [item["model_route"] for item in plan["candidates"]] == [
|
||
|
|
"route-primary",
|
||
|
|
"route-secondary",
|
||
|
|
"route-tertiary",
|
||
|
|
]
|
||
|
|
assert plan["candidates"][0]["generate"] is False
|
||
|
|
assert plan["candidates"][0]["preserve_when_not_selected"] is True
|
||
|
|
assert plan["duplicate_routes_ignored"] == ["route-primary-alias"]
|
||
|
|
|
||
|
|
|
||
|
|
def test_selected_primary_can_be_regenerated_without_dropping_other_candidates(tmp_path) -> None:
|
||
|
|
planner = _fixture(tmp_path)
|
||
|
|
|
||
|
|
plan = planner.plan(
|
||
|
|
primary_route="route-primary",
|
||
|
|
configured_routes=["route-secondary"],
|
||
|
|
source_candidate_id="candidate-primary",
|
||
|
|
source_image_id="image-primary",
|
||
|
|
selected_for_regeneration=["candidate-primary"],
|
||
|
|
)
|
||
|
|
|
||
|
|
assert plan["candidates"][0]["generate"] is True
|
||
|
|
assert all(item["preserve_when_not_selected"] for item in plan["candidates"])
|
||
|
|
|
||
|
|
|
||
|
|
def test_visual_review_route_cannot_be_used_as_generation_cross_review(tmp_path) -> None:
|
||
|
|
planner = _fixture(tmp_path)
|
||
|
|
|
||
|
|
with pytest.raises(ApiError) as captured:
|
||
|
|
planner.plan(
|
||
|
|
primary_route="route-primary",
|
||
|
|
configured_routes=["route-vision"],
|
||
|
|
source_candidate_id="candidate-primary",
|
||
|
|
source_image_id="image-primary",
|
||
|
|
)
|
||
|
|
|
||
|
|
assert captured.value.details["required_modality"] == "image_generation"
|
||
|
|
|
||
|
|
|
||
|
|
def test_at_least_one_distinct_secondary_model_is_required(tmp_path) -> None:
|
||
|
|
planner = _fixture(tmp_path)
|
||
|
|
|
||
|
|
with pytest.raises(ApiError) as captured:
|
||
|
|
planner.plan(
|
||
|
|
primary_route="route-primary",
|
||
|
|
configured_routes=["route-primary-alias"],
|
||
|
|
source_candidate_id="candidate-primary",
|
||
|
|
source_image_id="image-primary",
|
||
|
|
)
|
||
|
|
|
||
|
|
assert captured.value.details["duplicate_routes"] == ["route-primary-alias"]
|
||
|
|
assert (
|
||
|
|
captured.value.details["required_action"]
|
||
|
|
== "configure_distinct_generation_cross_review_routes"
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def test_visual_quality_routes_are_separate_from_generation_cross_review_routes() -> None:
|
||
|
|
visual = CrossReviewService.__new__(CrossReviewService)
|
||
|
|
visual.config = {
|
||
|
|
"model_assignments": {
|
||
|
|
"visual_review_routes": ["vision-a", "vision-b"],
|
||
|
|
"cross_review_routes": ["legacy-vision"],
|
||
|
|
"generation_cross_review_routes": ["image-a", "image-b"],
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
assert visual._routes() == ["vision-a", "vision-b"]
|
||
|
|
|
||
|
|
|
||
|
|
def test_followup_regenerates_only_explicit_candidates_and_preserves_slot_order(tmp_path) -> None:
|
||
|
|
planner = _fixture(tmp_path)
|
||
|
|
|
||
|
|
plan = planner.plan_followup(
|
||
|
|
candidates=[
|
||
|
|
{
|
||
|
|
"candidate_id": "primary-old",
|
||
|
|
"candidate_index": 1,
|
||
|
|
"image_id": "image-primary",
|
||
|
|
"model_route": "route-primary",
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"candidate_id": "secondary-old",
|
||
|
|
"candidate_index": 2,
|
||
|
|
"image_id": "image-secondary",
|
||
|
|
"model_route": "route-secondary",
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"candidate_id": "tertiary-old",
|
||
|
|
"candidate_index": 3,
|
||
|
|
"image_id": "image-tertiary",
|
||
|
|
"model_route": "route-tertiary",
|
||
|
|
},
|
||
|
|
],
|
||
|
|
selected_for_regeneration=["secondary-old"],
|
||
|
|
)
|
||
|
|
|
||
|
|
assert [item["source_candidate_id"] for item in plan["candidates"]] == [
|
||
|
|
"primary-old",
|
||
|
|
"secondary-old",
|
||
|
|
"tertiary-old",
|
||
|
|
]
|
||
|
|
assert [item["generate"] for item in plan["candidates"]] == [False, True, False]
|
||
|
|
assert plan["candidates"][0]["role"] == "primary_preserved"
|
||
|
|
|
||
|
|
|
||
|
|
def test_followup_rejects_unknown_or_empty_regeneration_selection(tmp_path) -> None:
|
||
|
|
planner = _fixture(tmp_path)
|
||
|
|
candidates = [
|
||
|
|
{
|
||
|
|
"candidate_id": "primary-old",
|
||
|
|
"candidate_index": 1,
|
||
|
|
"image_id": "image-primary",
|
||
|
|
"model_route": "route-primary",
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"candidate_id": "secondary-old",
|
||
|
|
"candidate_index": 2,
|
||
|
|
"image_id": "image-secondary",
|
||
|
|
"model_route": "route-secondary",
|
||
|
|
},
|
||
|
|
]
|
||
|
|
|
||
|
|
with pytest.raises(ApiError) as empty:
|
||
|
|
planner.plan_followup(candidates=candidates, selected_for_regeneration=[])
|
||
|
|
assert empty.value.details["required_action"] == "select_review_candidates"
|
||
|
|
|
||
|
|
with pytest.raises(ApiError) as unknown:
|
||
|
|
planner.plan_followup(candidates=candidates, selected_for_regeneration=["missing"])
|
||
|
|
assert unknown.value.details["unknown_candidate_ids"] == ["missing"]
|
||
|
|
|
||
|
|
|
||
|
|
def test_followup_rejects_duplicate_provider_model_slots(tmp_path) -> None:
|
||
|
|
planner = _fixture(tmp_path)
|
||
|
|
|
||
|
|
with pytest.raises(ApiError) as captured:
|
||
|
|
planner.plan_followup(
|
||
|
|
candidates=[
|
||
|
|
{
|
||
|
|
"candidate_id": "primary-old",
|
||
|
|
"candidate_index": 1,
|
||
|
|
"image_id": "image-primary",
|
||
|
|
"model_route": "route-primary",
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"candidate_id": "alias-old",
|
||
|
|
"candidate_index": 2,
|
||
|
|
"image_id": "image-alias",
|
||
|
|
"model_route": "route-primary-alias",
|
||
|
|
},
|
||
|
|
],
|
||
|
|
selected_for_regeneration=["alias-old"],
|
||
|
|
)
|
||
|
|
|
||
|
|
assert captured.value.details["required_action"] == "configure_distinct_generation_cross_review_routes"
|