698 行
26 KiB
Python
698 行
26 KiB
Python
from __future__ import annotations
|
|
|
|
import base64
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from backend.dto.common import ApiError
|
|
from backend.dto.job_dto import GenerationTask
|
|
from backend.dto.workflow_dto import WorkflowNode
|
|
from backend.routes.advanced_material_routes import (
|
|
delete_advanced_material_import,
|
|
list_advanced_material_imports,
|
|
list_advanced_materials,
|
|
upload_advanced_material,
|
|
)
|
|
from backend.services.advanced_material_service import (
|
|
AdvancedMaterialService,
|
|
AdvancedMaterialTaskCancelled,
|
|
)
|
|
from backend.services.prompt_governance_service import (
|
|
GenerationRouteService,
|
|
PromptModerationService,
|
|
)
|
|
from backend.services.queue_service import QueueService
|
|
from core.audit_service import AuditService
|
|
from core.config_service import ImageStudioConfig, StorageConfig
|
|
from core.identity import RequestContext
|
|
from core.storage import VfsStorage
|
|
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
|
from database.repositories.advanced_material_repository import AdvancedMaterialRepository
|
|
from database.repositories.job_repository import JobRepository
|
|
from database.repositories.material_repository import MaterialRepository
|
|
from engines.image_provider import (
|
|
GeneratedImagePayload,
|
|
ImageGenerationRequest,
|
|
ImageGenerationResult,
|
|
ImageProviderError,
|
|
)
|
|
from engines.single_flow import ONE_PIXEL_PNG, SingleFlowEngine
|
|
from engines.vision_provider import VisionRecognitionRequest, VisionRecognitionResult
|
|
|
|
|
|
PNG_DATA_URL = f"data:image/png;base64,{base64.b64encode(ONE_PIXEL_PNG).decode('ascii')}"
|
|
|
|
|
|
class _StaticConfigService:
|
|
def __init__(self, root: Path):
|
|
self.config = ImageStudioConfig(
|
|
storage=StorageConfig(
|
|
images_path=str(root / "images"),
|
|
thumbnails_path=str(root / "thumbnails"),
|
|
materials_path=str(root / "materials"),
|
|
advanced_materials_path=str(root / "advanced_materials"),
|
|
styles_path=str(root / "styles"),
|
|
archives_path=str(root / "archives"),
|
|
temp_path=str(root / "temp"),
|
|
)
|
|
)
|
|
|
|
def load(self) -> ImageStudioConfig:
|
|
return self.config
|
|
|
|
|
|
class _FakeVisionProvider:
|
|
provider_id = "fake-vision"
|
|
|
|
def __init__(self, *, prompt: str = "red silk dress, fitted waist, gold embroidery") -> None:
|
|
self.requests: list[VisionRecognitionRequest] = []
|
|
self.prompt = prompt
|
|
|
|
def recognize_material(self, request: VisionRecognitionRequest) -> VisionRecognitionResult:
|
|
self.requests.append(request)
|
|
return VisionRecognitionResult(
|
|
provider_id=self.provider_id,
|
|
model="fake-vision-model",
|
|
prompt=self.prompt,
|
|
negative_prompt="watermark, changed color",
|
|
tags=["dress", "red", "silk"],
|
|
confidence=0.97,
|
|
description="A red silk dress with gold embroidery.",
|
|
structured_data={
|
|
"raw_analysis": {
|
|
"attributes": {
|
|
"color": {
|
|
"value": "red",
|
|
"confidence": 0.99,
|
|
"source_state": "observed",
|
|
},
|
|
"material": {
|
|
"value": "silk",
|
|
"confidence": 0.93,
|
|
"source_state": "observed",
|
|
},
|
|
}
|
|
},
|
|
"normalized_asset_profile": {
|
|
"category": "dress",
|
|
"role": "garment",
|
|
"body_slots": ["upper_body", "waist", "hips", "upper_legs"],
|
|
"wear_layer": "base",
|
|
"must_preserve": ["red color", "gold embroidery"],
|
|
},
|
|
},
|
|
)
|
|
|
|
|
|
class _FakeImageProvider:
|
|
provider_id = "fake-image"
|
|
|
|
def __init__(
|
|
self,
|
|
*,
|
|
failures: int = 0,
|
|
failure_message: str = "controlled fake image failure",
|
|
failure_error_type: str = "network_error",
|
|
failure_status_code: int | None = None,
|
|
) -> None:
|
|
self.failures = failures
|
|
self.failure_message = failure_message
|
|
self.failure_error_type = failure_error_type
|
|
self.failure_status_code = failure_status_code
|
|
self.requests: list[ImageGenerationRequest] = []
|
|
self.on_generate = None
|
|
|
|
def generate_image(self, request: ImageGenerationRequest) -> ImageGenerationResult:
|
|
self.requests.append(request)
|
|
if self.on_generate is not None:
|
|
callback, self.on_generate = self.on_generate, None
|
|
callback()
|
|
if self.failures > 0:
|
|
self.failures -= 1
|
|
raise ImageProviderError(
|
|
self.failure_message,
|
|
provider_id=self.provider_id,
|
|
status_code=self.failure_status_code,
|
|
error_type=self.failure_error_type,
|
|
)
|
|
return ImageGenerationResult(
|
|
provider_id=self.provider_id,
|
|
model=request.model or "fake-image-model",
|
|
images=[
|
|
GeneratedImagePayload(
|
|
b64_json=base64.b64encode(ONE_PIXEL_PNG).decode("ascii"),
|
|
revised_prompt=request.prompt,
|
|
)
|
|
],
|
|
usage={"images": 1},
|
|
raw_metadata={"provider_base_url": "https://fake.invalid"},
|
|
)
|
|
|
|
|
|
class _FakeRuntime:
|
|
def __init__(self, vision: _FakeVisionProvider, image: _FakeImageProvider) -> None:
|
|
self.vision = vision
|
|
self.image = image
|
|
|
|
def vision_provider_for_route(self, _route: dict[str, Any]) -> _FakeVisionProvider:
|
|
return self.vision
|
|
|
|
def image_provider_for_route(self, _route: dict[str, Any]) -> _FakeImageProvider:
|
|
return self.image
|
|
|
|
|
|
class _CapturingSingleFlowEngine(SingleFlowEngine):
|
|
def __init__(self, **kwargs: Any) -> None:
|
|
super().__init__(**kwargs)
|
|
self.requests: list[dict[str, Any]] = []
|
|
|
|
def generate(
|
|
self,
|
|
node: WorkflowNode,
|
|
task: GenerationTask,
|
|
request: dict[str, Any],
|
|
):
|
|
self.requests.append(dict(request))
|
|
return super().generate(node, task, request)
|
|
|
|
|
|
class _RawRouteService:
|
|
def resolve_route(self, route_name: str, *, modality: str | None = None) -> dict[str, Any]:
|
|
assert modality == "image_generation"
|
|
return {
|
|
"route_name": route_name,
|
|
"provider_id": "fake-image",
|
|
"model_id": "fake-image-model",
|
|
"provider": {
|
|
"provider_id": "fake-image",
|
|
"base_url": "https://fake.invalid",
|
|
},
|
|
}
|
|
|
|
|
|
@dataclass
|
|
class _Fixture:
|
|
database: Database
|
|
service: AdvancedMaterialService
|
|
queue: QueueService
|
|
material_repository: MaterialRepository
|
|
advanced_repository: AdvancedMaterialRepository
|
|
storage: VfsStorage
|
|
storage_root: Path
|
|
vision: _FakeVisionProvider
|
|
image: _FakeImageProvider
|
|
engine: _CapturingSingleFlowEngine
|
|
|
|
|
|
def _fixture(
|
|
tmp_path: Path,
|
|
*,
|
|
image_failures: int = 0,
|
|
image_failure_message: str = "controlled fake image failure",
|
|
image_failure_error_type: str = "network_error",
|
|
image_failure_status_code: int | None = None,
|
|
vision_prompt: str = "red silk dress, fitted waist, gold embroidery",
|
|
config: dict[str, Any] | None = None,
|
|
) -> _Fixture:
|
|
database = Database(
|
|
DatabaseSettings(
|
|
backend="sqlite",
|
|
sqlite=SqliteSettings(path=str(tmp_path / "advanced-material-workflow.sqlite3")),
|
|
)
|
|
)
|
|
database.initialize()
|
|
audit = AuditService(database)
|
|
queue = QueueService(JobRepository(database), audit)
|
|
material_repository = MaterialRepository(database)
|
|
advanced_repository = AdvancedMaterialRepository(database)
|
|
storage_root = tmp_path / "vfs"
|
|
storage = VfsStorage(_StaticConfigService(storage_root)) # type: ignore[arg-type]
|
|
vision = _FakeVisionProvider(prompt=vision_prompt)
|
|
image = _FakeImageProvider(
|
|
failures=image_failures,
|
|
failure_message=image_failure_message,
|
|
failure_error_type=image_failure_error_type,
|
|
failure_status_code=image_failure_status_code,
|
|
)
|
|
runtime = _FakeRuntime(vision, image)
|
|
engine = _CapturingSingleFlowEngine(
|
|
storage=storage,
|
|
image_provider_factory=runtime.image_provider_for_route,
|
|
allow_mock_fallback=False,
|
|
)
|
|
service_config: dict[str, Any] = {
|
|
"model_assignments": {
|
|
"advanced_material_recognition": "fake-vision-route",
|
|
"advanced_material_validation_generation": "fake-image-route",
|
|
}
|
|
}
|
|
if config:
|
|
service_config.update(config)
|
|
governance_config = service_config.get("prompt_governance") if isinstance(service_config.get("prompt_governance"), dict) else {}
|
|
moderation_config = governance_config.get("moderation") if isinstance(governance_config.get("moderation"), dict) else {}
|
|
routing_config = governance_config.get("routing") if isinstance(governance_config.get("routing"), dict) else {}
|
|
service = AdvancedMaterialService(
|
|
advanced_repository,
|
|
material_repository,
|
|
queue,
|
|
audit,
|
|
_RawRouteService(), # type: ignore[arg-type]
|
|
runtime, # type: ignore[arg-type]
|
|
config=service_config,
|
|
storage=storage,
|
|
single_flow_engine=engine,
|
|
prompt_moderation_service=PromptModerationService(moderation_config),
|
|
generation_route_service=GenerationRouteService(routing_config),
|
|
)
|
|
return _Fixture(
|
|
database=database,
|
|
service=service,
|
|
queue=queue,
|
|
material_repository=material_repository,
|
|
advanced_repository=advanced_repository,
|
|
storage=storage,
|
|
storage_root=storage_root,
|
|
vision=vision,
|
|
image=image,
|
|
engine=engine,
|
|
)
|
|
|
|
|
|
def _upload_and_run_recognition(
|
|
fixture: _Fixture,
|
|
ctx: RequestContext | None = None,
|
|
) -> tuple[str, Any]:
|
|
ctx = ctx or RequestContext.system("advanced-material-test")
|
|
uploaded = fixture.service.upload(
|
|
{
|
|
"display_name": "red dress",
|
|
"filename": "red-dress.png",
|
|
"content_type": "image/png",
|
|
"image_data_url": PNG_DATA_URL,
|
|
},
|
|
ctx,
|
|
)
|
|
import_id = uploaded["import"]["import_id"]
|
|
recognition_task = fixture.queue.get_job(uploaded["task"]["task_id"])
|
|
fixture.service.execute_task(recognition_task, ctx)
|
|
detail = fixture.service.get_import(import_id)
|
|
return import_id, detail["attempts"][0]
|
|
|
|
|
|
def _run_validation(fixture: _Fixture, attempt: dict[str, Any]) -> dict[str, Any]:
|
|
task = fixture.queue.get_job(attempt["task_id"])
|
|
fixture.service.execute_task(task, RequestContext.system("advanced-material-test"))
|
|
return fixture.service.get_import(attempt["import_id"])
|
|
|
|
|
|
def test_single_flow_advanced_scope_never_writes_ordinary_image_roots(tmp_path: Path) -> None:
|
|
storage_root = tmp_path / "single-flow-vfs"
|
|
storage = VfsStorage(_StaticConfigService(storage_root)) # type: ignore[arg-type]
|
|
provider = _FakeImageProvider()
|
|
engine = SingleFlowEngine(
|
|
storage=storage,
|
|
image_provider_factory=lambda _route: provider,
|
|
allow_mock_fallback=False,
|
|
)
|
|
now = "2026-07-16T00:00:00+00:00"
|
|
task = GenerationTask(
|
|
task_id="task-advanced-output",
|
|
profile_id=None,
|
|
workflow_id=None,
|
|
node_id=None,
|
|
task_type="advanced_material_validation_generation",
|
|
status="queued",
|
|
model_route="fake-image-route",
|
|
progress=0,
|
|
idempotency_key=None,
|
|
request={},
|
|
result={},
|
|
error_message=None,
|
|
metadata={"source": "advanced_material"},
|
|
created_at=now,
|
|
updated_at=now,
|
|
)
|
|
node = WorkflowNode(
|
|
node_id="advanced-material:attempt-1",
|
|
workflow_id="advanced-material:import-1",
|
|
node_key="advanced_material_validation",
|
|
title="validation",
|
|
description=None,
|
|
step_name="validation",
|
|
node_type="generation",
|
|
status="running",
|
|
branch_name="main",
|
|
prompt="red silk dress",
|
|
negative_prompt="watermark",
|
|
model_route="fake-image-route",
|
|
selected_image_id=None,
|
|
sort_order=0,
|
|
metadata={"advanced_material": True},
|
|
created_at=now,
|
|
updated_at=now,
|
|
)
|
|
|
|
assets = engine.generate(
|
|
node,
|
|
task,
|
|
{
|
|
"count": 1,
|
|
"model_route": "fake-image-route",
|
|
"resolved_model_route": {
|
|
"status": "resolved",
|
|
"provider_id": "fake-image",
|
|
"model_id": "fake-image-model",
|
|
},
|
|
"output_scope": {
|
|
"kind": "advanced_material_validation",
|
|
"import_id": "import-1",
|
|
"attempt_id": "attempt-1",
|
|
"attempt_number": 1,
|
|
},
|
|
},
|
|
)
|
|
|
|
assert len(assets) == 1
|
|
assert assets[0].image_uri.startswith("storage://advanced_materials/attempts/import-1/attempt-1/")
|
|
assert assets[0].thumbnail_uri.startswith("storage://advanced_materials/attempts/import-1/attempt-1/")
|
|
assert storage.ensure_file(assets[0].image_uri).is_file()
|
|
assert storage.ensure_file(assets[0].thumbnail_uri).is_file()
|
|
assert not (storage_root / "images").exists()
|
|
assert not (storage_root / "thumbnails").exists()
|
|
|
|
|
|
def test_upload_recognize_validate_reject_revalidate_accept_is_isolated(tmp_path: Path) -> None:
|
|
fixture = _fixture(tmp_path)
|
|
ctx = RequestContext.system("advanced-material-lifecycle")
|
|
|
|
import_id, first_attempt = _upload_and_run_recognition(fixture)
|
|
|
|
assert fixture.vision.requests
|
|
assert fixture.vision.requests[0].image_uri.startswith("data:image/")
|
|
assert first_attempt["attempt_number"] == 1
|
|
assert fixture.service.get_import(import_id)["import"]["status"] == "validation_queued"
|
|
|
|
first_detail = _run_validation(fixture, first_attempt)
|
|
first_attempt = first_detail["attempts"][0]
|
|
assert first_detail["import"]["status"] == "awaiting_review"
|
|
assert first_attempt["status"] == "awaiting_review"
|
|
assert first_attempt["validation_image_uri"].startswith("storage://advanced_materials/")
|
|
assert fixture.image.requests[0].input_images == ()
|
|
assert "input_images" not in fixture.engine.requests[0]
|
|
assert "image" not in fixture.engine.requests[0]
|
|
assert fixture.engine.requests[0]["resolved_model_route"]["status"] == "resolved"
|
|
assert not (fixture.storage_root / "images").exists()
|
|
assert not (fixture.storage_root / "thumbnails").exists()
|
|
|
|
rejected = fixture.service.reject_attempt(
|
|
import_id,
|
|
first_attempt["attempt_id"],
|
|
{
|
|
"adjustment_text": "keep the sleeves longer and preserve the gold embroidery",
|
|
"decision_notes": "sleeves were too short",
|
|
"fidelity_score": 0.55,
|
|
},
|
|
ctx,
|
|
)
|
|
second_attempt = rejected["next_attempt"]
|
|
assert second_attempt["attempt_number"] == 2
|
|
assert "keep the sleeves longer" in second_attempt["prompt"]
|
|
assert rejected["attempt"]["status"] == "rejected"
|
|
|
|
second_detail = _run_validation(fixture, second_attempt)
|
|
second_attempt = second_detail["attempts"][0]
|
|
assert second_attempt["status"] == "awaiting_review"
|
|
assert fixture.image.requests[-1].input_images == ()
|
|
|
|
accepted = fixture.service.accept_attempt(
|
|
import_id,
|
|
second_attempt["attempt_id"],
|
|
{"fidelity_score": 0.96, "decision_notes": "faithful enough"},
|
|
ctx,
|
|
)
|
|
replay = fixture.service.accept_attempt(
|
|
import_id,
|
|
second_attempt["attempt_id"],
|
|
{"fidelity_score": 0.99},
|
|
ctx,
|
|
)
|
|
|
|
assert accepted["material"]["material_id"] == replay["material"]["material_id"]
|
|
assert accepted["material"]["priority"] == 100
|
|
assert accepted["import"]["status"] == "accepted"
|
|
standard_items, standard_total = fixture.material_repository.list_materials(page=1, page_size=10)
|
|
assert standard_items == []
|
|
assert standard_total == 0
|
|
advanced_items, advanced_total = fixture.advanced_repository.list_materials(page=1, page_size=10)
|
|
assert advanced_total == 1
|
|
assert advanced_items[0].material_id == accepted["material"]["material_id"]
|
|
catalog = fixture.service.list_catalog(page=1, page_size=10)
|
|
assert catalog["items"][0]["library_kind"] == "advanced"
|
|
assert catalog["items"][0]["priority"] == 100
|
|
|
|
|
|
def test_validation_failure_records_error_and_can_be_retried(tmp_path: Path) -> None:
|
|
fixture = _fixture(tmp_path, image_failures=1)
|
|
ctx = RequestContext.system("advanced-material-retry")
|
|
import_id, first_attempt = _upload_and_run_recognition(fixture)
|
|
failed_task = fixture.queue.get_job(first_attempt["task_id"])
|
|
|
|
with pytest.raises(ImageProviderError) as captured:
|
|
fixture.service.execute_task(failed_task, ctx)
|
|
fixture.service.handle_task_failure(failed_task, captured.value, ctx)
|
|
|
|
failed = fixture.service.get_import(import_id)
|
|
assert failed["import"]["status"] == "validation_failed"
|
|
assert failed["import"]["error"]["exception_type"] == "ImageProviderError"
|
|
assert failed["attempts"][0]["status"] == "failed"
|
|
assert failed["attempts"][0]["error"]["message"] == "controlled fake image failure"
|
|
|
|
retried = fixture.service.retry_import(import_id, ctx)
|
|
assert retried["attempt"]["attempt_number"] == 2
|
|
assert retried["task"]["status"] == "queued"
|
|
detail = _run_validation(fixture, retried["attempt"])
|
|
assert detail["import"]["status"] == "awaiting_review"
|
|
assert detail["attempts"][0]["status"] == "awaiting_review"
|
|
|
|
|
|
def _adult_routing_config() -> dict[str, Any]:
|
|
return {
|
|
"prompt_governance": {
|
|
"moderation": {"llm_review_enabled": False},
|
|
"routing": {
|
|
"safe_route": "global-safe-route",
|
|
"nsfw_route": "fake-nsfw-route",
|
|
"adult_auto_route_enabled": True,
|
|
"entrypoints": {"web": True},
|
|
},
|
|
},
|
|
"chat_access": {"adult_user_allowlist": ["material-manager"]},
|
|
}
|
|
|
|
|
|
def _material_manager_context() -> RequestContext:
|
|
return RequestContext.from_astrbot(
|
|
username="material-manager",
|
|
user_id="material-manager",
|
|
source="web",
|
|
platform="webui",
|
|
permissions={"can_manage_materials"},
|
|
)
|
|
|
|
|
|
def test_safe_validation_keeps_dedicated_route_instead_of_global_safe_route(tmp_path: Path) -> None:
|
|
fixture = _fixture(tmp_path, config=_adult_routing_config())
|
|
import_id, attempt = _upload_and_run_recognition(fixture, _material_manager_context())
|
|
|
|
detail = _run_validation(fixture, attempt)
|
|
|
|
assert detail["import"]["import_id"] == import_id
|
|
assert fixture.engine.requests[-1]["model_route"] == "fake-image-route"
|
|
assert detail["attempts"][0]["model_route"] == "fake-image-route"
|
|
decision = detail["attempts"][0]["metadata"]["prompt_governance"]["route_decision"]
|
|
assert decision["selected_route"] == "fake-image-route"
|
|
assert decision["global_safe_route_ignored"] == "global-safe-route"
|
|
|
|
|
|
def test_adult_validation_routes_directly_to_nsfw_for_allowlisted_manager(tmp_path: Path) -> None:
|
|
fixture = _fixture(
|
|
tmp_path,
|
|
vision_prompt="adult woman over 18, nude, red silk dress with gold embroidery",
|
|
config=_adult_routing_config(),
|
|
)
|
|
_import_id, attempt = _upload_and_run_recognition(fixture, _material_manager_context())
|
|
|
|
detail = _run_validation(fixture, attempt)
|
|
|
|
assert fixture.engine.requests[-1]["model_route"] == "fake-nsfw-route"
|
|
assert detail["attempts"][0]["model_route"] == "fake-nsfw-route"
|
|
assert detail["attempts"][0]["metadata"]["effective_model_route"] == "fake-nsfw-route"
|
|
|
|
|
|
def test_terror_or_extreme_violence_is_blocked_before_provider_call(tmp_path: Path) -> None:
|
|
fixture = _fixture(
|
|
tmp_path,
|
|
vision_prompt="how to kill a real person with torture gore",
|
|
config=_adult_routing_config(),
|
|
)
|
|
_import_id, attempt = _upload_and_run_recognition(fixture, _material_manager_context())
|
|
task = fixture.queue.get_job(attempt["task_id"])
|
|
|
|
with pytest.raises(ApiError) as captured:
|
|
fixture.service.execute_task(task, RequestContext.system("advanced-material-worker"))
|
|
|
|
assert captured.value.details["moderation_level"] == "violence_prohibited"
|
|
assert fixture.image.requests == []
|
|
|
|
|
|
def test_generic_content_safety_failure_does_not_blindly_route_to_nsfw(tmp_path: Path) -> None:
|
|
fixture = _fixture(
|
|
tmp_path,
|
|
image_failures=1,
|
|
image_failure_message="provider content safety rejection",
|
|
image_failure_error_type="http_error",
|
|
image_failure_status_code=400,
|
|
config=_adult_routing_config(),
|
|
)
|
|
_import_id, attempt = _upload_and_run_recognition(fixture, _material_manager_context())
|
|
task = fixture.queue.get_job(attempt["task_id"])
|
|
|
|
with pytest.raises(ImageProviderError):
|
|
fixture.service.execute_task(task, RequestContext.system("advanced-material-worker"))
|
|
|
|
assert len(fixture.image.requests) == 1
|
|
assert len(fixture.engine.requests) == 1
|
|
|
|
|
|
def test_explicit_nsfw_provider_rejection_falls_back_once_and_attributes_route(tmp_path: Path) -> None:
|
|
fixture = _fixture(
|
|
tmp_path,
|
|
image_failures=1,
|
|
image_failure_message="NSFW content policy blocked prompt",
|
|
image_failure_error_type="http_error",
|
|
image_failure_status_code=400,
|
|
config=_adult_routing_config(),
|
|
)
|
|
_import_id, attempt = _upload_and_run_recognition(fixture, _material_manager_context())
|
|
|
|
detail = _run_validation(fixture, attempt)
|
|
|
|
assert len(fixture.image.requests) == 2
|
|
assert [request["model_route"] for request in fixture.engine.requests] == [
|
|
"fake-image-route",
|
|
"fake-nsfw-route",
|
|
]
|
|
completed = detail["attempts"][0]
|
|
assert completed["model_route"] == "fake-nsfw-route"
|
|
assert completed["metadata"]["route_fallback"]["failure_class"]["safety_reason"] == "adult"
|
|
|
|
|
|
def test_late_validation_result_cannot_revive_cancelled_import_and_files_are_removed(tmp_path: Path) -> None:
|
|
fixture = _fixture(tmp_path)
|
|
ctx = RequestContext.system("advanced-material-cancel")
|
|
import_id, attempt = _upload_and_run_recognition(fixture, ctx)
|
|
task = fixture.queue.get_job(attempt["task_id"])
|
|
fixture.image.on_generate = lambda: fixture.service.cancel_import(import_id, ctx)
|
|
|
|
with pytest.raises(AdvancedMaterialTaskCancelled):
|
|
fixture.service.execute_task(task, RequestContext.system("advanced-material-worker"))
|
|
|
|
detail = fixture.service.get_import(import_id)
|
|
cancelled = detail["attempts"][0]
|
|
assert detail["import"]["status"] == "cancelled"
|
|
assert cancelled["status"] == "cancelled"
|
|
assert cancelled["usage"] == {"images": 1}
|
|
attempt_root = fixture.storage_root / "advanced_materials" / "attempts"
|
|
assert not attempt_root.exists() or not any(path.is_file() for path in attempt_root.rglob("*"))
|
|
|
|
|
|
def test_negative_constraints_do_not_trigger_positive_prompt_rules() -> None:
|
|
moderation = PromptModerationService({}).moderate(
|
|
original_prompt="adult fashion portrait in a red silk dress",
|
|
final_prompt="adult fashion portrait in a red silk dress",
|
|
negative_prompt="nudity, gore, child, watermark",
|
|
)
|
|
|
|
assert moderation.level == "safe"
|
|
|
|
|
|
def test_enabled_llm_review_without_reviewer_fails_closed() -> None:
|
|
moderation = PromptModerationService(
|
|
{"llm_review_enabled": True, "failure_policy": "fail_closed"}
|
|
).moderate(
|
|
original_prompt="safe fashion portrait",
|
|
final_prompt="safe fashion portrait",
|
|
)
|
|
|
|
assert moderation.level == "prohibited"
|
|
assert "moderation_service_unavailable" in moderation.reason_codes
|
|
|
|
|
|
class _DenyCostGuard:
|
|
def check_generation_budget(self, **_payload: Any) -> dict[str, Any]:
|
|
return {"allowed": False, "violations": ["task_budget_exceeded"]}
|
|
|
|
|
|
def test_advanced_material_budget_guard_blocks_before_provider_call(tmp_path: Path) -> None:
|
|
fixture = _fixture(
|
|
tmp_path,
|
|
config={
|
|
"advanced_materials": {
|
|
"cost_budget": {
|
|
"enabled": True,
|
|
"days": 30,
|
|
"max_task_count": 1,
|
|
}
|
|
}
|
|
},
|
|
)
|
|
fixture.service.cost_guard_service = _DenyCostGuard() # type: ignore[assignment]
|
|
_import_id, attempt = _upload_and_run_recognition(fixture)
|
|
task = fixture.queue.get_job(attempt["task_id"])
|
|
|
|
with pytest.raises(ApiError) as captured:
|
|
fixture.service.execute_task(task, RequestContext.system("advanced-material-worker"))
|
|
|
|
assert "budget exceeded" in captured.value.message
|
|
assert fixture.image.requests == []
|
|
|
|
|
|
class _RouteServiceStub:
|
|
def list_imports(self, **_query: Any) -> dict[str, Any]:
|
|
return {"items": [], "total": 0, "page": 1, "page_size": 30}
|
|
|
|
def list_materials(self, **_query: Any) -> dict[str, Any]:
|
|
return {"items": [], "total": 0, "page": 1, "page_size": 30}
|
|
|
|
def upload(self, _payload: dict[str, Any], _ctx: RequestContext) -> dict[str, Any]:
|
|
raise AssertionError("upload must not run without can_manage_materials")
|
|
|
|
def delete_import(self, import_id: str, _ctx: RequestContext) -> dict[str, Any]:
|
|
return {"import_id": import_id, "deleted": True}
|
|
|
|
|
|
def test_advanced_material_routes_enforce_manager_and_reader_permissions() -> None:
|
|
service = _RouteServiceStub()
|
|
anonymous = RequestContext()
|
|
authenticated_reader = RequestContext.from_astrbot(
|
|
username="reader",
|
|
user_id="reader",
|
|
permissions=set(),
|
|
)
|
|
manager = RequestContext.from_astrbot(
|
|
username="manager",
|
|
user_id="manager",
|
|
permissions={"can_manage_materials"},
|
|
)
|
|
|
|
assert list_advanced_material_imports(anonymous, service=service)["error_code"] == "UNAUTHORIZED"
|
|
assert list_advanced_material_imports(authenticated_reader, service=service)["error_code"] == "FORBIDDEN"
|
|
assert upload_advanced_material(authenticated_reader, {}, service=service)["error_code"] == "FORBIDDEN"
|
|
assert delete_advanced_material_import(authenticated_reader, "import-1", service=service)["error_code"] == "FORBIDDEN"
|
|
assert list_advanced_materials(anonymous, service=service)["error_code"] == "UNAUTHORIZED"
|
|
assert list_advanced_materials(authenticated_reader, service=service)["ok"] is True
|
|
assert list_advanced_material_imports(manager, service=service)["ok"] is True
|
|
assert delete_advanced_material_import(manager, "import-1", service=service)["data"]["deleted"] is True
|